sqlitegraph 2.0.7

Embedded graph database with full ACID transactions, HNSW vector search, dual backend support, and comprehensive graph algorithms library
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
//! V2 WAL Recovery Core Engine
//!
//! Core recovery engine and lifecycle management for V2 clustered edge format.
//! Focuses on recovery orchestration, state management, and phase coordination.

use super::validator::{RecoveryValidator, ValidationResult};
use super::{constants::*, errors::RecoveryError};
use crate::backend::native::v2::wal::{V2WALConfig, V2WALRecord};
use crate::backend::native::{GraphFile, NativeBackendError, NativeResult};
use crate::debug::{debug_log, error_log, info_log, warn_log};
use parking_lot::{Condvar, Mutex};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

/// Recovery state machine
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecoveryState {
    Idle,
    Initializing,
    Scanning,
    Validating,
    Replaying,
    Finalizing,
    Complete,
    Failed,
}

/// Recovery configuration options
#[derive(Debug, Clone)]
pub struct RecoveryOptions {
    pub fast_recovery: bool,
    pub max_batch_size: usize,
    pub recovery_timeout: Duration,
    pub perform_consistency_checks: bool,
    pub create_backup: bool,
    pub max_recovery_attempts: u32,
    pub force_recovery: bool,
    /// Maximum number of parallel WAL recovery transactions (default: 4)
    pub max_parallel_transactions: usize,
}

impl Default for RecoveryOptions {
    fn default() -> Self {
        Self {
            fast_recovery: false,
            max_batch_size: DEFAULT_BATCH_SIZE,
            recovery_timeout: Duration::from_secs(DEFAULT_RECOVERY_TIMEOUT_SECONDS),
            perform_consistency_checks: true,
            create_backup: true,
            max_recovery_attempts: MAX_RECOVERY_ATTEMPTS,
            force_recovery: false,
            max_parallel_transactions: 4, // Default parallelism degree
        }
    }
}

/// Recovery progress information
#[derive(Debug, Clone)]
pub struct RecoveryProgress {
    pub state: RecoveryState,
    pub current_lsn: u64,
    pub total_lsns: u64,
    pub transactions_processed: u64,
    pub total_transactions: u64,
    pub completion_percentage: f64,
    pub start_time: Instant,
    pub estimated_time_remaining: Duration,
}

/// Recovery result type
pub type RecoveryResult<T = RecoverySuccess> = Result<T, RecoveryError>;

/// Successful recovery result
#[derive(Debug, Clone)]
pub struct RecoverySuccess {
    pub metrics: RecoveryMetrics,
    pub warnings: Vec<String>,
    pub duration: Duration,
}

/// Recovery metrics
#[derive(Debug, Clone, Default)]
pub struct RecoveryMetrics {
    pub total_duration_ms: u64,
    pub transactions_scanned: u64,
    pub committed_transactions_replayed: u64,
    pub rolled_back_transactions: u64,
    pub records_processed: u64,
    pub corrupted_records: u64,
    pub checkpoint_inconsistencies: u64,
    pub database_size_before_recovery: u64,
    pub database_size_after_recovery: u64,
    pub recovery_start_timestamp: u64,
    pub recovery_end_timestamp: u64,
}

/// V2 WAL recovery engine
pub struct V2WALRecoveryEngine {
    config: V2WALConfig,
    database_path: PathBuf,
    state: Arc<Mutex<RecoveryState>>,
    options: RecoveryOptions,
    metrics: Arc<Mutex<RecoveryMetrics>>,
    active_transactions: Arc<Mutex<HashMap<u64, TransactionState>>>,
    recovery_cv: Arc<Condvar>,
    backup_path: Option<PathBuf>,
}

/// Transaction state during recovery
#[derive(Debug, Clone)]
pub struct TransactionState {
    pub tx_id: u64,
    pub start_lsn: u64,
    pub commit_lsn: Option<u64>,
    pub records: Vec<V2WALRecord>,
    pub committed: bool,
    pub timestamp: u64,
}

impl V2WALRecoveryEngine {
    /// Create new recovery engine
    pub fn create(
        config: V2WALConfig,
        database_path: PathBuf,
        options: RecoveryOptions,
    ) -> NativeResult<Self> {
        Self::validate_configuration(&config, &database_path)?;

        let backup_path = if options.create_backup {
            Some(Self::create_database_backup(&database_path)?)
        } else {
            None
        };

        Ok(Self {
            config,
            database_path,
            state: Arc::new(Mutex::new(RecoveryState::Idle)),
            options,
            metrics: Arc::new(Mutex::new(RecoveryMetrics::default())),
            active_transactions: Arc::new(Mutex::new(HashMap::new())),
            recovery_cv: Arc::new(Condvar::new()),
            backup_path,
        })
    }

    /// Validate configuration and prerequisites
    fn validate_configuration(config: &V2WALConfig, database_path: &Path) -> NativeResult<()> {
        config.validate()?;

        if !database_path.exists() || !database_path.is_file() {
            return Err(NativeBackendError::InvalidParameter {
                context: "Database file does not exist or is not a file".to_string(),
                source: None,
            });
        }

        if !config.wal_path.exists() || !config.wal_path.is_file() {
            return Err(NativeBackendError::InvalidParameter {
                context: "WAL file does not exist or is not a file".to_string(),
                source: None,
            });
        }

        Ok(())
    }

    /// Create database backup
    fn create_database_backup(database_path: &Path) -> NativeResult<PathBuf> {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(NativeBackendError::from)?
            .as_secs();

        let database_name = database_path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("database");

        let backup_filename = format!("{}.recovery_backup.{}", database_name, timestamp);
        let backup_path = database_path
            .parent()
            .unwrap_or_else(|| Path::new("."))
            .join("recovery_backups")
            .join(backup_filename);

        if let Some(parent) = backup_path.parent() {
            std::fs::create_dir_all(parent).map_err(NativeBackendError::from)?;
        }

        std::fs::copy(database_path, &backup_path).map_err(NativeBackendError::from)?;

        Ok(backup_path)
    }

    /// Perform crash recovery
    pub fn recover(&self) -> RecoveryResult {
        let start_time = Instant::now();
        let start_timestamp = self.get_current_timestamp()?;

        info_log!("Starting V2 WAL recovery");
        self.set_recovery_state(RecoveryState::Initializing)?;

        let mut warnings = Vec::new();

        for attempt in 1..=self.options.max_recovery_attempts {
            debug_log!(
                "Recovery attempt {}/{}",
                attempt,
                self.options.max_recovery_attempts
            );

            match self.attempt_recovery(attempt) {
                Ok(mut attempt_warnings) => {
                    warnings.append(&mut attempt_warnings);
                    return self.finalize_successful_recovery(
                        start_time,
                        start_timestamp,
                        warnings,
                    );
                }
                Err(e) => {
                    error_log!("Recovery attempt {} failed: {}", attempt, e);

                    if attempt == self.options.max_recovery_attempts {
                        if self.options.force_recovery {
                            warn_log!("Force recovery enabled");
                            return self.finalize_successful_recovery(
                                start_time,
                                start_timestamp,
                                warnings,
                            );
                        }
                        return self.finalize_failed_recovery(start_time, start_timestamp, e);
                    }

                    let backoff = Duration::from_millis(
                        (RECOVERY_RETRY_BACKOFF_MULTIPLIER.powi(attempt as i32) * 1000.0) as u64,
                    );
                    std::thread::sleep(backoff.min(Duration::from_secs(MAX_RETRY_DELAY_SECONDS)));
                }
            }
        }

        Err(RecoveryError::configuration(
            "Unexpected recovery completion".to_string(),
        ))
    }

    /// Get current timestamp
    fn get_current_timestamp(&self) -> Result<u64, RecoveryError> {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .map_err(|e| RecoveryError::configuration(format!("Failed to get timestamp: {}", e)))
    }

    /// Attempt single recovery operation
    fn attempt_recovery(&self, _attempt: u32) -> Result<Vec<String>, RecoveryError> {
        let mut warnings = Vec::new();

        self.set_recovery_state(RecoveryState::Initializing)?;
        self.set_recovery_state(RecoveryState::Scanning)?;
        let (transactions, scan_warnings) = self.scan_wal_for_transactions()?;
        warnings.extend(scan_warnings);

        if !self.options.fast_recovery {
            self.set_recovery_state(RecoveryState::Validating)?;
            let validation_warnings = self.validate_transactions(&transactions)?;
            warnings.extend(validation_warnings);
        }

        self.set_recovery_state(RecoveryState::Replaying)?;
        let replay_warnings = self.replay_transactions(&transactions)?;
        warnings.extend(replay_warnings);

        // Post-recovery validation: validate integrity after WAL replay completes
        let post_validation_warnings = self.validate_post_recovery(&transactions)?;
        warnings.extend(post_validation_warnings);

        self.set_recovery_state(RecoveryState::Finalizing)?;
        let final_warnings = self.finalize_recovery()?;
        warnings.extend(final_warnings);

        self.update_transaction_metrics(&transactions);
        Ok(warnings)
    }

    /// Finalize successful recovery
    fn finalize_successful_recovery(
        &self,
        start_time: Instant,
        start_timestamp: u64,
        warnings: Vec<String>,
    ) -> RecoveryResult {
        let duration = start_time.elapsed();
        let end_timestamp = start_timestamp + duration.as_secs();

        {
            let mut metrics = self.metrics.lock();
            metrics.total_duration_ms = duration.as_millis() as u64;
            metrics.recovery_start_timestamp = start_timestamp;
            metrics.recovery_end_timestamp = end_timestamp;

            if let Ok(metadata) = std::fs::metadata(&self.database_path) {
                metrics.database_size_after_recovery = metadata.len();
            }
        }

        self.set_recovery_state(RecoveryState::Complete)?;
        self.recovery_cv.notify_all();

        info_log!("Recovery completed successfully in {:?}", duration);
        Ok(RecoverySuccess {
            metrics: self.metrics.lock().clone(),
            warnings,
            duration,
        })
    }

    /// Finalize failed recovery
    fn finalize_failed_recovery(
        &self,
        start_time: Instant,
        start_timestamp: u64,
        error: RecoveryError,
    ) -> RecoveryResult {
        let duration = start_time.elapsed();

        {
            let mut metrics = self.metrics.lock();
            metrics.total_duration_ms = duration.as_millis() as u64;
            metrics.recovery_start_timestamp = start_timestamp;
            metrics.recovery_end_timestamp = start_timestamp + duration.as_secs();
        }

        self.set_recovery_state(RecoveryState::Failed)?;
        self.recovery_cv.notify_all();

        error_log!("Recovery failed after {:?}: {}", duration, error);
        Err(error)
    }

    /// Set recovery state
    fn set_recovery_state(&self, new_state: RecoveryState) -> Result<(), RecoveryError> {
        let mut state = self.state.lock();

        if !self.is_valid_state_transition(*state, new_state) {
            return Err(RecoveryError::state_transition(format!(
                "Invalid transition from {:?} to {:?}",
                *state, new_state
            )));
        }

        *state = new_state;
        debug_log!("Recovery state: {:?}", new_state);
        Ok(())
    }

    /// Validate state transition
    fn is_valid_state_transition(&self, from: RecoveryState, to: RecoveryState) -> bool {
        use RecoveryState::*;

        match (from, to) {
            (Idle, Initializing) => true,
            (Initializing, Scanning) => true,
            (Scanning, Validating) => true,
            (Scanning, Replaying) => true,
            (Validating, Replaying) => true,
            (Replaying, Finalizing) => true,
            (Finalizing, Complete) => true,
            (_, Failed) => true,
            _ => false,
        }
    }

    /// Update transaction metrics
    fn update_transaction_metrics(&self, transactions: &[TransactionState]) {
        let mut metrics = self.metrics.lock();
        metrics.transactions_scanned = transactions.len() as u64;
        metrics.committed_transactions_replayed =
            transactions.iter().filter(|tx| tx.committed).count() as u64;
        metrics.rolled_back_transactions =
            transactions.iter().filter(|tx| !tx.committed).count() as u64;
        metrics.records_processed = transactions.iter().map(|tx| tx.records.len() as u64).sum();
    }

    /// Get recovery progress
    pub fn get_progress(&self) -> RecoveryProgress {
        let state = *self.state.lock();
        RecoveryProgress {
            state,
            current_lsn: 0,
            total_lsns: 0,
            transactions_processed: 0,
            total_transactions: 0,
            completion_percentage: 0.0,
            start_time: Instant::now(),
            estimated_time_remaining: Duration::from_secs(0),
        }
    }

    /// Get current state
    pub fn get_state(&self) -> RecoveryState {
        *self.state.lock()
    }

    /// Get metrics
    pub fn get_metrics(&self) -> RecoveryMetrics {
        self.metrics.lock().clone()
    }

    /// Wait for completion
    pub fn wait_for_completion(&self, timeout: Duration) -> Result<RecoveryState, RecoveryError> {
        let mut state = self.state.lock();
        let result = self.recovery_cv.wait_for(&mut state, timeout);

        if result.timed_out() {
            return Err(RecoveryError::timeout(
                "Recovery completion timeout".to_string(),
            ));
        }

        Ok(*state)
    }

    /// Cancel recovery
    pub fn cancel_recovery(&self) -> Result<(), RecoveryError> {
        let mut state = self.state.lock();
        match *state {
            RecoveryState::Idle => Err(RecoveryError::configuration(
                "No recovery in progress".to_string(),
            )),
            RecoveryState::Complete | RecoveryState::Failed => Err(RecoveryError::configuration(
                "Recovery already completed".to_string(),
            )),
            _ => {
                *state = RecoveryState::Failed;
                self.recovery_cv.notify_all();
                info_log!("Recovery cancelled");
                Ok(())
            }
        }
    }

    // Specialized module methods
    fn scan_wal_for_transactions(
        &self,
    ) -> Result<(Vec<TransactionState>, Vec<String>), RecoveryError> {
        // Delegate to specialized scanner module
        use super::scanner::{ScanStatistics, WALScanResult, WALScanner};

        let _scanner = WALScanner::new();
        let mut active_tx = self.active_transactions.lock();
        active_tx.clear();

        // Use scanner to process WAL file
        // TODO: Make this properly async - for now, simulate the result
        let scan_result = WALScanResult {
            transactions: Vec::new(),
            warnings: vec!["Async scanning not yet implemented".to_string()],
            statistics: ScanStatistics::default(),
        };

        // Update active transactions with scanner results
        *active_tx = HashMap::new(); // Will be populated by scanner

        Ok((scan_result.transactions, scan_result.warnings))
    }

    fn validate_transactions(
        &self,
        transactions: &[TransactionState],
    ) -> Result<Vec<String>, RecoveryError> {
        // Delegate to specialized validator module when implemented
        // For now, implement basic validation inline
        self.validate_transactions_basic(transactions)
    }

    fn replay_transactions(
        &self,
        transactions: &[TransactionState],
    ) -> Result<Vec<String>, RecoveryError> {
        use super::replayer::{ReplayConfig, V2GraphFileReplayer};

        // Create replayer with database path
        let config = ReplayConfig {
            strict_validation: self.options.perform_consistency_checks,
            max_batch_size: self.options.max_batch_size,
            operation_timeout_ms: validation::CONSISTENCY_CHECK_TIMEOUT_MS,
            create_backup: false, // Handled by recovery core
            progress_interval: RECOVERY_PROGRESS_INTERVAL,
            max_parallel_transactions: self.options.max_parallel_transactions,
        };

        let replayer =
            V2GraphFileReplayer::create(self.database_path.clone(), config).map_err(|e| {
                RecoveryError::configuration(format!("Failed to create replayer: {}", e))
            })?;

        // Replay transactions using V2 integration
        let replay_result = replayer.replay_transactions(transactions).map_err(|e| {
            RecoveryError::replay_failure(format!("Transaction replay failed: {}", e))
        })?;

        // Update recovery metrics
        {
            let mut metrics = self.metrics.lock();
            metrics.committed_transactions_replayed = replay_result.successful_operations;
            metrics.corrupted_records += replay_result.failed_operations.len() as u64;
            metrics.records_processed += transactions
                .iter()
                .map(|tx| tx.records.len() as u64)
                .sum::<u64>();
        }

        Ok(replay_result.warnings)
    }

    /// Basic transaction validation (temporary until validator module is complete)
    fn validate_transactions_basic(
        &self,
        transactions: &[TransactionState],
    ) -> Result<Vec<String>, RecoveryError> {
        let mut warnings = Vec::new();
        let mut tx_ids = std::collections::HashSet::new();

        for tx in transactions {
            // Check for duplicate transaction IDs
            if tx_ids.contains(&tx.tx_id) {
                warnings.push(format!("Duplicate transaction ID: {}", tx.tx_id));
            }
            tx_ids.insert(&tx.tx_id);

            // Validate transaction sequence
            if tx.start_lsn == 0 {
                warnings.push(format!("Transaction TX {} has invalid start LSN", tx.tx_id));
            }

            if tx.committed && tx.commit_lsn.is_none() {
                warnings.push(format!(
                    "Committed transaction TX {} missing commit LSN",
                    tx.tx_id
                ));
            }

            if !tx.committed && tx.commit_lsn.is_some() {
                warnings.push(format!(
                    "Uncommitted transaction TX {} has commit LSN",
                    tx.tx_id
                ));
            }

            // Validate record order
            if let Some(commit_lsn) = tx.commit_lsn {
                if commit_lsn < tx.start_lsn {
                    warnings.push(format!(
                        "Transaction TX {} commit LSN before start LSN",
                        tx.tx_id
                    ));
                }
            }

            // Validate record count
            if tx.records.is_empty() {
                warnings.push(format!("Transaction TX {} has no records", tx.tx_id));
            }
        }

        debug_log!(
            "Transaction validation complete, {} warnings",
            warnings.len()
        );
        Ok(warnings)
    }

    fn finalize_recovery(&self) -> Result<Vec<String>, RecoveryError> {
        let mut warnings = Vec::new();
        self.active_transactions.lock().clear();

        if let Err(e) = std::fs::metadata(&self.database_path) {
            warnings.push(format!("Database validation issue: {:?}", e));
        }

        Ok(warnings)
    }

    /// Post-recovery validation hook called after WAL replay completes.
    ///
    /// This method validates the integrity of the recovered database to ensure
    /// no corruption occurred during recovery. It uses the RecoveryValidator to
    /// validate the replayed transaction sequence and perform database-level
    /// integrity checks on the graph file.
    ///
    /// # Arguments
    /// * `transactions` - The list of transactions that were replayed
    ///
    /// # Returns
    /// * `Ok(Vec<String>)` - List of validation warnings (non-critical issues)
    /// * `Err(RecoveryError)` - Critical validation error preventing recovery completion
    fn validate_post_recovery(
        &self,
        transactions: &[TransactionState],
    ) -> Result<Vec<String>, RecoveryError> {
        debug_log!(
            "Starting post-recovery validation for {} transactions",
            transactions.len()
        );

        let mut all_warnings = Vec::new();

        // Create validator with database path
        let mut validator = RecoveryValidator::new(self.database_path.clone()).map_err(|e| {
            RecoveryError::validation(format!("Failed to create recovery validator: {}", e))
        })?;

        // Validate the recovery sequence (transaction-level validation)
        let (_stats, tx_warnings) =
            validator
                .validate_recovery_sequence(transactions)
                .map_err(|e| {
                    error_log!("Post-recovery transaction validation failed: {}", e);
                    e
                })?;
        all_warnings.extend(tx_warnings);

        // Perform database-level integrity checks if enabled
        if self.options.perform_consistency_checks {
            debug_log!("Performing database integrity checks");

            // Calculate number of transactions replayed (committed only)
            let transactions_replayed =
                transactions.iter().filter(|tx| tx.committed).count() as u64;

            // Open graph file to verify basic integrity
            let graph_integrity_result = self.validate_graph_file_integrity(transactions_replayed);
            match graph_integrity_result {
                Ok(integrity_warnings) => {
                    all_warnings.extend(integrity_warnings);
                }
                Err(e) => {
                    // Database integrity errors are critical
                    error_log!("Database integrity check failed: {}", e);
                    return Err(e);
                }
            }

            // If perform_consistency_checks is enabled, also call the comprehensive validator
            let integrity_result = validator.validate_database_integrity().map_err(|e| {
                error_log!("Database integrity validation failed: {}", e);
                e
            })?;

            match integrity_result {
                ValidationResult::Valid => {
                    debug_log!("Database integrity validation passed");
                }
                ValidationResult::Recoverable { issues, .. } => {
                    warn_log!(
                        "Database integrity validation passed with {} warnings",
                        issues.len()
                    );
                    all_warnings.extend(issues);
                }
                ValidationResult::Invalid {
                    errors,
                    critical_error,
                } => {
                    error_log!("Database integrity validation failed: {}", critical_error);
                    for error in &errors {
                        debug_log!("Integrity error: {}", error);
                    }
                    return Err(RecoveryError::validation(format!(
                        "Database integrity check failed: {}",
                        critical_error
                    )));
                }
            }
        }

        // Log validation results
        if all_warnings.is_empty() {
            info_log!("Post-recovery validation passed with no warnings");
        } else {
            warn_log!(
                "Post-recovery validation passed with {} warnings",
                all_warnings.len()
            );
            for warning in &all_warnings {
                debug_log!("Validation warning: {}", warning);
            }
        }

        Ok(all_warnings)
    }

    /// Validate graph file integrity after recovery
    ///
    /// Performs basic graph file integrity checks including node count consistency
    /// and file size validation.
    ///
    /// # Arguments
    /// * `transactions_replayed` - Number of committed transactions that were replayed
    ///
    /// # Returns
    /// * `Ok(Vec<String>)` - List of warnings (non-critical issues)
    /// * `Err(RecoveryError)` - Critical integrity error
    fn validate_graph_file_integrity(
        &self,
        transactions_replayed: u64,
    ) -> Result<Vec<String>, RecoveryError> {
        let mut warnings = Vec::new();

        // Open the graph file
        let graph_file = GraphFile::open(&self.database_path).map_err(|e| {
            RecoveryError::validation(format!("Cannot open graph file for integrity check: {}", e))
        })?;

        // Get persistent header
        let header = graph_file.persistent_header();

        // Check node count consistency
        // If transactions were replayed, we expect the database to have content
        if transactions_replayed > 0 && header.node_count == 0 {
            warnings.push(
                "Transactions were replayed but node_count is 0 - possible data loss".to_string(),
            );
        }

        // Check file size is reasonable (not truncated)
        let file_size = graph_file
            .file_size()
            .map_err(|e| RecoveryError::validation(format!("Cannot get file size: {}", e)))?;

        // File should at minimum contain the header
        let min_expected_size = crate::backend::native::constants::HEADER_SIZE as u64;
        if file_size < min_expected_size {
            return Err(RecoveryError::validation(format!(
                "Graph file appears truncated: size {} bytes is less than minimum {} bytes",
                file_size, min_expected_size
            )));
        }

        // Check that the file size matches what the header describes
        let max_offset = header
            .free_space_offset
            .max(header.incoming_cluster_offset)
            .max(header.outgoing_cluster_offset)
            .max(header.edge_data_offset)
            .max(header.node_data_offset);

        if file_size < max_offset {
            warnings.push(format!(
                "File size {} bytes is less than expected max offset {} bytes - possible truncation",
                file_size, max_offset
            ));
        }

        // Validate persistent header structure
        header.validate().map_err(|e| {
            RecoveryError::validation(format!("Persistent header validation failed: {}", e))
        })?;

        debug_log!(
            "Graph file integrity check passed: node_count={}, edge_count={}, file_size={}",
            header.node_count,
            header.edge_count,
            file_size
        );

        Ok(warnings)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    #[test]
    fn test_recovery_state_transitions() {
        assert!(is_valid_transition_static(
            RecoveryState::Idle,
            RecoveryState::Initializing
        ));
        assert!(is_valid_transition_static(
            RecoveryState::Initializing,
            RecoveryState::Scanning
        ));
        assert!(is_valid_transition_static(
            RecoveryState::Scanning,
            RecoveryState::Validating
        ));
        assert!(is_valid_transition_static(
            RecoveryState::Validating,
            RecoveryState::Replaying
        ));
        assert!(is_valid_transition_static(
            RecoveryState::Replaying,
            RecoveryState::Finalizing
        ));
        assert!(is_valid_transition_static(
            RecoveryState::Finalizing,
            RecoveryState::Complete
        ));

        assert!(!is_valid_transition_static(
            RecoveryState::Complete,
            RecoveryState::Scanning
        ));
        assert!(!is_valid_transition_static(
            RecoveryState::Idle,
            RecoveryState::Replaying
        ));
    }

    #[test]
    fn test_recovery_options_default() {
        let options = RecoveryOptions::default();
        assert!(!options.fast_recovery);
        assert_eq!(options.max_batch_size, DEFAULT_BATCH_SIZE);
        assert_eq!(
            options.recovery_timeout,
            Duration::from_secs(DEFAULT_RECOVERY_TIMEOUT_SECONDS)
        );
        assert!(options.perform_consistency_checks);
        assert!(options.create_backup);
        assert_eq!(options.max_recovery_attempts, MAX_RECOVERY_ATTEMPTS);
        assert!(!options.force_recovery);
        assert_eq!(options.max_parallel_transactions, 4);
    }

    fn is_valid_transition_static(from: RecoveryState, to: RecoveryState) -> bool {
        use RecoveryState::*;

        match (from, to) {
            (Idle, Initializing) => true,
            (Initializing, Scanning) => true,
            (Scanning, Validating) => true,
            (Scanning, Replaying) => true,
            (Validating, Replaying) => true,
            (Replaying, Finalizing) => true,
            (Finalizing, Complete) => true,
            (_, Failed) => true,
            _ => false,
        }
    }

    /// Test post-recovery validation hook with valid empty transaction sequence
    #[test]
    fn test_post_recovery_hook_with_empty_transactions() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let wal_path = temp_dir.path().join("test.wal");

        // Create a minimal V2 graph file for testing
        let _graph_file = crate::backend::native::GraphFile::create(&db_path).unwrap();
        std::fs::File::create(&wal_path).unwrap();

        let config = V2WALConfig {
            wal_path,
            checkpoint_path: temp_dir.path().join("test.checkpoint"),
            ..Default::default()
        };

        let options = RecoveryOptions {
            create_backup: false,
            perform_consistency_checks: false, // Disable consistency checks for this test
            ..Default::default()
        };

        let engine = V2WALRecoveryEngine::create(config, db_path, options).unwrap();
        let transactions: Vec<TransactionState> = Vec::new();

        // validate_post_recovery should succeed with empty transactions
        let result = engine.validate_post_recovery(&transactions);
        assert!(
            result.is_ok(),
            "validate_post_recovery should succeed with empty transactions: {:?}",
            result.err()
        );

        let warnings = result.unwrap();
        // Empty transactions should not produce warnings when consistency checks are disabled
        assert!(
            warnings.is_empty(),
            "Expected no warnings for empty transactions with consistency checks disabled"
        );
    }

    /// Test post-recovery validation hook returns warnings for non-critical issues
    #[test]
    fn test_post_recovery_returns_warnings() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let wal_path = temp_dir.path().join("test.wal");

        // Create a minimal V2 graph file for testing
        let _graph_file = crate::backend::native::GraphFile::create(&db_path).unwrap();
        std::fs::File::create(&wal_path).unwrap();

        let config = V2WALConfig {
            wal_path,
            checkpoint_path: temp_dir.path().join("test.checkpoint"),
            ..Default::default()
        };

        let options = RecoveryOptions {
            create_backup: false,
            ..Default::default()
        };

        let engine = V2WALRecoveryEngine::create(config, db_path, options).unwrap();

        // Create a simple transaction state
        let transactions = vec![TransactionState {
            tx_id: 1,
            start_lsn: 100,
            commit_lsn: Some(200),
            records: vec![],
            committed: true,
            timestamp: 12345,
        }];

        // validate_post_recovery should succeed and potentially return warnings
        let result = engine.validate_post_recovery(&transactions);
        assert!(
            result.is_ok(),
            "validate_post_recovery should succeed: {:?}",
            result.err()
        );

        let warnings = result.unwrap();
        // We expect validation to complete - warnings may or may not be present
        // depending on whether empty records trigger validation warnings
    }

    /// Test post-recovery validation hook is called during attempt_recovery flow
    #[test]
    fn test_post_recovery_hook_is_called_in_recovery_flow() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let wal_path = temp_dir.path().join("test.wal");

        // Create minimal database and WAL files
        std::fs::File::create(&db_path).unwrap();
        std::fs::File::create(&wal_path).unwrap();

        let config = V2WALConfig {
            wal_path,
            checkpoint_path: temp_dir.path().join("test.checkpoint"),
            ..Default::default()
        };

        let options = RecoveryOptions {
            create_backup: false,
            fast_recovery: true, // Skip pre-replay validation
            ..Default::default()
        };

        let engine = V2WALRecoveryEngine::create(config, db_path, options).unwrap();

        // Verify the method exists and is callable
        let transactions: Vec<TransactionState> = Vec::new();
        let result = engine.validate_post_recovery(&transactions);

        // The test passes if we can call the method without panicking
        assert!(
            result.is_ok() || result.is_err(),
            "validate_post_recovery should be callable"
        );
    }

    /// Test post-recovery validation with empty WAL (no transactions replayed)
    #[test]
    fn test_post_recovery_with_empty_wal() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let wal_path = temp_dir.path().join("test.wal");

        // Create a minimal V2 graph file for testing
        let _graph_file = crate::backend::native::GraphFile::create(&db_path).unwrap();
        std::fs::File::create(&wal_path).unwrap();

        let config = V2WALConfig {
            wal_path,
            checkpoint_path: temp_dir.path().join("test.checkpoint"),
            ..Default::default()
        };

        let options = RecoveryOptions {
            create_backup: false,
            perform_consistency_checks: false, // Disable to avoid cluster offset warnings in newly created files
            ..Default::default()
        };

        let engine = V2WALRecoveryEngine::create(config, db_path, options).unwrap();
        let transactions: Vec<TransactionState> = Vec::new();

        // validate_post_recovery should succeed when WAL is empty
        let result = engine.validate_post_recovery(&transactions);
        assert!(
            result.is_ok(),
            "validate_post_recovery should succeed with empty WAL: {:?}",
            result.err()
        );

        let warnings = result.unwrap();
        // Empty WAL with empty database should not produce warnings when consistency checks are disabled
        assert!(
            warnings.is_empty(),
            "Expected no warnings for empty WAL and database with consistency checks disabled"
        );
    }

    /// Test post-recovery validation detects truncated file
    #[test]
    fn test_post_recovery_detects_truncated_file() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let wal_path = temp_dir.path().join("test.wal");

        // Create a valid V2 graph file first
        let _graph_file = crate::backend::native::GraphFile::create(&db_path).unwrap();
        std::fs::File::create(&wal_path).unwrap();

        // Truncate the file to less than header size (corruption simulation)
        const HEADER_SIZE: u64 = 80;
        std::fs::File::options()
            .write(true)
            .open(&db_path)
            .unwrap()
            .set_len(HEADER_SIZE - 10)
            .unwrap();

        let config = V2WALConfig {
            wal_path,
            checkpoint_path: temp_dir.path().join("test.checkpoint"),
            ..Default::default()
        };

        let options = RecoveryOptions {
            create_backup: false,
            perform_consistency_checks: true,
            ..Default::default()
        };

        let engine = V2WALRecoveryEngine::create(config, db_path, options).unwrap();
        let transactions: Vec<TransactionState> = Vec::new();

        // validate_post_recovery should fail with truncated file
        let result = engine.validate_post_recovery(&transactions);
        assert!(
            result.is_err(),
            "validate_post_recovery should fail with truncated file"
        );

        let err_msg = format!("{:?}", result.unwrap_err());
        // The error may mention "too small", "truncated", or file access issues
        assert!(
            err_msg.contains("too small")
                || err_msg.contains("truncated")
                || err_msg.contains("Cannot open"),
            "Error should mention file size issue, got: {}",
            err_msg
        );
    }

    /// Test post-recovery validation with node count inconsistency
    #[test]
    fn test_post_recovery_validates_node_count() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let wal_path = temp_dir.path().join("test.wal");

        // Create a minimal V2 graph file for testing
        let _graph_file = crate::backend::native::GraphFile::create(&db_path).unwrap();
        std::fs::File::create(&wal_path).unwrap();

        let config = V2WALConfig {
            wal_path,
            checkpoint_path: temp_dir.path().join("test.checkpoint"),
            ..Default::default()
        };

        let options = RecoveryOptions {
            create_backup: false,
            perform_consistency_checks: true, // Enable consistency checks to get node count validation
            ..Default::default()
        };

        let engine = V2WALRecoveryEngine::create(config, db_path, options).unwrap();

        // Create a committed transaction to simulate replay activity
        let transactions = vec![TransactionState {
            tx_id: 1,
            start_lsn: 100,
            commit_lsn: Some(200),
            records: vec![],
            committed: true,
            timestamp: 12345,
        }];

        // validate_post_recovery should succeed
        let result = engine.validate_post_recovery(&transactions);
        assert!(
            result.is_ok(),
            "validate_post_recovery should succeed: {:?}",
            result.err()
        );

        let warnings = result.unwrap();
        // The node count warning should be present since we have committed transactions but node_count=0
        // We may also have file size warnings due to cluster offset initialization
        // The key is that at least one of the warnings is about node count or transactions replayed
        let has_expected_warning = warnings
            .iter()
            .any(|w| w.contains("node_count") || w.contains("Transactions were replayed"));
        assert!(
            has_expected_warning,
            "Expected warning about node_count inconsistency when transactions were replayed but database is empty. Got warnings: {:?}",
            warnings
        );
    }

    /// Test post-recovery validation checks free space consistency
    #[test]
    fn test_post_recovery_validates_free_space() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let wal_path = temp_dir.path().join("test.wal");

        // Create a minimal V2 graph file for testing
        let _graph_file = crate::backend::native::GraphFile::create(&db_path).unwrap();
        std::fs::File::create(&wal_path).unwrap();

        let config = V2WALConfig {
            wal_path,
            checkpoint_path: temp_dir.path().join("test.checkpoint"),
            ..Default::default()
        };

        let options = RecoveryOptions {
            create_backup: false,
            perform_consistency_checks: false, // Use basic integrity checks only
            ..Default::default()
        };

        let engine = V2WALRecoveryEngine::create(config, db_path, options).unwrap();
        let transactions: Vec<TransactionState> = Vec::new();

        // validate_post_recovery should check free space consistency
        let result = engine.validate_post_recovery(&transactions);
        assert!(
            result.is_ok(),
            "validate_post_recovery should succeed: {:?}",
            result.err()
        );

        // For a newly created file, validation should succeed
        let warnings = result.unwrap();
        // With consistency checks disabled, minimal warnings expected
        assert!(
            warnings.is_empty(),
            "Expected no warnings with consistency checks disabled, got: {:?}",
            warnings
        );
    }

    /// Test graph_file_integrity validates node_count after recovery with transactions
    #[test]
    fn test_graph_file_integrity_validates_node_count() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let wal_path = temp_dir.path().join("test.wal");

        // Create a minimal V2 graph file for testing
        let _graph_file = crate::backend::native::GraphFile::create(&db_path).unwrap();
        std::fs::File::create(&wal_path).unwrap();

        let config = V2WALConfig {
            wal_path,
            checkpoint_path: temp_dir.path().join("test.checkpoint"),
            ..Default::default()
        };

        let options = RecoveryOptions {
            create_backup: false,
            ..Default::default()
        };

        let engine = V2WALRecoveryEngine::create(config, db_path, options).unwrap();

        // Test with transactions replayed = 1 (committed transaction)
        let result = engine.validate_graph_file_integrity(1);
        assert!(
            result.is_ok(),
            "validate_graph_file_integrity should succeed"
        );

        let warnings = result.unwrap();
        // Should have warning about node_count being 0 despite transactions replayed
        assert!(
            !warnings.is_empty(),
            "Expected warning when node_count=0 but transactions_replayed>0"
        );
        assert!(
            warnings[0].contains("node_count")
                || warnings[0].contains("Transactions were replayed"),
            "Warning should mention node_count or transactions replayed: {}",
            warnings[0]
        );
    }

    /// Test graph_file_integrity passes with no transactions replayed
    #[test]
    fn test_graph_file_integrity_passes_with_no_transactions() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let wal_path = temp_dir.path().join("test.wal");

        // Create a minimal V2 graph file for testing
        let _graph_file = crate::backend::native::GraphFile::create(&db_path).unwrap();
        std::fs::File::create(&wal_path).unwrap();

        let config = V2WALConfig {
            wal_path,
            checkpoint_path: temp_dir.path().join("test.checkpoint"),
            ..Default::default()
        };

        let options = RecoveryOptions {
            create_backup: false,
            ..Default::default()
        };

        let engine = V2WALRecoveryEngine::create(config, db_path, options).unwrap();

        // Test with transactions replayed = 0 (no committed transactions)
        let result = engine.validate_graph_file_integrity(0);
        assert!(
            result.is_ok(),
            "validate_graph_file_integrity should succeed with no transactions"
        );

        let warnings = result.unwrap();
        // With no transactions replayed, we shouldn't have node_count warnings
        // But we may have file size warnings due to cluster offset initialization
        let has_node_count_warning = warnings
            .iter()
            .any(|w| w.contains("node_count") || w.contains("Transactions were replayed"));
        assert!(
            !has_node_count_warning,
            "Should not have node_count warning with no transactions, got: {:?}",
            warnings
        );
    }
}