Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConcurrentBloomInterval #34486

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion bloom/src/bloom.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
//! Simple Bloom Filter
use {
bv::BitVec,
fnv::FnvHasher,
rand::{self, Rng},
serde::{Deserialize, Serialize},
solana_sdk::sanitize::{Sanitize, SanitizeError},
solana_sdk::{
sanitize::{Sanitize, SanitizeError},
timing::AtomicInterval,
},
std::{
cmp, fmt,
hash::Hasher,
marker::PhantomData,
ops::Deref,
sync::atomic::{AtomicU64, Ordering},
},
};
Expand Down Expand Up @@ -228,6 +233,40 @@ impl<T: BloomHashIndex> From<ConcurrentBloom<T>> for Bloom<T> {
}
}

/// Wrapper around `ConcurrentBloom` and `AtomicInterval` so the bloom filter
/// can be cleared periodically.
pub struct ConcurrentBloomInterval<T: BloomHashIndex> {
interval: AtomicInterval,
bloom: ConcurrentBloom<T>,
}

// Directly allow all methods of `AtomicBloom` to be called on `AtomicBloomInterval`.
impl<T: BloomHashIndex> Deref for ConcurrentBloomInterval<T> {
type Target = ConcurrentBloom<T>;
fn deref(&self) -> &Self::Target {
&self.bloom
}
}

impl<T: BloomHashIndex> ConcurrentBloomInterval<T> {
/// Create a new filter with the given parameters.
/// See `Bloom::random` for details.
pub fn new(num_items: usize, false_positive_rate: f64, max_bits: usize) -> Self {
let bloom = Bloom::random(num_items, false_positive_rate, max_bits);
Self {
interval: AtomicInterval::default(),
bloom: ConcurrentBloom::from(bloom),
}
}

/// Reset the filter if the reset interval has elapsed.
pub fn maybe_reset(&self, reset_interval_ms: u64) {
if self.interval.should_update(reset_interval_ms) {
self.bloom.clear();
}
}
}

#[cfg(test)]
mod test {
use {
Expand Down