Skip to main content

reifydb_core/encoded/
row.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::ops::Deref;
5
6use reifydb_type::util::cowvec::CowVec;
7use serde::{Deserialize, Serialize};
8
9use crate::encoded::shape::fingerprint::RowShapeFingerprint;
10
11pub const SHAPE_HEADER_SIZE: usize = 24;
12
13pub type EncodedRowIter = Box<dyn EncodedRowIterator>;
14
15pub trait EncodedRowIterator: Iterator<Item = EncodedRow> {}
16
17impl<I: Iterator<Item = EncodedRow>> EncodedRowIterator for I {}
18
19#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
20pub struct EncodedRow(pub CowVec<u8>);
21
22impl Deref for EncodedRow {
23	type Target = CowVec<u8>;
24
25	fn deref(&self) -> &Self::Target {
26		&self.0
27	}
28}
29
30impl EncodedRow {
31	pub fn make_mut(&mut self) -> &mut [u8] {
32		self.0.make_mut()
33	}
34
35	#[inline]
36	pub fn is_defined(&self, index: usize) -> bool {
37		let byte = SHAPE_HEADER_SIZE + index / 8;
38		let bit = index % 8;
39		(self.0[byte] & (1 << bit)) != 0
40	}
41
42	pub(crate) fn set_valid(&mut self, index: usize, valid: bool) {
43		let byte = SHAPE_HEADER_SIZE + index / 8;
44		let bit = index % 8;
45		if valid {
46			self.0.make_mut()[byte] |= 1 << bit;
47		} else {
48			self.0.make_mut()[byte] &= !(1 << bit);
49		}
50	}
51
52	#[inline]
53	pub fn fingerprint(&self) -> RowShapeFingerprint {
54		let bytes: [u8; 8] = self.0[0..8].try_into().unwrap();
55		RowShapeFingerprint::from_le_bytes(bytes)
56	}
57
58	pub fn set_fingerprint(&mut self, fingerprint: RowShapeFingerprint) {
59		self.0.make_mut()[0..8].copy_from_slice(&fingerprint.to_le_bytes());
60	}
61
62	#[inline]
63	pub fn created_at_nanos(&self) -> u64 {
64		let bytes: [u8; 8] = self.0[8..16].try_into().unwrap();
65		u64::from_le_bytes(bytes)
66	}
67
68	#[inline]
69	pub fn updated_at_nanos(&self) -> u64 {
70		let bytes: [u8; 8] = self.0[16..24].try_into().unwrap();
71		u64::from_le_bytes(bytes)
72	}
73
74	pub fn set_timestamps(&mut self, created_at_nanos: u64, updated_at_nanos: u64) {
75		let buf = self.0.make_mut();
76		buf[8..16].copy_from_slice(&created_at_nanos.to_le_bytes());
77		buf[16..24].copy_from_slice(&updated_at_nanos.to_le_bytes());
78	}
79}