pagedb 0.1.0-beta.6

Encrypted, portable, embedded page store with B+ tree and segment-file surfaces.
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
//! Structural-integrity stress: sustained commits mixing appends, rewrites,
//! and overflow values, verifying every committed key remains readable with
//! its last-written value. Exercises the interaction between the dirty-leaf
//! cache, in-txn splits, spine CoW at flush, and durable free-list recycling.
//!
//! Every scenario is generic over the VFS and runs against both `MemVfs` and
//! the real-file backends (`TokioVfs`, and `IouringVfs` on Linux): the memory
//! backend gives deterministic logic coverage, the disk backends cover write
//! submission/completion ordering, handle lifecycle across drop/reopen, and
//! fsync behaviour that an in-memory file cannot model.

use std::collections::BTreeMap;

use pagedb::options::{OpenOptions, RetainPolicy};
use pagedb::vfs::Vfs;
use pagedb::vfs::memory::MemVfs;
use pagedb::vfs::tokio_backend::TokioVfs;
use pagedb::{Db, RealmId, run_deep_walk};

const PAGE: usize = 4096;
const KEK: [u8; 32] = [9u8; 32];
const REALM: RealmId = RealmId::new([1; 16]);

fn opts() -> OpenOptions {
    OpenOptions::default().with_commit_history_retain(RetainPolicy::Disabled)
}

/// Unique on-disk root for a real-file backend run.
fn disk_root(tag: &str) -> std::path::PathBuf {
    let mut p = std::env::temp_dir();
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| d.as_nanos());
    p.push(format!(
        "pagedb-structural-{}-{}-{}",
        tag,
        std::process::id(),
        nanos
    ));
    std::fs::create_dir_all(&p).unwrap();
    p
}

fn key(i: u64) -> Vec<u8> {
    format!("key{i:08}").into_bytes()
}

/// Deterministic value for key `i` at rewrite generation `generation`.
/// Every 10th key gets an overflow-sized value (> page_size / 4); the rest
/// stay inline but large enough that leaves hold only a handful of records,
/// forcing frequent leaf and internal splits. The overflow decision also
/// depends on `generation`, so rewrites flip records between inline and
/// overflow representations, churning overflow-chain allocation and release.
fn value(i: u64, generation: u64) -> Vec<u8> {
    let len = if (i + generation) % 10 == 0 {
        3000
    } else {
        300
    };
    let seed = (i.wrapping_mul(31)).wrapping_add(generation) as u8;
    vec![seed; len]
}

/// Read back every key in `expected` through a fresh read txn and assert the
/// stored value matches the last write. Surfaces both lost updates (stale
/// value) and structural corruption (AEAD/MAC failure on a recycled page).
async fn verify_all<V: Vfs + Clone>(db: &Db<V>, expected: &BTreeMap<u64, u64>) {
    let r = db.begin_read().await.unwrap();
    for (&i, &generation) in expected {
        let got = r
            .get(&key(i))
            .await
            .unwrap_or_else(|e| panic!("get key {i} failed: {e:?}"));
        let want = value(i, generation);
        match got {
            None => panic!("key {i} missing (generation {generation})"),
            Some(v) => assert_eq!(
                v,
                want,
                "key {i} wrong value: got len {}, want generation {generation} len {}",
                v.len(),
                want.len()
            ),
        }
    }
}

/// Poll a future once, returning `Some(output)` if it completed.
async fn futures_poll_once<F: std::future::Future + Unpin>(fut: F) -> Option<F::Output> {
    let mut fut = fut;
    std::future::poll_fn(move |cx| {
        std::task::Poll::Ready(match std::pin::Pin::new(&mut fut).poll(cx) {
            std::task::Poll::Ready(v) => Some(v),
            std::task::Poll::Pending => None,
        })
    })
    .await
}

// ------------------------------------------------------------------------ //
// Scenario bodies, generic over the VFS.
// ------------------------------------------------------------------------ //

/// Monotonic inserts plus scattered rewrites, committed in small batches, with
/// a full read-back after every commit. A few thousand puts drive the tree
/// through multiple leaf splits, root growth, and internal splits while other
/// leaves sit dirty in the same txn — and drive freed pages through the
/// durable free-list back into circulation.
async fn sustained_impl<V: Vfs + Clone>(vfs: V) {
    let db = Db::open(vfs, KEK, PAGE, REALM, opts()).await.unwrap();
    // expected: key index -> last-written generation.
    let mut expected: BTreeMap<u64, u64> = BTreeMap::new();
    let mut next_key: u64 = 0;
    // Simple deterministic LCG for choosing rewrite targets.
    let mut rng: u64 = 0x243F_6A88_85A3_08D3;
    let mut lcg = move || {
        rng = rng
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        rng
    };

    for batch in 0..80u64 {
        let mut w = db.begin_write().await.unwrap();
        // (a) 20 rewrites of existing keys scattered across the keyspace,
        // dirtying leaves far from the append edge — recorded in the txn
        // BEFORE the appends below trigger any splits.
        if next_key > 100 {
            for _ in 0..20 {
                let i = lcg() % next_key;
                let generation = expected.get(&i).copied().unwrap_or(0) + 1;
                w.put(&key(i), &value(i, generation)).await.unwrap();
                expected.insert(i, generation);
            }
        }
        // (b) 50 monotonically increasing fresh keys, splitting the rightmost
        // leaf (and periodically the spine) as the last writes of the txn.
        for _ in 0..50 {
            let i = next_key;
            next_key += 1;
            w.put(&key(i), &value(i, 0)).await.unwrap();
            expected.insert(i, 0);
        }
        w.commit()
            .await
            .unwrap_or_else(|e| panic!("commit of batch {batch} failed: {e:?}"));
        verify_all(&db, &expected).await;
    }
}

/// The same mixed workload driven across repeated close/reopen cycles on a
/// shared VFS. A reopen discards the buffer pool, so every page referenced by
/// the durable header must decode from disk — a page that was freed and
/// recycled while a durable structure still referenced it stays hidden behind
/// the warm cache in a single-handle run and only surfaces here as an
/// AEAD/MAC failure or stale value on the cold read-back.
async fn reopen_cycle_impl<V: Vfs + Clone>(vfs: V) {
    let mut expected: BTreeMap<u64, u64> = BTreeMap::new();
    let mut next_key: u64 = 0;
    let mut rng: u64 = 0x9E37_79B9_7F4A_7C15;
    let mut lcg = move || {
        rng = rng
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        rng
    };

    for cycle in 0..12u64 {
        // The first cycle bootstraps and the rest reopen; `Db::open` resolves
        // which from what is on disk.
        let db = Db::open(vfs.clone(), KEK, PAGE, REALM, opts())
            .await
            .unwrap_or_else(|e| panic!("open before cycle {cycle} failed: {e:?}"));
        // Cold read-back straight after reopen: every key must decode from
        // disk before any new write warms or repairs the cache.
        verify_all(&db, &expected).await;

        for _txn in 0..6u64 {
            let mut w = db.begin_write().await.unwrap();
            // Scattered rewrites first, then appends, so splits triggered by
            // the appends are the txn's final structural mutations.
            if next_key > 100 {
                for _ in 0..20 {
                    let i = lcg() % next_key;
                    let generation = expected.get(&i).copied().unwrap_or(0) + 1;
                    w.put(&key(i), &value(i, generation)).await.unwrap();
                    expected.insert(i, generation);
                }
                // A few scattered deletes, so leaves shrink and freed
                // overflow chains flow through the free-list.
                for _ in 0..5 {
                    let i = lcg() % next_key;
                    if expected.remove(&i).is_some() {
                        assert!(w.delete(&key(i)).await.unwrap(), "delete key {i}");
                    }
                }
            }
            // Monotonic tail extension via the append fast path (cached
            // rightmost descent), the pattern an op-log embedder produces.
            for _ in 0..40 {
                let i = next_key;
                next_key += 1;
                w.put_append(&key(i), &value(i, 0)).await.unwrap();
                expected.insert(i, 0);
            }
            // A catalog write per txn, so both trees allocate pages in the
            // shared id space every commit.
            w.counter("txn-seq").unwrap().increment_by(1).await.unwrap();
            w.commit()
                .await
                .unwrap_or_else(|e| panic!("commit in cycle {cycle} failed: {e:?}"));
            // Structural integrity must hold after every commit: no page may
            // be simultaneously reachable from a live root and named free,
            // and every referenced page must decode. This catches a lost
            // reference at the commit that created it, long before the page
            // is recycled and the damage becomes an AEAD failure.
            let report = run_deep_walk(&db).await.unwrap();
            assert!(
                report.is_clean(),
                "deep walk found issues after a commit in cycle {cycle}: {:?}",
                report.page_issues
            );
        }
        verify_all(&db, &expected).await;
        // Drop without compaction or graceful drain, as an embedder that is
        // terminated right after a commit would.
        drop(db);
    }
}

/// Commit cancellation at every await point. A commit future dropped mid-way
/// (as an embedder shutting down mid-commit produces) must leave the store
/// openable and atomic: after reopen the visible state is exactly the
/// pre-commit state or exactly the post-commit state, never a blend, and a
/// deep walk stays clean. Iterates the cancellation point across the whole
/// commit sequence by polling the future a bounded number of times before
/// dropping it. On a real-file backend a dropped commit also abandons queued
/// write submissions mid-io, which the memory backend cannot model.
async fn cancelled_commit_impl<V: Vfs + Clone>(vfs: V) {
    let mut db = Db::open(vfs.clone(), KEK, PAGE, REALM, opts())
        .await
        .unwrap();
    let mut committed: BTreeMap<u64, u64> = BTreeMap::new();
    let mut next_key: u64 = 0;

    // Seed a base commit so every later batch mixes rewrites with appends.
    {
        let mut w = db.begin_write().await.unwrap();
        for _ in 0..120 {
            let i = next_key;
            next_key += 1;
            w.put(&key(i), &value(i, 0)).await.unwrap();
            committed.insert(i, 0);
        }
        w.commit().await.unwrap();
    }

    for k in 0..60usize {
        // Build a batch on top of the committed state.
        let mut staged = committed.clone();
        let mut w = db.begin_write().await.unwrap();
        for _ in 0..10 {
            let i = (next_key.wrapping_mul(7) + k as u64) % next_key;
            let generation = staged.get(&i).copied().unwrap_or(0) + 1;
            w.put(&key(i), &value(i, generation)).await.unwrap();
            staged.insert(i, generation);
        }
        for _ in 0..20 {
            let i = next_key;
            next_key += 1;
            w.put(&key(i), &value(i, 0)).await.unwrap();
            staged.insert(i, 0);
        }
        // Poll the commit future exactly `k` times, then drop it, modelling a
        // shutdown that cancels the in-flight commit at that await point.
        let completed = {
            let mut fut = Box::pin(w.commit());
            let mut completed = None;
            for _ in 0..k {
                let poll = futures_poll_once(fut.as_mut()).await;
                if let Some(result) = poll {
                    completed = Some(result.expect("commit that ran to completion succeeds"));
                    break;
                }
            }
            completed
            // Dropping `fut` here drops the write txn mid-commit when it
            // never completed.
        };

        // Reopen cold and demand atomicity: the surviving state is exactly
        // `staged` (commit landed) or exactly `committed` (commit lost).
        drop(db);
        db = Db::open(vfs.clone(), KEK, PAGE, REALM, opts())
            .await
            .unwrap_or_else(|e| panic!("reopen after cancellation at poll {k} failed: {e:?}"));
        let r = db.begin_read().await.unwrap();
        let landed = match completed {
            Some(_) => true,
            None => {
                // Probe with a key unique to the staged batch.
                let probe = next_key - 1;
                r.get(&key(probe))
                    .await
                    .unwrap_or_else(|e| panic!("probe read after cancel at {k}: {e:?}"))
                    .is_some()
            }
        };
        let want = if landed { &staged } else { &committed };
        for (&i, &generation) in want {
            let got = r
                .get(&key(i))
                .await
                .unwrap_or_else(|e| panic!("get key {i} after cancel at {k}: {e:?}"));
            assert_eq!(
                got.as_deref(),
                Some(value(i, generation).as_slice()),
                "key {i} inconsistent after commit cancelled at poll {k} (landed={landed})"
            );
        }
        drop(r);
        let report = run_deep_walk(&db).await.unwrap();
        assert!(
            report.is_clean(),
            "deep walk issues after commit cancelled at poll {k}: {:?}",
            report.page_issues
        );
        if landed {
            committed = staged;
        } else {
            // The lost batch's keys were never durable; rewind the key cursor
            // so bookkeeping matches the store.
            next_key -= 20;
        }
    }
}

/// Concurrent readers against a committing writer on a multi-thread runtime,
/// followed by a reopen and cold verification. Reader pins move the
/// reclamation floor while commits recycle pages, so this exercises the
/// begin-time floor scan, the shared allocator cache, and pager cache
/// concurrency under real parallelism.
async fn concurrent_impl<V: Vfs + Clone + Send + Sync + 'static>(vfs: V) {
    let db = std::sync::Arc::new(
        Db::open(vfs.clone(), KEK, PAGE, REALM, opts())
            .await
            .unwrap(),
    );

    let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
    let mut readers = Vec::new();
    for r in 0..3u64 {
        let db = db.clone();
        let stop = stop.clone();
        readers.push(tokio::spawn(async move {
            let mut i: u64 = r;
            while !stop.load(std::sync::atomic::Ordering::Relaxed) {
                let snap = db.begin_read().await.unwrap();
                for _ in 0..20 {
                    // Reads may race ahead of the writer; only decode
                    // errors matter, absence is fine.
                    let _ = snap.get(&key(i % 4000)).await.unwrap();
                    i = i.wrapping_add(13);
                }
                tokio::task::yield_now().await;
            }
        }));
    }

    let mut expected: BTreeMap<u64, u64> = BTreeMap::new();
    let mut next_key: u64 = 0;
    let mut rng: u64 = 0xDEAD_BEEF_CAFE_F00D;
    let mut lcg = move || {
        rng = rng
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        rng
    };
    for _batch in 0..60u64 {
        let mut w = db.begin_write().await.unwrap();
        if next_key > 100 {
            for _ in 0..15 {
                let i = lcg() % next_key;
                let generation = expected.get(&i).copied().unwrap_or(0) + 1;
                w.put(&key(i), &value(i, generation)).await.unwrap();
                expected.insert(i, generation);
            }
        }
        for _ in 0..30 {
            let i = next_key;
            next_key += 1;
            w.put_append(&key(i), &value(i, 0)).await.unwrap();
            expected.insert(i, 0);
        }
        w.commit().await.unwrap();
    }
    stop.store(true, std::sync::atomic::Ordering::Relaxed);
    for r in readers {
        r.await.unwrap();
    }

    let report = run_deep_walk(&db).await.unwrap();
    assert!(
        report.is_clean(),
        "deep walk issues: {:?}",
        report.page_issues
    );
    verify_all(&db, &expected).await;
    drop(db);

    let db = Db::open(vfs, KEK, PAGE, REALM, opts()).await.unwrap();
    verify_all(&db, &expected).await;
    let report = run_deep_walk(&db).await.unwrap();
    assert!(
        report.is_clean(),
        "post-reopen issues: {:?}",
        report.page_issues
    );
}

/// Randomized structural stress across many seeds: variable-length keys
/// (long separators force frequent multi-level internal splits), values
/// spanning inline through multi-page overflow chains, same-key rewrite
/// churn, deletes, range deletes, aborted txns, and reopen cycles. Runs a
/// deep-walk integrity check after every commit so a lost reference is
/// caught at the commit that created it, and a cold full read-back after
/// every reopen.
async fn randomized_impl<V: Vfs + Clone>(seeds: u64, mut make_vfs: impl FnMut(u64) -> V) {
    for seed in 0..seeds {
        let vfs = make_vfs(seed);
        let mut rng: u64 = seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1;
        let mut next = move || {
            rng ^= rng << 13;
            rng ^= rng >> 7;
            rng ^= rng << 17;
            rng
        };
        // Keys: index -> byte key. Length varies with the index so separator
        // keys differ in size across the tree.
        let skey = |i: u64| -> Vec<u8> {
            let pad = (i % 5) as usize * 20;
            format!("{:0width$}", i, width = 12 + pad).into_bytes()
        };
        let sval = |i: u64, generation: u64| -> Vec<u8> {
            // Value length cycles through inline, single-page overflow, and
            // multi-page overflow chains as the generation advances.
            let len = match (i + generation) % 4 {
                0 => 64,
                1 => 700,
                2 => 2500,
                _ => 9000,
            };
            vec![(i.wrapping_mul(37).wrapping_add(generation)) as u8; len]
        };

        let mut expected: BTreeMap<u64, u64> = BTreeMap::new();
        let mut next_key: u64 = 0;

        let db = Db::open(vfs.clone(), KEK, PAGE, REALM, opts())
            .await
            .unwrap();
        let mut db = db;
        for round in 0..14u64 {
            let mut w = db.begin_write().await.unwrap();
            let ops = 30 + (next() % 40);
            let mut staged = expected.clone();
            for _ in 0..ops {
                match next() % 10 {
                    // Append new keys.
                    0..=3 => {
                        let i = next_key;
                        next_key += 1;
                        w.put(&skey(i), &sval(i, 0)).await.unwrap();
                        staged.insert(i, 0);
                    }
                    // Rewrite an existing key (value class may flip).
                    4..=6 => {
                        if next_key > 0 {
                            let i = next() % next_key;
                            let generation = staged.get(&i).copied().unwrap_or(0) + 1;
                            w.put(&skey(i), &sval(i, generation)).await.unwrap();
                            staged.insert(i, generation);
                        }
                    }
                    // Delete a key.
                    7..=8 => {
                        if next_key > 0 {
                            let i = next() % next_key;
                            if staged.remove(&i).is_some() {
                                assert!(w.delete(&skey(i)).await.unwrap());
                            }
                        }
                    }
                    // Delete a small index window.
                    _ => {
                        if next_key > 20 {
                            let lo = next() % (next_key - 10);
                            let hi = lo + 1 + next() % 8;
                            // Keys are not ordered by index (lengths vary), so
                            // delete by explicit key list for exact bookkeeping.
                            for i in lo..hi.min(next_key) {
                                if staged.remove(&i).is_some() {
                                    assert!(w.delete(&skey(i)).await.unwrap());
                                }
                            }
                        }
                    }
                }
            }
            // One in five txns aborts: its staged mutations must vanish.
            if next() % 5 == 0 {
                w.abort().await;
            } else {
                w.commit()
                    .await
                    .unwrap_or_else(|e| panic!("seed {seed} round {round} commit: {e:?}"));
                expected = staged;
                let report = run_deep_walk(&db).await.unwrap();
                assert!(
                    report.is_clean(),
                    "seed {seed} round {round}: deep walk issues: {:?}",
                    report.page_issues
                );
            }
            // Reopen every few rounds: cold-verify everything.
            if next() % 3 == 0 {
                drop(db);
                db = Db::open(vfs.clone(), KEK, PAGE, REALM, opts())
                    .await
                    .unwrap_or_else(|e| panic!("seed {seed} round {round} reopen: {e:?}"));
                let r = db.begin_read().await.unwrap();
                for (&i, &generation) in &expected {
                    let got = r
                        .get(&skey(i))
                        .await
                        .unwrap_or_else(|e| panic!("seed {seed} round {round} get {i}: {e:?}"));
                    assert_eq!(
                        got.as_deref(),
                        Some(sval(i, generation).as_slice()),
                        "seed {seed} round {round} key {i} stale/missing"
                    );
                }
            }
        }
        drop(db);
    }
}

// ------------------------------------------------------------------------ //
// MemVfs runs.
// ------------------------------------------------------------------------ //

#[tokio::test(flavor = "current_thread")]
async fn sustained_put_rewrite_overflow_all_keys_survive() {
    sustained_impl(MemVfs::new()).await;
}

#[tokio::test(flavor = "current_thread")]
async fn reopen_cycle_all_keys_survive() {
    reopen_cycle_impl(MemVfs::new()).await;
}

#[tokio::test(flavor = "current_thread")]
async fn cancelled_commit_leaves_consistent_store() {
    cancelled_commit_impl(MemVfs::new()).await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn concurrent_readers_writer_reopen() {
    concurrent_impl(MemVfs::new()).await;
}

#[tokio::test(flavor = "current_thread")]
async fn randomized_structural_stress() {
    randomized_impl(16, |_| MemVfs::new()).await;
}

// ------------------------------------------------------------------------ //
// TokioVfs (real files) runs.
// ------------------------------------------------------------------------ //

#[tokio::test(flavor = "current_thread")]
async fn sustained_all_keys_survive_tokio_disk() {
    let root = disk_root("sustained");
    sustained_impl(TokioVfs::new(&root)).await;
    std::fs::remove_dir_all(&root).ok();
}

#[tokio::test(flavor = "current_thread")]
async fn reopen_cycle_all_keys_survive_tokio_disk() {
    let root = disk_root("reopen");
    reopen_cycle_impl(TokioVfs::new(&root)).await;
    std::fs::remove_dir_all(&root).ok();
}

#[tokio::test(flavor = "current_thread")]
async fn cancelled_commit_consistent_tokio_disk() {
    let root = disk_root("cancel");
    cancelled_commit_impl(TokioVfs::new(&root)).await;
    std::fs::remove_dir_all(&root).ok();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn concurrent_readers_writer_reopen_tokio_disk() {
    let root = disk_root("concurrent");
    concurrent_impl(TokioVfs::new(&root)).await;
    std::fs::remove_dir_all(&root).ok();
}

#[tokio::test(flavor = "current_thread")]
async fn randomized_structural_stress_tokio_disk() {
    let root = disk_root("randomized");
    let base = root.clone();
    randomized_impl(6, move |seed| {
        let dir = base.join(format!("seed{seed}"));
        std::fs::create_dir_all(&dir).unwrap();
        TokioVfs::new(dir)
    })
    .await;
    std::fs::remove_dir_all(&root).ok();
}

// ------------------------------------------------------------------------ //
// IouringVfs (Linux default backend) runs.
// ------------------------------------------------------------------------ //

#[cfg(target_os = "linux")]
#[tokio::test(flavor = "current_thread")]
async fn reopen_cycle_all_keys_survive_iouring_disk() {
    let root = disk_root("reopen-uring");
    reopen_cycle_impl(pagedb::vfs::IouringVfs::new(&root).unwrap()).await;
    std::fs::remove_dir_all(&root).ok();
}

#[cfg(target_os = "linux")]
#[tokio::test(flavor = "current_thread")]
async fn cancelled_commit_consistent_iouring_disk() {
    let root = disk_root("cancel-uring");
    cancelled_commit_impl(pagedb::vfs::IouringVfs::new(&root).unwrap()).await;
    std::fs::remove_dir_all(&root).ok();
}

#[cfg(target_os = "linux")]
#[tokio::test(flavor = "current_thread")]
async fn randomized_structural_stress_iouring_disk() {
    let root = disk_root("randomized-uring");
    let base = root.clone();
    randomized_impl(6, move |seed| {
        let dir = base.join(format!("seed{seed}"));
        std::fs::create_dir_all(&dir).unwrap();
        pagedb::vfs::IouringVfs::new(dir).unwrap()
    })
    .await;
    std::fs::remove_dir_all(&root).ok();
}