YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/utilities/geodb/geodb_impl.h
Line
Count
Source
1
//  Copyright (c) 2011-present, 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
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
21
#ifndef ROCKSDB_LITE
22
23
#pragma once
24
#include <algorithm>
25
#include <cmath>
26
#include <string>
27
#include <sstream>
28
#include <stdexcept>
29
#include <vector>
30
31
#include "yb/rocksdb/utilities/geo_db.h"
32
#include "yb/rocksdb/utilities/stackable_db.h"
33
#include "yb/rocksdb/env.h"
34
#include "yb/rocksdb/status.h"
35
36
namespace rocksdb {
37
38
// A specific implementation of GeoDB
39
40
class GeoDBImpl : public GeoDB {
41
 public:
42
  GeoDBImpl(DB* db, const GeoDBOptions& options);
43
  ~GeoDBImpl();
44
45
  // Associate the GPS location with the identified by 'id'. The value
46
  // is a blob that is associated with this object.
47
  virtual Status Insert(const GeoObject& object) override;
48
49
  // Retrieve the value of the object located at the specified GPS
50
  // location and is identified by the 'id'.
51
  virtual Status GetByPosition(const GeoPosition& pos, const Slice& id,
52
                               std::string* value) override;
53
54
  // Retrieve the value of the object identified by the 'id'. This method
55
  // could be potentially slower than GetByPosition
56
  virtual Status GetById(const Slice& id, GeoObject* object) override;
57
58
  // Delete the specified object
59
  virtual Status Remove(const Slice& id) override;
60
61
  // Returns a list of all items within a circular radius from the
62
  // specified gps location
63
  virtual GeoIterator* SearchRadial(const GeoPosition& pos, double radius,
64
                                    int number_of_values) override;
65
66
 private:
67
  DB* db_;
68
  const GeoDBOptions options_;
69
  const WriteOptions woptions_;
70
  const ReadOptions roptions_;
71
72
  // MSVC requires the definition for this static const to be in .CC file
73
  // The value of PI
74
  static const double PI;
75
76
  // convert degrees to radians
77
  static double radians(double x);
78
79
  // convert radians to degrees
80
  static double degrees(double x);
81
82
  // A pixel class that captures X and Y coordinates
83
  class Pixel {
84
   public:
85
    unsigned int x;
86
    unsigned int y;
87
    Pixel(unsigned int a, unsigned int b) :
88
19
     x(a), y(b) {
89
19
    }
90
  };
91
92
  // A Tile in the geoid
93
  class Tile {
94
   public:
95
    unsigned int x;
96
    unsigned int y;
97
    Tile(unsigned int a, unsigned int b) :
98
15
     x(a), y(b) {
99
15
    }
100
  };
101
102
  // convert a gps location to quad coordinate
103
  static std::string PositionToQuad(const GeoPosition& pos, int levelOfDetail);
104
105
  // arbitrary constant use for WGS84 via
106
  // http://en.wikipedia.org/wiki/World_Geodetic_System
107
  // http://mathforum.org/library/drmath/view/51832.html
108
  // http://msdn.microsoft.com/en-us/library/bb259689.aspx
109
  // http://www.tuicool.com/articles/NBrE73
110
  //
111
  const int Detail = 23;
112
  // MSVC requires the definition for this static const to be in .CC file
113
  static const double EarthRadius;
114
  static const double MinLatitude;
115
  static const double MaxLatitude;
116
  static const double MinLongitude;
117
  static const double MaxLongitude;
118
119
  // clips a number to the specified minimum and maximum values.
120
57
  static double clip(double n, double minValue, double maxValue) {
121
57
    return fmin(fmax(n, minValue), maxValue);
122
57
  }
123
124
  // Determines the map width and height (in pixels) at a specified level
125
  // of detail, from 1 (lowest detail) to 23 (highest detail).
126
  // Returns the map width and height in pixels.
127
19
  static unsigned int MapSize(int levelOfDetail) {
128
19
    return (unsigned int)(256 << levelOfDetail);
129
19
  }
130
131
  // Determines the ground resolution (in meters per pixel) at a specified
132
  // latitude and level of detail.
133
  // Latitude (in degrees) at which to measure the ground resolution.
134
  // Level of detail, from 1 (lowest detail) to 23 (highest detail).
135
  // Returns the ground resolution, in meters per pixel.
136
  static double GroundResolution(double latitude, int levelOfDetail);
137
138
  // Converts a point from latitude/longitude WGS-84 coordinates (in degrees)
139
  // into pixel XY coordinates at a specified level of detail.
140
  static Pixel PositionToPixel(const GeoPosition& pos, int levelOfDetail);
141
142
  static GeoPosition PixelToPosition(const Pixel& pixel, int levelOfDetail);
143
144
  // Converts a Pixel to a Tile
145
  static Tile PixelToTile(const Pixel& pixel);
146
147
  static Pixel TileToPixel(const Tile& tile);
148
149
  // Convert a Tile to a quadkey
150
  static std::string TileToQuadKey(const Tile& tile, int levelOfDetail);
151
152
  // Convert a quadkey to a tile and its level of detail
153
  static void QuadKeyToTile(std::string quadkey, Tile* tile,
154
                            int *levelOfDetail);
155
156
  // Return the distance between two positions on the earth
157
  static double distance(double lat1, double lon1,
158
                         double lat2, double lon2);
159
  static GeoPosition displaceLatLon(double lat, double lon,
160
                                    double deltay, double deltax);
161
162
  //
163
  // Returns the top left position after applying the delta to
164
  // the specified position
165
  //
166
2
  static GeoPosition boundingTopLeft(const GeoPosition& in, double radius) {
167
2
    return displaceLatLon(in.latitude, in.longitude, -radius, -radius);
168
2
  }
169
170
  //
171
  // Returns the bottom right position after applying the delta to
172
  // the specified position
173
  static GeoPosition boundingBottomRight(const GeoPosition& in,
174
2
                                         double radius) {
175
2
    return displaceLatLon(in.latitude, in.longitude, radius, radius);
176
2
  }
177
178
  //
179
  // Get all quadkeys within a radius of a specified position
180
  //
181
  Status searchQuadIds(const GeoPosition& position,
182
                       double radius,
183
                       std::vector<std::string>* quadKeys);
184
185
  //
186
  // Create keys for accessing rocksdb table(s)
187
  //
188
  static std::string MakeKey1(const GeoPosition& pos,
189
                              Slice id,
190
                              std::string quadkey);
191
  static std::string MakeKey2(Slice id);
192
  static std::string MakeKey1Prefix(std::string quadkey,
193
                                    Slice id);
194
  static std::string MakeQuadKeyPrefix(std::string quadkey);
195
};
196
197
}  // namespace rocksdb
198
199
#endif  // ROCKSDB_LITE