Skip to main content

VectorClock

Struct VectorClock 

Source
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 <= B if for all threads t: clock_A[t] <= clock_B[t]
  • A < B if A <= B and exists t where clock_A[t] < clock_B[t]
  • A concurrent with B if neither A <= B nor B <= A

§Fields

  • clocks - HashMap mapping thread_id -> logical timestamp

Implementations§

Source§

impl VectorClock

Source

pub fn new() -> Self

Create a new empty vector clock.

All threads start with implicit timestamp 0.

§Example
use sqlitegraph::algo::observability::VectorClock;

let vc = VectorClock::new();
assert!(vc.is_empty());
Source

pub fn is_empty(&self) -> bool

Check if the vector clock is empty (no threads recorded).

Source

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).

Source

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

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
Source

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

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

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

pub fn threads(&self) -> impl Iterator<Item = i64> + '_

Get all thread IDs with non-zero clocks.

Source

pub fn len(&self) -> usize

Get the number of threads with non-zero clocks.

Trait Implementations§

Source§

impl Clone for VectorClock

Source§

fn clone(&self) -> VectorClock

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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 Eq for VectorClock

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

Source§

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

Compare self to key and return true if they are equal.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V