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
//! V2 Snapshot Export for SQLiteGraph
//!
//! This module implements instant snapshot export functionality that bypasses WAL complexity
//! and provides atomic database state snapshots through direct file operations.
//!
//! ## Architecture
//!
//! Snapshot export diverges from WAL-based exports by:
//! 1. NO WAL reads or writes
//! 2. Direct atomic graph file copy
//! 3. Strict invariant validation before export
//! 4. Immediate consistency checks
//! 5. No recovery orchestration required

use super::{ExportManifest, ManifestSerializer};
use crate::backend::native::v2::snapshot::AtomicFileOperations;
use crate::backend::native::{
    graph_file::GraphFile,
    types::{NativeBackendError, NativeResult},
    v2::wal::recovery::states::{Authority, RecoveryState as ExplicitRecoveryState},
};
use std::fs;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

/// Configuration for snapshot export operations
#[derive(Debug, Clone)]
pub struct SnapshotExportConfig {
    /// Export directory path
    pub export_path: PathBuf,

    /// Snapshot identifier (human-readable)
    pub snapshot_id: String,

    /// Whether to include database statistics in manifest
    pub include_statistics: bool,

    /// Minimum stable state duration before allowing snapshot
    pub min_stable_duration: Duration,

    /// Whether to perform checksum validation
    pub checksum_validation: bool,
}

impl Default for SnapshotExportConfig {
    fn default() -> Self {
        Self {
            export_path: PathBuf::from("snapshot_export"),
            snapshot_id: format!(
                "snapshot_{}",
                SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs()
            ),
            include_statistics: true,
            min_stable_duration: Duration::from_secs(0),
            checksum_validation: true,
        }
    }
}

/// Snapshot validation report
#[derive(Debug, Clone)]
pub struct SnapshotValidationReport {
    /// Whether graph is in stable state
    pub is_stable: bool,

    /// Whether WAL is clean (no active WAL files)
    pub wal_clean: bool,

    /// Whether file consistency checks pass
    pub file_consistent: bool,

    /// Whether commit marker is valid
    pub commit_marker_valid: bool,

    /// Validation errors encountered
    pub errors: Vec<String>,

    /// Validation warnings encountered
    pub warnings: Vec<String>,
}

/// Snapshot export result
#[derive(Debug, Clone)]
pub struct SnapshotExportResult {
    /// Path to exported snapshot file
    pub snapshot_path: PathBuf,

    /// Path to exported manifest file
    pub manifest_path: PathBuf,

    /// Export duration
    pub export_duration: Duration,

    /// Snapshot size in bytes
    pub snapshot_size_bytes: u64,

    /// Export checksum
    pub checksum: u64,

    /// Number of records in snapshot
    pub record_count: u64,

    /// Export timestamp
    pub export_timestamp: u64,
}

/// Snapshot exporter for instant database state exports
pub struct SnapshotExporter {
    /// Graph file being exported
    graph_file: GraphFile,

    /// Export configuration
    config: SnapshotExportConfig,

    /// Source graph file path
    source_path: PathBuf,
}

impl SnapshotExporter {
    /// Create a new snapshot exporter
    pub fn new(graph_path: &Path, config: SnapshotExportConfig) -> NativeResult<Self> {
        // Validate graph file exists and is accessible
        if !graph_path.exists() {
            return Err(NativeBackendError::InvalidParameter {
                context: format!("Graph file does not exist: {:?}", graph_path),
                source: None,
            });
        }

        // Open graph file for validation
        let graph_file = GraphFile::open(graph_path)?;

        // Perform initial invariant validation
        Self::validate_initial_invariants(&graph_file, graph_path)?;

        Ok(Self {
            graph_file,
            config,
            source_path: graph_path.to_path_buf(),
        })
    }

    /// Export snapshot with atomic operations
    pub fn export_snapshot(&mut self) -> NativeResult<SnapshotExportResult> {
        let start_time = SystemTime::now();

        // Step 1: Final invariant validation before export
        let validation_report = self.validate_snapshot_conditions()?;
        if !validation_report.is_stable {
            return Err(NativeBackendError::InvalidState {
                context: "Graph is not in stable state for snapshot export".to_string(),
                source: None,
            });
        }

        // Step 2: Ensure export directory exists
        fs::create_dir_all(&self.config.export_path).map_err(|e| NativeBackendError::Io(e))?;

        // Step 3: Generate snapshot paths
        let snapshot_filename = format!("{}.v2", self.config.snapshot_id);
        let snapshot_path = self.config.export_path.join(snapshot_filename);
        let manifest_path = self.config.export_path.join("export.manifest");

        // Step 4: Perform atomic graph file copy using proper AtomicFileOperations
        let atomic_ops = AtomicFileOperations::new();
        atomic_ops.atomic_copy_file(&self.source_path, &snapshot_path)?;

        // Step 5: Calculate snapshot metadata
        let snapshot_size = fs::metadata(&snapshot_path)
            .map_err(|e| NativeBackendError::Io(e))?
            .len();

        let checksum = if self.config.checksum_validation {
            self.calculate_snapshot_checksum(&snapshot_path)?
        } else {
            0
        };

        // Step 6: Generate export manifest
        let manifest = self.generate_export_manifest(snapshot_size, checksum, SystemTime::now())?;

        // Step 7: Write manifest file
        ManifestSerializer::write_to_file(&manifest, &manifest_path)?;

        // Step 8: Final sync to ensure all files are durable
        self.sync_export_files(&snapshot_path, &manifest_path)?;

        let export_duration = start_time.elapsed().unwrap_or_default();

        Ok(SnapshotExportResult {
            snapshot_path,
            manifest_path,
            export_duration,
            snapshot_size_bytes: snapshot_size,
            checksum,
            record_count: self.graph_file.persistent_header().node_count as u64
                + self.graph_file.persistent_header().edge_count as u64,
            export_timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
        })
    }

    /// Validate snapshot preconditions
    pub fn validate_snapshot_conditions(&mut self) -> NativeResult<SnapshotValidationReport> {
        let mut report = SnapshotValidationReport {
            is_stable: true,
            wal_clean: true,
            file_consistent: true,
            commit_marker_valid: true,
            errors: Vec::new(),
            warnings: Vec::new(),
        };

        // Check for active transactions
        if self.graph_file.is_transaction_active() {
            report.is_stable = false;
            report
                .errors
                .push("Active transaction detected - snapshot requires stable state".to_string());
        }

        // Check WAL directory is clean
        let wal_path = self.source_path.with_extension("wal");
        if wal_path.exists() {
            let wal_size = fs::metadata(&wal_path)
                .map_err(|e| NativeBackendError::Io(e))?
                .len();

            if wal_size > 0 {
                report.wal_clean = false;
                report.warnings.push(format!(
                    "WAL file exists with size {} bytes - snapshot may not represent clean state",
                    wal_size
                ));
            }
        }

        // Validate file consistency
        match self.graph_file.validate_file_size() {
            Ok(_) => {}
            Err(e) => {
                report.file_consistent = false;
                report
                    .errors
                    .push(format!("File size validation failed: {}", e));
            }
        }

        // Validate commit marker
        match self.graph_file.verify_commit_marker() {
            Ok(_) => {}
            Err(e) => {
                report.commit_marker_valid = false;
                report
                    .errors
                    .push(format!("Commit marker validation failed: {}", e));
            }
        }

        // Additional header consistency checks
        let header = self.graph_file.persistent_header();
        #[allow(clippy::absurd_extreme_comparisons)]
        if header.node_count < 0 || header.edge_count < 0 {
            report.file_consistent = false;
            report
                .errors
                .push("Negative node or edge counts in header".to_string());
        }

        if header.node_count > 1_000_000 || header.edge_count > 10_000_000 {
            report
                .warnings
                .push("Large node/edge counts detected - verify export size".to_string());
        }

        Ok(report)
    }

    /// Perform initial invariants validation
    fn validate_initial_invariants(graph_file: &GraphFile, graph_path: &Path) -> NativeResult<()> {
        // Validate magic bytes using the proper V2 constants
        let header = graph_file.persistent_header();
        let expected_magic = crate::backend::native::constants::MAGIC_BYTES;
        if header.magic != expected_magic {
            return Err(NativeBackendError::InvalidMagicBytes {
                found: header.magic,
            });
        }

        // Validate V2 format (versions 2 and 3 are supported)
        if header.version != 2 && header.version != 3 {
            return Err(NativeBackendError::UnsupportedVersion {
                version: header.version,
                supported_version: 2, // Report 2 as base supported version
            });
        }

        // Validate file meets minimum V2 header size
        let metadata = fs::metadata(graph_path).map_err(|e| NativeBackendError::Io(e))?;
        let min_size = crate::backend::native::constants::HEADER_SIZE;
        if metadata.len() < min_size {
            return Err(NativeBackendError::FileTooSmall {
                size: metadata.len(),
                min_size,
            });
        }

        Ok(())
    }

    /// Calculate checksum of snapshot file
    fn calculate_snapshot_checksum(&self, snapshot_path: &Path) -> NativeResult<u64> {
        use std::io::Read;

        let mut file = fs::File::open(snapshot_path).map_err(|e| NativeBackendError::Io(e))?;

        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        let mut buffer = [0u8; 8192];

        loop {
            let bytes_read = file
                .read(&mut buffer)
                .map_err(|e| NativeBackendError::Io(e))?;

            if bytes_read == 0 {
                break;
            }

            std::hash::Hasher::write(&mut hasher, &buffer[..bytes_read]);
        }

        Ok(std::hash::Hasher::finish(&hasher))
    }

    /// Generate export manifest for snapshot
    fn generate_export_manifest(
        &self,
        snapshot_size: u64,
        checksum: u64,
        export_time: SystemTime,
    ) -> NativeResult<ExportManifest> {
        let header = self.graph_file.persistent_header();

        let mut manifest = ExportManifest::new();
        manifest.recovery_state = ExplicitRecoveryState::CleanShutdown;
        manifest.authority = Authority::GraphFile; // Snapshots use GraphFile authority
        manifest.export_mode = super::ExportMode::Snapshot;
        manifest.graph_checkpoint_lsn = 0; // No LSN for snapshots
        manifest.wal_start_lsn = None;
        manifest.wal_end_lsn = None;
        manifest.graph_format_version = header.version;
        manifest.wal_format_version = 1; // Default for snapshots
        manifest.v2_clustered_edges = true; // V2 format always uses clustered edges
        manifest.export_timestamp = export_time
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        manifest.export_duration_ms = 0; // Will be updated after export completes
        manifest.graph_checksum = checksum;
        manifest.wal_checksum = None; // No WAL for snapshots
        manifest.total_records = header.node_count as u64 + header.edge_count as u64;
        manifest.total_bytes = snapshot_size;

        Ok(manifest)
    }

    /// Sync exported files to ensure durability
    fn sync_export_files(&self, snapshot_path: &Path, manifest_path: &Path) -> NativeResult<()> {
        // Sync snapshot file
        {
            let file = fs::OpenOptions::new()
                .write(true)
                .open(snapshot_path)
                .map_err(|e| NativeBackendError::Io(e))?;
            file.sync_all().map_err(|e| NativeBackendError::Io(e))?;
        }

        // Sync manifest file
        {
            let file = fs::OpenOptions::new()
                .write(true)
                .open(manifest_path)
                .map_err(|e| NativeBackendError::Io(e))?;
            file.sync_all().map_err(|e| NativeBackendError::Io(e))?;
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::{NamedTempFile, TempDir};

    fn create_test_graph_file() -> NativeResult<(GraphFile, PathBuf)> {
        let temp_file = NamedTempFile::new().map_err(|e| NativeBackendError::Io(e))?;
        let path = temp_file.path().to_path_buf();

        // Keep temp file from being deleted
        let _ = temp_file.into_temp_path().keep().unwrap();

        let graph_file = GraphFile::create(&path)?;
        Ok((graph_file, path))
    }

    #[test]
    fn test_snapshot_exporter_creation() {
        let (graph_file, graph_path) =
            create_test_graph_file().expect("Failed to create test graph");

        let config = SnapshotExportConfig::default();
        let result = SnapshotExporter::new(&graph_path, config);

        match result {
            Ok(_) => println!("Snapshot exporter created successfully"),
            Err(e) => {
                println!("Snapshot exporter creation failed: {:?}", e);
                panic!(
                    "Snapshot exporter creation should succeed, but got error: {:?}",
                    e
                );
            }
        }
    }

    #[test]
    fn test_snapshot_exporter_fails_with_missing_file() {
        let missing_path = PathBuf::from("/nonexistent/graph.v2");
        let config = SnapshotExportConfig::default();

        let result = SnapshotExporter::new(&missing_path, config);

        assert!(
            result.is_err(),
            "Snapshot exporter creation should fail with missing file"
        );
    }
}