Skip to main content

reifydb_core/value/index/
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
9pub type EncodedIndexKeyIter = Box<dyn EncodedIndexKeyIterator>;
10
11pub trait EncodedIndexKeyIterator: Iterator<Item = EncodedIndexKey> {}
12
13impl<I: Iterator<Item = EncodedIndexKey>> EncodedIndexKeyIterator for I {}
14
15#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
16pub struct EncodedIndexKey(pub CowVec<u8>);
17
18impl Deref for EncodedIndexKey {
19	type Target = CowVec<u8>;
20
21	fn deref(&self) -> &Self::Target {
22		&self.0
23	}
24}
25
26impl EncodedIndexKey {
27	pub fn make_mut(&mut self) -> &mut [u8] {
28		self.0.make_mut()
29	}
30
31	#[inline]
32	pub fn is_defined(&self, index: usize) -> bool {
33		let byte = index / 8;
34		let bit = index % 8;
35		(self.0[byte] & (1 << bit)) != 0
36	}
37
38	pub(crate) fn set_valid(&mut self, index: usize, bitvec: bool) {
39		let byte = index / 8;
40		let bit = index % 8;
41		if bitvec {
42			self.0.make_mut()[byte] |= 1 << bit;
43		} else {
44			self.0.make_mut()[byte] &= !(1 << bit);
45		}
46	}
47
48	/// Creates an EncodedIndexKey from a byte slice
49	pub fn from_bytes(bytes: &[u8]) -> Self {
50		Self(CowVec::new(bytes.to_vec()))
51	}
52}