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, RowBuilder, SHAPE_HEADER_SIZE, SourceRowBuilder, read_created_at,
11		read_defined_at, read_fingerprint, read_storage_time, read_updated_at, sealed::Sealed,
12		write_fingerprint, 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 is_defined(&self, index: usize) -> bool {
62		read_defined_at(&self.0, SHAPE_HEADER_SIZE, index)
63	}
64
65	pub fn body(&self) -> &[u8] {
66		&self.0[SHAPE_HEADER_SIZE..]
67	}
68}
69
70impl From<EncodedRingBufferRow> for EncodedBytes {
71	fn from(row: EncodedRingBufferRow) -> Self {
72		row.0
73	}
74}
75
76impl From<EncodedBytes> for EncodedRingBufferRow {
77	fn from(bytes: EncodedBytes) -> Self {
78		Self(bytes)
79	}
80}
81
82#[repr(transparent)]
83#[derive(Debug, Clone, PartialEq, Eq)]
84pub struct EncodedRingBufferRowBuilder(EncodedRowBuilder);
85
86impl EncodedRingBufferRowBuilder {
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	#[inline]
115	pub fn time(&self) -> Option<DateTime> {
116		read_storage_time(self.as_slice())
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 is_defined(&self, index: usize) -> bool {
125		read_defined_at(self.as_slice(), SHAPE_HEADER_SIZE, index)
126	}
127
128	pub fn body(&self) -> &[u8] {
129		&self.as_slice()[SHAPE_HEADER_SIZE..]
130	}
131
132	pub fn freeze(self) -> EncodedRingBufferRow {
133		EncodedRingBufferRow(self.0.freeze())
134	}
135}
136
137impl SourceRowBuilder for EncodedRingBufferRowBuilder {
138	fn set_timestamps(&mut self, created_at: DateTime, updated_at: DateTime) {
139		EncodedRingBufferRowBuilder::set_timestamps(self, created_at, updated_at);
140	}
141
142	fn set_time(&mut self, time: DateTime) {
143		EncodedRingBufferRowBuilder::set_time(self, time);
144	}
145}
146
147impl Sealed for EncodedRingBufferRowBuilder {
148	fn buffer(&self) -> &Vec<u8> {
149		self.0.buffer()
150	}
151
152	fn buffer_mut(&mut self) -> &mut Vec<u8> {
153		self.0.buffer_mut()
154	}
155
156	fn take_buffer(self) -> Vec<u8> {
157		self.0.take_buffer()
158	}
159}
160
161impl EncodedRingBufferRow {
162	pub fn thaw(self) -> EncodedRingBufferRowBuilder {
163		EncodedRingBufferRowBuilder(self.0.thaw())
164	}
165}
166
167impl Deref for EncodedRingBufferRowBuilder {
168	type Target = [u8];
169
170	fn deref(&self) -> &Self::Target {
171		self.as_slice()
172	}
173}