1 use std::collections::hash_map::RandomState;
2 use std::hash::{BuildHasher, Hash, Hasher};
3 use std::time::{Duration, Instant};
4 use std::marker::PhantomData;
6 // Constants for roughly 1 in 1 million fp with 18m entries
7 /// Number of entries in the filter (each 4 bits). 256MiB in total.
8 const FILTER_SIZE: usize = 64 * 1024 * 1024 * 8;
9 const HASHES: usize = 27;
10 const ROLL_COUNT: usize = 1_240_000;
12 const GENERATION_BITS: usize = 2;
14 const GENERATION_BITS: usize = 4;
15 pub const GENERATION_COUNT: usize = (1 << GENERATION_BITS) - 1;
16 const ELEMENTS_PER_BYTE: usize = 8 / GENERATION_BITS;
18 pub struct RollingBloomFilter<T: Hash> {
20 inserted_in_last_generations: [usize; GENERATION_COUNT - 1],
21 inserted_since_last_roll: usize,
22 current_generation: u8,
24 hash_keys: [RandomState; HASHES],
25 _entry_type: PhantomData<T>,
28 impl<T: Hash> RollingBloomFilter<T> {
29 pub fn new() -> Self {
30 let mut bits = Vec::new();
31 bits.resize(FILTER_SIZE * GENERATION_BITS / 8, 0);
33 last_roll: Instant::now(),
34 inserted_since_last_roll: 0,
35 inserted_in_last_generations: [0; GENERATION_COUNT - 1],
36 current_generation: 1,
38 hash_keys: [RandomState::new(), RandomState::new(), RandomState::new(), RandomState::new(), RandomState::new(),
39 RandomState::new(), RandomState::new(), RandomState::new(), RandomState::new(), RandomState::new(),
40 RandomState::new(), RandomState::new(), RandomState::new(), RandomState::new(), RandomState::new(),
41 RandomState::new(), RandomState::new(), RandomState::new(), RandomState::new(), RandomState::new(),
42 RandomState::new(), RandomState::new(), RandomState::new(), RandomState::new(), RandomState::new(),
43 RandomState::new(), RandomState::new()],
44 _entry_type: PhantomData,
48 pub fn contains(&self, item: &T) -> bool {
49 for state in self.hash_keys.iter() {
50 let mut hasher = state.build_hasher();
51 item.hash(&mut hasher);
52 let idx = hasher.finish() as usize;
54 let byte = self.bits[(idx / ELEMENTS_PER_BYTE) % (FILTER_SIZE / 8)];
55 let bits_shift = (idx % ELEMENTS_PER_BYTE) * GENERATION_BITS;
56 let bits = (byte & ((GENERATION_COUNT as u8) << bits_shift)) >> bits_shift;
57 if bits == 0 { return false; }
62 pub fn get_element_count(&self) -> [usize; GENERATION_COUNT] {
63 let mut res = [0; GENERATION_COUNT];
64 res[0..(GENERATION_COUNT-1)].copy_from_slice(&self.inserted_in_last_generations);
65 *res.last_mut().unwrap() = self.inserted_since_last_roll;
69 pub fn insert(&mut self, item: &T, roll_duration: Duration) {
70 if Instant::now() - self.last_roll > roll_duration / GENERATION_COUNT as u32 ||
71 self.inserted_since_last_roll > ROLL_COUNT {
72 self.current_generation += 1;
73 if self.current_generation == GENERATION_COUNT as u8 + 1 { self.current_generation = 1; }
74 let remove_generation = self.current_generation;
76 for idx in 0..FILTER_SIZE {
77 let byte = &mut self.bits[(idx / ELEMENTS_PER_BYTE) % (FILTER_SIZE / 8)];
78 let bits_shift = (idx % ELEMENTS_PER_BYTE) * GENERATION_BITS;
79 let bits = (*byte & ((GENERATION_COUNT as u8) << bits_shift)) >> bits_shift;
81 if bits == remove_generation {
82 *byte &= !((GENERATION_COUNT as u8) << bits_shift);
85 self.last_roll = Instant::now();
86 let mut new_generations = [0; GENERATION_COUNT - 1];
87 new_generations[0..GENERATION_COUNT - 2].copy_from_slice(&self.inserted_in_last_generations[1..]);
88 new_generations[GENERATION_COUNT - 2] = self.inserted_since_last_roll;
89 self.inserted_in_last_generations = new_generations;
90 self.inserted_since_last_roll = 0;
93 for state in self.hash_keys.iter() {
94 let mut hasher = state.build_hasher();
95 item.hash(&mut hasher);
96 let idx = hasher.finish() as usize;
98 let byte = &mut self.bits[(idx / ELEMENTS_PER_BYTE) % (FILTER_SIZE / 8)];
99 let bits_shift = (idx % ELEMENTS_PER_BYTE) * GENERATION_BITS;
100 *byte &= !((GENERATION_COUNT as u8) << bits_shift);
101 *byte |= self.current_generation << bits_shift;
103 self.inserted_since_last_roll += 1;
109 let mut filter = RollingBloomFilter::new();
111 filter.insert(&i, Duration::from_secs(60 * 60 * 24));
114 assert!(filter.contains(&i));
116 for i in 1000..2000 {
117 assert!(!filter.contains(&i));
119 assert_eq!(filter.get_element_count(), [0, 0, 1000]);
120 filter.inserted_since_last_roll = ROLL_COUNT + 1;
121 filter.insert(&1000, Duration::from_secs(60 * 60 * 24));
122 assert_eq!(filter.get_element_count(), [0, ROLL_COUNT + 1, 1]);
124 assert!(filter.contains(&i));
126 filter.inserted_since_last_roll = ROLL_COUNT + 1;
127 for i in 1001..2000 {
128 filter.insert(&i, Duration::from_secs(60 * 60 * 24));
130 assert_eq!(filter.get_element_count(), [ROLL_COUNT + 1, ROLL_COUNT + 1, 999]);
132 assert!(filter.contains(&i));
134 filter.inserted_since_last_roll = ROLL_COUNT + 1;
135 filter.insert(&2000, Duration::from_secs(60 * 60 * 24));
136 assert_eq!(filter.get_element_count(), [ROLL_COUNT + 1, ROLL_COUNT + 1, 1]);
138 assert!(!filter.contains(&i));
140 for i in 1000..2001 {
141 assert!(filter.contains(&i));