Skip to main content

reifydb_core/key/
config.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use std::{borrow::Cow, str::FromStr};
5
6use reifydb_codec::key::{deserializer::KeyDeserializer, encoded::EncodedKey, serializer::KeySerializer};
7use smallvec::{SmallVec, smallvec};
8
9use super::KeyTag;
10use crate::{
11	interface::catalog::config::ConfigKey,
12	key::{
13		any::{ByteEncoding, Field, KeyFields},
14		bound::TaggedKeyBoundRange,
15	},
16};
17
18#[derive(Debug, Clone, PartialEq, Hash)]
19pub struct ConfigStorageKey {
20	pub key: ConfigKey,
21}
22
23impl ConfigStorageKey {
24	pub fn new(key: ConfigKey) -> Self {
25		Self {
26			key,
27		}
28	}
29
30	pub fn for_key(key: ConfigKey) -> EncodedKey {
31		Self::new(key).encode()
32	}
33
34	pub fn full_scan() -> TaggedKeyBoundRange {
35		TaggedKeyBoundRange::kind(Self::TAG)
36	}
37}
38
39impl ConfigStorageKey {
40	pub const TAG: KeyTag = KeyTag::ConfigStorage;
41
42	pub fn encode(&self) -> EncodedKey {
43		let mut serializer = KeySerializer::with_capacity(31);
44		serializer.extend_u8(Self::TAG as u8).extend_str(self.key.to_string());
45		serializer.to_encoded_key()
46	}
47
48	pub fn decode(key: &EncodedKey) -> Option<Self> {
49		let mut de = KeyDeserializer::from_bytes(key.as_slice());
50
51		let kind: KeyTag = de.read_u8().ok()?.try_into().ok()?;
52		if kind != Self::TAG {
53			return None;
54		}
55
56		let config_key_str = de.read_str().ok()?;
57		let key = ConfigKey::from_str(&config_key_str).ok()?;
58
59		Some(Self {
60			key,
61		})
62	}
63}
64
65impl KeyFields for ConfigStorageKey {
66	fn fields(&self) -> SmallVec<[Field<'_>; 6]> {
67		smallvec![Field::BytesDesc(ByteEncoding::Escaped, Cow::Owned(self.key.to_string().into_bytes()))]
68	}
69}