Skip to main content

reifydb_core/
delta.rs

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