Skip to main content

limbo_core/mvcc/
clock.rs

1use std::sync::atomic::{AtomicU64, Ordering};
2
3/// Logical clock.
4pub trait LogicalClock {
5    fn get_timestamp(&self) -> u64;
6    fn reset(&self, ts: u64);
7}
8
9/// A node-local clock backed by an atomic counter.
10#[derive(Debug, Default)]
11pub struct LocalClock {
12    ts_sequence: AtomicU64,
13}
14
15impl LocalClock {
16    pub fn new() -> Self {
17        Self {
18            ts_sequence: AtomicU64::new(0),
19        }
20    }
21}
22
23impl LogicalClock for LocalClock {
24    fn get_timestamp(&self) -> u64 {
25        self.ts_sequence.fetch_add(1, Ordering::SeqCst)
26    }
27
28    fn reset(&self, ts: u64) {
29        self.ts_sequence.store(ts, Ordering::SeqCst);
30    }
31}