-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod.rs
49 lines (39 loc) · 1.45 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use std::collections::hash_map::DefaultHasher;
use std::hash::Hash;
use std::hash::Hasher;
use tlms::telegrams::r09::R09Telegram;
pub const DEPULICATION_BUFFER_SIZE: usize = 30;
// Saves the hashes of last n telegrams
// if a new telegram is received we check if its contained
// in this circular buffer.
pub struct Filter {
pub last_elements: [u64; DEPULICATION_BUFFER_SIZE], // vector of hashes for deduplication
pub iterator: usize, // keeps track of the oldest element
}
impl Filter {
pub fn new() -> Filter {
Filter {
last_elements: [0; DEPULICATION_BUFFER_SIZE],
iterator: 0,
}
}
pub async fn calculate_hash<T: Hash>(t: &T) -> u64 {
let mut s = DefaultHasher::new();
t.hash(&mut s);
s.finish()
}
// checks if the given telegram hash is contained in the filter class
pub async fn deduplicate(self: &mut Filter, telegram: &R09Telegram) -> bool {
let telegram_hash = Self::calculate_hash(telegram).await;
// checks if the given telegram is already in the buffer
let contained = self.last_elements.contains(&telegram_hash);
if contained {
return true;
}
// updates the buffer adding the new telegram
let index = self.iterator;
self.last_elements[index] = telegram_hash;
self.iterator = (self.iterator + 1) % DEPULICATION_BUFFER_SIZE;
contained
}
}