Skip to main content

reifydb_core/key/
dictionary.rs

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