Skip to main content

reifydb_codec/row/queue/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4//! The queue storage family: the source header plus a `not_before` instant that gates when an item
5//! becomes due. Absence is a flag bit, not a sentinel instant, so a due-now item stays due-now.
6
7use std::ops::Deref;
8
9use reifydb_value::value::datetime::DateTime;
10
11use crate::row::{
12	bytes::{
13		EncodedBytes, EncodedRowBuilder, QUEUE_HEADER_SIZE, RowBuilder, read_created_at, read_defined_at,
14		read_fingerprint, read_not_before, read_updated_at, sealed::Sealed, write_fingerprint,
15		write_not_before, write_storage_time, write_timestamps,
16	},
17	shape::fingerprint::RowShapeFingerprint,
18};
19
20#[repr(transparent)]
21#[derive(Debug, Clone, PartialEq)]
22pub struct EncodedQueueRow(EncodedBytes);
23
24impl EncodedQueueRow {
25	pub fn view(bytes: &EncodedBytes) -> &Self {
26		// SAFETY: EncodedQueueRow is repr(transparent) over EncodedBytes, so the pointer cast preserves
27		// layout, and the returned reference borrows the same allocation for the same lifetime.
28		unsafe { &*(bytes as *const EncodedBytes as *const Self) }
29	}
30
31	pub fn bytes(&self) -> &EncodedBytes {
32		&self.0
33	}
34
35	pub fn as_slice(&self) -> &[u8] {
36		self.0.as_slice()
37	}
38
39	pub fn into_bytes(self) -> EncodedBytes {
40		self.0
41	}
42
43	#[inline]
44	pub fn fingerprint(&self) -> RowShapeFingerprint {
45		read_fingerprint(&self.0)
46	}
47
48	#[inline]
49	pub fn created_at(&self) -> DateTime {
50		read_created_at(&self.0)
51	}
52
53	#[inline]
54	pub fn updated_at(&self) -> DateTime {
55		read_updated_at(&self.0)
56	}
57
58	#[inline]
59	pub fn not_before(&self) -> Option<DateTime> {
60		read_not_before(&self.0)
61	}
62
63	#[inline]
64	pub fn is_defined(&self, index: usize) -> bool {
65		read_defined_at(&self.0, QUEUE_HEADER_SIZE, index)
66	}
67
68	pub fn body(&self) -> &[u8] {
69		&self.0[QUEUE_HEADER_SIZE..]
70	}
71}
72
73impl From<EncodedQueueRow> for EncodedBytes {
74	fn from(row: EncodedQueueRow) -> Self {
75		row.0
76	}
77}
78
79impl From<EncodedBytes> for EncodedQueueRow {
80	fn from(bytes: EncodedBytes) -> Self {
81		Self(bytes)
82	}
83}
84
85/// The write side of the queue family: a buffer whose source header and `not_before` slot are
86/// already reserved, which freezes into an [`EncodedQueueRow`] and never into a row of another family.
87#[repr(transparent)]
88#[derive(Debug, Clone, PartialEq, Eq)]
89pub struct EncodedQueueRowBuilder(EncodedRowBuilder);
90
91impl EncodedQueueRowBuilder {
92	pub(crate) fn wrap(builder: EncodedRowBuilder) -> Self {
93		Self(builder)
94	}
95
96	#[inline]
97	pub fn fingerprint(&self) -> RowShapeFingerprint {
98		read_fingerprint(self.as_slice())
99	}
100
101	pub fn set_fingerprint(&mut self, fingerprint: RowShapeFingerprint) {
102		write_fingerprint(self.as_mut_slice(), fingerprint);
103	}
104
105	#[inline]
106	pub fn created_at(&self) -> DateTime {
107		read_created_at(self.as_slice())
108	}
109
110	#[inline]
111	pub fn updated_at(&self) -> DateTime {
112		read_updated_at(self.as_slice())
113	}
114
115	pub fn set_timestamps(&mut self, created_at: DateTime, updated_at: DateTime) {
116		write_timestamps(self.as_mut_slice(), created_at, updated_at);
117	}
118
119	pub fn set_time(&mut self, time: DateTime) {
120		write_storage_time(self.as_mut_slice(), time);
121	}
122
123	#[inline]
124	pub fn not_before(&self) -> Option<DateTime> {
125		read_not_before(self.as_slice())
126	}
127
128	pub fn set_not_before(&mut self, not_before: DateTime) {
129		write_not_before(self.as_mut_slice(), not_before);
130	}
131
132	#[inline]
133	pub fn is_defined(&self, index: usize) -> bool {
134		read_defined_at(self.as_slice(), QUEUE_HEADER_SIZE, index)
135	}
136
137	pub fn body(&self) -> &[u8] {
138		&self.as_slice()[QUEUE_HEADER_SIZE..]
139	}
140
141	pub fn freeze(self) -> EncodedQueueRow {
142		EncodedQueueRow(self.0.freeze())
143	}
144}
145
146impl Sealed for EncodedQueueRowBuilder {
147	fn buffer(&self) -> &Vec<u8> {
148		self.0.buffer()
149	}
150
151	fn buffer_mut(&mut self) -> &mut Vec<u8> {
152		self.0.buffer_mut()
153	}
154
155	fn take_buffer(self) -> Vec<u8> {
156		self.0.take_buffer()
157	}
158}
159
160impl EncodedQueueRow {
161	pub fn thaw(self) -> EncodedQueueRowBuilder {
162		EncodedQueueRowBuilder(self.0.thaw())
163	}
164}
165
166impl Deref for EncodedQueueRowBuilder {
167	type Target = [u8];
168
169	fn deref(&self) -> &Self::Target {
170		self.as_slice()
171	}
172}