Coverage Report

Created: 2026-02-14 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rocksdb/table/plain/plain_table_bloom.h
Line
Count
Source
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
7
#include <memory>
8
#include <string>
9
#include <vector>
10
11
#include "port/port.h"
12
#include "rocksdb/slice.h"
13
#include "util/bloom_impl.h"
14
#include "util/hash.h"
15
#include "util/math.h"
16
17
namespace ROCKSDB_NAMESPACE {
18
class Slice;
19
class Allocator;
20
class Logger;
21
22
// A legacy Bloom filter implementation used by Plain Table db format, for
23
// schema backward compatibility. Not for use in new filter applications.
24
class PlainTableBloomV1 {
25
 public:
26
  // allocator: pass allocator to bloom filter, hence trace the usage of memory
27
  // total_bits: fixed total bits for the bloom
28
  // num_probes: number of hash probes for a single key
29
  // locality:  If positive, optimize for cache line locality, 0 otherwise.
30
  // hash_func:  customized hash function
31
  // huge_page_tlb_size:  if >0, try to allocate bloom bytes from huge page TLB
32
  //                      within this page size. Need to reserve huge pages for
33
  //                      it to be allocated, like:
34
  //                         sysctl -w vm.nr_hugepages=20
35
  //                     See linux doc Documentation/vm/hugetlbpage.txt
36
  explicit PlainTableBloomV1(uint32_t num_probes = 6);
37
  void SetTotalBits(Allocator* allocator, uint32_t total_bits,
38
                    uint32_t locality, size_t huge_page_tlb_size,
39
                    Logger* logger);
40
41
0
  ~PlainTableBloomV1() {}
42
43
  // Assuming single threaded access to this function.
44
  void AddHash(uint32_t hash);
45
46
  // Multithreaded access to this function is OK
47
  bool MayContainHash(uint32_t hash) const;
48
49
  void Prefetch(uint32_t hash);
50
51
0
  uint32_t GetNumBlocks() const { return kNumBlocks; }
52
53
0
  Slice GetRawData() const { return Slice(data_, GetTotalBits() / 8); }
54
55
  void SetRawData(char* raw_data, uint32_t total_bits, uint32_t num_blocks = 0);
56
57
0
  uint32_t GetTotalBits() const { return kTotalBits; }
58
59
0
  bool IsInitialized() const { return kNumBlocks > 0 || kTotalBits > 0; }
60
61
 private:
62
  uint32_t kTotalBits;
63
  uint32_t kNumBlocks;
64
  const uint32_t kNumProbes;
65
66
  char* data_;
67
68
  static constexpr int LOG2_CACHE_LINE_SIZE =
69
      ConstexprFloorLog2(CACHE_LINE_SIZE);
70
};
71
72
#if defined(_MSC_VER)
73
#pragma warning(push)
74
// local variable is initialized but not referenced
75
#pragma warning(disable : 4189)
76
#endif
77
0
inline void PlainTableBloomV1::Prefetch(uint32_t h) {
78
0
  if (kNumBlocks != 0) {
79
0
    uint32_t ignored;
80
0
    LegacyLocalityBloomImpl</*ExtraRotates*/ true>::PrepareHashMayMatch(
81
0
        h, kNumBlocks, data_, &ignored, LOG2_CACHE_LINE_SIZE);
82
0
  }
83
0
}
84
#if defined(_MSC_VER)
85
#pragma warning(pop)
86
#endif
87
88
0
inline bool PlainTableBloomV1::MayContainHash(uint32_t h) const {
89
0
  assert(IsInitialized());
90
0
  if (kNumBlocks != 0) {
91
0
    return LegacyLocalityBloomImpl<true>::HashMayMatch(
92
0
        h, kNumBlocks, kNumProbes, data_, LOG2_CACHE_LINE_SIZE);
93
0
  } else {
94
0
    return LegacyNoLocalityBloomImpl::HashMayMatch(h, kTotalBits, kNumProbes,
95
0
                                                   data_);
96
0
  }
97
0
}
98
99
0
inline void PlainTableBloomV1::AddHash(uint32_t h) {
100
0
  assert(IsInitialized());
101
0
  if (kNumBlocks != 0) {
102
0
    LegacyLocalityBloomImpl<true>::AddHash(h, kNumBlocks, kNumProbes, data_,
103
0
                                           LOG2_CACHE_LINE_SIZE);
104
0
  } else {
105
0
    LegacyNoLocalityBloomImpl::AddHash(h, kTotalBits, kNumProbes, data_);
106
0
  }
107
0
}
108
109
class BloomBlockBuilder {
110
 public:
111
  static const std::string kBloomBlock;
112
113
0
  explicit BloomBlockBuilder(uint32_t num_probes = 6) : bloom_(num_probes) {}
114
115
  void SetTotalBits(Allocator* allocator, uint32_t total_bits,
116
                    uint32_t locality, size_t huge_page_tlb_size,
117
0
                    Logger* logger) {
118
0
    bloom_.SetTotalBits(allocator, total_bits, locality, huge_page_tlb_size,
119
0
                        logger);
120
0
  }
121
122
0
  uint32_t GetNumBlocks() const { return bloom_.GetNumBlocks(); }
123
124
  void AddKeysHashes(const std::vector<uint32_t>& keys_hashes);
125
126
  Slice Finish();
127
128
 private:
129
  PlainTableBloomV1 bloom_;
130
};
131
132
}  // namespace ROCKSDB_NAMESPACE