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
//! TDD Tests for V2 Export/Import System
//!
//! This test module contains comprehensive TDD tests that validate the export/import
//! system functionality. All tests are designed to FAIL initially until the corresponding
//! functionality is implemented, following strict TDD methodology.
//!
//! ## Test Coverage
//!
//! 1. Export after clean checkpoint
//! 2. Export with active WAL tail
//! 3. Import into empty graph
//! 4. Import followed by recovery validation
//! 5. Import failure on incompatible manifest
//! 6. End-to-end export/import roundtrip

use sqlitegraph::backend::native::graph_file::GraphFile;
use sqlitegraph::backend::native::v2::Direction;
use sqlitegraph::backend::native::v2::export::{
    ExportConsistencyReport, ExportFactory, ExportManifest, ExportResult, V2ExportConfig,
    V2Exporter, manifest::ManifestSerializer,
};
use sqlitegraph::backend::native::v2::import::{
    ImportFactory, ImportMode, ImportResult, ImportValidationReport, V2ImportConfig, V2Importer,
};
use sqlitegraph::backend::native::v2::wal::recovery::states::{
    Authority, RecoveryContext, RecoveryState as ExplicitRecoveryState,
};
use sqlitegraph::backend::native::v2::wal::{
    BulkIngestConfig, BulkIngestExt, IsolationLevel, V2WALConfig, V2WALManager, V2WALReader,
    V2WALRecord,
};
use sqlitegraph::backend::native::{NativeBackendError, NativeResult};
use tempfile::tempdir;

/// Test 1: Export after clean checkpoint - no WAL tail needed
#[test]
fn test_export_clean_checkpoint_no_wal() -> NativeResult<()> {
    let temp_dir = tempdir()?;
    let graph_path = temp_dir.path().join("test.v2");
    let export_dir = temp_dir.path().join("export");

    // Create a clean graph file
    let _graph_file = GraphFile::create(&graph_path)?;

    // Create WAL and perform clean operations
    let wal_path = graph_path.with_extension("wal");
    let checkpoint_path = graph_path.with_extension("checkpoint");
    let wal_config = V2WALConfig {
        wal_path: wal_path.clone(),
        checkpoint_path: checkpoint_path.clone(),
        ..Default::default()
    };

    // Create WAL manager and write some data
    let manager = V2WALManager::create(wal_config)?;

    // Write and commit a clean transaction
    let tx_id = manager.begin_transaction(IsolationLevel::ReadCommitted)?;
    manager.write_transaction_record(
        tx_id,
        V2WALRecord::NodeInsert {
            node_id: 1,
            slot_offset: 0,
            node_data: vec![1, 2, 3, 4, 5],
        },
    )?;
    manager.commit_transaction(tx_id)?;

    // Force checkpoint to ensure clean state
    manager.force_checkpoint()?;

    // Create export configuration for checkpoint-aligned export
    let exporter = ExportFactory::create_checkpoint_aligned_exporter(&graph_path, &export_dir)?;

    // This should fail initially until export system is implemented
    let result = exporter.export_checkpoint_aligned();
    assert!(
        result.is_err(),
        "Should fail until export_checkpoint_aligned is implemented"
    );

    // Verify export files were created (if they were)
    let manifest_path = export_dir.join("export.manifest");
    let graph_file_path = export_dir.join("export.graph");
    let wal_file_path = export_dir.join("export.wal");

    // For now, we expect the operation to fail, so files may not exist
    // When implemented, these should exist and be valid

    Ok(())
}

/// Test 2: Export with active WAL tail - requires WAL records
#[test]
fn test_export_active_wal_tail() -> NativeResult<()> {
    let temp_dir = tempdir()?;
    let graph_path = temp_dir.path().join("test.v2");
    let export_dir = temp_dir.path().join("export");

    // Create a clean graph file
    let _graph_file = GraphFile::create(&graph_path)?;

    // Create WAL system
    let wal_path = graph_path.with_extension("wal");
    let checkpoint_path = graph_path.with_extension("checkpoint");
    let wal_config = V2WALConfig {
        wal_path: wal_path.clone(),
        checkpoint_path: checkpoint_path.clone(),
        ..Default::default()
    };

    let manager = V2WALManager::create(wal_config)?;

    // Write transaction but DO NOT commit (simulates active state)
    let tx_id = manager.begin_transaction(IsolationLevel::ReadCommitted)?;
    manager.write_transaction_record(
        tx_id,
        V2WALRecord::NodeInsert {
            node_id: 2,
            slot_offset: 0,
            node_data: vec![6, 7, 8, 9, 10],
        },
    )?;

    // Simulate crash by dropping manager without commit
    drop(manager);

    // Create export configuration for full export (includes WAL tail)
    let exporter = ExportFactory::create_full_exporter(&graph_path, &export_dir)?;

    // This should fail initially until export system is implemented
    let result = exporter.export_full();
    assert!(
        result.is_err(),
        "Should fail until export_full is implemented"
    );

    Ok(())
}

/// Test 3: Import into empty graph
#[test]
fn test_import_into_empty_graph() -> NativeResult<()> {
    let temp_dir = tempdir()?;
    let export_dir = temp_dir.path().join("export");
    let target_path = temp_dir.path().join("imported.v2");

    // Create mock export directory structure with all required files
    std::fs::create_dir_all(&export_dir)?;

    // Create mock manifest file with basic structure
    let manifest = ExportManifest::new();
    let manifest_bytes = format!(
        "V2EXPORT_MANIFEST\nversion=1\nrecovery_state=CleanShutdown\ngraph_checkpoint_lsn=50\nexport_mode=CheckpointAligned"
    );
    let manifest_path = export_dir.join("export.manifest");
    std::fs::write(&manifest_path, manifest_bytes)?;

    // Create mock graph file
    let graph_file_path = export_dir.join("export.graph");
    std::fs::write(&graph_file_path, b"mock exported graph data")?;

    // Create mock WAL file (optional for checkpoint-aligned export)
    let wal_file_path = export_dir.join("export.wal");
    std::fs::write(&wal_file_path, b"mock exported wal data")?;

    // Configure fresh import
    let config = V2ImportConfig {
        target_graph_path: target_path.clone(),
        export_dir_path: export_dir.clone(),
        import_mode: ImportMode::Fresh,
        validate_recovery: true,
        force_checkpoint_after_import: true,
    };

    // Try to create importer - should fail initially
    let importer_result = V2Importer::from_export_dir(&export_dir, &target_path, config);
    assert!(
        importer_result.is_err(),
        "Should fail until V2Importer::from_export_dir is implemented"
    );

    // Even if importer creation succeeded, validation should fail
    if let Ok(importer) = importer_result {
        let validation_result = importer.validate_export();
        assert!(
            validation_result.is_err(),
            "Should fail until validate_export is implemented"
        );

        // Even if validation succeeded, import should fail
        if let Ok(_validation) = validation_result {
            let import_result = importer.import();
            assert!(
                import_result.is_err(),
                "Should fail until import is implemented"
            );
        }
    }

    Ok(())
}

/// Test 4: Import followed by recovery validation
#[test]
fn test_import_recovery_validation() -> NativeResult<()> {
    let temp_dir = tempdir()?;
    let export_dir = temp_dir.path().join("export");
    let target_path = temp_dir.path().join("imported.v2");

    // Create comprehensive mock export
    std::fs::create_dir_all(&export_dir)?;

    // Create mock manifest
    let manifest_path = export_dir.join("export.manifest");
    let manifest_bytes = format!(
        "V2EXPORT_MANIFEST\nversion=1\nrecovery_state=DirtyShutdown\ngraph_checkpoint_lsn=100\nwal_start_lsn=101\nwal_end_lsn=150\nexport_mode=Full"
    );
    std::fs::write(&manifest_path, manifest_bytes)?;

    // Create mock export files
    let graph_file_path = export_dir.join("export.graph");
    let wal_file_path = export_dir.join("export.wal");
    std::fs::write(&graph_file_path, b"mock exported graph data")?;
    std::fs::write(&wal_file_path, b"mock exported wal records")?;

    // Configure import with recovery validation enabled
    let config = V2ImportConfig {
        target_graph_path: target_path.clone(),
        export_dir_path: export_dir.clone(),
        import_mode: ImportMode::Fresh,
        validate_recovery: true,
        force_checkpoint_after_import: true,
    };

    // Try to create importer - should fail initially
    let importer_result = V2Importer::from_export_dir(&export_dir, &target_path, config);
    assert!(
        importer_result.is_err(),
        "Should fail until V2Importer is implemented"
    );

    Ok(())
}

/// Test 5: Import failure on incompatible manifest
#[test]
fn test_import_incompatible_manifest() -> NativeResult<()> {
    let temp_dir = tempdir()?;
    let export_dir = temp_dir.path().join("export");
    let target_path = temp_dir.path().join("imported.v2");

    // Create mock export with incompatible manifest
    std::fs::create_dir_all(&export_dir)?;

    // Create manifest with incompatible version
    let manifest_path = export_dir.join("export.manifest");
    let incompatible_manifest = format!(
        "V2EXPORT_MANIFEST\nversion=999\nincompatible_format=true\ninvalid_field=bad_value"
    );
    std::fs::write(&manifest_path, incompatible_manifest)?;

    // Create mock graph file
    let graph_file_path = export_dir.join("export.graph");
    std::fs::write(&graph_file_path, b"mock exported graph data")?;

    // Configure import
    let config = V2ImportConfig {
        target_graph_path: target_path.clone(),
        export_dir_path: export_dir.clone(),
        import_mode: ImportMode::Fresh,
        validate_recovery: true,
        force_checkpoint_after_import: true,
    };

    // Should fail due to incompatible manifest
    let result = V2Importer::from_export_dir(&export_dir, &target_path, config);
    assert!(result.is_err(), "Should fail due to incompatible manifest");

    // Even if importer creation succeeded, validation should catch incompatibility
    if let Ok(importer) = result {
        let validation_result = importer.validate_export();
        assert!(
            validation_result.is_err(),
            "Should detect manifest incompatibility"
        );
    }

    Ok(())
}

/// Test 6: End-to-end export/import roundtrip
#[test]
fn test_end_to_end_export_import_roundtrip() -> NativeResult<()> {
    let temp_dir = tempdir()?;
    let source_path = temp_dir.path().join("source.v2");
    let export_dir = temp_dir.path().join("export");
    let target_path = temp_dir.path().join("restored.v2");

    // Create source graph file with some data
    let _source_graph = GraphFile::create(&source_path)?;

    // Create WAL system and write some realistic data
    let source_wal_path = source_path.with_extension("wal");
    let source_checkpoint_path = source_path.with_extension("checkpoint");
    let source_wal_config = V2WALConfig {
        wal_path: source_wal_path.clone(),
        checkpoint_path: source_checkpoint_path.clone(),
        ..Default::default()
    };

    let source_manager = V2WALManager::create(source_wal_config)?;

    // Write some test data
    for i in 1..10 {
        let tx_id = source_manager.begin_transaction(IsolationLevel::ReadCommitted)?;

        // Write node record
        source_manager.write_transaction_record(
            tx_id,
            V2WALRecord::NodeInsert {
                node_id: i,
                slot_offset: (i * 100) as u64,
                node_data: vec![i as u8; 10],
            },
        )?;

        // Write edge record
        source_manager.write_transaction_record(
            tx_id,
            V2WALRecord::ClusterCreate {
                node_id: i,
                direction: Direction::Outgoing,
                cluster_offset: (i * 1000) as u64,
                cluster_size: 5,
                edge_data: vec![i as u8; 20],
            },
        )?;

        source_manager.commit_transaction(tx_id)?;
    }

    // Force checkpoint for clean state
    source_manager.force_checkpoint()?;

    // Step 1: Export from source
    let source_exporter = ExportFactory::create_full_exporter(&source_path, &export_dir)?;
    let export_result = source_exporter.export_full();
    assert!(
        export_result.is_err(),
        "Should fail until export system is implemented"
    );

    // Step 2: Import to target
    let import_config = V2ImportConfig {
        target_graph_path: target_path.clone(),
        export_dir_path: export_dir.clone(),
        import_mode: ImportMode::Fresh,
        validate_recovery: true,
        force_checkpoint_after_import: true,
    };

    let target_importer = V2Importer::from_export_dir(&export_dir, &target_path, import_config)?;
    let import_result = target_importer.import();
    assert!(
        import_result.is_err(),
        "Should fail until import system is implemented"
    );

    // When implemented, verify:
    // 1. Export creates valid manifest, graph file, and WAL tail
    // 2. Import successfully reconstructs database state
    // 3. Recovery validation passes
    // 4. Final LSN and state match expectations

    Ok(())
}

/// Test 7: LSN-bounded export validation
#[test]
fn test_lsn_bounded_export_validation() -> NativeResult<()> {
    let temp_dir = tempdir()?;
    let graph_path = temp_dir.path().join("test.v2");
    let export_dir = temp_dir.path().join("export");

    // Create graph file
    let _graph_file = GraphFile::create(&graph_path)?;

    // Create WAL system
    let wal_path = graph_path.with_extension("wal");
    let checkpoint_path = graph_path.with_extension("checkpoint");
    let wal_config = V2WALConfig {
        wal_path: wal_path.clone(),
        checkpoint_path: checkpoint_path.clone(),
        ..Default::default()
    };

    let manager = V2WALManager::create(wal_config)?;

    // Write multiple transactions to create WAL tail
    for i in 1..5 {
        let tx_id = manager.begin_transaction(IsolationLevel::ReadCommitted)?;
        manager.write_transaction_record(
            tx_id,
            V2WALRecord::NodeInsert {
                node_id: i,
                slot_offset: (i * 100) as u64,
                node_data: vec![i as u8; 50],
            },
        )?;
        manager.commit_transaction(tx_id)?;
    }

    // Create export configuration for LSN-bounded export
    let config = V2ExportConfig {
        export_path: export_dir.join("export"),
        include_wal_tail: true,
        compression_enabled: false,
        checksum_validation: true,
    };

    let exporter_result = V2Exporter::from_graph_file(&graph_path, config);
    assert!(
        exporter_result.is_err(),
        "Should fail until V2Exporter is implemented"
    );

    // Even if exporter creation succeeded, LSN-bounded export should fail
    if let Ok(exporter) = exporter_result {
        let lsn_result = exporter.export_lsn_bounded(1, 100);
        assert!(
            lsn_result.is_err(),
            "Should fail until LSN-bounded export is implemented"
        );
    }

    Ok(())
}

/// Test 8: Manifest integrity validation
#[test]
fn test_manifest_integrity_validation() -> NativeResult<()> {
    let temp_dir = tempdir()?;
    let export_dir = temp_dir.path().join("export");

    // Create mock export with corrupted manifest
    std::fs::create_dir_all(&export_dir)?;

    // Create corrupted manifest (invalid magic bytes)
    let manifest_path = export_dir.join("export.manifest");
    std::fs::write(&manifest_path, b"CORRUPT_MANIFEST_DATA")?;

    // Try to read manifest - should fail due to corruption
    let read_result = ManifestSerializer::read_from_file(&manifest_path);
    assert!(
        read_result.is_err(),
        "Should fail due to corrupted manifest"
    );

    // Even if we could read it, validation should catch corruption
    if let Ok(_manifest) = read_result {
        let validation_result = _manifest.validate();
        assert!(
            validation_result.is_err(),
            "Should detect manifest corruption"
        );
    }

    Ok(())
}

/// Test 9: Bulk ingest integration for import performance
#[test]
fn test_bulk_ingest_integration() -> NativeResult<()> {
    let temp_dir = tempdir()?;
    let export_dir = temp_dir.path().join("export");
    let target_path = temp_dir.path().join("bulk_imported.v2");

    // Create mock export with WAL tail containing many records
    std::fs::create_dir_all(&export_dir)?;

    // Create mock manifest
    let manifest_path = export_dir.join("export.manifest");
    let manifest_bytes = format!(
        "V2EXPORT_MANIFEST\nversion=1\nrecovery_state=DirtyShutdown\nwal_start_lsn=1\nwal_end_lsn=1000\nexport_mode=Full"
    );
    std::fs::write(&manifest_path, manifest_bytes)?;

    // Create mock graph file
    let graph_file_path = export_dir.join("export.graph");
    std::fs::write(&graph_file_path, b"mock exported graph data")?;

    // Create mock WAL with many records (simulating bulk data)
    let wal_file_path = export_dir.join("export.wal");
    let mut wal_data = Vec::new();

    // Simulate 100 WAL records
    for i in 1..=100 {
        // Create a simple record pattern
        wal_data.extend_from_slice(&[1u8]); // Record type
        wal_data.extend_from_slice(&(i as u32).to_le_bytes()); // Record size
        wal_data.extend_from_slice(&[i; 10]); // Mock record data
    }
    std::fs::write(&wal_file_path, wal_data)?;

    // Configure import with bulk optimization
    let config = V2ImportConfig {
        target_graph_path: target_path.clone(),
        export_dir_path: export_dir.clone(),
        import_mode: ImportMode::Fresh,
        validate_recovery: true,
        force_checkpoint_after_import: true,
    };

    // Should fail initially but be designed for bulk ingest optimization
    let result = V2Importer::from_export_dir(&export_dir, &target_path, config);
    assert!(
        result.is_err(),
        "Should fail until import system is implemented"
    );

    Ok(())
}

/// Test 10: Recovery state detection accuracy
#[test]
fn test_recovery_state_detection_accuracy() -> NativeResult<()> {
    let temp_dir = tempdir()?;
    let graph_path = temp_dir.path().join("test.v2");

    // Create graph file
    let _graph_file = GraphFile::create(&graph_path)?;

    // Create WAL system
    let wal_path = graph_path.with_extension("wal");
    let checkpoint_path = graph_path.with_extension("checkpoint");
    let wal_config = V2WALConfig {
        wal_path: wal_path.clone(),
        checkpoint_path: checkpoint_path.clone(),
        ..Default::default()
    };

    let manager = V2WALManager::create(wal_config)?;

    // Test clean shutdown detection
    let tx_id = manager.begin_transaction(IsolationLevel::ReadCommitted)?;
    manager.write_transaction_record(
        tx_id,
        V2WALRecord::NodeInsert {
            node_id: 1,
            slot_offset: 0,
            node_data: vec![1, 2, 3],
        },
    )?;
    manager.commit_transaction(tx_id)?;
    manager.force_checkpoint()?;

    // Verify recovery state detection should work
    let recovery_context =
        RecoveryContext::analyze_files(&wal_path, &graph_path, &checkpoint_path)?;
    assert_eq!(recovery_context.state, ExplicitRecoveryState::CleanShutdown);
    assert_eq!(recovery_context.authority, Authority::GraphFile);

    // Test dirty shutdown detection
    let tx_id = manager.begin_transaction(IsolationLevel::ReadCommitted)?;
    manager.write_transaction_record(
        tx_id,
        V2WALRecord::NodeInsert {
            node_id: 2,
            slot_offset: 0,
            node_data: vec![4, 5, 6],
        },
    )?;
    // Don't commit - simulate crash
    drop(manager);

    let recovery_context =
        RecoveryContext::analyze_files(&wal_path, &graph_path, &checkpoint_path)?;
    assert_eq!(recovery_context.state, ExplicitRecoveryState::DirtyShutdown);
    assert_eq!(recovery_context.authority, Authority::WAL);

    Ok(())
}