haematite 0.3.0

Content-addressed, branchable, actor-native storage engine
Documentation
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
//! Binary WAL entry codec with CRC32 validation.

use super::buffer::{Mutation, WalError};

pub(crate) const TAG_PUT: u8 = 0x01;
pub(crate) const TAG_DELETE: u8 = 0x02;

const U32_SIZE: usize = 4;

/// The operation encoded by a WAL entry.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OperationType {
    /// Store `value` at `key`.
    Put,
    /// Remove `key`.
    Delete,
}

impl OperationType {
    pub(crate) const fn tag(self) -> u8 {
        match self {
            Self::Put => TAG_PUT,
            Self::Delete => TAG_DELETE,
        }
    }

    const fn from_tag(tag: u8) -> Result<Self, WalError> {
        match tag {
            TAG_PUT => Ok(Self::Put),
            TAG_DELETE => Ok(Self::Delete),
            found => Err(WalError::InvalidTag { found }),
        }
    }
}

/// A length-prefixed, checksummed write-ahead-log entry.
///
/// The deterministic payload used for the CRC is:
///
/// - `Put`: `[operation_type][key_len: u32 LE][key][value_len: u32 LE][value]`
/// - `Delete`: `[operation_type][key_len: u32 LE][key]`
///
/// The serialised entry appends `[crc32: u32 LE]`, where the checksum covers the
/// payload bytes above, including the length prefixes that make the binary
/// representation deterministic.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum WalEntry {
    /// Store `value` under `key`.
    Put {
        operation_type: OperationType,
        key: Vec<u8>,
        value: Vec<u8>,
        crc32: u32,
    },
    /// Delete `key`.
    Delete {
        operation_type: OperationType,
        key: Vec<u8>,
        crc32: u32,
    },
}

impl WalEntry {
    /// Build a `Put` entry and compute its CRC32 checksum.
    pub fn put<K, V>(key: K, value: V) -> Self
    where
        K: Into<Vec<u8>>,
        V: Into<Vec<u8>>,
    {
        let key = key.into();
        let value = value.into();
        let operation_type = OperationType::Put;
        let crc32 = checksum_for(operation_type, &key, Some(&value));
        Self::Put {
            operation_type,
            key,
            value,
            crc32,
        }
    }

    /// Build a `Delete` entry and compute its CRC32 checksum.
    pub fn delete<K>(key: K) -> Self
    where
        K: Into<Vec<u8>>,
    {
        let key = key.into();
        let operation_type = OperationType::Delete;
        let crc32 = checksum_for(operation_type, &key, None);
        Self::Delete {
            operation_type,
            key,
            crc32,
        }
    }

    /// Serialise this entry as `[payload][crc32: u32 LE]`.
    #[must_use]
    pub fn serialise(&self) -> Vec<u8> {
        let mut bytes = self.payload_bytes();
        bytes.extend_from_slice(&self.crc32().to_le_bytes());
        bytes
    }

    /// Deserialise and checksum-verify a WAL entry.
    pub fn deserialise(bytes: &[u8]) -> Result<Self, WalError> {
        let payload_len = bytes
            .len()
            .checked_sub(U32_SIZE)
            .ok_or(WalError::Truncated)?;
        let (payload, checksum_bytes) = bytes.split_at(payload_len);
        let expected = read_u32_le(checksum_bytes)?;
        let actual = crc32fast::hash(payload);
        if expected != actual {
            return Err(WalError::ChecksumMismatch { expected, actual });
        }

        let mut cursor = ByteCursor::new(payload);
        let operation_type = OperationType::from_tag(cursor.read_u8()?)?;
        match operation_type {
            OperationType::Put => {
                let key = cursor.read_len_prefixed_bytes()?.to_vec();
                let value = cursor.read_len_prefixed_bytes()?.to_vec();
                cursor.finish()?;
                Ok(Self::Put {
                    operation_type,
                    key,
                    value,
                    crc32: expected,
                })
            }
            OperationType::Delete => {
                let key = cursor.read_len_prefixed_bytes()?.to_vec();
                cursor.finish()?;
                Ok(Self::Delete {
                    operation_type,
                    key,
                    crc32: expected,
                })
            }
        }
    }

    /// Return this entry's operation type field.
    #[must_use]
    pub const fn operation_type(&self) -> OperationType {
        match self {
            Self::Put { operation_type, .. } | Self::Delete { operation_type, .. } => {
                *operation_type
            }
        }
    }

    /// Return this entry's key bytes.
    #[must_use]
    pub fn key(&self) -> &[u8] {
        match self {
            Self::Put { key, .. } | Self::Delete { key, .. } => key,
        }
    }

    /// Return this entry's value bytes, or `None` for deletes.
    #[must_use]
    pub fn value(&self) -> Option<&[u8]> {
        match self {
            Self::Put { value, .. } => Some(value),
            Self::Delete { .. } => None,
        }
    }

    /// Return the checksum stored in this entry.
    #[must_use]
    pub const fn crc32(&self) -> u32 {
        match self {
            Self::Put { crc32, .. } | Self::Delete { crc32, .. } => *crc32,
        }
    }

    /// Recompute the checksum from the deterministic payload fields.
    #[must_use]
    pub fn computed_crc32(&self) -> u32 {
        crc32fast::hash(&self.payload_bytes())
    }

    fn payload_bytes(&self) -> Vec<u8> {
        match self {
            Self::Put {
                operation_type,
                key,
                value,
                ..
            } => payload_bytes(*operation_type, key, Some(value)),
            Self::Delete {
                operation_type,
                key,
                ..
            } => payload_bytes(*operation_type, key, None),
        }
    }
}

impl From<&Mutation> for WalEntry {
    fn from(mutation: &Mutation) -> Self {
        match mutation {
            Mutation::Put { key, value } => Self::put(key.clone(), value.clone()),
            Mutation::Delete { key } => Self::delete(key.clone()),
        }
    }
}

impl From<Mutation> for WalEntry {
    fn from(mutation: Mutation) -> Self {
        match mutation {
            Mutation::Put { key, value } => Self::put(key, value),
            Mutation::Delete { key } => Self::delete(key),
        }
    }
}

fn checksum_for(operation_type: OperationType, key: &[u8], value: Option<&[u8]>) -> u32 {
    crc32fast::hash(&payload_bytes(operation_type, key, value))
}

fn payload_bytes(operation_type: OperationType, key: &[u8], value: Option<&[u8]>) -> Vec<u8> {
    let value_len = value.map_or(0, <[u8]>::len);
    let mut bytes = Vec::with_capacity(1 + U32_SIZE + key.len() + U32_SIZE + value_len);
    bytes.push(operation_type.tag());
    append_len(&mut bytes, key.len());
    bytes.extend_from_slice(key);
    if let Some(value) = value {
        append_len(&mut bytes, value.len());
        bytes.extend_from_slice(value);
    }
    bytes
}

fn append_len(bytes: &mut Vec<u8>, len: usize) {
    bytes.extend_from_slice(&(len as u32).to_le_bytes());
}

fn read_u32_le(bytes: &[u8]) -> Result<u32, WalError> {
    if bytes.len() != U32_SIZE {
        return Err(WalError::Truncated);
    }
    Ok(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
}

#[derive(Debug)]
struct ByteCursor<'a> {
    bytes: &'a [u8],
    offset: usize,
}

impl<'a> ByteCursor<'a> {
    const fn new(bytes: &'a [u8]) -> Self {
        Self { bytes, offset: 0 }
    }

    fn read_u8(&mut self) -> Result<u8, WalError> {
        let byte = *self.bytes.get(self.offset).ok_or(WalError::Truncated)?;
        self.offset += 1;
        Ok(byte)
    }

    fn read_u32(&mut self) -> Result<u32, WalError> {
        read_u32_le(self.read_bytes(U32_SIZE)?)
    }

    fn read_len_prefixed_bytes(&mut self) -> Result<&'a [u8], WalError> {
        let len = usize::try_from(self.read_u32()?).map_err(|_| WalError::LengthOverflow)?;
        self.read_bytes(len)
    }

    fn read_bytes(&mut self, len: usize) -> Result<&'a [u8], WalError> {
        let end = self
            .offset
            .checked_add(len)
            .ok_or(WalError::LengthOverflow)?;
        let slice = self
            .bytes
            .get(self.offset..end)
            .ok_or(WalError::Truncated)?;
        self.offset = end;
        Ok(slice)
    }

    const fn finish(&self) -> Result<(), WalError> {
        let trailing = self.bytes.len().saturating_sub(self.offset);
        if trailing == 0 {
            Ok(())
        } else {
            Err(WalError::TrailingBytes { trailing })
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{OperationType, TAG_DELETE, TAG_PUT, WalEntry, payload_bytes};
    use crate::wal::buffer::{Mutation, WalError};

    fn entry_bytes(payload: &[u8]) -> Vec<u8> {
        let mut bytes = Vec::from(payload);
        bytes.extend_from_slice(&crc32fast::hash(payload).to_le_bytes());
        bytes
    }

    #[test]
    fn put_entry_contains_operation_key_value_and_crc() {
        let entry = WalEntry::put(b"key".to_vec(), b"value".to_vec());
        let payload = payload_bytes(OperationType::Put, b"key", Some(b"value"));

        if let WalEntry::Put {
            operation_type,
            key,
            value,
            crc32,
        } = entry
        {
            assert_eq!(operation_type, OperationType::Put);
            assert_eq!(key, b"key".to_vec());
            assert_eq!(value, b"value".to_vec());
            assert_eq!(crc32, crc32fast::hash(&payload));
        } else {
            unreachable_put_entry();
        }
    }

    #[test]
    fn delete_entry_contains_operation_key_and_crc() {
        let entry = WalEntry::delete(b"key".to_vec());
        let payload = payload_bytes(OperationType::Delete, b"key", None);

        if let WalEntry::Delete {
            operation_type,
            key,
            crc32,
        } = entry
        {
            assert_eq!(operation_type, OperationType::Delete);
            assert_eq!(key, b"key".to_vec());
            assert_eq!(crc32, crc32fast::hash(&payload));
        } else {
            unreachable_delete_entry();
        }
    }

    #[test]
    fn crc_covers_operation_type_key_and_value_payload() {
        let entry = WalEntry::put(b"ab".to_vec(), b"xyz".to_vec());
        let mut payload = vec![TAG_PUT];
        payload.extend_from_slice(&2u32.to_le_bytes());
        payload.extend_from_slice(b"ab");
        payload.extend_from_slice(&3u32.to_le_bytes());
        payload.extend_from_slice(b"xyz");

        assert_eq!(entry.computed_crc32(), crc32fast::hash(&payload));
        assert_eq!(entry.crc32(), entry.computed_crc32());
    }

    #[test]
    fn put_serialise_deserialise_round_trips() -> Result<(), WalError> {
        let entry = WalEntry::put(b"alpha".to_vec(), b"beta".to_vec());
        let decoded = WalEntry::deserialise(&entry.serialise())?;
        assert_eq!(decoded, entry);
        Ok(())
    }

    #[test]
    fn delete_serialise_deserialise_round_trips() -> Result<(), WalError> {
        let entry = WalEntry::delete(b"alpha".to_vec());
        let decoded = WalEntry::deserialise(&entry.serialise())?;
        assert_eq!(decoded, entry);
        Ok(())
    }

    #[test]
    fn serialised_put_is_tagged_and_length_prefixed() {
        let bytes = WalEntry::put(b"ab".to_vec(), b"xyz".to_vec()).serialise();
        let mut expected = vec![TAG_PUT];
        expected.extend_from_slice(&2u32.to_le_bytes());
        expected.extend_from_slice(b"ab");
        expected.extend_from_slice(&3u32.to_le_bytes());
        expected.extend_from_slice(b"xyz");
        expected.extend_from_slice(&crc32fast::hash(&expected).to_le_bytes());
        assert_eq!(bytes, expected);
    }

    #[test]
    fn serialised_delete_is_tagged_and_length_prefixed() {
        let bytes = WalEntry::delete(b"ab".to_vec()).serialise();
        let mut expected = vec![TAG_DELETE];
        expected.extend_from_slice(&2u32.to_le_bytes());
        expected.extend_from_slice(b"ab");
        expected.extend_from_slice(&crc32fast::hash(&expected).to_le_bytes());
        assert_eq!(bytes, expected);
    }

    #[test]
    fn deserialise_detects_checksum_mismatch() {
        let mut bytes = WalEntry::put(b"k".to_vec(), b"v".to_vec()).serialise();
        if let Some(byte) = bytes.last_mut() {
            *byte ^= 0xff;
        }

        assert!(matches!(
            WalEntry::deserialise(&bytes),
            Err(WalError::ChecksumMismatch { .. })
        ));
    }

    #[test]
    fn deserialise_rejects_invalid_operation_tag() {
        let payload = [0xff, 0, 0, 0, 0];
        let bytes = entry_bytes(&payload);
        assert!(matches!(
            WalEntry::deserialise(&bytes),
            Err(WalError::InvalidTag { found: 0xff })
        ));
    }

    #[test]
    fn deserialise_rejects_truncated_input() {
        assert!(matches!(
            WalEntry::deserialise(&[TAG_PUT]),
            Err(WalError::Truncated)
        ));
    }

    #[test]
    fn deserialise_rejects_trailing_bytes() {
        let payload = [TAG_DELETE, 0, 0, 0, 0, 0xaa];
        let bytes = entry_bytes(&payload);
        assert!(matches!(
            WalEntry::deserialise(&bytes),
            Err(WalError::TrailingBytes { trailing: 1 })
        ));
    }

    #[test]
    fn wal_entry_is_debug() {
        let entry = WalEntry::delete(b"debug".to_vec());
        assert!(format!("{entry:?}").contains("Delete"));
    }

    #[test]
    fn mutation_converts_to_wal_entry() {
        let mutation = Mutation::Put {
            key: b"k".to_vec(),
            value: b"v".to_vec(),
        };
        let entry = WalEntry::from(&mutation);
        assert_eq!(entry, WalEntry::put(b"k".to_vec(), b"v".to_vec()));
    }

    fn unreachable_put_entry() {
        unreachable!("WalEntry::put must construct the Put variant");
    }

    fn unreachable_delete_entry() {
        unreachable!("WalEntry::delete must construct the Delete variant");
    }
}