Skip to main content

SharedHashMap

Struct SharedHashMap 

Source
pub struct SharedHashMap<K: Copy + Eq + 'static, V: Copy + 'static> { /* private fields */ }

Implementations§

Source§

impl<K: Copy + Eq + 'static, V: Copy + 'static> SharedHashMap<K, V>

Source

pub fn create(path: impl AsRef<Path>, capacity: usize) -> Result<Self, MapError>

Obtain the map at path, initializing an empty one if the path does not yet exist and attaching to it if it does. Attaching leaves live entries in place; a region built with a different capacity or payload type is a LayoutMismatch. reset reinitializes.

Source

pub fn reset(path: impl AsRef<Path>, capacity: usize) -> Result<Self, MapError>

Truncate the map at path and initialize a fresh empty one, discarding whatever entries a live peer holds. For a caller that knows it owns the path.

Source

pub fn open( path: impl AsRef<Path>, expected_capacity: usize, ) -> Result<Self, MapError>

Source

pub fn capacity(&self) -> usize

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn insert(&self, key: K, value: V) -> Result<InsertOutcome, MapError>

Insert or update. Returns Inserted for a new key, Updated when an existing key’s value was overwritten, Err(Full) if the table has no slot for the key (probed every slot without finding Empty, a key match, or a reclaimable tombstone).

§Tombstone reuse

Insert tracks the FIRST tombstone seen during the probe. If the probe terminates at an Empty (key absent) AND a tombstone was seen, the tombstone slot is reclaimed instead of consuming the Empty. This eliminates the need for an explicit compact() call in steady-state insert/remove workloads. compact() is still useful for bulk reclamation in workloads that don’t naturally trigger reuse (e.g. a long insert-only period after heavy removes).

Source

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

Look up a key. Returns None if absent.

Source

pub fn contains_key(&self, key: &K) -> bool

True if key is present.

Source

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

Remove a key. Returns the value if present.

Source

pub fn clear(&self)

Clear the entire map. Marks every slot Empty and resets both the live count and the tombstone counter to 0. Not concurrency-safe vs concurrent insert/remove - callers should ensure no other writers are active when calling this.

Source

pub fn tombstone_count(&self) -> usize

Current tombstone count (slots marked dead by remove that have not yet been reclaimed by compact).

Source

pub fn should_compact(&self, threshold_fraction: f64) -> bool

Heuristic: returns true if tombstones occupy at least threshold_fraction of capacity. Callers typically pass 0.30 (30 %) - past that, linear-probe chains stretch out and lookup/insert latency degrades sharply. Cheap O(1).

Source

pub fn compact(&self) -> Result<usize, MapError>

Reclaim tombstones via in-place rebuild. Returns the number of slots reclaimed.

§What it does

Snapshots every Occupied slot into a Vec<(K, V)>, resets every slot to Empty (zeroing both counters), then re-inserts each snapshotted pair via the normal probe. Since no tombstones remain, every key lands as close to its ideal slot as the live keys permit - probe chains shrink back to the no-deletion baseline.

§Concurrency

NOT concurrency-safe with insert / remove. The caller MUST guarantee no other writer (in any process holding an MMF handle to the same file) is mutating the map during compact. Readers calling get will see a transient empty state mid-rebuild and may return spurious None for keys that are about to be re-inserted; if that is unacceptable, serialise readers too.

§Cost

O(capacity) for the snapshot + reset, O(live_count * avg_probe) for re-insert. Allocates a temporary Vec<(K, V)> sized to the live count. For a 1 M-slot map at 50 % load, expect ~tens of milliseconds.

Source

pub fn snapshot(&self) -> Vec<(K, V)>

Walk and collect all (K, V) pairs currently present. Best- effort snapshot under concurrent writers.

Source

pub fn load_factor(&self) -> f64

Current load factor (count / capacity).

Source

pub fn flush(&self) -> Result<(), MapError>

Source

pub fn flush_async(&self) -> Result<(), MapError>

Non-blocking flush: schedules a writeback via the OS. Note: Windows is only partially async (sync to page cache, not to disk).

Trait Implementations§

Source§

impl<K: Copy + Eq + Send + Sync + 'static, V: Copy + Send + Sync + 'static> AdaptiveInstance for SharedHashMap<K, V>

Source§

fn header(&self) -> &HandshakeHeader

Source§

fn ring(&self) -> &ObservationRing

Source§

fn make_policy(&self) -> Box<dyn Policy>

Source§

fn apply_migration(&self, new_tag: u32)

Called by the sidecar when the policy returns a new strategy tag. Default implementation: just set the tag on the header. Primitives that need heavier migration (data-layout swap) override this to perform the swap before (or after) updating the tag.
Source§

impl<K: Copy + Eq + Send + 'static, V: Copy + Send + 'static> Send for SharedHashMap<K, V>

Source§

impl<K: Copy + Eq + Sync + 'static, V: Copy + Sync + 'static> Sync for SharedHashMap<K, V>

Auto Trait Implementations§

§

impl<K, V> !Freeze for SharedHashMap<K, V>

§

impl<K, V> !UnwindSafe for SharedHashMap<K, V>

§

impl<K, V> RefUnwindSafe for SharedHashMap<K, V>

§

impl<K, V> Unpin for SharedHashMap<K, V>

§

impl<K, V> UnsafeUnpin for SharedHashMap<K, V>

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