fjall/batch/
item.rs

1// Copyright (c) 2024-present, fjall-rs
2// This source code is licensed under both the Apache 2.0 and MIT License
3// (found in the LICENSE-* files in the repository)
4
5use super::PartitionKey;
6use lsm_tree::{UserKey, UserValue, ValueType};
7
8///
9/// Inner Item
10///
11pub struct InnerItem {
12    /// User-defined key - an arbitrary byte array
13    ///
14    /// Supports up to 2^16 bytes
15    pub key: UserKey,
16
17    /// User-defined value - an arbitrary byte array
18    ///
19    /// Supports up to 65535 bytes
20    pub value: UserValue,
21
22    /// Tombstone marker - if this is true, the value has been deleted
23    pub value_type: ValueType,
24}
25
26unsafe impl Sync for InnerItem {}
27unsafe impl Send for InnerItem {}
28
29/// Item
30#[derive(Clone, PartialEq, Eq)]
31pub struct Item {
32    /// Partition key - an arbitrary byte array
33    ///
34    /// Supports up to 2^8 bytes
35    pub partition: PartitionKey,
36
37    /// User-defined key - an arbitrary byte array
38    ///
39    /// Supports up to 2^16 bytes
40    pub key: UserKey,
41
42    /// User-defined value - an arbitrary byte array
43    ///
44    /// Supports up to 65535 bytes
45    pub value: UserValue,
46
47    /// Tombstone marker - if this is true, the value has been deleted
48    pub value_type: ValueType,
49}
50
51impl std::fmt::Debug for Item {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        write!(
54            f,
55            "{}:{:?}:{} => {:?}",
56            self.partition,
57            self.key,
58            match self.value_type {
59                ValueType::Value => "V",
60                ValueType::Tombstone => "T",
61                ValueType::WeakTombstone => "W",
62            },
63            self.value
64        )
65    }
66}
67
68impl Item {
69    ///
70    /// New item
71    ///
72    pub fn new<P: Into<PartitionKey>, K: Into<UserKey>, V: Into<UserValue>>(
73        partition: P,
74        key: K,
75        value: V,
76        value_type: ValueType,
77    ) -> Self {
78        let p = partition.into();
79        let k = key.into();
80        let v = value.into();
81
82        assert!(!p.is_empty());
83        assert!(!k.is_empty());
84
85        assert!(u8::try_from(p.len()).is_ok(), "Partition name too long");
86        assert!(
87            u16::try_from(k.len()).is_ok(),
88            "Keys can be up to 65535 bytes long"
89        );
90        assert!(
91            u32::try_from(v.len()).is_ok(),
92            "Values can be up to 2^32 bytes long"
93        );
94
95        Self {
96            partition: p,
97            key: k,
98            value: v,
99            value_type,
100        }
101    }
102}