reifydb_core/key/
binding.rs1use reifydb_codec::key::{
5 deserializer::KeyDeserializer,
6 encoded::{EncodedKey, EncodedKeyRange},
7 serializer::KeySerializer,
8};
9
10use super::{EncodableKey, KeyKind};
11use crate::interface::catalog::id::BindingId;
12
13#[derive(Debug, Clone, PartialEq)]
14pub struct BindingKey {
15 pub binding: BindingId,
16}
17
18impl EncodableKey for BindingKey {
19 const KIND: KeyKind = KeyKind::Binding;
20
21 fn encode(&self) -> EncodedKey {
22 let mut serializer = KeySerializer::with_capacity(9);
23 serializer.extend_u8(Self::KIND as u8).extend_u64(self.binding);
24 serializer.to_encoded_key()
25 }
26
27 fn decode(key: &EncodedKey) -> Option<Self> {
28 let mut de = KeyDeserializer::from_bytes(key.as_slice());
29
30 let kind: KeyKind = de.read_u8().ok()?.try_into().ok()?;
31 if kind != Self::KIND {
32 return None;
33 }
34
35 let binding = de.read_u64().ok()?;
36
37 Some(Self {
38 binding: BindingId(binding),
39 })
40 }
41}
42
43impl BindingKey {
44 pub fn encoded(binding: impl Into<BindingId>) -> EncodedKey {
45 Self {
46 binding: binding.into(),
47 }
48 .encode()
49 }
50
51 pub fn full_scan() -> EncodedKeyRange {
52 EncodedKeyRange::start_end(Some(Self::start()), Some(Self::end()))
53 }
54
55 fn start() -> EncodedKey {
56 let mut serializer = KeySerializer::with_capacity(1);
57 serializer.extend_u8(Self::KIND as u8);
58 serializer.to_encoded_key()
59 }
60
61 fn end() -> EncodedKey {
62 let mut serializer = KeySerializer::with_capacity(1);
63 serializer.extend_u8(Self::KIND as u8 - 1);
64 serializer.to_encoded_key()
65 }
66}
67
68#[cfg(test)]
69pub mod tests {
70 use super::{BindingKey, EncodableKey};
71 use crate::interface::catalog::id::BindingId;
72
73 #[test]
74 fn test_encode_decode() {
75 let key = BindingKey {
76 binding: BindingId(0xABCD),
77 };
78 let encoded = key.encode();
79 let decoded = BindingKey::decode(&encoded).unwrap();
80 assert_eq!(decoded.binding, 0xABCD);
81 }
82}