pub struct LWWMap<K: Eq + Hash + Ord, V> {
pub entries: Vec<(K, (V, u64, String))>,
pub vclock: VectorClock,
}Expand description
LWW-Map: A Last-Write-Wins Map CRDT.
An LWW-Map is a key-value map where each entry independently resolves conflicts using Last-Write-Wins (LWW) semantics. This is achieved by storing a timestamp and node identifier for each key-value pair.
§Key Properties
- Map Semantics: Stores key-value pairs.
- Per-Key LWW: Each key’s value is determined by the latest timestamp.
- Add/Update Wins: Updates with higher timestamps overwrite older ones.
§Algebraic Properties
- Commutativity: Yes.
- Associativity: Yes.
- Idempotence: Yes.
§Example
use crdt_data_types::LWWMap;
let mut map1 = LWWMap::new();
map1.insert("node_a", "key1".to_string(), "value1".to_string(), 100);
let mut map2 = LWWMap::new();
map2.insert("node_b", "key1".to_string(), "value2".to_string(), 200);
map1.merge(&map2);
assert_eq!(map1.get(&"key1".to_string()), Some(&"value2".to_string())); // Higher timestamp winsFields§
§entries: Vec<(K, (V, u64, String))>Internal storage for map entries: key -> (value, timestamp, node_id).
vclock: VectorClockVector clock representing the causal history of the map.
Implementations§
Source§impl<K, V> LWWMap<K, V>
impl<K, V> LWWMap<K, V>
Sourcepub fn insert(&mut self, node_id: &str, key: K, value: V, timestamp: u64)
pub fn insert(&mut self, node_id: &str, key: K, value: V, timestamp: u64)
Inserts or updates a value for a specific key.
The update is applied only if the new timestamp is higher than the current one for that key, or if they are equal and the new node_id is lexicographically greater.
Trait Implementations§
Source§impl<K, V> Crdt for LWWMap<K, V>
impl<K, V> Crdt for LWWMap<K, V>
Source§type Reader<'a> = LWWMapReader<'a, K, V>
type Reader<'a> = LWWMapReader<'a, K, V>
Zero-copy reader type associated with this CRDT.
Source§fn merge_from_readers(readers: &[Self::Reader<'_>]) -> Result<Self, CrdtError>
fn merge_from_readers(readers: &[Self::Reader<'_>]) -> Result<Self, CrdtError>
Merges N CRDTs from zero-copy readers into a single new CRDT instance. Read more
Source§impl<'de, K, V> Deserialize<'de> for LWWMap<K, V>
impl<'de, K, V> Deserialize<'de> for LWWMap<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>,
Deserialize this value from the given Serde deserializer. Read more
impl<K: Eq + Eq + Hash + Ord, V: Eq> Eq for LWWMap<K, V>
impl<K: PartialEq + Eq + Hash + Ord, V: PartialEq> StructuralPartialEq for LWWMap<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for LWWMap<K, V>
impl<K, V> RefUnwindSafe for LWWMap<K, V>
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>
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