mini_lsm/
key.rs

1use std::fmt::Debug;
2
3use bytes::Bytes;
4
5pub const TS_ENABLED: bool = false;
6
7pub struct Key<T: AsRef<[u8]>>(T);
8
9pub type KeySlice<'a> = Key<&'a [u8]>;
10pub type KeyVec = Key<Vec<u8>>;
11pub type KeyBytes = Key<Bytes>;
12
13impl<T: AsRef<[u8]>> Key<T> {
14    pub fn into_inner(self) -> T {
15        self.0
16    }
17
18    pub fn len(&self) -> usize {
19        self.0.as_ref().len()
20    }
21
22    pub fn is_empty(&self) -> bool {
23        self.0.as_ref().is_empty()
24    }
25
26    pub fn for_testing_ts(self) -> u64 {
27        0
28    }
29}
30
31impl Key<Vec<u8>> {
32    pub fn new() -> Self {
33        Self(Vec::new())
34    }
35
36    /// Create a `KeyVec` from a `Vec<u8>`. Will be removed in week 3.
37    pub fn from_vec(key: Vec<u8>) -> Self {
38        Self(key)
39    }
40
41    /// Clears the key and set ts to 0.
42    pub fn clear(&mut self) {
43        self.0.clear()
44    }
45
46    /// Append a slice to the end of the key
47    pub fn append(&mut self, data: &[u8]) {
48        self.0.extend(data)
49    }
50
51    /// Set the key from a slice without re-allocating. The signature will change in week 3.
52    pub fn set_from_slice(&mut self, key_slice: KeySlice) {
53        self.0.clear();
54        self.0.extend(key_slice.0);
55    }
56
57    pub fn as_key_slice(&self) -> KeySlice {
58        Key(self.0.as_slice())
59    }
60
61    pub fn into_key_bytes(self) -> KeyBytes {
62        Key(self.0.into())
63    }
64
65    /// Always use `raw_ref` to access the key in week 1 + 2. This function will be removed in week 3.
66    pub fn raw_ref(&self) -> &[u8] {
67        self.0.as_ref()
68    }
69
70    pub fn for_testing_key_ref(&self) -> &[u8] {
71        self.0.as_ref()
72    }
73
74    pub fn for_testing_from_vec_no_ts(key: Vec<u8>) -> Self {
75        Self(key)
76    }
77}
78
79impl Key<Bytes> {
80    pub fn as_key_slice(&self) -> KeySlice {
81        Key(&self.0)
82    }
83
84    /// Create a `KeyBytes` from a `Bytes`. Will be removed in week 3.
85    pub fn from_bytes(bytes: Bytes) -> KeyBytes {
86        Key(bytes)
87    }
88
89    /// Always use `raw_ref` to access the key in week 1 + 2. This function will be removed in week 3.
90    pub fn raw_ref(&self) -> &[u8] {
91        self.0.as_ref()
92    }
93
94    pub fn for_testing_from_bytes_no_ts(bytes: Bytes) -> KeyBytes {
95        Key(bytes)
96    }
97
98    pub fn for_testing_key_ref(&self) -> &[u8] {
99        self.0.as_ref()
100    }
101}
102
103impl<'a> Key<&'a [u8]> {
104    pub fn to_key_vec(self) -> KeyVec {
105        Key(self.0.to_vec())
106    }
107
108    /// Create a key slice from a slice. Will be removed in week 3.
109    pub fn from_slice(slice: &'a [u8]) -> Self {
110        Self(slice)
111    }
112
113    /// Always use `raw_ref` to access the key in week 1 + 2. This function will be removed in week 3.
114    pub fn raw_ref(self) -> &'a [u8] {
115        self.0
116    }
117
118    pub fn for_testing_key_ref(self) -> &'a [u8] {
119        self.0
120    }
121
122    pub fn for_testing_from_slice_no_ts(slice: &'a [u8]) -> Self {
123        Self(slice)
124    }
125
126    pub fn for_testing_from_slice_with_ts(slice: &'a [u8], _ts: u64) -> Self {
127        Self(slice)
128    }
129}
130
131impl<T: AsRef<[u8]> + Debug> Debug for Key<T> {
132    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
133        self.0.fmt(f)
134    }
135}
136
137impl<T: AsRef<[u8]> + Default> Default for Key<T> {
138    fn default() -> Self {
139        Self(T::default())
140    }
141}
142
143impl<T: AsRef<[u8]> + PartialEq> PartialEq for Key<T> {
144    fn eq(&self, other: &Self) -> bool {
145        self.0.eq(&other.0)
146    }
147}
148
149impl<T: AsRef<[u8]> + Eq> Eq for Key<T> {}
150
151impl<T: AsRef<[u8]> + Clone> Clone for Key<T> {
152    fn clone(&self) -> Self {
153        Self(self.0.clone())
154    }
155}
156
157impl<T: AsRef<[u8]> + Copy> Copy for Key<T> {}
158
159impl<T: AsRef<[u8]> + PartialOrd> PartialOrd for Key<T> {
160    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
161        self.0.partial_cmp(&other.0)
162    }
163}
164
165impl<T: AsRef<[u8]> + Ord> Ord for Key<T> {
166    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
167        self.0.cmp(&other.0)
168    }
169}