pub struct FabricHashMap<K, V> { /* private fields */ }Expand description
A hash map stored in leased fabric memory with open addressing.
Keys and values are serialized via postcard into fixed-size slots.
Collision resolution uses linear probing. The map owns its MemLease
and releases it on drop.
The maximum load factor is 75%. Inserting beyond this threshold returns
FabricError::CapacityExceeded.
§Type parameters
K: Key type. Must implementSerialize + DeserializeOwned + PartialEq.V: Value type. Must implementSerialize + DeserializeOwned.
§Example
use grafos_collections::map::FabricHashMap;
use grafos_std::mem::MemBuilder;
let mut map: FabricHashMap<u32, u32> = FabricHashMap::with_capacity(64, 8, 8)?;
map.insert(&1, &100)?;
map.insert(&2, &200)?;
assert_eq!(map.get(&1)?, Some(100));
assert_eq!(map.remove(&2)?, Some(200));Implementations§
Source§impl<K: Serialize + DeserializeOwned + PartialEq, V: Serialize + DeserializeOwned> FabricHashMap<K, V>
impl<K: Serialize + DeserializeOwned + PartialEq, V: Serialize + DeserializeOwned> FabricHashMap<K, V>
Sourcepub fn new(
lease: MemLease,
key_stride: usize,
val_stride: usize,
) -> Result<Self>
pub fn new( lease: MemLease, key_stride: usize, val_stride: usize, ) -> Result<Self>
Create a new hash map taking ownership of the given lease.
key_stride and val_stride are the maximum serialized sizes (in
bytes) for keys and values respectively. The number of buckets is
derived from (arena_size - 32) / bucket_size.
All buckets are zeroed (marked empty) during construction.
§Errors
Returns FabricError::CapacityExceeded if the arena is too small
to hold the 32-byte header plus at least one bucket.
Sourcepub fn from_lease(lease: MemLease) -> Result<Self>
pub fn from_lease(lease: MemLease) -> Result<Self>
Recover a hash map from an existing memory lease.
Reads the 32-byte header to discover count, bucket_count,
key_stride, and val_stride. This enables crash recovery: a
replacement tasklet can call MemBuilder::attach(lease_id) and
then FabricHashMap::from_lease() to reconnect to a surviving
hot-tier hash map without re-inserting data.
§Errors
FabricError::CapacityExceededif the header is inconsistent with the arena size.FabricError::Disconnectedif the read fails.
Sourcepub fn with_capacity(
bucket_count: usize,
key_stride: usize,
val_stride: usize,
) -> Result<Self>
pub fn with_capacity( bucket_count: usize, key_stride: usize, val_stride: usize, ) -> Result<Self>
Create a new hash map by acquiring a lease sized for bucket_count
buckets.
This is a convenience constructor that acquires a lease via
MemBuilder and then calls FabricHashMap::new.
§Errors
Returns FabricError::CapacityExceeded if the host cannot provide
an arena large enough.
Sourcepub fn insert(&mut self, key: &K, value: &V) -> Result<Option<V>>
pub fn insert(&mut self, key: &K, value: &V) -> Result<Option<V>>
Insert a key-value pair. Returns the previous value if the key already existed.
If the key is already present, the value is overwritten and the old
value is returned as Some(old). If the key is new, returns None.
§Errors
FabricError::CapacityExceededif inserting would exceed the 75% load factor, or if the serialized key/value exceeds their respective strides.FabricError::IoErrorif postcard serialization fails.
Sourcepub fn get(&self, key: &K) -> Result<Option<V>>
pub fn get(&self, key: &K) -> Result<Option<V>>
Look up a value by key.
Returns Some(value) if the key exists, None otherwise.
§Errors
Returns FabricError::IoError if serialization or remote read fails.
Sourcepub fn remove(&mut self, key: &K) -> Result<Option<V>>
pub fn remove(&mut self, key: &K) -> Result<Option<V>>
Remove a key-value pair. Returns the removed value if the key existed.
The bucket is tombstoned rather than cleared to preserve the linear probing chain. Tombstoned buckets are reused by subsequent inserts.
§Errors
Returns FabricError::IoError if serialization or remote I/O fails.
Sourcepub fn contains_key(&self, key: &K) -> Result<bool>
pub fn contains_key(&self, key: &K) -> Result<bool>
Returns true if the map contains the given key.
Equivalent to self.get(key)?.is_some().
Sourcepub fn lease_id(&self) -> u128
pub fn lease_id(&self) -> u128
Returns the lease ID of the memory lease for external renewal
management (e.g. via [grafos_leasekit::RenewalManager]).
Sourcepub fn expires_at_unix_secs(&self) -> u64
pub fn expires_at_unix_secs(&self) -> u64
Returns the expiry time (unix seconds) of the memory lease for external renewal management.
Sourcepub fn iter(&self) -> FabricHashMapIter<'_, K, V> ⓘ
pub fn iter(&self) -> FabricHashMapIter<'_, K, V> ⓘ
Returns an iterator over all key-value pairs.
The iterator scans all buckets, skipping empty and tombstoned slots. Iteration order is determined by bucket layout, not insertion order.