feldera_modular_bloom/lib.rs
1//! Bloom filters split into independently droppable modules.
2//!
3//! Implements the modular Bloom filter design described in ElasticBF and SHaMBa papers.
4//!
5//! A [`ModularBloomFilter`] is `D` independent Bloom filters over the same key
6//! set. Membership is the conjunction across modules, so
7//! dropping a trailing module raises the false positive rate but never produces
8//! a false negative. That makes filter accuracy a dial an owner can turn at any
9//! time, in either direction, instead of a property fixed when the filter was
10//! built.
11//!
12//! Dropping a module removes `1/D` of the bits and `1/D` of the hashes at the
13//! same time, so the ratio of hashes to bits stays at its optimum and every
14//! prefix of the modules is as accurate as a filter purpose-built at that size.
15//!
16//! # Scope
17//!
18//! This crate builds and probes filters in memory. It defines
19//! no serialization format. A caller that persists filters is responsible for
20//! storing [`ModuleLayout`] and the module words, and for handing them back
21//! to [`ModularBloomFilter::from_modules`]. Module words are exposed as plain
22//! `&[u64]` so the caller chooses the encoding, the framing and the checksums.
23//!
24//! # Interoperating with monolithic filters
25//!
26//! A monolithic `fastbloom` filter is exactly a modular filter with one module:
27//! module 0 sets and tests precisely the bits a bare `fastbloom::BloomFilter`
28//! would. [`ModuleLayout::monolithic`] describes such a filter, so one can be
29//! loaded and probed with no conversion and no separate code path. It has a
30//! single module, so there is nothing in it to truncate.
31//!
32//! # Building and probing
33//!
34//! Insertion and truncation are separated by type. [`ModularBloomFilterBuilder`]
35//! accepts keys, [`ModularBloomFilter`] answers queries and resizes.
36//!
37//! Only a filter holding every module may be inserted into. A key inserted
38//! while some modules are dropped is recorded in the modules present and
39//! missing from the ones absent. Membership is the conjunction over the
40//! modules held, so the key is found while they stay dropped, and lost as soon
41//! as any of them is read back from storage. Splitting the two roles by type
42//! means no such filter can be built.
43//!
44//! # Hashing
45//!
46//! Callers supply the key hash. The crate never sees the key itself, which keeps
47//! hashing policy with the caller and lets one hash serve every filter consulted
48//! during a lookup.
49//!
50//! One hash chain is threaded across all the modules, so **the caller must
51//! supply a hash that is well distributed across all 64 bits**. The chain seeds
52//! itself from the top half of the hash, so a caller whose high bits carry
53//! little entropy degrades every module at once, rather than just the first.
54//! xxh3 is a good choice. Measured with the top 32 bits pinned to zero, a
55//! four-module filter admits 4.3% of absent keys against a nominal 0.01%, so
56//! this is a real precondition and not a formality.
57
58mod bits;
59mod density;
60mod error;
61mod filter;
62mod layout;
63
64pub use density::ModuleDensity;
65pub use error::{LayoutError, LoadError};
66pub use filter::{ModularBloomFilter, ModularBloomFilterBuilder};
67pub use layout::ModuleLayout;