Skip to main content

reifydb_core/key/
identity_attribute_value.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};
9use reifydb_value::value::identity::IdentityId;
10
11use super::{EncodableKey, KeyKind};
12use crate::interface::catalog::identity::IdentityAttributeId;
13
14#[derive(Debug, Clone, PartialEq)]
15pub struct IdentityAttributeValueKey {
16	pub identity: IdentityId,
17	pub attribute: IdentityAttributeId,
18}
19
20impl IdentityAttributeValueKey {
21	pub fn new(identity: IdentityId, attribute: IdentityAttributeId) -> Self {
22		Self {
23			identity,
24			attribute,
25		}
26	}
27
28	pub fn encoded(identity: IdentityId, attribute: IdentityAttributeId) -> EncodedKey {
29		Self::new(identity, attribute).encode()
30	}
31
32	pub fn full_scan() -> EncodedKeyRange {
33		let mut start = KeySerializer::with_capacity(1);
34		start.extend_u8(Self::KIND as u8);
35		let mut end = KeySerializer::with_capacity(1);
36		end.extend_u8(Self::KIND as u8 - 1);
37		EncodedKeyRange::start_end(Some(start.to_encoded_key()), Some(end.to_encoded_key()))
38	}
39
40	pub fn identity_scan(identity: IdentityId) -> EncodedKeyRange {
41		let mut prefix = KeySerializer::with_capacity(17);
42		prefix.extend_u8(Self::KIND as u8).extend_identity_id(&identity);
43		EncodedKeyRange::prefix(prefix.to_encoded_key().as_slice())
44	}
45}
46
47impl EncodableKey for IdentityAttributeValueKey {
48	const KIND: KeyKind = KeyKind::IdentityAttributeValue;
49
50	fn encode(&self) -> EncodedKey {
51		let mut serializer = KeySerializer::with_capacity(25);
52		serializer.extend_u8(Self::KIND as u8).extend_identity_id(&self.identity).extend_u64(self.attribute);
53		serializer.to_encoded_key()
54	}
55
56	fn decode(key: &EncodedKey) -> Option<Self> {
57		let mut de = KeyDeserializer::from_bytes(key.as_slice());
58		let kind: KeyKind = de.read_u8().ok()?.try_into().ok()?;
59		if kind != Self::KIND {
60			return None;
61		}
62		let identity = de.read_identity_id().ok()?;
63		let attribute = de.read_u64().ok()?;
64		Some(Self {
65			identity,
66			attribute,
67		})
68	}
69}
70
71#[cfg(test)]
72mod tests {
73	use std::ops::RangeBounds;
74
75	use reifydb_value::value::{identity::IdentityId, uuid::Uuid7};
76	use uuid::Uuid;
77
78	use super::*;
79
80	fn identity(byte: u8) -> IdentityId {
81		IdentityId::from(Uuid7::from(Uuid::from_bytes([byte; 16])))
82	}
83
84	#[test]
85	fn identity_scan_holds_every_attribute_of_that_identity() {
86		// Small ids complement to a suffix starting 0xFF, so a prefix+0xFF end bound excludes them all.
87		let alice = identity(1);
88		let range = IdentityAttributeValueKey::identity_scan(alice);
89
90		for attribute in [0u64, 1, 2, u64::MAX] {
91			let key = IdentityAttributeValueKey::encoded(alice, attribute);
92			assert!(range.contains(&key), "attribute {attribute} must fall inside the identity scan");
93		}
94	}
95
96	#[test]
97	fn identity_scan_excludes_a_neighbouring_identity() {
98		// The scan must not widen into the next identity when the bound is carry-incremented.
99		let range = IdentityAttributeValueKey::identity_scan(identity(1));
100		let other = IdentityAttributeValueKey::encoded(identity(2), 1);
101
102		assert!(!range.contains(&other), "a different identity must stay outside the scan");
103	}
104}