Skip to main content

VectorClock

Struct VectorClock 

Source
pub struct VectorClock { /* private fields */ }
Expand description

Vector clock for tracking causality in distributed systems

A vector clock is a data structure used for determining the partial ordering of events in a distributed system and detecting causality violations.

Implementations§

Source§

impl VectorClock

Source

pub fn new(device_id: DeviceId) -> Self

Creates a new vector clock for the given device

§Arguments
  • device_id - The identifier of the device owning this clock
§Example
use oxigdal_sync::vector_clock::VectorClock;

let clock = VectorClock::new("device-1".to_string());
Source

pub fn from_version_vector(device_id: DeviceId, clock: VersionVector) -> Self

Creates a vector clock from an existing version vector

§Arguments
  • device_id - The device ID
  • clock - Existing version vector
Source

pub fn tick(&mut self) -> Timestamp

Increments the clock for this device

This should be called when a new event occurs on this device.

§Example
use oxigdal_sync::vector_clock::VectorClock;

let mut clock = VectorClock::new("device-1".to_string());
clock.tick();
assert_eq!(clock.get_time(&"device-1".to_string()), Some(1));
Source

pub fn get_time(&self, device_id: &DeviceId) -> Option<Timestamp>

Gets the timestamp for a specific device

§Arguments
  • device_id - The device to query
§Returns

The timestamp for the device, or None if the device is not in the clock

Source

pub fn current_time(&self) -> Timestamp

Gets the current timestamp for this device

Source

pub fn merge(&mut self, other: &VectorClock)

Merges another vector clock into this one

Takes the maximum timestamp for each device. This is used when receiving an event from another device.

§Arguments
  • other - The clock to merge
§Example
use oxigdal_sync::vector_clock::VectorClock;

let mut clock1 = VectorClock::new("device-1".to_string());
let mut clock2 = VectorClock::new("device-2".to_string());

clock1.tick();
clock2.tick();

clock1.merge(&clock2);
assert_eq!(clock1.get_time(&"device-2".to_string()), Some(1));
Source

pub fn compare(&self, other: &VectorClock) -> ClockOrdering

Compares this clock with another to determine causal ordering

§Arguments
  • other - The clock to compare with
§Returns

The ordering relationship between the clocks

§Example
use oxigdal_sync::vector_clock::{VectorClock, ClockOrdering};

let mut clock1 = VectorClock::new("device-1".to_string());
let mut clock2 = VectorClock::new("device-1".to_string());

clock2.tick();

assert_eq!(clock1.compare(&clock2), ClockOrdering::Before);
assert_eq!(clock2.compare(&clock1), ClockOrdering::After);
Source

pub fn happened_before(&self, other: &VectorClock) -> bool

Checks if this clock happened before the other

Source

pub fn happened_after(&self, other: &VectorClock) -> bool

Checks if this clock happened after the other

Source

pub fn is_concurrent(&self, other: &VectorClock) -> bool

Checks if clocks are concurrent (causally independent)

Source

pub fn device_id(&self) -> &DeviceId

Gets the device ID associated with this clock

Source

pub fn as_version_vector(&self) -> &VersionVector

Gets a reference to the underlying version vector

Source

pub fn into_version_vector(self) -> VersionVector

Converts the clock into a version vector

Source

pub fn with_device_id(&self, device_id: DeviceId) -> Self

Creates a clone with a new device ID

Source

pub fn update(&mut self, device_id: DeviceId, timestamp: Timestamp)

Updates the timestamp for a specific device

§Arguments
  • device_id - The device to update
  • timestamp - The new timestamp
Source

pub fn sum(&self) -> Timestamp

Gets the sum of all timestamps (useful for debugging)

Source

pub fn is_empty(&self) -> bool

Checks if the clock is empty (no devices)

Source

pub fn len(&self) -> usize

Gets the number of devices in the clock

Source

pub fn devices(&self) -> Vec<DeviceId>

Gets all device IDs in the clock

Trait Implementations§

Source§

impl CausalOrdering for VectorClock

Source§

fn precedes(&self, other: &Self) -> bool

Checks if this event causally precedes another
Source§

fn concurrent_with(&self, other: &Self) -> bool

Checks if this event is concurrent with another
Source§

impl Clone for VectorClock

Source§

fn clone(&self) -> VectorClock

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 Debug for VectorClock

Source§

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

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

impl Default for VectorClock

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for VectorClock

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 PartialEq for VectorClock

Source§

fn eq(&self, other: &VectorClock) -> 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 Serialize for VectorClock

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 Eq for VectorClock

Source§

impl StructuralPartialEq for VectorClock

Auto Trait Implementations§

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

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

Checks if this value is equivalent to the given key. 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.
Source§

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