Skip to main content

reifydb_core/
delta.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::cmp;
5
6use crate::encoded::{key::EncodedKey, row::EncodedRow};
7
8#[derive(Debug, PartialEq, Eq)]
9pub enum Delta {
10	Set {
11		key: EncodedKey,
12		row: EncodedRow,
13	},
14	/// Unset an entry, preserving the deleted values.
15	/// Symmetric with Set - use when the deleted data matters (e.g., row data, CDC).
16	Unset {
17		key: EncodedKey,
18		row: EncodedRow,
19	},
20	/// Remove an entry without preserving the deleted values.
21	/// Use when only the key matters (e.g., index entries, catalog metadata).
22	Remove {
23		key: EncodedKey,
24	},
25	/// Drop operation - completely erases historical versioned entries from storage.
26	/// Unlike Remove (which writes a tombstone and generates CDC), Drop:
27	/// - Deletes existing entries without writing anything new
28	/// - Never generates CDC events
29	/// - Only keeps the most recent version (aggressive cleanup)
30	Drop {
31		key: EncodedKey,
32	},
33}
34
35impl PartialOrd for Delta {
36	fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
37		Some(self.cmp(other))
38	}
39}
40
41impl Ord for Delta {
42	fn cmp(&self, other: &Self) -> cmp::Ordering {
43		self.key().cmp(other.key())
44	}
45}
46
47impl Delta {
48	/// Returns the key
49	pub fn key(&self) -> &EncodedKey {
50		match self {
51			Self::Set {
52				key,
53				..
54			} => key,
55			Self::Unset {
56				key,
57				..
58			} => key,
59			Self::Remove {
60				key,
61			} => key,
62			Self::Drop {
63				key,
64				..
65			} => key,
66		}
67	}
68
69	/// Returns the encoded row, if None, it means the entry is marked as remove or drop.
70	pub fn row(&self) -> Option<&EncodedRow> {
71		match self {
72			Self::Set {
73				row,
74				..
75			} => Some(row),
76			Self::Unset {
77				..
78			} => None,
79			Self::Remove {
80				..
81			} => None,
82			Self::Drop {
83				..
84			} => None,
85		}
86	}
87}
88
89impl Clone for Delta {
90	fn clone(&self) -> Self {
91		match self {
92			Self::Set {
93				key,
94				row,
95			} => Self::Set {
96				key: key.clone(),
97				row: row.clone(),
98			},
99			Self::Unset {
100				key,
101				row,
102			} => Self::Unset {
103				key: key.clone(),
104				row: row.clone(),
105			},
106			Self::Remove {
107				key,
108			} => Self::Remove {
109				key: key.clone(),
110			},
111			Self::Drop {
112				key,
113			} => Self::Drop {
114				key: key.clone(),
115			},
116		}
117	}
118}