reifydb_core/key/
dictionary.rs1use reifydb_codec::key::{
5 deserializer::KeyDeserializer,
6 encoded::{EncodedKey, EncodedKeyRange},
7 serializer::KeySerializer,
8};
9use reifydb_value::value::dictionary::DictionaryId;
10
11use super::{EncodableKey, EncodableKeyRange, KeyKind};
12
13#[derive(Debug, Clone, PartialEq)]
14pub struct DictionaryKey {
15 pub dictionary: DictionaryId,
16}
17
18impl DictionaryKey {
19 pub fn new(dictionary: DictionaryId) -> Self {
20 Self {
21 dictionary,
22 }
23 }
24
25 pub fn encoded(dictionary: impl Into<DictionaryId>) -> EncodedKey {
26 Self::new(dictionary.into()).encode()
27 }
28
29 pub fn full_scan() -> EncodedKeyRange {
30 EncodedKeyRange::start_end(Some(Self::dictionary_start()), Some(Self::dictionary_end()))
31 }
32
33 fn dictionary_start() -> EncodedKey {
34 let mut serializer = KeySerializer::with_capacity(1);
35 serializer.extend_u8(Self::KIND as u8);
36 serializer.to_encoded_key()
37 }
38
39 fn dictionary_end() -> EncodedKey {
40 let mut serializer = KeySerializer::with_capacity(1);
41 serializer.extend_u8(Self::KIND as u8 - 1);
42 serializer.to_encoded_key()
43 }
44}
45
46impl EncodableKey for DictionaryKey {
47 const KIND: KeyKind = KeyKind::Dictionary;
48
49 fn encode(&self) -> EncodedKey {
50 let mut serializer = KeySerializer::with_capacity(9);
51 serializer.extend_u8(Self::KIND as u8).extend_u64(self.dictionary);
52 serializer.to_encoded_key()
53 }
54
55 fn decode(key: &EncodedKey) -> Option<Self> {
56 let mut de = KeyDeserializer::from_bytes(key.as_slice());
57
58 let kind: KeyKind = de.read_u8().ok()?.try_into().ok()?;
59 if kind != Self::KIND {
60 return None;
61 }
62
63 let dictionary = de.read_u64().ok()?;
64
65 Some(Self {
66 dictionary: DictionaryId(dictionary),
67 })
68 }
69}
70
71#[derive(Debug, Clone, PartialEq)]
72pub struct DictionaryEntryKey {
73 pub dictionary: DictionaryId,
74 pub hash: [u8; 16],
75}
76
77impl DictionaryEntryKey {
78 pub fn new(dictionary: DictionaryId, hash: [u8; 16]) -> Self {
79 Self {
80 dictionary,
81 hash,
82 }
83 }
84
85 pub fn encoded(dictionary: impl Into<DictionaryId>, hash: [u8; 16]) -> EncodedKey {
86 Self::new(dictionary.into(), hash).encode()
87 }
88
89 pub fn full_scan(dictionary: DictionaryId) -> EncodedKeyRange {
90 EncodedKeyRange::start_end(Some(Self::entry_start(dictionary)), Some(Self::entry_end(dictionary)))
91 }
92
93 fn entry_start(dictionary: DictionaryId) -> EncodedKey {
94 let mut serializer = KeySerializer::with_capacity(9);
95 serializer.extend_u8(Self::KIND as u8).extend_u64(dictionary);
96 serializer.to_encoded_key()
97 }
98
99 fn entry_end(dictionary: DictionaryId) -> EncodedKey {
100 let mut serializer = KeySerializer::with_capacity(9);
101 serializer.extend_u8(Self::KIND as u8).extend_u64(*dictionary - 1);
102 serializer.to_encoded_key()
103 }
104}
105
106impl EncodableKey for DictionaryEntryKey {
107 const KIND: KeyKind = KeyKind::DictionaryEntry;
108
109 fn encode(&self) -> EncodedKey {
110 let mut serializer = KeySerializer::with_capacity(25);
111 serializer.extend_u8(Self::KIND as u8).extend_u64(self.dictionary).extend_bytes(self.hash);
112 serializer.to_encoded_key()
113 }
114
115 fn decode(key: &EncodedKey) -> Option<Self> {
116 let mut de = KeyDeserializer::from_bytes(key.as_slice());
117
118 let kind: KeyKind = de.read_u8().ok()?.try_into().ok()?;
119 if kind != Self::KIND {
120 return None;
121 }
122
123 let dictionary = de.read_u64().ok()?;
124 let hash_bytes = de.read_raw(16).ok()?;
125 let mut hash = [0u8; 16];
126 hash.copy_from_slice(hash_bytes);
127
128 Some(Self {
129 dictionary: DictionaryId(dictionary),
130 hash,
131 })
132 }
133}
134
135#[derive(Debug, Clone, PartialEq)]
136pub struct DictionaryEntryIndexKey {
137 pub dictionary: DictionaryId,
138 pub id: u128,
139}
140
141impl DictionaryEntryIndexKey {
142 pub fn new(dictionary: DictionaryId, id: u128) -> Self {
143 Self {
144 dictionary,
145 id,
146 }
147 }
148
149 pub fn encoded(dictionary: impl Into<DictionaryId>, id: u128) -> EncodedKey {
150 Self::new(dictionary.into(), id).encode()
151 }
152
153 pub fn full_scan(dictionary: DictionaryId) -> EncodedKeyRange {
154 EncodedKeyRange::start_end(Some(Self::index_start(dictionary)), Some(Self::index_end(dictionary)))
155 }
156
157 fn index_start(dictionary: DictionaryId) -> EncodedKey {
158 let mut serializer = KeySerializer::with_capacity(9);
159 serializer.extend_u8(Self::KIND as u8).extend_u64(dictionary);
160 serializer.to_encoded_key()
161 }
162
163 fn index_end(dictionary: DictionaryId) -> EncodedKey {
164 let mut serializer = KeySerializer::with_capacity(9);
165 serializer.extend_u8(Self::KIND as u8).extend_u64(*dictionary - 1);
166 serializer.to_encoded_key()
167 }
168}
169
170impl EncodableKey for DictionaryEntryIndexKey {
171 const KIND: KeyKind = KeyKind::DictionaryEntryIndex;
172
173 fn encode(&self) -> EncodedKey {
174 let mut serializer = KeySerializer::with_capacity(25);
175 serializer.extend_u8(Self::KIND as u8).extend_u64(self.dictionary).extend_u128_varint(self.id);
176 serializer.to_encoded_key()
177 }
178
179 fn decode(key: &EncodedKey) -> Option<Self> {
180 let mut de = KeyDeserializer::from_bytes(key.as_slice());
181
182 let kind: KeyKind = de.read_u8().ok()?.try_into().ok()?;
183 if kind != Self::KIND {
184 return None;
185 }
186
187 let dictionary = de.read_u64().ok()?;
188 let id = de.read_u128_varint().ok()?;
189
190 Some(Self {
191 dictionary: DictionaryId(dictionary),
192 id,
193 })
194 }
195}
196
197#[derive(Debug, Clone, PartialEq)]
198pub struct DictionaryEntryIndexKeyRange {
199 pub dictionary: DictionaryId,
200 pub start_id: Option<u128>,
201 pub end_id: Option<u128>,
202}
203
204impl DictionaryEntryIndexKeyRange {
205 pub fn new(dictionary: DictionaryId, start_id: Option<u128>, end_id: Option<u128>) -> Self {
206 Self {
207 dictionary,
208 start_id,
209 end_id,
210 }
211 }
212
213 pub fn full(dictionary: DictionaryId) -> Self {
214 Self {
215 dictionary,
216 start_id: None,
217 end_id: None,
218 }
219 }
220}
221
222impl EncodableKeyRange for DictionaryEntryIndexKeyRange {
223 const KIND: KeyKind = KeyKind::DictionaryEntryIndex;
224
225 fn start(&self) -> Option<EncodedKey> {
226 let mut serializer = KeySerializer::with_capacity(25);
227 serializer.extend_u8(Self::KIND as u8).extend_u64(self.dictionary);
228 if let Some(id) = self.start_id {
229 serializer.extend_u128_varint(id);
230 }
231 Some(serializer.to_encoded_key())
232 }
233
234 fn end(&self) -> Option<EncodedKey> {
235 if let Some(id) = self.end_id {
236 let mut serializer = KeySerializer::with_capacity(25);
237 serializer.extend_u8(Self::KIND as u8).extend_u64(self.dictionary).extend_u128_varint(id - 1);
238 Some(serializer.to_encoded_key())
239 } else {
240 let mut serializer = KeySerializer::with_capacity(9);
241 serializer.extend_u8(Self::KIND as u8).extend_u64(*self.dictionary - 1);
242 Some(serializer.to_encoded_key())
243 }
244 }
245
246 fn decode(_range: &EncodedKeyRange) -> (Option<Self>, Option<Self>) {
247 (None, None)
248 }
249}
250
251#[cfg(test)]
252pub mod tests {
253 use std::ops::Bound;
254
255 use super::*;
256
257 #[test]
258 fn test_dictionary_key_encode_decode() {
259 let key = DictionaryKey {
260 dictionary: DictionaryId(0x1234),
261 };
262 let encoded = key.encode();
263 let decoded = DictionaryKey::decode(&encoded).unwrap();
264 assert_eq!(decoded.dictionary, key.dictionary);
265 }
266
267 #[test]
268 fn test_dictionary_entry_key_encode_decode() {
269 let key = DictionaryEntryKey {
270 dictionary: DictionaryId(42),
271 hash: [
272 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
273 0x0f, 0x10,
274 ],
275 };
276 let encoded = key.encode();
277 let decoded = DictionaryEntryKey::decode(&encoded).unwrap();
278 assert_eq!(decoded.dictionary, key.dictionary);
279 assert_eq!(decoded.hash, key.hash);
280 }
281
282 #[test]
283 fn test_dictionary_entry_index_key_encode_decode() {
284 let key = DictionaryEntryIndexKey {
285 dictionary: DictionaryId(99),
286 id: 12345,
287 };
288 let encoded = key.encode();
289 let decoded = DictionaryEntryIndexKey::decode(&encoded).unwrap();
290 assert_eq!(decoded.dictionary, key.dictionary);
291 assert_eq!(decoded.id, key.id);
292 }
293
294 #[test]
295 fn test_dictionary_key_full_scan() {
296 let range = DictionaryKey::full_scan();
297 assert!(matches!(range.start, Bound::Included(_) | Bound::Excluded(_)));
298 assert!(matches!(range.end, Bound::Included(_) | Bound::Excluded(_)));
299 }
300
301 #[test]
302 fn test_dictionary_entry_key_full_scan() {
303 let range = DictionaryEntryKey::full_scan(DictionaryId(42));
304 assert!(matches!(range.start, Bound::Included(_) | Bound::Excluded(_)));
305 assert!(matches!(range.end, Bound::Included(_) | Bound::Excluded(_)));
306 }
307
308 #[test]
309 fn test_dictionary_entry_index_key_full_scan() {
310 let range = DictionaryEntryIndexKey::full_scan(DictionaryId(42));
311 assert!(matches!(range.start, Bound::Included(_) | Bound::Excluded(_)));
312 assert!(matches!(range.end, Bound::Included(_) | Bound::Excluded(_)));
313 }
314
315 #[test]
316 fn test_dictionary_entry_index_key_range() {
317 let range = DictionaryEntryIndexKeyRange::full(DictionaryId(42));
318 let start = range.start();
319 let end = range.end();
320 assert!(start.is_some());
321 assert!(end.is_some());
322 }
323}