reifydb_core/
delta.rs

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