reifydb_core/value/index/
encoded.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use std::ops::Deref;
5
6use serde::{Deserialize, Serialize};
7
8use crate::util::CowVec;
9
10pub type EncodedIndexKeyIter = Box<dyn EncodedIndexKeyIterator>;
11
12pub trait EncodedIndexKeyIterator: Iterator<Item = EncodedIndexKey> {}
13
14impl<I: Iterator<Item = EncodedIndexKey>> EncodedIndexKeyIterator for I {}
15
16#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
17pub struct EncodedIndexKey(pub CowVec<u8>);
18
19impl Deref for EncodedIndexKey {
20	type Target = CowVec<u8>;
21
22	fn deref(&self) -> &Self::Target {
23		&self.0
24	}
25}
26
27impl EncodedIndexKey {
28	pub fn make_mut(&mut self) -> &mut [u8] {
29		self.0.make_mut()
30	}
31
32	#[inline]
33	pub fn is_defined(&self, index: usize) -> bool {
34		let byte = index / 8;
35		let bit = index % 8;
36		(self.0[byte] & (1 << bit)) != 0
37	}
38
39	pub(crate) fn set_valid(&mut self, index: usize, bitvec: bool) {
40		let byte = index / 8;
41		let bit = index % 8;
42		if bitvec {
43			self.0.make_mut()[byte] |= 1 << bit;
44		} else {
45			self.0.make_mut()[byte] &= !(1 << bit);
46		}
47	}
48
49	/// Creates an EncodedIndexKey from a byte slice
50	pub fn from_bytes(bytes: &[u8]) -> Self {
51		Self(CowVec::new(bytes.to_vec()))
52	}
53}