aeternusdb 1.0.1

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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//! Major compaction tests.

#[cfg(test)]
mod tests {
    use crate::engine::{Engine, EngineConfig};
    use std::fs;

    fn compaction_config() -> EngineConfig {
        let _ = tracing_subscriber::fmt()
            .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
            .with_test_writer()
            .try_init();
        EngineConfig {
            write_buffer_size: 256,
            compaction_strategy: crate::compaction::CompactionStrategyType::Stcs,
            bucket_low: 0.5,
            bucket_high: 1.5,
            min_sstable_size: 50,
            min_threshold: 100,
            max_threshold: 200,
            tombstone_ratio_threshold: 0.2,
            tombstone_compaction_interval: 0,
            tombstone_bloom_fallback: false,
            tombstone_range_drop: false,
            thread_pool_size: 2,
        }
    }

    fn fresh_dir(name: &str) -> String {
        let path = format!("/tmp/aeternusdb_test_compaction_major_{}", name);
        let _ = fs::remove_dir_all(&path);
        path
    }

    /// # Scenario
    /// Major compaction merges all SSTables into exactly one,
    /// deduplicating and dropping all tombstones.
    ///
    /// # Starting environment
    /// Empty engine, 256 B write buffer.
    ///
    /// # Actions
    /// 1. Write 100 keys (8 B key + 8 B value), flush.
    /// 2. Record `before` stats.
    /// 3. `major_compact()`.
    /// 4. Record `after` stats.
    ///
    /// # Expected behavior
    /// - Returns `true`.
    /// - Exactly 1 SSTable remains.
    /// - Total SST size decreases (per-SSTable overhead eliminated).
    /// - All 100 keys readable.
    #[test]
    fn major_compact_merges_all_sstables_into_one() {
        let dir = fresh_dir("basic");
        let engine = Engine::open(&dir, compaction_config()).unwrap();

        for i in 0..100 {
            let key = format!("key_{:04}", i).into_bytes();
            let val = format!("val_{:04}", i).into_bytes();
            engine.put(key, val).unwrap();
        }
        engine.flush_all_frozen().unwrap();

        let before = engine.stats().unwrap();
        assert!(
            before.sstables_count >= 2,
            "need at least 2 SSTables, got {}",
            before.sstables_count
        );

        let compacted = engine.major_compact().unwrap();
        assert!(compacted, "major_compact should have run");

        let after = engine.stats().unwrap();
        assert_eq!(
            after.sstables_count, 1,
            "should have exactly 1 SSTable after major compaction, got {} (was {})",
            after.sstables_count, before.sstables_count,
        );
        // Merging many small SSTables into 1 eliminates per-SSTable overhead;
        // expect at least 20 % size reduction.
        let max_size = (before.total_sst_size_bytes as f64 * 0.80) as u64;
        assert!(
            after.total_sst_size_bytes <= max_size,
            "total SST size should decrease by ≥ 20 %: before={} B, after={} B, max allowed={} B",
            before.total_sst_size_bytes,
            after.total_sst_size_bytes,
            max_size,
        );

        for i in 0..100 {
            let key = format!("key_{:04}", i).into_bytes();
            let expected = format!("val_{:04}", i).into_bytes();
            assert_eq!(engine.get(key).unwrap(), Some(expected));
        }
    }

    /// # Scenario
    /// Major compaction is a no-op with 0 or 1 SSTables.
    ///
    /// # Starting environment
    /// Empty engine, 256 B write buffer.
    ///
    /// # Actions
    /// 1. `major_compact()` on empty engine.
    /// 2. Write 6 keys (fits in 1 SSTable after flush), flush.
    /// 3. `major_compact()` again.
    ///
    /// # Expected behavior
    /// - Both calls return `false`.
    /// - SSTable count and total size unchanged after each call.
    #[test]
    fn major_compact_returns_false_with_zero_or_one_sstable() {
        let dir = fresh_dir("noop_empty");
        let engine = Engine::open(&dir, compaction_config()).unwrap();

        let compacted = engine.major_compact().unwrap();
        assert!(!compacted, "no SSTables — should not compact");
        assert_eq!(engine.stats().unwrap().sstables_count, 0);

        for i in 0..6 {
            let key = format!("key_{:04}", i).into_bytes();
            engine.put(key, b"val".to_vec()).unwrap();
        }
        engine.flush_all_frozen().unwrap();

        let before = engine.stats().unwrap();
        assert_eq!(before.sstables_count, 1);

        let compacted = engine.major_compact().unwrap();
        assert!(!compacted, "1 SSTable — should not compact");

        let after = engine.stats().unwrap();
        assert_eq!(after.sstables_count, 1, "SSTable count should be unchanged");
        assert_eq!(
            after.total_sst_size_bytes, before.total_sst_size_bytes,
            "total size should be unchanged"
        );
    }

    /// # Scenario
    /// Major compaction drops all point tombstones because the merged
    /// output is the single authoritative SSTable — nothing to shadow.
    ///
    /// # Starting environment
    /// Empty engine, 256 B write buffer.
    ///
    /// # Actions
    /// 1. Write 30 keys (8 B key + 3 B value), flush.
    /// 2. Delete keys 0..15, flush.
    /// 3. Record `before` stats.
    /// 4. `major_compact()`.
    /// 5. Record `after` stats.
    ///
    /// # Expected behavior
    /// - Exactly 1 SSTable remains.
    /// - Total SST size decreases (tombstones + dead data eliminated).
    /// - Deleted keys 0..15 return `None`; live keys 15..30 return values.
    #[test]
    fn major_compact_drops_point_tombstones() {
        let dir = fresh_dir("drops_tombstones");
        let engine = Engine::open(&dir, compaction_config()).unwrap();

        for i in 0..30 {
            let key = format!("key_{:04}", i).into_bytes();
            engine.put(key, b"val".to_vec()).unwrap();
        }
        engine.flush_all_frozen().unwrap();

        for i in 0..15 {
            let key = format!("key_{:04}", i).into_bytes();
            engine.delete(key).unwrap();
        }
        engine.flush_all_frozen().unwrap();

        let before = engine.stats().unwrap();
        assert!(
            before.sstables_count >= 2,
            "need at least 2 SSTables, got {}",
            before.sstables_count
        );

        engine.major_compact().unwrap();

        let after = engine.stats().unwrap();
        assert_eq!(
            after.sstables_count, 1,
            "should have 1 SSTable after major compaction (was {})",
            before.sstables_count
        );
        // Half the keys were deleted — expect at least 30 % size reduction
        // (dead puts + tombstones removed).
        let max_size = (before.total_sst_size_bytes as f64 * 0.70) as u64;
        assert!(
            after.total_sst_size_bytes <= max_size,
            "total SST size should decrease by ≥ 30 % (tombstones + dead data dropped): before={} B, after={} B, max allowed={} B",
            before.total_sst_size_bytes,
            after.total_sst_size_bytes,
            max_size,
        );

        for i in 0..15 {
            let key = format!("key_{:04}", i).into_bytes();
            assert_eq!(engine.get(key).unwrap(), None, "key_{i:04} should be gone");
        }
        for i in 15..30 {
            let key = format!("key_{:04}", i).into_bytes();
            assert_eq!(engine.get(key).unwrap(), Some(b"val".to_vec()));
        }
    }

    /// # Scenario
    /// Major compaction applies range tombstones, suppressing all covered
    /// Puts with lower LSN and producing a single clean SSTable.
    ///
    /// # Starting environment
    /// Empty engine, 256 B write buffer.
    ///
    /// # Actions
    /// 1. Write 50 keys, flush.
    /// 2. `delete_range("key_0020", "key_0040")`, flush.
    /// 3. Record `before` stats.
    /// 4. `major_compact()`.
    /// 5. Record `after` stats.
    ///
    /// # Expected behavior
    /// - Exactly 1 SSTable.
    /// - Total SST size decreases (dead data + range tombstone dropped).
    /// - Keys 20..40 return `None`; other keys intact.
    #[test]
    fn major_compact_applies_range_tombstones() {
        let dir = fresh_dir("range_tombstones");
        let engine = Engine::open(&dir, compaction_config()).unwrap();

        for i in 0..50 {
            let key = format!("key_{:04}", i).into_bytes();
            engine.put(key, b"val".to_vec()).unwrap();
        }
        engine.flush_all_frozen().unwrap();

        engine
            .delete_range(b"key_0020".to_vec(), b"key_0040".to_vec())
            .unwrap();
        engine.flush_all_frozen().unwrap();

        let before = engine.stats().unwrap();
        assert!(
            before.sstables_count >= 2,
            "need at least 2 SSTables, got {}",
            before.sstables_count
        );

        engine.major_compact().unwrap();

        let after = engine.stats().unwrap();
        assert_eq!(
            after.sstables_count, 1,
            "should have 1 SSTable after major compaction (was {})",
            before.sstables_count
        );
        // 20 of 50 keys range-deleted — expect at least 25 % size reduction.
        let max_size = (before.total_sst_size_bytes as f64 * 0.75) as u64;
        assert!(
            after.total_sst_size_bytes <= max_size,
            "total SST size should decrease by ≥ 25 %: before={} B, after={} B, max allowed={} B",
            before.total_sst_size_bytes,
            after.total_sst_size_bytes,
            max_size,
        );

        for i in 0..20 {
            let key = format!("key_{:04}", i).into_bytes();
            assert_eq!(engine.get(key).unwrap(), Some(b"val".to_vec()));
        }
        for i in 20..40 {
            let key = format!("key_{:04}", i).into_bytes();
            assert_eq!(
                engine.get(key).unwrap(),
                None,
                "key_{i:04} should be suppressed"
            );
        }
        for i in 40..50 {
            let key = format!("key_{:04}", i).into_bytes();
            assert_eq!(engine.get(key).unwrap(), Some(b"val".to_vec()));
        }
    }

    /// # Scenario
    /// Major compaction deduplicates overwritten keys, keeping only the
    /// newest version.
    ///
    /// # Starting environment
    /// Empty engine, 256 B write buffer.
    ///
    /// # Actions
    /// 1. Write 20 keys with value `"v1"`, flush.
    /// 2. Overwrite same 20 keys with `"v2"`, flush.
    /// 3. Record `before` stats.
    /// 4. `major_compact()`.
    /// 5. Record `after` stats.
    ///
    /// # Expected behavior
    /// - Exactly 1 SSTable.
    /// - Total SST size decreases (v1 copies eliminated).
    /// - All 20 keys return `"v2"`.
    #[test]
    fn major_compact_deduplicates_and_keeps_newest() {
        let dir = fresh_dir("dedup");
        let engine = Engine::open(&dir, compaction_config()).unwrap();

        for i in 0..20 {
            let key = format!("key_{:04}", i).into_bytes();
            engine.put(key, b"v1".to_vec()).unwrap();
        }
        engine.flush_all_frozen().unwrap();

        for i in 0..20 {
            let key = format!("key_{:04}", i).into_bytes();
            engine.put(key, b"v2".to_vec()).unwrap();
        }
        engine.flush_all_frozen().unwrap();

        let before = engine.stats().unwrap();
        assert!(
            before.sstables_count >= 2,
            "need at least 2 SSTables, got {}",
            before.sstables_count
        );

        engine.major_compact().unwrap();

        let after = engine.stats().unwrap();
        assert_eq!(
            after.sstables_count, 1,
            "should have 1 SSTable after major compaction (was {})",
            before.sstables_count
        );
        // Two complete copies deduped into one — expect at least 25 % reduction.
        let max_size = (before.total_sst_size_bytes as f64 * 0.75) as u64;
        assert!(
            after.total_sst_size_bytes <= max_size,
            "total SST size should decrease by ≥ 25 % (duplicates eliminated): before={} B, after={} B, max allowed={} B",
            before.total_sst_size_bytes,
            after.total_sst_size_bytes,
            max_size,
        );

        for i in 0..20 {
            let key = format!("key_{:04}", i).into_bytes();
            assert_eq!(engine.get(key).unwrap(), Some(b"v2".to_vec()));
        }
    }

    /// # Scenario
    /// A range tombstone must not suppress Puts with a higher LSN.
    ///
    /// # Starting environment
    /// Empty engine, 256 B write buffer.
    ///
    /// # Actions
    /// 1. `delete_range("key_0000", "key_0100")`, flush (lower LSN).
    /// 2. Write keys 0..20 with value `"new"`, flush (higher LSN).
    /// 3. `major_compact()`.
    ///
    /// # Expected behavior
    /// - Exactly 1 SSTable.
    /// - All 20 keys return `"new"` (newer Puts survive the older range tombstone).
    #[test]
    fn major_compact_range_tombstone_doesnt_suppress_newer_put() {
        let dir = fresh_dir("newer_put_survives");
        let engine = Engine::open(&dir, compaction_config()).unwrap();

        engine
            .delete_range(b"key_0000".to_vec(), b"key_0100".to_vec())
            .unwrap();
        engine.flush_all_frozen().unwrap();

        for i in 0..20 {
            let key = format!("key_{:04}", i).into_bytes();
            engine.put(key, b"new".to_vec()).unwrap();
        }
        engine.flush_all_frozen().unwrap();

        let before = engine.stats().unwrap().sstables_count;
        assert!(before >= 2, "need at least 2 SSTables, got {before}");

        engine.major_compact().unwrap();

        let after = engine.stats().unwrap().sstables_count;
        assert_eq!(
            after, 1,
            "should have 1 SSTable after major compaction (was {before})"
        );

        for i in 0..20 {
            let key = format!("key_{:04}", i).into_bytes();
            assert_eq!(
                engine.get(key).unwrap(),
                Some(b"new".to_vec()),
                "key_{i:04} should survive — put LSN > range tombstone LSN"
            );
        }
    }

    /// # Scenario
    /// When every key has been deleted, major compaction produces 0 SSTables.
    ///
    /// # Starting environment
    /// Empty engine, 256 B write buffer.
    ///
    /// # Actions
    /// 1. Write 20 keys, flush.
    /// 2. Delete all 20 keys individually, write padding key, flush.
    /// 3. Record `before` stats.
    /// 4. `major_compact()`.
    /// 5. Record `after` stats.
    ///
    /// # Expected behavior
    /// - At most 1 SSTable remains (pad key may survive, or all SSTables empty).
    /// - Total SST size drastically reduced (\u2265 80 %).
    #[test]
    fn major_compact_everything_deleted() {
        let dir = fresh_dir("all_deleted");
        let engine = Engine::open(&dir, compaction_config()).unwrap();

        for i in 0..20 {
            let key = format!("key_{:04}", i).into_bytes();
            engine.put(key, b"val".to_vec()).unwrap();
        }
        engine.flush_all_frozen().unwrap();

        for i in 0..20 {
            let key = format!("key_{:04}", i).into_bytes();
            engine.delete(key).unwrap();
        }
        engine.put(b"zzz_pad".to_vec(), b"x".to_vec()).unwrap();
        engine.flush_all_frozen().unwrap();

        let before = engine.stats().unwrap();
        assert!(
            before.sstables_count >= 2,
            "need at least 2 SSTables, got {}",
            before.sstables_count
        );

        engine.major_compact().unwrap();

        let after = engine.stats().unwrap();
        // 20 of 21 entries eliminated \u2014 at most 1 SSTable (pad key) survives.
        assert!(
            after.sstables_count <= 1,
            "should have at most 1 SSTable after major compaction, got {} (was {})",
            after.sstables_count,
            before.sstables_count,
        );
        // At least 80 % size reduction.
        let max_size = (before.total_sst_size_bytes as f64 * 0.20) as u64;
        assert!(
            after.total_sst_size_bytes <= max_size,
            "total SST size should decrease by >= 80 %: before={} B, after={} B, max allowed={} B",
            before.total_sst_size_bytes,
            after.total_sst_size_bytes,
            max_size,
        );
    }

    /// # Scenario
    /// Major compaction result is durable across engine close/reopen.
    ///
    /// # Starting environment
    /// Empty engine, 256 B write buffer.
    ///
    /// # Actions
    /// 1. Write 60 keys, flush.
    /// 2. Delete keys 40..60, flush.
    /// 3. `major_compact()`, close engine.
    /// 4. Reopen engine, read all 60 keys.
    ///
    /// # Expected behavior
    /// - Keys 0..40 return their values.
    /// - Keys 40..60 return `None`.
    #[test]
    fn major_compact_survives_reopen() {
        let dir = fresh_dir("reopen");

        {
            let engine = Engine::open(&dir, compaction_config()).unwrap();

            for i in 0..60 {
                let key = format!("key_{:04}", i).into_bytes();
                engine.put(key, b"val".to_vec()).unwrap();
            }
            engine.flush_all_frozen().unwrap();

            for i in 40..60 {
                let key = format!("key_{:04}", i).into_bytes();
                engine.delete(key).unwrap();
            }
            engine.flush_all_frozen().unwrap();

            engine.major_compact().unwrap();
        }

        let engine = Engine::open(&dir, compaction_config()).unwrap();

        for i in 0..40 {
            let key = format!("key_{:04}", i).into_bytes();
            assert_eq!(engine.get(key).unwrap(), Some(b"val".to_vec()));
        }
        for i in 40..60 {
            let key = format!("key_{:04}", i).into_bytes();
            assert_eq!(engine.get(key).unwrap(), None);
        }
    }

    /// # Scenario
    /// After major compaction, a full-range scan returns only live keys.
    ///
    /// # Starting environment
    /// Empty engine, 256 B write buffer.
    ///
    /// # Actions
    /// 1. Write 30 keys, flush.
    /// 2. `delete_range("key_0010", "key_0020")`, flush.
    /// 3. Record `before` stats.
    /// 4. `major_compact()`.
    /// 5. Record `after` stats.
    /// 6. Scan full range.
    ///
    /// # Expected behavior
    /// - Exactly 1 SSTable.
    /// - Scan returns exactly 20 live keys (0..10 + 20..30).
    #[test]
    fn major_compact_scan_after() {
        let dir = fresh_dir("scan");
        let engine = Engine::open(&dir, compaction_config()).unwrap();

        for i in 0..30 {
            let key = format!("key_{:04}", i).into_bytes();
            engine.put(key, b"val".to_vec()).unwrap();
        }
        engine.flush_all_frozen().unwrap();

        engine
            .delete_range(b"key_0010".to_vec(), b"key_0020".to_vec())
            .unwrap();
        engine.flush_all_frozen().unwrap();

        let before = engine.stats().unwrap();
        assert!(
            before.sstables_count >= 2,
            "need at least 2 SSTables, got {}",
            before.sstables_count
        );

        engine.major_compact().unwrap();

        let after = engine.stats().unwrap();
        assert_eq!(
            after.sstables_count, 1,
            "should have 1 SSTable after major compaction (was {})",
            before.sstables_count
        );
        // 10 of 30 keys range-deleted — expect at least 20 % size reduction.
        let max_size = (before.total_sst_size_bytes as f64 * 0.80) as u64;
        assert!(
            after.total_sst_size_bytes <= max_size,
            "total SST size should decrease by ≥ 20 %: before={} B, after={} B, max allowed={} B",
            before.total_sst_size_bytes,
            after.total_sst_size_bytes,
            max_size,
        );

        let results: Vec<_> = engine.scan(b"key_0000", b"key_9999").unwrap().collect();
        assert_eq!(
            results.len(),
            20,
            "expected 20 live keys after major compact"
        );
    }

    /// # Scenario
    /// Major compaction correctly handles a mix of point tombstones and
    /// range tombstones in the same merge, dropping both.
    ///
    /// # Starting environment
    /// Empty engine, 256 B write buffer.
    ///
    /// # Actions
    /// 1. Write keys 0..50, flush.
    /// 2. Point-delete keys 0..10, flush.
    /// 3. `delete_range("key_0030", "key_0040")`, flush.
    /// 4. Record `before` stats.
    /// 5. `major_compact()`.
    /// 6. Record `after` stats.
    ///
    /// # Expected behavior
    /// - Exactly 1 SSTable.
    /// - Total SST size decreases (dead data + both tombstone types dropped).
    /// - Keys 0..10 return `None` (point-deleted).
    /// - Keys 10..30 return values (live).
    /// - Keys 30..40 return `None` (range-deleted).
    /// - Keys 40..50 return values (live).
    /// - Scan returns exactly 30 live keys.
    #[test]
    fn major_compact_mixed_point_and_range_tombstones() {
        let dir = fresh_dir("mixed_tombstones");
        let engine = Engine::open(&dir, compaction_config()).unwrap();

        for i in 0..50 {
            let key = format!("key_{:04}", i).into_bytes();
            engine.put(key, b"val".to_vec()).unwrap();
        }
        engine.flush_all_frozen().unwrap();

        // Point-delete keys 0..10
        for i in 0..10 {
            let key = format!("key_{:04}", i).into_bytes();
            engine.delete(key).unwrap();
        }
        engine.flush_all_frozen().unwrap();

        // Range-delete keys 30..40
        engine
            .delete_range(b"key_0030".to_vec(), b"key_0040".to_vec())
            .unwrap();
        engine.flush_all_frozen().unwrap();

        let before = engine.stats().unwrap();
        assert!(
            before.sstables_count >= 2,
            "need at least 2 SSTables, got {}",
            before.sstables_count
        );

        engine.major_compact().unwrap();

        let after = engine.stats().unwrap();
        assert_eq!(
            after.sstables_count, 1,
            "should have 1 SSTable (was {})",
            before.sstables_count
        );
        // 20 of 50 keys deleted (10 point + 10 range) — expect at least 25 % reduction.
        let max_size = (before.total_sst_size_bytes as f64 * 0.75) as u64;
        assert!(
            after.total_sst_size_bytes <= max_size,
            "total SST size should decrease by ≥ 25 %: before={} B, after={} B, max allowed={} B",
            before.total_sst_size_bytes,
            after.total_sst_size_bytes,
            max_size,
        );

        // Point-deleted
        for i in 0..10 {
            let key = format!("key_{:04}", i).into_bytes();
            assert_eq!(
                engine.get(key).unwrap(),
                None,
                "key_{i:04} should be point-deleted"
            );
        }
        // Live
        for i in 10..30 {
            let key = format!("key_{:04}", i).into_bytes();
            assert_eq!(engine.get(key).unwrap(), Some(b"val".to_vec()));
        }
        // Range-deleted
        for i in 30..40 {
            let key = format!("key_{:04}", i).into_bytes();
            assert_eq!(
                engine.get(key).unwrap(),
                None,
                "key_{i:04} should be range-deleted"
            );
        }
        // Live
        for i in 40..50 {
            let key = format!("key_{:04}", i).into_bytes();
            assert_eq!(engine.get(key).unwrap(), Some(b"val".to_vec()));
        }

        let scan_results: Vec<_> = engine.scan(b"key_0000", b"key_9999").unwrap().collect();
        assert_eq!(
            scan_results.len(),
            30,
            "expected 30 live keys after mixed tombstone major compact"
        );
    }
}