/Users/deen/code/yugabyte-db/src/yb/rocksdb/table/block_hash_index_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 | | #include <memory> |
21 | | #include <string> |
22 | | #include <vector> |
23 | | |
24 | | #include <gtest/gtest.h> |
25 | | |
26 | | #include "yb/rocksdb/comparator.h" |
27 | | #include "yb/rocksdb/iterator.h" |
28 | | #include "yb/rocksdb/slice_transform.h" |
29 | | #include "yb/rocksdb/table/block_hash_index.h" |
30 | | #include "yb/rocksdb/table/internal_iterator.h" |
31 | | |
32 | | #include "yb/rocksdb/util/testutil.h" |
33 | | |
34 | | namespace rocksdb { |
35 | | |
36 | | typedef std::map<std::string, std::string> Data; |
37 | | |
38 | | class MapIterator : public InternalIterator { |
39 | | public: |
40 | 2 | explicit MapIterator(const Data& data) : data_(data), pos_(data_.end()) {} |
41 | | |
42 | 38 | bool Valid() const override { return pos_ != data_.end(); } |
43 | | |
44 | 2 | void SeekToFirst() override { pos_ = data_.begin(); } |
45 | | |
46 | 0 | void SeekToLast() override { |
47 | 0 | pos_ = data_.end(); |
48 | 0 | --pos_; |
49 | 0 | } |
50 | | |
51 | 0 | void Seek(const Slice& target) override { |
52 | 0 | pos_ = data_.find(target.ToString()); |
53 | 0 | } |
54 | | |
55 | 25 | void Next() override { ++pos_; } |
56 | | |
57 | 0 | void Prev() override { --pos_; } |
58 | | |
59 | 49 | Slice key() const override { return pos_->first; } |
60 | | |
61 | 0 | Slice value() const override { return pos_->second; } |
62 | | |
63 | 5 | Status status() const override { return Status::OK(); } |
64 | | |
65 | | private: |
66 | | const Data& data_; |
67 | | Data::const_iterator pos_; |
68 | | }; |
69 | | |
70 | | class BlockTest : public RocksDBTest {}; |
71 | | |
72 | 1 | TEST_F(BlockTest, BasicTest) { |
73 | 1 | const size_t keys_per_block = 4; |
74 | 1 | const size_t prefix_size = 2; |
75 | 1 | std::vector<std::string> keys = {/* block 1 */ |
76 | 1 | "0101", "0102", "0103", "0201", |
77 | | /* block 2 */ |
78 | 1 | "0202", "0203", "0301", "0401", |
79 | | /* block 3 */ |
80 | 1 | "0501", "0601", "0701", "0801", |
81 | | /* block 4 */ |
82 | 1 | "0802", "0803", "0804", "0805", |
83 | | /* block 5 */ |
84 | 1 | "0806", "0807", "0808", "0809", }; |
85 | | |
86 | 1 | Data data_entries; |
87 | 20 | for (const auto& key : keys) { |
88 | 20 | data_entries.insert({key, key}); |
89 | 20 | } |
90 | | |
91 | 1 | Data index_entries; |
92 | 6 | for (size_t i = 3; i < keys.size(); i += keys_per_block) { |
93 | | // simply ignore the value part |
94 | 5 | index_entries.insert({keys[i], ""}); |
95 | 5 | } |
96 | | |
97 | 1 | MapIterator data_iter(data_entries); |
98 | 1 | MapIterator index_iter(index_entries); |
99 | | |
100 | 1 | auto prefix_extractor = NewFixedPrefixTransform(prefix_size); |
101 | 1 | std::unique_ptr<BlockHashIndex> block_hash_index(CreateBlockHashIndexOnTheFly( |
102 | 1 | &index_iter, &data_iter, static_cast<uint32_t>(index_entries.size()), |
103 | 1 | BytewiseComparator(), prefix_extractor)); |
104 | | |
105 | 1 | std::map<std::string, BlockHashIndex::RestartIndex> expected = { |
106 | 1 | {"01xx", BlockHashIndex::RestartIndex(0, 1)}, |
107 | 1 | {"02yy", BlockHashIndex::RestartIndex(0, 2)}, |
108 | 1 | {"03zz", BlockHashIndex::RestartIndex(1, 1)}, |
109 | 1 | {"04pp", BlockHashIndex::RestartIndex(1, 1)}, |
110 | 1 | {"05ww", BlockHashIndex::RestartIndex(2, 1)}, |
111 | 1 | {"06xx", BlockHashIndex::RestartIndex(2, 1)}, |
112 | 1 | {"07pp", BlockHashIndex::RestartIndex(2, 1)}, |
113 | 1 | {"08xz", BlockHashIndex::RestartIndex(2, 3)}, }; |
114 | | |
115 | 1 | const BlockHashIndex::RestartIndex* index = nullptr; |
116 | | // search existed prefixes |
117 | 8 | for (const auto& item : expected) { |
118 | 8 | index = block_hash_index->GetRestartIndex(item.first); |
119 | 8 | ASSERT_TRUE(index != nullptr); |
120 | 8 | ASSERT_EQ(item.second.first_index, index->first_index); |
121 | 8 | ASSERT_EQ(item.second.num_blocks, index->num_blocks); |
122 | 8 | } |
123 | | |
124 | | // search non exist prefixes |
125 | 1 | ASSERT_TRUE(!block_hash_index->GetRestartIndex("00xx")); |
126 | 1 | ASSERT_TRUE(!block_hash_index->GetRestartIndex("10yy")); |
127 | 1 | ASSERT_TRUE(!block_hash_index->GetRestartIndex("20zz")); |
128 | | |
129 | 1 | delete prefix_extractor; |
130 | 1 | } |
131 | | |
132 | | } // namespace rocksdb |
133 | | |
134 | 13.2k | int main(int argc, char** argv) { |
135 | 13.2k | ::testing::InitGoogleTest(&argc, argv); |
136 | 13.2k | return RUN_ALL_TESTS(); |
137 | 13.2k | } |