Skip to content

Commit

Permalink
Refactor BlockSplitBloomFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
zncleon committed Sep 17, 2023
1 parent 9fa5445 commit 37aaded
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/types/bloom_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ bool BlockSplitBloomFilter::Init(std::string bitset) {
return true;
}

std::unique_ptr<const BlockSplitBloomFilter> BlockSplitBloomFilter::CreateNonOwned(const std::string& bitset) {
return std::unique_ptr<const BlockSplitBloomFilter>(new BlockSplitBloomFilter(bitset));
}

static constexpr uint32_t kBloomFilterHeaderSizeGuess = 256;

bool BlockSplitBloomFilter::FindHash(uint64_t hash) const {
Expand Down Expand Up @@ -94,3 +98,5 @@ void BlockSplitBloomFilter::InsertHash(uint64_t hash) {
}

uint64_t BlockSplitBloomFilter::Hash(const char* data, size_t length) { return XXH64(data, length, /*seed=*/0); }

BlockSplitBloomFilter::BlockSplitBloomFilter(const std::string& bitset) : data_(bitset), num_bytes_(bitset.size()) {}
10 changes: 10 additions & 0 deletions src/types/bloom_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ class BlockSplitBloomFilter {
/// @return false if the number of bytes of Bloom filter bitset is not a power of 2, and true means successfully init
bool Init(std::string bitset);

/// Create the non-owned BlockSplitBloomFilter. It use the bitset as underlying bitset. It is the caller's
/// responsibility to ensure the bitset would not to change.
///
/// @param bitset The given bitset for the Bloom filter underlying bitset.
/// @return the unique_ptr of the const non-owned BlockSplitBloomFilter
static std::unique_ptr<const BlockSplitBloomFilter> CreateNonOwned(const std::string& bitset);

/// Minimum Bloom filter size, it sets to 32 bytes to fit a tiny Bloom filter.
static constexpr uint32_t kMinimumBloomFilterBytes = 32;

Expand Down Expand Up @@ -166,6 +173,9 @@ class BlockSplitBloomFilter {
static uint64_t Hash(const char* data, size_t length);

private:
// The private constructor of BlockSplitBloomFilter. It's only used for CreateNonOwned
explicit BlockSplitBloomFilter(const std::string& bitset);

// Bytes in a tiny Bloom filter block.
static constexpr int kBytesPerFilterBlock = 32;

Expand Down
6 changes: 3 additions & 3 deletions src/types/redis_bloom_chain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ void BloomChain::bloomAdd(const Slice &item, std::string *bf_data) {
}

bool BloomChain::bloomCheck(const Slice &item, std::string &bf_data) {
BlockSplitBloomFilter block_split_bloom_filter;
block_split_bloom_filter.Init(bf_data);
std::unique_ptr<const BlockSplitBloomFilter> block_split_bloom_filter_non_owned =
BlockSplitBloomFilter::CreateNonOwned(bf_data);

uint64_t h = BlockSplitBloomFilter::Hash(item.data(), item.size());
return block_split_bloom_filter.FindHash(h);
return block_split_bloom_filter_non_owned->FindHash(h);
}

rocksdb::Status BloomChain::Reserve(const Slice &user_key, uint32_t capacity, double error_rate, uint16_t expansion) {
Expand Down

0 comments on commit 37aaded

Please sign in to comment.