reifydb_core/key/
operator_settings.rs1use serde::{Deserialize, Serialize};
5
6use super::{EncodableKey, KeyKind};
7use crate::{
8 encoded::key::{EncodedKey, EncodedKeyRange},
9 interface::catalog::flow::FlowNodeId,
10 util::encoding::keycode::{deserializer::KeyDeserializer, serializer::KeySerializer},
11};
12
13#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14pub struct OperatorSettingsKey {
15 pub operator: FlowNodeId,
16}
17
18impl OperatorSettingsKey {
19 pub fn encoded(operator: impl Into<FlowNodeId>) -> EncodedKey {
20 Self {
21 operator: operator.into(),
22 }
23 .encode()
24 }
25}
26
27impl EncodableKey for OperatorSettingsKey {
28 const KIND: KeyKind = KeyKind::OperatorSettings;
29
30 fn encode(&self) -> EncodedKey {
31 let mut serializer = KeySerializer::with_capacity(9);
32 serializer.extend_u8(Self::KIND as u8).extend_u64(self.operator);
33 serializer.to_encoded_key()
34 }
35
36 fn decode(key: &EncodedKey) -> Option<Self> {
37 let mut de = KeyDeserializer::from_bytes(key.as_slice());
38
39 let kind: KeyKind = de.read_u8().ok()?.try_into().ok()?;
40 if kind != Self::KIND {
41 return None;
42 }
43
44 Some(Self {
45 operator: FlowNodeId(de.read_u64().ok()?),
46 })
47 }
48}
49
50pub struct OperatorSettingsKeyRange;
51
52impl OperatorSettingsKeyRange {
53 pub fn full_scan() -> EncodedKeyRange {
54 EncodedKeyRange::start_end(Some(Self::start()), Some(Self::end()))
55 }
56
57 fn start() -> EncodedKey {
58 let mut serializer = KeySerializer::with_capacity(1);
59 serializer.extend_u8(OperatorSettingsKey::KIND as u8);
60 serializer.to_encoded_key()
61 }
62
63 fn end() -> EncodedKey {
64 let mut serializer = KeySerializer::with_capacity(1);
65 serializer.extend_u8(OperatorSettingsKey::KIND as u8 - 1);
66 serializer.to_encoded_key()
67 }
68}
69
70#[cfg(test)]
71pub mod tests {
72 use super::*;
73 use crate::{
74 interface::catalog::{id::TableId, shape::ShapeId},
75 key::row_settings::RowSettingsKey,
76 };
77
78 #[test]
79 fn test_operator_settings_key_roundtrip() {
80 let key = OperatorSettingsKey {
81 operator: FlowNodeId(12345),
82 };
83
84 let encoded = key.encode();
85 let decoded = OperatorSettingsKey::decode(&encoded).unwrap();
86 assert_eq!(key, decoded);
87 }
88
89 #[test]
90 fn test_operator_settings_key_rejects_other_kind() {
91 let other = RowSettingsKey::encoded(ShapeId::Table(TableId(1)));
92 assert!(OperatorSettingsKey::decode(&other).is_none());
93 }
94}