Skip to content

Commit

Permalink
Make Ribbon filters available (rust-rocksdb#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf authored and vldm committed Sep 8, 2022
1 parent a2cb46a commit e2fc1b9
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion src/db_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,17 @@ impl BlockBasedOptions {
}
}

/// Sets the filter policy to reduce disk reads
/// Sets a [Bloom filter](https://github.com/facebook/rocksdb/wiki/RocksDB-Bloom-Filter)
/// policy to reduce disk reads.
///
/// # Examples
///
/// ```
/// use rocksdb::BlockBasedOptions;
///
/// let mut opts = BlockBasedOptions::default();
/// opts.set_bloom_filter(10.0, true);
/// ```
pub fn set_bloom_filter(&mut self, bits_per_key: c_double, block_based: bool) {
unsafe {
let bloom = if block_based {
Expand All @@ -574,6 +584,56 @@ impl BlockBasedOptions {
}
}

/// Sets a [Ribbon filter](http://rocksdb.org/blog/2021/12/29/ribbon-filter.html)
/// policy to reduce disk reads.
///
/// Ribbon filters use less memory in exchange for slightly more CPU usage
/// compared to an equivalent bloom filter.
///
/// # Examples
///
/// ```
/// use rocksdb::BlockBasedOptions;
///
/// let mut opts = BlockBasedOptions::default();
/// opts.set_ribbon_filter(10.0);
/// ```
pub fn set_ribbon_filter(&mut self, bloom_equivalent_bits_per_key: c_double) {
unsafe {
let ribbon = ffi::rocksdb_filterpolicy_create_ribbon(bloom_equivalent_bits_per_key);
ffi::rocksdb_block_based_options_set_filter_policy(self.inner, ribbon);
}
}

/// Sets a hybrid [Ribbon filter](http://rocksdb.org/blog/2021/12/29/ribbon-filter.html)
/// policy to reduce disk reads.
///
/// Uses Bloom filters before the given level, and Ribbon filters for all
/// other levels. This combines the memory savings from Ribbon filters
/// with the lower CPU usage of Bloom filters.
///
/// # Examples
///
/// ```
/// use rocksdb::BlockBasedOptions;
///
/// let mut opts = BlockBasedOptions::default();
/// opts.set_hybrid_ribbon_filter(10.0, 2);
/// ```
pub fn set_hybrid_ribbon_filter(
&mut self,
bloom_equivalent_bits_per_key: c_double,
bloom_before_level: c_int,
) {
unsafe {
let ribbon = ffi::rocksdb_filterpolicy_create_ribbon_hybrid(
bloom_equivalent_bits_per_key,
bloom_before_level,
);
ffi::rocksdb_block_based_options_set_filter_policy(self.inner, ribbon);
}
}

pub fn set_cache_index_and_filter_blocks(&mut self, v: bool) {
unsafe {
ffi::rocksdb_block_based_options_set_cache_index_and_filter_blocks(self.inner, v as u8);
Expand Down

0 comments on commit e2fc1b9

Please sign in to comment.