/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/slice_transform_test.cc
Line | Count | Source (jump to first uncovered line) |
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 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
21 | | // Use of this source code is governed by a BSD-style license that can be |
22 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
23 | | |
24 | | #include "yb/rocksdb/db/db_test_util.h" |
25 | | |
26 | | #include "yb/util/test_macros.h" |
27 | | #include "yb/rocksdb/util/testutil.h" |
28 | | |
29 | | namespace rocksdb { |
30 | | |
31 | | class SliceTransformTest : public RocksDBTest {}; |
32 | | |
33 | 1 | TEST_F(SliceTransformTest, CapPrefixTransform) { |
34 | 1 | std::string s; |
35 | 1 | s = "abcdefge"; |
36 | | |
37 | 1 | unique_ptr<const SliceTransform> transform; |
38 | | |
39 | 1 | transform.reset(NewCappedPrefixTransform(6)); |
40 | 1 | ASSERT_EQ(transform->Transform(s).ToString(), "abcdef"); |
41 | 1 | ASSERT_TRUE(transform->SameResultWhenAppended("123456")); |
42 | 1 | ASSERT_TRUE(transform->SameResultWhenAppended("1234567")); |
43 | 1 | ASSERT_TRUE(!transform->SameResultWhenAppended("12345")); |
44 | | |
45 | 1 | transform.reset(NewCappedPrefixTransform(8)); |
46 | 1 | ASSERT_EQ(transform->Transform(s).ToString(), "abcdefge"); |
47 | | |
48 | 1 | transform.reset(NewCappedPrefixTransform(10)); |
49 | 1 | ASSERT_EQ(transform->Transform(s).ToString(), "abcdefge"); |
50 | | |
51 | 1 | transform.reset(NewCappedPrefixTransform(0)); |
52 | 1 | ASSERT_EQ(transform->Transform(s).ToString(), ""); |
53 | | |
54 | 1 | transform.reset(NewCappedPrefixTransform(0)); |
55 | 1 | ASSERT_EQ(transform->Transform(std::string()).ToString(), ""); |
56 | 1 | } |
57 | | |
58 | | class SliceTransformDBTest : public RocksDBTest { |
59 | | private: |
60 | | std::string dbname_; |
61 | | Env* env_; |
62 | | DB* db_; |
63 | | |
64 | | public: |
65 | 1 | SliceTransformDBTest() : env_(Env::Default()), db_(nullptr) { |
66 | 1 | dbname_ = test::TmpDir() + "/slice_transform_db_test"; |
67 | 1 | EXPECT_OK(DestroyDB(dbname_, last_options_)); |
68 | 1 | } |
69 | | |
70 | 1 | ~SliceTransformDBTest() { |
71 | 1 | delete db_; |
72 | 1 | EXPECT_OK(DestroyDB(dbname_, last_options_)); |
73 | 1 | } |
74 | | |
75 | 6 | DB* db() { return db_; } |
76 | | |
77 | | // Return the current option configuration. |
78 | 0 | Options* GetOptions() { return &last_options_; } |
79 | | |
80 | 0 | void DestroyAndReopen() { |
81 | 0 | // Destroy using last options |
82 | 0 | Destroy(); |
83 | 0 | ASSERT_OK(TryReopen()); |
84 | 0 | } |
85 | | |
86 | 0 | void Destroy() { |
87 | 0 | delete db_; |
88 | 0 | db_ = nullptr; |
89 | 0 | ASSERT_OK(DestroyDB(dbname_, last_options_)); |
90 | 0 | } |
91 | | |
92 | 1 | Status TryReopen() { |
93 | 1 | delete db_; |
94 | 1 | db_ = nullptr; |
95 | 1 | last_options_.create_if_missing = true; |
96 | | |
97 | 1 | return DB::Open(last_options_, dbname_, &db_); |
98 | 1 | } |
99 | | |
100 | | Options last_options_; |
101 | | }; |
102 | | |
103 | 1 | TEST_F(SliceTransformDBTest, CapPrefix) { |
104 | 1 | last_options_.prefix_extractor.reset(NewCappedPrefixTransform(8)); |
105 | 1 | last_options_.statistics = rocksdb::CreateDBStatisticsForTests(); |
106 | 1 | BlockBasedTableOptions bbto; |
107 | 1 | bbto.filter_policy.reset(NewBloomFilterPolicy(10, false)); |
108 | 1 | bbto.whole_key_filtering = false; |
109 | 1 | last_options_.table_factory.reset(NewBlockBasedTableFactory(bbto)); |
110 | 1 | ASSERT_OK(TryReopen()); |
111 | | |
112 | 1 | ReadOptions ro; |
113 | 1 | FlushOptions fo; |
114 | 1 | WriteOptions wo; |
115 | | |
116 | 1 | ASSERT_OK(db()->Put(wo, "barbarbar", "foo")); |
117 | 1 | ASSERT_OK(db()->Put(wo, "barbarbar2", "foo2")); |
118 | 1 | ASSERT_OK(db()->Put(wo, "foo", "bar")); |
119 | 1 | ASSERT_OK(db()->Put(wo, "foo3", "bar3")); |
120 | 1 | ASSERT_OK(db()->Flush(fo)); |
121 | | |
122 | 1 | unique_ptr<Iterator> iter(db()->NewIterator(ro)); |
123 | | |
124 | 1 | iter->Seek("foo"); |
125 | 1 | ASSERT_OK(iter->status()); |
126 | 1 | ASSERT_TRUE(iter->Valid()); |
127 | 1 | ASSERT_EQ(iter->value().ToString(), "bar"); |
128 | 1 | ASSERT_EQ(TestGetTickerCount(last_options_, BLOOM_FILTER_PREFIX_USEFUL), 0U); |
129 | | |
130 | 1 | iter->Seek("foo2"); |
131 | 1 | ASSERT_OK(iter->status()); |
132 | 1 | ASSERT_TRUE(!iter->Valid()); |
133 | 1 | ASSERT_EQ(TestGetTickerCount(last_options_, BLOOM_FILTER_PREFIX_USEFUL), 1U); |
134 | | |
135 | 1 | iter->Seek("barbarbar"); |
136 | 1 | ASSERT_OK(iter->status()); |
137 | 1 | ASSERT_TRUE(iter->Valid()); |
138 | 1 | ASSERT_EQ(iter->value().ToString(), "foo"); |
139 | 1 | ASSERT_EQ(TestGetTickerCount(last_options_, BLOOM_FILTER_PREFIX_USEFUL), 1U); |
140 | | |
141 | 1 | iter->Seek("barfoofoo"); |
142 | 1 | ASSERT_OK(iter->status()); |
143 | 1 | ASSERT_TRUE(!iter->Valid()); |
144 | 1 | ASSERT_EQ(TestGetTickerCount(last_options_, BLOOM_FILTER_PREFIX_USEFUL), 2U); |
145 | | |
146 | 1 | iter->Seek("foobarbar"); |
147 | 1 | ASSERT_OK(iter->status()); |
148 | 1 | ASSERT_TRUE(!iter->Valid()); |
149 | 1 | ASSERT_EQ(TestGetTickerCount(last_options_, BLOOM_FILTER_PREFIX_USEFUL), 3U); |
150 | 1 | } |
151 | | |
152 | | } // namespace rocksdb |
153 | | |
154 | 13.2k | int main(int argc, char** argv) { |
155 | 13.2k | ::testing::InitGoogleTest(&argc, argv); |
156 | 13.2k | return RUN_ALL_TESTS(); |
157 | 13.2k | } |