pub struct ConcurrentHashMap<K, V, S = RandomState> { /* private fields */ }Expand description
Concurrent hash map with segment-based locking for scalability. This provides better scalability than a single mutex-protected HashMap.
Implementations§
Source§impl<K: Hash + Eq, V> ConcurrentHashMap<K, V>
impl<K: Hash + Eq, V> ConcurrentHashMap<K, V>
Sourcepub fn with_segments(num_segments: usize) -> Self
pub fn with_segments(num_segments: usize) -> Self
Create with a specific number of segments (must be power of 2).
Source§impl<K: Hash + Eq, V, S: BuildHasher> ConcurrentHashMap<K, V, S>
impl<K: Hash + Eq, V, S: BuildHasher> ConcurrentHashMap<K, V, S>
Sourcepub fn insert(&self, key: K, value: V) -> Result<Option<V>, SegmentPoisoned>
pub fn insert(&self, key: K, value: V) -> Result<Option<V>, SegmentPoisoned>
Insert a key-value pair.
Returns the previous value if the key existed, or None if it was a new key.
§Errors
Returns SegmentPoisoned if the segment lock was poisoned by a
panicked writer.
Sourcepub fn get_or_insert_with<F>(
&self,
key: K,
default: F,
) -> Result<V, SegmentPoisoned>
pub fn get_or_insert_with<F>( &self, key: K, default: F, ) -> Result<V, SegmentPoisoned>
Get a value by key, or insert it if it is not present.
Executes atomically under the segment’s write lock, ensuring that the
default closure runs exactly once on a cache miss and no concurrent insert
can overwrite it.
§Errors
Returns SegmentPoisoned if the segment lock was poisoned by a
panicked writer.
Sourcepub fn get(&self, key: &K) -> Result<Option<V>, SegmentPoisoned>where
V: Clone,
pub fn get(&self, key: &K) -> Result<Option<V>, SegmentPoisoned>where
V: Clone,
Get a value by key.
Returns the cloned value if found, or None if not found.
§Errors
Returns SegmentPoisoned if the segment lock was poisoned by a
panicked writer.
Sourcepub fn remove(&self, key: &K) -> Result<Option<V>, SegmentPoisoned>
pub fn remove(&self, key: &K) -> Result<Option<V>, SegmentPoisoned>
Remove a key-value pair.
Returns the removed value if the key existed, or None if it didn’t exist.
§Errors
Returns SegmentPoisoned if the segment lock was poisoned by a
panicked writer.
Sourcepub fn contains_key(&self, key: &K) -> Result<bool, SegmentPoisoned>
pub fn contains_key(&self, key: &K) -> Result<bool, SegmentPoisoned>
Check if a key exists.
Returns true if the key exists, false otherwise.
§Errors
Returns SegmentPoisoned if the segment lock was poisoned by a
panicked writer.