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
//! Prefetch Window Tuning Tests for Sequential I/O Optimization
//!
//! **Purpose:** Benchmark different prefetch window sizes (4, 8, 16 slots)
//! **Focus:** Chain traversal performance with different buffer configurations
//!
//! This test module measures the impact of different prefetch window sizes on
//! chain traversal performance. The SequentialReadBuffer can be configured with
//! different window sizes:
//!
//! - 4 slots (16KB): Lower memory, less effective for long chains
//! - 8 slots (32KB): Default, balanced for most workloads
//! - 16 slots (64KB): Higher memory, better for very long chains
//!
//! **NOTE:** As of Phase 32-01, L1 buffer neighbor extraction is instrumentation-only.
//! Full neighbor extraction from buffered NodeRecordV2 is deferred to Plan 32-04.
//! Therefore, these tests measure buffer operations directly rather than end-to-end
//! traversal performance with different window sizes.

use sqlitegraph::backend::native::{
    NativeNodeId,
    adjacency::{LinearDetector, SequentialReadBuffer},
    edge_store::EdgeStore,
    graph_file::GraphFile,
    graph_ops::{TraversalCache, TraversalCacheStats, TraversalContext, native_bfs},
    node_store::NodeStore,
};
use std::time::Instant;
use tempfile::TempDir;

//
// TEST HELPERS
//

/// Helper: Create a linear chain graph with specified node count
///
/// Creates a linear chain: 0 -> 1 -> 2 -> ... -> (n-1)
/// This topology is optimal for sequential I/O optimization.
///
/// Parameters:
/// - node_count: Number of nodes in the chain
///
/// Returns:
/// - GraphFile: The native graph file
/// - Vec<NativeNodeId>: Node IDs [0, 1, 2, ..., n-1]
/// - TempDir: Temporary directory (kept for cleanup)
fn create_chain_graph(node_count: usize) -> (GraphFile, Vec<NativeNodeId>, TempDir) {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test_chain.db");

    let mut graph_file = GraphFile::create(&db_path).expect("Failed to create graph file");

    // Create nodes
    let mut node_ids = Vec::with_capacity(node_count);
    for i in 0..node_count {
        let mut node_store = NodeStore::new(&mut graph_file);
        let node_id = node_store
            .allocate_node_id()
            .expect("Failed to allocate node ID");
        let record = sqlitegraph::backend::native::NodeRecord::new(
            node_id,
            "Node".to_string(),
            format!("node_{}", i),
            serde_json::json!({"id": i}),
        );
        node_store
            .write_node(&record)
            .expect("Failed to write node");
        node_ids.push(node_id);
    }

    // Create chain edges: 0->1, 1->2, ..., (n-2)->(n-1)
    let mut edge_store = EdgeStore::new(&mut graph_file);
    for i in 0..node_count.saturating_sub(1) {
        let edge = sqlitegraph::backend::native::EdgeRecord::new(
            i as i64 + 1,    // edge_id
            node_ids[i],     // from node i
            node_ids[i + 1], // to node i+1
            "chain".to_string(),
            serde_json::json!({"order": i}),
        );
        edge_store
            .write_edge(&edge)
            .expect("Failed to write chain edge");
    }

    (graph_file, node_ids, temp_dir)
}

/// Helper: Measure buffer prefetch operation timing
///
/// Measures the time to prefetch a window of node slots.
/// This directly tests the SequentialReadBuffer prefetch operation
/// without relying on full neighbor extraction.
///
/// Parameters:
/// - graph_file: The native graph file
/// - start_node: Starting node ID for prefetch
/// - prefetch_window: Buffer prefetch window size (4, 8, 16, etc.)
///
/// Returns:
/// - Duration: Prefetch time
/// - usize: Number of nodes cached after prefetch
fn measure_prefetch_timing(
    graph_file: &mut GraphFile,
    start_node: NativeNodeId,
    prefetch_window: usize,
) -> (std::time::Duration, usize) {
    let mut buffer = SequentialReadBuffer::with_prefetch_window(prefetch_window);

    let start_time = Instant::now();
    let _ = buffer.prefetch_from(graph_file, start_node);
    let duration = start_time.elapsed();

    let cached_count = buffer.len();

    (duration, cached_count)
}

//
// WINDOW SIZE TESTS
//

#[test]
fn test_prefetch_window_4_chain_500() {
    let node_count = 500;
    let (mut graph_file, node_ids, _temp_dir) = create_chain_graph(node_count);

    let start_node = node_ids[0];

    println!("\n=== Prefetch Window 4: Chain({}) ===", node_count);
    let (duration, cached_count) = measure_prefetch_timing(&mut graph_file, start_node, 4);

    println!("Window 4 prefetch: {:?}", duration);
    println!("  Cached nodes: {} (expected: ~4)", cached_count);
    println!(
        "  Throughput: {:.2} nodes/sec",
        cached_count as f64 / duration.as_secs_f64()
    );

    // Verify prefetch cached nodes
    assert!(cached_count > 0, "Prefetch should cache some nodes");
    assert!(cached_count <= 4, "Window 4 should cache at most 4 nodes");
}

#[test]
fn test_prefetch_window_8_chain_500() {
    let node_count = 500;
    let (mut graph_file, node_ids, _temp_dir) = create_chain_graph(node_count);

    let start_node = node_ids[0];

    println!("\n=== Prefetch Window 8: Chain({}) ===", node_count);
    let (duration, cached_count) = measure_prefetch_timing(&mut graph_file, start_node, 8);

    println!("Window 8 prefetch: {:?}", duration);
    println!("  Cached nodes: {} (expected: ~8)", cached_count);
    println!(
        "  Throughput: {:.2} nodes/sec",
        cached_count as f64 / duration.as_secs_f64()
    );

    // Verify prefetch cached nodes
    assert!(cached_count > 0, "Prefetch should cache some nodes");
    assert!(cached_count <= 8, "Window 8 should cache at most 8 nodes");
}

#[test]
fn test_prefetch_window_16_chain_500() {
    let node_count = 500;
    let (mut graph_file, node_ids, _temp_dir) = create_chain_graph(node_count);

    let start_node = node_ids[0];

    println!("\n=== Prefetch Window 16: Chain({}) ===", node_count);
    let (duration, cached_count) = measure_prefetch_timing(&mut graph_file, start_node, 16);

    println!("Window 16 prefetch: {:?}", duration);
    println!("  Cached nodes: {} (expected: ~16)", cached_count);
    println!(
        "  Throughput: {:.2} nodes/sec",
        cached_count as f64 / duration.as_secs_f64()
    );

    // Verify prefetch cached nodes
    assert!(cached_count > 0, "Prefetch should cache some nodes");
    assert!(
        cached_count <= 16,
        "Window 16 should cache at most 16 nodes"
    );
}

//
// COMPARISON TEST
//

#[test]
fn test_prefetch_window_comparison_chain_500() {
    let node_count = 500;

    println!(
        "\n=== Prefetch Window Comparison: Chain({}) ===",
        node_count
    );

    // Test window 4
    let (mut graph_file_4, node_ids_4, _temp_dir_4) = create_chain_graph(node_count);
    let start_node = node_ids_4[0];
    let (duration_4, cached_4) = measure_prefetch_timing(&mut graph_file_4, start_node, 4);

    // Test window 8
    let (mut graph_file_8, node_ids_8, _temp_dir_8) = create_chain_graph(node_count);
    let start_node_8 = node_ids_8[0];
    let (duration_8, cached_8) = measure_prefetch_timing(&mut graph_file_8, start_node_8, 8);

    // Test window 16
    let (mut graph_file_16, node_ids_16, _temp_dir_16) = create_chain_graph(node_count);
    let start_node_16 = node_ids_16[0];
    let (duration_16, cached_16) = measure_prefetch_timing(&mut graph_file_16, start_node_16, 16);

    // Print comparison table
    println!("\n=== Timing Comparison ===");
    println!("Window 4:  {:?} (cached: {})", duration_4, cached_4);
    println!("Window 8:  {:?} (cached: {})", duration_8, cached_8);
    println!("Window 16: {:?} (cached: {})", duration_16, cached_16);

    // Calculate timing ratios
    let ratio_4_to_8 = duration_4.as_nanos() as f64 / duration_8.as_nanos() as f64;
    let ratio_16_to_8 = duration_16.as_nanos() as f64 / duration_8.as_nanos() as f64;

    println!("\n=== Timing Ratios (relative to window 8) ===");
    println!("Window 4 vs 8:  {:.3}x", ratio_4_to_8);
    println!("Window 16 vs 8: {:.3}x", ratio_16_to_8);

    // Verify all prefetch operations cached nodes
    assert!(cached_4 > 0);
    assert!(cached_8 > 0);
    assert!(cached_16 > 0);

    // Larger windows should cache more nodes
    assert!(
        cached_4 <= cached_8,
        "Window 8 should cache >= nodes as window 4"
    );
    assert!(
        cached_8 <= cached_16,
        "Window 16 should cache >= nodes as window 8"
    );
}

//
// MEMORY OVERHEAD TESTS
//

#[test]
fn test_memory_overhead_per_buffer_size() {
    println!("\n=== SequentialReadBuffer Memory Overhead ===");

    // Create buffers with different window sizes
    let buffer_4 = SequentialReadBuffer::with_prefetch_window(4);
    let buffer_8 = SequentialReadBuffer::with_prefetch_window(8);
    let buffer_16 = SequentialReadBuffer::with_prefetch_window(16);

    // Each buffer starts empty, so base overhead is minimal
    println!("Empty buffer overhead:");
    println!(
        "  Window 4:  len={}, is_empty={}",
        buffer_4.len(),
        buffer_4.is_empty()
    );
    println!(
        "  Window 8:  len={}, is_empty={}",
        buffer_8.len(),
        buffer_8.is_empty()
    );
    println!(
        "  Window 16: len={}, is_empty={}",
        buffer_16.len(),
        buffer_16.is_empty()
    );

    // Verify empty buffers have no entries
    assert_eq!(buffer_4.len(), 0);
    assert_eq!(buffer_8.len(), 0);
    assert_eq!(buffer_16.len(), 0);
}

#[test]
fn test_memory_overhead_full_buffer() {
    println!("\n=== SequentialReadBuffer Full Buffer Memory ===");

    // Create a buffer with window 8 (default)
    let mut buffer = SequentialReadBuffer::with_prefetch_window(8);

    // Simulate filling buffer with NodeRecordV2 entries
    // Each NodeRecordV2 is approximately 150-200 bytes depending on data
    use sqlitegraph::backend::native::v2::node_record_v2::NodeRecordV2;

    let mut total_estimated_bytes = 0;
    for i in 1..=8 {
        let node = NodeRecordV2::new(
            i,
            "TestNode".to_string(),
            format!("node_{}", i),
            serde_json::json!({"data": "x".repeat(50)}), // ~50 bytes of JSON
        );
        // Estimate NodeRecordV2 size: kind (20) + name (20) + data (100) + overhead
        let estimated_size = 200; // Conservative estimate
        total_estimated_bytes += estimated_size;
        buffer.insert(node);
    }

    println!("Full buffer (8 slots):");
    println!("  Entries: {}", buffer.len());
    println!("  Estimated memory: ~{} bytes", total_estimated_bytes);
    println!(
        "  Per-slot average: ~{} bytes",
        total_estimated_bytes / buffer.len()
    );

    // Verify buffer is full
    assert_eq!(buffer.len(), 8);
    assert!(!buffer.is_empty());

    // Clear and verify empty
    buffer.clear();
    assert_eq!(buffer.len(), 0);
    assert!(buffer.is_empty());
}

//
// SMALLER CHAIN TESTS
//

#[test]
fn test_prefetch_window_sizes_chain_100() {
    let node_count = 100;

    println!("\n=== Prefetch Window Sizes: Chain({}) ===", node_count);

    // For smaller chains, prefetch window has less impact
    let windows = [4, 8, 16];
    let mut results = Vec::new();

    for &window in &windows {
        let (mut graph_file, node_ids, _temp_dir) = create_chain_graph(node_count);
        let start_node = node_ids[0];

        let (duration, cached) = measure_prefetch_timing(&mut graph_file, start_node, window);
        results.push((window, duration, cached));

        println!("Window {}: {:?} (cached: {})", window, duration, cached);
    }

    // Verify all windows cached some nodes
    for (_window, _duration, cached) in &results {
        assert!(*cached > 0, "Prefetch should cache nodes");
    }

    // Larger windows should cache more nodes
    assert!(results[0].2 <= results[1].2); // 4 <= 8
    assert!(results[1].2 <= results[2].2); // 8 <= 16
}

//
// BUFFER CONTAINMENT TESTS
//

#[test]
fn test_buffer_contains_after_prefetch() {
    let node_count = 20;
    let (mut graph_file, node_ids, _temp_dir) = create_chain_graph(node_count);

    let start_node = node_ids[0];

    // Prefetch with window 8
    let mut buffer = SequentialReadBuffer::with_prefetch_window(8);
    buffer
        .prefetch_from(&mut graph_file, start_node)
        .expect("Prefetch should succeed");

    // Buffer should contain the start node
    assert!(
        buffer.contains(start_node),
        "Buffer should contain start node"
    );

    // Buffer should have some nodes cached
    assert!(buffer.len() > 0, "Buffer should have cached nodes");
    assert!(buffer.len() <= 8, "Buffer should not exceed window size");

    println!("\n=== Buffer Containment After Prefetch ===");
    println!("  Start node: {}", start_node);
    println!("  Cached nodes: {}", buffer.len());
    println!("  Contains start: {}", buffer.contains(start_node));
}

//
// NEXT PREFETCH TRACKING TESTS
//

#[test]
fn test_next_prefetch_start_tracking() {
    let node_count = 20;
    let (mut graph_file, node_ids, _temp_dir) = create_chain_graph(node_count);

    let start_node = node_ids[0];

    // Prefetch with window 8
    let mut buffer = SequentialReadBuffer::with_prefetch_window(8);
    buffer
        .prefetch_from(&mut graph_file, start_node)
        .expect("Prefetch should succeed");

    // Check next prefetch start is tracked
    let next_start = buffer.next_prefetch_start();
    assert!(
        next_start.is_some(),
        "Next prefetch start should be tracked"
    );

    // Next prefetch should be start_node + window
    let expected_next = start_node + 8;
    assert_eq!(
        next_start.unwrap(),
        expected_next,
        "Next prefetch should be at start + window"
    );

    println!("\n=== Next Prefetch Start Tracking ===");
    println!("  Start node: {}", start_node);
    println!("  Window size: 8");
    println!("  Next prefetch start: {}", next_start.unwrap());
    println!("  Expected next: {}", expected_next);
}

//
// VERIFICATION TESTS
//

#[test]
fn test_buffer_window_configuration() {
    // Verify that buffers correctly store their window size
    let buffer_4 = SequentialReadBuffer::with_prefetch_window(4);
    let buffer_8 = SequentialReadBuffer::with_prefetch_window(8);
    let buffer_16 = SequentialReadBuffer::with_prefetch_window(16);

    assert_eq!(buffer_4.prefetch_window(), 4);
    assert_eq!(buffer_8.prefetch_window(), 8);
    assert_eq!(buffer_16.prefetch_window(), 16);

    // Default buffer should have window 8
    let buffer_default = SequentialReadBuffer::new();
    assert_eq!(buffer_default.prefetch_window(), 8);

    println!("\nBuffer window configuration verified:");
    println!(
        "  with_prefetch_window(4) -> {}",
        buffer_4.prefetch_window()
    );
    println!(
        "  with_prefetch_window(8) -> {}",
        buffer_8.prefetch_window()
    );
    println!(
        "  with_prefetch_window(16) -> {}",
        buffer_16.prefetch_window()
    );
    println!(
        "  SequentialReadBuffer::new() -> {}",
        buffer_default.prefetch_window()
    );
}

#[test]
fn test_traversal_context_custom_buffer() {
    // Verify that TraversalContext can be constructed with custom buffer
    let custom_buffer = SequentialReadBuffer::with_prefetch_window(16);
    let mut ctx = TraversalContext::new();
    ctx.buffer = custom_buffer;

    assert_eq!(ctx.buffer.prefetch_window(), 16);
    assert_eq!(ctx.buffer_hits, 0);
    assert_eq!(ctx.buffer_misses, 0);
    assert_eq!(ctx.combined_hit_rate(), 0.0);

    println!("\nTraversalContext custom buffer configuration verified:");
    println!("  Custom window 16 buffer works correctly");
}

//
// NATIVE BFS TEST (with default window)
//

#[test]
fn test_native_bfs_chain_500_with_default_window() {
    let node_count = 500;
    let (mut graph_file, node_ids, _temp_dir) = create_chain_graph(node_count);

    let start_node = node_ids[0];
    let depth = node_count as u32;

    println!(
        "\n=== Native BFS with Default Window 8: Chain({}) ===",
        node_count
    );

    let start_time = Instant::now();
    let result = native_bfs(&mut graph_file, start_node, depth);
    let duration = start_time.elapsed();

    assert!(result.is_ok(), "BFS should succeed");
    let visited = result.unwrap();

    println!("Native BFS: {:?}", duration);
    println!(
        "  Visited: {} nodes (expected: {})",
        visited.len(),
        node_count
    );
    println!(
        "  Throughput: {:.2} nodes/sec",
        node_count as f64 / duration.as_secs_f64()
    );

    // Note: As of Phase 32-01, L1 buffer neighbor extraction is instrumentation-only
    // This test validates that native_bfs works correctly with default window=8
    // Note: BFS excludes the start node from visited list, so we expect node_count - 1
    assert_eq!(
        visited.len(),
        node_count - 1,
        "BFS should visit all reachable nodes (excluding start)"
    );
}

//
// BUFFER CLEAR TESTS
//

#[test]
fn test_buffer_clear_operations() {
    let mut buffer = SequentialReadBuffer::with_prefetch_window(8);

    // Insert some nodes
    use sqlitegraph::backend::native::v2::node_record_v2::NodeRecordV2;
    for i in 1..=5 {
        buffer.insert(NodeRecordV2::new(
            i,
            "Test".to_string(),
            format!("node_{}", i),
            serde_json::json!({}),
        ));
    }

    assert_eq!(buffer.len(), 5);

    // Clear buffer
    buffer.clear();

    assert_eq!(buffer.len(), 0);
    assert!(buffer.is_empty());
    assert!(buffer.next_prefetch_start().is_none());

    println!("\nBuffer clear operations verified:");
    println!("  Clear removes all entries and resets tracking");
}