Skip to content

Commit 4031b98

Browse files
committed
A GIS implementation for rocksdb.
Summary: This patch stores gps locations in rocksdb. Each object is uniquely identified by an id. Each object has a gps (latitude, longitude) associated with it. The geodb supports looking up an object either by its gps location or by its id. There is a method to retrieve all objects within a circular radius centered at a specified gps location. Test Plan: Simple unit-test attached. Reviewers: leveldb, haobo Reviewed By: haobo CC: leveldb, tecbot, haobo Differential Revision: https://reviews.facebook.net/D15567
1 parent 64ae6e9 commit 4031b98

File tree

5 files changed

+845
-1
lines changed

5 files changed

+845
-1
lines changed

Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ TESTS = \
9393
write_batch_test\
9494
deletefile_test \
9595
table_test \
96-
thread_local_test
96+
thread_local_test \
97+
geodb_test
9798

9899
TOOLS = \
99100
sst_dump \
@@ -366,6 +367,9 @@ merge_test: db/merge_test.o $(LIBOBJECTS) $(TESTHARNESS)
366367
deletefile_test: db/deletefile_test.o $(LIBOBJECTS) $(TESTHARNESS)
367368
$(CXX) db/deletefile_test.o $(LIBOBJECTS) $(TESTHARNESS) $(EXEC_LDFLAGS) -o $@ $(LDFLAGS)
368369

370+
geodb_test: utilities/geodb/geodb_test.o $(LIBOBJECTS) $(TESTHARNESS)
371+
$(CXX) utilities/geodb/geodb_test.o $(LIBOBJECTS) $(TESTHARNESS) $(EXEC_LDFLAGS) -o $@ $(LDFLAGS) $(COVERAGEFLAGS)
372+
369373
$(MEMENVLIBRARY) : $(MEMENVOBJECTS)
370374
rm -f $@
371375
$(AR) -rs $@ $(MEMENVOBJECTS)

include/utilities/geo_db.h

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
2+
// This source code is licensed under the BSD-style license found in the
3+
// LICENSE file in the root directory of this source tree. An additional grant
4+
// of patent rights can be found in the PATENTS file in the same directory.
5+
//
6+
7+
#pragma once
8+
#include <string>
9+
#include <vector>
10+
11+
#include "utilities/stackable_db.h"
12+
#include "rocksdb/status.h"
13+
14+
namespace rocksdb {
15+
16+
//
17+
// Configurable options needed for setting up a Geo database
18+
//
19+
struct GeoDBOptions {
20+
// Backup info and error messages will be written to info_log
21+
// if non-nullptr.
22+
// Default: nullptr
23+
Logger* info_log;
24+
25+
explicit GeoDBOptions(Logger* _info_log = nullptr):info_log(_info_log) { }
26+
};
27+
28+
//
29+
// A position in the earth's geoid
30+
//
31+
class GeoPosition {
32+
public:
33+
double latitude;
34+
double longitude;
35+
36+
explicit GeoPosition(double la = 0, double lo = 0) :
37+
latitude(la), longitude(lo) {
38+
}
39+
};
40+
41+
//
42+
// Description of an object on the Geoid. It is located by a GPS location,
43+
// and is identified by the id. The value associated with this object is
44+
// an opaque string 'value'. Different objects identified by unique id's
45+
// can have the same gps-location associated with them.
46+
//
47+
class GeoObject {
48+
public:
49+
GeoPosition position;
50+
std::string id;
51+
std::string value;
52+
53+
GeoObject() {}
54+
55+
GeoObject(const GeoPosition& pos, const std::string& i,
56+
const std::string& val) :
57+
position(pos), id(i), value(val) {
58+
}
59+
};
60+
61+
//
62+
// Stack your DB with GeoDB to be able to get geo-spatial support
63+
//
64+
class GeoDB : public StackableDB {
65+
public:
66+
// GeoDBOptions have to be the same as the ones used in a previous
67+
// incarnation of the DB
68+
//
69+
// GeoDB owns the pointer `DB* db` now. You should not delete it or
70+
// use it after the invocation of GeoDB
71+
// GeoDB(DB* db, const GeoDBOptions& options) : StackableDB(db) {}
72+
GeoDB(DB* db, const GeoDBOptions& options) : StackableDB(db) {}
73+
virtual ~GeoDB() {}
74+
75+
// Insert a new object into the location database. The object is
76+
// uniquely identified by the id. If an object with the same id already
77+
// exists in the db, then the old one is overwritten by the new
78+
// object being inserted here.
79+
virtual Status Insert(const GeoObject& object) = 0;
80+
81+
// Retrieve the value of the object located at the specified GPS
82+
// location and is identified by the 'id'.
83+
virtual Status GetByPosition(const GeoPosition& pos,
84+
const Slice& id, std::string* value) = 0;
85+
86+
// Retrieve the value of the object identified by the 'id'. This method
87+
// could be potentially slower than GetByPosition
88+
virtual Status GetById(const Slice& id, GeoObject* object) = 0;
89+
90+
// Delete the specified object
91+
virtual Status Remove(const Slice& id) = 0;
92+
93+
// Returns a list of all items within a circular radius from the
94+
// specified gps location. If 'number_of_values' is specified,
95+
// then this call returns at most that many number of objects.
96+
// The radius is specified in 'meters'.
97+
virtual Status SearchRadial(const GeoPosition& pos,
98+
double radius,
99+
std::vector<GeoObject>* values,
100+
int number_of_values = INT_MAX) = 0;
101+
};
102+
103+
} // namespace rocksdb

0 commit comments

Comments
 (0)