1use reifydb_codec::row::bytes::EncodedBytes;
5
6use crate::key::any::TaggedKey;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum Delta {
10 Set {
11 key: TaggedKey,
12 bytes: EncodedBytes,
13 pre: SetPre,
14 },
15
16 Remove {
17 key: TaggedKey,
18 announce: RemoveAnnounce,
19 },
20}
21
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub enum SetPre {
24 Unknown,
25
26 Absent,
27
28 Present(EncodedBytes),
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum RemoveVisibility {
33 Silent,
34
35 Announced,
36
37 Unobserved,
38}
39
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub enum RemoveAnnounce {
42 Silent,
43
44 Announced {
45 pre: EncodedBytes,
46 },
47
48 Unobserved {
49 pre: EncodedBytes,
50 },
51}
52
53impl RemoveAnnounce {
54 pub fn announces(&self) -> bool {
55 !matches!(self, Self::Silent)
56 }
57
58 pub fn visible(&self) -> bool {
59 matches!(self, Self::Announced { .. })
60 }
61
62 pub fn pre(&self) -> Option<&EncodedBytes> {
63 match self {
64 Self::Silent => None,
65 Self::Announced {
66 pre,
67 }
68 | Self::Unobserved {
69 pre,
70 } => Some(pre),
71 }
72 }
73}
74
75impl Delta {
76 pub fn remove_silent(key: TaggedKey) -> Self {
77 Self::Remove {
78 key,
79 announce: RemoveAnnounce::Silent,
80 }
81 }
82
83 pub fn remove_announced(key: TaggedKey, pre: EncodedBytes) -> Self {
84 Self::Remove {
85 key,
86 announce: RemoveAnnounce::Announced {
87 pre,
88 },
89 }
90 }
91
92 pub fn remove_unobserved(key: TaggedKey, pre: EncodedBytes) -> Self {
93 Self::Remove {
94 key,
95 announce: RemoveAnnounce::Unobserved {
96 pre,
97 },
98 }
99 }
100
101 pub fn key(&self) -> &TaggedKey {
102 match self {
103 Self::Set {
104 key,
105 ..
106 } => key,
107 Self::Remove {
108 key,
109 ..
110 } => key,
111 }
112 }
113
114 pub fn bytes(&self) -> Option<&EncodedBytes> {
115 match self {
116 Self::Set {
117 bytes,
118 ..
119 } => Some(bytes),
120 Self::Remove {
121 ..
122 } => None,
123 }
124 }
125
126 pub fn announces(&self) -> bool {
127 match self {
128 Self::Set {
129 ..
130 } => true,
131 Self::Remove {
132 announce,
133 ..
134 } => announce.announces(),
135 }
136 }
137}