Coverage Report

Created: 2025-07-23 07:17

/src/rocksdb/table/plain/plain_table_bloom.cc
Line
Count
Source (jump to first uncovered line)
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
6
#include "table/plain/plain_table_bloom.h"
7
8
#include <algorithm>
9
#include <string>
10
11
#include "memory/allocator.h"
12
#include "util/dynamic_bloom.h"
13
14
namespace ROCKSDB_NAMESPACE {
15
16
namespace {
17
18
0
uint32_t GetTotalBitsForLocality(uint32_t total_bits) {
19
0
  uint32_t num_blocks =
20
0
      (total_bits + CACHE_LINE_SIZE * 8 - 1) / (CACHE_LINE_SIZE * 8);
21
22
  // Make num_blocks an odd number to make sure more bits are involved
23
  // when determining which block.
24
0
  if (num_blocks % 2 == 0) {
25
0
    num_blocks++;
26
0
  }
27
28
0
  return num_blocks * (CACHE_LINE_SIZE * 8);
29
0
}
30
}  // namespace
31
32
PlainTableBloomV1::PlainTableBloomV1(uint32_t num_probes)
33
0
    : kTotalBits(0), kNumBlocks(0), kNumProbes(num_probes), data_(nullptr) {}
34
35
void PlainTableBloomV1::SetRawData(char* raw_data, uint32_t total_bits,
36
0
                                   uint32_t num_blocks) {
37
0
  data_ = raw_data;
38
0
  kTotalBits = total_bits;
39
0
  kNumBlocks = num_blocks;
40
0
}
41
42
void PlainTableBloomV1::SetTotalBits(Allocator* allocator, uint32_t total_bits,
43
                                     uint32_t locality,
44
                                     size_t huge_page_tlb_size,
45
0
                                     Logger* logger) {
46
0
  kTotalBits = (locality > 0) ? GetTotalBitsForLocality(total_bits)
47
0
                              : (total_bits + 7) / 8 * 8;
48
0
  kNumBlocks = (locality > 0) ? (kTotalBits / (CACHE_LINE_SIZE * 8)) : 0;
49
50
0
  assert(kNumBlocks > 0 || kTotalBits > 0);
51
0
  assert(kNumProbes > 0);
52
53
0
  uint32_t sz = kTotalBits / 8;
54
0
  if (kNumBlocks > 0) {
55
0
    sz += CACHE_LINE_SIZE - 1;
56
0
  }
57
0
  assert(allocator);
58
59
0
  char* raw = allocator->AllocateAligned(sz, huge_page_tlb_size, logger);
60
0
  memset(raw, 0, sz);
61
0
  auto cache_line_offset = reinterpret_cast<uintptr_t>(raw) % CACHE_LINE_SIZE;
62
0
  if (kNumBlocks > 0 && cache_line_offset > 0) {
63
0
    raw += CACHE_LINE_SIZE - cache_line_offset;
64
0
  }
65
0
  data_ = raw;
66
0
}
67
68
void BloomBlockBuilder::AddKeysHashes(
69
0
    const std::vector<uint32_t>& keys_hashes) {
70
0
  for (auto hash : keys_hashes) {
71
0
    bloom_.AddHash(hash);
72
0
  }
73
0
}
74
75
0
Slice BloomBlockBuilder::Finish() { return bloom_.GetRawData(); }
76
77
const std::string BloomBlockBuilder::kBloomBlock = "kBloomBlock";
78
}  // namespace ROCKSDB_NAMESPACE