cesiumdb 0.1.0

Blazing fast, persistent key-value store for Rust
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
// Copyright (c) Sienna Satterwhite, CesiumDB Contributors
// SPDX-License-Identifier: GPL-3.0-only WITH Classpath-exception-2.0

use std::io;

use bytes::Bytes;
use thiserror::Error;

use crate::segment::BlockType;

#[derive(Error, Debug)]
pub enum ManifestError {
    #[error("I/O error: {0}")]
    Io(#[from] io::Error),
    #[error("corrupted manifest header")]
    CorruptedHeader,
    #[error("invalid magic number: expected 0x43455349, got {0:#x}")]
    InvalidMagic(u32),
    #[error("unsupported manifest version: {0}")]
    UnsupportedVersion(u32),
    #[error("CRC mismatch: expected {expected:#x}, got {actual:#x}")]
    CrcMismatch { expected: u32, actual: u32 },
    #[error("invalid VersionEdit type: {0:#x}")]
    InvalidEditType(u8),
    #[error("segment error: {0}")]
    SegmentError(#[from] SegmentError),
}

#[derive(Error, Debug)]
pub enum CesiumError {
    #[error("memtable error")]
    MemtableError(MemtableError),
    #[error("fs error")]
    FsError(FsError),
    #[error("block error")]
    BlockError(BlockError),
    #[error("segment error")]
    SegmentError(SegmentError),
    #[error("journal error")]
    JournalError(JournalError),
    #[error("compaction error: {0}")]
    CompactionError(#[from] CompactionError),
    #[error("manifest error: {0}")]
    ManifestError(#[from] ManifestError),
}

#[derive(Error, Debug)]
pub enum CompactionError {
    #[error("compaction not initialized")]
    NotInitialized,
    #[error("compaction job failed: {0}")]
    JobFailed(String),
    #[error("version changed during compaction")]
    VersionChanged,
    #[error("segment error: {0}")]
    SegmentError(#[from] SegmentError),
    #[error("I/O error: {0}")]
    IoError(#[from] io::Error),
    #[error("shutting down")]
    ShuttingDown,
}

#[derive(Error, Debug)]
pub enum MemtableError {
    #[error("data insertion would exceed maximum capacity")]
    DataExceedsMaximum,
    #[error("memtable is frozen")]
    MemtableIsFrozen,
}

#[derive(Error, Debug)]
pub enum BlockError {
    #[error("block is corrupted")]
    CorruptedBlock,
    #[error("block is full")]
    BlockFull,
    #[error("entry is too large for block")]
    TooLargeForBlock,
}

#[derive(Error, Debug)]
pub enum JournalError {
    #[error("journal entry too short")]
    EntryTooShort,
    #[error("invalid journal entry type")]
    InvalidEntryType,
}

#[derive(Error, Debug)]
pub enum SegmentError {
    #[error("segment is full")]
    InsufficientSpace,
    #[error("segment must be multiple of 4096")]
    InvalidSize,
    #[error("read-ahead cache size is invalid")]
    InvalidReadAheadSize,
    #[error("can't create writer for {0} block id {1}")]
    CantCreateWriter(BlockType, u64),
    #[error("can't create reader")]
    CantCreateReader,
    #[error("read out of bounds")]
    ReadOutOfBounds,
    #[error("write out of bounds")]
    WriteOutOfBounds,
    #[error("missing key")]
    MissingKey,
    #[error("corrupted block")]
    CorruptedBlock,
    #[error("segment is closing, no more blocks can be written")]
    Closing,
    #[error("segment is not closing")]
    NotClosing,
    #[error("segment is read-only")]
    ReadOnly,
    #[error("io error")]
    IoError(io::Error),
}

#[derive(Error, Debug)]
pub enum FsError {
    #[error("os i/o error")]
    IoError(io::Error),
    #[error("invalid header format")]
    InvalidHeaderFormat(String),
    #[error("no contiguous space available, can't find a contiguous block big enough")]
    NoContiguousSpace,
    #[error("block is too fragmented")]
    FragmentationLimit,
    #[error("filesystem is full")]
    StorageExhausted,
    #[error("frange is already open")]
    FRangeAlreadyOpen,
    #[error("frange is already closed")]
    FRangeAlreadyClosed,
    #[error("frange not found")]
    FRangeNotFound,
    #[error("frange is still open")]
    FRangeStillOpen,
    #[error("read out of bounds")]
    ReadOutOfBounds,
    #[error("write out of bounds")]
    WriteOutOfBounds,
    #[error("block index out of bounds")]
    BlockIndexOutOfBounds,
    #[error("no adjacent space available, can't find a free range after metadata")]
    NoAdjacentSpace,
    #[error("adjacent space isn't big enough")]
    InsufficientSpace,
    #[error("metadata too large")]
    MetadataTooLarge,
    #[error("no free space available")]
    NoFreeSpace,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cesium_error_display() {
        let err = CesiumError::MemtableError(MemtableError::DataExceedsMaximum);
        assert_eq!(err.to_string(), "memtable error");

        let err = CesiumError::BlockError(BlockError::BlockFull);
        assert_eq!(err.to_string(), "block error");

        let err = CesiumError::SegmentError(SegmentError::InsufficientSpace);
        assert_eq!(err.to_string(), "segment error");
    }

    #[test]
    fn test_memtable_error_display() {
        let err = MemtableError::DataExceedsMaximum;
        assert_eq!(
            err.to_string(),
            "data insertion would exceed maximum capacity"
        );

        let err = MemtableError::MemtableIsFrozen;
        assert_eq!(err.to_string(), "memtable is frozen");
    }

    #[test]
    fn test_block_error_display() {
        let err = BlockError::CorruptedBlock;
        assert_eq!(err.to_string(), "block is corrupted");

        let err = BlockError::BlockFull;
        assert_eq!(err.to_string(), "block is full");

        let err = BlockError::TooLargeForBlock;
        assert_eq!(err.to_string(), "entry is too large for block");
    }

    #[test]
    fn test_journal_error_display() {
        let err = JournalError::EntryTooShort;
        assert_eq!(err.to_string(), "journal entry too short");

        let err = JournalError::InvalidEntryType;
        assert_eq!(err.to_string(), "invalid journal entry type");
    }

    #[test]
    fn test_segment_error_display() {
        let err = SegmentError::InsufficientSpace;
        assert_eq!(err.to_string(), "segment is full");

        let err = SegmentError::InvalidSize;
        assert_eq!(err.to_string(), "segment must be multiple of 4096");

        let err = SegmentError::InvalidReadAheadSize;
        assert_eq!(err.to_string(), "read-ahead cache size is invalid");

        let err = SegmentError::ReadOutOfBounds;
        assert_eq!(err.to_string(), "read out of bounds");

        let err = SegmentError::WriteOutOfBounds;
        assert_eq!(err.to_string(), "write out of bounds");

        let err = SegmentError::MissingKey;
        assert_eq!(err.to_string(), "missing key");

        let err = SegmentError::CorruptedBlock;
        assert_eq!(err.to_string(), "corrupted block");

        let err = SegmentError::Closing;
        assert_eq!(
            err.to_string(),
            "segment is closing, no more blocks can be written"
        );

        let err = SegmentError::NotClosing;
        assert_eq!(err.to_string(), "segment is not closing");

        let err = SegmentError::ReadOnly;
        assert_eq!(err.to_string(), "segment is read-only");
    }

    #[test]
    fn test_segment_error_with_block_type() {
        use crate::segment::BlockType;

        let err = SegmentError::CantCreateWriter(BlockType::Key, 42);
        assert!(err.to_string().contains("key"));
        assert!(err.to_string().contains("42"));
    }

    #[test]
    fn test_fs_error_display() {
        let err = FsError::InvalidHeaderFormat("bad header".to_string());
        let err_str = err.to_string();
        assert!(
            err_str.contains("invalid header format"),
            "Error string: {}",
            err_str
        );
        // Note: The error format doesn't include the string parameter in display

        let err = FsError::NoContiguousSpace;
        assert_eq!(
            err.to_string(),
            "no contiguous space available, can't find a contiguous block big enough"
        );

        let err = FsError::FragmentationLimit;
        assert_eq!(err.to_string(), "block is too fragmented");

        let err = FsError::StorageExhausted;
        assert_eq!(err.to_string(), "filesystem is full");

        let err = FsError::FRangeAlreadyOpen;
        assert_eq!(err.to_string(), "frange is already open");

        let err = FsError::FRangeAlreadyClosed;
        assert_eq!(err.to_string(), "frange is already closed");

        let err = FsError::FRangeNotFound;
        assert_eq!(err.to_string(), "frange not found");

        let err = FsError::FRangeStillOpen;
        assert_eq!(err.to_string(), "frange is still open");

        let err = FsError::ReadOutOfBounds;
        assert_eq!(err.to_string(), "read out of bounds");

        let err = FsError::WriteOutOfBounds;
        assert_eq!(err.to_string(), "write out of bounds");

        let err = FsError::BlockIndexOutOfBounds;
        assert_eq!(err.to_string(), "block index out of bounds");

        let err = FsError::NoAdjacentSpace;
        assert_eq!(
            err.to_string(),
            "no adjacent space available, can't find a free range after metadata"
        );

        let err = FsError::InsufficientSpace;
        assert_eq!(err.to_string(), "adjacent space isn't big enough");

        let err = FsError::MetadataTooLarge;
        assert_eq!(err.to_string(), "metadata too large");

        let err = FsError::NoFreeSpace;
        assert_eq!(err.to_string(), "no free space available");
    }

    #[test]
    fn test_error_wrapping() {
        let memtable_err = MemtableError::DataExceedsMaximum;
        let cesium_err = CesiumError::MemtableError(memtable_err);

        match cesium_err {
            | CesiumError::MemtableError(MemtableError::DataExceedsMaximum) => {},
            | _ => panic!("expected DataExceedsMaximum"),
        }
    }

    #[test]
    fn test_error_debug() {
        let err = MemtableError::DataExceedsMaximum;
        let debug_str = format!("{:?}", err);
        assert!(debug_str.contains("DataExceedsMaximum"));

        let err = BlockError::BlockFull;
        let debug_str = format!("{:?}", err);
        assert!(debug_str.contains("BlockFull"));
    }

    #[test]
    fn test_io_error_wrapping() {
        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
        let segment_err = SegmentError::IoError(io_err);

        match segment_err {
            | SegmentError::IoError(_) => {},
            | _ => panic!("expected IoError variant"),
        }
    }

    #[test]
    fn test_fs_io_error_wrapping() {
        let io_err = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "access denied");
        let fs_err = FsError::IoError(io_err);

        match fs_err {
            | FsError::IoError(_) => {},
            | _ => panic!("expected IoError variant"),
        }
    }

    #[test]
    fn test_all_memtable_error_variants() {
        let errors = vec![
            MemtableError::DataExceedsMaximum,
            MemtableError::MemtableIsFrozen,
        ];

        for err in errors {
            // just verify they can all be created and displayed
            let _ = err.to_string();
        }
    }

    #[test]
    fn test_all_block_error_variants() {
        let errors = vec![
            BlockError::CorruptedBlock,
            BlockError::BlockFull,
            BlockError::TooLargeForBlock,
        ];

        for err in errors {
            let _ = err.to_string();
        }
    }

    #[test]
    fn test_all_journal_error_variants() {
        let errors = vec![JournalError::EntryTooShort, JournalError::InvalidEntryType];

        for err in errors {
            let _ = err.to_string();
        }
    }

    #[test]
    fn test_cesium_error_from_memtable() {
        let mem_err = MemtableError::MemtableIsFrozen;
        let cesium_err = CesiumError::MemtableError(mem_err);

        assert!(matches!(
            cesium_err,
            CesiumError::MemtableError(MemtableError::MemtableIsFrozen)
        ));
    }

    #[test]
    fn test_cesium_error_from_block() {
        let block_err = BlockError::BlockFull;
        let cesium_err = CesiumError::BlockError(block_err);

        assert!(matches!(
            cesium_err,
            CesiumError::BlockError(BlockError::BlockFull)
        ));
    }

    #[test]
    fn test_cesium_error_from_segment() {
        let segment_err = SegmentError::ReadOnly;
        let cesium_err = CesiumError::SegmentError(segment_err);

        assert!(matches!(
            cesium_err,
            CesiumError::SegmentError(SegmentError::ReadOnly)
        ));
    }
}