|
| 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