Skip to main content

reifydb_core/key/
identity_attribute.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use reifydb_codec::key::{
5	deserializer::KeyDeserializer,
6	encoded::{EncodedKey, EncodedKeyRange},
7	serializer::KeySerializer,
8};
9
10use super::{EncodableKey, KeyKind};
11use crate::interface::catalog::identity::IdentityAttributeId;
12
13#[derive(Debug, Clone, PartialEq)]
14pub struct IdentityAttributeKey {
15	pub attribute: IdentityAttributeId,
16}
17
18impl IdentityAttributeKey {
19	pub fn new(attribute: IdentityAttributeId) -> Self {
20		Self {
21			attribute,
22		}
23	}
24
25	pub fn encoded(attribute: IdentityAttributeId) -> EncodedKey {
26		Self::new(attribute).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 IdentityAttributeKey {
39	const KIND: KeyKind = KeyKind::IdentityAttribute;
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.attribute);
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 attribute = de.read_u64().ok()?;
54		Some(Self {
55			attribute,
56		})
57	}
58}