-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sascha Grunert <[email protected]>
- Loading branch information
1 parent
526bab4
commit b5702dc
Showing
17 changed files
with
1,827 additions
and
266 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
use std::{ | ||
collections::HashMap, | ||
fmt::Debug, | ||
hash::Hash, | ||
time::{Duration, Instant}, | ||
}; | ||
use tracing::warn; | ||
|
||
#[derive(Debug)] | ||
/// A HashMap bounded by element age and maximum amount of items | ||
pub struct BoundedHashMap<K, V> { | ||
map: HashMap<K, (Instant, V)>, | ||
max_duration: Duration, | ||
max_items: usize, | ||
} | ||
|
||
impl<K, V> BoundedHashMap<K, V> | ||
where | ||
K: Eq + Hash + Clone + Debug, | ||
V: Debug, | ||
{ | ||
/// Insert an element into the hashmap by: | ||
/// - removing timed-out elements | ||
/// - removing the oldest element if no space left | ||
pub fn insert(&mut self, k: K, v: V) -> Option<V> { | ||
let now = Instant::now(); | ||
|
||
// Remove timed-out items | ||
let old_len = self.map.len(); | ||
self.map | ||
.retain(|_, (inserted, _)| now - *inserted <= self.max_duration); | ||
if old_len < self.map.len() { | ||
warn!("Removed {} timed out elements", self.map.len() - old_len) | ||
} | ||
|
||
// Remove the oldest entry if still not enough space left | ||
if self.map.len() >= self.max_items { | ||
let mut key_to_remove = k.clone(); | ||
|
||
let mut oldest = now; | ||
for (key, (inserted, _)) in self.map.iter() { | ||
if *inserted < oldest { | ||
oldest = *inserted; | ||
key_to_remove = key.clone(); | ||
} | ||
} | ||
|
||
warn!("Removing oldest key: {:?}", key_to_remove); | ||
self.map.remove(&key_to_remove); | ||
} | ||
|
||
self.map.insert(k, (Instant::now(), v)).map(|v| v.1) | ||
} | ||
|
||
/// Remove an element from the hashmap and return it if the element has not expired. | ||
pub fn remove(&mut self, k: &K) -> Option<V> { | ||
let now = Instant::now(); | ||
|
||
if let Some((key, (inserted, value))) = self.map.remove_entry(k) { | ||
if now - inserted > self.max_duration { | ||
warn!("Max duration expired for key: {:?}", key); | ||
None | ||
} else { | ||
Some(value) | ||
} | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
impl<K, V> Default for BoundedHashMap<K, V> { | ||
fn default() -> Self { | ||
Self { | ||
map: Default::default(), | ||
max_duration: Duration::new(60 * 60, 0), // 1 hour | ||
max_items: 1000, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::thread::sleep; | ||
|
||
#[test] | ||
fn bounded_hashmap_test() { | ||
let mut sut = BoundedHashMap::default(); | ||
sut.max_items = 2; | ||
|
||
assert_eq!(sut.map.len(), 0); | ||
|
||
// Insert first item should be fine | ||
assert!(sut.insert(0, 0).is_none()); | ||
assert_eq!(sut.map.len(), 1); | ||
|
||
// Insert second item should be fine, removal should work as well | ||
assert!(sut.insert(1, 0).is_none()); | ||
assert_eq!(sut.map.len(), 2); | ||
assert!(sut.remove(&1).is_some()); | ||
assert_eq!(sut.map.len(), 1); | ||
assert!(sut.insert(1, 0).is_none()); | ||
|
||
// Insert third item should be fine, but remove oldest | ||
assert!(sut.insert(2, 0).is_none()); | ||
assert_eq!(sut.map.len(), 2); | ||
assert!(sut.map.get(&0).is_none()); | ||
assert!(sut.map.get(&1).is_some()); | ||
assert!(sut.map.get(&2).is_some()); | ||
|
||
// Insert another item should be fine, but remove oldest | ||
assert!(sut.insert(3, 0).is_none()); | ||
assert_eq!(sut.map.len(), 2); | ||
assert!(sut.map.get(&1).is_none()); | ||
assert!(sut.map.get(&2).is_some()); | ||
assert!(sut.map.get(&3).is_some()); | ||
|
||
// Change the max age of the elements, all should be timed out | ||
sut.max_duration = Duration::from_millis(100); | ||
sleep(Duration::from_millis(200)); | ||
assert!(sut.insert(0, 0).is_none()); | ||
assert!(sut.map.get(&1).is_none()); | ||
assert!(sut.map.get(&2).is_none()); | ||
assert!(sut.map.get(&3).is_none()); | ||
|
||
// The last element should be also timed out if we wait | ||
sleep(Duration::from_millis(200)); | ||
assert!(sut.remove(&0).is_none()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.