reifydb_core/key/
partition.rs1use reifydb_codec::key::encoded::EncodedKey;
5use reifydb_macro::KeyCodec;
6use reifydb_value::value::partition::Partition;
7
8use super::KeyTag;
9use crate::{
10 interface::catalog::object::ObjectId,
11 key::{
12 any::{Field, KeyFields, Width},
13 bound::{TaggedKeyBound, TaggedKeyBoundRange, object_fields},
14 catalog::{KeyDeserializerCatalogExt, KeySerializerCatalogExt},
15 },
16};
17
18#[derive(Debug, Clone, PartialEq, KeyCodec, Hash)]
19#[key(tag = Partition)]
20pub struct PartitionKey {
21 pub object: ObjectId,
22 pub partition: Partition,
23}
24
25impl PartitionKey {
26 pub fn new(object: impl Into<ObjectId>, partition: Partition) -> Self {
27 Self {
28 object: object.into(),
29 partition,
30 }
31 }
32
33 pub fn encoded(object: impl Into<ObjectId>, partition: Partition) -> EncodedKey {
34 Self::new(object, partition).encode()
35 }
36
37 pub fn full_scan(object: impl Into<ObjectId>) -> TaggedKeyBoundRange {
38 let object = object.into();
39 TaggedKeyBoundRange::start_end(
40 TaggedKeyBound::prefix(Self::TAG, object_fields(object)),
41 TaggedKeyBound::prefix(Self::TAG, object_fields(object.prev())),
42 )
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use std::ops::RangeBounds;
49
50 use reifydb_value::value::{Value, partition::Partition};
51
52 use super::PartitionKey;
53 use crate::interface::catalog::{id::TableId, object::ObjectId};
54
55 #[test]
56 fn test_roundtrip() {
57 let key = PartitionKey {
58 object: ObjectId::Table(TableId(7)),
59 partition: Partition::of(&[Value::Utf8("us".to_string())]),
60 };
61 let decoded = PartitionKey::decode(&key.encode()).unwrap();
62 assert_eq!(decoded, key);
63 }
64
65 #[test]
66 fn test_partitions_of_object_share_prefix() {
67 let object = ObjectId::Table(TableId(3));
68 let range = PartitionKey::full_scan(object).encode();
69 let k = PartitionKey::encoded(object, Partition::of(&[Value::Utf8("us".to_string())]));
70 assert!(range.contains(&k));
71 let other = PartitionKey::encoded(
72 ObjectId::Table(TableId(4)),
73 Partition::of(&[Value::Utf8("us".to_string())]),
74 );
75 assert!(!range.contains(&other));
76 }
77}