Skip to main content

reifydb_codec/row/ringbuffer/
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, RINGBUFFER_HEADER_SIZE, RowBuilder, SourceRowBuilder, read_created_at,
11		read_defined_at, read_fingerprint, read_sequence, read_storage_time, read_updated_at, sealed::Sealed,
12		write_fingerprint, write_sequence, write_storage_time, write_timestamps,
13	},
14	shape::fingerprint::RowShapeFingerprint,
15};
16
17#[repr(transparent)]
18#[derive(Debug, Clone, PartialEq)]
19pub struct EncodedRingBufferRow(EncodedBytes);
20
21impl EncodedRingBufferRow {
22	pub fn view(bytes: &EncodedBytes) -> &Self {
23		// SAFETY: EncodedRingBufferRow 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 time(&self) -> Option<DateTime> {
57		read_storage_time(&self.0)
58	}
59
60	#[inline]
61	pub fn sequence(&self) -> u64 {
62		read_sequence(&self.0)
63	}
64
65	#[inline]
66	pub fn is_defined(&self, index: usize) -> bool {
67		read_defined_at(&self.0, RINGBUFFER_HEADER_SIZE, index)
68	}
69
70	pub fn body(&self) -> &[u8] {
71		&self.0[RINGBUFFER_HEADER_SIZE..]
72	}
73}
74
75impl From<EncodedRingBufferRow> for EncodedBytes {
76	fn from(row: EncodedRingBufferRow) -> Self {
77		row.0
78	}
79}
80
81impl From<EncodedBytes> for EncodedRingBufferRow {
82	fn from(bytes: EncodedBytes) -> Self {
83		Self(bytes)
84	}
85}
86
87#[repr(transparent)]
88#[derive(Debug, Clone, PartialEq, Eq)]
89pub struct EncodedRingBufferRowBuilder(EncodedRowBuilder);
90
91impl EncodedRingBufferRowBuilder {
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	#[inline]
120	pub fn time(&self) -> Option<DateTime> {
121		read_storage_time(self.as_slice())
122	}
123
124	pub fn set_time(&mut self, time: DateTime) {
125		write_storage_time(self.as_mut_slice(), time);
126	}
127
128	#[inline]
129	pub fn sequence(&self) -> u64 {
130		read_sequence(self.as_slice())
131	}
132
133	pub fn set_sequence(&mut self, sequence: u64) {
134		write_sequence(self.as_mut_slice(), sequence);
135	}
136
137	#[inline]
138	pub fn is_defined(&self, index: usize) -> bool {
139		read_defined_at(self.as_slice(), RINGBUFFER_HEADER_SIZE, index)
140	}
141
142	pub fn body(&self) -> &[u8] {
143		&self.as_slice()[RINGBUFFER_HEADER_SIZE..]
144	}
145
146	pub fn freeze(self) -> EncodedRingBufferRow {
147		EncodedRingBufferRow(self.0.freeze())
148	}
149}
150
151impl SourceRowBuilder for EncodedRingBufferRowBuilder {
152	fn set_timestamps(&mut self, created_at: DateTime, updated_at: DateTime) {
153		EncodedRingBufferRowBuilder::set_timestamps(self, created_at, updated_at);
154	}
155
156	fn set_time(&mut self, time: DateTime) {
157		EncodedRingBufferRowBuilder::set_time(self, time);
158	}
159}
160
161impl Sealed for EncodedRingBufferRowBuilder {
162	fn buffer(&self) -> &Vec<u8> {
163		self.0.buffer()
164	}
165
166	fn buffer_mut(&mut self) -> &mut Vec<u8> {
167		self.0.buffer_mut()
168	}
169
170	fn take_buffer(self) -> Vec<u8> {
171		self.0.take_buffer()
172	}
173}
174
175impl EncodedRingBufferRow {
176	pub fn thaw(self) -> EncodedRingBufferRowBuilder {
177		EncodedRingBufferRowBuilder(self.0.thaw())
178	}
179}
180
181impl Deref for EncodedRingBufferRowBuilder {
182	type Target = [u8];
183
184	fn deref(&self) -> &Self::Target {
185		self.as_slice()
186	}
187}