filter_block.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. //
  5. // A filter block is stored near the end of a Table file. It contains
  6. // filters (e.g., bloom filters) for all data blocks in the table combined
  7. // into a single filter block.
  8. #ifndef STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_
  9. #define STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_
  10. #include <cstddef>
  11. #include <cstdint>
  12. #include <string>
  13. #include <vector>
  14. #include "leveldb/slice.h"
  15. #include "util/hash.h"
  16. namespace leveldb {
  17. class FilterPolicy;
  18. // A FilterBlockBuilder is used to construct all of the filters for a
  19. // particular Table. It generates a single string which is stored as
  20. // a special block in the Table.
  21. //
  22. // The sequence of calls to FilterBlockBuilder must match the regexp:
  23. // (StartBlock AddKey*)* Finish
  24. class FilterBlockBuilder {
  25. public:
  26. explicit FilterBlockBuilder(const FilterPolicy*);
  27. FilterBlockBuilder(const FilterBlockBuilder&) = delete;
  28. FilterBlockBuilder& operator=(const FilterBlockBuilder&) = delete;
  29. void StartBlock(uint64_t block_offset);
  30. void AddKey(const Slice& key);
  31. Slice Finish();
  32. private:
  33. void GenerateFilter();
  34. const FilterPolicy* policy_;
  35. std::string keys_; // Flattened key contents
  36. std::vector<size_t> start_; // Starting index in keys_ of each key
  37. std::string result_; // Filter data computed so far
  38. std::vector<Slice> tmp_keys_; // policy_->CreateFilter() argument
  39. std::vector<uint32_t> filter_offsets_;
  40. };
  41. class FilterBlockReader {
  42. public:
  43. // REQUIRES: "contents" and *policy must stay live while *this is live.
  44. FilterBlockReader(const FilterPolicy* policy, const Slice& contents);
  45. bool KeyMayMatch(uint64_t block_offset, const Slice& key);
  46. private:
  47. const FilterPolicy* policy_;
  48. const char* data_; // Pointer to filter data (at block-start)
  49. const char* offset_; // Pointer to beginning of offset array (at block-end)
  50. size_t num_; // Number of entries in offset array
  51. size_t base_lg_; // Encoding parameter (see kFilterBaseLg in .cc file)
  52. };
  53. } // namespace leveldb
  54. #endif // STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_