trine-kv 0.5.13

Embedded LSM MVCC key-value database.
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
use super::*;
#[cfg(target_os = "wasi")]
use crate::{FailOnCorruptionPolicy, WriteOptions, recovery, table};
use crate::{HostStorageBackend, StorageMode};

#[test]
fn maintenance_success_does_not_clear_unreported_error() {
    let coordinator = MaintenanceCoordinator::new();
    coordinator.record_error(&Error::Corruption {
        message: "publish failed".to_string(),
    });

    record_maintenance_success(&coordinator);

    let error = coordinator
        .take_error()
        .expect("unreported background error remains visible");
    assert!(matches!(error, Error::Corruption { message } if message == "publish failed"));
    assert!(coordinator.take_error().is_none());
}

#[test]
fn maintenance_error_preserves_runtime_busy_category() {
    let coordinator = MaintenanceCoordinator::new();
    coordinator.record_error(&Error::runtime_busy("flush already active"));

    let error = coordinator
        .take_error()
        .expect("unreported background error remains visible");
    assert!(matches!(error, Error::RuntimeBusy { message } if message == "flush already active"));
}

#[test]
fn maintenance_error_preserves_structured_fencing_fields() {
    let coordinator = MaintenanceCoordinator::new();
    coordinator.record_error(&Error::Fenced {
        held_epoch: 7,
        current_epoch: 8,
    });

    let error = coordinator
        .take_error()
        .expect("unreported background error remains visible");
    assert!(matches!(
        error,
        Error::Fenced {
            held_epoch: 7,
            current_epoch: 8
        }
    ));
}

#[test]
fn background_shutdown_cancels_runtime_token() {
    let maintenance = Arc::new(MaintenanceCoordinator::new());
    let runtime_shutdown = CancellationToken::new();
    let workers = Mutex::new(Vec::new());

    shutdown_background_workers(&maintenance, &runtime_shutdown, &workers);

    assert!(runtime_shutdown.is_cancelled());
}

#[test]
fn background_maintenance_budget_tracks_pressure_thresholds() {
    let mut options = DbOptions::memory();
    options.max_immutable_memtables = 6;
    options.max_l0_files = 3;
    let db = Db::open_sync(options).expect("memory db opens");

    let budget = db.background_maintenance_budget();

    assert_eq!(budget.max_flush_inputs(), 6);
    assert_eq!(budget.max_compaction_inputs(), 4);
    assert_eq!(db.background_flush_request_threshold(), 5);
    assert_eq!(
        Db::background_maintenance_progress_wait(),
        BACKGROUND_MAINTENANCE_PROGRESS_WAIT
    );
}

#[test]
fn background_flush_request_threshold_keeps_tiny_pressure_foreground() {
    let mut options = DbOptions::memory();
    options.max_immutable_memtables = 2;
    let db = Db::open_sync(options).expect("memory db opens");

    assert_eq!(db.background_flush_request_threshold(), 3);
}

#[test]
fn write_pressure_maintenance_reports_foreground_progress() {
    let path = temp_db_path("write-pressure-foreground-maintenance");
    let mut options = DbOptions::persistent(&path);
    options.background_worker_count = 0;
    options.write_buffer_bytes = 1;
    options.max_immutable_memtables = 2;
    options.max_l0_files = 64;
    let db = Db::open_sync(options).expect("open db");

    db.put_sync(b"a", b"one").expect("write first immutable");
    db.put_sync(b"b", b"two").expect("write second immutable");
    let pressure = db.write_pressure().expect("inspect write pressure");
    assert!(pressure.flush);

    let outcome = db
        .run_maintenance_for_pressure(&path, pressure)
        .expect("foreground pressure maintenance");

    assert!(outcome.made_progress());
    assert_eq!(outcome.flushes, 2);
    assert!(!outcome.busy());
    assert_eq!(db.stats().immutable_memtables, 0);

    drop(db);
    fs::remove_dir_all(path).expect("cleanup test db");
}

#[test]
fn persistent_open_attaches_runtime_enabled_native_storage_backend() {
    let path = temp_db_path("persistent-runtime-native-storage");
    let mut options = DbOptions::persistent(&path);
    options.background_worker_count = 0;
    options.default_bucket_options = options.default_bucket_options.with_blob_threshold_bytes(4);
    let db = Db::open_sync(options).expect("persistent db opens");

    let capabilities = db.inner.native_storage.capabilities();
    assert!(capabilities.supports(StorageCapability::AsyncTasks));
    assert!(capabilities.supports(StorageCapability::BlockingAdapter));
    assert!(capabilities.supports(StorageCapability::BackgroundThreads));
    assert!(!capabilities.supports(StorageCapability::PlatformAsyncIo));

    let value = b"value-stored-through-blob".to_vec();
    db.put_sync(b"key", value.clone()).expect("write");
    db.flush_sync()
        .expect("flush through db-owned native storage");
    assert_eq!(
        db.get_sync(b"key").expect("read after flush"),
        Some(value.clone())
    );
    let stats = db.stats();
    assert_eq!(stats.live_blob_files, 1);
    assert!(stats.live_blob_bytes >= value.len() as u64);
    assert!(stats.storage_uses_sync_adapter);
    assert!(!stats.storage_uses_platform_io_driver);
    assert!(!stats.storage_uses_platform_async_io);
    assert_eq!(stats.storage_sync_adapter_queue_capacity, 1024);
    assert!(stats.storage_sync_adapter_submitted_tasks >= stats.storage_sync_adapter_tasks);
    assert!(stats.storage_operations.open_append.requests > 0);
    assert!(stats.storage_operations.write_object.requests > 0);
    assert_eq!(stats.storage_inline_tasks, 0);

    drop(db);
    fs::remove_dir_all(path).expect("cleanup test db");
}

#[test]
fn native_read_only_open_skips_writer_lease_and_rejects_writes() {
    let path = temp_db_path("native-read-only-open");
    let mut writable_options = DbOptions::persistent(&path);
    writable_options.background_worker_count = 0;
    {
        let db = Db::open_sync(writable_options.clone()).expect("persistent db opens");
        db.put_sync(b"key", b"value").expect("write succeeds");
        db.flush_sync().expect("flush succeeds");
    }

    let db = Db::open_sync(writable_options.read_only()).expect("read-only db opens");

    assert!(db.options().read_only);
    assert!(!db.inner.substrate.wal_is_present());
    assert_eq!(
        db.get_sync(b"key").expect("read-only read succeeds"),
        Some(b"value".to_vec())
    );
    assert!(matches!(
        db.put_sync(b"other", b"value"),
        Err(Error::ReadOnly)
    ));
    assert_eq!(db.stats().storage_operations.read_object_bytes.requests, 0);
    assert_eq!(
        db.stats().storage_operations.acquire_writer_lease.requests,
        0
    );

    drop(db);
    fs::remove_dir_all(path).expect("cleanup test db");
}

#[test]
fn native_read_only_open_replays_non_empty_wal() {
    let path = temp_db_path("native-read-only-open-wal-replay");
    let mut writable_options = DbOptions::persistent(&path);
    writable_options.background_worker_count = 0;
    {
        let db = Db::open_sync(writable_options.clone()).expect("persistent db opens");
        db.put_sync(b"key", b"value").expect("write succeeds");
    }

    let db = Db::open_sync(writable_options.read_only()).expect("read-only db opens");

    assert_eq!(
        db.get_sync(b"key").expect("read-only WAL read succeeds"),
        Some(b"value".to_vec())
    );
    assert!(
        db.stats().storage_operations.read_object_bytes.requests > 0,
        "read-only open must read non-empty WAL shards"
    );

    drop(db);
    fs::remove_dir_all(path).expect("cleanup test db");
}

#[cfg(not(target_os = "wasi"))]
#[test]
fn wasi_persistent_backend_requires_wasi_target() {
    let path = temp_db_path("wasi-persistent-host-unsupported");
    let options = DbOptions::wasi_persistent(&path);
    assert_eq!(options.runtime.mode, crate::runtime::RuntimeMode::Inline);
    assert_eq!(options.background_worker_count, 0);

    let wasi_error = Db::open_sync(options).expect_err("WASI backend requires WASI target");
    assert!(matches!(wasi_error, Error::UnsupportedBackend { .. }));
    assert!(wasi_error.to_string().contains("WASI persistent"));
}

#[cfg(not(target_os = "wasi"))]
#[test]
fn wasi_persistent_open_async_requires_wasi_target() {
    let path = temp_db_path("wasi-persistent-async-host-unsupported");
    let error = block_on_test_future(Db::open(DbOptions::wasi_persistent(&path)))
        .expect_err("WASI async open requires WASI target");

    assert!(matches!(error, Error::UnsupportedBackend { .. }));
    assert!(error.to_string().contains("WASI persistent"));
}

#[cfg(target_os = "wasi")]
#[test]
fn wasi_persistent_backend_uses_host_filesystem() {
    let path = temp_db_path("wasi-persistent-host");
    let db = Db::open_sync(DbOptions::wasi_persistent(&path)).expect("WASI db opens");
    db.put_sync(b"key", b"value").expect("WASI write succeeds");
    db.flush_sync().expect("WASI flush succeeds");
    drop(db);

    let db = Db::open_sync(DbOptions::wasi_persistent_read_only(&path))
        .expect("WASI read-only db reopens");
    assert_eq!(
        db.get_sync(b"key").expect("WASI read succeeds"),
        Some(b"value".to_vec())
    );
    drop(db);

    cleanup_wasi_temp_db_path(path);
}

#[cfg(target_os = "wasi")]
#[test]
fn wasi_persistent_writable_reopen_releases_lock_file() {
    let path = temp_db_path("wasi-persistent-writable-reopen");
    let db = Db::open_sync(DbOptions::wasi_persistent(&path)).expect("WASI db opens");
    db.put_sync(b"key", b"value").expect("WASI write succeeds");
    drop(db);

    let db = Db::open_sync(DbOptions::wasi_persistent(&path))
        .expect("WASI writable reopen should not see a stale lock");
    assert_eq!(
        db.get_sync(b"key").expect("WASI read succeeds"),
        Some(b"value".to_vec())
    );
    drop(db);

    cleanup_wasi_temp_db_path(path);
}

#[cfg(target_os = "wasi")]
#[test]
fn wasi_persistent_write_rejects_strict_sync_durability() {
    let path = temp_db_path("wasi-persistent-strict-write");
    let db = Db::open_sync(DbOptions::wasi_persistent(&path)).expect("WASI db opens");

    let error = db
        .put_with_options_sync(b"key", b"value", WriteOptions::sync_all_strict())
        .expect_err("WASI write rejects strict sync durability");
    assert!(matches!(
        error,
        Error::UnsupportedDurability {
            requested: DurabilityMode::SyncAllStrict
        }
    ));
    drop(db);

    cleanup_wasi_temp_db_path(path);
}

#[cfg(target_os = "wasi")]
#[test]
fn wasi_persistent_safe_temp_repair_does_not_remove_leftover_lock() {
    let path = temp_db_path("wasi-persistent-stale-lock");
    let db = Db::open_sync(DbOptions::wasi_persistent(&path)).expect("WASI db opens");
    db.put_sync(b"key", b"value").expect("WASI write succeeds");
    drop(db);
    fs::write(
        path.join(recovery::PROCESS_LOCK_FILE_NAME),
        b"pid=wasi\nnonce=stale\n",
    )
    .expect("stale lock marker writes");

    let error = Db::open_sync(DbOptions::wasi_persistent(&path))
        .expect_err("stale WASI lock requires explicit repair");
    assert!(matches!(error, Error::Corruption { .. }));
    assert!(error.to_string().contains("LOCK"));

    let mut options = DbOptions::wasi_persistent(&path);
    options.fail_on_corruption = FailOnCorruptionPolicy::RepairSafeTemporaryFiles;
    let error = Db::open_sync(options)
        .expect_err("safe temporary repair must not delete a WASI lock marker");
    assert!(matches!(error, Error::Corruption { .. }));
    assert!(error.to_string().contains("LOCK"));
    assert!(path.join(recovery::PROCESS_LOCK_FILE_NAME).exists());

    cleanup_wasi_temp_db_path(path);
}

#[cfg(target_os = "wasi")]
#[test]
fn wasi_persistent_repair_policy_does_not_remove_active_lock() {
    let path = temp_db_path("wasi-persistent-active-lock");
    let db = Db::open_sync(DbOptions::wasi_persistent(&path)).expect("WASI db opens");

    let mut options = DbOptions::wasi_persistent(&path);
    options.fail_on_corruption = FailOnCorruptionPolicy::RepairSafeTemporaryFiles;
    let error = Db::open_sync(options).expect_err("active WASI writer keeps its lock");
    assert!(matches!(error, Error::Corruption { .. }));
    assert!(error.to_string().contains("LOCK"));
    assert!(path.join(recovery::PROCESS_LOCK_FILE_NAME).exists());

    db.put_sync(b"key", b"value")
        .expect("original WASI writer remains usable");
    drop(db);

    cleanup_wasi_temp_db_path(path);
}

#[cfg(target_os = "wasi")]
#[test]
fn wasi_persistent_open_rejects_unreferenced_table_file() {
    let path = temp_db_path("wasi-persistent-unreferenced-table");
    let db = Db::open_sync(DbOptions::wasi_persistent(&path)).expect("WASI db opens");
    drop(db);
    let table_name = format!("table-{:020}.{}", 999_u64, table::TABLE_FILE_EXTENSION);
    fs::write(path.join(&table_name), b"orphan table").expect("orphan table writes");

    let error = Db::open_sync(DbOptions::wasi_persistent(&path))
        .expect_err("WASI open rejects unreferenced table files");
    assert!(matches!(error, Error::Corruption { .. }));
    assert!(error.to_string().contains(&table_name));

    cleanup_wasi_temp_db_path(path);
}

#[cfg(target_os = "wasi")]
#[test]
fn wasi_persistent_open_async_uses_host_filesystem() {
    let path = temp_db_path("wasi-persistent-async-host");
    let db = block_on_test_future(Db::open(DbOptions::wasi_persistent(&path)))
        .expect("WASI async db opens");
    db.put_sync(b"key", b"value").expect("WASI write succeeds");
    db.flush_sync().expect("WASI flush succeeds");
    drop(db);

    let db = block_on_test_future(Db::open(DbOptions::wasi_persistent_read_only(&path)))
        .expect("WASI async read-only db reopens");
    assert_eq!(
        db.get_sync(b"key").expect("WASI read succeeds"),
        Some(b"value".to_vec())
    );
    drop(db);

    cleanup_wasi_temp_db_path(path);
}

#[test]
fn browser_persistent_backend_is_explicitly_unsupported() {
    let options = DbOptions::browser_persistent();
    assert_eq!(options.runtime.mode, crate::runtime::RuntimeMode::Inline);
    assert_eq!(options.background_worker_count, 0);

    let browser_error =
        Db::open_sync(options).expect_err("browser backend is not wired for sync open");
    assert!(matches!(browser_error, Error::UnsupportedBackend { .. }));
    assert!(browser_error.to_string().contains("browser persistent"));
}

#[test]
fn browser_persistent_read_only_options_disable_creation() {
    let options = DbOptions::browser_persistent_read_only();
    assert!(options.read_only);
    assert!(!options.create_if_missing);
    assert_eq!(options.runtime.mode, crate::runtime::RuntimeMode::Inline);
    assert_eq!(options.background_worker_count, 0);
}

#[test]
fn browser_persistent_at_records_namespace_path() {
    let options = DbOptions::browser_persistent_at("app-a");
    match &options.storage_mode {
        StorageMode::HostPersistent {
            backend: HostStorageBackend::Browser { path },
        } => assert_eq!(path, std::path::Path::new("app-a")),
        other => panic!("unexpected browser storage mode: {other:?}"),
    }

    let read_only = DbOptions::browser_persistent_read_only_at("app-a");
    assert!(read_only.read_only);
    assert!(!read_only.create_if_missing);
    match &read_only.storage_mode {
        StorageMode::HostPersistent {
            backend: HostStorageBackend::Browser { path },
        } => assert_eq!(path, std::path::Path::new("app-a")),
        other => panic!("unexpected browser storage mode: {other:?}"),
    }
}

#[test]
fn get_many_sync_preserves_order_missing_deletes_and_duplicates() {
    let db = Db::open_sync(DbOptions::memory()).expect("memory db opens");
    db.put_sync(b"a", b"one").expect("first write");
    db.put_sync(b"b", b"two").expect("second write");
    db.delete_sync(b"deleted").expect("delete writes");

    let keys = [
        b"b".as_slice(),
        b"missing".as_slice(),
        b"a".as_slice(),
        b"b".as_slice(),
        b"deleted".as_slice(),
    ];
    let values = db.get_many_sync(&keys).expect("batch reads");

    assert_eq!(
        values,
        vec![
            Some(b"two".to_vec()),
            None,
            Some(b"one".to_vec()),
            Some(b"two".to_vec()),
            None,
        ]
    );
}

#[test]
fn bucket_get_many_sync_reads_named_bucket_only() {
    let db = Db::open_sync(DbOptions::memory()).expect("memory db opens");
    db.put_sync(b"same", b"default").expect("default write");
    let users = db.bucket_sync("users").expect("named bucket opens");
    users.put_sync(b"same", b"named").expect("named write");

    let keys = [b"same".as_slice(), b"missing".as_slice()];
    let values = users.get_many_sync(&keys).expect("named batch reads");

    assert_eq!(values, vec![Some(b"named".to_vec()), None]);
    assert_eq!(
        db.get_many_sync(&keys).expect("default batch reads"),
        vec![Some(b"default".to_vec()), None]
    );
}

#[test]
fn bucket_reader_get_many_sync_keeps_snapshot_view() {
    let db = Db::open_sync(DbOptions::memory()).expect("memory db opens");
    let bucket = db.default_bucket_sync().expect("default bucket opens");
    bucket.put_sync(b"a", b"one").expect("first write");
    bucket.put_sync(b"b", b"two").expect("second write");
    let snapshot = db.snapshot();
    let reader = bucket.reader(&snapshot).expect("reader opens");

    bucket.put_sync(b"a", b"new").expect("new write");
    bucket.put_sync(b"c", b"three").expect("third write");

    let keys = [b"a".as_slice(), b"c".as_slice(), b"b".as_slice()];
    let values = reader
        .get_many_owned_sync(&keys)
        .expect("snapshot batch reads");

    assert_eq!(
        values,
        vec![Some(b"one".to_vec()), None, Some(b"two".to_vec())]
    );
    assert_eq!(
        bucket.get_many_sync(&keys).expect("current batch reads"),
        vec![
            Some(b"new".to_vec()),
            Some(b"three".to_vec()),
            Some(b"two".to_vec()),
        ]
    );
}

#[test]
fn get_many_async_preserves_order() {
    let db = Db::open_sync(DbOptions::memory()).expect("memory db opens");
    db.put_sync(b"a", b"one").expect("first write");
    db.put_sync(b"b", b"two").expect("second write");

    let keys = [b"b".as_slice(), b"missing".as_slice(), b"a".as_slice()];
    let values = block_on_test_future(db.get_many(&keys)).expect("async batch reads");

    assert_eq!(
        values,
        vec![Some(b"two".to_vec()), None, Some(b"one".to_vec())]
    );
}

#[test]
fn get_many_sync_groups_persistent_keys_by_data_block() {
    let path = temp_db_path("get-many-block-grouping");
    let options = DbOptions::persistent(&path).with_default_bucket_options(BucketOptions {
        block_bytes: 4096,
        ..BucketOptions::default()
    });
    let db = Db::open_sync(options).expect("persistent db opens");
    for index in 0..8 {
        let key = format!("key-{index:02}");
        let value = format!("value-{index:02}");
        db.put_sync(key.as_bytes(), value.as_bytes())
            .expect("write key");
    }
    db.flush_sync().expect("flush table");

    let before = db.stats();
    let keys = [
        b"key-01".as_slice(),
        b"key-02".as_slice(),
        b"key-03".as_slice(),
        b"key-01".as_slice(),
    ];
    let values = db.get_many_sync(&keys).expect("batch reads");
    let after = db.stats();

    assert_eq!(
        values,
        vec![
            Some(b"value-01".to_vec()),
            Some(b"value-02".to_vec()),
            Some(b"value-03".to_vec()),
            Some(b"value-01".to_vec()),
        ]
    );
    assert_eq!(
        after
            .read_path
            .point_data_block_reads
            .saturating_sub(before.read_path.point_data_block_reads),
        1,
        "batch keys in one data block should share the block read"
    );

    drop(db);
    fs::remove_dir_all(path).expect("cleanup test db");
}

#[test]
fn point_read_stats_split_l0_and_non_l0_probes() {
    let path = temp_db_path("point-read-l0-probe-stats");
    let mut options = DbOptions::persistent(&path);
    options.background_worker_count = 0;
    options.max_l0_files = 16;
    let db = Db::open_sync(options).expect("persistent db opens");
    for table_index in 0..2 {
        for row_index in 0..4 {
            let key = format!("key-{table_index}-{row_index}");
            let value = format!("value-{table_index}-{row_index}");
            db.put_sync(key.as_bytes(), value.as_bytes())
                .expect("write key");
        }
        db.flush_sync().expect("flush table");
    }

    let before = db.stats();
    assert_eq!(before.l0_tables, 2, "test needs two L0 tables");
    let value = db
        .get_sync(b"key-0-2")
        .expect("point read succeeds")
        .expect("value exists");
    let after = db.stats();

    assert_eq!(value, b"value-0-2".to_vec());
    let table_probes = after
        .read_path
        .point_table_probes
        .saturating_sub(before.read_path.point_table_probes);
    let l0_probes = after
        .read_path
        .point_l0_table_probes
        .saturating_sub(before.read_path.point_l0_table_probes);
    let non_l0_probes = after
        .read_path
        .point_non_l0_table_probes
        .saturating_sub(before.read_path.point_non_l0_table_probes);
    assert_eq!(table_probes, l0_probes.saturating_add(non_l0_probes));
    assert_eq!(l0_probes, 1);
    assert_eq!(non_l0_probes, 0);
    assert_eq!(
        after
            .read_path
            .point_l0_lookup_keys
            .saturating_sub(before.read_path.point_l0_lookup_keys),
        1
    );
    assert_eq!(
        after
            .read_path
            .point_l0_overlap_extra_table_probes
            .saturating_sub(before.read_path.point_l0_overlap_extra_table_probes),
        0
    );

    let before = db.stats();
    let keys = [b"key-0-2".as_slice(); 8];
    let values = db.get_many_sync(&keys).expect("batch read succeeds");
    let after = db.stats();
    assert_eq!(values, vec![Some(b"value-0-2".to_vec()); 8]);
    assert_eq!(
        after
            .read_path
            .batch_point_input_keys
            .saturating_sub(before.read_path.batch_point_input_keys),
        8
    );
    assert_eq!(
        after
            .read_path
            .batch_point_unique_keys
            .saturating_sub(before.read_path.batch_point_unique_keys),
        1
    );
    assert_eq!(
        after
            .read_path
            .batch_point_table_groups
            .saturating_sub(before.read_path.batch_point_table_groups),
        1
    );
    assert_eq!(
        after
            .read_path
            .batch_point_l0_lookup_keys
            .saturating_sub(before.read_path.batch_point_l0_lookup_keys),
        1
    );
    assert_eq!(
        after
            .read_path
            .batch_point_l0_overlap_extra_table_probes
            .saturating_sub(before.read_path.batch_point_l0_overlap_extra_table_probes),
        0
    );

    drop(db);
    fs::remove_dir_all(path).expect("cleanup test db");
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[test]
fn browser_persistent_open_async_requires_browser_target() {
    let error = block_on_test_future(Db::open(DbOptions::browser_persistent_read_only()))
        .expect_err("browser async open requires browser target");
    assert!(matches!(error, Error::UnsupportedBackend { .. }));
    assert!(error.to_string().contains("browser persistent"));
}

#[test]
fn compaction_reservations_serialize_table_rewrites_within_one_bucket() {
    let base = reservation("default", KeyRange::half_open(b"a", b"c"));

    assert!(compaction_reservations_conflict(
        &base,
        &reservation("default", KeyRange::half_open(b"b", b"d"))
    ));
    assert!(compaction_reservations_conflict(
        &base,
        &reservation("default", KeyRange::half_open(b"c", b"e"))
    ));
    assert!(!compaction_reservations_conflict(
        &base,
        &reservation("other", KeyRange::half_open(b"b", b"d"))
    ));
}

#[test]
fn maintenance_coordinator_allows_compactions_in_different_buckets() {
    let coordinator = Arc::new(MaintenanceCoordinator::new());
    let first = coordinator
        .reserve_compactions(vec![reservation(
            "default",
            KeyRange::half_open(b"a", b"c"),
        )])
        .expect("first compaction reserves");
    let second = coordinator
        .reserve_compactions(vec![
            reservation("default", KeyRange::half_open(b"b", b"d")),
            reservation("default", KeyRange::half_open(b"c", b"e")),
            reservation("other", KeyRange::half_open(b"b", b"d")),
        ])
        .expect("non-overlapping compactions reserve");

    assert!(!second.contains("default", &KeyRange::half_open(b"b", b"d")));
    assert!(!second.contains("default", &KeyRange::half_open(b"c", b"e")));
    assert!(second.contains("other", &KeyRange::half_open(b"b", b"d")));

    drop(first);
    drop(second);
    let third = coordinator
        .reserve_compactions(vec![reservation(
            "default",
            KeyRange::half_open(b"b", b"d"),
        )])
        .expect("released range can reserve again");
    assert!(third.contains("default", &KeyRange::half_open(b"b", b"d")));
}

#[test]
fn maintenance_coordinator_serializes_flush_with_table_rewrites() {
    let coordinator = Arc::new(MaintenanceCoordinator::new());
    let compaction = coordinator
        .reserve_compactions(vec![reservation("default", KeyRange::all())])
        .expect("compaction reserves");
    assert!(coordinator.try_start_flush().is_none());
    drop(compaction);

    let flush = coordinator.try_start_flush().expect("flush starts");
    assert!(
        coordinator
            .reserve_compactions(vec![reservation("other", KeyRange::all())])
            .is_none()
    );
    drop(flush);

    assert!(
        coordinator
            .reserve_compactions(vec![reservation("other", KeyRange::all())])
            .is_some()
    );
}

#[test]
fn native_async_close_waits_for_active_publish_before_releasing_lease() {
    let path = temp_db_path("native-close-waits-for-publish");
    let mut options = DbOptions::persistent(&path);
    options.background_worker_count = 0;
    let db = Db::open_sync(options).expect("open db");
    db.put_sync(b"key", b"value").expect("write");

    let activity = db
        .inner
        .publish_barrier
        .begin_activity()
        .expect("test holds active publish");
    let thread_db = db.clone();
    let (started_tx, started_rx) = mpsc::channel();
    let (done_tx, done_rx) = mpsc::channel();
    let handle = thread::spawn(move || {
        started_tx.send(()).expect("report close thread start");
        let result = block_on_test_future(thread_db.close());
        done_tx.send(result).expect("send close result");
    });

    started_rx
        .recv_timeout(Duration::from_secs(1))
        .expect("close thread starts");
    assert!(
        done_rx.recv_timeout(Duration::from_millis(50)).is_err(),
        "native async close must wait for active publish before releasing the writer lease"
    );

    drop(activity);
    done_rx
        .recv_timeout(Duration::from_secs(5))
        .expect("close finishes after active publish exits")
        .expect("close succeeds");
    handle.join().expect("close thread joins");

    drop(db);
    fs::remove_dir_all(path).expect("cleanup test db");
}

#[test]
fn flush_waits_for_existing_flush_guard() {
    let path = temp_db_path("flush-waits-for-existing-guard");
    let mut options = DbOptions::persistent(&path);
    options.background_worker_count = 0;
    let db = Db::open_sync(options).expect("open db");
    db.put_sync(b"key", b"value").expect("write");

    let flush_guard = db
        .inner
        .maintenance
        .try_start_flush()
        .expect("test holds flush guard");
    let thread_db = db.clone();
    let (started_tx, started_rx) = mpsc::channel();
    let (done_tx, done_rx) = mpsc::channel();
    let handle = thread::spawn(move || {
        started_tx.send(()).expect("report flush thread start");
        done_tx
            .send(thread_db.flush_sync())
            .expect("send flush result");
    });

    started_rx
        .recv_timeout(Duration::from_secs(1))
        .expect("flush thread starts");
    assert!(
        done_rx.recv_timeout(Duration::from_millis(50)).is_err(),
        "public flush must wait while another flush guard is active"
    );

    drop(flush_guard);
    done_rx
        .recv_timeout(Duration::from_secs(5))
        .expect("flush finishes after guard release")
        .expect("flush succeeds");
    handle.join().expect("flush thread joins");

    let stats = db.stats();
    assert_eq!(stats.memtable_bytes, 0);
    assert_eq!(stats.immutable_memtables, 0);
    assert!(stats.total_tables > 0);

    drop(db);
    fs::remove_dir_all(path).expect("cleanup test db");
}

#[test]
fn flush_returns_after_default_background_flush_publishes_tables() {
    let path = temp_db_path("flush-default-background-publishes");
    let mut options = DbOptions::persistent(&path);
    options.write_buffer_bytes = 128;
    let db = Db::open_sync(options).expect("open db");

    for index in 0..128_u32 {
        let key = format!("key-{index:04}");
        db.put_sync(key.as_bytes(), [b'x'; 96]).expect("write");
    }

    db.flush_sync().expect("public flush");
    let stats = db.stats();
    assert_eq!(stats.memtable_bytes, 0);
    assert_eq!(stats.immutable_memtables, 0);
    assert!(stats.total_tables > 0);

    drop(db);
    fs::remove_dir_all(path).expect("cleanup test db");
}

#[test]
fn compact_range_is_not_silent_best_effort() {
    let path = temp_db_path("compact-range-waits-for-guard");
    let mut options = DbOptions::persistent(&path);
    options.background_worker_count = 0;
    let db = Db::open_sync(options).expect("open db");
    db.put_sync(b"a1", b"one").expect("write first");
    db.flush_sync().expect("flush first table");
    db.put_sync(b"a2", b"two").expect("write second");
    db.flush_sync().expect("flush second table");

    let compaction_guard = db
        .inner
        .maintenance
        .reserve_compactions(vec![reservation(DEFAULT_BUCKET_NAME, KeyRange::all())])
        .expect("test holds compaction reservation");
    let thread_db = db.clone();
    let (started_tx, started_rx) = mpsc::channel();
    let (done_tx, done_rx) = mpsc::channel();
    let handle = thread::spawn(move || {
        started_tx.send(()).expect("report compaction thread start");
        done_tx
            .send(thread_db.compact_range_sync(KeyRange::all()))
            .expect("send compaction result");
    });

    started_rx
        .recv_timeout(Duration::from_secs(1))
        .expect("compaction thread starts");
    assert!(
        done_rx.recv_timeout(Duration::from_millis(50)).is_err(),
        "public compact_range must wait while its range is reserved"
    );

    drop(compaction_guard);
    done_rx
        .recv_timeout(Duration::from_secs(5))
        .expect("compaction finishes after guard release")
        .expect("compaction succeeds");
    handle.join().expect("compaction thread joins");
    assert!(db.stats().compaction_runs > 0);
    assert!(db.stats().maintenance_cooperative_yields > 0);

    drop(db);
    fs::remove_dir_all(path).expect("cleanup test db");
}

fn reservation(bucket: &str, range: KeyRange) -> CompactionReservation {
    CompactionReservation {
        bucket: bucket.to_owned(),
        range,
    }
}

fn temp_db_path(name: &str) -> std::path::PathBuf {
    let nonce = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("system clock is after UNIX epoch")
        .as_nanos();
    #[cfg(target_os = "wasi")]
    let process_id = "wasi".to_owned();
    #[cfg(not(target_os = "wasi"))]
    let process_id = std::process::id().to_string();
    let db_name = format!("trine-kv-{name}-{process_id}-{nonce}");

    #[cfg(target_os = "wasi")]
    {
        std::path::PathBuf::from("target/wasi-test-data").join(db_name)
    }

    #[cfg(not(target_os = "wasi"))]
    {
        std::env::temp_dir().join(db_name)
    }
}

#[cfg(target_os = "wasi")]
fn cleanup_wasi_temp_db_path(path: std::path::PathBuf) {
    let _ = fs::remove_dir_all(path);
}