reifydb_core/key/
config.rs1use std::str::FromStr;
5
6use reifydb_codec::key::{
7 deserializer::KeyDeserializer,
8 encoded::{EncodedKey, EncodedKeyRange},
9 serializer::KeySerializer,
10};
11
12use super::{EncodableKey, KeyKind};
13use crate::interface::catalog::config::ConfigKey;
14
15#[derive(Debug, Clone, PartialEq)]
16pub struct ConfigStorageKey {
17 pub key: ConfigKey,
18}
19
20impl ConfigStorageKey {
21 pub fn new(key: ConfigKey) -> Self {
22 Self {
23 key,
24 }
25 }
26
27 pub fn for_key(key: ConfigKey) -> EncodedKey {
28 Self::new(key).encode()
29 }
30
31 pub fn full_scan() -> EncodedKeyRange {
32 let mut start = KeySerializer::with_capacity(1);
33 start.extend_u8(Self::KIND as u8);
34 let mut end = KeySerializer::with_capacity(1);
35 end.extend_u8(Self::KIND as u8 - 1);
36 EncodedKeyRange::start_end(Some(start.to_encoded_key()), Some(end.to_encoded_key()))
37 }
38}
39
40impl EncodableKey for ConfigStorageKey {
41 const KIND: KeyKind = KeyKind::ConfigStorage;
42
43 fn encode(&self) -> EncodedKey {
44 let mut serializer = KeySerializer::with_capacity(31);
45 serializer.extend_u8(Self::KIND as u8).extend_str(self.key.to_string());
46 serializer.to_encoded_key()
47 }
48
49 fn decode(key: &EncodedKey) -> Option<Self> {
50 let mut de = KeyDeserializer::from_bytes(key.as_slice());
51
52 let kind: KeyKind = de.read_u8().ok()?.try_into().ok()?;
53 if kind != Self::KIND {
54 return None;
55 }
56
57 let config_key_str = de.read_str().ok()?;
58 let key = ConfigKey::from_str(&config_key_str)
59 .expect("failed to decode ConfigKey from storage, unknown key");
60
61 Some(Self {
62 key,
63 })
64 }
65}