Struct crdts::map::Map

source ·
pub struct Map<K: Ord, V: Val<A>, A: Ord + Hash> { /* private fields */ }
Expand description

Map CRDT - Supports Composition of CRDT’s with reset-remove semantics.

Reset-remove means that if one replica removes an entry while another actor concurrently edits that entry, once we sync these two maps, we will see that the entry is still in the map but all edits seen by the removing actor will be gone.

See examples/reset_remove.rs for an example of reset-remove semantics in action.

Implementations§

source§

impl<K: Ord, V: Val<A>, A: Ord + Hash + Clone> Map<K, V, A>

source

pub fn new() -> Self

Constructs an empty Map

source

pub fn is_empty(&self) -> ReadCtx<bool, A>

Returns true if the map has no entries, false otherwise

source

pub fn len(&self) -> ReadCtx<usize, A>

Returns the number of entries in the Map

source

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

Retrieve value stored under a key

source

pub fn update<F>(&self, key: impl Into<K>, ctx: AddCtx<A>, f: F) -> Op<K, V, A>where F: FnOnce(&V, AddCtx<A>) -> V::Op,

Update a value under some key.

If the key is not present in the map, the updater will be given the result of V::default(). The default value is used to ensure eventual consistency since our Map’s values are CRDTs themselves.

The impl Into<K> bound provides a nice way of providing an input key that can easily convert to the Map’s key. For example, we can call this function with "hello": &str and it can be converted to String.

source

pub fn rm(&self, key: impl Into<K>, ctx: RmCtx<A>) -> Op<K, V, A>

Remove an entry from the Map

The impl Into<K> bound provides a nice way of providing an input key that can easily convert to the Map’s key. For example, we can call this function with "hello": &str and it can be converted to String.

source

pub fn read_ctx(&self) -> ReadCtx<(), A>

Retrieve the current read context

source

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

Gets an iterator over the keys of the Map.

Examples
use crdts::Map;
use crdts::MVReg;
use crdts::CmRDT;

type Actor = &'static str;
type Key = &'static str;

let actor = "actor";

let mut map: Map<i32, MVReg<Key, Actor>, Actor> = Map::new();

let add_ctx = map.read_ctx().derive_add_ctx(actor);
map.apply(map.update(100, add_ctx, |v, a| v.write("foo", a)));

let add_ctx = map.read_ctx().derive_add_ctx(actor);
map.apply(map.update(50, add_ctx, |v, a| v.write("bar", a)));

let add_ctx = map.read_ctx().derive_add_ctx(actor);
map.apply(map.update(200, add_ctx, |v, a| v.write("baz", a)));


let mut keys: Vec<_> = map.keys().map(|key_ctx| *key_ctx.val).collect();

keys.sort();

assert_eq!(keys, &[50, 100, 200]);
source

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

Gets an iterator over the values of the Map.

Examples
use crdts::Map;
use crdts::MVReg;
use crdts::CmRDT;

type Actor = &'static str;
type Key = &'static str;

let actor = "actor";

let mut map: Map<i32, MVReg<Key, Actor>, Actor> = Map::new();

let add_ctx = map.read_ctx().derive_add_ctx(actor);
map.apply(map.update(100, add_ctx, |v, a| v.write("foo", a)));

let add_ctx = map.read_ctx().derive_add_ctx(actor);
map.apply(map.update(50, add_ctx, |v, a| v.write("bar", a)));

let add_ctx = map.read_ctx().derive_add_ctx(actor);
map.apply(map.update(200, add_ctx, |v, a| v.write("baz", a)));


let mut values: Vec<_> = map
    .values()
    .map(|val_ctx| val_ctx.val.read().val[0])
    .collect();

values.sort();

assert_eq!(values, &["bar", "baz", "foo"]);
source

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

Gets an iterator over the entries of the Map.

Examples
use crdts::Map;
use crdts::MVReg;
use crdts::CmRDT;

type Actor = &'static str;
type Key = &'static str;

let actor = "actor";

let mut map: Map<i32, MVReg<Key, Actor>, Actor> = Map::new();

let add_ctx = map.read_ctx().derive_add_ctx(actor);
map.apply(map.update(100, add_ctx, |v, a| v.write("foo", a)));

let add_ctx = map.read_ctx().derive_add_ctx(actor);
map.apply(map.update(50, add_ctx, |v, a| v.write("bar", a)));

let add_ctx = map.read_ctx().derive_add_ctx(actor);
map.apply(map.update(200, add_ctx, |v, a| v.write("baz", a)));


let mut items: Vec<_> = map
    .iter()
    .map(|item_ctx| (*item_ctx.val.0, item_ctx.val.1.read().val[0]))
    .collect();

items.sort();

assert_eq!(items, &[(50, "bar"), (100, "foo"), (200, "baz")]);

Trait Implementations§

source§

impl<K: Clone + Ord, V: Clone + Val<A>, A: Clone + Ord + Hash> Clone for Map<K, V, A>

source§

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

Returns a copy 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, V: Val<A> + Debug, A: Ord + Hash + Clone + Debug> CmRDT for Map<K, V, A>

§

type Op = Op<K, V, A>

Op defines a mutation to the CRDT. As long as Op’s from one actor are replayed in exactly the same order they were generated by that actor, the CRDT will converge. In other words, we must have a total ordering on each actors operations, while requiring only a partial order over all ops. E.g. Read more
§

type Validation = CmRDTValidation<V, A>

The validation error returned by validate_op.
source§

fn validate_op(&self, op: &Self::Op) -> Result<(), Self::Validation>

Some CRDT’s have stricter requirements on how they must be used. To avoid violating these requirements, CRDT’s provide an interface to optionally validate op’s before they are applied. Read more
source§

fn apply(&mut self, op: Self::Op)

Apply an Op to the CRDT
source§

impl<K: Ord + Clone + Debug, V: Val<A> + CvRDT + Debug, A: Ord + Hash + Clone + Debug> CvRDT for Map<K, V, A>

§

type Validation = CvRDTValidation<K, V, A>

The validation error returned by validate_merge.
source§

fn validate_merge(&self, other: &Self) -> Result<(), Self::Validation>

Some CRDT’s have stricter requirements on how they must be used. To avoid violating these requirements, CRDT’s provide an interface to optionally validate merge compatibility before attempting to merge. Read more
source§

fn merge(&mut self, other: Self)

Merge the given CRDT into the current CRDT.
source§

impl<K: Debug + Ord, V: Debug + Val<A>, A: Debug + Ord + Hash> Debug for Map<K, V, A>

source§

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

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

impl<K: Ord, V: Val<A>, A: Ord + Hash> Default for Map<K, V, A>

source§

fn default() -> Self

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

impl<'de, K, V, A> Deserialize<'de> for Map<K, V, A>where K: Deserialize<'de> + Ord, V: Deserialize<'de> + Val<A>, A: Deserialize<'de> + Ord + Hash,

source§

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

Deserialize this value from the given Serde deserializer. Read more
source§

impl<K: PartialEq + Ord, V: PartialEq + Val<A>, A: PartialEq + Ord + Hash> PartialEq<Map<K, V, A>> for Map<K, V, A>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K: Ord, V: Val<A>, A: Ord + Hash> ResetRemove<A> for Map<K, V, A>

source§

fn reset_remove(&mut self, clock: &VClock<A>)

Remove data that is strictly smaller than this clock
source§

impl<K, V, A> Serialize for Map<K, V, A>where K: Serialize + Ord, V: Serialize + Val<A>, A: Serialize + Ord + Hash,

source§

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

Serialize this value into the given Serde serializer. Read more
source§

impl<K: Eq + Ord, V: Eq + Val<A>, A: Eq + Ord + Hash> Eq for Map<K, V, A>

source§

impl<K: Ord, V: Val<A>, A: Ord + Hash> StructuralEq for Map<K, V, A>

source§

impl<K: Ord, V: Val<A>, A: Ord + Hash> StructuralPartialEq for Map<K, V, A>

Auto Trait Implementations§

§

impl<K, V, A> RefUnwindSafe for Map<K, V, A>where A: RefUnwindSafe, K: RefUnwindSafe, V: RefUnwindSafe,

§

impl<K, V, A> Send for Map<K, V, A>where A: Send, K: Send, V: Send,

§

impl<K, V, A> Sync for Map<K, V, A>where A: Sync, K: Sync, V: Sync,

§

impl<K, V, A> Unpin for Map<K, V, A>

§

impl<K, V, A> UnwindSafe for Map<K, V, A>where A: RefUnwindSafe, K: RefUnwindSafe, V: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,

source§

impl<A, T> Val<A> for Twhere A: Ord, T: Clone + Default + ResetRemove<A> + CmRDT,