Skip to main content

ModularBloomFilter

Struct ModularBloomFilter 

Source
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

Source

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.

Source

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.

Source

pub fn density(&self) -> &ModuleDensity

Bits set per module, for a caller that wants to record them alongside the words.

Source

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.

Source

pub fn contains_hash(&self, hash: u64) -> bool

Returns false only when hash was definitely never inserted.

Source

pub fn layout(&self) -> &ModuleLayout

Shape the filter was built with, whatever its current residency.

Source

pub fn resident_modules(&self) -> u32

Modules currently held in memory.

Source

pub fn resident_bytes(&self) -> usize

Bytes the resident modules occupy.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn false_positive_rate(&self) -> f64

False positive rate at the current residency, from the measured fill.

Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ModularBloomFilter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.