Skip to main content

reifydb_core/key/
column.rs

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