Skip to main content

reifydb_core/key/
namespace_dictionary.rs

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