1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use std::fmt;
use std::time::Duration;

use crate::prelude::*;
use kanidm_proto::v1::OperationError;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Eq, PartialOrd, Ord, Hash)]
pub struct Cid {
    // Mental note: Derive ord always checks in order of struct fields.
    pub ts: Duration,
    pub s_uuid: Uuid,
}

impl fmt::Display for Cid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:032}-{}", self.ts.as_nanos(), self.s_uuid)
    }
}

impl Cid {
    #[cfg(test)]
    pub(crate) fn new(s_uuid: Uuid, ts: Duration) -> Self {
        Cid { s_uuid, ts }
    }

    pub fn new_lamport(s_uuid: Uuid, ts: Duration, max_ts: &Duration) -> Self {
        let ts = if ts > *max_ts {
            ts
        } else {
            *max_ts + Duration::from_nanos(1)
        };
        Cid { ts, s_uuid }
    }

    #[cfg(test)]
    pub unsafe fn new_zero() -> Self {
        Self::new_count(0)
    }

    #[cfg(test)]
    pub unsafe fn new_count(c: u64) -> Self {
        Cid {
            s_uuid: uuid!("00000000-0000-0000-0000-000000000000"),
            ts: Duration::new(c, 0),
        }
    }

    #[cfg(test)]
    pub fn new_random_s_d(ts: Duration) -> Self {
        Cid {
            s_uuid: Uuid::new_v4(),
            ts,
        }
    }

    #[allow(clippy::expect_used)]
    pub fn sub_secs(&self, secs: u64) -> Result<Self, OperationError> {
        self.ts
            .checked_sub(Duration::from_secs(secs))
            .map(|r| Cid {
                s_uuid: uuid!("00000000-0000-0000-0000-000000000000"),
                ts: r,
            })
            .ok_or(OperationError::InvalidReplChangeId)
    }
}

#[cfg(test)]
mod tests {
    use crate::prelude::*;
    use std::cmp::Ordering;
    use std::time::Duration;

    use crate::repl::cid::Cid;

    #[test]
    fn test_cid_ordering() {
        // Check diff ts
        let cid_a = Cid::new(
            uuid!("00000000-0000-0000-0000-000000000001"),
            Duration::new(5, 0),
        );
        let cid_b = Cid::new(
            uuid!("00000000-0000-0000-0000-000000000001"),
            Duration::new(15, 0),
        );

        assert!(cid_a.cmp(&cid_a) == Ordering::Equal);
        assert!(cid_a.cmp(&cid_b) == Ordering::Less);
        assert!(cid_b.cmp(&cid_a) == Ordering::Greater);

        // check same ts, d_uuid, diff s_uuid
        let cid_e = Cid::new(
            uuid!("00000000-0000-0000-0000-000000000000"),
            Duration::new(5, 0),
        );
        let cid_f = Cid::new(
            uuid!("00000000-0000-0000-0000-000000000001"),
            Duration::new(5, 0),
        );

        assert!(cid_e.cmp(&cid_e) == Ordering::Equal);
        assert!(cid_e.cmp(&cid_f) == Ordering::Less);
        assert!(cid_f.cmp(&cid_e) == Ordering::Greater);
    }

    #[test]
    fn test_cid_lamport() {
        let s_uuid = uuid!("00000000-0000-0000-0000-000000000001");

        let ts5 = Duration::new(5, 0);
        let ts10 = Duration::new(10, 0);
        let ts15 = Duration::new(15, 0);

        let cid_z = unsafe { Cid::new_zero() };

        let cid_a = Cid::new_lamport(s_uuid, ts5, &ts5);
        assert!(cid_a.cmp(&cid_z) == Ordering::Greater);
        let cid_b = Cid::new_lamport(s_uuid, ts15, &ts10);
        assert!(cid_b.cmp(&cid_a) == Ordering::Greater);
        // Even with an older ts, we should still step forward.
        let cid_c = Cid::new_lamport(s_uuid, ts10, &ts15);
        assert!(cid_c.cmp(&cid_b) == Ordering::Greater);
    }
}