libdictenstein 0.1.0

High-performance dictionary data structures (trie, DAWG, double-array trie, suffix automaton, lock-free durable persistent ART) behind one trait API; pairs with liblevenshtein for fuzzy matching
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
//! Crash recovery for PersistentARTrieChar.
//!
//! This module implements corruption detection and WAL-based recovery for the
//! character-level persistent trie. It handles:
//!
//! - Arena checksum validation (V3+ arenas)
//! - File header checksum verification (V2+ headers)
//! - WAL segment collection from archive mode
//! - Full trie reconstruction from WAL records
//!
//! # Recovery Strategies
//!
//! 1. **Normal**: File checksums valid, WAL replayed after last checkpoint
//! 2. **Partial Recovery**: Some arenas corrupted, rebuild affected portions from WAL
//! 3. **Rebuild from WAL**: File severely corrupted, full reconstruction from archived WAL
//! 4. **Unrecoverable**: WAL also corrupted or missing, data loss
//!
//! # Example
//!
//! ```text
//! use libdictenstein::persistent_artrie_char::recovery::{
//!     RecoveryManager, RecoveryPolicy, detect_corruption
//! };
//!
//! // Check for corruption
//! if let Some(corruption) = detect_corruption(&trie_path)? {
//!     println!("Corruption detected: {:?}", corruption);
//!
//!     // Attempt recovery
//!     let report = RecoveryManager::new(&trie_path, &wal_config)
//!         .recover_with_policy(RecoveryPolicy::AutoRecover)?;
//!
//!     println!("Recovered {} records", report.records_replayed);
//! }
//! ```

use std::fs::{self, File};
use std::io::{BufReader, Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

use super::arena::{ArenaValidation, CharNodeArena, HEADER_SIZE as ARENA_HEADER_SIZE};
use super::dict_impl_char::{CharTrieFileHeader, CHAR_FILE_HEADER_SIZE, CHAR_TRIE_MAGIC};
use crate::persistent_artrie::disk_manager::BLOCK_SIZE;
use crate::persistent_artrie::error::{PersistentARTrieError, Result};
use crate::persistent_artrie::wal::{Lsn, WalConfig, WalReader, WalRecord, WalWriter};

// Re-export node-agnostic recovery types from the 1-byte implementation
pub use crate::persistent_artrie::recovery::{
    find_wal_archive_segments, rebuild_from_wal_segments, IncrementalRecovery, RecoveredState,
    RecoveryError, RecoveryStats,
};

/// Helper to convert io::Error to PersistentARTrieError
fn io_err(operation: &str, path: &Path, e: std::io::Error) -> PersistentARTrieError {
    PersistentARTrieError::IoError {
        operation: operation.to_string(),
        path: path.display().to_string(),
        source: e,
    }
}

/// Recovery mode indicating what type of recovery was performed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecoveryMode {
    /// File was valid, normal open (possibly with WAL replay after checkpoint)
    Normal {
        /// Number of WAL records replayed after last checkpoint
        wal_records_replayed: usize,
    },

    /// Some arenas were corrupted but recovered from WAL
    PartialRecovery {
        /// Block IDs of corrupted arenas
        corrupted_arenas: Vec<u32>,
        /// Number of records recovered from WAL
        recovered_records: usize,
    },

    /// File severely corrupted, rebuilt from WAL segments
    RebuildFromWal {
        /// Number of WAL segments processed
        segments_processed: usize,
        /// Total records replayed
        records_replayed: usize,
    },

    /// Recovery failed, data may be lost
    Unrecoverable {
        /// Reason for failure
        reason: String,
    },
}

impl RecoveryMode {
    /// Returns true if recovery was successful
    pub fn is_success(&self) -> bool {
        !matches!(self, RecoveryMode::Unrecoverable { .. })
    }

    /// Returns the number of records replayed during recovery
    pub fn records_replayed(&self) -> usize {
        match self {
            RecoveryMode::Normal {
                wal_records_replayed,
            } => *wal_records_replayed,
            RecoveryMode::PartialRecovery {
                recovered_records, ..
            } => *recovered_records,
            RecoveryMode::RebuildFromWal {
                records_replayed, ..
            } => *records_replayed,
            RecoveryMode::Unrecoverable { .. } => 0,
        }
    }
}

/// Recovery statistics and outcome.
#[derive(Debug, Clone)]
pub struct RecoveryReport {
    /// The type of recovery performed
    pub mode: RecoveryMode,
    /// Total time spent in recovery
    pub duration: Duration,
    /// Number of records replayed
    pub records_replayed: usize,
    /// Checkpoint LSN that recovery started from (if any)
    pub checkpoint_lsn: Option<Lsn>,
    /// Number of WAL segments processed
    pub segments_processed: usize,
    /// Number of corrupted records skipped
    pub corrupted_records_skipped: usize,
}

impl RecoveryReport {
    /// Create a new report for normal open
    pub fn normal() -> Self {
        Self {
            mode: RecoveryMode::Normal {
                wal_records_replayed: 0,
            },
            duration: Duration::ZERO,
            records_replayed: 0,
            checkpoint_lsn: None,
            segments_processed: 0,
            corrupted_records_skipped: 0,
        }
    }
}

/// Information about detected corruption.
#[derive(Debug, Clone)]
pub struct CorruptionInfo {
    /// Type of corruption detected
    pub corruption_type: CorruptionType,
    /// Block IDs of corrupted arenas (if applicable)
    pub corrupted_arenas: Vec<u32>,
    /// Whether WAL is available for recovery
    pub wal_available: bool,
    /// Number of WAL segments found
    pub wal_segments: usize,
}

/// Types of corruption that can be detected.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CorruptionType {
    /// File header checksum mismatch
    HeaderChecksum { stored: u32, computed: u32 },
    /// File header magic number is invalid
    InvalidMagic,
    /// One or more arenas have checksum mismatches
    ArenaChecksum {
        /// Number of arenas with invalid checksums
        count: usize,
    },
    /// File is truncated
    Truncated { expected: u64, actual: u64 },
    /// File cannot be opened
    FileNotReadable { reason: String },
}

/// Policy for handling corruption during open.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RecoveryPolicy {
    /// Automatically recover, report what happened
    #[default]
    AutoRecover,
    /// Fail immediately if any corruption detected
    FailOnCorruption,
    /// Recover partial data, report losses
    RecoverPartial,
}

/// Detect corruption in a trie file without loading it.
///
/// This function performs lightweight validation:
/// 1. Checks file header magic and checksum
/// 2. Optionally validates arena checksums (if `validate_arenas` is true)
///
/// # Arguments
///
/// * `path` - Path to the .artrie file
/// * `validate_arenas` - Whether to validate all arena checksums (slower but thorough)
///
/// # Returns
///
/// * `Ok(None)` - No corruption detected
/// * `Ok(Some(info))` - Corruption detected, info contains details
/// * `Err(e)` - Error while checking (distinct from corruption)
pub fn detect_corruption(path: &Path, validate_arenas: bool) -> Result<Option<CorruptionInfo>> {
    // Check if file exists
    if !path.exists() {
        return Ok(None); // No file = no corruption
    }

    let file = match File::open(path) {
        Ok(f) => f,
        Err(e) => {
            return Ok(Some(CorruptionInfo {
                corruption_type: CorruptionType::FileNotReadable {
                    reason: e.to_string(),
                },
                corrupted_arenas: vec![],
                wal_available: false,
                wal_segments: 0,
            }));
        }
    };

    let metadata = file.metadata().map_err(|e| io_err("metadata", path, e))?;
    let file_size = metadata.len();

    // Check minimum size for header
    if file_size < CHAR_FILE_HEADER_SIZE as u64 {
        return Ok(Some(CorruptionInfo {
            corruption_type: CorruptionType::Truncated {
                expected: CHAR_FILE_HEADER_SIZE as u64,
                actual: file_size,
            },
            corrupted_arenas: vec![],
            wal_available: check_wal_available(path),
            wal_segments: count_wal_segments(path),
        }));
    }

    // Read and validate header
    let mut reader = BufReader::new(file);
    let mut header_buf = [0u8; CHAR_FILE_HEADER_SIZE];
    reader
        .read_exact(&mut header_buf)
        .map_err(|e| io_err("read header", path, e))?;

    let header = CharTrieFileHeader::from_bytes(&header_buf);

    // Check magic number
    if header.magic != CHAR_TRIE_MAGIC {
        return Ok(Some(CorruptionInfo {
            corruption_type: CorruptionType::InvalidMagic,
            corrupted_arenas: vec![],
            wal_available: check_wal_available(path),
            wal_segments: count_wal_segments(path),
        }));
    }

    // Verify header checksum (V2+)
    if header.has_checksum() && !header.verify_checksum() {
        return Ok(Some(CorruptionInfo {
            corruption_type: CorruptionType::HeaderChecksum {
                stored: header.header_checksum,
                computed: header.compute_checksum(),
            },
            corrupted_arenas: vec![],
            wal_available: check_wal_available(path),
            wal_segments: count_wal_segments(path),
        }));
    }

    // Optionally validate arena checksums
    if validate_arenas {
        let corrupted = validate_all_arenas(&mut reader, file_size);
        if !corrupted.is_empty() {
            return Ok(Some(CorruptionInfo {
                corruption_type: CorruptionType::ArenaChecksum {
                    count: corrupted.len(),
                },
                corrupted_arenas: corrupted,
                wal_available: check_wal_available(path),
                wal_segments: count_wal_segments(path),
            }));
        }
    }

    Ok(None)
}

/// Validate all arenas in a file.
fn validate_all_arenas<R: Read + Seek>(reader: &mut R, file_size: u64) -> Vec<u32> {
    let mut corrupted = Vec::new();
    let mut block_id = 0u32;
    let mut offset = CHAR_FILE_HEADER_SIZE as u64;

    // Round up to block boundary
    offset = (offset + BLOCK_SIZE as u64 - 1) / BLOCK_SIZE as u64 * BLOCK_SIZE as u64;

    while offset + BLOCK_SIZE as u64 <= file_size {
        if reader.seek(SeekFrom::Start(offset)).is_err() {
            break;
        }

        let mut block_buf = vec![0u8; BLOCK_SIZE];
        if reader.read_exact(&mut block_buf).is_err() {
            break; // Truncated file
        }

        // Only validate if it looks like an arena (has arena magic)
        if block_buf.len() >= ARENA_HEADER_SIZE {
            match CharNodeArena::validate_checksums(&block_buf) {
                Ok(ArenaValidation::Valid) => {}
                Ok(ArenaValidation::HeaderChecksumMismatch { .. })
                | Ok(ArenaValidation::DataChecksumMismatch { .. })
                | Ok(ArenaValidation::Truncated { .. }) => {
                    corrupted.push(block_id);
                }
                Ok(ArenaValidation::InvalidMagic) => {
                    // Not an arena, skip
                }
                Err(_) => {
                    corrupted.push(block_id);
                }
            }
        }

        offset += BLOCK_SIZE as u64;
        block_id += 1;
    }

    corrupted
}

/// Check if WAL is available for the given trie path.
fn check_wal_available(trie_path: &Path) -> bool {
    let wal_path = trie_path.with_extension("wal");
    wal_path.exists()
}

/// Count WAL segments available (active + archived).
fn count_wal_segments(trie_path: &Path) -> usize {
    let wal_path = trie_path.with_extension("wal");
    let archive_dir = trie_path
        .parent()
        .unwrap_or(Path::new("."))
        .join("wal_archive");

    let mut count = 0;

    // Check active WAL
    if wal_path.exists() {
        count += 1;
    }

    // Check archived segments
    if archive_dir.exists() {
        if let Ok(entries) = fs::read_dir(&archive_dir) {
            count += entries
                .filter_map(|e| e.ok())
                .filter(|e| e.path().extension().map_or(false, |ext| ext == "segment"))
                .count();
        }
    }

    count
}

/// Recovery manager for corrupted trie files.
pub struct RecoveryManager {
    /// Path to the trie file
    trie_path: PathBuf,
    /// Path to WAL file
    wal_path: PathBuf,
    /// WAL configuration for archive mode
    wal_config: WalConfig,
}

impl RecoveryManager {
    /// Create a new recovery manager.
    pub fn new(trie_path: impl AsRef<Path>, wal_config: WalConfig) -> Self {
        let trie_path = trie_path.as_ref().to_path_buf();
        let wal_path = trie_path.with_extension("wal");

        Self {
            trie_path,
            wal_path,
            wal_config,
        }
    }

    /// Check if recovery is needed.
    pub fn needs_recovery(&self) -> Result<bool> {
        detect_corruption(&self.trie_path, false).map(|opt| opt.is_some())
    }

    /// Perform recovery with specified policy.
    ///
    /// # Arguments
    ///
    /// * `policy` - How to handle corruption
    /// * `apply_fn` - Callback to apply recovered operations
    ///
    /// # Returns
    ///
    /// Recovery report with statistics, or error if recovery failed.
    pub fn recover_with_callback<F>(
        &self,
        policy: RecoveryPolicy,
        mut apply_fn: F,
    ) -> Result<RecoveryReport>
    where
        F: FnMut(RecoveredOperation) -> Result<()>,
    {
        let start = Instant::now();

        // Check for corruption
        let corruption = detect_corruption(&self.trie_path, true)?;

        match (corruption, policy) {
            // No corruption - normal operation
            (None, _) => {
                // Just replay any WAL records after checkpoint
                let replay_count = self.replay_wal_after_checkpoint(&mut apply_fn)?;

                Ok(RecoveryReport {
                    mode: RecoveryMode::Normal {
                        wal_records_replayed: replay_count,
                    },
                    duration: start.elapsed(),
                    records_replayed: replay_count,
                    checkpoint_lsn: self.get_checkpoint_lsn()?,
                    segments_processed: 1,
                    corrupted_records_skipped: 0,
                })
            }

            // Corruption detected but policy says fail
            (Some(info), RecoveryPolicy::FailOnCorruption) => {
                Err(PersistentARTrieError::CorruptedFile {
                    reason: format!("{:?}", info.corruption_type),
                })
            }

            // Corruption detected - attempt recovery
            (Some(info), RecoveryPolicy::AutoRecover | RecoveryPolicy::RecoverPartial) => {
                if !info.wal_available && info.wal_segments == 0 {
                    return Err(PersistentARTrieError::RecoveryError {
                        reason: "No WAL available for recovery".to_string(),
                    });
                }

                self.rebuild_from_wal(&mut apply_fn, start)
            }
        }
    }

    /// Replay WAL records after the last checkpoint.
    fn replay_wal_after_checkpoint<F>(&self, apply_fn: &mut F) -> Result<usize>
    where
        F: FnMut(RecoveredOperation) -> Result<()>,
    {
        if !self.wal_path.exists() {
            return Ok(0);
        }

        let checkpoint_lsn = self.get_checkpoint_lsn()?.unwrap_or(0);

        let reader = match WalReader::new(&self.wal_path) {
            Ok(r) => r,
            Err(_) => return Ok(0),
        };

        let mut replayed = 0;
        for result in reader.iter() {
            match result {
                Ok((lsn, record)) => {
                    // Only replay records after checkpoint
                    if lsn <= checkpoint_lsn {
                        continue;
                    }

                    for op in self.record_to_operations(lsn, record) {
                        apply_fn(op)?;
                        replayed += 1;
                    }
                }
                Err(_) => {
                    break;
                }
            }
        }

        Ok(replayed)
    }

    /// Rebuild the entire trie from WAL segments.
    fn rebuild_from_wal<F>(&self, apply_fn: &mut F, start: Instant) -> Result<RecoveryReport>
    where
        F: FnMut(RecoveredOperation) -> Result<()>,
    {
        // Collect all WAL segments
        let wal_writer = if self.wal_path.exists() {
            WalWriter::open(&self.wal_path)?
        } else {
            // No WAL - can't recover
            return Err(PersistentARTrieError::RecoveryError {
                reason: "WAL file not found".to_string(),
            });
        };

        let segments = wal_writer.collect_wal_segments(&self.wal_config)?;
        let segment_count = segments.len();

        if segments.is_empty() {
            return Ok(RecoveryReport {
                mode: RecoveryMode::RebuildFromWal {
                    segments_processed: 0,
                    records_replayed: 0,
                },
                duration: start.elapsed(),
                records_replayed: 0,
                checkpoint_lsn: None,
                segments_processed: 0,
                corrupted_records_skipped: 0,
            });
        }

        let mut replayed = 0;
        let mut corrupted_skipped = 0;

        // Process all segments in order
        'segments: for segment_path in &segments {
            let reader = match WalReader::new(segment_path) {
                Ok(r) => r,
                Err(_) => continue, // Skip unreadable segments
            };

            for result in reader.iter() {
                match result {
                    Ok((lsn, record)) => {
                        for op in self.record_to_operations(lsn, record) {
                            apply_fn(op)?;
                            replayed += 1;
                        }
                    }
                    Err(_) => {
                        corrupted_skipped += 1;
                        break 'segments;
                    }
                }
            }
        }

        Ok(RecoveryReport {
            mode: RecoveryMode::RebuildFromWal {
                segments_processed: segment_count,
                records_replayed: replayed,
            },
            duration: start.elapsed(),
            records_replayed: replayed,
            checkpoint_lsn: None,
            segments_processed: segment_count,
            corrupted_records_skipped: corrupted_skipped,
        })
    }

    /// Get the checkpoint LSN from the trie file header.
    fn get_checkpoint_lsn(&self) -> Result<Option<Lsn>> {
        if !self.trie_path.exists() {
            return Ok(None);
        }

        let mut file =
            File::open(&self.trie_path).map_err(|e| io_err("open", &self.trie_path, e))?;
        let mut header_buf = [0u8; CHAR_FILE_HEADER_SIZE];
        file.read_exact(&mut header_buf)
            .map_err(|e| io_err("read header", &self.trie_path, e))?;

        let header = CharTrieFileHeader::from_bytes(&header_buf);
        if header.checkpoint_lsn > 0 {
            Ok(Some(header.checkpoint_lsn))
        } else {
            Ok(None)
        }
    }

    /// Convert a WAL record to recovery operations.
    ///
    /// Returns a vector because BatchInsert records contain multiple operations.
    fn record_to_operations(&self, lsn: Lsn, record: WalRecord) -> Vec<RecoveredOperation> {
        crate::persistent_artrie::recovery::recovered_operations_from_record(lsn, record)
            .into_iter()
            .map(RecoveredOperation::from)
            .collect()
    }
}

/// A recovered operation ready to be applied.
#[derive(Debug, Clone)]
pub enum RecoveredOperation {
    /// Insert a term with optional value
    Insert {
        /// Log sequence number
        lsn: Lsn,
        /// Term bytes (UTF-8 encoded)
        term: Vec<u8>,
        /// Optional serialized value
        value: Option<Vec<u8>>,
    },
    /// Remove a term
    Remove {
        /// Log sequence number
        lsn: Lsn,
        /// Term bytes (UTF-8 encoded)
        term: Vec<u8>,
    },
    /// Increment a numeric value
    Increment {
        /// Log sequence number
        lsn: Lsn,
        /// Term bytes
        term: Vec<u8>,
        /// Delta that was added
        delta: i64,
        /// `Some(v)` = absolute set, `None` = delta (D2.8 D6) — mirrors the core
        /// `RecoveredOperation::Increment.result`; the conversion at `from_core`
        /// passes it through unchanged.
        result: Option<i64>,
    },
    /// Upsert a value
    Upsert {
        /// Log sequence number
        lsn: Lsn,
        /// Term bytes
        term: Vec<u8>,
        /// New value
        value: Vec<u8>,
    },
    /// Compare and swap
    CompareAndSwap {
        /// Log sequence number
        lsn: Lsn,
        /// Term bytes
        term: Vec<u8>,
        /// New value
        new_value: Vec<u8>,
        /// Whether successful
        success: bool,
    },
}

impl From<crate::persistent_artrie::recovery::RecoveredOperation> for RecoveredOperation {
    fn from(op: crate::persistent_artrie::recovery::RecoveredOperation) -> Self {
        match op {
            crate::persistent_artrie::recovery::RecoveredOperation::Insert { lsn, term, value } => {
                Self::Insert { lsn, term, value }
            }
            crate::persistent_artrie::recovery::RecoveredOperation::Remove { lsn, term } => {
                Self::Remove { lsn, term }
            }
            crate::persistent_artrie::recovery::RecoveredOperation::Increment {
                lsn,
                term,
                delta,
                result,
            } => Self::Increment {
                lsn,
                term,
                delta,
                result,
            },
            crate::persistent_artrie::recovery::RecoveredOperation::Upsert { lsn, term, value } => {
                Self::Upsert { lsn, term, value }
            }
            crate::persistent_artrie::recovery::RecoveredOperation::CompareAndSwap {
                lsn,
                term,
                new_value,
                success,
            } => Self::CompareAndSwap {
                lsn,
                term,
                new_value,
                success,
            },
        }
    }
}

impl RecoveredOperation {
    /// Get the term as a string (if valid UTF-8).
    pub fn term_str(&self) -> Option<&str> {
        let bytes = match self {
            Self::Insert { term, .. } => term,
            Self::Remove { term, .. } => term,
            Self::Increment { term, .. } => term,
            Self::Upsert { term, .. } => term,
            Self::CompareAndSwap { term, .. } => term,
        };
        std::str::from_utf8(bytes).ok()
    }

    /// Get the LSN of this operation.
    pub fn lsn(&self) -> Lsn {
        match self {
            Self::Insert { lsn, .. } => *lsn,
            Self::Remove { lsn, .. } => *lsn,
            Self::Increment { lsn, .. } => *lsn,
            Self::Upsert { lsn, .. } => *lsn,
            Self::CompareAndSwap { lsn, .. } => *lsn,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[test]
    fn test_recovery_mode_is_success() {
        assert!(RecoveryMode::Normal {
            wal_records_replayed: 0
        }
        .is_success());
        assert!(RecoveryMode::PartialRecovery {
            corrupted_arenas: vec![],
            recovered_records: 0
        }
        .is_success());
        assert!(RecoveryMode::RebuildFromWal {
            segments_processed: 0,
            records_replayed: 0
        }
        .is_success());
        assert!(!RecoveryMode::Unrecoverable {
            reason: "test".to_string()
        }
        .is_success());
    }

    #[test]
    fn test_detect_corruption_missing_file() {
        let dir = tempdir().expect("create tempdir");
        let path = dir.path().join("nonexistent.artrie");

        let result = detect_corruption(&path, false).expect("detect_corruption");
        assert!(result.is_none(), "Missing file should not be corruption");
    }

    #[test]
    fn test_detect_corruption_truncated_file() {
        let dir = tempdir().expect("create tempdir");
        let path = dir.path().join("truncated.artrie");

        // Create a truncated file
        fs::write(&path, &[0u8; 10]).expect("write file");

        let result = detect_corruption(&path, false).expect("detect_corruption");
        assert!(result.is_some());
        match result.unwrap().corruption_type {
            CorruptionType::Truncated { expected, actual } => {
                assert_eq!(expected, CHAR_FILE_HEADER_SIZE as u64);
                assert_eq!(actual, 10);
            }
            _ => panic!("Expected Truncated corruption type"),
        }
    }

    #[test]
    fn test_detect_corruption_invalid_magic() {
        let dir = tempdir().expect("create tempdir");
        let path = dir.path().join("bad_magic.artrie");

        // Create file with wrong magic
        let mut data = [0u8; CHAR_FILE_HEADER_SIZE];
        data[0..4].copy_from_slice(b"XXXX"); // Wrong magic
        fs::write(&path, &data).expect("write file");

        let result = detect_corruption(&path, false).expect("detect_corruption");
        assert!(result.is_some());
        assert!(matches!(
            result.unwrap().corruption_type,
            CorruptionType::InvalidMagic
        ));
    }

    #[test]
    fn test_corruption_info_wal_check() {
        let dir = tempdir().expect("create tempdir");
        let trie_path = dir.path().join("test.artrie");
        let wal_path = dir.path().join("test.wal");

        // Create files
        fs::write(&trie_path, &[0u8; 10]).expect("write trie");
        fs::write(&wal_path, &[0u8; 100]).expect("write wal");

        assert!(check_wal_available(&trie_path));
        assert!(count_wal_segments(&trie_path) >= 1);
    }

    #[test]
    fn test_recovery_manager_no_file() {
        let dir = tempdir().expect("create tempdir");
        let path = dir.path().join("missing.artrie");

        let config = WalConfig::default();
        let manager = RecoveryManager::new(&path, config);

        assert!(!manager.needs_recovery().expect("needs_recovery"));
    }

    #[test]
    fn test_recovered_operation_term_str() {
        let op = RecoveredOperation::Insert {
            lsn: 1,
            term: b"hello".to_vec(),
            value: None,
        };
        assert_eq!(op.term_str(), Some("hello"));
        assert_eq!(op.lsn(), 1);

        // Invalid UTF-8
        let op = RecoveredOperation::Insert {
            lsn: 2,
            term: vec![0xFF, 0xFE],
            value: None,
        };
        assert_eq!(op.term_str(), None);
    }

    #[test]
    fn test_recovery_report_normal() {
        let report = RecoveryReport::normal();
        assert!(report.mode.is_success());
        assert_eq!(report.records_replayed, 0);
    }
}