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
impl VectorClock
Sourcepub fn from_version_vector(device_id: DeviceId, clock: VersionVector) -> Self
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 IDclock- Existing version vector
Sourcepub fn tick(&mut self) -> Timestamp
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));Sourcepub fn current_time(&self) -> Timestamp
pub fn current_time(&self) -> Timestamp
Gets the current timestamp for this device
Sourcepub fn merge(&mut self, other: &VectorClock)
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));Sourcepub fn compare(&self, other: &VectorClock) -> ClockOrdering
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);Sourcepub fn happened_before(&self, other: &VectorClock) -> bool
pub fn happened_before(&self, other: &VectorClock) -> bool
Checks if this clock happened before the other
Sourcepub fn happened_after(&self, other: &VectorClock) -> bool
pub fn happened_after(&self, other: &VectorClock) -> bool
Checks if this clock happened after the other
Sourcepub fn is_concurrent(&self, other: &VectorClock) -> bool
pub fn is_concurrent(&self, other: &VectorClock) -> bool
Checks if clocks are concurrent (causally independent)
Sourcepub fn as_version_vector(&self) -> &VersionVector
pub fn as_version_vector(&self) -> &VersionVector
Gets a reference to the underlying version vector
Sourcepub fn into_version_vector(self) -> VersionVector
pub fn into_version_vector(self) -> VersionVector
Converts the clock into a version vector
Sourcepub fn with_device_id(&self, device_id: DeviceId) -> Self
pub fn with_device_id(&self, device_id: DeviceId) -> Self
Creates a clone with a new device ID
Trait Implementations§
Source§impl CausalOrdering for VectorClock
impl CausalOrdering for VectorClock
Source§impl Clone for VectorClock
impl Clone for VectorClock
Source§fn clone(&self) -> VectorClock
fn clone(&self) -> VectorClock
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more