Skip to main content

reifydb_codec/row/pod/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4//! The pod storage family: an entry read and written whole, carrying no header at all, so the body
5//! is the whole row and offset zero is payload rather than a fingerprint or a stamp.
6
7use std::ops::Deref;
8
9use reifydb_value::{byte_size::ByteSize, util::cowvec::CowVec};
10
11use crate::row::bytes::{EncodedBytes, EncodedRowBuilder, RowBuilder, read_defined_at, sealed::Sealed};
12
13pub const POD_HEADER_SIZE: usize = 0;
14
15#[repr(transparent)]
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct EncodedPodRow(EncodedBytes);
18
19impl EncodedPodRow {
20	pub fn new(body: &[u8]) -> Self {
21		Self(EncodedBytes(CowVec::new(body.to_vec())))
22	}
23
24	pub fn view(bytes: &EncodedBytes) -> &Self {
25		// SAFETY: EncodedPodRow is repr(transparent) over EncodedBytes, so the pointer cast
26		// preserves layout, and the returned reference borrows the same allocation for the same lifetime.
27		unsafe { &*(bytes as *const EncodedBytes as *const Self) }
28	}
29
30	pub fn bytes(&self) -> &EncodedBytes {
31		&self.0
32	}
33
34	pub fn as_slice(&self) -> &[u8] {
35		self.0.as_slice()
36	}
37
38	pub fn into_bytes(self) -> EncodedBytes {
39		self.0
40	}
41
42	pub fn body(&self) -> &[u8] {
43		&self.0[POD_HEADER_SIZE..]
44	}
45
46	pub fn body_mut(&mut self) -> &mut [u8] {
47		&mut self.0.make_mut()[POD_HEADER_SIZE..]
48	}
49
50	pub fn len(&self) -> usize {
51		self.0.len()
52	}
53
54	pub fn is_empty(&self) -> bool {
55		self.body().is_empty()
56	}
57
58	pub fn byte_size(&self) -> ByteSize {
59		ByteSize::from(self.0.len() as u64)
60	}
61}
62
63impl From<EncodedBytes> for EncodedPodRow {
64	fn from(bytes: EncodedBytes) -> Self {
65		Self(bytes)
66	}
67}
68
69impl From<EncodedPodRow> for EncodedBytes {
70	fn from(row: EncodedPodRow) -> Self {
71		row.0
72	}
73}
74
75/// The write side of the pod family: a buffer with no header at all, so offset zero is already
76/// payload and no stamp or fingerprint may be written into it.
77#[repr(transparent)]
78#[derive(Debug, Clone, PartialEq, Eq)]
79pub struct EncodedPodRowBuilder(EncodedRowBuilder);
80
81impl EncodedPodRowBuilder {
82	pub(crate) fn wrap(builder: EncodedRowBuilder) -> Self {
83		Self(builder)
84	}
85
86	#[inline]
87	pub fn is_defined(&self, index: usize) -> bool {
88		read_defined_at(self.as_slice(), POD_HEADER_SIZE, index)
89	}
90
91	pub fn body(&self) -> &[u8] {
92		&self.as_slice()[POD_HEADER_SIZE..]
93	}
94
95	pub fn freeze(self) -> EncodedPodRow {
96		EncodedPodRow(self.0.freeze())
97	}
98}
99
100impl Sealed for EncodedPodRowBuilder {
101	fn buffer(&self) -> &Vec<u8> {
102		self.0.buffer()
103	}
104
105	fn buffer_mut(&mut self) -> &mut Vec<u8> {
106		self.0.buffer_mut()
107	}
108
109	fn take_buffer(self) -> Vec<u8> {
110		self.0.take_buffer()
111	}
112}
113
114impl EncodedPodRow {
115	pub fn thaw(self) -> EncodedPodRowBuilder {
116		EncodedPodRowBuilder(self.0.thaw())
117	}
118}
119
120impl Deref for EncodedPodRowBuilder {
121	type Target = [u8];
122
123	fn deref(&self) -> &Self::Target {
124		self.as_slice()
125	}
126}