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
//! V2 WAL Checkpoint Error Handling
//!
//! This module provides comprehensive error handling and diagnostics for the checkpoint system,
//! including detailed error types, recovery strategies, and diagnostic information.

use crate::backend::native::NativeBackendError;
use std::fmt;
use std::io;

/// Enhanced checkpoint error with detailed diagnostic information
#[derive(Debug, Clone)]
pub struct CheckpointError {
    /// Error kind for categorization
    pub kind: CheckpointErrorKind,

    /// Human-readable error message
    pub message: String,

    /// Source error if applicable
    pub source: Option<String>,

    /// Error context and diagnostics
    pub context: ErrorContext,

    /// Recovery suggestions
    pub recovery: RecoverySuggestion,

    /// Error timestamp
    pub timestamp: std::time::SystemTime,
}

/// Categorized checkpoint error types
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CheckpointErrorKind {
    /// Configuration-related errors
    Configuration,

    /// File system I/O errors
    Io,

    /// V2 graph integration errors
    V2Integration,

    /// Checkpoint strategy errors
    Strategy,

    /// State management errors
    State,

    /// Validation errors
    Validation,

    /// Resource exhaustion errors
    Resource,

    /// Timeout errors
    Timeout,

    /// Concurrency errors
    Concurrency,

    /// Data corruption errors
    Corruption,

    /// Unknown/unexpected errors
    Unknown,
}

/// Error context information for debugging
#[derive(Debug, Clone, Default)]
pub struct ErrorContext {
    /// Checkpoint LSN range when error occurred
    pub lsn_range: Option<(u64, u64)>,

    /// Number of records processed when error occurred
    pub records_processed: Option<u64>,

    /// Number of dirty blocks when error occurred
    pub dirty_blocks: Option<u64>,

    /// Checkpoint file path
    pub checkpoint_path: Option<String>,

    /// WAL file path
    pub wal_path: Option<String>,

    /// Operation being performed
    pub operation: Option<String>,

    /// Additional context key-value pairs
    pub metadata: std::collections::HashMap<String, String>,
}

/// Recovery suggestions for different error types
#[derive(Debug, Clone)]
pub enum RecoverySuggestion {
    /// No recovery needed
    None,

    /// Retry the operation
    Retry { max_attempts: u32, backoff_ms: u64 },

    /// Restart checkpoint process
    Restart,

    /// Check disk space and permissions
    CheckDiskSpace,

    /// Validate V2 graph file integrity
    ValidateV2File,

    /// Reduce checkpoint batch size
    ReduceBatchSize,

    /// Increase timeout values
    IncreaseTimeout,

    /// Manual intervention required
    ManualIntervention(String),

    /// Custom recovery message
    Custom(String),
}

impl CheckpointError {
    /// Create a new checkpoint error
    pub fn new(kind: CheckpointErrorKind, message: impl Into<String>) -> Self {
        Self {
            kind,
            message: message.into(),
            source: None,
            context: ErrorContext::default(),
            recovery: RecoverySuggestion::None,
            timestamp: std::time::SystemTime::now(),
        }
    }

    /// Add context information to the error
    pub fn with_context(mut self, context: ErrorContext) -> Self {
        self.context = context;
        self
    }

    /// Add recovery suggestion to the error
    pub fn with_recovery(mut self, recovery: RecoverySuggestion) -> Self {
        self.recovery = recovery;
        self
    }

    /// Add source error information
    pub fn with_source(mut self, source: impl Into<String>) -> Self {
        self.source = Some(source.into());
        self
    }

    /// Create configuration error
    pub fn configuration(message: impl Into<String>) -> Self {
        Self::new(CheckpointErrorKind::Configuration, message)
            .with_recovery(RecoverySuggestion::CheckDiskSpace)
    }

    /// Create I/O error
    pub fn io(message: impl Into<String>) -> Self {
        Self::new(CheckpointErrorKind::Io, message).with_recovery(RecoverySuggestion::Retry {
            max_attempts: 3,
            backoff_ms: 100,
        })
    }

    /// Create V2 integration error
    pub fn v2_integration(message: impl Into<String>) -> Self {
        Self::new(CheckpointErrorKind::V2Integration, message)
            .with_recovery(RecoverySuggestion::ValidateV2File)
    }

    /// Create strategy error
    pub fn strategy(message: impl Into<String>) -> Self {
        Self::new(CheckpointErrorKind::Strategy, message).with_recovery(RecoverySuggestion::Custom(
            "Review checkpoint strategy configuration".to_string(),
        ))
    }

    /// Create state error
    pub fn state(message: impl Into<String>) -> Self {
        Self::new(CheckpointErrorKind::State, message).with_recovery(RecoverySuggestion::Restart)
    }

    /// Create validation error
    pub fn validation(message: impl Into<String>) -> Self {
        Self::new(CheckpointErrorKind::Validation, message)
            .with_recovery(RecoverySuggestion::ValidateV2File)
    }

    /// Create resource error
    pub fn resource(message: impl Into<String>) -> Self {
        Self::new(CheckpointErrorKind::Resource, message)
            .with_recovery(RecoverySuggestion::ReduceBatchSize)
    }

    /// Create timeout error
    pub fn timeout(message: impl Into<String>) -> Self {
        Self::new(CheckpointErrorKind::Timeout, message)
            .with_recovery(RecoverySuggestion::IncreaseTimeout)
    }

    /// Create corruption error
    pub fn corruption(message: impl Into<String>) -> Self {
        Self::new(CheckpointErrorKind::Corruption, message).with_recovery(
            RecoverySuggestion::ManualIntervention("Data corruption detected".to_string()),
        )
    }

    /// Create unknown error
    pub fn unknown(message: impl Into<String>) -> Self {
        Self::new(CheckpointErrorKind::Unknown, message)
    }

    /// Create checkpoint required error (for overflow handling)
    pub fn checkpoint_required(message: impl Into<String>) -> Self {
        Self::new(CheckpointErrorKind::Resource, message).with_recovery(RecoverySuggestion::Custom(
            "Checkpoint required to free dirty blocks".to_string(),
        ))
    }

    /// Get error severity level
    pub fn severity(&self) -> ErrorSeverity {
        match self.kind {
            CheckpointErrorKind::Configuration | CheckpointErrorKind::Strategy => {
                ErrorSeverity::Warning
            }
            CheckpointErrorKind::Io
            | CheckpointErrorKind::Resource
            | CheckpointErrorKind::Timeout => ErrorSeverity::Error,
            CheckpointErrorKind::State | CheckpointErrorKind::Validation => ErrorSeverity::Error,
            CheckpointErrorKind::V2Integration | CheckpointErrorKind::Corruption => {
                ErrorSeverity::Critical
            }
            CheckpointErrorKind::Concurrency | CheckpointErrorKind::Unknown => ErrorSeverity::Error,
        }
    }

    /// Check if error is recoverable
    pub fn is_recoverable(&self) -> bool {
        !matches!(self.kind, CheckpointErrorKind::Corruption)
    }

    /// Get suggested retry delay in milliseconds
    pub fn retry_delay_ms(&self) -> Option<u64> {
        match &self.recovery {
            RecoverySuggestion::Retry { backoff_ms, .. } => Some(*backoff_ms),
            RecoverySuggestion::IncreaseTimeout => Some(5000), // 5 seconds
            _ => None,
        }
    }

    /// Generate diagnostic report
    pub fn diagnostic_report(&self) -> String {
        let mut report = format!("Checkpoint Error Report\n");
        report.push_str(&format!("=====================\n"));
        report.push_str(&format!("Error Kind: {:?}\n", self.kind));
        report.push_str(&format!("Severity: {:?}\n", self.severity()));
        report.push_str(&format!("Message: {}\n", self.message));

        if let Some(source) = &self.source {
            report.push_str(&format!("Source: {}\n", source));
        }

        report.push_str(&format!("Timestamp: {:?}\n", self.timestamp));
        report.push_str(&format!("Recoverable: {}\n", self.is_recoverable()));

        if let Some(lsn_range) = self.context.lsn_range {
            report.push_str(&format!("LSN Range: {}-{}\n", lsn_range.0, lsn_range.1));
        }

        if let Some(records) = self.context.records_processed {
            report.push_str(&format!("Records Processed: {}\n", records));
        }

        if let Some(blocks) = self.context.dirty_blocks {
            report.push_str(&format!("Dirty Blocks: {}\n", blocks));
        }

        if let Some(operation) = &self.context.operation {
            report.push_str(&format!("Operation: {}\n", operation));
        }

        report.push_str(&format!("Recovery: {:?}\n", self.recovery));

        if !self.context.metadata.is_empty() {
            report.push_str("Additional Context:\n");
            for (key, value) in &self.context.metadata {
                report.push_str(&format!("  {}: {}\n", key, value));
            }
        }

        report
    }
}

impl fmt::Display for CheckpointError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{:?}] {}", self.kind, self.message)
    }
}

impl std::error::Error for CheckpointError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None // We store source as String for serialization compatibility
    }
}

/// Error severity levels
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum ErrorSeverity {
    Warning,
    Error,
    Critical,
}

/// Result type for checkpoint operations
pub type CheckpointResult<T> = Result<T, CheckpointError>;

/// Convert from NativeBackendError
impl From<NativeBackendError> for CheckpointError {
    fn from(error: NativeBackendError) -> Self {
        let message = error.to_string();

        match error {
            NativeBackendError::Io(_) => {
                Self::io(&message).with_source(format!("NativeBackendError: {}", message))
            }
            NativeBackendError::InvalidHeader { field, reason, .. } => Self::configuration(message)
                .with_source(format!(
                    "NativeBackendError: Invalid header {}: {}",
                    field, reason
                )),
            NativeBackendError::CorruptNodeRecord {
                node_id, reason, ..
            } => Self::corruption(message).with_source(format!(
                "NativeBackendError: Corrupt node {}: {}",
                node_id, reason
            )),
            NativeBackendError::CorruptEdgeRecord {
                edge_id, reason, ..
            } => Self::corruption(message).with_source(format!(
                "NativeBackendError: Corrupt edge {}: {}",
                edge_id, reason
            )),
            NativeBackendError::InvalidMagic {
                expected, found, ..
            } => Self::corruption(message).with_source(format!(
                "NativeBackendError: Invalid magic expected {:x}, found {:x}",
                expected, found
            )),
            NativeBackendError::ValidationFailed {
                metric,
                expected,
                actual,
                ..
            } => Self::validation(message).with_source(format!(
                "NativeBackendError: Validation failed for {}: expected {}, found {}",
                metric, expected, actual
            )),
            NativeBackendError::InvalidParameter { context, .. } => {
                Self::configuration(message).with_source(format!("NativeBackendError: {}", context))
            }
            NativeBackendError::InvalidState { context, .. } => {
                Self::state(message).with_source(format!("NativeBackendError: {}", context))
            }
            NativeBackendError::CorruptionDetected { context, .. } => {
                Self::corruption(message).with_source(format!("NativeBackendError: {}", context))
            }
            _ => Self::unknown(message).with_source("NativeBackendError".to_string()),
        }
    }
}

/// Convert from std::io::Error
impl From<io::Error> for CheckpointError {
    fn from(error: io::Error) -> Self {
        let message = error.to_string();
        let kind = error.kind();

        let mut checkpoint_error = match kind {
            io::ErrorKind::NotFound => Self::io(format!("File not found: {}", message)),
            io::ErrorKind::PermissionDenied => Self::io(format!("Permission denied: {}", message)),
            io::ErrorKind::AlreadyExists => Self::io(format!("File already exists: {}", message)),
            io::ErrorKind::InvalidInput => {
                Self::configuration(format!("Invalid input: {}", message))
            }
            io::ErrorKind::InvalidData => Self::corruption(format!("Invalid data: {}", message)),
            io::ErrorKind::TimedOut => Self::timeout(format!("Operation timed out: {}", message)),
            io::ErrorKind::WriteZero => Self::io(format!("Write zero bytes: {}", message)),
            io::ErrorKind::Interrupted => Self::io(format!("Operation interrupted: {}", message)),
            io::ErrorKind::UnexpectedEof => {
                Self::corruption(format!("Unexpected EOF: {}", message))
            }
            _ => Self::io(format!("I/O error: {}", message)),
        };

        checkpoint_error.source = Some(format!("io::Error kind: {:?}", kind));
        checkpoint_error
    }
}

/// Create unknown error from any error type
impl From<Box<dyn std::error::Error + Send + Sync>> for CheckpointError {
    fn from(error: Box<dyn std::error::Error + Send + Sync>) -> Self {
        Self::unknown(error.to_string()).with_source("Unknown error source".to_string())
    }
}

/// Error collection for multiple checkpoint errors
#[derive(Debug, Clone)]
pub struct CheckpointErrorCollection {
    /// Collection of errors
    pub errors: Vec<CheckpointError>,

    /// Collection timestamp
    pub timestamp: std::time::SystemTime,
}

impl CheckpointErrorCollection {
    /// Create new error collection
    pub fn new() -> Self {
        Self {
            errors: Vec::new(),
            timestamp: std::time::SystemTime::now(),
        }
    }

    /// Add error to collection
    pub fn add_error(&mut self, error: CheckpointError) {
        self.errors.push(error);
    }

    /// Add multiple errors to collection
    pub fn add_errors<I>(&mut self, errors: I)
    where
        I: IntoIterator<Item = CheckpointError>,
    {
        for error in errors {
            self.add_error(error);
        }
    }

    /// Check if collection has any errors
    pub fn has_errors(&self) -> bool {
        !self.errors.is_empty()
    }

    /// Get highest severity error
    pub fn highest_severity(&self) -> Option<ErrorSeverity> {
        self.errors.iter().map(|error| error.severity()).max()
    }

    /// Get number of errors by severity
    pub fn count_by_severity(&self) -> (usize, usize, usize) {
        let (mut warning, mut error_count, mut critical) = (0, 0, 0);

        for error in &self.errors {
            match error.severity() {
                ErrorSeverity::Warning => warning += 1,
                ErrorSeverity::Error => error_count += 1,
                ErrorSeverity::Critical => critical += 1,
            }
        }

        (warning, error_count, critical)
    }

    /// Check if any errors are unrecoverable
    pub fn has_unrecoverable_errors(&self) -> bool {
        self.errors.iter().any(|error| !error.is_recoverable())
    }

    /// Generate summary report
    pub fn summary_report(&self) -> String {
        if !self.has_errors() {
            return "No checkpoint errors".to_string();
        }

        let (warning, error, critical) = self.count_by_severity();
        let total = self.errors.len();

        format!(
            "Checkpoint Error Summary\n========================\n\
             Total Errors: {}\n\
             Warnings: {}\n\
             Errors: {}\n\
             Critical: {}\n\
             Recoverable: {}\n\
             Timestamp: {:?}",
            total,
            warning,
            error,
            critical,
            total - self.errors.iter().filter(|e| !e.is_recoverable()).count(),
            self.timestamp
        )
    }

    /// Generate detailed report
    pub fn detailed_report(&self) -> String {
        if !self.has_errors() {
            return "No checkpoint errors".to_string();
        }

        let mut report = self.summary_report();
        report.push_str("\n\nDetailed Errors:\n");

        for (i, error) in self.errors.iter().enumerate() {
            report.push_str(&format!("\n{}. {}\n", i + 1, error));
            if let Some(retry_delay) = error.retry_delay_ms() {
                report.push_str(&format!("   Retry delay: {}ms\n", retry_delay));
            }
        }

        report
    }
}

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

/// Macro for creating checkpoint errors with context
#[macro_export]
macro_rules! checkpoint_error {
    ($kind:expr, $message:expr) => {
        $crate::backend::native::v2::wal::checkpoint::errors::CheckpointError::new($kind, $message)
    };
    ($kind:expr, $message:expr, context: $context:expr) => {
        $crate::backend::native::v2::wal::checkpoint::errors::CheckpointError::new($kind, $message)
            .with_context($context)
    };
    ($kind:expr, $message:expr, context: $context:expr, recovery: $recovery:expr) => {
        $crate::backend::native::v2::wal::checkpoint::errors::CheckpointError::new($kind, $message)
            .with_context($context)
            .with_recovery($recovery)
    };
}

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

    #[test]
    fn test_checkpoint_error_creation() {
        let error = CheckpointError::configuration("Invalid path");
        assert_eq!(error.kind, CheckpointErrorKind::Configuration);
        assert_eq!(error.message, "Invalid path");
        assert_eq!(error.severity(), ErrorSeverity::Warning);
        assert!(error.is_recoverable());
    }

    #[test]
    fn test_checkpoint_error_with_context() {
        let context = ErrorContext {
            lsn_range: Some((1000, 2000)),
            records_processed: Some(500),
            ..Default::default()
        };

        let error = CheckpointError::io("File write failed").with_context(context);

        assert_eq!(error.context.lsn_range, Some((1000, 2000)));
        assert_eq!(error.context.records_processed, Some(500));
    }

    #[test]
    fn test_checkpoint_error_recovery() {
        let error = CheckpointError::timeout("Operation timed out")
            .with_recovery(RecoverySuggestion::IncreaseTimeout);

        assert!(matches!(
            error.recovery,
            RecoverySuggestion::IncreaseTimeout
        ));
        assert_eq!(error.retry_delay_ms(), Some(5000));
    }

    #[test]
    fn test_checkpoint_error_from_native() {
        let native_error =
            NativeBackendError::Io(std::io::Error::new(std::io::ErrorKind::StorageFull, "test"));

        let checkpoint_error: CheckpointError = native_error.into();
        assert_eq!(checkpoint_error.kind, CheckpointErrorKind::Io);
        assert!(checkpoint_error.message.contains("I/O error"));
    }

    #[test]
    fn test_error_collection() {
        let mut collection = CheckpointErrorCollection::new();

        collection.add_error(CheckpointError::configuration("Bad config"));
        collection.add_error(CheckpointError::io("Disk error"));
        collection.add_error(CheckpointError::corruption("Data corrupted"));

        assert!(collection.has_errors());
        assert!(collection.has_unrecoverable_errors());
        assert_eq!(collection.errors.len(), 3);

        let (warning, error, critical) = collection.count_by_severity();
        assert_eq!(warning, 1);
        assert_eq!(error, 1);
        assert_eq!(critical, 1);
    }

    #[test]
    fn test_error_severity_levels() {
        let config_error = CheckpointError::configuration("test");
        let io_error = CheckpointError::io("test");
        let corruption_error = CheckpointError::corruption("test");

        assert_eq!(config_error.severity(), ErrorSeverity::Warning);
        assert_eq!(io_error.severity(), ErrorSeverity::Error);
        assert_eq!(corruption_error.severity(), ErrorSeverity::Critical);

        assert!(config_error.severity() < io_error.severity());
        assert!(io_error.severity() < corruption_error.severity());
    }

    #[test]
    fn test_diagnostic_report() {
        let context = ErrorContext {
            lsn_range: Some((1000, 2000)),
            records_processed: Some(500),
            operation: Some("test_operation".to_string()),
            ..Default::default()
        };

        let error = CheckpointError::io("Test error")
            .with_context(context)
            .with_recovery(RecoverySuggestion::Retry {
                max_attempts: 3,
                backoff_ms: 100,
            });

        let report = error.diagnostic_report();
        assert!(report.contains("Test error"));
        assert!(report.contains("1000-2000"));
        assert!(report.contains("Records Processed: 500"));
        assert!(report.contains("test_operation"));
    }

    #[test]
    fn test_macro_usage() {
        let error = checkpoint_error!(
            CheckpointErrorKind::Io,
            "Test error",
            context: ErrorContext {
                operation: Some("test".to_string()),
                ..Default::default()
            },
            recovery: RecoverySuggestion::Retry { max_attempts: 1, backoff_ms: 50 }
        );

        assert_eq!(error.kind, CheckpointErrorKind::Io);
        assert_eq!(error.message, "Test error");
        assert_eq!(error.context.operation, Some("test".to_string()));
    }
}