Struct crdts::vclock::VClock

source ·
pub struct VClock<A: Ord> {
    pub dots: BTreeMap<A, u64>,
}
Expand description

A VClock is a standard vector clock. It contains a set of “actors” and associated counters. When a particular actor witnesses a mutation, their associated counter in a VClock is incremented. VClock is typically used as metadata for associated application data, rather than as the container for application data. VClock just tracks causality. It can tell you if something causally descends something else, or if different replicas are “concurrent” (were mutated in isolation, and need to be resolved externally).

Fields§

§dots: BTreeMap<A, u64>

dots is the mapping from actors to their associated counters

Implementations§

source§

impl<A: Ord> VClock<A>

source

pub fn new() -> Self

Returns a new VClock instance.

source

pub fn clone_without(&self, base_clock: &VClock<A>) -> VClock<A>where A: Clone,

Returns a clone of self but with information that is older than given clock is forgotten

source

pub fn inc(&self, actor: A) -> Dot<A>where A: Clone,

Generate Op to increment an actor’s counter.

Examples
use crdts::{VClock, CmRDT};
let mut a = VClock::new();

// `a.inc()` does not mutate the vclock!
let op = a.inc("A");
assert_eq!(a, VClock::new());

// we must apply the op to the VClock to have
// its edit take effect.
a.apply(op.clone());
assert_eq!(a.get(&"A"), 1);

// Op's can be replicated to another node and
// applied to the local state there.
let mut other_node = VClock::new();
other_node.apply(op);
assert_eq!(other_node.get(&"A"), 1);
source

pub fn get(&self, actor: &A) -> u64

Return the associated counter for this actor. All actors not in the vclock have an implied count of 0

source

pub fn dot(&self, actor: A) -> Dot<A>

Return the Dot for a given actor

source

pub fn concurrent(&self, other: &VClock<A>) -> bool

True if two vector clocks have diverged.

Examples
use crdts::{VClock, CmRDT};
let (mut a, mut b) = (VClock::new(), VClock::new());
a.apply(a.inc("A"));
b.apply(b.inc("B"));
assert!(a.concurrent(&b));
source

pub fn is_empty(&self) -> bool

Returns true if this vector clock contains nothing.

source

pub fn intersection(left: &VClock<A>, right: &VClock<A>) -> VClock<A>where A: Clone,

Returns the common elements (same actor and counter) for two VClock instances.

source

pub fn glb(&mut self, other: &Self)

Reduces this VClock to the greatest-lower-bound of the given VClock and itsef, as an example see the following code.

use crdts::{VClock, Dot, ResetRemove, CmRDT};
let mut c = VClock::new();
c.apply(Dot::new(23, 6));
c.apply(Dot::new(89, 14));
let c2 = c.clone();

c.glb(&c2); // this is a no-op since `glb { c, c } = c`
assert_eq!(c, c2);

c.apply(Dot::new(43, 1));
assert_eq!(c.get(&43), 1);
c.glb(&c2); // should remove the 43 => 1 entry
assert_eq!(c.get(&43), 0);
source

pub fn iter(&self) -> impl Iterator<Item = Dot<&A>>

Returns an iterator over the dots in this vclock

Trait Implementations§

source§

impl<A: Ord + Clone + Debug + Arbitrary> Arbitrary for VClock<A>

source§

fn arbitrary(g: &mut Gen) -> Self

Return an arbitrary value. Read more
source§

fn shrink(&self) -> Box<dyn Iterator<Item = Self>>

Return an iterator of values that are smaller than itself. Read more
source§

impl<A: Clone + Ord> Clone for VClock<A>

source§

fn clone(&self) -> VClock<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<A: Ord + Clone + Debug> CmRDT for VClock<A>

source§

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

Monotonically adds the given actor version to this VClock.

Examples
use crdts::{VClock, Dot, CmRDT};
let mut v = VClock::new();

v.apply(Dot::new("A", 2));

// now all dots applied to `v` from actor `A` where
// the counter is not bigger than 2 are nops.
v.apply(Dot::new("A", 0));
assert_eq!(v.get(&"A"), 2);
§

type Op = Dot<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 = DotRange<A>

The validation error returned by validate_op.
source§

fn validate_op(&self, dot: &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§

impl<A: Ord + Clone + Debug> CvRDT for VClock<A>

§

type Validation = Infallible

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<A: Debug + Ord> Debug for VClock<A>

source§

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

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

impl<A: Ord> Default for VClock<A>

source§

fn default() -> Self

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

impl<'de, A> Deserialize<'de> for VClock<A>where A: Deserialize<'de> + Ord,

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<A: Ord + Display> Display for VClock<A>

source§

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

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

impl<A: Ord + Clone + Debug> From<Dot<A>> for VClock<A>

source§

fn from(dot: Dot<A>) -> Self

Converts to this type from the input type.
source§

impl<A: Ord + Clone + Debug> FromIterator<Dot<A>> for VClock<A>

source§

fn from_iter<I: IntoIterator<Item = Dot<A>>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl<A: Hash + Ord> Hash for VClock<A>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<A: Ord> IntoIterator for VClock<A>

source§

fn into_iter(self) -> Self::IntoIter

Consumes the vclock and returns an iterator over dots in the clock

§

type Item = Dot<A>

The type of the elements being iterated over.
§

type IntoIter = IntoIter<A>

Which kind of iterator are we turning this into?
source§

impl<A: PartialEq + Ord> PartialEq<VClock<A>> for VClock<A>

source§

fn eq(&self, other: &VClock<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<A: Ord> PartialOrd<VClock<A>> for VClock<A>

source§

fn partial_cmp(&self, other: &VClock<A>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<A: Ord> ResetRemove<A> for VClock<A>

source§

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

Forget any actors that have smaller counts than the count in the given vclock

source§

impl<A> Serialize for VClock<A>where A: Serialize + Ord,

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<A: Eq + Ord> Eq for VClock<A>

source§

impl<A: Ord> StructuralEq for VClock<A>

source§

impl<A: Ord> StructuralPartialEq for VClock<A>

Auto Trait Implementations§

§

impl<A> RefUnwindSafe for VClock<A>where A: RefUnwindSafe,

§

impl<A> Send for VClock<A>where A: Send,

§

impl<A> Sync for VClock<A>where A: Sync,

§

impl<A> Unpin for VClock<A>

§

impl<A> UnwindSafe for VClock<A>where A: 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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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,