reifydb_core/key/
policy.rs1use reifydb_codec::key::{
5 deserializer::KeyDeserializer,
6 encoded::{EncodedKey, EncodedKeyRange},
7 serializer::KeySerializer,
8};
9
10use super::{EncodableKey, KeyKind};
11use crate::interface::catalog::policy::PolicyId;
12
13#[derive(Debug, Clone, PartialEq)]
14pub struct PolicyKey {
15 pub policy: PolicyId,
16}
17
18impl PolicyKey {
19 pub fn new(policy: PolicyId) -> Self {
20 Self {
21 policy,
22 }
23 }
24
25 pub fn encoded(policy: PolicyId) -> EncodedKey {
26 Self::new(policy).encode()
27 }
28
29 pub fn full_scan() -> EncodedKeyRange {
30 let mut start = KeySerializer::with_capacity(1);
31 start.extend_u8(Self::KIND as u8);
32 let mut end = KeySerializer::with_capacity(1);
33 end.extend_u8(Self::KIND as u8 - 1);
34 EncodedKeyRange::start_end(Some(start.to_encoded_key()), Some(end.to_encoded_key()))
35 }
36}
37
38impl EncodableKey for PolicyKey {
39 const KIND: KeyKind = KeyKind::Policy;
40
41 fn encode(&self) -> EncodedKey {
42 let mut serializer = KeySerializer::with_capacity(9);
43 serializer.extend_u8(Self::KIND as u8).extend_u64(self.policy);
44 serializer.to_encoded_key()
45 }
46
47 fn decode(key: &EncodedKey) -> Option<Self> {
48 let mut de = KeyDeserializer::from_bytes(key.as_slice());
49 let kind: KeyKind = de.read_u8().ok()?.try_into().ok()?;
50 if kind != Self::KIND {
51 return None;
52 }
53 let policy = de.read_u64().ok()?;
54 Some(Self {
55 policy,
56 })
57 }
58}