Skip to main content

reifydb_core/
delta.rs

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