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 <stddef.h>
  11. #include <stdint.h>
  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. void StartBlock(uint64_t block_offset);
  28. void AddKey(const Slice& key);
  29. Slice Finish();
  30. private:
  31. void GenerateFilter();
  32. const FilterPolicy* policy_;
  33. std::string keys_; // Flattened key contents
  34. std::vector<size_t> start_; // Starting index in keys_ of each key
  35. std::string result_; // Filter data computed so far
  36. std::vector<Slice> tmp_keys_; // policy_->CreateFilter() argument
  37. std::vector<uint32_t> filter_offsets_;
  38. // No copying allowed
  39. FilterBlockBuilder(const FilterBlockBuilder&);
  40. void operator=(const FilterBlockBuilder&);
  41. };
  42. class FilterBlockReader {
  43. public:
  44. // REQUIRES: "contents" and *policy must stay live while *this is live.
  45. FilterBlockReader(const FilterPolicy* policy, const Slice& contents);
  46. bool KeyMayMatch(uint64_t block_offset, const Slice& key);
  47. private:
  48. const FilterPolicy* policy_;
  49. const char* data_; // Pointer to filter data (at block-start)
  50. const char* offset_; // Pointer to beginning of offset array (at block-end)
  51. size_t num_; // Number of entries in offset array
  52. size_t base_lg_; // Encoding parameter (see kFilterBaseLg in .cc file)
  53. };
  54. }
  55. #endif // STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_