pub struct LWWMap<K: Ord + Clone, V: Clone> { /* private fields */ }Expand description
A last-writer-wins map (LWW-Map).
Each key maps to a value with a HybridTimestamp. Concurrent writes
to the same key resolve by keeping the value with the highest timestamp.
Keys can be removed; a remove is stored as a tombstone with a timestamp
so that stale puts don’t resurrect deleted keys.
§Example
use crdt_kit::prelude::*;
use crdt_kit::clock::HybridTimestamp;
let ts = |ms, node| HybridTimestamp { physical: ms, logical: 0, node_id: node };
let mut m1 = LWWMap::new();
m1.insert("color", "red", ts(100, 1));
let mut m2 = LWWMap::new();
m2.insert("color", "blue", ts(200, 2));
m1.merge(&m2);
assert_eq!(m1.get(&"color"), Some(&"blue")); // later timestamp winsImplementations§
Source§impl<K: Ord + Clone, V: Clone> LWWMap<K, V>
impl<K: Ord + Clone, V: Clone> LWWMap<K, V>
Sourcepub fn insert(&mut self, key: K, value: V, timestamp: HybridTimestamp)
pub fn insert(&mut self, key: K, value: V, timestamp: HybridTimestamp)
Insert or update a key-value pair with the given timestamp.
If the key already exists with a newer or equal timestamp, this is a no-op.
Sourcepub fn remove(&mut self, key: &K, timestamp: HybridTimestamp) -> bool
pub fn remove(&mut self, key: &K, timestamp: HybridTimestamp) -> bool
Remove a key with the given timestamp.
The removal only takes effect if its timestamp is greater than the
current entry’s timestamp. Returns true if the key was removed.
Sourcepub fn get(&self, key: &K) -> Option<&V>
pub fn get(&self, key: &K) -> Option<&V>
Get the value associated with a key, if it exists and is alive.
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Check if a key is present and alive in the map.
Trait Implementations§
Source§impl<K: Ord + Clone, V: Clone> DeltaCrdt for LWWMap<K, V>
impl<K: Ord + Clone, V: Clone> DeltaCrdt for LWWMap<K, V>
Source§type Delta = LWWMapDelta<K, V>
type Delta = LWWMapDelta<K, V>
The type of delta produced by this CRDT.
Source§fn delta(&self, other: &Self) -> LWWMapDelta<K, V>
fn delta(&self, other: &Self) -> LWWMapDelta<K, V>
Source§fn apply_delta(&mut self, delta: &LWWMapDelta<K, V>)
fn apply_delta(&mut self, delta: &LWWMapDelta<K, V>)
Apply a delta to this replica’s state. Read more
impl<K: Eq + Ord + Clone, V: Eq + Clone> Eq for LWWMap<K, V>
impl<K: Ord + Clone, V: Clone> StructuralPartialEq for LWWMap<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for LWWMap<K, V>
impl<K, V> RefUnwindSafe for LWWMap<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for LWWMap<K, V>
impl<K, V> Sync for LWWMap<K, V>
impl<K, V> Unpin for LWWMap<K, V>
impl<K, V> UnsafeUnpin for LWWMap<K, V>
impl<K, V> UnwindSafe for LWWMap<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
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