Skip to main content

reifydb_core/key/operator/keyspace/
window.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use reifydb_value::{util::hash::Hash128, 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 AccumulatorKey {
23	pub group: Desc<GroupId>,
24}
25
26#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
27pub struct BufferKey {
28	pub group: Desc<GroupId>,
29}
30
31#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
32pub struct RollingPaneKey {
33	pub group: Desc<GroupId>,
34	pub pane: Asc<u64>,
35}
36
37#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
38pub struct RunningKey {
39	pub group: Desc<GroupId>,
40}
41
42#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
43pub struct CountKey {
44	pub group: Desc<GroupId>,
45}
46
47#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
48pub struct SessionKey {
49	pub group: Desc<GroupId>,
50}
51
52#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
53pub struct RollingMetaKey {
54	pub group: Desc<GroupId>,
55}
56
57#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
58pub struct EngineMetaKey {
59	pub group: Desc<GroupId>,
60}
61
62#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
63pub struct EmitKey {
64	pub group: Desc<GroupId>,
65	pub row: Asc<RowNumber>,
66}
67
68#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
69pub struct RowIndexKey {
70	pub group: Desc<GroupId>,
71	pub row: Asc<RowNumber>,
72}
73
74#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
75pub struct SessionMemberKey {
76	pub group: Desc<GroupId>,
77	pub row: Asc<RowNumber>,
78}
79
80#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
81pub struct WindowMetaKey {
82	pub group: Desc<GroupId>,
83	pub window: Desc<Hash128>,
84}
85
86#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
87pub struct WindowMetaSuffix {
88	pub window: Desc<Hash128>,
89}
90
91#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
92pub struct GuestAccumulatorKey {
93	pub group: Desc<GroupId>,
94	pub slot: Asc<[u8; 16]>,
95}
96
97#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
98pub struct GuestBufferKey {
99	pub group: Desc<GroupId>,
100	pub slot: Asc<[u8; 16]>,
101}
102
103#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
104pub struct GuestRunningKey {
105	pub group: Desc<GroupId>,
106	pub slot: Asc<[u8; 16]>,
107}
108
109#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
110pub struct GuestWindowPublishKey {
111	pub group: Desc<GroupId>,
112	pub slot: Asc<[u8; 16]>,
113}
114
115#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, KeyLayout, HeapSize)]
116pub struct GuestRetainedEntryKey {
117	pub group: Desc<GroupId>,
118	pub slot: Asc<[u8; 16]>,
119}
120
121#[derive(Clone, Copy, Debug, PartialEq, Eq)]
122pub struct Accumulator;
123
124impl Keyspace for Accumulator {
125	const ID: KeyspaceId = KeyspaceId::ACCUMULATOR;
126	const NAME: &'static str = "ACCUMULATOR";
127	const RANGE_CACHED: bool = true;
128
129	type GroupedKey = AccumulatorKey;
130	type Suffix = ();
131
132	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
133		(key.group.0, ())
134	}
135
136	fn join(group: GroupId, _suffix: Self::Suffix) -> Self::GroupedKey {
137		AccumulatorKey {
138			group: Desc(group),
139		}
140	}
141}
142
143#[derive(Clone, Copy, Debug, PartialEq, Eq)]
144pub struct Buffer;
145
146impl Keyspace for Buffer {
147	const ID: KeyspaceId = KeyspaceId::BUFFER;
148	const NAME: &'static str = "BUFFER";
149	const RANGE_CACHED: bool = true;
150
151	type GroupedKey = BufferKey;
152	type Suffix = ();
153
154	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
155		(key.group.0, ())
156	}
157
158	fn join(group: GroupId, _suffix: Self::Suffix) -> Self::GroupedKey {
159		BufferKey {
160			group: Desc(group),
161		}
162	}
163}
164
165#[derive(Clone, Copy, Debug, PartialEq, Eq)]
166pub struct RollingPane;
167
168impl Keyspace for RollingPane {
169	const ID: KeyspaceId = KeyspaceId::ROLLING_PANE;
170	const NAME: &'static str = "ROLLING_PANE";
171	const RANGE_CACHED: bool = true;
172
173	type GroupedKey = RollingPaneKey;
174	type Suffix = Asc<u64>;
175
176	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
177		(key.group.0, key.pane)
178	}
179
180	fn join(group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
181		RollingPaneKey {
182			group: Desc(group),
183			pane: suffix,
184		}
185	}
186}
187
188#[derive(Clone, Copy, Debug, PartialEq, Eq)]
189pub struct Running;
190
191impl Keyspace for Running {
192	const ID: KeyspaceId = KeyspaceId::RUNNING;
193	const NAME: &'static str = "RUNNING";
194	const RANGE_CACHED: bool = true;
195
196	type GroupedKey = RunningKey;
197	type Suffix = ();
198
199	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
200		(key.group.0, ())
201	}
202
203	fn join(group: GroupId, _suffix: Self::Suffix) -> Self::GroupedKey {
204		RunningKey {
205			group: Desc(group),
206		}
207	}
208}
209
210#[derive(Clone, Copy, Debug, PartialEq, Eq)]
211pub struct Count;
212
213impl Keyspace for Count {
214	const ID: KeyspaceId = KeyspaceId::COUNT;
215	const NAME: &'static str = "COUNT";
216	const RANGE_CACHED: bool = true;
217
218	type GroupedKey = CountKey;
219	type Suffix = ();
220
221	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
222		(key.group.0, ())
223	}
224
225	fn join(group: GroupId, _suffix: Self::Suffix) -> Self::GroupedKey {
226		CountKey {
227			group: Desc(group),
228		}
229	}
230}
231
232#[derive(Clone, Copy, Debug, PartialEq, Eq)]
233pub struct Session;
234
235impl Keyspace for Session {
236	const ID: KeyspaceId = KeyspaceId::SESSION;
237	const NAME: &'static str = "SESSION";
238	const RANGE_CACHED: bool = true;
239
240	type GroupedKey = SessionKey;
241	type Suffix = ();
242
243	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
244		(key.group.0, ())
245	}
246
247	fn join(group: GroupId, _suffix: Self::Suffix) -> Self::GroupedKey {
248		SessionKey {
249			group: Desc(group),
250		}
251	}
252}
253
254#[derive(Clone, Copy, Debug, PartialEq, Eq)]
255pub struct RollingMeta;
256
257impl Keyspace for RollingMeta {
258	const ID: KeyspaceId = KeyspaceId::ROLLING_META;
259	const NAME: &'static str = "ROLLING_META";
260	const RANGE_CACHED: bool = true;
261
262	type GroupedKey = RollingMetaKey;
263	type Suffix = ();
264
265	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
266		(key.group.0, ())
267	}
268
269	fn join(group: GroupId, _suffix: Self::Suffix) -> Self::GroupedKey {
270		RollingMetaKey {
271			group: Desc(group),
272		}
273	}
274}
275
276#[derive(Clone, Copy, Debug, PartialEq, Eq)]
277pub struct EngineMeta;
278
279impl Keyspace for EngineMeta {
280	const ID: KeyspaceId = KeyspaceId::ENGINE_META;
281	const NAME: &'static str = "ENGINE_META";
282	const RANGE_CACHED: bool = true;
283
284	type GroupedKey = EngineMetaKey;
285	type Suffix = ();
286
287	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
288		(key.group.0, ())
289	}
290
291	fn join(group: GroupId, _suffix: Self::Suffix) -> Self::GroupedKey {
292		EngineMetaKey {
293			group: Desc(group),
294		}
295	}
296}
297
298#[derive(Clone, Copy, Debug, PartialEq, Eq)]
299pub struct Emit;
300
301impl Keyspace for Emit {
302	const ID: KeyspaceId = KeyspaceId::EMIT;
303	const NAME: &'static str = "EMIT";
304	const RANGE_CACHED: bool = true;
305
306	type GroupedKey = EmitKey;
307	type Suffix = Asc<RowNumber>;
308
309	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
310		(key.group.0, key.row)
311	}
312
313	fn join(group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
314		EmitKey {
315			group: Desc(group),
316			row: suffix,
317		}
318	}
319}
320
321#[derive(Clone, Copy, Debug, PartialEq, Eq)]
322pub struct RowIndex;
323
324impl Keyspace for RowIndex {
325	const ID: KeyspaceId = KeyspaceId::ROW_INDEX;
326	const NAME: &'static str = "ROW_INDEX";
327	const RANGE_CACHED: bool = true;
328
329	type GroupedKey = RowIndexKey;
330	type Suffix = Asc<RowNumber>;
331
332	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
333		(key.group.0, key.row)
334	}
335
336	fn join(group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
337		RowIndexKey {
338			group: Desc(group),
339			row: suffix,
340		}
341	}
342}
343
344#[derive(Clone, Copy, Debug, PartialEq, Eq)]
345pub struct SessionMember;
346
347impl Keyspace for SessionMember {
348	const ID: KeyspaceId = KeyspaceId::SESSION_MEMBER;
349	const NAME: &'static str = "SESSION_MEMBER";
350	const RANGE_CACHED: bool = true;
351
352	type GroupedKey = SessionMemberKey;
353	type Suffix = Asc<RowNumber>;
354
355	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
356		(key.group.0, key.row)
357	}
358
359	fn join(group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
360		SessionMemberKey {
361			group: Desc(group),
362			row: suffix,
363		}
364	}
365}
366
367#[derive(Clone, Copy, Debug, PartialEq, Eq)]
368pub struct WindowMeta;
369
370impl Keyspace for WindowMeta {
371	const ID: KeyspaceId = KeyspaceId::WINDOW_META;
372	const NAME: &'static str = "WINDOW_META";
373	const RANGE_CACHED: bool = true;
374
375	type GroupedKey = WindowMetaKey;
376	type Suffix = WindowMetaSuffix;
377
378	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
379		(
380			key.group.0,
381			WindowMetaSuffix {
382				window: key.window,
383			},
384		)
385	}
386
387	fn join(group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
388		WindowMetaKey {
389			group: Desc(group),
390			window: suffix.window,
391		}
392	}
393}
394
395#[derive(Clone, Copy, Debug, PartialEq, Eq)]
396pub struct GuestAccumulator;
397
398impl Keyspace for GuestAccumulator {
399	const ID: KeyspaceId = KeyspaceId::GUEST_ACCUMULATOR;
400	const NAME: &'static str = "GUEST_ACCUMULATOR";
401	const RANGE_CACHED: bool = true;
402
403	type GroupedKey = GuestAccumulatorKey;
404	type Suffix = Asc<[u8; 16]>;
405
406	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
407		(key.group.0, key.slot)
408	}
409
410	fn join(group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
411		GuestAccumulatorKey {
412			group: Desc(group),
413			slot: suffix,
414		}
415	}
416}
417
418#[derive(Clone, Copy, Debug, PartialEq, Eq)]
419pub struct GuestBuffer;
420
421impl Keyspace for GuestBuffer {
422	const ID: KeyspaceId = KeyspaceId::GUEST_BUFFER;
423	const NAME: &'static str = "GUEST_BUFFER";
424	const RANGE_CACHED: bool = true;
425
426	type GroupedKey = GuestBufferKey;
427	type Suffix = Asc<[u8; 16]>;
428
429	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
430		(key.group.0, key.slot)
431	}
432
433	fn join(group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
434		GuestBufferKey {
435			group: Desc(group),
436			slot: suffix,
437		}
438	}
439}
440
441#[derive(Clone, Copy, Debug, PartialEq, Eq)]
442pub struct GuestRunning;
443
444impl Keyspace for GuestRunning {
445	const ID: KeyspaceId = KeyspaceId::GUEST_RUNNING;
446	const NAME: &'static str = "GUEST_RUNNING";
447	const RANGE_CACHED: bool = true;
448
449	type GroupedKey = GuestRunningKey;
450	type Suffix = Asc<[u8; 16]>;
451
452	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
453		(key.group.0, key.slot)
454	}
455
456	fn join(group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
457		GuestRunningKey {
458			group: Desc(group),
459			slot: suffix,
460		}
461	}
462}
463
464#[derive(Clone, Copy, Debug, PartialEq, Eq)]
465pub struct GuestWindowPublish;
466
467impl Keyspace for GuestWindowPublish {
468	const ID: KeyspaceId = KeyspaceId::GUEST_WINDOW_PUBLISH;
469	const NAME: &'static str = "GUEST_WINDOW_PUBLISH";
470	const RANGE_CACHED: bool = true;
471
472	type GroupedKey = GuestWindowPublishKey;
473	type Suffix = Asc<[u8; 16]>;
474
475	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
476		(key.group.0, key.slot)
477	}
478
479	fn join(group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
480		GuestWindowPublishKey {
481			group: Desc(group),
482			slot: suffix,
483		}
484	}
485}
486
487#[derive(Clone, Copy, Debug, PartialEq, Eq)]
488pub struct GuestRetainedEntry;
489
490impl Keyspace for GuestRetainedEntry {
491	const ID: KeyspaceId = KeyspaceId::GUEST_RETAINED_ENTRY;
492	const NAME: &'static str = "GUEST_RETAINED_ENTRY";
493	const RANGE_CACHED: bool = true;
494
495	type GroupedKey = GuestRetainedEntryKey;
496	type Suffix = Asc<[u8; 16]>;
497
498	fn split(key: &Self::GroupedKey) -> (GroupId, Self::Suffix) {
499		(key.group.0, key.slot)
500	}
501
502	fn join(group: GroupId, suffix: Self::Suffix) -> Self::GroupedKey {
503		GuestRetainedEntryKey {
504			group: Desc(group),
505			slot: suffix,
506		}
507	}
508}