plain_table_bloom.cc 2.4 KB

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