Skip to main content

reifydb_core/key/operator/keyspace/
root.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use reifydb_value::value::row_number::RowNumber;
5
6use crate::{
7	key::{
8		operator::{
9			state::{GroupId, KeyspaceId},
10			traits::Keyspace,
11		},
12		typed::{
13			BoundedKey, DenseKey, KeyLayout,
14			direction::{Asc, Desc, Direction, KeyField},
15			layout::{KeyColumn, KeyColumnType, KeyLayout, KeyValue, KeyValues},
16		},
17	},
18	metrics::heap::HeapSize,
19};
20
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
22pub struct SourceWatermarkKey {}
23
24#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
25pub struct SealLedgerKey {
26	pub group: Desc<GroupId>,
27}
28
29#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
30pub struct NodeCounterKey {
31	pub kind: Asc<u8>,
32}
33
34#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
35pub enum NodeCounterKind {
36	RowNumber,
37	Group,
38}
39
40impl NodeCounterKind {
41	pub fn tag(&self) -> u8 {
42		match self {
43			Self::RowNumber => 0,
44			Self::Group => 1,
45		}
46	}
47
48	pub fn from_tag(tag: u8) -> Option<Self> {
49		match tag {
50			0 => Some(Self::RowNumber),
51			1 => Some(Self::Group),
52			_ => None,
53		}
54	}
55}
56
57impl NodeCounterKey {
58	pub fn of(kind: NodeCounterKind) -> Self {
59		Self {
60			kind: Asc(kind.tag()),
61		}
62	}
63}
64
65#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
66pub struct GateVisibilityKey {
67	pub row: Asc<RowNumber>,
68}
69
70#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
71pub struct TakeRowKey {
72	pub row: Asc<RowNumber>,
73}
74
75#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
76pub struct GroupRowMappingKey {
77	pub group: Desc<GroupId>,
78}
79
80#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
81pub struct GuestRowMappingKey {
82	pub group: Desc<GroupId>,
83	pub id: Asc<[u8; 16]>,
84}
85
86#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
87pub struct GuestRowMappingSuffix {
88	pub id: Asc<[u8; 16]>,
89}
90
91#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
92pub struct CustomUnmanagedKey {
93	pub group: Desc<GroupId>,
94	pub id: Asc<[u8; 16]>,
95}
96
97#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
98pub struct CustomUnmanagedSuffix {
99	pub id: Asc<[u8; 16]>,
100}
101
102impl CustomUnmanagedSuffix {
103	pub const ID_LEN: usize = 16;
104
105	pub fn of(id: &[u8]) -> Option<Self> {
106		if id.len() > Self::ID_LEN {
107			return None;
108		}
109		let mut bytes = [0u8; Self::ID_LEN];
110		bytes[..id.len()].copy_from_slice(id);
111		Some(Self {
112			id: Asc(bytes),
113		})
114	}
115}
116
117#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
118pub struct CustomManagedKey {
119	pub group: Desc<GroupId>,
120	pub id: Asc<[u8; 16]>,
121}
122
123#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
124pub struct CustomManagedSuffix {
125	pub id: Asc<[u8; 16]>,
126}
127
128impl CustomManagedSuffix {
129	pub const ID_LEN: usize = 16;
130
131	pub fn of(id: &[u8]) -> Option<Self> {
132		if id.len() > Self::ID_LEN {
133			return None;
134		}
135		let mut bytes = [0u8; Self::ID_LEN];
136		bytes[..id.len()].copy_from_slice(id);
137		Some(Self {
138			id: Asc(bytes),
139		})
140	}
141}
142
143#[derive(Clone, Copy, Debug, PartialEq, Eq)]
144pub struct SourceWatermark;
145
146impl Keyspace for SourceWatermark {
147	const ID: KeyspaceId = KeyspaceId::SOURCE_WATERMARK;
148	const NAME: &'static str = "SOURCE_WATERMARK";
149	const RANGE_CACHED: bool = true;
150
151	type GroupedKey = SourceWatermarkKey;
152	type Suffix = SourceWatermarkKey;
153
154	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
155		(GroupId::ROOT, *key)
156	}
157
158	fn join(_group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
159		suffix
160	}
161}
162
163#[derive(Clone, Copy, Debug, PartialEq, Eq)]
164pub struct SealLedger;
165
166impl Keyspace for SealLedger {
167	const ID: KeyspaceId = KeyspaceId::SEAL_LEDGER;
168	const NAME: &'static str = "SEAL_LEDGER";
169	const RANGE_CACHED: bool = true;
170
171	type GroupedKey = SealLedgerKey;
172	type Suffix = ();
173
174	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
175		(key.group.0, ())
176	}
177
178	fn join(group: GroupId, _suffix: Self::Suffix) -> Self::GroupedKey {
179		SealLedgerKey {
180			group: Desc(group),
181		}
182	}
183}
184
185#[derive(Clone, Copy, Debug, PartialEq, Eq)]
186pub struct NodeCounter;
187
188impl Keyspace for NodeCounter {
189	const ID: KeyspaceId = KeyspaceId::NODE_COUNTER;
190	const NAME: &'static str = "NODE_COUNTER";
191	const RANGE_CACHED: bool = true;
192
193	type GroupedKey = NodeCounterKey;
194	type Suffix = NodeCounterKey;
195
196	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
197		(GroupId::ROOT, *key)
198	}
199
200	fn join(_group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
201		suffix
202	}
203}
204
205#[derive(Clone, Copy, Debug, PartialEq, Eq)]
206pub struct GateVisibility;
207
208impl Keyspace for GateVisibility {
209	const ID: KeyspaceId = KeyspaceId::GATE_VISIBILITY;
210	const NAME: &'static str = "GATE_VISIBILITY";
211	const RANGE_CACHED: bool = true;
212
213	type GroupedKey = GateVisibilityKey;
214	type Suffix = GateVisibilityKey;
215
216	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
217		(GroupId::ROOT, *key)
218	}
219
220	fn join(_group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
221		suffix
222	}
223}
224
225#[derive(Clone, Copy, Debug, PartialEq, Eq)]
226pub struct TakeRow;
227
228impl Keyspace for TakeRow {
229	const ID: KeyspaceId = KeyspaceId::TAKE_ROW;
230	const NAME: &'static str = "TAKE_ROW";
231	const RANGE_CACHED: bool = true;
232
233	type GroupedKey = TakeRowKey;
234	type Suffix = TakeRowKey;
235
236	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
237		(GroupId::ROOT, *key)
238	}
239
240	fn join(_group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
241		suffix
242	}
243}
244
245#[derive(Clone, Copy, Debug, PartialEq, Eq)]
246pub struct GroupRowMapping;
247
248impl Keyspace for GroupRowMapping {
249	const ID: KeyspaceId = KeyspaceId::GROUP_ROW_MAPPING;
250	const NAME: &'static str = "GROUP_ROW_MAPPING";
251	const RANGE_CACHED: bool = true;
252
253	type GroupedKey = GroupRowMappingKey;
254	type Suffix = ();
255
256	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
257		(key.group.0, ())
258	}
259
260	fn join(group: GroupId, _suffix: Self::Suffix) -> Self::GroupedKey {
261		GroupRowMappingKey {
262			group: Desc(group),
263		}
264	}
265}
266
267#[derive(Clone, Copy, Debug, PartialEq, Eq)]
268pub struct GuestRowMapping;
269
270impl Keyspace for GuestRowMapping {
271	const ID: KeyspaceId = KeyspaceId::GUEST_ROW_MAPPING;
272	const NAME: &'static str = "GUEST_ROW_MAPPING";
273	const RANGE_CACHED: bool = true;
274
275	type GroupedKey = GuestRowMappingKey;
276	type Suffix = GuestRowMappingSuffix;
277
278	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
279		(
280			key.group.0,
281			GuestRowMappingSuffix {
282				id: key.id,
283			},
284		)
285	}
286
287	fn join(group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
288		GuestRowMappingKey {
289			group: Desc(group),
290			id: suffix.id,
291		}
292	}
293}
294
295#[derive(Clone, Copy, Debug, PartialEq, Eq)]
296pub struct CustomUnmanaged;
297
298impl Keyspace for CustomUnmanaged {
299	const ID: KeyspaceId = KeyspaceId::CUSTOM_UNMANAGED;
300	const NAME: &'static str = "CUSTOM_UNMANAGED";
301	const RANGE_CACHED: bool = false;
302
303	type GroupedKey = CustomUnmanagedKey;
304	type Suffix = CustomUnmanagedSuffix;
305
306	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
307		(
308			key.group.0,
309			CustomUnmanagedSuffix {
310				id: key.id,
311			},
312		)
313	}
314
315	fn join(group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
316		CustomUnmanagedKey {
317			group: Desc(group),
318			id: suffix.id,
319		}
320	}
321}
322
323#[derive(Clone, Copy, Debug, PartialEq, Eq)]
324pub struct CustomManaged;
325
326impl Keyspace for CustomManaged {
327	const ID: KeyspaceId = KeyspaceId::CUSTOM_MANAGED;
328	const NAME: &'static str = "CUSTOM_MANAGED";
329	const RANGE_CACHED: bool = false;
330
331	type GroupedKey = CustomManagedKey;
332	type Suffix = CustomManagedSuffix;
333
334	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
335		(
336			key.group.0,
337			CustomManagedSuffix {
338				id: key.id,
339			},
340		)
341	}
342
343	fn join(group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
344		CustomManagedKey {
345			group: Desc(group),
346			id: suffix.id,
347		}
348	}
349}