reifydb_core/key/
namespace_dictionary.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 super::{EncodableKey, KeyKind};
5use crate::{
6	EncodedKey, EncodedKeyRange,
7	interface::{DictionaryId, NamespaceId},
8	util::encoding::keycode::{KeyDeserializer, KeySerializer},
9};
10
11const VERSION: u8 = 1;
12
13#[derive(Debug, Clone, PartialEq)]
14pub struct NamespaceDictionaryKey {
15	pub namespace: NamespaceId,
16	pub dictionary: DictionaryId,
17}
18
19impl NamespaceDictionaryKey {
20	pub fn new(namespace: NamespaceId, dictionary: DictionaryId) -> Self {
21		Self {
22			namespace,
23			dictionary,
24		}
25	}
26
27	pub fn encoded(namespace: impl Into<NamespaceId>, dictionary: impl Into<DictionaryId>) -> EncodedKey {
28		Self::new(namespace.into(), dictionary.into()).encode()
29	}
30
31	pub fn full_scan(namespace: NamespaceId) -> EncodedKeyRange {
32		EncodedKeyRange::start_end(Some(Self::link_start(namespace)), Some(Self::link_end(namespace)))
33	}
34
35	fn link_start(namespace: NamespaceId) -> EncodedKey {
36		let mut serializer = KeySerializer::with_capacity(10);
37		serializer.extend_u8(VERSION).extend_u8(Self::KIND as u8).extend_u64(namespace);
38		serializer.to_encoded_key()
39	}
40
41	fn link_end(namespace: NamespaceId) -> EncodedKey {
42		let mut serializer = KeySerializer::with_capacity(10);
43		serializer.extend_u8(VERSION).extend_u8(Self::KIND as u8).extend_u64(*namespace - 1);
44		serializer.to_encoded_key()
45	}
46}
47
48impl EncodableKey for NamespaceDictionaryKey {
49	const KIND: KeyKind = KeyKind::NamespaceDictionary;
50
51	fn encode(&self) -> EncodedKey {
52		let mut serializer = KeySerializer::with_capacity(18);
53		serializer
54			.extend_u8(VERSION)
55			.extend_u8(Self::KIND as u8)
56			.extend_u64(self.namespace)
57			.extend_u64(self.dictionary);
58		serializer.to_encoded_key()
59	}
60
61	fn decode(key: &EncodedKey) -> Option<Self> {
62		let mut de = KeyDeserializer::from_bytes(key.as_slice());
63
64		let version = de.read_u8().ok()?;
65		if version != VERSION {
66			return None;
67		}
68
69		let kind: KeyKind = de.read_u8().ok()?.try_into().ok()?;
70		if kind != Self::KIND {
71			return None;
72		}
73
74		let namespace = de.read_u64().ok()?;
75		let dictionary = de.read_u64().ok()?;
76
77		Some(Self {
78			namespace: NamespaceId(namespace),
79			dictionary: DictionaryId(dictionary),
80		})
81	}
82}
83
84#[cfg(test)]
85mod tests {
86	use super::*;
87
88	#[test]
89	fn test_namespace_dictionary_key_encode_decode() {
90		let key = NamespaceDictionaryKey {
91			namespace: NamespaceId(1025),
92			dictionary: DictionaryId(2048),
93		};
94		let encoded = key.encode();
95		let decoded = NamespaceDictionaryKey::decode(&encoded).unwrap();
96		assert_eq!(decoded.namespace, key.namespace);
97		assert_eq!(decoded.dictionary, key.dictionary);
98	}
99
100	#[test]
101	fn test_namespace_dictionary_key_full_scan() {
102		use std::ops::Bound;
103		let range = NamespaceDictionaryKey::full_scan(NamespaceId(1025));
104		assert!(matches!(range.start, Bound::Included(_) | Bound::Excluded(_)));
105		assert!(matches!(range.end, Bound::Included(_) | Bound::Excluded(_)));
106	}
107}