Skip to main content

BoundedHashMap

Struct BoundedHashMap 

Source
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>

Source

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.

Source

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.

Source

pub fn get_mut(&mut self, key: &K) -> Option<&mut V>

Returns a mutable reference to the value for the given key, or None.

Source

pub fn get(&self, key: &K) -> Option<&V>

Returns a reference to the value for the given key, or None.

Source

pub fn len(&self) -> usize

Returns the number of entries currently stored.

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no entries.

Source

pub fn capacity(&self) -> usize

Returns the maximum number of entries before eviction begins.

Source

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).

Source

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);
    }
}

Trait Implementations§

Source§

impl<K: Clone + Hash + Eq, V> Default for BoundedHashMap<K, V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K, V, S> Freeze for BoundedHashMap<K, V, S>

§

impl<K, V, S> RefUnwindSafe for BoundedHashMap<K, V, S>

§

impl<K, V, S> Send for BoundedHashMap<K, V, S>
where S: Send, K: Send, V: Send,

§

impl<K, V, S> Sync for BoundedHashMap<K, V, S>
where S: Sync, K: Sync, V: Sync,

§

impl<K, V, S> Unpin for BoundedHashMap<K, V, S>
where S: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, S> UnsafeUnpin for BoundedHashMap<K, V, S>

§

impl<K, V, S> UnwindSafe for BoundedHashMap<K, V, S>
where K: UnwindSafe, V: UnwindSafe, S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V