Skip to main content

reifydb_codec/row/ringbuffer/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4//! The ring buffer storage family: the source header, byte for byte as the table family. Eviction
5//! overwrites a slot in place, so `created_at` survives a rewrite while `updated_at` moves.
6
7use std::ops::Deref;
8
9use reifydb_value::value::datetime::DateTime;
10
11use crate::row::{
12	bytes::{
13		EncodedBytes, EncodedRowBuilder, RowBuilder, SHAPE_HEADER_SIZE, SourceRowBuilder, read_created_at,
14		read_defined_at, read_fingerprint, read_storage_time, read_updated_at, sealed::Sealed,
15		write_fingerprint, write_storage_time, write_timestamps,
16	},
17	shape::fingerprint::RowShapeFingerprint,
18};
19
20#[repr(transparent)]
21#[derive(Debug, Clone, PartialEq)]
22pub struct EncodedRingBufferRow(EncodedBytes);
23
24impl EncodedRingBufferRow {
25	pub fn view(bytes: &EncodedBytes) -> &Self {
26		// SAFETY: EncodedRingBufferRow 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 time(&self) -> Option<DateTime> {
60		read_storage_time(&self.0)
61	}
62
63	#[inline]
64	pub fn is_defined(&self, index: usize) -> bool {
65		read_defined_at(&self.0, SHAPE_HEADER_SIZE, index)
66	}
67
68	pub fn body(&self) -> &[u8] {
69		&self.0[SHAPE_HEADER_SIZE..]
70	}
71}
72
73impl From<EncodedRingBufferRow> for EncodedBytes {
74	fn from(row: EncodedRingBufferRow) -> Self {
75		row.0
76	}
77}
78
79impl From<EncodedBytes> for EncodedRingBufferRow {
80	fn from(bytes: EncodedBytes) -> Self {
81		Self(bytes)
82	}
83}
84
85/// The write side of the ring buffer family: a buffer whose source header is already reserved, which
86/// freezes into an [`EncodedRingBufferRow`] and never into a row of another family.
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 is_defined(&self, index: usize) -> bool {
130		read_defined_at(self.as_slice(), SHAPE_HEADER_SIZE, index)
131	}
132
133	pub fn body(&self) -> &[u8] {
134		&self.as_slice()[SHAPE_HEADER_SIZE..]
135	}
136
137	pub fn freeze(self) -> EncodedRingBufferRow {
138		EncodedRingBufferRow(self.0.freeze())
139	}
140}
141
142impl SourceRowBuilder for EncodedRingBufferRowBuilder {
143	fn set_timestamps(&mut self, created_at: DateTime, updated_at: DateTime) {
144		EncodedRingBufferRowBuilder::set_timestamps(self, created_at, updated_at);
145	}
146
147	fn set_time(&mut self, time: DateTime) {
148		EncodedRingBufferRowBuilder::set_time(self, time);
149	}
150}
151
152impl Sealed for EncodedRingBufferRowBuilder {
153	fn buffer(&self) -> &Vec<u8> {
154		self.0.buffer()
155	}
156
157	fn buffer_mut(&mut self) -> &mut Vec<u8> {
158		self.0.buffer_mut()
159	}
160
161	fn take_buffer(self) -> Vec<u8> {
162		self.0.take_buffer()
163	}
164}
165
166impl EncodedRingBufferRow {
167	pub fn thaw(self) -> EncodedRingBufferRowBuilder {
168		EncodedRingBufferRowBuilder(self.0.thaw())
169	}
170}
171
172impl Deref for EncodedRingBufferRowBuilder {
173	type Target = [u8];
174
175	fn deref(&self) -> &Self::Target {
176		self.as_slice()
177	}
178}