redb 4.3.0

Rust Embedded 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
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
#![cfg(feature = "experimental-multiprocess")]

use redb::backends::InMemoryBackend;
use redb::{ConcurrencyMode, Database, DatabaseError, StorageError};

#[cfg(any(target_os = "linux", target_vendor = "apple", windows))]
mod shared_reader {
    use super::*;
    use redb::{ReadableDatabase, ReadableTable, TableDefinition};
    use std::path::Path;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, Ordering};

    const TABLE: TableDefinition<u64, u64> = TableDefinition::new("x");

    fn shared() -> redb::Builder {
        let mut builder = Database::builder();
        builder.set_concurrency_mode(ConcurrencyMode::MultiWriter);
        builder
    }

    fn create(path: &Path) -> Database {
        let db = shared().create(path).unwrap();
        let write = db.begin_write().unwrap();
        {
            let mut table = write.open_table(TABLE).unwrap();
            table.insert(0, 0).unwrap();
        }
        write.commit().unwrap();
        db
    }

    fn value(db: &impl ReadableDatabase) -> u64 {
        let read = db.begin_read().unwrap();
        let table = read.open_table(TABLE).unwrap();
        table.get(0).unwrap().unwrap().value()
    }

    /// Following a peer's commits means seeing its data, not only its newer id
    #[test]
    fn a_shared_reader_sees_a_peers_writes() {
        let tmpfile = tempfile::NamedTempFile::new().unwrap();
        let writer = create(tmpfile.path());
        let reader = shared().open_read_only(tmpfile.path()).unwrap();

        // Caches the page the value lives on
        assert_eq!(value(&reader), 0);

        // Staleness needs the allocator to recycle the cached page, so churn until it does
        for expected in 1..60u64 {
            let write = writer.begin_write().unwrap();
            {
                let mut table = write.open_table(TABLE).unwrap();
                table.insert(0, expected).unwrap();
            }
            write.commit().unwrap();

            assert_eq!(
                value(&reader),
                expected,
                "the reader served a cached page after the writer committed {expected}"
            );
        }
    }

    /// A shared reader reads pages by the immutable geometry alone, so the region counts, which an
    /// unclean header leaves unvalidated, never reach it
    #[test]
    fn a_shared_reader_ignores_the_layout() {
        use std::io::{Seek, SeekFrom, Write};

        let tmpfile = tempfile::NamedTempFile::new().unwrap();
        let writer = create(tmpfile.path());
        let write = writer.begin_write().unwrap();
        {
            let mut table = write.open_table(TABLE).unwrap();
            table.insert(0, 7).unwrap();
        }
        write.commit().unwrap();

        // The region counts, torn to zero under the live writer
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .open(tmpfile.path())
            .unwrap();
        file.seek(SeekFrom::Start(24)).unwrap();
        file.write_all(&[0u8; 8]).unwrap();
        drop(file);

        let reader = shared().open_read_only(tmpfile.path()).unwrap();
        assert_eq!(value(&reader), 7);
        // Its close would rewrite the header, which is not what this tests
        std::mem::forget(writer);
    }

    /// Simulates power loss: once `dead`, every write is dropped
    #[derive(Debug)]
    struct CrashBackend {
        inner: redb::backends::FileBackend,
        dead: Arc<AtomicBool>,
    }

    impl redb::StorageBackend for CrashBackend {
        fn len(&self) -> Result<u64, std::io::Error> {
            self.inner.len()
        }
        fn read(&self, offset: u64, out: &mut [u8]) -> Result<(), std::io::Error> {
            self.inner.read(offset, out)
        }
        fn set_len(&self, len: u64) -> Result<(), std::io::Error> {
            if self.dead.load(Ordering::SeqCst) {
                return Ok(());
            }
            self.inner.set_len(len)
        }
        fn sync_data(&self) -> Result<(), std::io::Error> {
            if self.dead.load(Ordering::SeqCst) {
                return Ok(());
            }
            self.inner.sync_data()
        }
        fn write(&self, offset: u64, data: &[u8]) -> Result<(), std::io::Error> {
            if self.dead.load(Ordering::SeqCst) {
                return Ok(());
            }
            self.inner.write(offset, data)
        }
    }

    /// A writer takes `SHARED_WRITER_BYTE` before `do_repair()` runs, so a reader admitted on that
    /// byte alone would walk a tree the repair is discarding. The consistent byte keeps it out
    #[test]
    fn a_repairing_writer_does_not_admit_a_shared_reader() {
        let tmpfile = tempfile::NamedTempFile::new().unwrap();

        // Crashed after a commit, so the next open has to repair it
        let dead = Arc::new(AtomicBool::new(false));
        let file = std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .open(tmpfile.path())
            .unwrap();
        let db = Database::builder()
            .create_with_backend(CrashBackend {
                inner: redb::backends::FileBackend::new(file).unwrap(),
                dead: Arc::clone(&dead),
            })
            .unwrap();
        let write = db.begin_write().unwrap();
        {
            let mut table = write.open_table(TABLE).unwrap();
            table.insert(0, 9).unwrap();
        }
        write.commit().unwrap();
        dead.store(true, Ordering::SeqCst);
        drop(db);

        // The repair callback fires inside do_repair()
        let path = tmpfile.path().to_path_buf();
        let during: Arc<std::sync::Mutex<Option<String>>> = Arc::new(std::sync::Mutex::new(None));
        let recorder = Arc::clone(&during);

        let mut builder = shared();
        builder.set_repair_callback(move |_| {
            let mut recorded = recorder.lock().unwrap();
            if recorded.is_some() {
                return;
            }
            *recorded = Some(match shared().open_read_only(&path) {
                Ok(_) => "opened".to_string(),
                Err(err) => format!("{err:?}"),
            });
        });
        let repaired = builder.open(tmpfile.path()).unwrap();

        assert_eq!(
            during.lock().unwrap().take().as_deref(),
            Some("RepairAborted"),
            "a shared reader was admitted while the database was still being repaired"
        );

        let reader = shared().open_read_only(tmpfile.path()).unwrap();
        assert_eq!(value(&reader), 9);
        drop(repaired);
    }
}

#[cfg(any(target_os = "linux", target_vendor = "apple", windows))]
mod writer_byte {
    use super::*;
    use redb::TableDefinition;
    use std::path::Path;
    use std::sync::mpsc;
    use std::thread;
    use std::time::Duration;

    const TABLE: TableDefinition<u64, u64> = TableDefinition::new("x");

    fn create(path: &Path, mode: ConcurrencyMode) -> Result<Database, DatabaseError> {
        let mut builder = Database::builder();
        builder.set_concurrency_mode(mode);
        builder.create(path)
    }

    /// Which is what makes one write transaction at a time across the cohort
    #[test]
    fn a_write_transaction_excludes_another_process() {
        let tmpfile = tempfile::NamedTempFile::new().unwrap();
        let db = create(tmpfile.path(), ConcurrencyMode::MultiWriter).unwrap();
        let peer = create(tmpfile.path(), ConcurrencyMode::MultiWriter).unwrap();

        let held = db.begin_write().unwrap();

        let (tx, rx) = mpsc::channel();
        let waiting = thread::spawn(move || {
            let write = peer.begin_write().unwrap();
            tx.send(()).unwrap();
            write.abort().unwrap();
        });

        assert!(
            rx.recv_timeout(Duration::from_millis(200)).is_err(),
            "a second process began a write transaction while the first was open"
        );
        held.commit().unwrap();
        rx.recv_timeout(Duration::from_secs(10))
            .expect("the waiting process never began its transaction");
        waiting.join().unwrap();
    }

    /// The byte is released by the whole transaction ending, not by the commit alone
    #[test]
    fn an_aborted_transaction_releases_the_byte() {
        let tmpfile = tempfile::NamedTempFile::new().unwrap();
        let db = create(tmpfile.path(), ConcurrencyMode::MultiWriter).unwrap();
        let peer = create(tmpfile.path(), ConcurrencyMode::MultiWriter).unwrap();

        db.begin_write().unwrap().abort().unwrap();
        // Dropped rather than aborted, which aborts through Drop
        drop(db.begin_write().unwrap());

        let write = peer.begin_write().unwrap();
        write.commit().unwrap();
    }

    /// It locks the whole file, which covers the writer byte
    #[test]
    fn an_exclusive_writer_transaction_does_not_puncture_the_whole_file_lock() {
        let tmpfile = tempfile::NamedTempFile::new().unwrap();
        let db = create(tmpfile.path(), ConcurrencyMode::ExclusiveWriter).unwrap();

        let write = db.begin_write().unwrap();
        {
            let mut table = write.open_table(TABLE).unwrap();
            table.insert(0, 0).unwrap();
        }
        write.commit().unwrap();

        assert!(matches!(
            Database::open(tmpfile.path()),
            Err(DatabaseError::DatabaseAlreadyOpen)
        ));
    }
}

#[cfg(any(target_os = "linux", target_vendor = "apple", windows))]
#[test]
fn the_concurrency_mode_excludes_incompatible_opens() {
    fn create(path: &std::path::Path, mode: ConcurrencyMode) -> Result<Database, DatabaseError> {
        let mut builder = Database::builder();
        builder.set_concurrency_mode(mode);
        builder.create(path)
    }

    let tmpfile = tempfile::NamedTempFile::new().unwrap();
    let db = create(tmpfile.path(), ConcurrencyMode::MultiWriter).unwrap();

    let peer = create(tmpfile.path(), ConcurrencyMode::MultiWriter).unwrap();
    assert!(matches!(
        create(tmpfile.path(), ConcurrencyMode::SingleWriter),
        Err(DatabaseError::DatabaseAlreadyOpen)
    ));
    assert!(matches!(
        Database::open(tmpfile.path()),
        Err(DatabaseError::DatabaseAlreadyOpen)
    ));

    drop(peer);
    drop(db);
    Database::open(tmpfile.path()).unwrap();
}

/// A multi-writer check runs on the file as another process last committed it, and the handle
/// goes on from there: fresh savepoint ids, and a close that commits after what it synced to
#[cfg(any(target_os = "linux", target_vendor = "apple", windows))]
#[test]
fn an_integrity_check_syncs_to_a_peers_commits() {
    use redb::{ReadableDatabase, ReadableTable, TableDefinition};
    const TABLE: TableDefinition<u64, u64> = TableDefinition::new("x");

    let tmpfile = tempfile::NamedTempFile::new().unwrap();
    let mut db = Database::builder()
        .set_concurrency_mode(ConcurrencyMode::MultiWriter)
        .create(tmpfile.path())
        .unwrap();
    let peer = Database::builder()
        .set_concurrency_mode(ConcurrencyMode::MultiWriter)
        .open(tmpfile.path())
        .unwrap();
    let txn = peer.begin_write().unwrap();
    txn.open_table(TABLE).unwrap().insert(1, 1).unwrap();
    txn.commit().unwrap();
    let txn = peer.begin_write().unwrap();
    let peers_savepoint = txn.persistent_savepoint().unwrap();
    txn.commit().unwrap();
    drop(peer);

    assert!(db.check_integrity().unwrap());
    let read = db.begin_read().unwrap();
    let table = read.open_table(TABLE).unwrap();
    assert_eq!(table.get(1).unwrap().unwrap().value(), 1);
    drop(table);
    drop(read);
    let txn = db.begin_write().unwrap();
    let own_savepoint = txn.persistent_savepoint().unwrap();
    assert_ne!(own_savepoint, peers_savepoint);
    txn.commit().unwrap();
    drop(db);

    let db = Database::builder()
        .set_concurrency_mode(ConcurrencyMode::MultiWriter)
        .open(tmpfile.path())
        .unwrap();
    let read = db.begin_read().unwrap();
    let table = read.open_table(TABLE).unwrap();
    assert_eq!(table.get(1).unwrap().unwrap().value(), 1);
    let txn = db.begin_write().unwrap();
    let mut savepoints: Vec<u64> = txn.list_persistent_savepoints().unwrap().collect();
    savepoints.sort_unstable();
    assert_eq!(savepoints, vec![peers_savepoint, own_savepoint]);
}

/// A shared commit is 2-phase, so a corrupt primary slot is corruption rather than a torn write
/// to fall back from: the check repairs nothing from under a process reading that slot
#[cfg(any(target_os = "linux", target_vendor = "apple", windows))]
#[test]
fn a_corrupt_primary_is_not_repaired_from_under_a_peers_read() {
    use redb::{ReadableDatabase, ReadableTable, TableDefinition};
    use std::io::{Read, Seek, SeekFrom, Write};
    const TABLE: TableDefinition<u64, u64> = TableDefinition::new("x");

    let tmpfile = tempfile::NamedTempFile::new().unwrap();
    let mut db = Database::builder()
        .set_concurrency_mode(ConcurrencyMode::MultiWriter)
        .create(tmpfile.path())
        .unwrap();
    for key in [1, 2] {
        let txn = db.begin_write().unwrap();
        txn.open_table(TABLE).unwrap().insert(key, key).unwrap();
        txn.commit().unwrap();
    }
    let peer = Database::builder()
        .set_concurrency_mode(ConcurrencyMode::MultiWriter)
        .open_read_only(tmpfile.path())
        .unwrap();
    let read = peer.begin_read().unwrap();

    // The header: the god byte at 9, whose low bit names the primary of the two 128-byte commit
    // slots at 64 and 192, each ending in a 16-byte checksum
    let mut file = std::fs::OpenOptions::new()
        .read(true)
        .write(true)
        .open(tmpfile.path())
        .unwrap();
    let mut god_byte = [0u8];
    file.seek(SeekFrom::Start(9)).unwrap();
    file.read_exact(&mut god_byte).unwrap();
    let checksum = 64 + 128 * u64::from(god_byte[0] & 1) + 112;
    let mut bytes = [0u8; 16];
    file.seek(SeekFrom::Start(checksum)).unwrap();
    file.read_exact(&mut bytes).unwrap();
    for byte in &mut bytes {
        *byte ^= 0xFF;
    }
    file.seek(SeekFrom::Start(checksum)).unwrap();
    file.write_all(&bytes).unwrap();
    file.sync_all().unwrap();
    drop(file);

    assert!(matches!(
        db.check_integrity(),
        Err(DatabaseError::Storage(StorageError::Corrupted(_)))
    ));
    let table = read.open_table(TABLE).unwrap();
    assert_eq!(table.get(1).unwrap().unwrap().value(), 1);
    assert_eq!(table.get(2).unwrap().unwrap().value(), 2);
}

/// The check waits for another process's write transaction to end, as a write transaction would
#[cfg(any(target_os = "linux", target_vendor = "apple", windows))]
#[test]
fn an_integrity_check_waits_for_a_peers_write_transaction() {
    use std::sync::mpsc;
    use std::thread;
    use std::time::Duration;

    let tmpfile = tempfile::NamedTempFile::new().unwrap();
    let mut db = Database::builder()
        .set_concurrency_mode(ConcurrencyMode::MultiWriter)
        .create(tmpfile.path())
        .unwrap();
    let peer = Database::builder()
        .set_concurrency_mode(ConcurrencyMode::MultiWriter)
        .open(tmpfile.path())
        .unwrap();
    let txn = peer.begin_write().unwrap();

    let (tx, rx) = mpsc::channel();
    let checking = thread::spawn(move || {
        tx.send(db.check_integrity().unwrap()).unwrap();
        db
    });
    assert!(
        rx.recv_timeout(Duration::from_millis(200)).is_err(),
        "the check ran under another process's write transaction"
    );
    txn.abort().unwrap();
    assert!(
        rx.recv_timeout(Duration::from_secs(10))
            .expect("the check never ran")
    );
    checking.join().unwrap();
}

/// A multi-writer handle's write transaction begins from the file as another process last
/// committed it, and so does the commit its close makes.
#[cfg(any(target_os = "linux", target_vendor = "apple", windows))]
mod peer_commits {
    use super::*;
    use redb::{CompactionError, ReadableDatabase, ReadableTable, TableDefinition};
    use std::path::Path;

    const TABLE: TableDefinition<u64, u64> = TableDefinition::new("x");

    fn open(path: &Path) -> Database {
        Database::builder()
            .set_concurrency_mode(ConcurrencyMode::MultiWriter)
            .open(path)
            .unwrap()
    }

    /// Two handles on a file that a clean close left behind, so that neither open has to
    /// repair it and commit.
    fn two_handles(path: &Path) -> (Database, Database) {
        Database::builder()
            .set_concurrency_mode(ConcurrencyMode::MultiWriter)
            .create(path)
            .unwrap();
        (open(path), open(path))
    }

    fn insert(db: &Database, key: u64) {
        let txn = db.begin_write().unwrap();
        txn.open_table(TABLE).unwrap().insert(key, key).unwrap();
        txn.commit().unwrap();
    }

    fn value(db: &Database, key: u64) -> u64 {
        let read = db.begin_read().unwrap();
        let table = read.open_table(TABLE).unwrap();
        table.get(key).unwrap().unwrap().value()
    }

    /// A read on a writable handle reads the file, so it sees a peer's commit without this
    /// handle writing anything itself.
    #[test]
    fn a_read_transaction_on_a_writable_handle_sees_a_peers_commit() {
        let tmpfile = tempfile::NamedTempFile::new().unwrap();
        let (db, peer) = two_handles(tmpfile.path());
        insert(&peer, 1);

        assert_eq!(value(&db, 1), 1);
    }

    /// The transaction sees the peer's commit, and its own commit lands after it, so that
    /// neither one is lost.
    #[test]
    fn a_write_transaction_syncs_to_a_peers_commit() {
        let tmpfile = tempfile::NamedTempFile::new().unwrap();
        let (db, peer) = two_handles(tmpfile.path());
        insert(&peer, 1);

        let txn = db.begin_write().unwrap();
        {
            let mut table = txn.open_table(TABLE).unwrap();
            assert_eq!(table.get(1).unwrap().unwrap().value(), 1);
            table.insert(2, 2).unwrap();
        }
        txn.commit().unwrap();
        let txn = peer.begin_write().unwrap();
        {
            let table = txn.open_table(TABLE).unwrap();
            assert_eq!(table.get(1).unwrap().unwrap().value(), 1);
            assert_eq!(table.get(2).unwrap().unwrap().value(), 2);
        }
        txn.abort().unwrap();
        drop(peer);
        drop(db);

        let db = open(tmpfile.path());
        assert_eq!(value(&db, 1), 1);
        assert_eq!(value(&db, 2), 2);
    }

    /// The peer's persistent savepoint is held from that transaction onwards, and a savepoint
    /// the transaction creates itself takes a fresh id. A savepoint the peer has deleted is
    /// forgotten, releasing the pin it held.
    #[test]
    fn a_write_transaction_syncs_to_a_peers_savepoint() {
        let tmpfile = tempfile::NamedTempFile::new().unwrap();
        let (mut db, peer) = two_handles(tmpfile.path());
        let txn = peer.begin_write().unwrap();
        let peers_savepoint = txn.persistent_savepoint().unwrap();
        txn.commit().unwrap();

        db.begin_write().unwrap().abort().unwrap();
        assert!(matches!(
            db.compact(),
            Err(CompactionError::PersistentSavepointExists)
        ));
        let txn = db.begin_write().unwrap();
        let own_savepoint = txn.persistent_savepoint().unwrap();
        assert_ne!(own_savepoint, peers_savepoint);
        txn.commit().unwrap();
        {
            let txn = db.begin_write().unwrap();
            let mut savepoints: Vec<u64> = txn.list_persistent_savepoints().unwrap().collect();
            savepoints.sort_unstable();
            assert_eq!(savepoints, vec![peers_savepoint, own_savepoint]);
            txn.abort().unwrap();
        }

        let txn = peer.begin_write().unwrap();
        assert!(txn.delete_persistent_savepoint(peers_savepoint).unwrap());
        assert!(txn.delete_persistent_savepoint(own_savepoint).unwrap());
        txn.commit().unwrap();
        // Straight to the compaction: it refreshes the savepoints the peer deleted itself
        db.compact().unwrap();
    }

    #[test]
    fn an_aborted_savepoint_is_not_revived_by_a_peer() {
        check_abandoned_savepoint(|txn| txn.abort().unwrap());
    }

    #[test]
    fn a_dropped_transactions_savepoint_is_not_revived_by_a_peer() {
        check_abandoned_savepoint(drop);
    }

    #[test]
    fn a_panicked_transactions_savepoint_is_not_revived_by_a_peer() {
        check_abandoned_savepoint(|txn| {
            let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                let _txn = txn;
                panic!("unwinding through the transaction");
            }));
            assert!(unwound.is_err());
        });
    }

    fn check_abandoned_savepoint(abandon: impl Fn(redb::WriteTransaction)) {
        for advance_snapshot in [false, true] {
            let tmpfile = tempfile::NamedTempFile::new().unwrap();
            let (mut db, peer) = two_handles(tmpfile.path());
            insert(&db, 0);

            let txn = db.begin_write().unwrap();
            let id = txn.persistent_savepoint().unwrap();
            let stale = txn.get_persistent_savepoint(id).unwrap();
            abandon(txn);

            if advance_snapshot {
                insert(&peer, 1);
            }
            let txn = peer.begin_write().unwrap();
            assert_eq!(txn.persistent_savepoint().unwrap(), id);
            txn.commit().unwrap();

            let mut txn = db.begin_write().unwrap();
            assert!(matches!(
                txn.restore_savepoint(&stale),
                Err(redb::SavepointError::InvalidSavepoint)
            ));
            let replacement = txn.get_persistent_savepoint(id).unwrap();
            let another_handle = txn.get_persistent_savepoint(id).unwrap();
            txn.abort().unwrap();
            drop(stale);

            // Unchanged registrations retain their identity across peer commits and lookups.
            insert(&peer, 2);
            let mut txn = db.begin_write().unwrap();
            txn.restore_savepoint(&replacement).unwrap();
            txn.restore_savepoint(&another_handle).unwrap();
            txn.commit().unwrap();
            {
                let read = peer.begin_read().unwrap();
                let table = read.open_table(TABLE).unwrap();
                assert_eq!(table.get(0).unwrap().unwrap().value(), 0);
                assert_eq!(table.get(1).unwrap().is_some(), advance_snapshot);
                assert!(table.get(2).unwrap().is_none());
            }

            let txn = peer.begin_write().unwrap();
            assert!(txn.delete_persistent_savepoint(id).unwrap());
            txn.commit().unwrap();
            let mut txn = db.begin_write().unwrap();
            assert!(matches!(
                txn.restore_savepoint(&replacement),
                Err(redb::SavepointError::InvalidSavepoint)
            ));
            txn.abort().unwrap();
            drop(replacement);
            drop(another_handle);
            db.compact().unwrap();
            assert!(db.check_integrity().unwrap());
        }
    }

    /// When the close's commit cannot sync to the file's latest commit, the close writes no
    /// shutdown header. Writing one would put this handle's stale header, marked clean, over
    /// that commit.
    #[test]
    fn a_close_writes_no_header_over_a_commit_it_could_not_sync_to() {
        use std::io::{Read, Seek, SeekFrom, Write};

        let tmpfile = tempfile::NamedTempFile::new().unwrap();
        let (db, peer) = two_handles(tmpfile.path());
        insert(&peer, 1);
        drop(peer);
        // The peer's commit with its primary slot's checksum flipped: corruption, not a torn
        // write to fall back from, since a shared commit is 2-phase. The god byte at 9 names the
        // primary of the two 128-byte slots at 64 and 192, each ending in a 16-byte checksum
        let mut file = std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .open(tmpfile.path())
            .unwrap();
        let mut god_byte = [0u8];
        file.seek(SeekFrom::Start(9)).unwrap();
        file.read_exact(&mut god_byte).unwrap();
        let checksum = 64 + 128 * u64::from(god_byte[0] & 1) + 112;
        let mut flipped = [0u8; 16];
        file.seek(SeekFrom::Start(checksum)).unwrap();
        file.read_exact(&mut flipped).unwrap();
        for byte in &mut flipped {
            *byte ^= 0xFF;
        }
        file.seek(SeekFrom::Start(checksum)).unwrap();
        file.write_all(&flipped).unwrap();
        file.sync_all().unwrap();

        drop(db);
        let mut after = [0u8; 16];
        file.seek(SeekFrom::Start(checksum)).unwrap();
        file.read_exact(&mut after).unwrap();
        assert_eq!(
            after, flipped,
            "the close wrote its header over the peer's commit"
        );
    }

    /// The close commits from the file as the peer last committed it, rather than from the
    /// state this handle had.
    #[test]
    fn a_close_records_a_peers_commit() {
        let tmpfile = tempfile::NamedTempFile::new().unwrap();
        let (db, peer) = two_handles(tmpfile.path());
        insert(&peer, 1);
        drop(peer);
        drop(db);

        let db = open(tmpfile.path());
        assert_eq!(value(&db, 1), 1);
    }

    /// A transaction dropped while a panic unwinds leaves its pages in the write buffer, and
    /// the peer can then commit those same pages. The sync discards the buffer, so the page
    /// reads back as the peer committed it.
    #[test]
    fn a_write_transaction_reads_a_peers_page_over_a_page_a_panic_left_buffered() {
        let tmpfile = tempfile::NamedTempFile::new().unwrap();
        let (db, peer) = two_handles(tmpfile.path());
        let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let txn = db.begin_write().unwrap();
            txn.open_table(TABLE).unwrap().insert(1, 1).unwrap();
            panic!("unwinding through the transaction");
        }));
        assert!(unwound.is_err());
        let txn = peer.begin_write().unwrap();
        txn.open_table(TABLE).unwrap().insert(1, 2).unwrap();
        txn.commit().unwrap();

        let txn = db.begin_write().unwrap();
        assert_eq!(
            txn.open_table(TABLE)
                .unwrap()
                .get(1)
                .unwrap()
                .unwrap()
                .value(),
            2
        );
    }
}

#[cfg(any(target_os = "linux", target_vendor = "apple", windows))]
#[test]
fn file_backend_custom_open_supports_shared_modes() {
    use redb::backends::FileBackend;
    use redb::{ReadableDatabase, ReadableTable, TableDefinition};

    const TABLE: TableDefinition<u64, u64> = TableDefinition::new("x");
    for mode in [ConcurrencyMode::SingleWriter, ConcurrencyMode::MultiWriter] {
        let tmpfile = tempfile::NamedTempFile::new().unwrap();
        let mut builder = Database::builder();
        builder.set_concurrency_mode(mode);
        let open =
            || builder.create_with_backend(FileBackend::new(tmpfile.reopen().unwrap()).unwrap());
        let db = open().unwrap();
        let write = db.begin_write().unwrap();
        write.open_table(TABLE).unwrap().insert(0, 0).unwrap();
        write.commit().unwrap();

        let reader = builder.open_read_only(tmpfile.path()).unwrap();
        let snapshot = reader.begin_read().unwrap().open_table(TABLE).unwrap();
        assert!(matches!(
            Database::open(tmpfile.path()),
            Err(DatabaseError::DatabaseAlreadyOpen)
        ));

        let peer = if mode == ConcurrencyMode::MultiWriter {
            Some(open().unwrap())
        } else {
            assert!(matches!(open(), Err(DatabaseError::DatabaseAlreadyOpen)));
            None
        };
        for value in 1..4 {
            let writer = peer.as_ref().unwrap_or(&db);
            let write = writer.begin_write().unwrap();
            write.open_table(TABLE).unwrap().insert(0, value).unwrap();
            write.commit().unwrap();

            assert_eq!(snapshot.get(0).unwrap().unwrap().value(), 0);
            for current in [reader.begin_read().unwrap(), db.begin_read().unwrap()] {
                assert_eq!(
                    current
                        .open_table(TABLE)
                        .unwrap()
                        .get(0)
                        .unwrap()
                        .unwrap()
                        .value(),
                    value
                );
            }
        }
        drop(snapshot);
        drop(reader);
        drop(peer);
        drop(db);
        Database::open(tmpfile.path()).unwrap();
    }
}

/// A backend without locks can only be used in exclusive-writer mode.
#[test]
fn sharing_a_backend_without_locks_is_unsupported() {
    let mut builder = Database::builder();
    builder.set_concurrency_mode(ConcurrencyMode::MultiWriter);
    let err = builder
        .create_with_backend(InMemoryBackend::new())
        .unwrap_err();
    assert!(matches!(
        err,
        DatabaseError::Storage(StorageError::Unsupported)
    ));

    // ... while the default mode opens on one as it always has
    Database::builder()
        .create_with_backend(InMemoryBackend::new())
        .unwrap();
}

#[cfg(any(target_os = "linux", target_vendor = "apple", windows))]
mod reclamation {
    use super::*;
    use redb::{ReadableDatabase, ReadableTable, TableDefinition};

    const TABLE: TableDefinition<u64, &[u8]> = TableDefinition::new("x");

    /// A read transaction in another process pins its snapshot: the writer's reclamation stops
    /// at it, however many commits go by
    #[test]
    fn a_readers_pin_survives_heavy_reclamation() {
        let tmpfile = tempfile::NamedTempFile::new().unwrap();
        let writer = Database::builder()
            .set_concurrency_mode(ConcurrencyMode::SingleWriter)
            .create(tmpfile.path())
            .unwrap();

        let value = vec![7u8; 512];
        let txn = writer.begin_write().unwrap();
        {
            let mut t = txn.open_table(TABLE).unwrap();
            for key in 0..128u64 {
                t.insert(&key, value.as_slice()).unwrap();
            }
        }
        txn.commit().unwrap();

        let reader = Database::builder()
            .set_concurrency_mode(ConcurrencyMode::SingleWriter)
            .open_read_only(tmpfile.path())
            .unwrap();
        let pinned = reader.begin_read().unwrap();

        // Every round frees the previous round's pages; without the reader's pin bounding the
        // writer's reclamation, the pinned snapshot's pages would be reused under it
        for round in 0..20u8 {
            let overwrite = vec![round; 512];
            let txn = writer.begin_write().unwrap();
            {
                let mut t = txn.open_table(TABLE).unwrap();
                for key in 0..128u64 {
                    t.insert(&key, overwrite.as_slice()).unwrap();
                }
            }
            txn.commit().unwrap();
        }

        let t = pinned.open_table(TABLE).unwrap();
        for key in 0..128u64 {
            assert_eq!(t.get(&key).unwrap().unwrap().value(), value.as_slice());
        }
        drop(t);
        drop(pinned);

        // With the pin gone the writer keeps going, and a fresh read sees the last round
        let txn = writer.begin_write().unwrap();
        {
            let mut t = txn.open_table(TABLE).unwrap();
            t.insert(&0, [0u8; 4].as_slice()).unwrap();
        }
        txn.commit().unwrap();
        let read = reader.begin_read().unwrap();
        let t = read.open_table(TABLE).unwrap();
        assert_eq!(t.get(&0).unwrap().unwrap().value(), [0u8; 4].as_slice());
    }
}

#[cfg(any(target_os = "linux", target_vendor = "apple", windows))]
mod compaction {
    use super::*;
    use redb::{
        CompactionError, MultimapTableDefinition, ReadOnlyDatabase, ReadableDatabase,
        ReadableTable, ReadableTableMetadata, TableDefinition,
    };
    use std::path::Path;

    const TABLE: TableDefinition<u64, &[u8]> = TableDefinition::new("x");

    fn create(path: &Path, mode: ConcurrencyMode) -> Database {
        Database::builder()
            .set_concurrency_mode(mode)
            .create(path)
            .unwrap()
    }

    fn open_read_only(path: &Path, mode: ConcurrencyMode) -> ReadOnlyDatabase {
        Database::builder()
            .set_concurrency_mode(mode)
            .open_read_only(path)
            .unwrap()
    }

    fn insert(db: &Database, keys: std::ops::Range<u64>, value: &[u8]) {
        let txn = db.begin_write().unwrap();
        {
            let mut t = txn.open_table(TABLE).unwrap();
            for key in keys {
                t.insert(&key, value).unwrap();
            }
        }
        txn.commit().unwrap();
    }

    /// A read transaction in another process is a transaction in progress, as a local one is
    #[test]
    fn compaction_refuses_a_peers_read_transaction() {
        for mode in [ConcurrencyMode::SingleWriter, ConcurrencyMode::MultiWriter] {
            let tmpfile = tempfile::NamedTempFile::new().unwrap();
            let mut writer = create(tmpfile.path(), mode);
            insert(&writer, 0..1, &[1u8; 512]);

            let reader = open_read_only(tmpfile.path(), mode);
            let pinned = reader.begin_read().unwrap();
            assert!(
                matches!(
                    writer.compact(),
                    Err(CompactionError::TransactionInProgress)
                ),
                "{mode:?}"
            );

            drop(pinned);
            writer.compact().unwrap();
            let read = reader.begin_read().unwrap();
            let t = read.open_table(TABLE).unwrap();
            assert_eq!(t.get(&0).unwrap().unwrap().value(), [1u8; 512].as_slice());
        }
    }

    #[test]
    fn untyped_tables_pin_a_peers_snapshot_until_dropped() {
        const MULTIMAP: MultimapTableDefinition<u64, &[u8]> = MultimapTableDefinition::new("multi");
        for mode in [ConcurrencyMode::SingleWriter, ConcurrencyMode::MultiWriter] {
            for multimap in [false, true] {
                let tmpfile = tempfile::NamedTempFile::new().unwrap();
                let mut writer = create(tmpfile.path(), mode);
                let value = [7u8; 512];
                let write = writer.begin_write().unwrap();
                {
                    let mut table = write.open_table(TABLE).unwrap();
                    let mut multi = write.open_multimap_table(MULTIMAP).unwrap();
                    for key in 0..128u64 {
                        table.insert(key, value.as_slice()).unwrap();
                        multi.insert(key, value.as_slice()).unwrap();
                    }
                }
                write.commit().unwrap();

                let reader = Database::builder()
                    .set_concurrency_mode(mode)
                    .set_cache_size(0)
                    .open_read_only(tmpfile.path())
                    .unwrap();
                let read = reader.begin_read().unwrap();
                let table: Box<dyn ReadableTableMetadata> = if multimap {
                    Box::new(read.open_untyped_multimap_table(MULTIMAP).unwrap())
                } else {
                    Box::new(read.open_untyped_table(TABLE).unwrap())
                };
                let before = table.stats().unwrap();
                assert!(before.branch_pages() > 0);
                drop(read);

                let write = writer.begin_write().unwrap();
                write.delete_table(TABLE).unwrap();
                write.delete_multimap_table(MULTIMAP).unwrap();
                write.commit().unwrap();
                for _ in 0..3 {
                    writer.begin_write().unwrap().commit().unwrap();
                }
                assert!(
                    matches!(
                        writer.compact(),
                        Err(CompactionError::TransactionInProgress)
                    ),
                    "{mode:?}, multimap={multimap}"
                );

                // The table alone pins the snapshot, and an empty cache forces stats to read it.
                let after = table.stats().unwrap();
                assert_eq!(table.len().unwrap(), 128);
                assert_eq!(after.stored_bytes(), before.stored_bytes());
                assert_eq!(after.tree_height(), before.tree_height());
                assert_eq!(after.leaf_pages(), before.leaf_pages());
                assert_eq!(after.branch_pages(), before.branch_pages());

                drop(table);
                writer.compact().unwrap();
            }
        }
    }

    /// The file shrinks under a peer that has it open, and the peer's next read follows the pages
    /// that moved
    #[test]
    fn compaction_shrinks_a_file_a_peer_has_open() {
        for mode in [ConcurrencyMode::SingleWriter, ConcurrencyMode::MultiWriter] {
            let tmpfile = tempfile::NamedTempFile::new().unwrap();
            let mut writer = create(tmpfile.path(), mode);
            let value = vec![7u8; 1024];
            insert(&writer, 0..512, &value);

            // Caches the pages the compaction is about to move
            let reader = open_read_only(tmpfile.path(), mode);
            {
                let read = reader.begin_read().unwrap();
                let t = read.open_table(TABLE).unwrap();
                assert_eq!(t.get(&511).unwrap().unwrap().value(), value.as_slice());
            }

            let txn = writer.begin_write().unwrap();
            {
                let mut t = txn.open_table(TABLE).unwrap();
                for key in 8..512u64 {
                    t.remove(&key).unwrap();
                }
            }
            txn.commit().unwrap();

            let before = std::fs::metadata(tmpfile.path()).unwrap().len();
            assert!(writer.compact().unwrap(), "{mode:?}");
            let after = std::fs::metadata(tmpfile.path()).unwrap().len();
            assert!(after < before, "{mode:?}: {before} -> {after}");

            let read = reader.begin_read().unwrap();
            let t = read.open_table(TABLE).unwrap();
            for key in 0..8u64 {
                assert_eq!(t.get(&key).unwrap().unwrap().value(), value.as_slice());
            }
            assert!(t.get(&8).unwrap().is_none());
        }
    }
}