Struct noosphere_core::data::map::Map

source ·
pub struct Map<K, V, A>where
    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

Constructs an empty Map

Returns true if the map has no entries, false otherwise

Returns the number of entries in the Map

Retrieve value stored under a key

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.

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.

Retrieve the current read context

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]);

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"]);

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

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
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
The validation error returned by validate_op.
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
Apply an Op to the CRDT
The validation error returned by validate_merge.
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
Merge the given CRDT into the current CRDT.
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Remove data that is strictly smaller than this clock
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

Calls U::from(self).

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

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Scrape the references from an impl Read. Read more
Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more