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
15	Unset {
16		key: EncodedKey,
17		row: EncodedRow,
18	},
19
20	Remove {
21		key: EncodedKey,
22	},
23
24	Drop {
25		key: EncodedKey,
26	},
27}
28
29impl PartialOrd for Delta {
30	fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
31		Some(self.cmp(other))
32	}
33}
34
35impl Ord for Delta {
36	fn cmp(&self, other: &Self) -> cmp::Ordering {
37		self.key().cmp(other.key())
38	}
39}
40
41impl Delta {
42	pub fn key(&self) -> &EncodedKey {
43		match self {
44			Self::Set {
45				key,
46				..
47			} => key,
48			Self::Unset {
49				key,
50				..
51			} => key,
52			Self::Remove {
53				key,
54			} => key,
55			Self::Drop {
56				key,
57				..
58			} => key,
59		}
60	}
61
62	pub fn row(&self) -> Option<&EncodedRow> {
63		match self {
64			Self::Set {
65				row,
66				..
67			} => Some(row),
68			Self::Unset {
69				..
70			} => None,
71			Self::Remove {
72				..
73			} => None,
74			Self::Drop {
75				..
76			} => None,
77		}
78	}
79}
80
81impl Clone for Delta {
82	fn clone(&self) -> Self {
83		match self {
84			Self::Set {
85				key,
86				row,
87			} => Self::Set {
88				key: key.clone(),
89				row: row.clone(),
90			},
91			Self::Unset {
92				key,
93				row,
94			} => Self::Unset {
95				key: key.clone(),
96				row: row.clone(),
97			},
98			Self::Remove {
99				key,
100			} => Self::Remove {
101				key: key.clone(),
102			},
103			Self::Drop {
104				key,
105			} => Self::Drop {
106				key: key.clone(),
107			},
108		}
109	}
110}