oxirs-tdb 0.3.1

Apache Jena TDB/TDB2 compatible RDF storage engine with B+Tree indexes
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
//! Diagnostic engine and built-in check collectors.
//!
//! This module hosts:
//! - [`DiagnosticEngine`] - registry + runner of [`DiagnosticCheck`] implementations
//! - [`DiagnosticContext`] - the input data fed to each check
//! - The trait [`DiagnosticCheck`] each individual check implements
//! - All built-in checks (buffer pool, memory, storage, performance, index/dictionary
//!   consistency, WAL integrity, corruption detection)

use crate::dictionary::NodeId;
use crate::error::Result;
use crate::index::Triple;
use crate::storage::BufferPoolStats;
use std::collections::HashSet;
use std::path::Path;
use std::time::Instant;

use crate::diagnostics_types::{
    DiagnosticLevel, DiagnosticReport, DiagnosticResult, DiagnosticSummary, HealthStatus, Severity,
};

/// Diagnostic engine
pub struct DiagnosticEngine {
    /// Registered diagnostic checks
    pub(crate) checks: Vec<Box<dyn DiagnosticCheck + Send + Sync>>,
}

/// Trait for diagnostic checks
pub trait DiagnosticCheck: Send + Sync {
    /// Get check name
    fn name(&self) -> &str;

    /// Get check category
    fn category(&self) -> &str;

    /// Minimum level required to run this check
    fn min_level(&self) -> DiagnosticLevel;

    /// Run the diagnostic check
    fn run(&self, context: &DiagnosticContext) -> Result<Vec<DiagnosticResult>>;
}

/// Context for running diagnostics
pub struct DiagnosticContext {
    /// Total number of triples
    pub triple_count: u64,
    /// Buffer pool statistics
    pub buffer_pool_stats: BufferPoolStats,
    /// Dictionary size
    pub dictionary_size: u64,
    /// Total storage size (bytes)
    pub storage_size_bytes: u64,
    /// Memory usage (bytes)
    pub memory_usage_bytes: usize,
    /// SPO index triples (for consistency checks)
    pub spo_triples: Option<Vec<Triple>>,
    /// POS index triples (for consistency checks)
    pub pos_triples: Option<Vec<Triple>>,
    /// OSP index triples (for consistency checks)
    pub osp_triples: Option<Vec<Triple>>,
    /// Dictionary node IDs (for consistency checks)
    pub dictionary_node_ids: Option<HashSet<NodeId>>,
    /// Data directory path (for WAL checks)
    pub data_dir: Option<String>,
}

impl DiagnosticContext {
    /// Create a quick context (no deep data collection)
    pub fn quick(
        triple_count: u64,
        buffer_pool_stats: BufferPoolStats,
        dictionary_size: u64,
        storage_size_bytes: u64,
        memory_usage_bytes: usize,
    ) -> Self {
        Self {
            triple_count,
            buffer_pool_stats,
            dictionary_size,
            storage_size_bytes,
            memory_usage_bytes,
            spo_triples: None,
            pos_triples: None,
            osp_triples: None,
            dictionary_node_ids: None,
            data_dir: None,
        }
    }

    /// Create a deep context (with full data collection for consistency checks)
    #[allow(clippy::too_many_arguments)]
    pub fn deep(
        triple_count: u64,
        buffer_pool_stats: BufferPoolStats,
        dictionary_size: u64,
        storage_size_bytes: u64,
        memory_usage_bytes: usize,
        spo_triples: Vec<Triple>,
        pos_triples: Vec<Triple>,
        osp_triples: Vec<Triple>,
        dictionary_node_ids: HashSet<NodeId>,
        data_dir: String,
    ) -> Self {
        Self {
            triple_count,
            buffer_pool_stats,
            dictionary_size,
            storage_size_bytes,
            memory_usage_bytes,
            spo_triples: Some(spo_triples),
            pos_triples: Some(pos_triples),
            osp_triples: Some(osp_triples),
            dictionary_node_ids: Some(dictionary_node_ids),
            data_dir: Some(data_dir),
        }
    }
}

impl DiagnosticEngine {
    /// Create a new diagnostic engine
    pub fn new() -> Self {
        let mut engine = Self { checks: Vec::new() };

        // Register built-in checks (Quick level)
        engine.register(Box::new(BufferPoolCheck));
        engine.register(Box::new(MemoryUsageCheck));

        // Register standard checks
        engine.register(Box::new(StorageEfficiencyCheck));
        engine.register(Box::new(PerformanceCheck));

        // Register advanced/deep checks
        engine.register(Box::new(IndexConsistencyCheck));
        engine.register(Box::new(DictionaryConsistencyCheck));
        engine.register(Box::new(WalIntegrityCheck));
        engine.register(Box::new(CorruptionDetectionCheck));

        engine
    }

    /// Register a diagnostic check
    pub fn register(&mut self, check: Box<dyn DiagnosticCheck + Send + Sync>) {
        self.checks.push(check);
    }

    /// Run diagnostics
    pub fn run(&self, level: DiagnosticLevel, context: &DiagnosticContext) -> DiagnosticReport {
        let start = Instant::now();
        let mut results = Vec::new();

        // Run all applicable checks
        for check in &self.checks {
            if Self::should_run_check(check.min_level(), level) {
                match check.run(context) {
                    Ok(mut check_results) => {
                        results.append(&mut check_results);
                    }
                    Err(e) => {
                        results.push(
                            DiagnosticResult::new(check.category(), check.name(), Severity::Error)
                                .with_description(format!("Diagnostic check failed: {}", e)),
                        );
                    }
                }
            }
        }

        let summary = DiagnosticSummary::from_results(&results);

        // Determine overall health status
        let health_status = if summary.critical_count > 0 {
            HealthStatus::Critical
        } else if summary.error_count > 0 {
            HealthStatus::Unhealthy
        } else if summary.warning_count > 0 {
            HealthStatus::Degraded
        } else {
            HealthStatus::Healthy
        };

        DiagnosticReport {
            timestamp: chrono::Utc::now().to_rfc3339(),
            level,
            duration: start.elapsed(),
            health_status,
            results,
            summary,
        }
    }

    fn should_run_check(check_level: DiagnosticLevel, requested_level: DiagnosticLevel) -> bool {
        matches!(
            (check_level, requested_level),
            (DiagnosticLevel::Quick, _)
                | (
                    DiagnosticLevel::Standard,
                    DiagnosticLevel::Standard | DiagnosticLevel::Deep
                )
                | (DiagnosticLevel::Deep, DiagnosticLevel::Deep)
        )
    }
}

impl Default for DiagnosticEngine {
    fn default() -> Self {
        Self::new()
    }
}

// Built-in diagnostic checks

/// Buffer pool diagnostic check
pub(crate) struct BufferPoolCheck;

impl DiagnosticCheck for BufferPoolCheck {
    fn name(&self) -> &str {
        "Buffer Pool Health"
    }

    fn category(&self) -> &str {
        "Performance"
    }

    fn min_level(&self) -> DiagnosticLevel {
        DiagnosticLevel::Quick
    }

    fn run(&self, context: &DiagnosticContext) -> Result<Vec<DiagnosticResult>> {
        let mut results = Vec::new();
        let hit_rate = context.buffer_pool_stats.hit_rate();

        // Check hit rate
        if hit_rate < 0.5 {
            results.push(
                DiagnosticResult::new(
                    self.category(),
                    "Low Buffer Pool Hit Rate",
                    Severity::Warning,
                )
                .with_description(format!(
                    "Buffer pool hit rate is {:.2}%, which is below optimal (>90%)",
                    hit_rate * 100.0
                ))
                .with_recommendation(
                    "Consider increasing buffer pool size or analyzing query patterns",
                )
                .with_detail("current_hit_rate", format!("{:.2}%", hit_rate * 100.0))
                .with_detail("target_hit_rate", "90%"),
            );
        } else if hit_rate >= 0.9 {
            results.push(
                DiagnosticResult::new(
                    self.category(),
                    "Optimal Buffer Pool Performance",
                    Severity::Info,
                )
                .with_description(format!("Buffer pool hit rate is {:.2}%", hit_rate * 100.0))
                .with_detail("hit_rate", format!("{:.2}%", hit_rate * 100.0)),
            );
        }

        Ok(results)
    }
}

/// Memory usage diagnostic check
pub(crate) struct MemoryUsageCheck;

impl DiagnosticCheck for MemoryUsageCheck {
    fn name(&self) -> &str {
        "Memory Usage"
    }

    fn category(&self) -> &str {
        "Resources"
    }

    fn min_level(&self) -> DiagnosticLevel {
        DiagnosticLevel::Quick
    }

    fn run(&self, context: &DiagnosticContext) -> Result<Vec<DiagnosticResult>> {
        let mut results = Vec::new();
        let memory_mb = context.memory_usage_bytes / (1024 * 1024);

        // Check if memory usage is excessive (> 1GB)
        if memory_mb > 1024 {
            results.push(
                DiagnosticResult::new(self.category(), "High Memory Usage", Severity::Warning)
                    .with_description(format!("Memory usage is {} MB", memory_mb))
                    .with_recommendation(
                        "Monitor memory growth and consider reducing buffer pool size",
                    )
                    .with_detail("memory_usage_mb", memory_mb.to_string()),
            );
        } else {
            results.push(
                DiagnosticResult::new(self.category(), "Normal Memory Usage", Severity::Info)
                    .with_description(format!("Memory usage is {} MB", memory_mb))
                    .with_detail("memory_usage_mb", memory_mb.to_string()),
            );
        }

        Ok(results)
    }
}

/// Storage efficiency diagnostic check
pub(crate) struct StorageEfficiencyCheck;

impl DiagnosticCheck for StorageEfficiencyCheck {
    fn name(&self) -> &str {
        "Storage Efficiency"
    }

    fn category(&self) -> &str {
        "Storage"
    }

    fn min_level(&self) -> DiagnosticLevel {
        DiagnosticLevel::Standard
    }

    fn run(&self, context: &DiagnosticContext) -> Result<Vec<DiagnosticResult>> {
        let mut results = Vec::new();

        // Calculate storage per triple
        if let Some(bytes_per_triple) = context.storage_size_bytes.checked_div(context.triple_count)
        {
            if bytes_per_triple > 500 {
                results.push(
                    DiagnosticResult::new(self.category(), "Storage Overhead", Severity::Warning)
                        .with_description(format!(
                            "Storage overhead is {} bytes per triple (expected ~100-200)",
                            bytes_per_triple
                        ))
                        .with_recommendation("Consider running compaction or enabling compression")
                        .with_detail("bytes_per_triple", bytes_per_triple.to_string()),
                );
            } else {
                results.push(
                    DiagnosticResult::new(self.category(), "Efficient Storage", Severity::Info)
                        .with_description(format!(
                            "Storage efficiency: {} bytes per triple",
                            bytes_per_triple
                        ))
                        .with_detail("bytes_per_triple", bytes_per_triple.to_string()),
                );
            }
        }

        Ok(results)
    }
}

/// Performance diagnostic check
pub(crate) struct PerformanceCheck;

impl DiagnosticCheck for PerformanceCheck {
    fn name(&self) -> &str {
        "Performance Metrics"
    }

    fn category(&self) -> &str {
        "Performance"
    }

    fn min_level(&self) -> DiagnosticLevel {
        DiagnosticLevel::Standard
    }

    fn run(&self, context: &DiagnosticContext) -> Result<Vec<DiagnosticResult>> {
        let mut results = Vec::new();

        use std::sync::atomic::Ordering;

        let evictions = context.buffer_pool_stats.evictions.load(Ordering::Relaxed);
        let total_fetches = context
            .buffer_pool_stats
            .total_fetches
            .load(Ordering::Relaxed);

        // Check eviction rate
        if total_fetches > 0 {
            let eviction_rate = (evictions as f64 / total_fetches as f64) * 100.0;

            if eviction_rate > 10.0 {
                results.push(
                    DiagnosticResult::new(self.category(), "High Eviction Rate", Severity::Warning)
                        .with_description(format!(
                            "Buffer pool eviction rate is {:.2}% (target <5%)",
                            eviction_rate
                        ))
                        .with_recommendation("Increase buffer pool size to reduce evictions")
                        .with_detail("eviction_rate", format!("{:.2}%", eviction_rate)),
                );
            } else {
                results.push(
                    DiagnosticResult::new(self.category(), "Normal Eviction Rate", Severity::Info)
                        .with_description(format!("Eviction rate: {:.2}%", eviction_rate))
                        .with_detail("eviction_rate", format!("{:.2}%", eviction_rate)),
                );
            }
        }

        Ok(results)
    }
}

/// Index consistency diagnostic check.
///
/// Verifies that SPO, POS, and OSP indexes contain the same set of triples.
pub(crate) struct IndexConsistencyCheck;

impl DiagnosticCheck for IndexConsistencyCheck {
    fn name(&self) -> &str {
        "Index Consistency"
    }

    fn category(&self) -> &str {
        "Integrity"
    }

    fn min_level(&self) -> DiagnosticLevel {
        DiagnosticLevel::Deep
    }

    fn run(&self, context: &DiagnosticContext) -> Result<Vec<DiagnosticResult>> {
        let mut results = Vec::new();

        // Check if we have the data needed for consistency checks
        if context.spo_triples.is_none()
            || context.pos_triples.is_none()
            || context.osp_triples.is_none()
        {
            results.push(
                DiagnosticResult::new(self.category(), "Insufficient Data", Severity::Warning)
                    .with_description("Deep context required for index consistency check")
                    .with_recommendation("Use DiagnosticContext::deep() for thorough checks"),
            );
            return Ok(results);
        }

        let spo_triples = context
            .spo_triples
            .as_ref()
            .expect("field validated to be Some above");
        let pos_triples = context
            .pos_triples
            .as_ref()
            .expect("field validated to be Some above");
        let osp_triples = context
            .osp_triples
            .as_ref()
            .expect("field validated to be Some above");

        // Convert to sets for comparison
        let spo_set: HashSet<Triple> = spo_triples.iter().copied().collect();
        let pos_set: HashSet<Triple> = pos_triples.iter().copied().collect();
        let osp_set: HashSet<Triple> = osp_triples.iter().copied().collect();

        // Check sizes
        if spo_set.len() != pos_set.len() || spo_set.len() != osp_set.len() {
            results.push(
                DiagnosticResult::new(self.category(), "Index Size Mismatch", Severity::Error)
                    .with_description(format!(
                        "Index sizes differ: SPO={}, POS={}, OSP={}",
                        spo_set.len(),
                        pos_set.len(),
                        osp_set.len()
                    ))
                    .with_recommendation("Run database repair to rebuild indexes")
                    .with_detail("spo_count", spo_set.len().to_string())
                    .with_detail("pos_count", pos_set.len().to_string())
                    .with_detail("osp_count", osp_set.len().to_string()),
            );
        }

        // Check if sets are equal
        let spo_minus_pos: HashSet<_> = spo_set.difference(&pos_set).collect();
        let pos_minus_spo: HashSet<_> = pos_set.difference(&spo_set).collect();
        let spo_minus_osp: HashSet<_> = spo_set.difference(&osp_set).collect();

        if !spo_minus_pos.is_empty() || !pos_minus_spo.is_empty() {
            results.push(
                DiagnosticResult::new(
                    self.category(),
                    "SPO/POS Inconsistency",
                    Severity::Critical,
                )
                .with_description(format!(
                    "SPO and POS indexes differ: {} triples in SPO not in POS, {} triples in POS not in SPO",
                    spo_minus_pos.len(),
                    pos_minus_spo.len()
                ))
                .with_recommendation("Critical: Run database repair immediately")
                .with_detail("spo_exclusive", spo_minus_pos.len().to_string())
                .with_detail("pos_exclusive", pos_minus_spo.len().to_string()),
            );
        }

        if !spo_minus_osp.is_empty() {
            let osp_minus_spo: HashSet<_> = osp_set.difference(&spo_set).collect();
            results.push(
                DiagnosticResult::new(
                    self.category(),
                    "SPO/OSP Inconsistency",
                    Severity::Critical,
                )
                .with_description(format!(
                    "SPO and OSP indexes differ: {} triples in SPO not in OSP, {} triples in OSP not in SPO",
                    spo_minus_osp.len(),
                    osp_minus_spo.len()
                ))
                .with_recommendation("Critical: Run database repair immediately")
                .with_detail("spo_exclusive", spo_minus_osp.len().to_string())
                .with_detail("osp_exclusive", osp_minus_spo.len().to_string()),
            );
        }

        // If all checks pass
        if results.is_empty() {
            results.push(
                DiagnosticResult::new(self.category(), "Indexes Consistent", Severity::Info)
                    .with_description(format!(
                        "All three indexes (SPO, POS, OSP) are consistent with {} triples each",
                        spo_set.len()
                    ))
                    .with_detail("triple_count", spo_set.len().to_string()),
            );
        }

        Ok(results)
    }
}

/// Dictionary consistency diagnostic check.
///
/// Verifies that all `NodeId`s in indexes exist in the dictionary.
pub(crate) struct DictionaryConsistencyCheck;

impl DiagnosticCheck for DictionaryConsistencyCheck {
    fn name(&self) -> &str {
        "Dictionary Consistency"
    }

    fn category(&self) -> &str {
        "Integrity"
    }

    fn min_level(&self) -> DiagnosticLevel {
        DiagnosticLevel::Deep
    }

    fn run(&self, context: &DiagnosticContext) -> Result<Vec<DiagnosticResult>> {
        let mut results = Vec::new();

        // Check if we have the data needed
        if context.spo_triples.is_none() || context.dictionary_node_ids.is_none() {
            results.push(
                DiagnosticResult::new(self.category(), "Insufficient Data", Severity::Warning)
                    .with_description("Deep context required for dictionary consistency check")
                    .with_recommendation("Use DiagnosticContext::deep() for thorough checks"),
            );
            return Ok(results);
        }

        let spo_triples = context
            .spo_triples
            .as_ref()
            .expect("field validated to be Some above");
        let dictionary_ids = context
            .dictionary_node_ids
            .as_ref()
            .expect("field validated to be Some above");

        // Collect all node IDs from triples
        let mut index_node_ids = HashSet::new();
        for triple in spo_triples {
            index_node_ids.insert(triple.subject);
            index_node_ids.insert(triple.predicate);
            index_node_ids.insert(triple.object);
        }

        // Find missing node IDs
        let missing_ids: HashSet<_> = index_node_ids.difference(dictionary_ids).collect();

        if !missing_ids.is_empty() {
            results.push(
                DiagnosticResult::new(
                    self.category(),
                    "Missing Dictionary Entries",
                    Severity::Critical,
                )
                .with_description(format!(
                    "Found {} NodeIds in indexes that don't exist in dictionary",
                    missing_ids.len()
                ))
                .with_recommendation("Critical: Database corruption detected. Restore from backup.")
                .with_detail("missing_count", missing_ids.len().to_string())
                .with_detail("total_index_ids", index_node_ids.len().to_string())
                .with_detail("dictionary_size", dictionary_ids.len().to_string()),
            );
        } else {
            // Check for orphaned dictionary entries (exist in dictionary but not in indexes)
            let orphaned_ids: HashSet<_> = dictionary_ids.difference(&index_node_ids).collect();

            if orphaned_ids.len() > 100 {
                // Allow some orphaned entries, but warn if excessive
                results.push(
                    DiagnosticResult::new(
                        self.category(),
                        "Excessive Orphaned Entries",
                        Severity::Warning,
                    )
                    .with_description(format!(
                        "Found {} orphaned dictionary entries not referenced by any triple",
                        orphaned_ids.len()
                    ))
                    .with_recommendation(
                        "Consider running compaction to reclaim unused dictionary space",
                    )
                    .with_detail("orphaned_count", orphaned_ids.len().to_string()),
                );
            }

            results.push(
                DiagnosticResult::new(self.category(), "Dictionary Consistent", Severity::Info)
                    .with_description("All NodeIds in indexes exist in dictionary")
                    .with_detail("index_node_ids", index_node_ids.len().to_string())
                    .with_detail("dictionary_size", dictionary_ids.len().to_string()),
            );
        }

        Ok(results)
    }
}

/// WAL integrity diagnostic check.
///
/// Verifies Write-Ahead Log file integrity.
pub(crate) struct WalIntegrityCheck;

impl DiagnosticCheck for WalIntegrityCheck {
    fn name(&self) -> &str {
        "WAL Integrity"
    }

    fn category(&self) -> &str {
        "Integrity"
    }

    fn min_level(&self) -> DiagnosticLevel {
        DiagnosticLevel::Standard
    }

    fn run(&self, context: &DiagnosticContext) -> Result<Vec<DiagnosticResult>> {
        let mut results = Vec::new();

        // Check if we have the data directory
        let Some(data_dir) = &context.data_dir else {
            results.push(
                DiagnosticResult::new(self.category(), "Insufficient Data", Severity::Info)
                    .with_description("Data directory not provided for WAL integrity check"),
            );
            return Ok(results);
        };

        let wal_path = Path::new(data_dir).join("wal");

        // Check if WAL file exists
        if !wal_path.exists() {
            results.push(
                DiagnosticResult::new(self.category(), "WAL Not Found", Severity::Warning)
                    .with_description("Write-Ahead Log file not found")
                    .with_recommendation("WAL may not be initialized or may have been deleted")
                    .with_detail("wal_path", wal_path.display().to_string()),
            );
            return Ok(results);
        }

        // Check WAL file size
        match std::fs::metadata(&wal_path) {
            Ok(metadata) => {
                let size_mb = metadata.len() / (1024 * 1024);

                if size_mb > 1024 {
                    // WAL larger than 1GB
                    results.push(
                        DiagnosticResult::new(self.category(), "Large WAL File", Severity::Warning)
                            .with_description(format!("WAL file is {} MB", size_mb))
                            .with_recommendation("Consider checkpointing WAL to reduce size")
                            .with_detail("wal_size_mb", size_mb.to_string()),
                    );
                } else {
                    results.push(
                        DiagnosticResult::new(self.category(), "WAL Size Normal", Severity::Info)
                            .with_description(format!("WAL file size: {} MB", size_mb))
                            .with_detail("wal_size_mb", size_mb.to_string()),
                    );
                }

                // TODO: Add CRC checksum verification for WAL entries
                // This would require reading WAL entries and verifying checksums
            }
            Err(e) => {
                results.push(
                    DiagnosticResult::new(self.category(), "WAL Access Error", Severity::Error)
                        .with_description(format!("Failed to access WAL file: {}", e))
                        .with_recommendation("Check file permissions and disk health"),
                );
            }
        }

        Ok(results)
    }
}

/// Corruption detection check.
///
/// Performs various corruption detection checks.
pub(crate) struct CorruptionDetectionCheck;

impl DiagnosticCheck for CorruptionDetectionCheck {
    fn name(&self) -> &str {
        "Corruption Detection"
    }

    fn category(&self) -> &str {
        "Integrity"
    }

    fn min_level(&self) -> DiagnosticLevel {
        DiagnosticLevel::Deep
    }

    fn run(&self, context: &DiagnosticContext) -> Result<Vec<DiagnosticResult>> {
        let mut results = Vec::new();

        // Check for unreasonable triple count
        if context.triple_count > 0 && context.dictionary_size == 0 {
            results.push(
                DiagnosticResult::new(self.category(), "Suspicious State", Severity::Critical)
                    .with_description("Triples exist but dictionary is empty")
                    .with_recommendation("Critical: Possible corruption. Restore from backup."),
            );
        }

        // Check for unreasonable storage efficiency
        if let Some(bytes_per_triple) = context.storage_size_bytes.checked_div(context.triple_count)
        {
            // Triples should not be less than ~50 bytes or more than 10KB each
            if bytes_per_triple < 50 {
                results.push(
                    DiagnosticResult::new(
                        self.category(),
                        "Suspicious Storage Size",
                        Severity::Warning,
                    )
                    .with_description(format!(
                        "Storage size ({} bytes/triple) is unusually small",
                        bytes_per_triple
                    ))
                    .with_recommendation("Verify storage statistics are being collected correctly"),
                );
            } else if bytes_per_triple > 10_000 {
                results.push(
                    DiagnosticResult::new(
                        self.category(),
                        "Excessive Storage Overhead",
                        Severity::Warning,
                    )
                    .with_description(format!(
                        "Storage size ({} bytes/triple) is unusually large",
                        bytes_per_triple
                    ))
                    .with_recommendation(
                        "Possible fragmentation or corruption. Consider compaction.",
                    ),
                );
            }
        }

        // Check triple count vs dictionary size ratio
        if context.triple_count > 0 && context.dictionary_size > 0 {
            let ratio = context.dictionary_size as f64 / context.triple_count as f64;

            // Typically, dictionary should have 1-3x as many entries as triples
            if ratio > 10.0 {
                results.push(
                    DiagnosticResult::new(self.category(), "Dictionary Bloat", Severity::Warning)
                        .with_description(format!(
                            "Dictionary has {:.1}x more entries than triples",
                            ratio
                        ))
                        .with_recommendation(
                            "Consider compaction to remove unused dictionary entries",
                        )
                        .with_detail("ratio", format!("{:.2}", ratio)),
                );
            }
        }

        // If no issues found
        if results.is_empty() {
            results.push(
                DiagnosticResult::new(self.category(), "No Corruption Detected", Severity::Info)
                    .with_description("All corruption checks passed"),
            );
        }

        Ok(results)
    }
}