mushroomdb 0.6.9

Embedded graph database with Cypher queries, rule triggers, and Arrow export
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
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
//! Tests for Task 4b: group-commit write queue (spec B3.5).
//!
//! Test list:
//!  1. group_atomicity_all_or_nothing — submissions in a group never torn
//!  2. one_fsync_per_group — exactly one Fs::sync for N submissions in a group
//!  3. fifo_within_caller — sequential submits preserve commit ordering
//!  4. concurrent_submitters_all_commit — 8 threads submit concurrently, all land
//!  5. crash_before_group_fsync_loses_group — unsynced group is lost on crash
//!  6. intra_group_prefix_survives_crash — frame-1 of 2-sub group survives crash mid-frame-2
//!  7. direct_api_unchanged — write/read/write_batch/insert_node still work
//!  8. deferred_events_fire_after_flush — events buffered until flush (R2)
//!  9. deferred_events_discarded_on_failure — events discarded when fsync fails (R2)
//! 10. group_commit_throughput_bench (ignored) — RealFs informational bench
//! 11. group_commit_simfs_amortization_bench (ignored) — SimFs gate: 8 writers >= 3x serial
//! 12. shared_db_fsync_failure_degrades_and_truncates_wal — F1(c) integration via SharedDb
//! 13. direct_write_before_group_survives_group_fsync_failure — F1(c) concurrent variant
//! 14. write_batch_strict_always_fsyncs — N single-op + one 5-op batch → correct fsync count

use core_api::{BatchOp, FsyncPolicy, GraphDb, MutationEvent, SharedDb};
use core_storage::fs::{FileId, Fs, FsIntrospect};
use std::collections::HashMap;
use std::sync::{Arc, Barrier, Mutex};
use std::thread;

// ── Test helpers ──────────────────────────────────────────────────────────────

fn tmp(name: &str) -> std::path::PathBuf {
    let d = std::env::temp_dir().join(format!(
        "graphdb-gc-{}-{}-{}",
        name,
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .subsec_nanos()
    ));
    let _ = std::fs::remove_dir_all(&d);
    d
}

/// Minimal counting Fs for sync-count assertions.
#[derive(Default)]
struct CountingFs {
    files: HashMap<FileId, Vec<u8>>,
    syncs: usize,
}

impl Fs for CountingFs {
    fn append(&mut self, file: FileId, data: &[u8]) -> std::io::Result<()> {
        self.files.entry(file).or_default().extend_from_slice(data);
        Ok(())
    }

    fn sync(&mut self, _file: FileId) -> std::io::Result<()> {
        self.syncs += 1;
        Ok(())
    }

    fn read(&self, file: FileId) -> std::io::Result<Vec<u8>> {
        Ok(self.files.get(&file).cloned().unwrap_or_default())
    }

    fn write_atomic(&mut self, file: FileId, data: &[u8]) -> std::io::Result<()> {
        self.files.insert(file, data.to_vec());
        Ok(())
    }
}

impl FsIntrospect for CountingFs {
    fn total_appended(&self) -> usize {
        0
    }

    fn sync_count(&self) -> usize {
        self.syncs
    }
}

fn counting_db() -> GraphDb<CountingFs> {
    GraphDb::open_with(CountingFs::default()).unwrap()
}

// ── Test 1: group atomicity — submissions in a group are individually crash-atomic ──

#[test]
fn group_atomicity_each_submission_is_a_separate_wal_frame() {
    // Two submissions in one commit_group call must each be a separate WAL
    // Batch frame.  Verify by inspecting WAL frame count on recovery.
    use core_storage::wal::decode_all;
    use sim_harness::SimFs;

    let fs = SimFs::new();
    let mut db = GraphDb::open_with(fs).unwrap();

    let g = vec![
        vec![BatchOp::InsertNode {
            label: "A".into(),
            key: "sub1".into(),
            props: vec![],
        }],
        vec![BatchOp::InsertNode {
            label: "A".into(),
            key: "sub2".into(),
            props: vec![],
        }],
    ];

    let (results, sync_err) = db.commit_group(g);
    assert!(results.iter().all(|r| r.is_ok()), "both submissions ok");
    assert!(sync_err.is_none(), "no sync error");

    // Verify in-memory state.
    assert!(db.has_node("sub1"));
    assert!(db.has_node("sub2"));

    // Verify WAL has two top-level Batch frames (one per submission).
    let fs = db.into_fs();
    let wal = fs.read(FileId::Wal).unwrap();
    let (records, _) = decode_all(&wal);
    let batch_count = records
        .iter()
        .filter(|r| matches!(r, core_storage::wal::WalRecord::Batch(_)))
        .count();
    assert_eq!(
        batch_count, 2,
        "two submissions must produce two WAL Batch frames"
    );
}

// ── Test 2: exactly one Fs::sync per commit_group call ───────────────────────

#[test]
fn one_fsync_per_group_strict_policy() {
    let mut db = counting_db();
    // Default policy is Strict.

    let groups: Vec<Vec<BatchOp>> = (0..8)
        .map(|i| {
            vec![BatchOp::InsertNode {
                label: "A".into(),
                key: format!("n{i}"),
                props: vec![],
            }]
        })
        .collect();

    let (results, sync_err) = db.commit_group(groups);
    assert!(results.iter().all(|r| r.is_ok()), "all submissions ok");
    assert!(sync_err.is_none(), "sync succeeded");
    assert_eq!(
        db.fs_sync_count(),
        1,
        "group of 8 submissions must use exactly ONE fsync"
    );
    assert_eq!(db.node_count(), 8);
}

#[test]
fn two_groups_produce_two_fsyncs() {
    let mut db = counting_db();

    let g1 = vec![vec![BatchOp::InsertNode {
        label: "A".into(),
        key: "a".into(),
        props: vec![],
    }]];
    let g2 = vec![vec![BatchOp::InsertNode {
        label: "A".into(),
        key: "b".into(),
        props: vec![],
    }]];

    db.commit_group(g1);
    db.commit_group(g2);
    assert_eq!(
        db.fs_sync_count(),
        2,
        "two separate commit_group calls = two fsyncs"
    );
}

#[test]
fn relaxed_policy_group_skips_fsync() {
    let mut db = counting_db();
    db.set_fsync_policy(FsyncPolicy::Relaxed);

    let groups: Vec<Vec<BatchOp>> = (0..4)
        .map(|i| {
            vec![BatchOp::InsertNode {
                label: "A".into(),
                key: format!("r{i}"),
                props: vec![],
            }]
        })
        .collect();

    let (results, sync_err) = db.commit_group(groups);
    assert!(results.iter().all(|r| r.is_ok()));
    assert!(sync_err.is_none());
    assert_eq!(
        db.fs_sync_count(),
        0,
        "Relaxed policy must skip all fsyncs even in a group"
    );
}

// ── Test 3: FIFO ordering within one caller ───────────────────────────────────

#[test]
fn fifo_ordering_within_single_caller() {
    let dir = tmp("fifo");
    let db = SharedDb::open(&dir).unwrap();

    const N: usize = 20;
    let mut prev_nodes = 0usize;

    // Each submit_batch call is a serialized round-trip through the queue.
    // After each call, the node count must be monotonically non-decreasing.
    for i in 0..N {
        let ops = vec![BatchOp::InsertNode {
            label: "A".into(),
            key: format!("seq{i}"),
            props: vec![],
        }];
        db.submit_batch(ops).unwrap();
        let n = db.read().node_count();
        assert!(
            n >= prev_nodes,
            "node count must not decrease: was {prev_nodes}, now {n}"
        );
        prev_nodes = n;
    }
    assert_eq!(db.read().node_count(), N);
}

// ── Test 4: concurrent submitters all commit ──────────────────────────────────

#[test]
fn concurrent_submitters_all_commit() {
    let dir = tmp("conc");
    let db = SharedDb::open(&dir).unwrap();
    const WRITERS: usize = 8;
    const OPS_PER_WRITER: usize = 25;

    let start = Arc::new(Barrier::new(WRITERS));
    let handles: Vec<_> = (0..WRITERS)
        .map(|w| {
            let db = db.clone();
            let start = Arc::clone(&start);
            thread::spawn(move || {
                start.wait(); // all writers start simultaneously
                for i in 0..OPS_PER_WRITER {
                    let key = format!("w{w}_n{i}");
                    db.submit_batch(vec![BatchOp::InsertNode {
                        label: "N".into(),
                        key,
                        props: vec![],
                    }])
                    .expect("submit_batch must succeed");
                }
            })
        })
        .collect();

    for h in handles {
        h.join().expect("writer thread panicked");
    }

    let expected = WRITERS * OPS_PER_WRITER;
    let actual = db.read().node_count();
    assert_eq!(
        actual, expected,
        "all {expected} concurrent submissions must commit"
    );
}

// ── Test 5: crash before group fsync loses the unsynced group ─────────────────

#[test]
fn crash_before_group_fsync_loses_unsynced_group() {
    use core_storage::fs::FileId;
    use sim_harness::SimFs;

    // Group 1: commit_group (includes fsync) — survives crash.
    let fs = SimFs::new();
    let mut db = GraphDb::open_with(fs).unwrap();

    let g1 = vec![vec![BatchOp::InsertNode {
        label: "A".into(),
        key: "g1".into(),
        props: vec![],
    }]];
    db.commit_group(g1);

    // Capture WAL after group 1's fsync.
    let after_g1_bytes = db.fs_total_appended();

    // Group 2: commit_group_nosync — appended but NOT synced.
    let g2 = vec![
        vec![BatchOp::InsertNode {
            label: "A".into(),
            key: "g2a".into(),
            props: vec![],
        }],
        vec![BatchOp::InsertNode {
            label: "A".into(),
            key: "g2b".into(),
            props: vec![],
        }],
    ];
    db.commit_group_nosync(g2);

    // Simulate crash: truncate WAL to the synced portion (discard g2 frames).
    let fs = db.into_fs();
    let wal = fs.read(FileId::Wal).unwrap();
    assert!(
        wal.len() > after_g1_bytes,
        "WAL must contain g2 bytes before crash"
    );

    // Reconstruct survivor: trim WAL to synced bytes.
    let mut survivor = SimFs::new();
    // Copy snapshot if present.
    let snap = fs.read(FileId::Snapshot).unwrap();
    if !snap.is_empty() {
        survivor.write_atomic(FileId::Snapshot, &snap).unwrap();
    }
    survivor
        .write_atomic(FileId::Wal, &wal[..after_g1_bytes])
        .unwrap();

    let db2 = GraphDb::open_with(survivor).unwrap();
    assert!(db2.has_node("g1"), "g1 (synced group) must survive");
    assert!(
        !db2.has_node("g2a"),
        "g2a (unsynced group) must be lost on crash"
    );
    assert!(
        !db2.has_node("g2b"),
        "g2b (unsynced group) must be lost on crash"
    );
}

// ── Test 6: intra-group prefix survival — frame-1 survives crash mid-frame-2 ──
//
// When a group has 2+ submissions and the process crashes mid-second-frame,
// the first submission's WAL frame is complete and must survive replay.  The
// second frame is torn at the CRC boundary and must be dropped whole.

#[test]
fn intra_group_prefix_survives_crash() {
    use sim_harness::SimFs;

    // Probe: measure the byte size of one Batch frame (one InsertNode).
    let probe_fs = SimFs::new();
    let mut probe = GraphDb::open_with(probe_fs).unwrap();
    probe
        .commit_group_nosync(vec![vec![BatchOp::InsertNode {
            label: "A".into(),
            key: "s1".into(),
            props: vec![],
        }]])
        .into_iter()
        .for_each(|r| {
            r.unwrap();
        });
    let frame1_bytes = probe.fs_total_appended(); // bytes for exactly one Batch frame
    drop(probe);

    // Set up SimFs to crash 3 bytes into the SECOND frame (tears its CRC).
    let crash_at = frame1_bytes + 3;
    let fs = SimFs::with_crash_after(crash_at);
    let mut db = GraphDb::open_with(fs).unwrap();

    // Commit both submissions in one group_nosync call.
    // Frame 1 fits within crash_at; frame 2 is torn.
    let results = db.commit_group_nosync(vec![
        vec![BatchOp::InsertNode {
            label: "A".into(),
            key: "s1".into(),
            props: vec![],
        }],
        vec![BatchOp::InsertNode {
            label: "A".into(),
            key: "s2".into(),
            props: vec![],
        }],
    ]);
    // Frame 1 should succeed; frame 2 may err (crash mid-append) or appear
    // to succeed (crash after append but before in-process acknowledgement).
    let _ = results;

    // Replay from the surviving WAL (SimFs preserves up to crash_at bytes).
    let fs = db.into_fs();
    let survivor = fs.surviving_state();
    let db2 = GraphDb::open_with(survivor).unwrap();

    // Frame 1 is fully within crash_at → its ops must be present.
    assert!(
        db2.has_node("s1"),
        "first submission frame (before crash point) must survive"
    );
    // Frame 2 is torn → CRC mismatch → dropped on recovery.
    assert!(
        !db2.has_node("s2"),
        "torn second-frame submission must be dropped on recovery"
    );
    assert_eq!(
        db2.node_count(),
        1,
        "only the complete first frame survives"
    );
}

// ── Test 7: direct &mut self APIs unchanged ───────────────────────────────────

#[test]
fn direct_apis_unchanged_alongside_queue() {
    let dir = tmp("direct");
    let db = SharedDb::open(&dir).unwrap();

    // Direct write path still works.
    db.write()
        .insert_node("N", "direct1", vec![])
        .expect("direct insert_node must work");
    db.write()
        .insert_node("N", "direct2", vec![])
        .expect("direct insert_node must work");

    // Queue path works concurrently.
    db.submit_batch(vec![BatchOp::InsertNode {
        label: "N".into(),
        key: "queued1".into(),
        props: vec![],
    }])
    .expect("submit_batch must work");

    // write_batch still works.
    db.write()
        .write_batch(|b| {
            b.insert_node("N", "batch1", vec![]);
            b.insert_node("N", "batch2", vec![]);
        })
        .expect("write_batch must work");

    let n = db.read().node_count();
    assert_eq!(n, 5, "direct + queued + batch all committed");
}

// ── Test 8: deferred events fire after flush (R2) ─────────────────────────────
//
// Under Strict policy the drain thread defers subscriber events until after
// the group fsync.  Test the mechanism via commit_group_nosync + explicit
// deferred-events API on GraphDb (unit-level, no drain thread involvement).

#[test]
fn deferred_events_fire_after_flush() {
    use sim_harness::SimFs;

    let received: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
    let received2 = Arc::clone(&received);

    let fs = SimFs::new();
    let mut db = GraphDb::open_with(fs).unwrap();
    db.set_event_sink(Box::new(move |ev| {
        if let MutationEvent::NodeInserted { key, .. } = ev {
            received2.lock().unwrap().push(key);
        }
    }));

    // Enable deferred mode — simulates what the drain thread does.
    db.set_deferred_events_mode(true);

    // Commit; events must NOT fire yet.
    db.commit_group_nosync(vec![
        vec![BatchOp::InsertNode {
            label: "A".into(),
            key: "ev1".into(),
            props: vec![],
        }],
        vec![BatchOp::InsertNode {
            label: "A".into(),
            key: "ev2".into(),
            props: vec![],
        }],
    ]);
    assert!(
        received.lock().unwrap().is_empty(),
        "events must not fire before flush"
    );

    // Flush — simulates what the drain thread does after a successful fsync.
    db.flush_deferred_events();
    db.set_deferred_events_mode(false);

    let keys = received.lock().unwrap().clone();
    assert!(keys.contains(&"ev1".to_string()), "ev1 must be delivered");
    assert!(keys.contains(&"ev2".to_string()), "ev2 must be delivered");
}

// ── Test 9: deferred events discarded on fsync failure (R2) ──────────────────

#[test]
fn deferred_events_discarded_on_failure() {
    use sim_harness::SimFs;

    let received: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
    let received2 = Arc::clone(&received);

    let fs = SimFs::new();
    let mut db = GraphDb::open_with(fs).unwrap();
    db.set_event_sink(Box::new(move |ev| {
        if let MutationEvent::NodeInserted { key, .. } = ev {
            received2.lock().unwrap().push(key);
        }
    }));

    // Enable deferred mode and commit.
    db.set_deferred_events_mode(true);
    db.commit_group_nosync(vec![vec![BatchOp::InsertNode {
        label: "A".into(),
        key: "lost".into(),
        props: vec![],
    }]]);
    assert!(
        received.lock().unwrap().is_empty(),
        "events must not fire before discard"
    );

    // Discard — simulates what the drain thread does on fsync failure.
    db.discard_deferred_events();
    db.set_deferred_events_mode(false);

    // Even after discard+mode-off, no event must have been delivered.
    assert!(
        received.lock().unwrap().is_empty(),
        "discarded events must never be delivered to subscribers"
    );
}

// ── Test 10: RealFs throughput bench (informational, ignored) ────────────────

/// RealFs group-commit throughput bench — informational only, no ratio gate.
///
/// Records real-world throughput numbers for observability.  The amortization
/// gate lives in `group_commit_simfs_amortization_bench` (test 11) which uses
/// SimFs with injected fsync latency and is environment-independent.
///
/// # Serial baseline
///
/// Uses `db.write().insert_node()` under `FsyncPolicy::Strict`.  `write_batch`
/// would be equally correct now that the single-op batch durability bug is fixed
/// (see test 14), but `insert_node` is kept here for continuity.
///
/// Run manually with: `cargo test --release group_commit_throughput_bench -- --ignored --nocapture`
#[test]
#[ignore]
fn group_commit_throughput_bench() {
    use std::time::Instant;

    const WRITERS: usize = 8;
    const OPS_PER_WRITER: usize = 200;
    const TOTAL_OPS: usize = WRITERS * OPS_PER_WRITER;

    // ── Serialized-writer baseline (direct path, one fsync per insert_node) ──
    //
    // Uses db.write().insert_node() under FsyncPolicy::Strict so each call
    // acquires the write lock AND fsyncs exactly once before returning.
    let dir_serial = tmp("bench-serial");
    let db_serial = SharedDb::open(&dir_serial).unwrap();
    // Warm up OS page cache / WAL file.
    db_serial.write().insert_node("W", "warm", vec![]).unwrap();

    let t0 = Instant::now();
    for i in 0..TOTAL_OPS {
        db_serial
            .write()
            .insert_node("W", &format!("s{i}"), vec![])
            .unwrap();
    }
    let serial_elapsed = t0.elapsed();
    let serial_ops_per_s = TOTAL_OPS as f64 / serial_elapsed.as_secs_f64();

    // ── 8-concurrent-writer path (group-commit queue) ─────────────────────
    let dir_conc = tmp("bench-conc");
    let db_conc = SharedDb::open(&dir_conc).unwrap();

    // Warm up.
    {
        let db = db_conc.clone();
        db.submit_batch(vec![BatchOp::InsertNode {
            label: "W".into(),
            key: "warm".into(),
            props: vec![],
        }])
        .unwrap();
    }

    let start = Arc::new(Barrier::new(WRITERS));
    let t1 = Instant::now();
    let handles: Vec<_> = (0..WRITERS)
        .map(|w| {
            let db = db_conc.clone();
            let start = Arc::clone(&start);
            thread::spawn(move || {
                start.wait();
                for i in 0..OPS_PER_WRITER {
                    db.submit_batch(vec![BatchOp::InsertNode {
                        label: "W".into(),
                        key: format!("w{w}n{i}"),
                        props: vec![],
                    }])
                    .unwrap();
                }
            })
        })
        .collect();
    for h in handles {
        h.join().unwrap();
    }
    let conc_elapsed = t1.elapsed();
    let conc_ops_per_s = TOTAL_OPS as f64 / conc_elapsed.as_secs_f64();

    let ratio = conc_ops_per_s / serial_ops_per_s;

    // ── Reader-under-burst p95 (cheap proxy) ──────────────────────────────
    // Measure read latency while 8 writers are hammering the queue.
    let dir_reader = tmp("bench-reader");
    let db_reader = SharedDb::open(&dir_reader).unwrap();
    // Pre-populate a few nodes so reads are non-trivial.
    for i in 0..10 {
        db_reader
            .write()
            .insert_node("R", &format!("pre{i}"), vec![])
            .unwrap();
    }

    let start2 = Arc::new(Barrier::new(WRITERS + 1));
    let read_done = Arc::new(std::sync::atomic::AtomicBool::new(false));
    let read_latencies: Arc<Mutex<Vec<u128>>> = Arc::new(Mutex::new(Vec::new()));

    let writer_handles: Vec<_> = (0..WRITERS)
        .map(|w| {
            let db = db_reader.clone();
            let start2 = Arc::clone(&start2);
            let read_done = Arc::clone(&read_done);
            thread::spawn(move || {
                start2.wait();
                let mut i = 0usize;
                while !read_done.load(std::sync::atomic::Ordering::Relaxed) {
                    let _ = db.submit_batch(vec![BatchOp::InsertNode {
                        label: "W".into(),
                        key: format!("bw{w}_{i}"),
                        props: vec![],
                    }]);
                    i += 1;
                }
            })
        })
        .collect();

    let lat_db = db_reader.clone();
    let lat_lats = Arc::clone(&read_latencies);
    let lat_start = Arc::clone(&start2);
    let lat_read_done = Arc::clone(&read_done);
    let reader_handle = thread::spawn(move || {
        lat_start.wait();
        let deadline = Instant::now() + std::time::Duration::from_millis(500);
        while Instant::now() < deadline {
            let t = Instant::now();
            let _ = lat_db.reader().query(
                "MATCH (n:R) RETURN n.id",
                &std::collections::BTreeMap::new(),
            );
            let elapsed_us = t.elapsed().as_micros();
            lat_lats.lock().unwrap().push(elapsed_us);
        }
        lat_read_done.store(true, std::sync::atomic::Ordering::Relaxed);
    });

    reader_handle.join().unwrap();
    for h in writer_handles {
        let _ = h.join();
    }

    let mut lats = read_latencies.lock().unwrap().clone();
    let reader_p95_us = if lats.is_empty() {
        0u128
    } else {
        lats.sort_unstable();
        lats[lats.len() * 95 / 100]
    };

    // Print bench JSON.
    // gate_pass is informational only — the ratio is not asserted here because
    // on fast SSD/tmpdir the channel round-trip of submit_batch dominates over
    // fsync cost.  See group_commit_simfs_amortization_bench for the gated proof.
    println!(
        "{}",
        serde_json::json!({
            "serialized_writer_ops_per_s": serial_ops_per_s as u64,
            "eight_writer_ops_per_s": conc_ops_per_s as u64,
            "ratio": format!("{ratio:.2}"),
            "reader_under_burst_p95_us": reader_p95_us,
            "gate_pass": ratio >= 3.0,
        })
    );
}

// ── Test 11: SimFs amortization bench (gated, ignored) ───────────────────────

/// Environment-independent amortization proof (spec B3.5 gate).
///
/// Uses `SimFs::with_sync_delay_us` to inject a controlled fsync latency
/// so the result is independent of the storage hardware.  When fsync costs
/// FSYNC_DELAY_US, committing 8 ops under one group fsync is ~8× cheaper than
/// 8 serial fsyncs.  This test asserts the ratio is >= 3× (half the theoretical
/// maximum), leaving headroom for group sizes < 8.
///
/// # Method
///
/// **Serial baseline** — TOTAL_OPS sequential `insert_node` calls on a
/// `GraphDb<SimFs>` under `FsyncPolicy::Strict`.  Each call triggers one
/// `SimFs::sync` (one FSYNC_DELAY_US sleep).  Total time ≈ TOTAL_OPS × delay.
///
/// **Group path** — same TOTAL_OPS split into groups of WRITERS ops each,
/// committed via `commit_group` (which calls `SimFs::sync` once per group).
/// Total time ≈ (TOTAL_OPS / WRITERS) × delay.
///
/// Theoretical ratio = WRITERS = 8×.  Practical ratio will be close to 8×
/// because both paths use the same SimFs implementation.
///
/// Run manually with: `cargo test --release group_commit_simfs_amortization_bench -- --ignored --nocapture`
#[test]
#[ignore]
fn group_commit_simfs_amortization_bench() {
    use sim_harness::SimFs;
    use std::time::Instant;

    // Simulated fsync cost (spinning-disk / NVMe-with-flush approximation).
    const FSYNC_DELAY_US: u64 = 5_000; // 5 ms
    const WRITERS: usize = 8;
    const OPS_PER_WRITER: usize = 50; // small: sleep dominates, not CPU
    const TOTAL_OPS: usize = WRITERS * OPS_PER_WRITER;

    // ── Serial baseline ──────────────────────────────────────────────────────
    //
    // TOTAL_OPS sequential insert_node calls under FsyncPolicy::Strict.
    // Each call: append WAL record → SimFs::sync (sleeps FSYNC_DELAY_US µs).
    let fs_serial = SimFs::with_sync_delay_us(FSYNC_DELAY_US);
    let mut db_serial = GraphDb::open_with(fs_serial).unwrap();
    // db opens with FsyncPolicy::Strict by default; no change needed.

    let t0 = Instant::now();
    for i in 0..TOTAL_OPS {
        db_serial
            .insert_node("W", &format!("s{i}"), vec![])
            .unwrap();
    }
    let serial_elapsed = t0.elapsed();
    let serial_ops_per_s = TOTAL_OPS as f64 / serial_elapsed.as_secs_f64();

    // ── Group path ───────────────────────────────────────────────────────────
    //
    // TOTAL_OPS ops committed in groups of WRITERS via commit_group.
    // commit_group calls SimFs::sync ONCE per group (one FSYNC_DELAY_US sleep).
    let fs_group = SimFs::with_sync_delay_us(FSYNC_DELAY_US);
    let mut db_group = GraphDb::open_with(fs_group).unwrap();

    let t1 = Instant::now();
    for g in 0..(TOTAL_OPS / WRITERS) {
        let batches: Vec<Vec<BatchOp>> = (0..WRITERS)
            .map(|i| {
                vec![BatchOp::InsertNode {
                    label: "W".into(),
                    key: format!("g{g}n{i}"),
                    props: vec![],
                }]
            })
            .collect();
        let (results, sync_err) = db_group.commit_group(batches);
        assert!(sync_err.is_none(), "simfs sync must not fail");
        for r in results {
            r.unwrap();
        }
    }
    let group_elapsed = t1.elapsed();
    let group_ops_per_s = TOTAL_OPS as f64 / group_elapsed.as_secs_f64();

    let ratio = group_ops_per_s / serial_ops_per_s;

    println!(
        "{}",
        serde_json::json!({
            "bench": "simfs_amortization",
            "fsync_delay_us": FSYNC_DELAY_US,
            "writers": WRITERS,
            "total_ops": TOTAL_OPS,
            "serial_ops_per_s": serial_ops_per_s as u64,
            "group_ops_per_s": group_ops_per_s as u64,
            "ratio": format!("{ratio:.2}"),
            "gate_pass": ratio >= 3.0,
        })
    );

    assert!(
        ratio >= 3.0,
        "group-commit ({group_ops_per_s:.0} ops/s) must be >= 3x serial \
         ({serial_ops_per_s:.0} ops/s) under {FSYNC_DELAY_US}µs injected fsync \
         latency; ratio = {ratio:.2}"
    );
}

// ── Test 12: F1(c) — SharedDb fsync-failure integration (single submitter) ───

/// Full fsync-failure contract exercised through the live drain thread
/// (not GraphDb methods directly).
///
/// Verifies:
/// - Group submitter receives Err on fsync failure.
/// - WAL is truncated to the pre-group offset after failure.
/// - Subsequent `submit_batch` returns Err(degraded).
/// - Reopen/replay: failed group absent, pre-group data intact.
#[test]
fn shared_db_fsync_failure_degrades_and_truncates_wal() {
    use std::sync::atomic::{AtomicBool, Ordering};

    let dir = tmp("f1c-single");
    let fail = Arc::new(AtomicBool::new(false));
    let fail2 = Arc::clone(&fail);

    let db = SharedDb::open_with_test_sync(&dir, move |path| {
        if fail2.load(Ordering::Acquire) {
            Err(std::io::Error::other("injected fsync failure"))
        } else {
            core_storage::sync_wal_at(path)
        }
    })
    .unwrap();

    // Normal submission — must succeed.
    db.submit_batch(vec![BatchOp::InsertNode {
        label: "A".into(),
        key: "pre".into(),
        props: vec![],
    }])
    .unwrap();

    let pre_group_wal_len = std::fs::metadata(dir.join("wal.bin")).unwrap().len();

    // Enable fsync failure for the next group.
    fail.store(true, Ordering::Release);

    let result = db.submit_batch(vec![BatchOp::InsertNode {
        label: "A".into(),
        key: "fail-group".into(),
        props: vec![],
    }]);
    assert!(result.is_err(), "group with failing fsync must return Err");

    // WAL must be truncated back to pre-group length.
    let post_wal_len = std::fs::metadata(dir.join("wal.bin")).unwrap().len();
    assert_eq!(
        post_wal_len, pre_group_wal_len,
        "WAL must be truncated to pre-group length after fsync failure"
    );

    // Subsequent submit must fail with degraded error.
    let result2 = db.submit_batch(vec![BatchOp::InsertNode {
        label: "A".into(),
        key: "post-fail".into(),
        props: vec![],
    }]);
    assert!(
        result2.is_err(),
        "subsequent submit_batch must return Err after degradation"
    );

    // Reopen and replay — failed group must be absent, pre-group data intact.
    drop(db);
    let db2 = SharedDb::open(&dir).unwrap();
    assert!(
        db2.read().has_node("pre"),
        "pre-group node must survive replay"
    );
    assert!(
        !db2.read().has_node("fail-group"),
        "failed-group node must be absent on replay"
    );
}

// ── Test 13: F1(c) variant — direct write before group survives failure ───────

/// Regression test for the truncation race (F1(d)):
/// a direct write acknowledged Ok before the group commit is NOT wiped
/// by the drain's truncation on group fsync failure.
///
/// With the WAL mutex, the direct write and the group commit are fully
/// serialized: the direct write's frames land at WAL positions below
/// `pre_group_wal_len`, so `truncate_wal_at(pre_len)` leaves them intact.
///
/// Verifies on reopen: the directly-acknowledged write survives replay
/// and the failed group node is absent.
#[test]
fn direct_write_before_group_survives_group_fsync_failure() {
    use std::sync::atomic::{AtomicBool, Ordering};

    let dir = tmp("f1c-concurrent");
    let fail = Arc::new(AtomicBool::new(false));
    let fail2 = Arc::clone(&fail);

    let db = SharedDb::open_with_test_sync(&dir, move |path| {
        if fail2.load(Ordering::Acquire) {
            Err(std::io::Error::other("injected fsync failure"))
        } else {
            core_storage::sync_wal_at(path)
        }
    })
    .unwrap();

    // Direct write: durably acknowledged before any group failure.
    // The WAL mutex ensures this write's append + fsync is atomic with
    // respect to any subsequent drain group.
    db.write().insert_node("A", "direct-ok", vec![]).unwrap();

    // Record WAL offset AFTER the direct write: the group's frames will
    // land here, and truncation reverts to exactly this position.
    let pre_group_wal_len = std::fs::metadata(dir.join("wal.bin")).unwrap().len();

    // Enable fsync failure for the next drain group.
    fail.store(true, Ordering::Release);

    let result = db.submit_batch(vec![BatchOp::InsertNode {
        label: "A".into(),
        key: "group-fail".into(),
        props: vec![],
    }]);
    assert!(result.is_err(), "group fsync failure must return Err");

    // WAL must be truncated to pre-group boundary.
    // The direct write's frames (before pre_group_wal_len) are untouched.
    let post_wal_len = std::fs::metadata(dir.join("wal.bin")).unwrap().len();
    assert_eq!(
        post_wal_len, pre_group_wal_len,
        "WAL truncated to pre-group boundary; direct write frames are preserved"
    );

    // Reopen: direct write survives; failed group node is absent.
    drop(db);
    let db2 = SharedDb::open(&dir).unwrap();
    assert!(
        db2.read().has_node("direct-ok"),
        "directly-acknowledged write must survive replay"
    );
    assert!(
        !db2.read().has_node("group-fail"),
        "failed group node must be absent on replay"
    );
}

// ── Test 14: write_batch Strict always fsyncs ─────────────────────────────────

/// Verifies the fix for the single-op write_batch durability bug:
/// under `FsyncPolicy::Strict`, every `write_batch` call must issue exactly
/// one fsync, regardless of how many ops the batch contains.
///
/// Checks two sub-cases:
/// - N=5 single-op batches → exactly 5 fsyncs.
/// - One 5-op batch → exactly 1 fsync.
#[test]
fn write_batch_strict_always_fsyncs() {
    // ── 5 single-op write_batch calls → 5 fsyncs ────────────────────────────
    let mut db = counting_db();
    // Default policy is Strict.
    assert_eq!(db.fsync_policy(), FsyncPolicy::Strict);

    for i in 0..5usize {
        db.write_batch(|b| {
            b.insert_node("X", &format!("single{i}"), vec![]);
        })
        .expect("write_batch must succeed");
    }
    assert_eq!(
        db.fs_sync_count(),
        5,
        "5 single-op write_batch calls under Strict must produce exactly 5 fsyncs"
    );
    assert_eq!(db.node_count(), 5);

    // ── One 5-op write_batch → exactly 1 fsync (cumulative: 6) ─────────────
    db.write_batch(|b| {
        for j in 0..5usize {
            b.insert_node("X", &format!("multi{j}"), vec![]);
        }
    })
    .expect("5-op write_batch must succeed");
    assert_eq!(
        db.fs_sync_count(),
        6,
        "one 5-op write_batch under Strict must produce exactly 1 additional fsync (total 6)"
    );
    assert_eq!(db.node_count(), 10);
}

// ── Concurrency torture: overlapping keys and rule/index consistency ─────────

/// Many threads race to insert the *same* key. Regardless of interleaving, the
/// store must end with exactly one such node and no corruption — duplicate
/// submissions are rejected, not double-applied.
#[test]
fn concurrent_overlapping_key_inserts_land_exactly_once() {
    let dir = tmp("conc-overlap");
    let db = SharedDb::open(&dir).unwrap();
    const WRITERS: usize = 16;

    let start = Arc::new(Barrier::new(WRITERS));
    let ok_count = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let handles: Vec<_> = (0..WRITERS)
        .map(|_| {
            let db = db.clone();
            let start = Arc::clone(&start);
            let ok_count = Arc::clone(&ok_count);
            thread::spawn(move || {
                start.wait();
                let r = db.submit_batch(vec![BatchOp::InsertNode {
                    label: "N".into(),
                    key: "shared".into(),
                    props: vec![],
                }]);
                if r.is_ok() {
                    ok_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                }
            })
        })
        .collect();
    for h in handles {
        h.join().expect("writer thread panicked");
    }

    assert_eq!(
        ok_count.load(std::sync::atomic::Ordering::Relaxed),
        1,
        "exactly one insert of the shared key may succeed"
    );
    assert!(db.read().has_node("shared"), "the node must exist");
    assert_eq!(db.read().node_count(), 1, "exactly one node total");
}

/// Concurrent writers that trigger rule-fires must leave the derived edges,
/// the fulltext-style property index, and node state mutually consistent — no
/// lost edges, no stale index entries, no panics.
#[test]
fn concurrent_writes_keep_rules_and_index_consistent() {
    use core_api::{Predicate, RuleDef};
    use std::collections::BTreeMap;

    let dir = tmp("conc-rules-index");
    let db = SharedDb::open(&dir).unwrap();

    // Rule: Talent.city == Company.city → IN_CITY. Plus an equality index on
    // Talent.city so the index-maintenance path runs under concurrency too.
    db.write()
        .create_rule(RuleDef {
            name: "same_city".into(),
            src_label: "Talent".into(),
            dst_label: "Company".into(),
            predicate: Predicate::FieldEqual {
                field: "city".into(),
            },
            edge_type: "IN_CITY".into(),
            weight_prop: None,
            max_edges: None,
            approximate: false,
            via_label: None,
            via_edge: None,
            via_dir: None,
            namespace: None,
        })
        .unwrap();
    db.write().enable_index("Talent", "city").unwrap();

    const N_TALENT: usize = 30;
    const N_COMPANY: usize = 10;

    let start = Arc::new(Barrier::new(2));
    let db_t = db.clone();
    let start_t = Arc::clone(&start);
    let t_thread = thread::spawn(move || {
        start_t.wait();
        for i in 0..N_TALENT {
            db_t.submit_batch(vec![BatchOp::InsertNode {
                label: "Talent".into(),
                key: format!("t{i}"),
                props: vec![("city".into(), core_api::Value::Str("austin".into()))],
            }])
            .unwrap();
        }
    });
    let db_c = db.clone();
    let start_c = Arc::clone(&start);
    let c_thread = thread::spawn(move || {
        start_c.wait();
        for i in 0..N_COMPANY {
            db_c.submit_batch(vec![BatchOp::InsertNode {
                label: "Company".into(),
                key: format!("c{i}"),
                props: vec![("city".into(), core_api::Value::Str("austin".into()))],
            }])
            .unwrap();
        }
    });
    t_thread.join().unwrap();
    c_thread.join().unwrap();

    // Every Talent×Company austin pair must have an IN_CITY edge.
    let edges = db
        .read()
        .query(
            "MATCH (t:Talent)-[r:IN_CITY]->(c:Company) RETURN t",
            &BTreeMap::new(),
        )
        .unwrap();
    assert_eq!(
        edges.len(),
        N_TALENT * N_COMPANY,
        "all derived edges must be present after concurrent rule-fires"
    );

    // The property index must see every austin Talent.
    let indexed = db
        .read()
        .query(
            "MATCH (t:Talent {city: 'austin'}) RETURN t",
            &BTreeMap::new(),
        )
        .unwrap();
    assert_eq!(
        indexed.len(),
        N_TALENT,
        "the equality index must be consistent under concurrent writes"
    );
    assert_eq!(db.read().node_count(), N_TALENT + N_COMPANY);
}