/Users/deen/code/yugabyte-db/src/yb/rocksdb/utilities/geodb/geodb_test.cc
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 | | #ifndef ROCKSDB_LITE |
21 | | #include "yb/rocksdb/utilities/geodb/geodb_impl.h" |
22 | | |
23 | | #include <cctype> |
24 | | #include "yb/rocksdb/util/testharness.h" |
25 | | #include "yb/rocksdb/util/testutil.h" |
26 | | |
27 | | #include "yb/util/test_util.h" |
28 | | |
29 | | namespace rocksdb { |
30 | | |
31 | | class GeoDBTest : public RocksDBTest { |
32 | | public: |
33 | | static const std::string kDefaultDbName; |
34 | | static Options options; |
35 | | DB* db; |
36 | | GeoDB* geodb; |
37 | | |
38 | 2 | GeoDBTest() { |
39 | 2 | GeoDBOptions geodb_options; |
40 | 2 | EXPECT_OK(DestroyDB(kDefaultDbName, options)); |
41 | 2 | options.create_if_missing = true; |
42 | 2 | Status status = DB::Open(options, kDefaultDbName, &db); |
43 | 2 | geodb = new GeoDBImpl(db, geodb_options); |
44 | 2 | } |
45 | | |
46 | 2 | ~GeoDBTest() { |
47 | 2 | delete geodb; |
48 | 2 | } |
49 | | |
50 | 12 | GeoDB* getdb() { |
51 | 12 | return geodb; |
52 | 12 | } |
53 | | }; |
54 | | |
55 | | const std::string GeoDBTest::kDefaultDbName = test::TmpDir() + "/geodb_test"; |
56 | | Options GeoDBTest::options = Options(); |
57 | | |
58 | | // Insert, Get and Remove |
59 | 1 | TEST_F(GeoDBTest, SimpleTest) { |
60 | 1 | GeoPosition pos1(100, 101); |
61 | 1 | std::string id1("id1"); |
62 | 1 | std::string value1("value1"); |
63 | | |
64 | | // insert first object into database |
65 | 1 | GeoObject obj1(pos1, id1, value1); |
66 | 1 | Status status = getdb()->Insert(obj1); |
67 | 1 | ASSERT_TRUE(status.ok()); |
68 | | |
69 | | // insert second object into database |
70 | 1 | GeoPosition pos2(200, 201); |
71 | 1 | std::string id2("id2"); |
72 | 1 | std::string value2 = "value2"; |
73 | 1 | GeoObject obj2(pos2, id2, value2); |
74 | 1 | status = getdb()->Insert(obj2); |
75 | 1 | ASSERT_TRUE(status.ok()); |
76 | | |
77 | | // retrieve first object using position |
78 | 1 | std::string value; |
79 | 1 | status = getdb()->GetByPosition(pos1, Slice(id1), &value); |
80 | 1 | ASSERT_TRUE(status.ok()); |
81 | 1 | ASSERT_EQ(value, value1); |
82 | | |
83 | | // retrieve first object using id |
84 | 1 | GeoObject obj; |
85 | 1 | status = getdb()->GetById(Slice(id1), &obj); |
86 | 1 | ASSERT_TRUE(status.ok()); |
87 | 1 | ASSERT_EQ(obj.position.latitude, 100); |
88 | 1 | ASSERT_EQ(obj.position.longitude, 101); |
89 | 1 | ASSERT_EQ(obj.id.compare(id1), 0); |
90 | 1 | ASSERT_EQ(obj.value, value1); |
91 | | |
92 | | // delete first object |
93 | 1 | status = getdb()->Remove(Slice(id1)); |
94 | 1 | ASSERT_TRUE(status.ok()); |
95 | 1 | status = getdb()->GetByPosition(pos1, Slice(id1), &value); |
96 | 1 | ASSERT_TRUE(status.IsNotFound()); |
97 | 1 | status = getdb()->GetById(id1, &obj); |
98 | 1 | ASSERT_TRUE(status.IsNotFound()); |
99 | | |
100 | | // check that we can still find second object |
101 | 1 | status = getdb()->GetByPosition(pos2, id2, &value); |
102 | 1 | ASSERT_TRUE(status.ok()); |
103 | 1 | ASSERT_EQ(value, value2); |
104 | 1 | status = getdb()->GetById(id2, &obj); |
105 | 1 | ASSERT_TRUE(status.ok()); |
106 | 1 | } |
107 | | |
108 | | // Search. |
109 | | // Verify distances via http://www.stevemorse.org/nearest/distance.php |
110 | 1 | TEST_F(GeoDBTest, Search) { |
111 | 1 | GeoPosition pos1(45, 45); |
112 | 1 | std::string id1("mid1"); |
113 | 1 | std::string value1 = "midvalue1"; |
114 | | |
115 | | // insert object at 45 degree latitude |
116 | 1 | GeoObject obj1(pos1, id1, value1); |
117 | 1 | Status status = getdb()->Insert(obj1); |
118 | 1 | ASSERT_TRUE(status.ok()); |
119 | | |
120 | | // search all objects centered at 46 degree latitude with |
121 | | // a radius of 200 kilometers. We should find the one object that |
122 | | // we inserted earlier. |
123 | 1 | GeoIterator* iter1 = getdb()->SearchRadial(GeoPosition(46, 46), 200000); |
124 | 1 | ASSERT_TRUE(status.ok()); |
125 | 1 | ASSERT_EQ(iter1->geo_object().value, "midvalue1"); |
126 | 1 | uint32_t size = 0; |
127 | 2 | while (iter1->Valid()) { |
128 | 1 | size++; |
129 | 1 | iter1->Next(); |
130 | 1 | } |
131 | 1 | ASSERT_EQ(size, 1U); |
132 | 1 | delete iter1; |
133 | | |
134 | | // search all objects centered at 46 degree latitude with |
135 | | // a radius of 2 kilometers. There should be none. |
136 | 1 | GeoIterator* iter2 = getdb()->SearchRadial(GeoPosition(46, 46), 2); |
137 | 1 | ASSERT_TRUE(status.ok()); |
138 | 1 | ASSERT_FALSE(iter2->Valid()); |
139 | 1 | delete iter2; |
140 | 1 | } |
141 | | |
142 | | } // namespace rocksdb |
143 | | |
144 | 13.2k | int main(int argc, char* argv[]) { |
145 | 13.2k | ::testing::InitGoogleTest(&argc, argv); |
146 | 13.2k | return RUN_ALL_TESTS(); |
147 | 13.2k | } |
148 | | #else |
149 | | |
150 | | #include <stdio.h> |
151 | | |
152 | | int main() { |
153 | | fprintf(stderr, "SKIPPED\n"); |
154 | | return 0; |
155 | | } |
156 | | |
157 | | #endif // !ROCKSDB_LITE |