Skip to main content

reifydb_core/key/
column.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 crate::{
11	interface::catalog::{id::ColumnId, shape::ShapeId},
12	key::{
13		EncodableKey, KeyKind,
14		catalog::{KeyDeserializerCatalogExt, KeySerializerCatalogExt},
15	},
16};
17
18#[derive(Debug, Clone, PartialEq)]
19pub struct ColumnKey {
20	pub shape: ShapeId,
21	pub column: ColumnId,
22}
23
24impl EncodableKey for ColumnKey {
25	const KIND: KeyKind = KeyKind::Column;
26
27	fn encode(&self) -> EncodedKey {
28		let mut serializer = KeySerializer::with_capacity(18);
29		serializer.extend_u8(Self::KIND as u8).extend_shape_id(self.shape).extend_u64(self.column);
30		serializer.to_encoded_key()
31	}
32
33	fn decode(key: &EncodedKey) -> Option<Self> {
34		let mut de = KeyDeserializer::from_bytes(key.as_slice());
35
36		let kind: KeyKind = de.read_u8().ok()?.try_into().ok()?;
37		if kind != Self::KIND {
38			return None;
39		}
40
41		let shape = de.read_shape_id().ok()?;
42		let column = de.read_u64().ok()?;
43
44		Some(Self {
45			shape,
46			column: ColumnId(column),
47		})
48	}
49}
50
51impl ColumnKey {
52	pub fn encoded(shape: impl Into<ShapeId>, column: impl Into<ColumnId>) -> EncodedKey {
53		Self {
54			shape: shape.into(),
55			column: column.into(),
56		}
57		.encode()
58	}
59
60	pub fn full_scan(shape: impl Into<ShapeId>) -> EncodedKeyRange {
61		let shape = shape.into();
62		EncodedKeyRange::start_end(Some(Self::start(shape)), Some(Self::end(shape)))
63	}
64
65	fn start(shape: ShapeId) -> EncodedKey {
66		let mut serializer = KeySerializer::with_capacity(10);
67		serializer.extend_u8(Self::KIND as u8).extend_shape_id(shape);
68		serializer.to_encoded_key()
69	}
70
71	fn end(shape: ShapeId) -> EncodedKey {
72		let mut serializer = KeySerializer::with_capacity(10);
73		serializer.extend_u8(Self::KIND as u8).extend_shape_id(shape.prev());
74		serializer.to_encoded_key()
75	}
76}
77
78#[cfg(test)]
79pub mod tests {
80	use super::EncodableKey;
81	use crate::{
82		interface::catalog::{id::ColumnId, shape::ShapeId},
83		key::ColumnKey,
84	};
85
86	#[test]
87	fn test_encode_decode() {
88		let key = ColumnKey {
89			shape: ShapeId::table(0xABCD),
90			column: ColumnId(0x123456789ABCDEF0),
91		};
92		let encoded = key.encode();
93
94		let expected: Vec<u8> =
95			vec![0xF8, 0x01, 0x3F, 0x54, 0x32, 0x00, 0xED, 0xCB, 0xA9, 0x87, 0x65, 0x43, 0x21, 0x0F];
96
97		assert_eq!(encoded.as_slice(), expected);
98
99		let key = ColumnKey::decode(&encoded).unwrap();
100		assert_eq!(key.shape, 0xABCD);
101		assert_eq!(key.column, 0x123456789ABCDEF0);
102	}
103
104	#[test]
105	fn test_order_preserving() {
106		let key1 = ColumnKey {
107			shape: ShapeId::table(1),
108			column: ColumnId(100),
109		};
110		let key2 = ColumnKey {
111			shape: ShapeId::table(1),
112			column: ColumnId(200),
113		};
114		let key3 = ColumnKey {
115			shape: ShapeId::table(2),
116			column: ColumnId(0),
117		};
118
119		let encoded1 = key1.encode();
120		let encoded2 = key2.encode();
121		let encoded3 = key3.encode();
122
123		assert!(encoded3 < encoded2, "ordering not preserved");
124		assert!(encoded2 < encoded1, "ordering not preserved");
125	}
126}