Skip to main content

logicaffeine_data/crdt/causal/
dot.rs

1//! Dot - Unique event identifier.
2//!
3//! A Dot uniquely identifies an event in a CRDT's history.
4
5use super::super::replica::ReplicaId;
6use serde::{Deserialize, Serialize};
7
8/// A unique event identifier in a CRDT's history.
9///
10/// A Dot represents a single event that occurred at a specific replica
11/// with a specific sequence number. Two dots are equal if and only if
12/// they have the same replica and counter.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
14pub struct Dot {
15    /// The replica that generated this event.
16    pub replica: ReplicaId,
17    /// The sequence number of this event at the replica.
18    pub counter: u64,
19}
20
21impl Dot {
22    /// Create a new Dot with the given replica and counter.
23    pub fn new(replica: ReplicaId, counter: u64) -> Self {
24        Self { replica, counter }
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31    use std::collections::HashSet;
32
33    #[test]
34    fn test_dot_creation() {
35        let dot = Dot::new(42, 1);
36        assert_eq!(dot.replica, 42);
37        assert_eq!(dot.counter, 1);
38    }
39
40    #[test]
41    fn test_dot_equality() {
42        let a = Dot::new(1, 5);
43        let b = Dot::new(1, 5);
44        let c = Dot::new(1, 6);
45        let d = Dot::new(2, 5);
46
47        assert_eq!(a, b);
48        assert_ne!(a, c);
49        assert_ne!(a, d);
50    }
51
52    #[test]
53    fn test_dot_hash() {
54        let mut set = HashSet::new();
55        set.insert(Dot::new(1, 1));
56        set.insert(Dot::new(1, 2));
57        set.insert(Dot::new(1, 1)); // Duplicate
58
59        assert_eq!(set.len(), 2);
60    }
61}