Skip to main content

reifydb_core/encoded/
encoded.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
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::schema::fingerprint::SchemaFingerprint;
10
11/// Size of schema header (fingerprint) in bytes
12pub const SCHEMA_HEADER_SIZE: usize = 8;
13
14/// A boxed values iterator.
15pub type EncodedValuesIter = Box<dyn EncodedValuesIterator>;
16
17pub trait EncodedValuesIterator: Iterator<Item = EncodedValues> {}
18
19impl<I: Iterator<Item = EncodedValues>> EncodedValuesIterator for I {}
20
21#[derive(Debug, Clone, Serialize, Deserialize)]
22// [schema_finger_print]:[bitvec]:[static_values]:[dynamic_values]
23#[derive(PartialEq, Eq)]
24pub struct EncodedValues(pub CowVec<u8>);
25
26impl Deref for EncodedValues {
27	type Target = CowVec<u8>;
28
29	fn deref(&self) -> &Self::Target {
30		&self.0
31	}
32}
33
34impl EncodedValues {
35	pub fn make_mut(&mut self) -> &mut [u8] {
36		self.0.make_mut()
37	}
38
39	#[inline]
40	pub fn is_defined(&self, index: usize) -> bool {
41		let byte = SCHEMA_HEADER_SIZE + index / 8;
42		let bit = index % 8;
43		(self.0[byte] & (1 << bit)) != 0
44	}
45
46	pub(crate) fn set_valid(&mut self, index: usize, valid: bool) {
47		let byte = SCHEMA_HEADER_SIZE + index / 8;
48		let bit = index % 8;
49		if valid {
50			self.0.make_mut()[byte] |= 1 << bit;
51		} else {
52			self.0.make_mut()[byte] &= !(1 << bit);
53		}
54	}
55
56	/// Read the schema fingerprint from the header
57	#[inline]
58	pub fn fingerprint(&self) -> SchemaFingerprint {
59		let bytes: [u8; 8] = self.0[0..8].try_into().unwrap();
60		SchemaFingerprint::from_le_bytes(bytes)
61	}
62
63	/// Write the schema fingerprint to the header
64	pub fn set_fingerprint(&mut self, fingerprint: SchemaFingerprint) {
65		self.0.make_mut()[0..8].copy_from_slice(&fingerprint.to_le_bytes());
66	}
67}