Struct garage_table::crdt::LwwMap[][src]

pub struct LwwMap<K, V> { /* fields omitted */ }
Expand description

Last Write Win Map

This types defines a CRDT for a map from keys to values. The values have an associated timestamp, such that the last written value takes precedence over previous ones. As for the simpler LWW type, the value type V is also required to implement the CRDT trait. We do not encourage mutating the values associated with a given key without updating the timestamp, in fact at the moment we do not provide a .get_mut() method that would allow that.

Internally, the map is stored as a vector of keys and values, sorted by ascending key order. This is why the key type K must implement Ord (and also to ensure a unique serialization, such that two values can be compared for equality based on their hashes). As a consequence, insertions take O(n) time. This means that LWWMap should be used for reasonably small maps. However, note that even if we were using a more efficient data structure such as a BTreeMap, the serialization cost O(n) would still have to be paid at each modification, so we are actually not losing anything here.

Implementations

impl<K, V> LwwMap<K, V> where
    K: Ord,
    V: Crdt
[src]

pub fn new() -> Self[src]

Create a new empty map CRDT

pub fn migrate_from_raw_item(k: K, ts: u64, v: V) -> Self[src]

Used to migrate from a map defined in an incompatible format. This produces a map that contains a single item with the specified timestamp (copied from the incompatible format). Do this as many times as you have items to migrate, and put them all together using the CRDT merge operator.

pub fn update_mutator(&self, k: K, new_v: V) -> Self[src]

Returns a map that contains a single mapping from the specified key to the specified value. This map is a mutator, or a delta-CRDT, such that when it is merged with the original map, the previous value will be replaced with the one specified here. The timestamp in the provided mutator is set to the maximum of the current system’s clock and 1 + the previous value’s timestamp (if there is one), so that the new value will always take precedence (LWW rule).

Typically, to update the value associated to a key in the map, you would do the following:

let my_update = my_crdt.update_mutator(key_to_modify, new_value);
my_crdt.merge(&my_update);

However extracting the mutator on its own and only sending that on the network is very interesting as it is much smaller than the whole map.

pub fn take_and_clear(&mut self) -> Self[src]

Takes all of the values of the map and returns them. The current map is reset to the empty map. This is very usefull to produce in-place a new map that contains only a delta that modifies a certain value:

let mut a = get_my_crdt_value();
let old_a = a.take_and_clear();
a.merge(&old_a.update_mutator(key_to_modify, new_value));
put_my_crdt_value(a);

Of course in this simple example we could have written simply pyt_my_crdt_value(a.update_mutator(key_to_modify, new_value)), but in the case where the map is a field in a struct for instance (as is always the case), this becomes very handy:

let mut a = get_my_crdt_value();
let old_a_map = a.map_field.take_and_clear();
a.map_field.merge(&old_a_map.update_mutator(key_to_modify, new_value));
put_my_crdt_value(a);

pub fn clear(&mut self)[src]

Removes all values from the map

pub fn get(&self, k: &K) -> Option<&V>[src]

Get a reference to the value assigned to a key

pub fn items(&self) -> &[(K, u64, V)][src]

Gets a reference to all of the items, as a slice. Usefull to iterate on all map values. In most case you will want to ignore the timestamp (second item of the tuple).

pub fn len(&self) -> usize[src]

Returns the number of items in the map

pub fn is_empty(&self) -> bool[src]

Returns true if the map is empty

Trait Implementations

impl<K: Clone, V: Clone> Clone for LwwMap<K, V>[src]

fn clone(&self) -> LwwMap<K, V>[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<K, V> Crdt for LwwMap<K, V> where
    K: Clone + Ord,
    V: Clone + Crdt
[src]

fn merge(&mut self, other: &Self)[src]

Merge the two datastructures according to the CRDT rules. self is modified to contain the merged CRDT value. other is not modified. Read more

impl<K: Debug, V: Debug> Debug for LwwMap<K, V>[src]

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

Formats the value using the given formatter. Read more

impl<K, V> Default for LwwMap<K, V> where
    K: Ord,
    V: Crdt
[src]

fn default() -> Self[src]

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

impl<'de, K, V> Deserialize<'de> for LwwMap<K, V> where
    K: Deserialize<'de>,
    V: Deserialize<'de>, 
[src]

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
    __D: Deserializer<'de>, 
[src]

Deserialize this value from the given Serde deserializer. Read more

impl<K: PartialEq, V: PartialEq> PartialEq<LwwMap<K, V>> for LwwMap<K, V>[src]

fn eq(&self, other: &LwwMap<K, V>) -> bool[src]

This method tests for self and other values to be equal, and is used by ==. Read more

fn ne(&self, other: &LwwMap<K, V>) -> bool[src]

This method tests for !=.

impl<K, V> Serialize for LwwMap<K, V> where
    K: Serialize,
    V: Serialize
[src]

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error> where
    __S: Serializer
[src]

Serialize this value into the given Serde serializer. Read more

impl<K, V> StructuralPartialEq for LwwMap<K, V>[src]

Auto Trait Implementations

impl<K, V> RefUnwindSafe for LwwMap<K, V> where
    K: RefUnwindSafe,
    V: RefUnwindSafe

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> where
    K: Unpin,
    V: Unpin

impl<K, V> UnwindSafe for LwwMap<K, V> where
    K: UnwindSafe,
    V: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

fn in_current_span(self) -> Instrumented<Self>[src]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Pointable for T

pub const ALIGN: usize

The alignment of pointer.

type Init = T

The type for initializers.

pub unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more

pub unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more

pub unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more

pub unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]