Skip to main content

reifydb_core/key/
operator_settings.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use reifydb_codec::key::encoded::EncodedKey;
5use reifydb_macro::KeyCodec;
6use serde::{Deserialize, Serialize};
7
8use super::KeyTag;
9use crate::{
10	interface::catalog::flow::OperatorId,
11	key::{
12		any::{Field, KeyFields, Width},
13		bound::TaggedKeyBoundRange,
14	},
15};
16
17#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, KeyCodec, Hash)]
18#[key(tag = OperatorSettings)]
19pub struct OperatorSettingsKey {
20	pub operator: OperatorId,
21}
22
23impl OperatorSettingsKey {
24	pub fn new(operator: impl Into<OperatorId>) -> Self {
25		Self {
26			operator: operator.into(),
27		}
28	}
29
30	pub fn encoded(operator: impl Into<OperatorId>) -> EncodedKey {
31		Self::new(operator).encode()
32	}
33
34	pub fn full_scan() -> TaggedKeyBoundRange {
35		TaggedKeyBoundRange::kind(Self::TAG)
36	}
37}
38
39#[cfg(test)]
40pub mod tests {
41	use super::*;
42	use crate::{
43		interface::catalog::{id::TableId, storage::StorageId},
44		key::row::RowSettingsKey,
45	};
46
47	#[test]
48	fn test_operator_settings_key_roundtrip() {
49		let key = OperatorSettingsKey {
50			operator: OperatorId(12345),
51		};
52
53		let encoded = key.encode();
54		let decoded = OperatorSettingsKey::decode(&encoded).unwrap();
55		assert_eq!(key, decoded);
56	}
57
58	#[test]
59	fn test_operator_settings_key_rejects_other_kind() {
60		let other = RowSettingsKey::encoded(StorageId::Table(TableId(1)));
61		assert!(OperatorSettingsKey::decode(&other).is_none());
62	}
63
64	#[test]
65	fn test_order_preserving() {
66		let key1 = OperatorSettingsKey {
67			operator: OperatorId(1),
68		};
69		let key2 = OperatorSettingsKey {
70			operator: OperatorId(2),
71		};
72
73		let encoded1 = key1.encode();
74		let encoded2 = key2.encode();
75
76		assert!(encoded2 < encoded1, "ordering not preserved");
77	}
78}
79
80#[cfg(test)]
81mod verify_byte_identical {
82	use reifydb_codec::key::serializer::KeySerializer;
83
84	use super::OperatorSettingsKey;
85	use crate::interface::catalog::flow::OperatorId;
86
87	fn legacy_encode(key: &OperatorSettingsKey) -> Vec<u8> {
88		let mut serializer = KeySerializer::with_capacity(9);
89		serializer.extend_u8(OperatorSettingsKey::TAG as u8).extend_u64(key.operator);
90		serializer.to_encoded_key().as_slice().to_vec()
91	}
92
93	#[test]
94	fn matches_legacy_byte_layout() {
95		for operator in [0u64, 1, 42, 12345, u64::MAX] {
96			let key = OperatorSettingsKey {
97				operator: OperatorId(operator),
98			};
99			assert_eq!(legacy_encode(&key), key.encode().as_slice().to_vec(), "operator={operator:#x}");
100		}
101	}
102}