Skip to main content

LWWMap

Struct LWWMap 

Source
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 wins

Implementations§

Source§

impl<K: Ord + Clone, V: Clone> LWWMap<K, V>

Source

pub fn new() -> Self

Create a new empty LWW-Map.

Source

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.

Source

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.

Source

pub fn get(&self, key: &K) -> Option<&V>

Get the value associated with a key, if it exists and is alive.

Source

pub fn contains_key(&self, key: &K) -> bool

Check if a key is present and alive in the map.

Source

pub fn len(&self) -> usize

Get the number of alive keys.

Source

pub fn is_empty(&self) -> bool

Check if the map has no alive keys.

Source

pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>

Iterate over alive key-value pairs.

Source

pub fn keys(&self) -> impl Iterator<Item = &K>

Get all alive keys.

Source

pub fn values(&self) -> impl Iterator<Item = &V>

Get all alive values.

Trait Implementations§

Source§

impl<K: Clone + Ord + Clone, V: Clone + Clone> Clone for LWWMap<K, V>

Source§

fn clone(&self) -> LWWMap<K, V>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Ord + Clone, V: Clone> Crdt for LWWMap<K, V>

Source§

fn merge(&mut self, other: &Self)

Merge another replica’s state into this one. Read more
Source§

impl<K: Debug + Ord + Clone, V: Debug + Clone> Debug for LWWMap<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Ord + Clone, V: Clone> Default for LWWMap<K, V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<K: Ord + Clone, V: Clone> DeltaCrdt for LWWMap<K, V>

Source§

type Delta = LWWMapDelta<K, V>

The type of delta produced by this CRDT.
Source§

fn delta(&self, other: &Self) -> LWWMapDelta<K, V>

Generate a delta containing changes in self that other does not have. Read more
Source§

fn apply_delta(&mut self, delta: &LWWMapDelta<K, V>)

Apply a delta to this replica’s state. Read more
Source§

impl<K: PartialEq + Ord + Clone, V: PartialEq + Clone> PartialEq for LWWMap<K, V>

Source§

fn eq(&self, other: &LWWMap<K, V>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K: Ord + Clone, V: Clone> Versioned for LWWMap<K, V>

Source§

const CURRENT_VERSION: u8 = 1

Current schema version for this CRDT type’s serialization format.
Source§

const CRDT_TYPE: CrdtType = CrdtType::LWWMap

The CRDT type identifier for the envelope.
Source§

impl<K: Eq + Ord + Clone, V: Eq + Clone> Eq for LWWMap<K, V>

Source§

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>

§

impl<K, V> Send for LWWMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for LWWMap<K, V>
where K: Sync, V: Sync,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.