diff --git a/src/types/bloom_filter.cc b/src/types/bloom_filter.cc index e7c0debf352..bcbc06a4d10 100644 --- a/src/types/bloom_filter.cc +++ b/src/types/bloom_filter.cc @@ -64,6 +64,10 @@ bool BlockSplitBloomFilter::Init(std::string bitset) { return true; } +std::unique_ptr BlockSplitBloomFilter::CreateNonOwned(const std::string& bitset) { + return std::unique_ptr(new BlockSplitBloomFilter(bitset)); +} + static constexpr uint32_t kBloomFilterHeaderSizeGuess = 256; bool BlockSplitBloomFilter::FindHash(uint64_t hash) const { @@ -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()) {} diff --git a/src/types/bloom_filter.h b/src/types/bloom_filter.h index effe68e4b48..a6d7c0a5d89 100644 --- a/src/types/bloom_filter.h +++ b/src/types/bloom_filter.h @@ -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 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; @@ -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; diff --git a/src/types/redis_bloom_chain.cc b/src/types/redis_bloom_chain.cc index 8aa06e14e12..f36524d1653 100644 --- a/src/types/redis_bloom_chain.cc +++ b/src/types/redis_bloom_chain.cc @@ -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 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) {