interstellar 0.2.0

A high-performance graph database with Gremlin-style traversals and GQL query language
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
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
//! Crash recovery logic for replaying write-ahead log.
//!
//! Recovers database to consistent state by replaying committed transactions from WAL.
//!
//! # Recovery Process
//!
//! When a database is opened after a crash or unclean shutdown, the recovery module:
//!
//! 1. Scans the WAL for all entries
//! 2. Identifies committed transactions (have both BeginTx and CommitTx entries)
//! 3. Replays committed transactions in order to the main data file
//! 4. Discards aborted and incomplete transactions
//! 5. Truncates the WAL after successful recovery
//!
//! # Transaction Model
//!
//! ```text
//! BeginTx { tx_id: 1 }
//! InsertNode { ... }
//! InsertEdge { ... }
//! CommitTx { tx_id: 1 }   <-- Transaction is committed
//!
//! BeginTx { tx_id: 2 }
//! InsertNode { ... }
//! AbortTx { tx_id: 2 }    <-- Transaction is aborted (discarded)
//!
//! BeginTx { tx_id: 3 }
//! InsertNode { ... }
//! <crash>                 <-- Transaction is incomplete (discarded)
//! ```
//!
//! # Idempotency
//!
//! Recovery is designed to be idempotent - running it multiple times on the same
//! WAL produces the same result. This is achieved by:
//!
//! - Always writing records at their designated positions (based on ID)
//! - Using deterministic position calculations
//! - Truncating the WAL only after successful recovery

use std::fs::File;

use crate::error::StorageError;
use crate::value::{EdgeId, VertexId};

use super::records::{EdgeRecord, NodeRecord, EDGE_RECORD_SIZE, HEADER_SIZE, NODE_RECORD_SIZE};
use super::wal::{WalEntry, WriteAheadLog};

/// Recover database from WAL.
///
/// This function reads all entries from the WAL, identifies committed transactions,
/// and replays their operations to the main data file. After successful recovery,
/// the WAL is truncated.
///
/// # Arguments
///
/// * `wal` - The write-ahead log to recover from
/// * `data_file` - The main data file to apply changes to
/// * `node_capacity` - Current node table capacity (for offset calculations)
///
/// # Recovery Algorithm
///
/// 1. Read all committed entries from the WAL using `get_committed_entries()`
/// 2. For each entry, apply the operation to the data file:
///    - `InsertNode`: Write node record at calculated offset
///    - `InsertEdge`: Write edge record at calculated offset
///    - `DeleteNode`: Mark node as deleted
///    - `DeleteEdge`: Mark edge as deleted
///    - `UpdateProperty`: (Not implemented yet - requires property arena)
/// 3. Sync the data file
/// 4. Truncate the WAL
///
/// # Errors
///
/// - [`StorageError::Io`] - I/O error reading WAL or writing to data file
/// - [`StorageError::WalCorrupted`] - WAL entry is corrupted
///
/// # Example
///
/// ```ignore
/// use interstellar::storage::mmap::recovery::recover;
/// use interstellar::storage::mmap::wal::WriteAheadLog;
/// use std::fs::File;
///
/// let mut wal = WriteAheadLog::open("my_graph.wal")?;
/// let data_file = File::options().read(true).write(true).open("my_graph.db")?;
///
/// // Check if recovery is needed
/// if wal.needs_recovery() {
///     recover(&mut wal, &data_file, 1000)?;
/// }
/// ```
pub fn recover(
    wal: &mut WriteAheadLog,
    data_file: &File,
    node_capacity: u64,
) -> Result<RecoveryStats, StorageError> {
    let mut stats = RecoveryStats::default();

    // Get all committed entries
    let entries = wal.get_committed_entries()?;

    if entries.is_empty() {
        // Nothing to recover, just truncate WAL if needed
        if wal.file_size()? > 0 {
            wal.truncate()?;
        }
        return Ok(stats);
    }

    // Replay each entry
    for entry in entries {
        match entry {
            WalEntry::InsertNode { id, record } => {
                write_node_to_file(data_file, id, &record.into())?;
                stats.nodes_recovered += 1;
            }
            WalEntry::InsertEdge { id, record } => {
                write_edge_to_file(data_file, id, &record.into(), node_capacity)?;
                stats.edges_recovered += 1;
            }
            WalEntry::DeleteNode { id } => {
                mark_node_deleted(data_file, id)?;
                stats.nodes_deleted += 1;
            }
            WalEntry::DeleteEdge { id } => {
                mark_edge_deleted(data_file, id, node_capacity)?;
                stats.edges_deleted += 1;
            }
            WalEntry::UpdateProperty { .. } => {
                // Property updates require property arena support
                // For now, skip these during recovery
                // TODO: Implement property update recovery in Phase 4
                stats.properties_updated += 1;
            }
            WalEntry::SchemaUpdate { offset, data } => {
                write_schema_to_file(data_file, offset, &data)?;
                stats.schema_updates += 1;
            }
            // Transaction markers are not replayed
            WalEntry::BeginTx { .. }
            | WalEntry::CommitTx { .. }
            | WalEntry::AbortTx { .. }
            | WalEntry::Checkpoint { .. } => {}
            // Index operations are handled via the index specs JSON file
            // during MmapGraph::open(), so we skip them during recovery
            WalEntry::CreateIndex { .. } | WalEntry::DropIndex { .. } => {}
        }
    }

    // Sync the data file to ensure all changes are persisted
    data_file.sync_data()?;

    // Truncate the WAL now that recovery is complete
    wal.truncate()?;

    Ok(stats)
}

/// Statistics from a recovery operation.
///
/// Provides information about what was recovered from the WAL.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct RecoveryStats {
    /// Number of node records recovered
    pub nodes_recovered: u64,
    /// Number of edge records recovered
    pub edges_recovered: u64,
    /// Number of nodes marked as deleted
    pub nodes_deleted: u64,
    /// Number of edges marked as deleted
    pub edges_deleted: u64,
    /// Number of property updates applied
    pub properties_updated: u64,
    /// Number of schema updates applied
    pub schema_updates: u64,
}

impl RecoveryStats {
    /// Check if any operations were performed
    pub fn is_empty(&self) -> bool {
        self.nodes_recovered == 0
            && self.edges_recovered == 0
            && self.nodes_deleted == 0
            && self.edges_deleted == 0
            && self.properties_updated == 0
            && self.schema_updates == 0
    }

    /// Total number of operations performed
    pub fn total_operations(&self) -> u64 {
        self.nodes_recovered
            + self.edges_recovered
            + self.nodes_deleted
            + self.edges_deleted
            + self.properties_updated
            + self.schema_updates
    }
}

// =============================================================================
// Helper Functions for Writing Records
// =============================================================================

/// Calculate the byte offset for a node record.
///
/// Node records are stored immediately after the header, in a contiguous array.
///
/// # Formula
///
/// `offset = HEADER_SIZE + (vertex_id * NODE_RECORD_SIZE)`
#[inline]
fn node_offset(id: VertexId) -> u64 {
    HEADER_SIZE as u64 + (id.0 * NODE_RECORD_SIZE as u64)
}

/// Calculate the byte offset for an edge record.
///
/// Edge records are stored after all node records, in a contiguous array.
///
/// # Formula
///
/// `offset = HEADER_SIZE + (node_capacity * NODE_RECORD_SIZE) + (edge_id * EDGE_RECORD_SIZE)`
#[inline]
fn edge_offset(id: EdgeId, node_capacity: u64) -> u64 {
    HEADER_SIZE as u64
        + (node_capacity * NODE_RECORD_SIZE as u64)
        + (id.0 * EDGE_RECORD_SIZE as u64)
}

/// Write a node record to the data file at the correct offset.
///
/// # Platform Notes
///
/// On Unix, uses `write_all_at` for positioned writes without seeking.
/// On other platforms, uses seek + write_all.
fn write_node_to_file(file: &File, id: VertexId, record: &NodeRecord) -> Result<(), StorageError> {
    let offset = node_offset(id);
    let bytes = record.to_bytes();

    #[cfg(unix)]
    {
        use std::os::unix::fs::FileExt;
        file.write_all_at(&bytes, offset)?;
    }

    #[cfg(not(unix))]
    {
        use std::io::{Seek, SeekFrom, Write};
        let mut file = file;
        file.seek(SeekFrom::Start(offset))?;
        file.write_all(&bytes)?;
    }

    Ok(())
}

/// Write an edge record to the data file at the correct offset.
///
/// # Platform Notes
///
/// On Unix, uses `write_all_at` for positioned writes without seeking.
/// On other platforms, uses seek + write_all.
fn write_edge_to_file(
    file: &File,
    id: EdgeId,
    record: &EdgeRecord,
    node_capacity: u64,
) -> Result<(), StorageError> {
    let offset = edge_offset(id, node_capacity);
    let bytes = record.to_bytes();

    #[cfg(unix)]
    {
        use std::os::unix::fs::FileExt;
        file.write_all_at(&bytes, offset)?;
    }

    #[cfg(not(unix))]
    {
        use std::io::{Seek, SeekFrom, Write};
        let mut file = file;
        file.seek(SeekFrom::Start(offset))?;
        file.write_all(&bytes)?;
    }

    Ok(())
}

/// Mark a node as deleted in the data file.
///
/// Reads the current record, sets the deleted flag, and writes it back.
fn mark_node_deleted(file: &File, id: VertexId) -> Result<(), StorageError> {
    let offset = node_offset(id);

    // Read current record
    let mut bytes = [0u8; NODE_RECORD_SIZE];

    #[cfg(unix)]
    {
        use std::os::unix::fs::FileExt;
        file.read_exact_at(&mut bytes, offset)?;
    }

    #[cfg(not(unix))]
    {
        use std::io::{Read, Seek, SeekFrom};
        let mut file = file;
        file.seek(SeekFrom::Start(offset))?;
        file.read_exact(&mut bytes)?;
    }

    let mut record = NodeRecord::from_bytes(&bytes);
    record.mark_deleted();

    // Write back
    let updated_bytes = record.to_bytes();

    #[cfg(unix)]
    {
        use std::os::unix::fs::FileExt;
        file.write_all_at(&updated_bytes, offset)?;
    }

    #[cfg(not(unix))]
    {
        use std::io::{Seek, SeekFrom, Write};
        let mut file = file;
        file.seek(SeekFrom::Start(offset))?;
        file.write_all(&updated_bytes)?;
    }

    Ok(())
}

/// Mark an edge as deleted in the data file.
///
/// Reads the current record, sets the deleted flag, and writes it back.
fn mark_edge_deleted(file: &File, id: EdgeId, node_capacity: u64) -> Result<(), StorageError> {
    let offset = edge_offset(id, node_capacity);

    // Read current record
    let mut bytes = [0u8; EDGE_RECORD_SIZE];

    #[cfg(unix)]
    {
        use std::os::unix::fs::FileExt;
        file.read_exact_at(&mut bytes, offset)?;
    }

    #[cfg(not(unix))]
    {
        use std::io::{Read, Seek, SeekFrom};
        let mut file = file;
        file.seek(SeekFrom::Start(offset))?;
        file.read_exact(&mut bytes)?;
    }

    let mut record = EdgeRecord::from_bytes(&bytes);
    record.mark_deleted();

    // Write back
    let updated_bytes = record.to_bytes();

    #[cfg(unix)]
    {
        use std::os::unix::fs::FileExt;
        file.write_all_at(&updated_bytes, offset)?;
    }

    #[cfg(not(unix))]
    {
        use std::io::{Seek, SeekFrom, Write};
        let mut file = file;
        file.seek(SeekFrom::Start(offset))?;
        file.write_all(&updated_bytes)?;
    }

    Ok(())
}

/// Write schema data to the data file at the specified offset.
///
/// Used during recovery to replay schema updates from the WAL.
fn write_schema_to_file(file: &File, offset: u64, data: &[u8]) -> Result<(), StorageError> {
    // Ensure file is large enough
    let metadata = file.metadata()?;
    let required_size = offset + data.len() as u64;
    if required_size > metadata.len() {
        file.set_len(required_size)?;
    }

    #[cfg(unix)]
    {
        use std::os::unix::fs::FileExt;
        file.write_all_at(data, offset)?;
    }

    #[cfg(not(unix))]
    {
        use std::io::{Seek, SeekFrom, Write};
        let mut file = file;
        file.seek(SeekFrom::Start(offset))?;
        file.write_all(data)?;
    }

    Ok(())
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::mmap::records::FileHeader;
    use crate::storage::mmap::wal::{SerializableEdgeRecord, SerializableNodeRecord, WalEntry};
    use std::fs::OpenOptions;
    use tempfile::TempDir;

    /// Helper: Create a test database file with header and space for records
    fn create_test_db(dir: &TempDir, node_capacity: u64, edge_capacity: u64) -> File {
        let db_path = dir.path().join("test.db");

        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(true)
            .open(&db_path)
            .expect("create db file");

        // Calculate file size
        let node_table_size = node_capacity * NODE_RECORD_SIZE as u64;
        let edge_table_size = edge_capacity * EDGE_RECORD_SIZE as u64;
        let file_size = HEADER_SIZE as u64 + node_table_size + edge_table_size;

        file.set_len(file_size).expect("set file length");

        // Write header
        let mut header = FileHeader::new();
        header.node_capacity = node_capacity;
        header.edge_capacity = edge_capacity;

        #[cfg(unix)]
        {
            use std::os::unix::fs::FileExt;
            file.write_all_at(&header.to_bytes(), 0)
                .expect("write header");
        }

        #[cfg(not(unix))]
        {
            use std::io::{Seek, SeekFrom, Write};
            let mut f = &file;
            f.seek(SeekFrom::Start(0)).unwrap();
            f.write_all(&header.to_bytes()).unwrap();
        }

        file.sync_all().expect("sync file");

        file
    }

    /// Helper: Read a node record from the test file
    fn read_node_record(file: &File, id: VertexId) -> NodeRecord {
        let offset = node_offset(id);
        let mut bytes = [0u8; NODE_RECORD_SIZE];

        #[cfg(unix)]
        {
            use std::os::unix::fs::FileExt;
            file.read_exact_at(&mut bytes, offset).expect("read node");
        }

        #[cfg(not(unix))]
        {
            use std::io::{Read, Seek, SeekFrom};
            let mut f = file;
            f.seek(SeekFrom::Start(offset)).unwrap();
            f.read_exact(&mut bytes).unwrap();
        }

        NodeRecord::from_bytes(&bytes)
    }

    /// Helper: Read an edge record from the test file
    fn read_edge_record(file: &File, id: EdgeId, node_capacity: u64) -> EdgeRecord {
        let offset = edge_offset(id, node_capacity);
        let mut bytes = [0u8; EDGE_RECORD_SIZE];

        #[cfg(unix)]
        {
            use std::os::unix::fs::FileExt;
            file.read_exact_at(&mut bytes, offset).expect("read edge");
        }

        #[cfg(not(unix))]
        {
            use std::io::{Read, Seek, SeekFrom};
            let mut f = file;
            f.seek(SeekFrom::Start(offset)).unwrap();
            f.read_exact(&mut bytes).unwrap();
        }

        EdgeRecord::from_bytes(&bytes)
    }

    // =========================================================================
    // Offset Calculation Tests
    // =========================================================================

    #[test]
    fn test_node_offset_calculation() {
        // Node 0 should be at HEADER_SIZE
        assert_eq!(node_offset(VertexId(0)), HEADER_SIZE as u64);

        // Node 1 should be at HEADER_SIZE + NODE_RECORD_SIZE
        assert_eq!(
            node_offset(VertexId(1)),
            HEADER_SIZE as u64 + NODE_RECORD_SIZE as u64
        );

        // Node 100 should be at HEADER_SIZE + 100 * NODE_RECORD_SIZE
        assert_eq!(
            node_offset(VertexId(100)),
            HEADER_SIZE as u64 + 100 * NODE_RECORD_SIZE as u64
        );
    }

    #[test]
    fn test_edge_offset_calculation() {
        let node_capacity = 1000u64;

        // Edge 0 should be after all node records
        let expected_base = HEADER_SIZE as u64 + node_capacity * NODE_RECORD_SIZE as u64;
        assert_eq!(edge_offset(EdgeId(0), node_capacity), expected_base);

        // Edge 1 should be at base + EDGE_RECORD_SIZE
        assert_eq!(
            edge_offset(EdgeId(1), node_capacity),
            expected_base + EDGE_RECORD_SIZE as u64
        );

        // Edge 500 should be at base + 500 * EDGE_RECORD_SIZE
        assert_eq!(
            edge_offset(EdgeId(500), node_capacity),
            expected_base + 500 * EDGE_RECORD_SIZE as u64
        );
    }

    // =========================================================================
    // RecoveryStats Tests
    // =========================================================================

    #[test]
    fn test_recovery_stats_default_is_empty() {
        let stats = RecoveryStats::default();
        assert!(stats.is_empty());
        assert_eq!(stats.total_operations(), 0);
    }

    #[test]
    fn test_recovery_stats_not_empty_with_operations() {
        let mut stats = RecoveryStats::default();
        stats.nodes_recovered = 5;
        assert!(!stats.is_empty());
        assert_eq!(stats.total_operations(), 5);
    }

    #[test]
    fn test_recovery_stats_total_operations() {
        let stats = RecoveryStats {
            nodes_recovered: 10,
            edges_recovered: 20,
            nodes_deleted: 3,
            edges_deleted: 2,
            properties_updated: 5,
            schema_updates: 0,
        };
        assert_eq!(stats.total_operations(), 40);
    }

    // =========================================================================
    // Write Helper Tests
    // =========================================================================

    #[test]
    fn test_write_node_to_file() {
        let dir = TempDir::new().unwrap();
        let file = create_test_db(&dir, 100, 100);

        let node = NodeRecord::new(42, 7);
        write_node_to_file(&file, VertexId(42), &node).expect("write node");
        file.sync_all().expect("sync");

        let read_back = read_node_record(&file, VertexId(42));

        // Copy fields from packed struct
        let id = read_back.id;
        let label_id = read_back.label_id;

        assert_eq!(id, 42);
        assert_eq!(label_id, 7);
    }

    #[test]
    fn test_write_edge_to_file() {
        let dir = TempDir::new().unwrap();
        let node_capacity = 100;
        let file = create_test_db(&dir, node_capacity, 100);

        let edge = EdgeRecord::new(10, 5, 1, 2);
        write_edge_to_file(&file, EdgeId(10), &edge, node_capacity).expect("write edge");
        file.sync_all().expect("sync");

        let read_back = read_edge_record(&file, EdgeId(10), node_capacity);

        // Copy fields from packed struct
        let id = read_back.id;
        let label_id = read_back.label_id;
        let src = read_back.src;
        let dst = read_back.dst;

        assert_eq!(id, 10);
        assert_eq!(label_id, 5);
        assert_eq!(src, 1);
        assert_eq!(dst, 2);
    }

    #[test]
    fn test_mark_node_deleted() {
        let dir = TempDir::new().unwrap();
        let file = create_test_db(&dir, 100, 100);

        // First write a node
        let node = NodeRecord::new(5, 3);
        write_node_to_file(&file, VertexId(5), &node).expect("write node");
        file.sync_all().expect("sync");

        // Verify it's not deleted
        let before = read_node_record(&file, VertexId(5));
        assert!(!before.is_deleted());

        // Mark as deleted
        mark_node_deleted(&file, VertexId(5)).expect("mark deleted");
        file.sync_all().expect("sync");

        // Verify it's deleted
        let after = read_node_record(&file, VertexId(5));
        assert!(after.is_deleted());
    }

    #[test]
    fn test_mark_edge_deleted() {
        let dir = TempDir::new().unwrap();
        let node_capacity = 100;
        let file = create_test_db(&dir, node_capacity, 100);

        // First write an edge
        let edge = EdgeRecord::new(7, 2, 0, 1);
        write_edge_to_file(&file, EdgeId(7), &edge, node_capacity).expect("write edge");
        file.sync_all().expect("sync");

        // Verify it's not deleted
        let before = read_edge_record(&file, EdgeId(7), node_capacity);
        assert!(!before.is_deleted());

        // Mark as deleted
        mark_edge_deleted(&file, EdgeId(7), node_capacity).expect("mark deleted");
        file.sync_all().expect("sync");

        // Verify it's deleted
        let after = read_edge_record(&file, EdgeId(7), node_capacity);
        assert!(after.is_deleted());
    }

    // =========================================================================
    // Recovery Integration Tests
    // =========================================================================

    #[test]
    fn test_recover_empty_wal() {
        let dir = TempDir::new().unwrap();
        let wal_path = dir.path().join("test.wal");
        let node_capacity = 100;
        let file = create_test_db(&dir, node_capacity, 100);

        let mut wal = WriteAheadLog::open(&wal_path).expect("open WAL");

        let stats = recover(&mut wal, &file, node_capacity).expect("recover");

        assert!(stats.is_empty());
        assert_eq!(stats.total_operations(), 0);
    }

    #[test]
    fn test_recover_single_committed_node() {
        let dir = TempDir::new().unwrap();
        let wal_path = dir.path().join("test.wal");
        let node_capacity = 100;
        let file = create_test_db(&dir, node_capacity, 100);

        let mut wal = WriteAheadLog::open(&wal_path).expect("open WAL");

        // Write a committed transaction with one node
        let tx_id = wal.begin_transaction().expect("begin tx");
        wal.log(WalEntry::InsertNode {
            id: VertexId(0),
            record: SerializableNodeRecord {
                id: 0,
                label_id: 42,
                flags: 0,
                first_out_edge: u64::MAX,
                first_in_edge: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log insert");
        wal.log(WalEntry::CommitTx { tx_id }).expect("commit");
        wal.sync().expect("sync");

        // Recover
        let stats = recover(&mut wal, &file, node_capacity).expect("recover");

        assert_eq!(stats.nodes_recovered, 1);
        assert_eq!(stats.edges_recovered, 0);
        assert_eq!(stats.total_operations(), 1);

        // Verify the node was written
        let node = read_node_record(&file, VertexId(0));
        let id = node.id;
        let label_id = node.label_id;

        assert_eq!(id, 0);
        assert_eq!(label_id, 42);

        // WAL should be truncated
        assert_eq!(wal.file_size().expect("file size"), 0);
    }

    #[test]
    fn test_recover_multiple_committed_transactions() {
        let dir = TempDir::new().unwrap();
        let wal_path = dir.path().join("test.wal");
        let node_capacity = 100;
        let file = create_test_db(&dir, node_capacity, 100);

        let mut wal = WriteAheadLog::open(&wal_path).expect("open WAL");

        // Transaction 1: Insert node 0
        let tx1 = wal.begin_transaction().expect("begin tx1");
        wal.log(WalEntry::InsertNode {
            id: VertexId(0),
            record: SerializableNodeRecord {
                id: 0,
                label_id: 10,
                flags: 0,
                first_out_edge: u64::MAX,
                first_in_edge: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log");
        wal.log(WalEntry::CommitTx { tx_id: tx1 }).expect("commit");

        // Transaction 2: Insert node 1 and edge 0
        let tx2 = wal.begin_transaction().expect("begin tx2");
        wal.log(WalEntry::InsertNode {
            id: VertexId(1),
            record: SerializableNodeRecord {
                id: 1,
                label_id: 20,
                flags: 0,
                first_out_edge: 0,
                first_in_edge: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log");
        wal.log(WalEntry::InsertEdge {
            id: EdgeId(0),
            record: SerializableEdgeRecord {
                id: 0,
                label_id: 5,
                flags: 0,
                src: 0,
                dst: 1,
                next_out: u64::MAX,
                next_in: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log");
        wal.log(WalEntry::CommitTx { tx_id: tx2 }).expect("commit");

        wal.sync().expect("sync");

        // Recover
        let stats = recover(&mut wal, &file, node_capacity).expect("recover");

        assert_eq!(stats.nodes_recovered, 2);
        assert_eq!(stats.edges_recovered, 1);

        // Verify nodes - copy fields from packed struct to avoid unaligned reference
        let node0 = read_node_record(&file, VertexId(0));
        let (node0_id, node0_label) = (node0.id, node0.label_id);
        assert_eq!(node0_id, 0);
        assert_eq!(node0_label, 10);

        let node1 = read_node_record(&file, VertexId(1));
        let (node1_id, node1_label) = (node1.id, node1.label_id);
        assert_eq!(node1_id, 1);
        assert_eq!(node1_label, 20);

        // Verify edge - copy fields from packed struct
        let edge0 = read_edge_record(&file, EdgeId(0), node_capacity);
        let (edge0_id, edge0_label, edge0_src, edge0_dst) =
            (edge0.id, edge0.label_id, edge0.src, edge0.dst);
        assert_eq!(edge0_id, 0);
        assert_eq!(edge0_label, 5);
        assert_eq!(edge0_src, 0);
        assert_eq!(edge0_dst, 1);
    }

    #[test]
    fn test_recover_discards_uncommitted_transactions() {
        let dir = TempDir::new().unwrap();
        let wal_path = dir.path().join("test.wal");
        let node_capacity = 100;
        let file = create_test_db(&dir, node_capacity, 100);

        let mut wal = WriteAheadLog::open(&wal_path).expect("open WAL");

        // Transaction 1: Committed
        let tx1 = wal.begin_transaction().expect("begin tx1");
        wal.log(WalEntry::InsertNode {
            id: VertexId(0),
            record: SerializableNodeRecord {
                id: 0,
                label_id: 100,
                flags: 0,
                first_out_edge: u64::MAX,
                first_in_edge: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log");
        wal.log(WalEntry::CommitTx { tx_id: tx1 }).expect("commit");

        // Transaction 2: NOT committed (simulates crash)
        let _tx2 = wal.begin_transaction().expect("begin tx2");
        wal.log(WalEntry::InsertNode {
            id: VertexId(1),
            record: SerializableNodeRecord {
                id: 1,
                label_id: 999, // This should NOT appear in the DB
                flags: 0,
                first_out_edge: u64::MAX,
                first_in_edge: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log");
        // No commit!

        wal.sync().expect("sync");

        // Recover
        let stats = recover(&mut wal, &file, node_capacity).expect("recover");

        // Only 1 node recovered (from committed tx)
        assert_eq!(stats.nodes_recovered, 1);

        // Verify node 0 was written (committed) - copy fields from packed struct
        let node0 = read_node_record(&file, VertexId(0));
        let (node0_id, node0_label) = (node0.id, node0.label_id);
        assert_eq!(node0_id, 0);
        assert_eq!(node0_label, 100);

        // Verify node 1 was NOT written (uncommitted)
        // The record will be all zeros since we didn't write to that slot
        let node1 = read_node_record(&file, VertexId(1));
        let node1_id = node1.id;
        let node1_label = node1.label_id;
        // Uncommitted node should not have label_id 999
        assert!(
            node1_id != 1 || node1_label != 999,
            "Uncommitted transaction should not be recovered"
        );
    }

    #[test]
    fn test_recover_discards_aborted_transactions() {
        let dir = TempDir::new().unwrap();
        let wal_path = dir.path().join("test.wal");
        let node_capacity = 100;
        let file = create_test_db(&dir, node_capacity, 100);

        let mut wal = WriteAheadLog::open(&wal_path).expect("open WAL");

        // Transaction 1: Committed
        let tx1 = wal.begin_transaction().expect("begin tx1");
        wal.log(WalEntry::InsertNode {
            id: VertexId(0),
            record: SerializableNodeRecord {
                id: 0,
                label_id: 50,
                flags: 0,
                first_out_edge: u64::MAX,
                first_in_edge: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log");
        wal.log(WalEntry::CommitTx { tx_id: tx1 }).expect("commit");

        // Transaction 2: Aborted
        let tx2 = wal.begin_transaction().expect("begin tx2");
        wal.log(WalEntry::InsertNode {
            id: VertexId(1),
            record: SerializableNodeRecord {
                id: 1,
                label_id: 888, // This should NOT appear
                flags: 0,
                first_out_edge: u64::MAX,
                first_in_edge: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log");
        wal.log(WalEntry::AbortTx { tx_id: tx2 }).expect("abort");

        // Transaction 3: Committed
        let tx3 = wal.begin_transaction().expect("begin tx3");
        wal.log(WalEntry::InsertNode {
            id: VertexId(2),
            record: SerializableNodeRecord {
                id: 2,
                label_id: 60,
                flags: 0,
                first_out_edge: u64::MAX,
                first_in_edge: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log");
        wal.log(WalEntry::CommitTx { tx_id: tx3 }).expect("commit");

        wal.sync().expect("sync");

        // Recover
        let stats = recover(&mut wal, &file, node_capacity).expect("recover");

        // Only 2 nodes recovered (tx1 and tx3)
        assert_eq!(stats.nodes_recovered, 2);

        // Verify node 0 and 2 were written - copy fields from packed struct
        let node0 = read_node_record(&file, VertexId(0));
        let node0_label = node0.label_id;
        assert_eq!(node0_label, 50);

        let node2 = read_node_record(&file, VertexId(2));
        let node2_label = node2.label_id;
        assert_eq!(node2_label, 60);

        // Verify node 1 was NOT written (aborted)
        let node1 = read_node_record(&file, VertexId(1));
        assert!(
            node1.label_id != 888,
            "Aborted transaction should not be recovered"
        );
    }

    #[test]
    fn test_recover_handles_delete_operations() {
        let dir = TempDir::new().unwrap();
        let wal_path = dir.path().join("test.wal");
        let node_capacity = 100;
        let file = create_test_db(&dir, node_capacity, 100);

        let mut wal = WriteAheadLog::open(&wal_path).expect("open WAL");

        // Transaction 1: Insert and delete
        let tx1 = wal.begin_transaction().expect("begin tx1");
        wal.log(WalEntry::InsertNode {
            id: VertexId(0),
            record: SerializableNodeRecord {
                id: 0,
                label_id: 10,
                flags: 0,
                first_out_edge: u64::MAX,
                first_in_edge: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log");
        wal.log(WalEntry::InsertNode {
            id: VertexId(1),
            record: SerializableNodeRecord {
                id: 1,
                label_id: 20,
                flags: 0,
                first_out_edge: u64::MAX,
                first_in_edge: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log");
        wal.log(WalEntry::DeleteNode { id: VertexId(0) })
            .expect("log delete");
        wal.log(WalEntry::CommitTx { tx_id: tx1 }).expect("commit");

        wal.sync().expect("sync");

        // Recover
        let stats = recover(&mut wal, &file, node_capacity).expect("recover");

        assert_eq!(stats.nodes_recovered, 2);
        assert_eq!(stats.nodes_deleted, 1);

        // Node 0 should be deleted
        let node0 = read_node_record(&file, VertexId(0));
        assert!(node0.is_deleted(), "Node 0 should be marked as deleted");

        // Node 1 should exist
        let node1 = read_node_record(&file, VertexId(1));
        assert!(!node1.is_deleted(), "Node 1 should not be deleted");
    }

    #[test]
    fn test_recover_is_idempotent() {
        let dir = TempDir::new().unwrap();
        let wal_path = dir.path().join("test.wal");
        let node_capacity = 100;
        let file = create_test_db(&dir, node_capacity, 100);

        // First, write some data and recover
        {
            let mut wal = WriteAheadLog::open(&wal_path).expect("open WAL");

            let tx_id = wal.begin_transaction().expect("begin tx");
            wal.log(WalEntry::InsertNode {
                id: VertexId(0),
                record: SerializableNodeRecord {
                    id: 0,
                    label_id: 123,
                    flags: 0,
                    first_out_edge: u64::MAX,
                    first_in_edge: u64::MAX,
                    prop_head: u64::MAX,
                },
            })
            .expect("log");
            wal.log(WalEntry::CommitTx { tx_id }).expect("commit");
            wal.sync().expect("sync");

            let stats = recover(&mut wal, &file, node_capacity).expect("recover");
            assert_eq!(stats.nodes_recovered, 1);
        }

        // WAL should be empty now
        {
            let mut wal = WriteAheadLog::open(&wal_path).expect("reopen WAL");
            assert_eq!(wal.file_size().expect("file size"), 0);

            // Recovery on empty WAL should succeed with no operations
            let stats = recover(&mut wal, &file, node_capacity).expect("recover again");
            assert!(stats.is_empty());
        }

        // Data should still be there - copy field from packed struct
        let node = read_node_record(&file, VertexId(0));
        let node_label = node.label_id;
        assert_eq!(node_label, 123);
    }

    #[test]
    fn test_recover_truncates_wal_on_success() {
        let dir = TempDir::new().unwrap();
        let wal_path = dir.path().join("test.wal");
        let node_capacity = 100;
        let file = create_test_db(&dir, node_capacity, 100);

        let mut wal = WriteAheadLog::open(&wal_path).expect("open WAL");

        // Write a transaction
        let tx_id = wal.begin_transaction().expect("begin tx");
        wal.log(WalEntry::InsertNode {
            id: VertexId(0),
            record: SerializableNodeRecord {
                id: 0,
                label_id: 1,
                flags: 0,
                first_out_edge: u64::MAX,
                first_in_edge: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log");
        wal.log(WalEntry::CommitTx { tx_id }).expect("commit");
        wal.sync().expect("sync");

        // WAL should have content
        assert!(wal.file_size().expect("file size") > 0);

        // Recover
        recover(&mut wal, &file, node_capacity).expect("recover");

        // WAL should be truncated
        assert_eq!(wal.file_size().expect("file size"), 0);
    }

    #[test]
    fn test_recover_preserves_order() {
        let dir = TempDir::new().unwrap();
        let wal_path = dir.path().join("test.wal");
        let node_capacity = 100;
        let file = create_test_db(&dir, node_capacity, 100);

        let mut wal = WriteAheadLog::open(&wal_path).expect("open WAL");

        // Transaction 1: Insert node 0 with label 1
        let tx1 = wal.begin_transaction().expect("begin tx1");
        wal.log(WalEntry::InsertNode {
            id: VertexId(0),
            record: SerializableNodeRecord {
                id: 0,
                label_id: 1,
                flags: 0,
                first_out_edge: u64::MAX,
                first_in_edge: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log");
        wal.log(WalEntry::CommitTx { tx_id: tx1 }).expect("commit");

        // Transaction 2: Update node 0 with label 2 (by re-inserting)
        let tx2 = wal.begin_transaction().expect("begin tx2");
        wal.log(WalEntry::InsertNode {
            id: VertexId(0),
            record: SerializableNodeRecord {
                id: 0,
                label_id: 2, // Updated label
                flags: 0,
                first_out_edge: u64::MAX,
                first_in_edge: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log");
        wal.log(WalEntry::CommitTx { tx_id: tx2 }).expect("commit");

        wal.sync().expect("sync");

        // Recover
        recover(&mut wal, &file, node_capacity).expect("recover");

        // Node should have the final label (2) since tx2 came after tx1
        // Copy field from packed struct to avoid unaligned reference
        let node = read_node_record(&file, VertexId(0));
        let node_label = node.label_id;
        assert_eq!(
            node_label, 2,
            "Later transaction should overwrite earlier one"
        );
    }

    #[test]
    fn test_recover_mixed_nodes_and_edges() {
        let dir = TempDir::new().unwrap();
        let wal_path = dir.path().join("test.wal");
        let node_capacity = 100;
        let file = create_test_db(&dir, node_capacity, 100);

        let mut wal = WriteAheadLog::open(&wal_path).expect("open WAL");

        // Complex transaction with multiple nodes and edges
        let tx_id = wal.begin_transaction().expect("begin tx");

        // Insert 3 nodes
        for i in 0..3 {
            wal.log(WalEntry::InsertNode {
                id: VertexId(i),
                record: SerializableNodeRecord {
                    id: i,
                    label_id: (i * 10) as u32,
                    flags: 0,
                    first_out_edge: u64::MAX,
                    first_in_edge: u64::MAX,
                    prop_head: u64::MAX,
                },
            })
            .expect("log node");
        }

        // Insert 2 edges
        wal.log(WalEntry::InsertEdge {
            id: EdgeId(0),
            record: SerializableEdgeRecord {
                id: 0,
                label_id: 100,
                flags: 0,
                src: 0,
                dst: 1,
                next_out: u64::MAX,
                next_in: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log edge");

        wal.log(WalEntry::InsertEdge {
            id: EdgeId(1),
            record: SerializableEdgeRecord {
                id: 1,
                label_id: 101,
                flags: 0,
                src: 1,
                dst: 2,
                next_out: u64::MAX,
                next_in: u64::MAX,
                prop_head: u64::MAX,
            },
        })
        .expect("log edge");

        wal.log(WalEntry::CommitTx { tx_id }).expect("commit");
        wal.sync().expect("sync");

        // Recover
        let stats = recover(&mut wal, &file, node_capacity).expect("recover");

        assert_eq!(stats.nodes_recovered, 3);
        assert_eq!(stats.edges_recovered, 2);

        // Verify all nodes - copy fields from packed struct
        for i in 0..3 {
            let node = read_node_record(&file, VertexId(i));
            let (node_id, node_label) = (node.id, node.label_id);
            assert_eq!(node_id, i);
            assert_eq!(node_label, (i * 10) as u32);
        }

        // Verify all edges - copy fields from packed struct
        let edge0 = read_edge_record(&file, EdgeId(0), node_capacity);
        let (edge0_id, edge0_src, edge0_dst) = (edge0.id, edge0.src, edge0.dst);
        assert_eq!(edge0_id, 0);
        assert_eq!(edge0_src, 0);
        assert_eq!(edge0_dst, 1);

        let edge1 = read_edge_record(&file, EdgeId(1), node_capacity);
        let (edge1_id, edge1_src, edge1_dst) = (edge1.id, edge1.src, edge1.dst);
        assert_eq!(edge1_id, 1);
        assert_eq!(edge1_src, 1);
        assert_eq!(edge1_dst, 2);
    }
}