pub struct ModularBloomFilter { /* private fields */ }Expand description
A Bloom filter whose accuracy can be traded for memory by dropping modules.
A probe consults resident modules in order and stops at the first rejection, so an absent key usually touches far fewer modules than are resident.
Implementations§
Source§impl ModularBloomFilter
impl ModularBloomFilter
Sourcepub fn from_modules(
layout: ModuleLayout,
words: &[u64],
) -> Result<Self, LoadError>
pub fn from_modules( layout: ModuleLayout, words: &[u64], ) -> Result<Self, LoadError>
Loads a filter from words, which hold whole modules laid out in order
starting at module 0.
Pass all layout.total_modules() modules to load the filter at the
accuracy it was written with. Pass the first n of them, meaning
n * layout.words_per_module() words, to load it at the accuracy n
modules give; the result holds only those modules and occupies
n * layout.bytes_per_module() bytes.
Fails if words is not a whole number of modules, or holds more modules
than layout describes.
Sourcepub fn from_modules_with_density(
layout: ModuleLayout,
words: &[u64],
density: ModuleDensity,
) -> Result<Self, LoadError>
pub fn from_modules_with_density( layout: ModuleLayout, words: &[u64], density: ModuleDensity, ) -> Result<Self, LoadError>
Loads a filter from words together with the density recorded when it
was built.
density may cover more modules than words holds, which is the point:
rates and Self::modules_for then answer for residencies the filter is
not currently at, including from a filter loaded with no modules at all.
A density covering fewer modules than words holds is discarded in
favour of measuring them, which is what Self::from_modules does.
Sourcepub fn density(&self) -> &ModuleDensity
pub fn density(&self) -> &ModuleDensity
Bits set per module, for a caller that wants to record them alongside the words.
Sourcepub fn push_module(&mut self, words: &[u64]) -> Result<(), LoadError>
pub fn push_module(&mut self, words: &[u64]) -> Result<(), LoadError>
Appends one more module, raising the filter’s accuracy by one rung.
words must hold exactly layout().words_per_module() words, and
must be the module at index resident_modules(), since modules are
stored in order. Modules already resident are left untouched.
Fails if words is the wrong length, or if every module the layout
describes is already resident.
Sourcepub fn contains_hash(&self, hash: u64) -> bool
pub fn contains_hash(&self, hash: u64) -> bool
Returns false only when hash was definitely never inserted.
Sourcepub fn layout(&self) -> &ModuleLayout
pub fn layout(&self) -> &ModuleLayout
Shape the filter was built with, whatever its current residency.
Sourcepub fn resident_modules(&self) -> u32
pub fn resident_modules(&self) -> u32
Modules currently held in memory.
Sourcepub fn resident_bytes(&self) -> usize
pub fn resident_bytes(&self) -> usize
Bytes the resident modules occupy.
Sourcepub fn truncate(&mut self, modules: u32)
pub fn truncate(&mut self, modules: u32)
Drops modules beyond the first modules, releasing their memory.
Truncating to zero leaves a filter that admits every key, which is the same answer a batch with no filter gives. Truncating to at least the current residency does nothing.
Sourcepub fn retune(&mut self, target_false_positive_rate: f64) -> u32
pub fn retune(&mut self, target_false_positive_rate: f64) -> u32
Drops every module the filter can spare while still meeting
target_false_positive_rate, and returns the new residency.
The rate is taken from the bits actually set, not from an assumed key count, so the result holds even for a filter whose layout was sized from a bad estimate. Never grows the filter: a target the current residency cannot meet leaves it unchanged.
Sourcepub fn modules_for(&self, target_false_positive_rate: f64) -> u32
pub fn modules_for(&self, target_false_positive_rate: f64) -> u32
Smallest residency that would still meet target_false_positive_rate.
Answers the question without changing the filter, so a memory budget can cost several filters before committing to any of them.
Sourcepub fn fill_ratio(&self) -> f64
pub fn fill_ratio(&self) -> f64
Fraction of bits set, measured over the resident modules.
Modules are all the same size, so the mean of their fills is exactly the set bits over the resident bits. Dividing by the layout’s full size instead would make a truncated filter under-report its own rate.
Sourcepub fn false_positive_rate(&self) -> f64
pub fn false_positive_rate(&self) -> f64
False positive rate at the current residency, from the measured fill.
Sourcepub fn false_positive_rate_with(&self, modules: u32) -> f64
pub fn false_positive_rate_with(&self, modules: u32) -> f64
False positive rate this filter would have with modules resident.
Accepts any modules up to layout().total_modules(), including
counts above the current residency; the fill of the resident modules
stands in for the ones not held. Larger values are clamped.
Sourcepub fn module(&self, index: u32) -> Option<&[u64]>
pub fn module(&self, index: u32) -> Option<&[u64]>
Words of module index, or None if it is not resident.
This is the handle a caller writes to storage. Modules are independent, so they may be written and read individually.
Sourcepub fn modules(&self) -> Vec<&[u64]>
pub fn modules(&self) -> Vec<&[u64]>
Words of each resident module, in order.
Allocates a vector of slice references; use Self::module to reach a
single module without allocating.
Trait Implementations§
Source§impl Clone for ModularBloomFilter
impl Clone for ModularBloomFilter
Source§fn clone(&self) -> ModularBloomFilter
fn clone(&self) -> ModularBloomFilter
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more