pub struct VectorClock { /* private fields */ }Expand description
Vector clock for happens-before analysis.
A vector clock is a mapping from thread IDs to logical timestamps. It implements a partial order on events in concurrent/distributed systems.
The happens-before relation is defined as:
A <= Bif for all threads t: clock_A[t] <= clock_B[t]A < BifA <= Band exists t where clock_A[t] < clock_B[t]Aconcurrent withBif neitherA <= BnorB <= A
§Fields
clocks- HashMap mapping thread_id -> logical timestamp
Implementations§
Source§impl VectorClock
impl VectorClock
Sourcepub fn get(&self, thread_id: i64) -> u64
pub fn get(&self, thread_id: i64) -> u64
Get the clock value for a specific thread.
Returns 0 if the thread is not in the clock (implicit value).
Sourcepub fn increment(&mut self, thread_id: i64)
pub fn increment(&mut self, thread_id: i64)
Increment the clock for a specific thread.
Returns self for method chaining.
§Arguments
thread_id- Thread whose clock should be incremented
§Example
use sqlitegraph::algo::observability::VectorClock;
let mut vc = VectorClock::new();
vc.increment(1);
assert_eq!(vc.get(1), 1);
vc.increment(1);
assert_eq!(vc.get(1), 2);Sourcepub fn incremented(self, thread_id: i64) -> Self
pub fn incremented(self, thread_id: i64) -> Self
Return a new vector clock with the given thread incremented.
Convenience method for creating incremented copies without mutation.
§Arguments
thread_id- Thread whose clock should be incremented
Sourcepub fn merge(&mut self, other: &VectorClock)
pub fn merge(&mut self, other: &VectorClock)
Merge another vector clock into this one.
Takes the element-wise maximum of both clocks. Used after thread synchronization (e.g., after acquiring a lock).
§Arguments
other- Vector clock to merge with
§Example
use sqlitegraph::algo::observability::VectorClock;
let mut vc1 = VectorClock::new().incremented(1);
let vc2 = VectorClock::new().incremented(2);
vc1.merge(&vc2);
assert_eq!(vc1.get(1), 1);
assert_eq!(vc1.get(2), 1);Sourcepub fn happens_before(&self, other: &VectorClock) -> bool
pub fn happens_before(&self, other: &VectorClock) -> bool
Check if this vector clock happens-before another.
Returns true if for all threads t: self[t] <= other[t],
and there exists at least one thread where self[t] < other[t].
This is the strict partial order: this event MUST complete before the other event can occur.
§Arguments
other- Vector clock to compare against
§Returns
true if this clock happens-before the other, false otherwise
(including when clocks are equal or concurrent).
§Example
use sqlitegraph::algo::observability::VectorClock;
let vc1 = VectorClock::new().incremented(1);
let mut vc2 = VectorClock::new().incremented(1);
vc2.increment(1); // Thread 1: vc1 = 1, vc2 = 2
assert!(vc1.happens_before(&vc2));
assert!(!vc2.happens_before(&vc1));Sourcepub fn is_concurrent(&self, other: &VectorClock) -> bool
pub fn is_concurrent(&self, other: &VectorClock) -> bool
Check if this vector clock is concurrent with another.
Two clocks are concurrent if neither happens-before the other. This means there exist threads t1, t2 such that:
- self[t1] > other[t1]
- self[t2] < other[t2]
Concurrent events may execute in either order (potential race).
§Arguments
other- Vector clock to compare against
§Returns
true if clocks are concurrent (neither happens-before the other).
§Example
use sqlitegraph::algo::observability::VectorClock;
let vc1 = VectorClock::new().incremented(1); // Thread 1: 1, Thread 2: 0
let vc2 = VectorClock::new().incremented(2); // Thread 1: 0, Thread 2: 1
assert!(vc1.is_concurrent(&vc2));Trait Implementations§
Source§impl Clone for VectorClock
impl Clone for VectorClock
Source§fn clone(&self) -> VectorClock
fn clone(&self) -> VectorClock
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for VectorClock
impl Debug for VectorClock
Source§impl Default for VectorClock
impl Default for VectorClock
impl Eq for VectorClock
Source§impl PartialEq for VectorClock
impl PartialEq for VectorClock
Source§fn eq(&self, other: &VectorClock) -> bool
fn eq(&self, other: &VectorClock) -> bool
self and other values to be equal, and is used by ==.impl StructuralPartialEq for VectorClock
Auto Trait Implementations§
impl Freeze for VectorClock
impl RefUnwindSafe for VectorClock
impl Send for VectorClock
impl Sync for VectorClock
impl Unpin for VectorClock
impl UnsafeUnpin for VectorClock
impl UnwindSafe for VectorClock
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more