pub struct SyncBtreeMap<K: Eq + Hash, V> { /* private fields */ }Expand description
A concurrent BTreeMap with a Go sync.Map-style read/dirty architecture:
read: an immutable snapshot, atomically published.get/iter/Indexread it lock-free.dirty: the canonical, mutable map, guarded bylock.
Every slot is an Arc<Entry<V>> shared between the snapshot and dirty.
The entry holds an atomic pointer to the value, so updating an existing key
swaps the pointer in place (O(1)) — no snapshot rebuild — and readers
always see the latest value. New keys and removals are published lazily
(tracked by the amended flag). Snapshots and retired values are kept
alive until the map is dropped, so references returned by get stay valid.
Implementations§
Source§impl<K, V> SyncBtreeMap<K, V>
impl<K, V> SyncBtreeMap<K, V>
pub fn new_arc() -> Arc<Self> ⓘ
pub fn new() -> Self
pub fn with_capacity(_capacity: usize) -> Self
pub fn with_map(map: BTreeMap<K, V>) -> Selfwhere
K: Ord,
pub fn insert(&self, k: K, v: V) -> Option<V>
pub fn insert_mut(&mut self, k: K, v: V) -> Option<V>
pub fn remove(&self, k: &K) -> Option<V>
pub fn remove_mut(&mut self, k: &K) -> Option<V>
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn clear(&self)
pub fn clear_mut(&mut self)
pub fn shrink_to_fit(&self)
pub fn shrink_to_fit_mut(&mut self)
pub fn from(map: BTreeMap<K, V>) -> Self
Sourcepub fn get<Q>(&self, k: &Q) -> Option<&V>
pub fn get<Q>(&self, k: &Q) -> Option<&V>
Returns a reference to the value corresponding to the key.
The key may be any borrowed form of the map’s key type, but
Hash and Eq on the borrowed form must match those for
the key type.
Reads are lock-free: the value is served from the immutable read
snapshot through a shared entry, so updates are visible immediately.
If the key was added to dirty since the last snapshot was published,
a fresh snapshot is published first.
§Examples
use dark_std::sync::{SyncBtreeMap};
let mut map = SyncBtreeMap::new();
map.insert_mut(1, "a");
assert_eq!(*map.get(&1).unwrap(), "a");
assert_eq!(map.get(&2).is_none(), true);Sourcepub fn get_mut(&self, k: &K) -> Option<BtreeMapRefMut<'_, K, V>>
pub fn get_mut(&self, k: &K) -> Option<BtreeMapRefMut<'_, K, V>>
Returns a mutable handle to the value for k, implemented with
copy-on-write: the value is cloned, the handle mutates the clone, and
the result is swapped back into the shared entry (O(1)) when the handle
is dropped. Concurrent readers may observe the pre-mutation value until
the handle is dropped.
pub fn contains_key(&self, x: &K) -> bool
Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘwhere
K: Clone,
pub fn iter(&self) -> Iter<'_, K, V> ⓘwhere
K: Clone,
Iterate over the current contents. A fresh snapshot is published first, so all entries written so far are visible.
pub fn iter_mut(&self) -> IterMut<'_, K, V> ⓘ
pub fn into_iter(self) -> MapIntoIter<K, V> ⓘwhere
K: Ord,
pub fn into_inner(self) -> BTreeMap<K, V>where
K: Ord,
Trait Implementations§
Source§impl<K, V> Debug for SyncBtreeMap<K, V>
impl<K, V> Debug for SyncBtreeMap<K, V>
Source§impl<'de, K, V> Deserialize<'de> for SyncBtreeMap<K, V>
impl<'de, K, V> Deserialize<'de> for SyncBtreeMap<K, V>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<K, V> Display for SyncBtreeMap<K, V>
impl<K, V> Display for SyncBtreeMap<K, V>
Source§impl<K, V> Index<&K> for SyncBtreeMap<K, V>
impl<K, V> Index<&K> for SyncBtreeMap<K, V>
Source§impl<'a, K, V> IntoIterator for &'a SyncBtreeMap<K, V>
impl<'a, K, V> IntoIterator for &'a SyncBtreeMap<K, V>
Source§impl<K: Eq + Hash + Ord, V> IntoIterator for SyncBtreeMap<K, V>
impl<K: Eq + Hash + Ord, V> IntoIterator for SyncBtreeMap<K, V>
impl<K: Eq + Hash, V> Send for SyncBtreeMap<K, V>
Safety: dirty is only ever accessed under lock; the read snapshot is
immutable once published; values behind entries are immutable once
published and swapped out atomically; retired values and retired snapshots
are kept alive until the map is dropped, so references derived from get
remain valid for the lifetime of &self.