Skip to main content

reifydb_core/
delta.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use std::cmp;
5
6use reifydb_codec::{key::encoded::EncodedKey, row::bytes::EncodedBytes};
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub enum Delta {
11	Set {
12		key: EncodedKey,
13		bytes: EncodedBytes,
14	},
15
16	Remove {
17		key: EncodedKey,
18		announce: RemoveAnnounce,
19	},
20}
21
22#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
23pub enum RemoveAnnounce {
24	Silent,
25
26	Announced {
27		pre: EncodedBytes,
28	},
29}
30
31impl RemoveAnnounce {
32	pub fn announces(&self) -> bool {
33		matches!(self, Self::Announced { .. })
34	}
35
36	pub fn pre(&self) -> Option<&EncodedBytes> {
37		match self {
38			Self::Silent => None,
39			Self::Announced {
40				pre,
41			} => Some(pre),
42		}
43	}
44}
45
46impl PartialOrd for Delta {
47	fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
48		Some(self.cmp(other))
49	}
50}
51
52impl Ord for Delta {
53	fn cmp(&self, other: &Self) -> cmp::Ordering {
54		self.key().cmp(other.key())
55	}
56}
57
58impl Delta {
59	pub fn remove_silent(key: EncodedKey) -> Self {
60		Self::Remove {
61			key,
62			announce: RemoveAnnounce::Silent,
63		}
64	}
65
66	pub fn remove_announced(key: EncodedKey, pre: EncodedBytes) -> Self {
67		Self::Remove {
68			key,
69			announce: RemoveAnnounce::Announced {
70				pre,
71			},
72		}
73	}
74
75	pub fn key(&self) -> &EncodedKey {
76		match self {
77			Self::Set {
78				key,
79				..
80			} => key,
81			Self::Remove {
82				key,
83				..
84			} => key,
85		}
86	}
87
88	pub fn bytes(&self) -> Option<&EncodedBytes> {
89		match self {
90			Self::Set {
91				bytes,
92				..
93			} => Some(bytes),
94			Self::Remove {
95				..
96			} => None,
97		}
98	}
99
100	pub fn announces(&self) -> bool {
101		match self {
102			Self::Set {
103				..
104			} => true,
105			Self::Remove {
106				announce,
107				..
108			} => announce.announces(),
109		}
110	}
111}