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