pub struct BoundedHashMap<K, V, S = DefaultHasher> { /* private fields */ }Expand description
A fixed-capacity hash map that evicts the oldest entries when full.
When the map is at capacity, each insert pushes out the oldest entry
(FIFO eviction order). This is used to track recently-seen message IDs
for deduplication, preventing unbounded memory growth in long-running
nodes.
§Example
// Module is crate-private; use from within beam.
use beam::utils::BoundedHashMap;
let mut map = BoundedHashMap::new(2);
map.insert("a", 1);
map.insert("b", 2);Implementations§
Source§impl<K: Clone + Hash + Eq, V> BoundedHashMap<K, V>
impl<K: Clone + Hash + Eq, V> BoundedHashMap<K, V>
Sourcepub fn new(max_entries: usize) -> Self
pub fn new(max_entries: usize) -> Self
Creates a new BoundedHashMap with the given maximum capacity.
Uses the default FxHash hasher for non-cryptographic hashing.
§Panics
Does not panic; a capacity of 0 will simply evict on every insert.
Sourcepub fn insert(&mut self, key: K, value: V)
pub fn insert(&mut self, key: K, value: V)
Inserts a key-value pair, evicting the oldest entry if at capacity.
If the key already exists, the value is updated in place and the eviction queue is not modified (the key’s position is preserved). If capacity is 0, the insert is silently dropped.
Sourcepub fn get_mut(&mut self, key: &K) -> Option<&mut V>
pub fn get_mut(&mut self, key: &K) -> Option<&mut V>
Returns a mutable reference to the value for the given key, or None.
Sourcepub fn get(&self, key: &K) -> Option<&V>
pub fn get(&self, key: &K) -> Option<&V>
Returns a reference to the value for the given key, or None.
Sourcepub fn take(&mut self, key: &K) -> Option<V>
pub fn take(&mut self, key: &K) -> Option<V>
Removes and returns the value for the given key, or None.
Also removes the key from the eviction queue to prevent it from being re-inserted as a stale entry on the next FIFO eviction. If you re-insert the same key later, it goes to the front of the queue (most-recently-used).
Sourcepub fn iter(&self) -> impl Iterator<Item = (&K, &V)>
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>
Iterator over all (key, value) pairs.
Used by periodic cleanup tasks (e.g., the quorum reaper) that need to
scan all entries for expiration. Order is unspecified — typically the
HashMap’s random iteration order. For FIFO-scoped iteration, callers
should combine with take() to evict expired entries.
§Examples
for (key, value) in map.iter() {
if should_evict(&value) {
map.take(&key);
}
}