pub struct HashIndex<K: Hash + Eq, V: Copy> { /* private fields */ }Expand description
A thread-safe hash index for O(1) key lookups.
Backed by DashMap (sharded concurrent hash map) for lock-free reads. Each read operation only locks one of ~16 internal shards, enabling high concurrent throughput without global lock contention.
Best for exact-match queries on unique keys.
§Performance
- Concurrent reads: ~6x faster than RwLock under contention
- Point lookups: O(1) average case
- No lock acquisition for reads in common case
§Example
use grafeo_core::index::HashIndex;
use grafeo_common::types::NodeId;
let index: HashIndex<String, NodeId> = HashIndex::new();
index.insert("alix".to_string(), NodeId::new(1));
index.insert("gus".to_string(), NodeId::new(2));
assert_eq!(index.get(&"alix".to_string()), Some(NodeId::new(1)));Implementations§
Source§impl<K: Hash + Eq, V: Copy> HashIndex<K, V>
impl<K: Hash + Eq, V: Copy> HashIndex<K, V>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new hash index with the given capacity.
Sourcepub fn insert(&self, key: K, value: V) -> Option<V>
pub fn insert(&self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the index.
Returns the previous value if the key was already present.
Sourcepub fn get(&self, key: &K) -> Option<V>
pub fn get(&self, key: &K) -> Option<V>
Gets the value for a key.
This is a lock-free read operation that only briefly locks one of the internal shards.
Trait Implementations§
Auto Trait Implementations§
impl<K, V> Freeze for HashIndex<K, V>
impl<K, V> !RefUnwindSafe for HashIndex<K, V>
impl<K, V> Send for HashIndex<K, V>
impl<K, V> Sync for HashIndex<K, V>
impl<K, V> Unpin for HashIndex<K, V>
impl<K, V> UnsafeUnpin for HashIndex<K, V>
impl<K, V> UnwindSafe for HashIndex<K, V>where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more