aeternusdb 1.0.0

An embeddable, persistent key-value store built on an LSM-tree architecture.
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//! Crash-recovery tests: verify data survives drop without close().
//!
//! These tests exercise the frozen-WAL-replay path in `Engine::open()` that
//! is **never** reached by the normal close-then-reopen tests (because
//! `close()` flushes all frozen memtables to SSTables first).
//!
//! A real crash (power failure, OOM kill, SIGKILL) leaves the engine in one
//! of several states: (a) only the active WAL has data, (b) one or more
//! frozen memtables exist alongside SSTables, or (c) a mix of puts, deletes,
//! and range-deletes are spread across all three layers. Every test simulates
//! a crash by dropping the `Engine` struct without calling `close()`, then
//! reopens and verifies that all committed data is recovered via WAL replay.
//!
//! ## Layer coverage
//! - `memtable__*`: active WAL only (no freeze triggered — tests WAL replay)
//! - `memtable_sstable__*`: frozen memtables + SSTables survive crash
//!   (tests frozen-WAL replay combined with SSTable reads)
//!
//! ## See also
//! - [`tests_recovery`] — clean close → reopen path
//! - [`tests_stress`] `*crash*` — crash recovery under heavy load

#[cfg(test)]
#[allow(non_snake_case)]
mod tests {
    use crate::engine::Engine;
    use crate::engine::tests::helpers::*;
    use tempfile::TempDir;

    // ----------------------------------------------------------------
    // Local helpers
    // ----------------------------------------------------------------

    /// Write puts in a loop until the engine has at least one frozen memtable.
    /// Returns how many keys were written (0..count).
    fn fill_until_frozen(engine: &Engine, prefix: &str) -> usize {
        let mut i = 0;
        loop {
            let key = format!("{}_{:04}", prefix, i).into_bytes();
            let value = format!("val_{:04}", i).into_bytes();
            engine.put(key, value).expect("put");
            let stats = engine.stats().expect("stats");
            if stats.frozen_count > 0 {
                return i + 1;
            }
            i += 1;
            assert!(i < 10_000, "Expected FlushRequired within 10 000 puts");
        }
    }

    /// Write puts in a loop until the engine has at least one SSTable AND
    /// at least one frozen memtable (i.e. all three layers are populated).
    fn fill_until_sstable_and_frozen(engine: &Engine, prefix: &str) -> usize {
        // Phase 1: write until we get frozen memtables
        let count1 = fill_until_frozen(engine, prefix);
        // Phase 2: flush all frozen → creates SSTables
        engine.flush_all_frozen().expect("flush");
        // Phase 3: keep writing until we have another frozen memtable
        let mut i = count1;
        loop {
            let key = format!("{}_{:04}", prefix, i).into_bytes();
            let value = format!("val_{:04}", i).into_bytes();
            engine.put(key, value).expect("put");
            i += 1;
            let stats = engine.stats().expect("stats");
            if stats.frozen_count >= 1 {
                return i;
            }
            assert!(i < 10_000, "Expected SSTable + frozen within 10 000 puts");
        }
    }

    /// Flush all frozen memtables to SSTables.
    fn drain_frozen(engine: &Engine) {
        engine.flush_all_frozen().expect("flush_all_frozen");
    }

    // ================================================================
    // 1. Active WAL only — no freeze triggered
    // ================================================================

    /// # Scenario
    /// Active WAL data (puts only, no freeze) survives a simulated crash.
    ///
    /// # Starting environment
    /// Fresh engine with memtable-only config (64 KB buffer) — no frozen
    /// memtables, no SSTables.
    ///
    /// # Actions
    /// 1. Put 3 keys (`k1`, `k2`, `k3`).
    /// 2. Verify frozen_count = 0 and sstables_count = 0.
    /// 3. Drop the engine without calling `close()` (simulates crash).
    /// 4. Reopen and get all 3 keys.
    ///
    /// # Expected behavior
    /// All 3 keys are recovered — the active WAL is replayed into a new
    /// memtable during `Engine::open()`.
    #[test]
    fn memtable__crash_recovery_active_wal_puts() {
        let dir = TempDir::new().unwrap();
        {
            let engine = Engine::open(dir.path(), memtable_only_config()).expect("open");
            engine.put(b"k1".to_vec(), b"v1".to_vec()).unwrap();
            engine.put(b"k2".to_vec(), b"v2".to_vec()).unwrap();
            engine.put(b"k3".to_vec(), b"v3".to_vec()).unwrap();

            let stats = engine.stats().unwrap();
            assert_eq!(stats.frozen_count, 0);
            assert_eq!(stats.sstables_count, 0);
            // Drop without close — simulates crash
        }

        let engine = reopen(dir.path());
        assert_eq!(engine.get(b"k1".to_vec()).unwrap(), Some(b"v1".to_vec()));
        assert_eq!(engine.get(b"k2".to_vec()).unwrap(), Some(b"v2".to_vec()));
        assert_eq!(engine.get(b"k3".to_vec()).unwrap(), Some(b"v3".to_vec()));
    }

    // ================================================================
    // 2. Frozen memtable survives crash (frozen-WAL replay path)
    // ================================================================

    /// # Scenario
    /// Data in a frozen memtable (not yet flushed to SSTable) survives crash.
    ///
    /// # Starting environment
    /// Engine with small buffer (128 bytes) — writes fill up quickly.
    ///
    /// # Actions
    /// 1. Write keys until `frozen_count >= 1` (via `fill_until_frozen`).
    /// 2. Drop without `close()` — frozen memtable NOT flushed to SSTable.
    /// 3. Reopen and get all written keys.
    ///
    /// # Expected behavior
    /// Every key is recovered — the frozen memtable's WAL is replayed.
    #[test]
    fn memtable__crash_recovery_with_frozen() {
        let dir = TempDir::new().unwrap();
        let count;
        {
            let engine = Engine::open(dir.path(), small_buffer_config()).expect("open");
            count = fill_until_frozen(&engine, "key");

            let stats = engine.stats().unwrap();
            assert!(
                stats.frozen_count >= 1,
                "Expected at least 1 frozen memtable, got {}",
                stats.frozen_count
            );
            // Drop without close — frozen memtable NOT flushed to SSTable
        }

        let engine = reopen(dir.path());
        for i in 0..count {
            let key = format!("key_{:04}", i).into_bytes();
            let expected = format!("val_{:04}", i).into_bytes();
            assert_eq!(
                engine.get(key).unwrap(),
                Some(expected),
                "Missing key_{:04} after crash recovery",
                i
            );
        }
    }

    // ================================================================
    // 3. Frozen memtable + SSTables survive crash
    // ================================================================

    /// # Scenario
    /// All three layers (active memtable + frozen memtable + SSTables)
    /// are populated, then the engine crashes.
    ///
    /// # Starting environment
    /// Engine with small buffer (128 bytes).
    ///
    /// # Actions
    /// 1. Write keys until both `sstables_count > 0` AND `frozen_count >= 1`
    ///    (via `fill_until_sstable_and_frozen`).
    /// 2. Drop without `close()`.
    /// 3. Reopen and get all written keys.
    ///
    /// # Expected behavior
    /// All keys are recovered — data from SSTables, the replayed frozen WAL,
    /// and the active WAL are all merged correctly.
    #[test]
    fn memtable_sstable__crash_recovery_frozen_and_sstable() {
        let dir = TempDir::new().unwrap();
        let count;
        {
            let engine = Engine::open(dir.path(), small_buffer_config()).expect("open");
            count = fill_until_sstable_and_frozen(&engine, "key");

            let stats = engine.stats().unwrap();
            assert!(stats.sstables_count > 0, "Expected at least 1 SSTable");
            assert!(
                stats.frozen_count >= 1,
                "Expected at least 1 frozen memtable, got {}",
                stats.frozen_count
            );
            // Drop without close — all three layers populated
        }

        let engine = reopen(dir.path());
        for i in 0..count {
            let key = format!("key_{:04}", i).into_bytes();
            let expected = format!("val_{:04}", i).into_bytes();
            assert_eq!(
                engine.get(key).unwrap(),
                Some(expected),
                "Missing key_{:04} after crash with SSTable + frozen",
                i
            );
        }
    }

    // ================================================================
    // 4. Delete tombstones in active WAL survive crash
    // ================================================================

    /// # Scenario
    /// Delete tombstones in the active WAL survive a crash.
    ///
    /// # Starting environment
    /// Fresh engine with memtable-only config (no freeze triggered).
    ///
    /// # Actions
    /// 1. Put `"keep"` = `"yes"` and `"gone"` = `"bye"`.
    /// 2. Delete `"gone"`.
    /// 3. Verify frozen_count = 0.
    /// 4. Drop without `close()`.
    /// 5. Reopen and get both keys.
    ///
    /// # Expected behavior
    /// `"keep"` returns `Some("yes")`; `"gone"` returns `None` — the
    /// tombstone in the WAL is replayed correctly.
    #[test]
    fn memtable__crash_recovery_delete_in_active_wal() {
        let dir = TempDir::new().unwrap();
        {
            let engine = Engine::open(dir.path(), memtable_only_config()).expect("open");
            engine.put(b"keep".to_vec(), b"yes".to_vec()).unwrap();
            engine.put(b"gone".to_vec(), b"bye".to_vec()).unwrap();
            engine.delete(b"gone".to_vec()).unwrap();

            let stats = engine.stats().unwrap();
            assert_eq!(stats.frozen_count, 0);
            // Drop without close
        }

        let engine = reopen(dir.path());
        assert_eq!(engine.get(b"keep".to_vec()).unwrap(), Some(b"yes".to_vec()));
        assert_eq!(engine.get(b"gone".to_vec()).unwrap(), None);
    }

    // ================================================================
    // 5. Range-delete tombstones in active WAL survive crash
    // ================================================================

    /// # Scenario
    /// Range-delete tombstones in the active WAL survive a crash.
    ///
    /// # Starting environment
    /// Fresh engine with memtable-only config (no freeze triggered).
    ///
    /// # Actions
    /// 1. Put 10 two-byte keys `[k, 0]` through `[k, 9]`.
    /// 2. Range-delete `[k\x03, k\x07)` — covers keys 3–6.
    /// 3. Verify frozen_count = 0.
    /// 4. Drop without `close()`.
    /// 5. Reopen and get all 10 keys.
    ///
    /// # Expected behavior
    /// Keys 3–6 return `None`; all others return their original values.
    /// The range tombstone in the WAL is correctly replayed.
    #[test]
    fn memtable__crash_recovery_range_delete_in_wal() {
        let dir = TempDir::new().unwrap();
        {
            let engine = Engine::open(dir.path(), memtable_only_config()).expect("open");
            for i in 0..10u8 {
                engine
                    .put(vec![b'k', i], format!("v{}", i).into_bytes())
                    .unwrap();
            }
            // Range-delete keys [k\x03, k\x07)
            engine.delete_range(vec![b'k', 3], vec![b'k', 7]).unwrap();

            let stats = engine.stats().unwrap();
            assert_eq!(stats.frozen_count, 0);
            // Drop without close
        }

        let engine = reopen(dir.path());
        for i in 0..10u8 {
            let val = engine.get(vec![b'k', i]).unwrap();
            if (3..7).contains(&i) {
                assert_eq!(val, None, "k{} should be range-deleted", i);
            } else {
                assert_eq!(
                    val,
                    Some(format!("v{}", i).into_bytes()),
                    "k{} should survive",
                    i
                );
            }
        }
    }

    // ================================================================
    // 6. Delete tombstones in frozen memtable survive crash
    // ================================================================

    /// # Scenario
    /// Delete tombstones in a frozen memtable survive a crash.
    ///
    /// # Starting environment
    /// Engine with small buffer; keys 0–49 are inserted and drained to
    /// SSTables (frozen_count = 0, sstables_count > 0).
    ///
    /// # Actions
    /// 1. Phase 1: populate 50 keys into SSTables.
    /// 2. Phase 2: drain all frozen memtables to SSTables.
    /// 3. Phase 3: issue point deletes until `frozen_count >= 1` (the frozen
    ///    memtable now contains tombstones).
    /// 4. Drop without `close()`.
    /// 5. Reopen and get all 50 keys.
    ///
    /// # Expected behavior
    /// Deleted keys return `None`; non-deleted keys return their values.
    /// The tombstones in the frozen memtable’s WAL are correctly replayed.
    #[test]
    fn memtable_sstable__crash_recovery_frozen_with_deletes() {
        let dir = TempDir::new().unwrap();
        let mut deleted_keys = Vec::new();
        {
            let engine = Engine::open(dir.path(), small_buffer_config()).expect("open");

            // Phase 1: write keys into SSTables
            for i in 0..50u32 {
                engine
                    .put(
                        format!("key_{:04}", i).into_bytes(),
                        format!("val_{:04}", i).into_bytes(),
                    )
                    .unwrap();
            }

            // Phase 2: drain any lingering frozen memtable
            drain_frozen(&engine);
            let stats = engine.stats().unwrap();
            assert!(stats.sstables_count > 0, "Expected SSTables from phase 1");
            assert_eq!(stats.frozen_count, 0);

            // Phase 3: issue deletes until a frozen memtable with tombstones appears
            for i in 0..50u32 {
                let key = format!("key_{:04}", i).into_bytes();
                engine.delete(key).unwrap();
                deleted_keys.push(i);
                if engine.stats().unwrap().frozen_count > 0 {
                    break;
                }
            }

            let stats = engine.stats().unwrap();
            assert!(
                stats.frozen_count >= 1,
                "Expected at least 1 frozen memtable with delete tombstones, got {}",
                stats.frozen_count
            );
            // Drop without close
        }

        let engine = reopen(dir.path());
        for i in 0..50u32 {
            let key = format!("key_{:04}", i).into_bytes();
            if deleted_keys.contains(&i) {
                assert_eq!(
                    engine.get(key).unwrap(),
                    None,
                    "key_{:04} should be deleted after crash",
                    i
                );
            } else {
                assert_eq!(
                    engine.get(key).unwrap(),
                    Some(format!("val_{:04}", i).into_bytes()),
                    "key_{:04} should still exist after crash",
                    i
                );
            }
        }
    }

    // ================================================================
    // 7. Range-delete tombstones in frozen memtable survive crash
    // ================================================================

    /// # Scenario
    /// Range-delete tombstones in a frozen memtable survive a crash.
    ///
    /// # Starting environment
    /// Engine with small buffer; 50 keys populated and drained to SSTables.
    ///
    /// # Actions
    /// 1. Phase 1: populate 50 keys into SSTables.
    /// 2. Phase 2: drain frozen memtables.
    /// 3. Phase 3: issue range-delete `[key_0010, key_0020)`, then write
    ///    filler until `frozen_count >= 1`.
    /// 4. Drop without `close()`.
    /// 5. Reopen and get keys inside and outside the deleted range.
    ///
    /// # Expected behavior
    /// Keys 10–19: `None` (range-deleted). Keys 0–9 and 20–49: present.
    /// The range tombstone persisted in the frozen WAL is correctly replayed.
    #[test]
    fn memtable_sstable__crash_recovery_frozen_with_range_deletes() {
        let dir = TempDir::new().unwrap();
        {
            let engine = Engine::open(dir.path(), small_buffer_config()).expect("open");

            // Phase 1: populate SSTables
            for i in 0..50u32 {
                engine
                    .put(
                        format!("key_{:04}", i).into_bytes(),
                        format!("val_{:04}", i).into_bytes(),
                    )
                    .unwrap();
            }

            // Phase 2: drain frozen
            drain_frozen(&engine);
            assert!(engine.stats().unwrap().sstables_count > 0);
            assert_eq!(engine.stats().unwrap().frozen_count, 0);

            // Phase 3: issue range delete, then fill until frozen_count >= 1
            engine
                .delete_range(b"key_0010".to_vec(), b"key_0020".to_vec())
                .unwrap();

            let mut filler = 0u32;
            while engine.stats().unwrap().frozen_count == 0 {
                engine
                    .put(
                        format!("fill_{:04}", filler).into_bytes(),
                        format!("fval_{:04}", filler).into_bytes(),
                    )
                    .unwrap();
                filler += 1;
                assert!(filler < 1_000, "Expected FlushRequired for filler puts");
            }

            let stats = engine.stats().unwrap();
            assert!(
                stats.frozen_count >= 1,
                "Expected at least 1 frozen memtable with range tombstone, got {}",
                stats.frozen_count
            );
            // Drop without close
        }

        let engine = reopen(dir.path());
        // Keys in the range-deleted interval should be gone
        for i in 10..20u32 {
            let key = format!("key_{:04}", i).into_bytes();
            assert_eq!(
                engine.get(key).unwrap(),
                None,
                "key_{:04} should be range-deleted after crash",
                i
            );
        }
        // Keys outside the interval should survive
        for i in (0..10).chain(20..50) {
            let key = format!("key_{:04}", i).into_bytes();
            assert_eq!(
                engine.get(key).unwrap(),
                Some(format!("val_{:04}", i).into_bytes()),
                "key_{:04} should survive crash",
                i
            );
        }
    }

    // ================================================================
    // 8. Scan returns correct results after crash (no close)
    // ================================================================

    /// # Scenario
    /// Scan returns correct, sorted results after a crash with all three
    /// storage layers populated.
    ///
    /// # Starting environment
    /// Engine with small buffer.
    ///
    /// # Actions
    /// 1. Write keys until `sstables_count > 0` AND `frozen_count >= 1`.
    /// 2. Drop without `close()`.
    /// 3. Reopen and scan the full key range.
    ///
    /// # Expected behavior
    /// Scan returns all written keys in sorted order. The count matches the
    /// total keys written before the crash.
    #[test]
    fn memtable_sstable__crash_recovery_scan_correct() {
        let dir = TempDir::new().unwrap();
        let total;
        {
            let engine = Engine::open(dir.path(), small_buffer_config()).expect("open");
            total = fill_until_sstable_and_frozen(&engine, "sk");

            let stats = engine.stats().unwrap();
            assert!(stats.sstables_count > 0);
            assert!(
                stats.frozen_count >= 1,
                "Expected at least 1 frozen memtable, got {}",
                stats.frozen_count
            );
            // Drop without close
        }

        let engine = reopen(dir.path());
        let results = collect_scan(&engine, b"sk_", b"sk_\xff");
        assert_eq!(
            results.len(),
            total,
            "scan should return all {} keys after crash",
            total
        );
        // Verify sorted order
        for pair in results.windows(2) {
            assert!(pair[0].0 < pair[1].0, "scan must be sorted after crash");
        }
    }

    // ================================================================
    // 9. Mixed ops (puts + deletes + range deletes) across all layers,
    //    crash, reopen, full verification via get + scan.
    // ================================================================

    /// # Scenario
    /// Mixed operations (puts + point deletes + range deletes + overwrites)
    /// across all three layers survive a crash, verified via both get and scan.
    ///
    /// # Starting environment
    /// Engine with small buffer (128 bytes).
    ///
    /// # Actions
    /// 1. Put 50 keys.
    /// 2. Point-delete `key_0005` and `key_0015`.
    /// 3. Range-delete `[key_0030, key_0040)`.
    /// 4. Overwrite `key_0002` with a new value.
    /// 5. Write padding to ensure `frozen_count >= 1` and `sstables_count > 0`.
    /// 6. Drop without `close()`.
    /// 7. Reopen; get individual keys and scan the full range.
    ///
    /// # Expected behavior
    /// - Point-deleted keys (5, 15): `None`.
    /// - Range-deleted keys (30–39): `None`.
    /// - Overwritten key (2): returns new value.
    /// - Surviving keys: return original values.
    /// - Scan is sorted and excludes all deleted keys.
    #[test]
    fn memtable_sstable__crash_recovery_mixed_ops() {
        let dir = TempDir::new().unwrap();
        {
            let engine = Engine::open(dir.path(), small_buffer_config()).expect("open");

            // Puts: key_0000..key_0049
            for i in 0..50u32 {
                engine
                    .put(
                        format!("key_{:04}", i).into_bytes(),
                        format!("val_{:04}", i).into_bytes(),
                    )
                    .unwrap();
            }

            // Point-delete a few
            engine.delete(b"key_0005".to_vec()).unwrap();
            engine.delete(b"key_0015".to_vec()).unwrap();

            // Range-delete [key_0030, key_0040)
            engine
                .delete_range(b"key_0030".to_vec(), b"key_0040".to_vec())
                .unwrap();

            // Overwrite one key
            engine
                .put(b"key_0002".to_vec(), b"new_val_0002".to_vec())
                .unwrap();

            // Flush all frozen to SSTables
            engine.flush_all_frozen().unwrap();

            // Ensure we have at least one frozen memtable for crash testing
            if engine.stats().unwrap().frozen_count == 0 {
                let mut j = 0u32;
                while engine.stats().unwrap().frozen_count == 0 {
                    engine
                        .put(
                            format!("pad_{:04}", j).into_bytes(),
                            format!("pval_{:04}", j).into_bytes(),
                        )
                        .unwrap();
                    j += 1;
                }
            }

            let stats = engine.stats().unwrap();
            assert!(stats.sstables_count > 0, "Expected SSTables");
            assert!(
                stats.frozen_count >= 1,
                "Expected at least 1 frozen memtable, got {}",
                stats.frozen_count
            );
            // Drop without close
        }

        let engine = reopen(dir.path());

        // Verify point deletes
        assert_eq!(engine.get(b"key_0005".to_vec()).unwrap(), None);
        assert_eq!(engine.get(b"key_0015".to_vec()).unwrap(), None);

        // Verify range deletes
        for i in 30..40u32 {
            assert_eq!(
                engine.get(format!("key_{:04}", i).into_bytes()).unwrap(),
                None,
                "key_{:04} should be range-deleted",
                i
            );
        }

        // Verify overwrite
        assert_eq!(
            engine.get(b"key_0002".to_vec()).unwrap(),
            Some(b"new_val_0002".to_vec())
        );

        // Verify surviving keys
        for i in [0u32, 1, 3, 4, 6, 7, 10, 20, 25, 40, 41, 49] {
            assert_eq!(
                engine.get(format!("key_{:04}", i).into_bytes()).unwrap(),
                Some(format!("val_{:04}", i).into_bytes()),
                "key_{:04} should survive crash",
                i
            );
        }

        // Scan should be sorted and consistent
        let results = collect_scan(&engine, b"key_", b"key_\xff");
        for pair in results.windows(2) {
            assert!(pair[0].0 < pair[1].0, "scan must be sorted");
        }
        // Deleted keys must not appear in scan
        let keys_in_scan: Vec<&Vec<u8>> = results.iter().map(|(k, _)| k).collect();
        assert!(!keys_in_scan.contains(&&b"key_0005".to_vec()));
        assert!(!keys_in_scan.contains(&&b"key_0015".to_vec()));
        for i in 30..40u32 {
            assert!(!keys_in_scan.contains(&&format!("key_{:04}", i).into_bytes()));
        }
    }
}