plain_table_bloom.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #include <string>
  7. #include <vector>
  8. #include "rocksdb/slice.h"
  9. #include "port/port.h"
  10. #include "util/bloom_impl.h"
  11. #include "util/hash.h"
  12. #include "third-party/folly/folly/ConstexprMath.h"
  13. #include <memory>
  14. namespace ROCKSDB_NAMESPACE {
  15. class Slice;
  16. class Allocator;
  17. class Logger;
  18. // A legacy Bloom filter implementation used by Plain Table db format, for
  19. // schema backward compatibility. Not for use in new filter applications.
  20. class PlainTableBloomV1 {
  21. public:
  22. // allocator: pass allocator to bloom filter, hence trace the usage of memory
  23. // total_bits: fixed total bits for the bloom
  24. // num_probes: number of hash probes for a single key
  25. // locality: If positive, optimize for cache line locality, 0 otherwise.
  26. // hash_func: customized hash function
  27. // huge_page_tlb_size: if >0, try to allocate bloom bytes from huge page TLB
  28. // within this page size. Need to reserve huge pages for
  29. // it to be allocated, like:
  30. // sysctl -w vm.nr_hugepages=20
  31. // See linux doc Documentation/vm/hugetlbpage.txt
  32. explicit PlainTableBloomV1(uint32_t num_probes = 6);
  33. void SetTotalBits(Allocator* allocator, uint32_t total_bits,
  34. uint32_t locality, size_t huge_page_tlb_size,
  35. Logger* logger);
  36. ~PlainTableBloomV1() {}
  37. // Assuming single threaded access to this function.
  38. void AddHash(uint32_t hash);
  39. // Multithreaded access to this function is OK
  40. bool MayContainHash(uint32_t hash) const;
  41. void Prefetch(uint32_t hash);
  42. uint32_t GetNumBlocks() const { return kNumBlocks; }
  43. Slice GetRawData() const { return Slice(data_, GetTotalBits() / 8); }
  44. void SetRawData(char* raw_data, uint32_t total_bits, uint32_t num_blocks = 0);
  45. uint32_t GetTotalBits() const { return kTotalBits; }
  46. bool IsInitialized() const { return kNumBlocks > 0 || kTotalBits > 0; }
  47. private:
  48. uint32_t kTotalBits;
  49. uint32_t kNumBlocks;
  50. const uint32_t kNumProbes;
  51. char* data_;
  52. static constexpr int LOG2_CACHE_LINE_SIZE =
  53. folly::constexpr_log2(CACHE_LINE_SIZE);
  54. };
  55. #if defined(_MSC_VER)
  56. #pragma warning(push)
  57. // local variable is initialized but not referenced
  58. #pragma warning(disable : 4189)
  59. #endif
  60. inline void PlainTableBloomV1::Prefetch(uint32_t h) {
  61. if (kNumBlocks != 0) {
  62. uint32_t ignored;
  63. LegacyLocalityBloomImpl</*ExtraRotates*/ true>::PrepareHashMayMatch(
  64. h, kNumBlocks, data_, &ignored, LOG2_CACHE_LINE_SIZE);
  65. }
  66. }
  67. #if defined(_MSC_VER)
  68. #pragma warning(pop)
  69. #endif
  70. inline bool PlainTableBloomV1::MayContainHash(uint32_t h) const {
  71. assert(IsInitialized());
  72. if (kNumBlocks != 0) {
  73. return LegacyLocalityBloomImpl<true>::HashMayMatch(
  74. h, kNumBlocks, kNumProbes, data_, LOG2_CACHE_LINE_SIZE);
  75. } else {
  76. return LegacyNoLocalityBloomImpl::HashMayMatch(h, kTotalBits, kNumProbes,
  77. data_);
  78. }
  79. }
  80. inline void PlainTableBloomV1::AddHash(uint32_t h) {
  81. assert(IsInitialized());
  82. if (kNumBlocks != 0) {
  83. LegacyLocalityBloomImpl<true>::AddHash(h, kNumBlocks, kNumProbes, data_,
  84. LOG2_CACHE_LINE_SIZE);
  85. } else {
  86. LegacyNoLocalityBloomImpl::AddHash(h, kTotalBits, kNumProbes, data_);
  87. }
  88. }
  89. class BloomBlockBuilder {
  90. public:
  91. static const std::string kBloomBlock;
  92. explicit BloomBlockBuilder(uint32_t num_probes = 6) : bloom_(num_probes) {}
  93. void SetTotalBits(Allocator* allocator, uint32_t total_bits,
  94. uint32_t locality, size_t huge_page_tlb_size,
  95. Logger* logger) {
  96. bloom_.SetTotalBits(allocator, total_bits, locality, huge_page_tlb_size,
  97. logger);
  98. }
  99. uint32_t GetNumBlocks() const { return bloom_.GetNumBlocks(); }
  100. void AddKeysHashes(const std::vector<uint32_t>& keys_hashes);
  101. Slice Finish();
  102. private:
  103. PlainTableBloomV1 bloom_;
  104. };
  105. }; // namespace ROCKSDB_NAMESPACE