entrenar/integrity/lineage/
timestamp.rs1use serde::{Deserialize, Serialize};
4use std::cmp::Ordering;
5
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct LamportTimestamp {
12 pub counter: u64,
14 pub node_id: String,
16}
17
18impl LamportTimestamp {
19 pub fn new(node_id: &str) -> Self {
21 Self { counter: 0, node_id: node_id.to_string() }
22 }
23
24 pub fn with_counter(node_id: &str, counter: u64) -> Self {
26 Self { counter, node_id: node_id.to_string() }
27 }
28
29 pub fn increment(&mut self) -> Self {
33 self.counter += 1;
34 self.clone()
35 }
36
37 pub fn merge(&mut self, other: &Self) -> Self {
42 self.counter = self.counter.max(other.counter) + 1;
43 self.clone()
44 }
45
46 pub fn happens_before(&self, other: &Self) -> bool {
54 match self.counter.cmp(&other.counter) {
55 Ordering::Less => true,
56 Ordering::Greater => false,
57 Ordering::Equal => self.node_id < other.node_id,
58 }
59 }
60
61 pub fn is_concurrent_with(&self, other: &Self) -> bool {
63 self.counter == other.counter && self.node_id != other.node_id
64 }
65}
66
67impl PartialOrd for LamportTimestamp {
68 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
69 Some(self.cmp(other))
70 }
71}
72
73impl Ord for LamportTimestamp {
74 fn cmp(&self, other: &Self) -> Ordering {
75 match self.counter.cmp(&other.counter) {
76 Ordering::Equal => self.node_id.cmp(&other.node_id),
77 other => other,
78 }
79 }
80}