-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_spatial_correctness_integration.cpp
More file actions
424 lines (342 loc) · 16.4 KB
/
Copy pathtest_spatial_correctness_integration.cpp
File metadata and controls
424 lines (342 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* ThemisDB | File: test_spatial_correctness_integration.cpp | Version: 0.0.15
* Maturity: 🟢 PRODUCTION-READY | Score: 98/100
* Gap Summary: total=3; TODO=1, Stub=1, Unimpl=0, Mock=1, Sim=0, Debt=0, C=n/a, H=n/a, M=n/a, L=n/a
* Status: Production Ready
* (Automatisch generiert, Änderungen werden überschrieben)
*/
// Integration tests: Spatial index correctness (Production Readiness Checklist)
//
// Validates that SpatialIndexManager returns correct results for the full set
// of spatial query types:
// - searchIntersects (bbox overlap)
// - searchWithin (full containment)
// - searchContains (index entry contains query bbox)
// - searchNearby (radius / haversine)
// - searchKNN (k nearest centroids)
// - searchZRange (elevation band)
// - Negative cases (query outside all entries returns empty)
// - Precision gate (no false positives across a large synthetic dataset)
//
// All tests use a real (temporary) RocksDB instance and the built-in CPU exact
// backend to exercise the full SpatialIndexManager path.
#include <gtest/gtest.h>
#include "index/spatial_index.h"
#include "geo/spatial_backend.h"
#include "storage/rocksdb_wrapper.h"
#include "utils/geo/ewkb.h"
#include <algorithm>
#include <chrono>
#include <filesystem>
#include <memory>
#include <set>
#include <string>
#include <vector>
namespace fs = std::filesystem;
using namespace themis;
using namespace themis::index;
using namespace themis::geo;
// ---------------------------------------------------------------------------
// Fixture
// ---------------------------------------------------------------------------
class SpatialCorrectnessTest : public ::testing::Test {
protected:
static constexpr const char* kTable = "correctness_table";
void SetUp() override {
auto now = std::chrono::high_resolution_clock::now().time_since_epoch().count();
db_path_ = (fs::temp_directory_path() /
("themis_spatial_correctness_" + std::to_string(now))).string();
RocksDBWrapper::Config cfg;
cfg.db_path = db_path_;
cfg.memtable_size_mb = 16;
cfg.block_cache_size_mb = 16;
db_ = std::make_unique<RocksDBWrapper>(cfg);
ASSERT_TRUE(db_->open());
mgr_ = std::make_unique<SpatialIndexManager>(*db_);
// Wire the CPU exact backend for full exact-check coverage.
auto* cpu = getCpuExactBackend();
ASSERT_NE(cpu, nullptr);
mgr_->setExactBackend(cpu);
RTreeConfig rtcfg;
rtcfg.total_bounds = MBR(-180.0, -90.0, 180.0, 90.0);
ASSERT_TRUE(mgr_->createSpatialIndex(kTable, "geometry", rtcfg));
}
void TearDown() override {
mgr_.reset();
db_.reset();
fs::remove_all(db_path_);
}
// ----- helpers -----
static GeoSidecar pointSidecar(double lon, double lat) {
GeoSidecar sc;
sc.mbr = MBR(lon, lat, lon, lat);
sc.centroid = Coordinate(lon, lat);
return sc;
}
static GeoSidecar boxSidecar(double minx, double miny,
double maxx, double maxy) {
GeoSidecar sc;
sc.mbr = MBR(minx, miny, maxx, maxy);
sc.centroid = Coordinate((minx + maxx) * 0.5, (miny + maxy) * 0.5);
return sc;
}
static GeoSidecar pointSidecar3D(double lon, double lat, double z) {
GeoSidecar sc;
sc.mbr = MBR(lon, lat, lon, lat);
sc.centroid = Coordinate(lon, lat);
sc.z_min = z;
sc.z_max = z;
return sc;
}
static bool hasKey(const std::vector<SpatialResult>& v,
const std::string& k) {
return std::any_of(v.begin(), v.end(),
[&](const SpatialResult& r){ return r.primary_key == k; });
}
std::string db_path_;
std::unique_ptr<RocksDBWrapper> db_;
std::unique_ptr<SpatialIndexManager> mgr_;
};
// ===========================================================================
// searchIntersects – correctness
// ===========================================================================
TEST_F(SpatialCorrectnessTest, Intersects_SinglePoint_Inside) {
ASSERT_TRUE(mgr_->insert(kTable, "berlin", pointSidecar(13.4, 52.5)));
auto res = mgr_->searchIntersects(kTable, MBR(12.0, 51.0, 15.0, 54.0));
ASSERT_EQ(res.size(), 1u);
EXPECT_EQ(res[0].primary_key, "berlin");
}
TEST_F(SpatialCorrectnessTest, Intersects_SinglePoint_Outside) {
ASSERT_TRUE(mgr_->insert(kTable, "berlin", pointSidecar(13.4, 52.5)));
auto res = mgr_->searchIntersects(kTable, MBR(0.0, 0.0, 5.0, 5.0));
EXPECT_TRUE(res.empty());
}
TEST_F(SpatialCorrectnessTest, Intersects_MultiplePoints_CorrectSubset) {
ASSERT_TRUE(mgr_->insert(kTable, "berlin", pointSidecar(13.4, 52.5)));
ASSERT_TRUE(mgr_->insert(kTable, "paris", pointSidecar( 2.35, 48.85)));
ASSERT_TRUE(mgr_->insert(kTable, "hamburg", pointSidecar(10.0, 53.6)));
ASSERT_TRUE(mgr_->insert(kTable, "madrid", pointSidecar(-3.7, 40.4)));
// Bbox covering Germany only
auto res = mgr_->searchIntersects(kTable, MBR(6.0, 47.0, 15.0, 55.0));
EXPECT_EQ(res.size(), 2u);
EXPECT_TRUE(hasKey(res, "berlin"));
EXPECT_TRUE(hasKey(res, "hamburg"));
EXPECT_FALSE(hasKey(res, "paris"));
EXPECT_FALSE(hasKey(res, "madrid"));
}
TEST_F(SpatialCorrectnessTest, Intersects_BoxVsBox_Overlapping) {
// Insert a box covering [10,50]-[12,52]
ASSERT_TRUE(mgr_->insert(kTable, "poly_a", boxSidecar(10.0, 50.0, 12.0, 52.0)));
// Query bbox overlaps the inserted box
auto res = mgr_->searchIntersects(kTable, MBR(11.0, 51.0, 14.0, 54.0));
ASSERT_EQ(res.size(), 1u);
EXPECT_EQ(res[0].primary_key, "poly_a");
}
TEST_F(SpatialCorrectnessTest, Intersects_BoxVsBox_Disjoint) {
ASSERT_TRUE(mgr_->insert(kTable, "poly_a", boxSidecar(10.0, 50.0, 12.0, 52.0)));
// Query bbox is completely outside the inserted box
auto res = mgr_->searchIntersects(kTable, MBR(20.0, 60.0, 25.0, 65.0));
EXPECT_TRUE(res.empty());
}
// ===========================================================================
// Insert / remove correctness
// ===========================================================================
TEST_F(SpatialCorrectnessTest, Remove_DisappearsFromResults) {
auto sc = pointSidecar(13.4, 52.5);
ASSERT_TRUE(mgr_->insert(kTable, "berlin", sc));
EXPECT_EQ(mgr_->searchIntersects(kTable, MBR(12.0, 51.0, 15.0, 54.0)).size(), 1u);
ASSERT_TRUE(mgr_->remove(kTable, "berlin", sc));
EXPECT_TRUE(mgr_->searchIntersects(kTable, MBR(12.0, 51.0, 15.0, 54.0)).empty());
}
TEST_F(SpatialCorrectnessTest, Remove_OtherEntriesUnaffected) {
auto sc_b = pointSidecar(13.4, 52.5);
auto sc_p = pointSidecar(2.35, 48.85);
ASSERT_TRUE(mgr_->insert(kTable, "berlin", sc_b));
ASSERT_TRUE(mgr_->insert(kTable, "paris", sc_p));
ASSERT_TRUE(mgr_->remove(kTable, "berlin", sc_b));
auto res = mgr_->searchIntersects(kTable, MBR(-180.0, -90.0, 180.0, 90.0));
EXPECT_EQ(res.size(), 1u);
EXPECT_TRUE(hasKey(res, "paris"));
EXPECT_FALSE(hasKey(res, "berlin"));
}
TEST_F(SpatialCorrectnessTest, Update_MovesEntryToNewLocation) {
auto sc_old = pointSidecar(13.4, 52.5); // Berlin
auto sc_new = pointSidecar(2.35, 48.85); // Paris
ASSERT_TRUE(mgr_->insert(kTable, "city", sc_old));
ASSERT_TRUE(mgr_->update(kTable, "city", sc_old, sc_new));
// Should no longer be in the Berlin area
auto in_berlin = mgr_->searchIntersects(kTable, MBR(12.0, 51.0, 15.0, 54.0));
EXPECT_FALSE(hasKey(in_berlin, "city"));
// Should now be in the Paris area
auto in_paris = mgr_->searchIntersects(kTable, MBR(1.0, 47.0, 4.0, 50.0));
EXPECT_TRUE(hasKey(in_paris, "city"));
}
// ===========================================================================
// searchWithin – point contained in query bbox
// ===========================================================================
TEST_F(SpatialCorrectnessTest, Within_PointStrictlyInside) {
ASSERT_TRUE(mgr_->insert(kTable, "pt", pointSidecar(5.0, 5.0)));
auto res = mgr_->searchWithin(kTable, MBR(0.0, 0.0, 10.0, 10.0));
ASSERT_FALSE(res.empty());
EXPECT_TRUE(hasKey(res, "pt"));
}
TEST_F(SpatialCorrectnessTest, Within_PointOutsideBbox) {
ASSERT_TRUE(mgr_->insert(kTable, "pt", pointSidecar(50.0, 50.0)));
auto res = mgr_->searchWithin(kTable, MBR(0.0, 0.0, 10.0, 10.0));
EXPECT_FALSE(hasKey(res, "pt"));
}
// ===========================================================================
// searchContains – index entry contains the query bbox
// ===========================================================================
TEST_F(SpatialCorrectnessTest, Contains_LargeBoxContainsPoint) {
// Insert a large box [0,0]-[20,20]
ASSERT_TRUE(mgr_->insert(kTable, "big", boxSidecar(0.0, 0.0, 20.0, 20.0)));
// Insert a small box [30,30]-[40,40] that does NOT contain the query point
ASSERT_TRUE(mgr_->insert(kTable, "small", boxSidecar(30.0, 30.0, 40.0, 40.0)));
// Query point (7.0, 7.0) is inside "big" but not inside "small"
auto res = mgr_->searchContains(kTable, 7.0 /*x=lon*/, 7.0 /*y=lat*/);
EXPECT_TRUE(hasKey(res, "big"));
EXPECT_FALSE(hasKey(res, "small"));
}
// ===========================================================================
// searchNearby – radius / haversine (geographic coordinates)
// ===========================================================================
TEST_F(SpatialCorrectnessTest, Nearby_PointWithinRadius) {
// Insert a point at the origin and one 5000 km away (lon=45, lat=0)
// Distance from (0,0) to (45,0) ≈ 5003 km; only the origin should be within 1000 km.
ASSERT_TRUE(mgr_->insert(kTable, "origin", pointSidecar(0.0, 0.0)));
ASSERT_TRUE(mgr_->insert(kTable, "distant", pointSidecar(45.0, 0.0)));
// searchNearby(table, x=lon, y=lat, radius_m)
auto res = mgr_->searchNearby(kTable,
0.0 /*x=lon*/, 0.0 /*y=lat*/,
1'000'000.0 /*1000 km*/);
EXPECT_TRUE(hasKey(res, "origin"));
EXPECT_FALSE(hasKey(res, "distant"));
}
TEST_F(SpatialCorrectnessTest, Nearby_NoPointWithinRadius) {
// Only point at (45, 0); query from origin with 100 km radius
ASSERT_TRUE(mgr_->insert(kTable, "distant", pointSidecar(45.0, 0.0)));
auto res = mgr_->searchNearby(kTable,
0.0 /*x=lon*/, 0.0 /*y=lat*/,
100'000.0 /*100 km*/);
EXPECT_TRUE(res.empty());
}
// ===========================================================================
// searchKNN – k nearest neighbours
// ===========================================================================
TEST_F(SpatialCorrectnessTest, KNN_ReturnExactlyK) {
// Insert 10 European cities
const std::vector<std::pair<std::string, std::pair<double,double>>> cities = {
{"berlin", {13.4, 52.5}},
{"paris", { 2.35, 48.85}},
{"madrid", {-3.7, 40.4}},
{"rome", {12.5, 41.9}},
{"vienna", {16.37, 48.2}},
{"amsterdam", { 4.9, 52.37}},
{"warsaw", {21.0, 52.23}},
{"prague", {14.42, 50.08}},
{"budapest", {19.04, 47.5}},
{"athens", {23.73, 37.98}},
};
for (const auto& c : cities)
ASSERT_TRUE(mgr_->insert(kTable, c.first,
pointSidecar(c.second.first, c.second.second)));
int k = 5;
auto res = mgr_->searchKNN(kTable, 13.4 /*lon=x*/, 52.5 /*lat=y*/, k); // centred on Berlin
EXPECT_EQ((int)res.size(), k);
}
TEST_F(SpatialCorrectnessTest, KNN_ClosestIsNearest) {
ASSERT_TRUE(mgr_->insert(kTable, "close", pointSidecar(1.0, 1.0)));
ASSERT_TRUE(mgr_->insert(kTable, "far", pointSidecar(50.0, 50.0)));
auto res = mgr_->searchKNN(kTable, 1.0 /*lon=x*/, 1.0 /*lat=y*/, 2);
ASSERT_GE(res.size(), 1u);
EXPECT_EQ(res[0].primary_key, "close");
}
// ===========================================================================
// searchZRange – 3-D elevation band
// ===========================================================================
TEST_F(SpatialCorrectnessTest, ZRange_FindsPointInBand) {
// Insert points at different altitudes
ASSERT_TRUE(mgr_->insert(kTable, "sea", pointSidecar3D(10.0, 50.0, 0.0)));
ASSERT_TRUE(mgr_->insert(kTable, "hill", pointSidecar3D(10.0, 50.0, 300.0)));
ASSERT_TRUE(mgr_->insert(kTable, "summit", pointSidecar3D(10.0, 50.0, 800.0)));
// Band 200–500 m → only "hill"
auto res = mgr_->searchZRange(kTable, 200.0, 500.0);
EXPECT_TRUE(hasKey(res, "hill"));
EXPECT_FALSE(hasKey(res, "sea"));
EXPECT_FALSE(hasKey(res, "summit"));
}
// ===========================================================================
// Precision gate: no false positives from a grid of 100 labelled points
// ===========================================================================
TEST_F(SpatialCorrectnessTest, Precision_NoFalsePositives) {
// Lay out a 10×10 grid of points: lon ∈ [0,9], lat ∈ [0,9]
for (int lon = 0; lon < 10; ++lon) {
for (int lat = 0; lat < 10; ++lat) {
std::string pk = "p_" + std::to_string(lon) + "_" + std::to_string(lat);
ASSERT_TRUE(mgr_->insert(kTable, pk, pointSidecar(lon, lat)));
}
}
// Query bbox covering only lon [2,4], lat [3,5]
auto res = mgr_->searchIntersects(kTable, MBR(2.0, 3.0, 4.0, 5.0));
// Every returned entry MUST be inside (or on the border of) the query bbox.
for (const auto& r : res) {
EXPECT_GE(r.mbr.minx, 2.0) << "False positive: " << r.primary_key;
EXPECT_LE(r.mbr.maxx, 4.0) << "False positive: " << r.primary_key;
EXPECT_GE(r.mbr.miny, 3.0) << "False positive: " << r.primary_key;
EXPECT_LE(r.mbr.maxy, 5.0) << "False positive: " << r.primary_key;
}
// Also verify expected points are present (completeness check)
// Points with lon∈{2,3,4} and lat∈{3,4,5} = 9 points
EXPECT_EQ(res.size(), 9u);
}
// ===========================================================================
// Recall gate: a wider query must find all expected entries
// ===========================================================================
TEST_F(SpatialCorrectnessTest, Recall_AllPointsFoundInWorldQuery) {
const std::vector<std::pair<std::string, std::pair<double,double>>> pts = {
{"a", {-90.0, -45.0}},
{"b", { 90.0, 45.0}},
{"c", { 0.0, 0.0}},
{"d", {179.0, 89.0}},
{"e", {-179.0, -89.0}},
};
for (const auto& p : pts)
ASSERT_TRUE(mgr_->insert(kTable, p.first,
pointSidecar(p.second.first, p.second.second)));
auto res = mgr_->searchIntersects(kTable, MBR(-180.0, -90.0, 180.0, 90.0));
EXPECT_EQ(res.size(), pts.size());
for (const auto& p : pts)
EXPECT_TRUE(hasKey(res, p.first)) << "Missing: " << p.first;
}
// ===========================================================================
// Index management: hasSpatialIndex / createSpatialIndex / dropSpatialIndex
// ===========================================================================
TEST_F(SpatialCorrectnessTest, IndexManagement_CreateAndDropCycle) {
const char* kTbl2 = "tmp_table";
ASSERT_FALSE(mgr_->hasSpatialIndex(kTbl2));
ASSERT_TRUE(mgr_->createSpatialIndex(kTbl2));
EXPECT_TRUE(mgr_->hasSpatialIndex(kTbl2));
ASSERT_TRUE(mgr_->dropSpatialIndex(kTbl2));
EXPECT_FALSE(mgr_->hasSpatialIndex(kTbl2));
}
TEST_F(SpatialCorrectnessTest, IndexManagement_SearchOnDroppedIndexReturnsEmpty) {
const char* kTbl3 = "drop_test";
ASSERT_TRUE(mgr_->createSpatialIndex(kTbl3));
ASSERT_TRUE(mgr_->insert(kTbl3, "p1", pointSidecar(5.0, 5.0)));
ASSERT_TRUE(mgr_->dropSpatialIndex(kTbl3));
// After drop the index is gone; search should return empty, not crash.
auto res = mgr_->searchIntersects(kTbl3, MBR(-180.0, -90.0, 180.0, 90.0));
EXPECT_TRUE(res.empty());
}
// ===========================================================================
// Metrics correctness
// ===========================================================================
TEST_F(SpatialCorrectnessTest, Metrics_InsertAndQueryCountsIncrement) {
mgr_->resetMetrics();
const auto& m = mgr_->getMetrics();
ASSERT_TRUE(mgr_->insert(kTable, "p", pointSidecar(1.0, 1.0)));
EXPECT_EQ(m.insert_count.load(), 1u);
mgr_->searchIntersects(kTable, MBR(-10.0, -10.0, 10.0, 10.0));
EXPECT_EQ(m.query_count.load(), 1u);
}