From bb485e986a1ca782cfd87b9f6c0f800346c85a30 Mon Sep 17 00:00:00 2001 From: qieqieplus Date: Fri, 9 Jul 2021 15:44:57 -0700 Subject: [PATCH] Add ribbon filter to C API (#8486) Summary: Pull Request resolved: https://github.com/facebook/rocksdb/pull/8486 Reviewed By: jay-zhuang Differential Revision: D29625501 Pulled By: pdillinger fbshipit-source-id: e6e2a455ae62a71f3a202278a751b9bba17ad03c --- db/c.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ db/c_test.c | 11 ++++++++--- include/rocksdb/c.h | 2 ++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/db/c.cc b/db/c.cc index 3c719e6ff..4e9c6ec22 100644 --- a/db/c.cc +++ b/db/c.cc @@ -88,6 +88,7 @@ using ROCKSDB_NAMESPACE::NewBloomFilterPolicy; using ROCKSDB_NAMESPACE::NewCompactOnDeletionCollectorFactory; using ROCKSDB_NAMESPACE::NewGenericRateLimiter; using ROCKSDB_NAMESPACE::NewLRUCache; +using ROCKSDB_NAMESPACE::NewRibbonFilterPolicy; using ROCKSDB_NAMESPACE::OptimisticTransactionDB; using ROCKSDB_NAMESPACE::OptimisticTransactionOptions; using ROCKSDB_NAMESPACE::Options; @@ -3882,6 +3883,47 @@ rocksdb_filterpolicy_t* rocksdb_filterpolicy_create_bloom(int bits_per_key) { return rocksdb_filterpolicy_create_bloom_format(bits_per_key, true); } +rocksdb_filterpolicy_t* rocksdb_filterpolicy_create_ribbon_format( + int bloom_equivalent_bits_per_key) { + // Make a rocksdb_filterpolicy_t, but override all of its methods so + // they delegate to a NewRibbonFilterPolicy() instead of user + // supplied C functions. + struct Wrapper : public rocksdb_filterpolicy_t { + const FilterPolicy* rep_; + ~Wrapper() override { delete rep_; } + const char* Name() const override { return rep_->Name(); } + void CreateFilter(const Slice* keys, int n, + std::string* dst) const override { + return rep_->CreateFilter(keys, n, dst); + } + bool KeyMayMatch(const Slice& key, const Slice& filter) const override { + return rep_->KeyMayMatch(key, filter); + } + ROCKSDB_NAMESPACE::FilterBitsBuilder* GetBuilderWithContext( + const ROCKSDB_NAMESPACE::FilterBuildingContext& context) + const override { + return rep_->GetBuilderWithContext(context); + } + ROCKSDB_NAMESPACE::FilterBitsReader* GetFilterBitsReader( + const Slice& contents) const override { + return rep_->GetFilterBitsReader(contents); + } + static void DoNothing(void*) {} + }; + Wrapper* wrapper = new Wrapper; + wrapper->rep_ = NewRibbonFilterPolicy(bloom_equivalent_bits_per_key); + wrapper->state_ = nullptr; + wrapper->delete_filter_ = nullptr; + wrapper->destructor_ = &Wrapper::DoNothing; + return wrapper; +} + +rocksdb_filterpolicy_t* rocksdb_filterpolicy_create_ribbon( + int bloom_equivalent_bits_per_key) { + return rocksdb_filterpolicy_create_ribbon_format( + bloom_equivalent_bits_per_key); +} + rocksdb_mergeoperator_t* rocksdb_mergeoperator_create( void* state, void (*destructor)(void*), char* (*full_merge)(void*, const char* key, size_t key_length, diff --git a/db/c_test.c b/db/c_test.c index 5b7459b06..eb94eb864 100644 --- a/db/c_test.c +++ b/db/c_test.c @@ -1043,7 +1043,7 @@ int main(int argc, char** argv) { } StartPhase("filter"); - for (run = 0; run <= 2; run++) { + for (run = 0; run <= 3; run++) { // First run uses custom filter // Second run uses old block-based bloom filter // Third run uses full bloom filter @@ -1054,8 +1054,10 @@ int main(int argc, char** argv) { FilterKeyMatch, NULL, FilterName); } else if (run == 1) { policy = rocksdb_filterpolicy_create_bloom(8); - } else { + } else if (run == 2) { policy = rocksdb_filterpolicy_create_bloom_full(8); + } else { + policy = rocksdb_filterpolicy_create_ribbon(8); } rocksdb_block_based_options_set_filter_policy(table_options, policy); @@ -1121,9 +1123,12 @@ int main(int argc, char** argv) { } else if (run == 1) { // Essentially a fingerprint of the block-based Bloom schema CheckCondition(hits == 241); - } else { + } else if (run == 2) { // Essentially a fingerprint of full Bloom schema, format_version=5 CheckCondition(hits == 188); + } else { + // Essentially a fingerprint of Ribbon schema + CheckCondition(hits == 226); } CheckCondition( (keys_to_query - hits) == diff --git a/include/rocksdb/c.h b/include/rocksdb/c.h index 8280b25a2..51fe928e8 100644 --- a/include/rocksdb/c.h +++ b/include/rocksdb/c.h @@ -1602,6 +1602,8 @@ extern ROCKSDB_LIBRARY_API rocksdb_filterpolicy_t* rocksdb_filterpolicy_create_bloom(int bits_per_key); extern ROCKSDB_LIBRARY_API rocksdb_filterpolicy_t* rocksdb_filterpolicy_create_bloom_full(int bits_per_key); +extern ROCKSDB_LIBRARY_API rocksdb_filterpolicy_t* +rocksdb_filterpolicy_create_ribbon(int bloom_equivalent_bits_per_key); /* Merge Operator */