Skip to main content

ossa_core/
time.rs

1use ossa_crdt::{map::twopmap::TwoPMapOp, register::LWW};
2use serde::{Deserialize, Serialize};
3
4#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
5pub enum CausalTime<Time> {
6    Current { operation_position: u8 }, // Points to the current ECG node.
7    Time(Time),                         // Points to another ECG node.
8}
9
10impl<Time> CausalTime<Time> {
11    pub fn current_time(operation_position: u8) -> CausalTime<Time> {
12        CausalTime::Current { operation_position }
13    }
14
15    pub fn time(time: Time) -> CausalTime<Time> {
16        CausalTime::Time(time)
17    }
18}
19
20pub trait ConcretizeTime<HeaderId> {
21    type Serialized;
22
23    fn concretize_time(src: Self::Serialized, current_header: HeaderId) -> Self;
24}
25
26impl<HeaderId, T: ConcretizeTime<HeaderId>, V> ConcretizeTime<HeaderId> for LWW<T, V> {
27    type Serialized = LWW<T::Serialized, V>;
28
29    fn concretize_time(src: Self::Serialized, current_header: HeaderId) -> Self {
30        LWW {
31            time: T::concretize_time(src.time, current_header),
32            value: src.value,
33        }
34    }
35}
36
37impl<
38        HeaderId: Clone,
39        K: ConcretizeTime<HeaderId>,
40        V: ConcretizeTime<HeaderId>,
41        Op: ConcretizeTime<HeaderId>,
42    > ConcretizeTime<HeaderId> for TwoPMapOp<K, V, Op>
43{
44    type Serialized = TwoPMapOp<K::Serialized, V::Serialized, Op::Serialized>;
45
46    fn concretize_time(src: Self::Serialized, current_header: HeaderId) -> Self {
47        match src {
48            TwoPMapOp::Insert { key, value } => TwoPMapOp::Insert {
49                key: K::concretize_time(key, current_header.clone()),
50                value: V::concretize_time(value, current_header),
51            },
52            TwoPMapOp::Apply { key, operation } => TwoPMapOp::Apply {
53                key: K::concretize_time(key, current_header.clone()),
54                operation: Op::concretize_time(operation, current_header),
55            },
56            TwoPMapOp::Delete { key } => TwoPMapOp::Delete {
57                key: K::concretize_time(key, current_header),
58            },
59        }
60    }
61}