reifydb_core/value/index/
encoded.rs1use 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 pub fn from_bytes(bytes: &[u8]) -> Self {
50 Self(CowVec::new(bytes.to_vec()))
51 }
52}