作者: 谢瑞阳 10225101483 徐翔宇 10225101535
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.3 KiB

  1. // Copyright (c) 2012 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include "table/filter_block.h"
  5. #include "leveldb/filter_policy.h"
  6. #include "util/coding.h"
  7. namespace leveldb {
  8. // See doc/table_format.md for an explanation of the filter block format.
  9. // Generate new filter every 2KB of data
  10. static const size_t kFilterBaseLg = 11;
  11. static const size_t kFilterBase = 1 << kFilterBaseLg;
  12. FilterBlockBuilder::FilterBlockBuilder(const FilterPolicy* policy)
  13. : policy_(policy) {}
  14. void FilterBlockBuilder::StartBlock(uint64_t block_offset) {
  15. uint64_t filter_index = (block_offset / kFilterBase);
  16. assert(filter_index >= filter_offsets_.size());
  17. while (filter_index > filter_offsets_.size()) {
  18. GenerateFilter();
  19. }
  20. }
  21. void FilterBlockBuilder::AddKey(const Slice& key) {
  22. Slice k = key;
  23. start_.push_back(keys_.size());
  24. keys_.append(k.data(), k.size());
  25. }
  26. Slice FilterBlockBuilder::Finish() {
  27. if (!start_.empty()) {
  28. GenerateFilter();
  29. }
  30. // Append array of per-filter offsets
  31. const uint32_t array_offset = result_.size();
  32. for (size_t i = 0; i < filter_offsets_.size(); i++) {
  33. PutFixed32(&result_, filter_offsets_[i]);
  34. }
  35. PutFixed32(&result_, array_offset);
  36. result_.push_back(kFilterBaseLg); // Save encoding parameter in result
  37. return Slice(result_);
  38. }
  39. void FilterBlockBuilder::GenerateFilter() {
  40. const size_t num_keys = start_.size();
  41. if (num_keys == 0) {
  42. // Fast path if there are no keys for this filter
  43. filter_offsets_.push_back(result_.size());
  44. return;
  45. }
  46. // Make list of keys from flattened key structure
  47. start_.push_back(keys_.size()); // Simplify length computation
  48. tmp_keys_.resize(num_keys);
  49. for (size_t i = 0; i < num_keys; i++) {
  50. const char* base = keys_.data() + start_[i];
  51. size_t length = start_[i + 1] - start_[i];
  52. tmp_keys_[i] = Slice(base, length);
  53. }
  54. // Generate filter for current set of keys and append to result_.
  55. filter_offsets_.push_back(result_.size());
  56. policy_->CreateFilter(&tmp_keys_[0], static_cast<int>(num_keys), &result_);
  57. tmp_keys_.clear();
  58. keys_.clear();
  59. start_.clear();
  60. }
  61. FilterBlockReader::FilterBlockReader(const FilterPolicy* policy,
  62. const Slice& contents)
  63. : policy_(policy), data_(nullptr), offset_(nullptr), num_(0), base_lg_(0) {
  64. size_t n = contents.size();
  65. if (n < 5) return; // 1 byte for base_lg_ and 4 for start of offset array
  66. base_lg_ = contents[n - 1];
  67. uint32_t last_word = DecodeFixed32(contents.data() + n - 5);
  68. if (last_word > n - 5) return;
  69. data_ = contents.data();
  70. offset_ = data_ + last_word;
  71. num_ = (n - 5 - last_word) / 4;
  72. }
  73. bool FilterBlockReader::KeyMayMatch(uint64_t block_offset, const Slice& key) {
  74. uint64_t index = block_offset >> base_lg_;
  75. if (index < num_) {
  76. uint32_t start = DecodeFixed32(offset_ + index * 4);
  77. uint32_t limit = DecodeFixed32(offset_ + index * 4 + 4);
  78. if (start <= limit && limit <= static_cast<size_t>(offset_ - data_)) {
  79. Slice filter = Slice(data_ + start, limit - start);
  80. return policy_->KeyMayMatch(key, filter);
  81. } else if (start == limit) {
  82. // Empty filters do not match any keys
  83. return false;
  84. }
  85. }
  86. return true; // Errors are treated as potential matches
  87. }
  88. } // namespace leveldb