armdb 0.7.0

sharded bitcask key-value storage optimized for NVMe
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! Integration tests for `Db::run_migration` multi-step walk and
//! schema-mismatch detection (V-001, V-002, V-005, V-006).
//!
//! Feature gates: the armour + typed-tree + rapira-codec features must all be
//! active (same as `db_close_flush_tests.rs`).
#![cfg(all(feature = "typed-tree", feature = "armour", feature = "rapira-codec"))]

use armdb::armour::{Db, TypedMigration};
use armdb::{
    CollectionMeta, Config, DbError, MigrateAction, NoHook, RapiraCodec, SchemaMismatchKind,
};
use armour_core::GetType;
use rapira::Rapira;
use tempfile::tempdir;

// ---------------------------------------------------------------------------
// Test fixtures
//
// We need several struct families that share a NAME so they open the same
// on-disk collection but carry different VERSION constants (and in the
// typ-hash test, different fields).
//
// Family A: "mig_test_items" — V1, V2, V3
//   All have the same fields (value: u64) so the serialisation layout is the
//   same across versions. This lets migration fns read V1 data as V3 without
//   needing to re-define the on-disk format.
//
// Family B: "mig_hash_items" — two structs with the same VERSION=1 but
//   different field layouts, giving different GetType hashes.
//   Used for the typ_hash drift test.
// ---------------------------------------------------------------------------

// ── Family A ──────────────────────────────────────────────────────────────

#[derive(Clone, Debug, PartialEq, Rapira, GetType)]
struct ItemV1 {
    value: u64,
}

impl CollectionMeta for ItemV1 {
    type SelfId = [u8; 8];
    const NAME: &'static str = "mig_test_items";
    const VERSION: u16 = 1;
}

#[derive(Clone, Debug, PartialEq, Rapira, GetType)]
struct ItemV2 {
    value: u64,
}

impl CollectionMeta for ItemV2 {
    type SelfId = [u8; 8];
    const NAME: &'static str = "mig_test_items";
    const VERSION: u16 = 2;
}

#[derive(Clone, Debug, PartialEq, Rapira, GetType)]
struct ItemV3 {
    value: u64,
}

impl CollectionMeta for ItemV3 {
    type SelfId = [u8; 8];
    const NAME: &'static str = "mig_test_items";
    const VERSION: u16 = 3;
}

// ── Family B ──────────────────────────────────────────────────────────────
// Same NAME and VERSION=1, but different fields → different GetType hash.

#[derive(Clone, Debug, PartialEq, Rapira, GetType)]
struct HashItemA {
    value: u64,
}

impl CollectionMeta for HashItemA {
    type SelfId = [u8; 8];
    const NAME: &'static str = "mig_hash_items";
    const VERSION: u16 = 1;
}

#[derive(Clone, Debug, PartialEq, Rapira, GetType)]
struct HashItemB {
    /// Different field layout: two fields instead of one.
    value: u64,
    extra: u32,
}

impl CollectionMeta for HashItemB {
    type SelfId = [u8; 8];
    const NAME: &'static str = "mig_hash_items";
    const VERSION: u16 = 1;
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn key(id: u64) -> [u8; 8] {
    id.to_be_bytes()
}

fn unwrap_err<T>(r: Result<T, DbError>) -> DbError {
    match r {
        Err(e) => e,
        Ok(_) => panic!("expected Err, got Ok"),
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

/// Two-step migration V1→V2→V3 must apply both steps in order.
///
/// All three versions share the same on-disk layout (value: u64), so the
/// migration callbacks operate on ItemV3 for both steps — that is the type
/// used when opening.  We add 1 per step: starting value=1, V1→V2 adds 1,
/// V2→V3 adds 1 again, final value=3.
///
/// The second reopen (version already at 3) must NOT run any migration.
#[test]
fn migration_multi_step_walks_intermediate_versions() {
    let dir = tempdir().unwrap();

    // Phase 1: open with V1, insert two entries (value = 1).
    {
        let db = Db::open_test(dir.path()).unwrap();
        let tree = db
            .open_typed_tree::<ItemV1, RapiraCodec, _>(Config::test(), NoHook, &[])
            .unwrap();
        tree.put(&key(0), ItemV1 { value: 1 }).unwrap();
        tree.put(&key(1), ItemV1 { value: 1 }).unwrap();
        db.close().unwrap();
    }

    // Migration functions for the V3 opener.
    // Both callbacks have type fn(&[u8;8], &ItemV3) -> MigrateAction<ItemV3>
    // because that is what open_typed_tree::<ItemV3, ...> expects.
    // The V1/V2 on-disk data decodes fine as ItemV3 (same layout).
    fn add_one_step(_k: &[u8; 8], v: &ItemV3) -> MigrateAction<ItemV3> {
        MigrateAction::Update(ItemV3 { value: v.value + 1 })
    }

    #[allow(clippy::type_complexity)]
    let migrations: &[(u16, fn(&[u8; 8], &ItemV3) -> MigrateAction<ItemV3>)] =
        &[(1, add_one_step), (2, add_one_step)];

    // Phase 2: reopen with V3 and two migration steps.
    {
        let db = Db::open_test(dir.path()).unwrap();
        let tree = db
            .open_typed_tree::<ItemV3, RapiraCodec, _>(Config::test(), NoHook, migrations)
            .unwrap();
        // Each entry was incremented twice: 1 → 2 → 3.
        assert_eq!(
            tree.get(&key(0)).unwrap().value,
            3,
            "entry 0 must reach v=3"
        );
        assert_eq!(
            tree.get(&key(1)).unwrap().value,
            3,
            "entry 1 must reach v=3"
        );
        db.close().unwrap();
    }

    // Phase 3: reopen with V3 again — stored version is already 3 so no
    // migration must run; values remain 3.
    {
        let db = Db::open_test(dir.path()).unwrap();
        let tree = db
            .open_typed_tree::<ItemV3, RapiraCodec, _>(Config::test(), NoHook, migrations)
            .unwrap();
        assert_eq!(tree.get(&key(0)).unwrap().value, 3, "no second migration");
        assert_eq!(tree.get(&key(1)).unwrap().value, 3, "no second migration");
    }
}

/// When no migration step is registered for the stored version, open must
/// return `DbError::SchemaMismatch { kind: MissingStep { from: 1 } }`.
#[test]
fn migration_missing_step_returns_schema_mismatch() {
    let dir = tempdir().unwrap();

    // Open with V1, close.
    {
        let db = Db::open_test(dir.path()).unwrap();
        let _tree = db
            .open_typed_tree::<ItemV1, RapiraCodec, _>(Config::test(), NoHook, &[])
            .unwrap();
        db.close().unwrap();
    }

    // Reopen with V3, no migrations registered → MissingStep { from: 1 }.
    let db = Db::open_test(dir.path()).unwrap();
    let err = unwrap_err(db.open_typed_tree::<ItemV3, RapiraCodec, _>(Config::test(), NoHook, &[]));
    match err {
        DbError::SchemaMismatch {
            kind: SchemaMismatchKind::MissingStep { from },
            ..
        } => {
            assert_eq!(from, 1, "MissingStep should report from=1");
        }
        other => panic!("expected SchemaMismatch::MissingStep, got: {other}"),
    }
}

/// Reopening with a lower VERSION than stored must return
/// `DbError::SchemaMismatch { kind: Downgrade { stored: 3, requested: 2 } }`.
#[test]
fn migration_downgrade_returns_schema_mismatch() {
    let dir = tempdir().unwrap();

    // Open with V3 to persist version=3.
    {
        let db = Db::open_test(dir.path()).unwrap();
        let _tree = db
            .open_typed_tree::<ItemV3, RapiraCodec, _>(Config::test(), NoHook, &[])
            .unwrap();
        db.close().unwrap();
    }

    // Reopen with V2 → Downgrade { stored: 3, requested: 2 }.
    let db = Db::open_test(dir.path()).unwrap();
    let err = unwrap_err(db.open_typed_tree::<ItemV2, RapiraCodec, _>(Config::test(), NoHook, &[]));
    match err {
        DbError::SchemaMismatch {
            kind: SchemaMismatchKind::Downgrade { stored, requested },
            ..
        } => {
            assert_eq!(stored, 3);
            assert_eq!(requested, 2);
        }
        other => panic!("expected SchemaMismatch::Downgrade, got: {other}"),
    }
}

/// Reopening the same VERSION but with a different struct layout (different
/// typ_hash) must return `DbError::SchemaMismatch { kind: TypHash { .. } }`.
#[test]
fn migration_typ_hash_drift_returns_schema_mismatch() {
    let dir = tempdir().unwrap();

    // Open with HashItemA (value: u64), VERSION=1.
    {
        let db = Db::open_test(dir.path()).unwrap();
        let _tree = db
            .open_typed_tree::<HashItemA, RapiraCodec, _>(Config::test(), NoHook, &[])
            .unwrap();
        db.close().unwrap();
    }

    // Reopen with HashItemB (value: u64, extra: u32) — same VERSION=1 but
    // different struct layout → different GetType hash.
    let db = Db::open_test(dir.path()).unwrap();
    let err =
        unwrap_err(db.open_typed_tree::<HashItemB, RapiraCodec, _>(Config::test(), NoHook, &[]));
    match err {
        DbError::SchemaMismatch {
            kind: SchemaMismatchKind::TypHash { stored, expected },
            ..
        } => {
            assert_ne!(stored, expected, "hashes must differ");
        }
        other => panic!("expected SchemaMismatch::TypHash, got: {other}"),
    }
}

/// First open of a fresh directory (no prior CollectionInfo) with any VERSION
/// and an empty migrations list must succeed, and the stored version must
/// equal the type's VERSION constant.
#[test]
fn migration_first_open_no_step_required() {
    let dir = tempdir().unwrap();

    // Open V2 on a fresh dir with no migrations.
    {
        let db = Db::open_test(dir.path()).unwrap();
        let _tree = db
            .open_typed_tree::<ItemV2, RapiraCodec, _>(Config::test(), NoHook, &[])
            .unwrap();
        db.close().unwrap();
    }

    // Verify stored version is 2.
    let db = Db::open_test(dir.path()).unwrap();
    let stored = db.db_info().collections;
    let info = stored
        .get("mig_test_items")
        .expect("collection info must exist");
    assert_eq!(info.version, 2, "stored version must match V2::VERSION");
}

/// Per-step version commits (resumability): if db.info already records
/// stored.version=2 (the V1→V2 step committed before a crash), only the
/// V2→V3 step must run on the next open — the V1→V2 step must not repeat.
#[test]
fn migration_resumes_from_intermediate_version() {
    let dir = tempdir().unwrap();

    // step_add_10 represents V1→V2: adds 10.
    fn add_ten(_k: &[u8; 8], v: &ItemV3) -> MigrateAction<ItemV3> {
        MigrateAction::Update(ItemV3 {
            value: v.value + 10,
        })
    }
    // step_add_100 represents V2→V3: adds 100.
    fn add_hundred(_k: &[u8; 8], v: &ItemV3) -> MigrateAction<ItemV3> {
        MigrateAction::Update(ItemV3 {
            value: v.value + 100,
        })
    }

    #[allow(clippy::type_complexity)]
    let migrations: &[(u16, fn(&[u8; 8], &ItemV3) -> MigrateAction<ItemV3>)] =
        &[(1, add_ten), (2, add_hundred)];

    // Phase 1: open V1, write one entry (value=1), then patch db.info to
    // version=2 to simulate a crash after the V1→V2 step committed.
    {
        let db = Db::open_test(dir.path()).unwrap();
        let tree = db
            .open_typed_tree::<ItemV1, RapiraCodec, _>(Config::test(), NoHook, &[])
            .unwrap();
        tree.put(&key(0), ItemV1 { value: 1 }).unwrap();
        // Simulate: V1→V2 committed version=2 but V2→V3 did not run.
        db.db_info_update(|info| {
            if let Some(ci) = info.collections.get_mut(ItemV1::NAME) {
                ci.version = 2;
            }
        })
        .expect("test persist");
        db.close().unwrap();
    }

    // Phase 2: open with V3 and both migration steps.
    // Only V2→V3 (+100) must run; V1→V2 (+10) must not repeat.
    // Expected final value: 1 + 100 = 101, NOT 1 + 10 + 100 = 111.
    {
        let db = Db::open_test(dir.path()).unwrap();
        let tree = db
            .open_typed_tree::<ItemV3, RapiraCodec, _>(Config::test(), NoHook, migrations)
            .unwrap();
        assert_eq!(
            tree.get(&key(0)).unwrap().value,
            101,
            "only V2→V3 step should run; expected 1 + 100 = 101"
        );
    }
}

// --- D6: sentinel typ_hash on intermediate migration versions ---

/// Type for sentinel tests. VERSION=3, migrations registered partially
/// to get interrupted multi-step migration without crash simulation.
#[derive(Clone, Debug, PartialEq, Rapira, GetType)]
struct SentinelItemV3 {
    value: i64,
}
impl CollectionMeta for SentinelItemV3 {
    type SelfId = [u8; 8];
    const NAME: &'static str = "sentinel_items";
    const VERSION: u16 = 3;
}

/// Same NAME/type, VERSION=2 — "intermediate version binary" for rollback.
#[derive(Clone, Debug, PartialEq, Rapira, GetType)]
struct SentinelItemV2 {
    value: i64,
}
impl CollectionMeta for SentinelItemV2 {
    type SelfId = [u8; 8];
    const NAME: &'static str = "sentinel_items";
    const VERSION: u16 = 2;
}

fn keep_v3(_k: &[u8; 8], _v: &SentinelItemV3) -> MigrateAction<SentinelItemV3> {
    MigrateAction::Keep
}

#[test]
fn interrupted_multistep_migration_writes_sentinel_and_allows_rollback() {
    let dir = tempdir().unwrap();

    // Phase 1: v3 data, patch db.info → stored.version=1.
    {
        let db = Db::open_test(dir.path()).unwrap();
        let tree = db
            .open_typed_tree::<SentinelItemV3, RapiraCodec, _>(Config::test(), NoHook, &[])
            .unwrap();
        for i in 0u64..3 {
            tree.put(&i.to_be_bytes(), SentinelItemV3 { value: i as i64 })
                .unwrap();
        }
        db.db_info_update(|info| {
            let e = info.collections.get_mut(SentinelItemV3::NAME).unwrap();
            e.version = 1;
        })
        .unwrap();
    }

    // Phase 2: v3 binary with INCOMPLETE migrations [(1, keep)] —
    // step 1→2 commits, step 2→3 fails MissingStep.
    {
        let db = Db::open_test(dir.path()).unwrap();
        let migrations: &[TypedMigration<[u8; 8], SentinelItemV3>] = &[(1u16, keep_v3)];
        let err = unwrap_err(db.open_typed_tree::<SentinelItemV3, RapiraCodec, _>(
            Config::test(),
            NoHook,
            migrations,
        ));
        assert!(
            matches!(
                err,
                DbError::SchemaMismatch {
                    kind: SchemaMismatchKind::MissingStep { from: 2 },
                    ..
                }
            ),
            "unexpected: {err:?}"
        );
        let info = db.db_info();
        let e = &info.collections[SentinelItemV3::NAME];
        assert_eq!(e.version, 2);
        assert_eq!(e.typ_hash, armdb::armour::TYP_HASH_MIGRATING);
    }

    // Phase 3: rollback — v2 binary opens {version:2, typ_hash:sentinel}
    // without SchemaMismatch (V-005 skipped) and self-heals via save_info.
    {
        let db = Db::open_test(dir.path()).unwrap();
        let tree = db
            .open_typed_tree::<SentinelItemV2, RapiraCodec, _>(Config::test(), NoHook, &[])
            .unwrap();
        assert_eq!(tree.len(), 3);
        let info = db.db_info();
        let e = &info.collections[SentinelItemV2::NAME];
        assert_eq!(e.version, 2);
        assert_ne!(
            e.typ_hash,
            armdb::armour::TYP_HASH_MIGRATING,
            "save_info must self-heal"
        );
    }
}

#[test]
fn v005_still_fires_on_real_typ_hash_mismatch() {
    let dir = tempdir().unwrap();
    {
        let db = Db::open_test(dir.path()).unwrap();
        let _ = db
            .open_typed_tree::<SentinelItemV2, RapiraCodec, _>(Config::test(), NoHook, &[])
            .unwrap();
        db.db_info_update(|info| {
            info.collections
                .get_mut(SentinelItemV2::NAME)
                .unwrap()
                .typ_hash = 12345;
        })
        .unwrap();
    }
    let db = Db::open_test(dir.path()).unwrap();
    let err = unwrap_err(db.open_typed_tree::<SentinelItemV2, RapiraCodec, _>(
        Config::test(),
        NoHook,
        &[],
    ));
    assert!(
        matches!(
            err,
            DbError::SchemaMismatch {
                kind: SchemaMismatchKind::TypHash { .. },
                ..
            }
        ),
        "unexpected: {err:?}"
    );
}