-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathserde.rs
More file actions
494 lines (416 loc) · 13.8 KB
/
Copy pathserde.rs
File metadata and controls
494 lines (416 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#![allow(dead_code)]
//! Serde for log storage
//!
//! This module provides encoding and decoding for log records stored in SlateDB.
//! The encoding scheme is designed to preserve lexicographic ordering of keys
//! while supporting variable-length user keys.
//!
//! # Key Format
//!
//! All keys start with a version byte and record type discriminator:
//!
//! ```text
//! | version (u8) | type (u8) | ... record-specific fields ... |
//! ```
//!
//! # Record Types
//!
//! - `LogEntry` (0x01): User data entries with key and sequence number
//! - `SeqBlock` (0x02): Sequence number block allocation tracking
//!
//! # TerminatedBytes Encoding
//!
//! Variable-length user keys use a terminated encoding that preserves
//! lexicographic ordering. Keys are escaped and terminated with `0x00`:
//!
//! - `0x00` → `0x01 0x01`
//! - `0x01` → `0x01 0x02`
//! - `0xFF` → `0x01 0x03`
//! - All other bytes unchanged
//! - Terminated with `0x00` delimiter
//!
//! Using `0x00` as the terminator ensures shorter keys sort before longer
//! keys with the same prefix (e.g., "/foo" < "/foo/bar"). This simplifies
//! prefix-based range queries: start at `prefix + 0x00`, end at `prefix + 0xFF`.
use bytes::{BufMut, Bytes, BytesMut};
use common::serde::terminated_bytes;
use crate::error::Error;
impl From<common::serde::DeserializeError> for Error {
fn from(err: common::serde::DeserializeError) -> Self {
Error::Encoding(err.message)
}
}
/// Key format version (currently 0x01)
pub const KEY_VERSION: u8 = 0x01;
/// Record type discriminators for log storage
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecordType {
/// Log entry record containing user key, sequence, and value
LogEntry = 0x01,
/// Block allocation record for sequence number tracking
SeqBlock = 0x02,
}
impl RecordType {
/// Returns the ID of this record type
pub fn id(&self) -> u8 {
*self as u8
}
/// Converts a u8 id back to a RecordType
pub fn from_id(id: u8) -> Result<Self, Error> {
match id {
0x01 => Ok(RecordType::LogEntry),
0x02 => Ok(RecordType::SeqBlock),
_ => Err(Error::Encoding(format!(
"invalid record type: 0x{:02x}",
id
))),
}
}
}
/// Key for a log entry record.
///
/// The key encodes the user key and sequence number in a format that
/// preserves lexicographic ordering:
///
/// ```text
/// | version (u8) | type (u8) | key (TerminatedBytes) | sequence (u64 BE) |
/// ```
///
/// This ensures that:
/// - All entries for a given key are contiguous
/// - Within a key, entries are ordered by sequence number
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LogEntryKey {
/// The user-provided key identifying the log stream
pub key: Bytes,
/// The sequence number assigned to this entry
pub sequence: u64,
}
impl LogEntryKey {
/// Creates a new log entry key
pub fn new(key: Bytes, sequence: u64) -> Self {
Self { key, sequence }
}
/// Encodes the key to bytes for storage
pub fn encode(&self) -> Bytes {
let mut buf = BytesMut::new();
buf.put_u8(KEY_VERSION);
buf.put_u8(RecordType::LogEntry.id());
terminated_bytes::serialize(&self.key, &mut buf);
buf.put_u64(self.sequence);
buf.freeze()
}
/// Decodes a log entry key from bytes
pub fn decode(data: &[u8]) -> Result<Self, Error> {
if data.len() < 2 {
return Err(Error::Encoding(
"buffer too short for log entry key".to_string(),
));
}
if data[0] != KEY_VERSION {
return Err(Error::Encoding(format!(
"invalid key version: expected 0x{:02x}, got 0x{:02x}",
KEY_VERSION, data[0]
)));
}
let record_type = RecordType::from_id(data[1])?;
if record_type != RecordType::LogEntry {
return Err(Error::Encoding(format!(
"invalid record type: expected LogEntry, got {:?}",
record_type
)));
}
let mut buf = &data[2..];
let key = terminated_bytes::deserialize(&mut buf)?;
if buf.len() < 8 {
return Err(Error::Encoding(
"buffer too short for sequence number".to_string(),
));
}
let sequence = u64::from_be_bytes([
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
]);
Ok(LogEntryKey { key, sequence })
}
/// Creates a prefix for scanning all entries of a given key.
///
/// The prefix includes the version, type, and terminated key bytes,
/// allowing efficient range scans for a single key's log.
pub fn key_prefix(key: &[u8]) -> Bytes {
let mut buf = BytesMut::new();
buf.put_u8(KEY_VERSION);
buf.put_u8(RecordType::LogEntry.id());
terminated_bytes::serialize(key, &mut buf);
buf.freeze()
}
}
/// Key for the SeqBlock record (static, singleton key).
///
/// ```text
/// | version (u8) | type (u8) |
/// ```
///
/// There is exactly one SeqBlock record in the database.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LastSeqBlockKey;
impl LastSeqBlockKey {
/// Encodes the SeqBlock key
pub fn encode(&self) -> Bytes {
Bytes::from(vec![KEY_VERSION, RecordType::SeqBlock.id()])
}
/// Decodes and validates a SeqBlock key
pub fn decode(data: &[u8]) -> Result<Self, Error> {
if data.len() < 2 {
return Err(Error::Encoding(
"buffer too short for SeqBlock key".to_string(),
));
}
if data[0] != KEY_VERSION {
return Err(Error::Encoding(format!(
"invalid key version: expected 0x{:02x}, got 0x{:02x}",
KEY_VERSION, data[0]
)));
}
let record_type = RecordType::from_id(data[1])?;
if record_type != RecordType::SeqBlock {
return Err(Error::Encoding(format!(
"invalid record type: expected SeqBlock, got {:?}",
record_type
)));
}
Ok(LastSeqBlockKey)
}
}
/// Value for the SeqBlock record.
///
/// Stores the current sequence block allocation:
///
/// ```text
/// | base_sequence (u64 BE) | block_size (u64 BE) |
/// ```
///
/// The allocated range is `[base_sequence, base_sequence + block_size)`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SeqBlock {
/// Base sequence number of the allocated block
pub base_sequence: u64,
/// Size of the allocated block
pub block_size: u64,
}
impl SeqBlock {
/// Creates a new SeqBlock value
pub fn new(base_sequence: u64, block_size: u64) -> Self {
Self {
base_sequence,
block_size,
}
}
/// Encodes the value to bytes
pub fn encode(&self) -> Bytes {
let mut buf = BytesMut::with_capacity(16);
buf.put_u64(self.base_sequence);
buf.put_u64(self.block_size);
buf.freeze()
}
/// Decodes a SeqBlock value from bytes
pub fn decode(data: &[u8]) -> Result<Self, Error> {
if data.len() < 16 {
return Err(Error::Encoding(format!(
"buffer too short for SeqBlock value: need 16 bytes, got {}",
data.len()
)));
}
let base_sequence = u64::from_be_bytes([
data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
]);
let block_size = u64::from_be_bytes([
data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15],
]);
Ok(SeqBlock {
base_sequence,
block_size,
})
}
/// Returns the next sequence number after this block
pub fn next_base(&self) -> u64 {
self.base_sequence + self.block_size
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_convert_record_type_to_id_and_back() {
// given
let log_entry = RecordType::LogEntry;
let seq_block = RecordType::SeqBlock;
// when/then
assert_eq!(log_entry.id(), 0x01);
assert_eq!(seq_block.id(), 0x02);
assert_eq!(RecordType::from_id(0x01).unwrap(), RecordType::LogEntry);
assert_eq!(RecordType::from_id(0x02).unwrap(), RecordType::SeqBlock);
}
#[test]
fn should_reject_invalid_record_type() {
// given
let invalid_byte = 0x99;
// when
let result = RecordType::from_id(invalid_byte);
// then
assert!(result.is_err());
}
#[test]
fn should_serialize_and_deserialize_log_entry_key() {
// given
let key = LogEntryKey::new(Bytes::from("orders"), 12345);
// when
let encoded = key.encode();
let decoded = LogEntryKey::decode(&encoded).unwrap();
// then
assert_eq!(decoded, key);
}
#[test]
fn should_serialize_and_deserialize_log_entry_key_with_special_bytes() {
// given - key containing all special bytes: terminator (0x00), escape (0x01), range end (0xFF)
let key = LogEntryKey::new(Bytes::from_static(&[0x61, 0x00, 0x01, 0xFF, 0x62]), 99999);
// when
let encoded = key.encode();
let decoded = LogEntryKey::decode(&encoded).unwrap();
// then
assert_eq!(decoded, key);
}
#[test]
fn should_serialize_log_entry_key_with_correct_structure() {
// given
let key = LogEntryKey::new(Bytes::from("test"), 256);
// when
let encoded = key.encode();
// then
assert_eq!(encoded[0], KEY_VERSION);
assert_eq!(encoded[1], RecordType::LogEntry.id());
// "test" + terminator = [t, e, s, t, 0x00]
// sequence 256 = 0x0000000000000100
}
#[test]
fn should_order_log_entries_by_key_then_sequence() {
// given
let entries = [
LogEntryKey::new(Bytes::from("a"), 2),
LogEntryKey::new(Bytes::from("a"), 1),
LogEntryKey::new(Bytes::from("b"), 1),
LogEntryKey::new(Bytes::from("a"), 3),
];
// when
let mut encoded: Vec<Bytes> = entries.iter().map(|e| e.encode()).collect();
encoded.sort();
// then - should be ordered: a:1, a:2, a:3, b:1
let decoded: Vec<LogEntryKey> = encoded
.iter()
.map(|e| LogEntryKey::decode(e).unwrap())
.collect();
assert_eq!(decoded[0], LogEntryKey::new(Bytes::from("a"), 1));
assert_eq!(decoded[1], LogEntryKey::new(Bytes::from("a"), 2));
assert_eq!(decoded[2], LogEntryKey::new(Bytes::from("a"), 3));
assert_eq!(decoded[3], LogEntryKey::new(Bytes::from("b"), 1));
}
#[test]
fn should_generate_key_prefix_for_scanning() {
// given
let user_key = b"orders";
let entry1 = LogEntryKey::new(Bytes::from_static(user_key), 1);
let entry2 = LogEntryKey::new(Bytes::from_static(user_key), 100);
let other_entry = LogEntryKey::new(Bytes::from("other"), 1);
// when
let prefix = LogEntryKey::key_prefix(user_key);
// then
assert!(entry1.encode().starts_with(&prefix));
assert!(entry2.encode().starts_with(&prefix));
assert!(!other_entry.encode().starts_with(&prefix));
}
#[test]
fn should_fail_deserialize_log_entry_key_with_wrong_version() {
// given
let data = LogEntryKey::new(Bytes::from("test"), 1).encode();
let mut modified = data.to_vec();
modified[0] = 0x99; // wrong version
// when
let result = LogEntryKey::decode(&modified);
// then
assert!(result.is_err());
}
#[test]
fn should_fail_deserialize_log_entry_key_with_wrong_type() {
// given - SeqBlock type instead of LogEntry
let data = vec![KEY_VERSION, RecordType::SeqBlock.id()];
// when
let result = LogEntryKey::decode(&data);
// then
assert!(result.is_err());
}
#[test]
fn should_serialize_and_deserialize_seq_block_key() {
// given
let key = LastSeqBlockKey;
// when
let encoded = key.encode();
let decoded = LastSeqBlockKey::decode(&encoded).unwrap();
// then
assert_eq!(decoded, key);
assert_eq!(encoded.len(), 2);
assert_eq!(encoded[0], KEY_VERSION);
assert_eq!(encoded[1], RecordType::SeqBlock.id());
}
#[test]
fn should_fail_deserialize_seq_block_key_with_wrong_type() {
// given
let data = vec![KEY_VERSION, RecordType::LogEntry.id()];
// when
let result = LastSeqBlockKey::decode(&data);
// then
assert!(result.is_err());
}
#[test]
fn should_serialize_and_deserialize_seq_block_value() {
// given
let value = SeqBlock::new(1000, 100);
// when
let encoded = value.encode();
let decoded = SeqBlock::decode(&encoded).unwrap();
// then
assert_eq!(decoded, value);
assert_eq!(encoded.len(), 16);
}
#[test]
fn should_calculate_next_base() {
// given
let value = SeqBlock::new(1000, 100);
// when
let next = value.next_base();
// then
assert_eq!(next, 1100);
}
#[test]
fn should_fail_deserialize_seq_block_value_too_short() {
// given
let data = vec![0u8; 15]; // need 16 bytes
// when
let result = SeqBlock::decode(&data);
// then
assert!(result.is_err());
}
#[test]
fn should_serialize_seq_block_value_in_big_endian() {
// given
let value = SeqBlock::new(0x0102030405060708, 0x1112131415161718);
// when
let encoded = value.encode();
// then
assert_eq!(
encoded.as_ref(),
&[
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, // base_sequence
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, // block_size
]
);
}
}