瀏覽代碼

prepare multi index support

Patrick Brosi 4 年之前
父節點
當前提交
f6d4834b68
共有 4 個文件被更改,包括 92 次插入62 次删除
  1. 6 0
      CMakeLists.txt
  2. 5 3
      src/osmfixer/FixerMain.cpp
  3. 77 56
      src/osmfixer/server/StatServer.cpp
  4. 4 3
      src/osmfixer/server/StatServer.h

+ 6 - 0
CMakeLists.txt

@@ -69,3 +69,9 @@ else()
69 69
 endif()
70 70
 
71 71
 add_subdirectory(src)
72
+
73
+# install target
74
+install(
75
+  FILES build/osmfixer DESTINATION bin
76
+  PERMISSIONS OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE
77
+)

+ 5 - 3
src/osmfixer/FixerMain.cpp

@@ -26,11 +26,13 @@ int main(int argc, char** argv) {
26 26
     exit(1);
27 27
   }
28 28
 
29
-  StatIdx idx;
30
-  idx.readFromFile(argv[1]);
29
+  std::vector<StatIdx> idx(argc);
30
+  for(int i = 1; i < argc; i++) {
31
+    idx[i].readFromFile(argv[i]);
32
+  }
31 33
 
32 34
   LOG(INFO) << "Starting server...";
33
-  StatServer serv(&idx);
35
+  StatServer serv(idx);
34 36
 
35 37
   LOG(INFO) << "Listening on port " << port;
36 38
   util::http::HttpServer(port, &serv).run();

+ 77 - 56
src/osmfixer/server/StatServer.cpp

@@ -89,40 +89,45 @@ util::http::Answer StatServer::handleHeatMapReq(const Params& pars) const {
89 89
   if (cb.size()) json << cb << "(";
90 90
   json << "{\"ok\":[";
91 91
 
92
-  auto hgOk = _idx->getHeatGridOk(bbox, z);
93 92
   char sep = ' ';
94
-  for (auto cluster : hgOk) {
95
-    json << sep;
96
-    sep = ',';
93
+  for (const auto& idx : _idxs) {
94
+    auto hgOk = idx.getHeatGridOk(bbox, z);
95
+    for (auto cluster : hgOk) {
96
+      json << sep;
97
+      sep = ',';
97 98
 
98
-    json << "[" << cluster.latLng.getY() << "," << cluster.latLng.getX() << ","
99
-         << cluster.size << "]";
99
+      json << "[" << cluster.latLng.getY() << "," << cluster.latLng.getX() << ","
100
+           << cluster.size << "]";
101
+    }
100 102
   }
101 103
 
102 104
   json << "],\"sugg\":[";
103 105
 
104
-  auto hgSugg = _idx->getHeatGridSugg(bbox, z);
105
-
106 106
   sep = ' ';
107
-  for (auto cluster : hgSugg) {
108
-    json << sep;
109
-    sep = ',';
107
+  for (const auto& idx : _idxs) {
108
+    auto hgSugg = idx.getHeatGridSugg(bbox, z);
110 109
 
111
-    json << "[" << cluster.latLng.getY() << "," << cluster.latLng.getX() << ","
112
-         << cluster.size << "]";
110
+    for (auto cluster : hgSugg) {
111
+      json << sep;
112
+      sep = ',';
113
+
114
+      json << "[" << cluster.latLng.getY() << "," << cluster.latLng.getX() << ","
115
+           << cluster.size << "]";
116
+    }
113 117
   }
114 118
 
115 119
   json << "],\"err\":[";
116 120
 
117
-  auto hgErr = _idx->getHeatGridErr(bbox, z);
118
-
119 121
   sep = ' ';
120
-  for (auto cluster : hgErr) {
121
-    json << sep;
122
-    sep = ',';
122
+  for (const auto& idx : _idxs) {
123
+    auto hgErr = idx.getHeatGridErr(bbox, z);
124
+    for (auto cluster : hgErr) {
125
+      json << sep;
126
+      sep = ',';
123 127
 
124
-    json << "[" << cluster.latLng.getY() << "," << cluster.latLng.getX() << ","
125
-         << cluster.size << "]";
128
+      json << "[" << cluster.latLng.getY() << "," << cluster.latLng.getX() << ","
129
+           << cluster.size << "]";
130
+    }
126 131
   }
127 132
 
128 133
   json << "]}";
@@ -143,6 +148,9 @@ util::http::Answer StatServer::handleMapReq(const Params& pars) const {
143 148
   if (pars.count("cb")) cb = pars.find("cb")->second.c_str();
144 149
   auto box = util::split(pars.find("bbox")->second, ',');
145 150
 
151
+  // TODO!
152
+  size_t did = 0;
153
+
146 154
   if (box.size() != 4) throw std::invalid_argument("Invalid request.");
147 155
 
148 156
   double lat1 = atof(box[0].c_str());
@@ -160,31 +168,37 @@ util::http::Answer StatServer::handleMapReq(const Params& pars) const {
160 168
   if (cb.size()) json << cb << "(";
161 169
   json << "{\"stats\":[";
162 170
 
163
-  auto ret = _idx->getStations(bbox);
164 171
   char sep = ' ';
165
-  for (auto stat : ret) {
166
-    json << sep;
167
-    sep = ',';
168
-    printStation(stat, &json);
172
+  for (const auto& idx : _idxs) {
173
+    auto ret = idx.getStations(bbox);
174
+    for (auto stat : ret) {
175
+      json << sep;
176
+      sep = ',';
177
+      printStation(stat, did, &json);
178
+    }
169 179
   }
170 180
 
171 181
   json << "], \"groups\":[";
172
-  auto gret = _idx->getGroups(bbox);
173 182
   sep = ' ';
174
-  for (auto group : gret) {
175
-    if (group->polyStations.size() == 1 && group->osmid < 2) continue;
176
-    json << sep;
177
-    sep = ',';
178
-    printGroup(group, &json);
183
+  for (const auto& idx : _idxs) {
184
+    auto gret = idx.getGroups(bbox);
185
+    for (auto group : gret) {
186
+      if (group->polyStations.size() == 1 && group->osmid < 2) continue;
187
+      json << sep;
188
+      sep = ',';
189
+      printGroup(group, &json);
190
+    }
179 191
   }
180 192
 
181 193
   json << "], \"su\":[";
182
-  auto suggs = _idx->getSuggestions(bbox);
183 194
   sep = ' ';
184
-  for (auto sugg : suggs) {
185
-    json << sep;
186
-    sep = ',';
187
-    printSugg(sugg, &json);
195
+  for (const auto& idx : _idxs) {
196
+    auto suggs = idx.getSuggestions(bbox);
197
+    for (auto sugg : suggs) {
198
+      json << sep;
199
+      sep = ',';
200
+      printSugg(sugg, &json);
201
+    }
188 202
   }
189 203
 
190 204
   json << "]}";
@@ -198,13 +212,13 @@ util::http::Answer StatServer::handleMapReq(const Params& pars) const {
198 212
 }
199 213
 
200 214
 // _____________________________________________________________________________
201
-void StatServer::printStation(const Station* stat, std::ostream* out) const {
215
+void StatServer::printStation(const Station* stat, size_t did, std::ostream* out) const {
202 216
   auto latLng =
203 217
       util::geo::webMercToLatLng<double>(stat->pos.getX(), stat->pos.getY());
204 218
   (*out) << "{\"id\":" << stat->id << ",\"lat\":" << latLng.getY()
205 219
          << ",\"lon\":" << latLng.getX();
206 220
 
207
-  if (stat->origGroup != stat->group || _idx->getGroup(stat->group)->osmid == 1)
221
+  if (stat->origGroup != stat->group || _idxs[did].getGroup(stat->group)->osmid == 1)
208 222
     (*out) << ",\"su\":1";
209 223
 
210 224
   if (stat->attrErrs.size())
@@ -283,7 +297,11 @@ util::http::Answer StatServer::handleGroupReq(const Params& pars) const {
283 297
   if (pars.count("cb")) cb = pars.find("cb")->second.c_str();
284 298
 
285 299
   size_t gid = atol(pars.find("id")->second.c_str());
286
-  auto group = _idx->getGroup(gid);
300
+
301
+  // TODO!
302
+  size_t did=0;
303
+
304
+  auto group = _idxs[did].getGroup(gid);
287 305
 
288 306
   if (!group) return util::http::Answer("404 Not Found", "Group not found.");
289 307
 
@@ -318,7 +336,7 @@ util::http::Answer StatServer::handleGroupReq(const Params& pars) const {
318 336
   for (const auto& sid : group->stations) {
319 337
     json << sep;
320 338
     sep = ',';
321
-    const auto stat = _idx->getStation(sid);
339
+    const auto stat = _idxs[did].getStation(sid);
322 340
     json << "{\"id\":" << sid << ","
323 341
          << "\"osmid\":" << stat->osmid << ","
324 342
          << "\"group\":" << stat->group << ","
@@ -363,7 +381,10 @@ util::http::Answer StatServer::handleStatReq(const Params& pars) const {
363 381
   if (pars.count("cb")) cb = pars.find("cb")->second.c_str();
364 382
 
365 383
   size_t sid = atol(pars.find("id")->second.c_str());
366
-  auto stat = _idx->getStation(sid);
384
+  // TODO!
385
+  size_t did=0;
386
+
387
+  auto stat = _idxs[did].getStation(sid);
367 388
 
368 389
   if (!stat) return util::http::Answer("404 Not Found", "Station not found.");
369 390
 
@@ -400,7 +421,7 @@ util::http::Answer StatServer::handleStatReq(const Params& pars) const {
400 421
   for (const auto& err : stat->attrErrs) {
401 422
     json << sep;
402 423
     sep = ',';
403
-    const auto otherStat = _idx->getStation(err.otherId);
424
+    const auto otherStat = _idxs[did].getStation(err.otherId);
404 425
     json << "{";
405 426
     json << "\"attr\":[\"" << err.attr << "\",\"" << stat->attrs.at(err.attr)[0]
406 427
          << "\"]";
@@ -420,39 +441,39 @@ util::http::Answer StatServer::handleStatReq(const Params& pars) const {
420 441
 
421 442
   // suggestions
422 443
   if (stat->group != stat->origGroup ||
423
-      _idx->getGroup(stat->group)->osmid == 1) {
424
-    if (_idx->getGroup(stat->origGroup)->osmid < 2) {
425
-      if (_idx->getGroup(stat->group)->osmid == 1) {
444
+      _idxs[did].getGroup(stat->group)->osmid == 1) {
445
+    if (_idxs[did].getGroup(stat->origGroup)->osmid < 2) {
446
+      if (_idxs[did].getGroup(stat->group)->osmid == 1) {
426 447
         json << "{\"type\": 1, \"target_gid\":" << stat->group << "}";
427 448
         seper = ',';
428
-      } else if (_idx->getGroup(stat->group)->osmid > 1) {
449
+      } else if (_idxs[did].getGroup(stat->group)->osmid > 1) {
429 450
         json << "{\"type\": 2, \"target_gid\":" << stat->group
430
-             << ",\"target_osm_rel_id\":" << _idx->getGroup(stat->group)->osmid
451
+             << ",\"target_osm_rel_id\":" << _idxs[did].getGroup(stat->group)->osmid
431 452
              << "}";
432 453
         seper = ',';
433 454
       }
434 455
     } else {
435
-      if (_idx->getGroup(stat->group)->osmid == 1 &&
436
-          _idx->getGroup(stat->group)->stations.size() > 1) {
456
+      if (_idxs[did].getGroup(stat->group)->osmid == 1 &&
457
+          _idxs[did].getGroup(stat->group)->stations.size() > 1) {
437 458
         json << "{\"type\": 3,\"orig_gid\":" << stat->origGroup
438 459
              << ",\"orig_osm_rel_id\":"
439
-             << _idx->getGroup(stat->origGroup)->osmid
460
+             << _idxs[did].getGroup(stat->origGroup)->osmid
440 461
              << ",\"target_gid\":" << stat->group << "}";
441 462
         seper = ',';
442
-      } else if (_idx->getGroup(stat->group)->osmid > 1) {
463
+      } else if (_idxs[did].getGroup(stat->group)->osmid > 1) {
443 464
         json << "{\"type\": 4,\"orig_gid\":" << stat->origGroup
444 465
              << ",\"orig_osm_rel_id\":"
445
-             << _idx->getGroup(stat->origGroup)->osmid
466
+             << _idxs[did].getGroup(stat->origGroup)->osmid
446 467
              << ",\"target_gid\":" << stat->group
447
-             << ",\"target_osm_rel_id\":" << _idx->getGroup(stat->group)->osmid
468
+             << ",\"target_osm_rel_id\":" << _idxs[did].getGroup(stat->group)->osmid
448 469
              << "}";
449 470
         seper = ',';
450 471
       } else {
451 472
         json << "{\"type\": 5,\"orig_gid\":" << stat->origGroup
452 473
              << ",\"orig_osm_rel_id\":"
453
-             << _idx->getGroup(stat->origGroup)->osmid
474
+             << _idxs[did].getGroup(stat->origGroup)->osmid
454 475
              << ",\"target_gid\":" << stat->group
455
-             << ",\"target_osm_rel_id\":" << _idx->getGroup(stat->group)->osmid
476
+             << ",\"target_osm_rel_id\":" << _idxs[did].getGroup(stat->group)->osmid
456 477
              << "}";
457 478
         seper = ',';
458 479
       }
@@ -460,7 +481,7 @@ util::http::Answer StatServer::handleStatReq(const Params& pars) const {
460 481
   }
461 482
 
462 483
   for (auto attrErr : stat->attrErrs) {
463
-    const auto otherStat = _idx->getStation(attrErr.otherId);
484
+    const auto otherStat = _idxs[did].getStation(attrErr.otherId);
464 485
     if (otherStat->osmid == stat->osmid) {
465 486
       // fix attributes
466 487
       json << seper << "{\"type\": 6,\"attr\":\"" << attrErr.attr << "\""

+ 4 - 3
src/osmfixer/server/StatServer.h

@@ -16,7 +16,8 @@ typedef std::map<std::string, std::string> Params;
16 16
 
17 17
 class StatServer : public util::http::Handler {
18 18
  public:
19
-  explicit StatServer(const osmfixer::StatIdx* idx) : _idx(idx) {}
19
+  explicit StatServer(const std::vector<osmfixer::StatIdx>& idxs)
20
+      : _idxs(idxs) {}
20 21
 
21 22
   virtual util::http::Answer handle(const util::http::Req& request,
22 23
                                     int connection) const;
@@ -29,11 +30,11 @@ class StatServer : public util::http::Handler {
29 30
   util::http::Answer handleStatReq(const Params& pars) const;
30 31
   util::http::Answer handleGroupReq(const Params& pars) const;
31 32
 
32
-  void printStation(const Station* stat, std::ostream* out) const;
33
+  void printStation(const Station* stat, size_t did, std::ostream* out) const;
33 34
   static void printGroup(const Group* stat, std::ostream* out);
34 35
   static void printSugg(const Suggestion* stat, std::ostream* out);
35 36
 
36
-  const osmfixer::StatIdx* _idx;
37
+  const std::vector<osmfixer::StatIdx>& _idxs;
37 38
 };
38 39
 }  // namespace osmfixer
39 40