Skip to main content

reifydb_codec/row/queue/
mod.rs

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