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 GroupRowMappingKey {
72	pub group: Desc<GroupId>,
73}
74
75#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
76pub struct GuestRowMappingKey {
77	pub group: Desc<GroupId>,
78	pub id: Asc<[u8; 16]>,
79}
80
81#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
82pub struct GuestRowMappingSuffix {
83	pub id: Asc<[u8; 16]>,
84}
85
86#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
87pub struct CustomNotCachedKey {
88	pub group: Desc<GroupId>,
89	pub id: Asc<[u8; 16]>,
90}
91
92#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
93pub struct CustomNotCachedSuffix {
94	pub id: Asc<[u8; 16]>,
95}
96
97impl CustomNotCachedSuffix {
98	pub const ID_LEN: usize = 16;
99
100	pub fn of(id: &[u8]) -> Option<Self> {
101		if id.len() > Self::ID_LEN {
102			return None;
103		}
104		let mut bytes = [0u8; Self::ID_LEN];
105		bytes[..id.len()].copy_from_slice(id);
106		Some(Self {
107			id: Asc(bytes),
108		})
109	}
110}
111
112#[derive(Clone, Copy, Debug, PartialEq, Eq)]
113pub struct SourceWatermark;
114
115impl Keyspace for SourceWatermark {
116	const ID: KeyspaceId = KeyspaceId::SOURCE_WATERMARK;
117	const NAME: &'static str = "SOURCE_WATERMARK";
118	const RANGE_CACHED: bool = true;
119
120	type GroupedKey = SourceWatermarkKey;
121	type Suffix = SourceWatermarkKey;
122
123	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
124		(GroupId::ROOT, *key)
125	}
126
127	fn join(_group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
128		suffix
129	}
130}
131
132#[derive(Clone, Copy, Debug, PartialEq, Eq)]
133pub struct SealLedger;
134
135impl Keyspace for SealLedger {
136	const ID: KeyspaceId = KeyspaceId::SEAL_LEDGER;
137	const NAME: &'static str = "SEAL_LEDGER";
138	const RANGE_CACHED: bool = true;
139
140	type GroupedKey = SealLedgerKey;
141	type Suffix = ();
142
143	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
144		(key.group.0, ())
145	}
146
147	fn join(group: GroupId, _suffix: Self::Suffix) -> Self::GroupedKey {
148		SealLedgerKey {
149			group: Desc(group),
150		}
151	}
152}
153
154#[derive(Clone, Copy, Debug, PartialEq, Eq)]
155pub struct NodeCounter;
156
157impl Keyspace for NodeCounter {
158	const ID: KeyspaceId = KeyspaceId::NODE_COUNTER;
159	const NAME: &'static str = "NODE_COUNTER";
160	const RANGE_CACHED: bool = true;
161
162	type GroupedKey = NodeCounterKey;
163	type Suffix = NodeCounterKey;
164
165	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
166		(GroupId::ROOT, *key)
167	}
168
169	fn join(_group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
170		suffix
171	}
172}
173
174#[derive(Clone, Copy, Debug, PartialEq, Eq)]
175pub struct GateVisibility;
176
177impl Keyspace for GateVisibility {
178	const ID: KeyspaceId = KeyspaceId::GATE_VISIBILITY;
179	const NAME: &'static str = "GATE_VISIBILITY";
180	const RANGE_CACHED: bool = true;
181
182	type GroupedKey = GateVisibilityKey;
183	type Suffix = GateVisibilityKey;
184
185	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
186		(GroupId::ROOT, *key)
187	}
188
189	fn join(_group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
190		suffix
191	}
192}
193
194#[derive(Clone, Copy, Debug, PartialEq, Eq)]
195pub struct GroupRowMapping;
196
197impl Keyspace for GroupRowMapping {
198	const ID: KeyspaceId = KeyspaceId::GROUP_ROW_MAPPING;
199	const NAME: &'static str = "GROUP_ROW_MAPPING";
200	const RANGE_CACHED: bool = true;
201
202	type GroupedKey = GroupRowMappingKey;
203	type Suffix = ();
204
205	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
206		(key.group.0, ())
207	}
208
209	fn join(group: GroupId, _suffix: Self::Suffix) -> Self::GroupedKey {
210		GroupRowMappingKey {
211			group: Desc(group),
212		}
213	}
214}
215
216#[derive(Clone, Copy, Debug, PartialEq, Eq)]
217pub struct GuestRowMapping;
218
219impl Keyspace for GuestRowMapping {
220	const ID: KeyspaceId = KeyspaceId::GUEST_ROW_MAPPING;
221	const NAME: &'static str = "GUEST_ROW_MAPPING";
222	const RANGE_CACHED: bool = true;
223
224	type GroupedKey = GuestRowMappingKey;
225	type Suffix = GuestRowMappingSuffix;
226
227	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
228		(
229			key.group.0,
230			GuestRowMappingSuffix {
231				id: key.id,
232			},
233		)
234	}
235
236	fn join(group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
237		GuestRowMappingKey {
238			group: Desc(group),
239			id: suffix.id,
240		}
241	}
242}
243
244#[derive(Clone, Copy, Debug, PartialEq, Eq)]
245pub struct CustomNotCached;
246
247impl Keyspace for CustomNotCached {
248	const ID: KeyspaceId = KeyspaceId::CUSTOM_NOT_CACHED;
249	const NAME: &'static str = "CUSTOM_NOT_CACHED";
250	const RANGE_CACHED: bool = false;
251
252	type GroupedKey = CustomNotCachedKey;
253	type Suffix = CustomNotCachedSuffix;
254
255	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
256		(
257			key.group.0,
258			CustomNotCachedSuffix {
259				id: key.id,
260			},
261		)
262	}
263
264	fn join(group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
265		CustomNotCachedKey {
266			group: Desc(group),
267			id: suffix.id,
268		}
269	}
270}