kimberlite_store/
types.rs1use std::fmt::{self, Debug, Display};
4
5use bytes::Bytes;
6
7pub const PAGE_SIZE: usize = 4096;
19
20pub const MAX_KEY_LENGTH: usize = 1024;
25
26#[allow(dead_code)]
31pub const MAX_VALUE_LENGTH: usize = PAGE_SIZE - 128; pub const PAGE_HEADER_SIZE: usize = 32;
35
36pub const CRC_SIZE: usize = 4;
38
39pub const BTREE_MIN_KEYS: usize = 4;
41
42#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
51pub struct PageId(u64);
52
53impl PageId {
54 pub const SUPERBLOCK: PageId = PageId(0);
56
57 pub fn new(id: u64) -> Self {
59 Self(id)
60 }
61
62 pub fn as_u64(self) -> u64 {
64 self.0
65 }
66
67 pub fn byte_offset(self) -> u64 {
69 self.0 * PAGE_SIZE as u64
70 }
71
72 pub fn next(self) -> Self {
74 Self(self.0 + 1)
75 }
76}
77
78impl Debug for PageId {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 write!(f, "PageId({})", self.0)
81 }
82}
83
84impl Display for PageId {
85 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86 write!(f, "{}", self.0)
87 }
88}
89
90impl From<u64> for PageId {
91 fn from(id: u64) -> Self {
92 Self(id)
93 }
94}
95
96impl From<PageId> for u64 {
97 fn from(id: PageId) -> Self {
98 id.0
99 }
100}
101
102#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
111pub struct TableId(u64);
112
113impl TableId {
114 pub fn new(id: u64) -> Self {
116 Self(id)
117 }
118
119 pub fn as_u64(self) -> u64 {
121 self.0
122 }
123}
124
125impl Debug for TableId {
126 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127 write!(f, "TableId({})", self.0)
128 }
129}
130
131impl Display for TableId {
132 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133 write!(f, "{}", self.0)
134 }
135}
136
137impl From<u64> for TableId {
138 fn from(id: u64) -> Self {
139 Self(id)
140 }
141}
142
143impl From<TableId> for u64 {
144 fn from(id: TableId) -> Self {
145 id.0
146 }
147}
148
149#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
158pub struct Key(Bytes);
159
160impl Key {
161 pub fn new(data: impl Into<Bytes>) -> Self {
167 let bytes = data.into();
168 debug_assert!(
169 bytes.len() <= MAX_KEY_LENGTH,
170 "key length {} exceeds maximum {}",
171 bytes.len(),
172 MAX_KEY_LENGTH
173 );
174 Self(bytes)
175 }
176
177 pub fn from_static(data: &'static [u8]) -> Self {
179 Self::new(Bytes::from_static(data))
180 }
181
182 pub fn as_bytes(&self) -> &[u8] {
184 &self.0
185 }
186
187 pub fn len(&self) -> usize {
189 self.0.len()
190 }
191
192 pub fn is_empty(&self) -> bool {
194 self.0.is_empty()
195 }
196
197 pub fn into_bytes(self) -> Bytes {
199 self.0
200 }
201
202 pub fn min() -> Self {
204 Self(Bytes::new())
205 }
206
207 pub fn max() -> Self {
209 Self(Bytes::from(vec![0xFF; MAX_KEY_LENGTH]))
210 }
211}
212
213impl Debug for Key {
214 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215 write!(f, "Key(")?;
217 for (i, byte) in self.0.iter().take(16).enumerate() {
218 if i > 0 {
219 write!(f, " ")?;
220 }
221 write!(f, "{byte:02x}")?;
222 }
223 if self.0.len() > 16 {
224 write!(f, "...+{} more", self.0.len() - 16)?;
225 }
226 write!(f, ")")
227 }
228}
229
230impl Display for Key {
231 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
232 if let Ok(s) = std::str::from_utf8(&self.0) {
234 if s.chars().all(|c| c.is_ascii_graphic() || c == ' ') {
235 return write!(f, "{s}");
236 }
237 }
238 for byte in &self.0 {
240 write!(f, "{byte:02x}")?;
241 }
242 Ok(())
243 }
244}
245
246impl From<&[u8]> for Key {
247 fn from(data: &[u8]) -> Self {
248 Self::new(Bytes::copy_from_slice(data))
249 }
250}
251
252impl From<Vec<u8>> for Key {
253 fn from(data: Vec<u8>) -> Self {
254 Self::new(Bytes::from(data))
255 }
256}
257
258impl From<Bytes> for Key {
259 fn from(data: Bytes) -> Self {
260 Self::new(data)
261 }
262}
263
264impl From<&str> for Key {
265 fn from(s: &str) -> Self {
266 Self::new(Bytes::copy_from_slice(s.as_bytes()))
267 }
268}
269
270impl From<String> for Key {
271 fn from(s: String) -> Self {
272 Self::new(Bytes::from(s))
273 }
274}
275
276impl AsRef<[u8]> for Key {
277 fn as_ref(&self) -> &[u8] {
278 &self.0
279 }
280}