reifydb_core/value/index/
encoded.rs1use std::{
5 borrow::Borrow,
6 cmp::Ordering,
7 fmt,
8 hash::{Hash, Hasher},
9 mem,
10 ops::Deref,
11};
12
13use serde::{
14 de::{Deserialize, Deserializer},
15 ser::{Serialize, Serializer},
16};
17
18pub type EncodedIndexKeyIter = Box<dyn EncodedIndexKeyIterator>;
19
20pub trait EncodedIndexKeyIterator: Iterator<Item = EncodedIndexKey> {}
21
22impl<I: Iterator<Item = EncodedIndexKey>> EncodedIndexKeyIterator for I {}
23
24#[derive(Clone)]
25pub enum EncodedIndexKey {
26 Inline {
27 len: u8,
28 buf: [u8; 62],
29 },
30 Heap(Vec<u8>),
31}
32
33const _: () = assert!(mem::size_of::<EncodedIndexKey>() == 64);
34
35impl EncodedIndexKey {
36 const INLINE_CAP: usize = 62;
37
38 pub fn new(bytes: impl AsRef<[u8]>) -> Self {
39 let bytes = bytes.as_ref();
40 if bytes.len() <= Self::INLINE_CAP {
41 let mut buf = [0u8; 62];
42 buf[..bytes.len()].copy_from_slice(bytes);
43 EncodedIndexKey::Inline {
44 len: bytes.len() as u8,
45 buf,
46 }
47 } else {
48 EncodedIndexKey::Heap(bytes.to_vec())
49 }
50 }
51
52 pub fn from_bytes(bytes: &[u8]) -> Self {
53 Self::new(bytes)
54 }
55
56 pub fn as_slice(&self) -> &[u8] {
57 match self {
58 EncodedIndexKey::Inline {
59 len,
60 buf,
61 } => &buf[..*len as usize],
62 EncodedIndexKey::Heap(v) => v.as_slice(),
63 }
64 }
65
66 pub fn make_mut(&mut self) -> &mut [u8] {
67 match self {
68 EncodedIndexKey::Inline {
69 len,
70 buf,
71 } => &mut buf[..*len as usize],
72 EncodedIndexKey::Heap(v) => v.as_mut_slice(),
73 }
74 }
75
76 #[inline]
77 pub fn is_defined(&self, index: usize) -> bool {
78 let byte = index / 8;
79 let bit = index % 8;
80 (self.as_slice()[byte] & (1 << bit)) != 0
81 }
82
83 pub(crate) fn set_valid(&mut self, index: usize, bitvec: bool) {
84 let byte = index / 8;
85 let bit = index % 8;
86 if bitvec {
87 self.make_mut()[byte] |= 1 << bit;
88 } else {
89 self.make_mut()[byte] &= !(1 << bit);
90 }
91 }
92}
93
94impl Deref for EncodedIndexKey {
95 type Target = [u8];
96
97 fn deref(&self) -> &[u8] {
98 self.as_slice()
99 }
100}
101
102impl AsRef<[u8]> for EncodedIndexKey {
103 fn as_ref(&self) -> &[u8] {
104 self.as_slice()
105 }
106}
107
108impl Borrow<[u8]> for EncodedIndexKey {
109 fn borrow(&self) -> &[u8] {
110 self.as_slice()
111 }
112}
113
114impl PartialEq for EncodedIndexKey {
115 fn eq(&self, other: &Self) -> bool {
116 self.as_slice() == other.as_slice()
117 }
118}
119
120impl Eq for EncodedIndexKey {}
121
122impl PartialOrd for EncodedIndexKey {
123 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
124 Some(self.cmp(other))
125 }
126}
127
128impl Ord for EncodedIndexKey {
129 fn cmp(&self, other: &Self) -> Ordering {
130 self.as_slice().cmp(other.as_slice())
131 }
132}
133
134impl Hash for EncodedIndexKey {
135 fn hash<H: Hasher>(&self, state: &mut H) {
136 self.as_slice().hash(state);
137 }
138}
139
140impl Serialize for EncodedIndexKey {
141 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
142 self.as_slice().serialize(serializer)
143 }
144}
145
146impl<'de> Deserialize<'de> for EncodedIndexKey {
147 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
148 let vec = Vec::<u8>::deserialize(deserializer)?;
149 Ok(EncodedIndexKey::new(vec))
150 }
151}
152
153impl fmt::Debug for EncodedIndexKey {
154 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155 write!(f, "EncodedIndexKey({:02x?})", self.as_slice())
156 }
157}