sqlitegraph 2.2.3

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
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
//! Comprehensive unit tests for NodeStore V3 components
//!
//! This module provides test coverage for:
//! - B+Tree lookup operations
//! - NodePage loading and decompression
//! - Traversal cache behavior (via store module)
//! - Error handling
//!
//! Test utilities:
//! - Test helpers for NodeRecordV3 creation
//! - Integration-style tests for end-to-end flows

use crate::backend::native::NativeBackendError;
use crate::backend::native::NativeResult;
use crate::backend::native::types::NodeFlags;
use crate::backend::native::v3::index::{IndexPage, IndexPageType};
use crate::backend::native::v3::node::page::NodePage;
use crate::backend::native::v3::node::record::NodeRecordV3;
use crate::backend::native::v3::node::store::TraversalCache;
use std::sync::Arc;

/// Test node ID range for scalability tests
pub const TEST_NODE_COUNT: usize = 100;

/// Page capacity for test fixtures
pub const TEST_PAGE_CAPACITY: usize = 20;

// ============================================================================
// B+Tree Lookup Tests
// ============================================================================

#[test]
fn test_btree_lookup_single_node() {
    // Create a simple index page with one entry
    let index_page = IndexPage::new_leaf(1);

    // Verify basic page structure
    assert_eq!(index_page.page_id(), 1);
    assert!(matches!(index_page.page_type(), IndexPageType::Leaf));
    assert_eq!(index_page.count(), 0);
}

#[test]
fn test_btree_lookup_multiple_nodes() {
    // Test sequential node lookups across multiple pages
    let page_ids: Vec<u64> = (1..=5).collect();

    for (i, &page_id) in page_ids.iter().enumerate() {
        let index_page = IndexPage::new_leaf(page_id);
        assert_eq!(index_page.page_id(), page_id);
        assert_eq!(index_page.count(), 0);

        // Simulate adding node_id = i * 100 to page_id
        let test_node_id = (i * 100) as i64;
        assert!(test_node_id >= 0);
    }
}

#[test]
fn test_btree_lookup_nonexistent_node() {
    // Test that missing node returns None
    let index_page = IndexPage::new_leaf(1);

    // In a full implementation, searching for a non-existent key
    // would return None. Here we verify the page is empty.
    assert_eq!(index_page.count(), 0);
}

#[test]
fn test_btree_index_page_creation() {
    // Test internal vs leaf page creation
    let leaf_page = IndexPage::new_leaf(10);
    assert!(matches!(leaf_page.page_type(), IndexPageType::Leaf));
    assert_eq!(leaf_page.page_id(), 10);
}

#[test]
fn test_btree_page_type_discrimination() {
    // Verify leaf vs internal page behavior
    let leaf = IndexPage::new_leaf(1);
    assert!(matches!(leaf.page_type(), IndexPageType::Leaf));

    // Index pages support both types for B+Tree structure
    // Internal pages would be created with IndexPage::new_internal()
    // Testing the basic structure exists
}

// ============================================================================
// NodePage Loading Tests
// ============================================================================

#[test]
fn test_page_loading_decompression() {
    // Test that NodePage can pack and unpack correctly
    let mut page = NodePage::new(1);

    // Add nodes with varying data sizes
    for i in 0..5 {
        let node = NodeRecordV3::new_inline(
            i as i64,
            NodeFlags::empty(),
            i as u16 * 10,
            i as u16 * 20,
            vec![i as u8; 32],
            i as u64 * 1000,
            i as u32 * 5,
            i as u64 * 2000,
            i as u32 * 3,
        );
        page.add_node(node).unwrap();
    }

    // Pack to bytes (simulating disk write)
    let bytes = page.pack().unwrap();

    // Unpack (simulating disk read + decompression)
    let loaded_page = NodePage::unpack(&bytes).unwrap();

    // Verify all nodes loaded correctly
    assert_eq!(loaded_page.node_count(), 5);
    assert_eq!(loaded_page.page_id, 1);

    for (i, node) in loaded_page.nodes.iter().enumerate() {
        assert_eq!(node.id(), i as i64);
        assert_eq!(node.kind_offset, (i * 10) as u16);
        assert_eq!(node.name_offset, (i * 20) as u16);
    }
}

#[test]
fn test_page_checksum_validation() {
    // Create a page and verify checksum
    let mut page = NodePage::new(42);

    let node = NodeRecordV3::new_inline(
        123,
        NodeFlags::empty(),
        10,
        20,
        b"test data".to_vec(),
        1000,
        5,
        2000,
        3,
    );
    page.add_node(node).unwrap();

    let bytes = page.pack().unwrap();

    // Valid checksum should unpack correctly
    assert!(NodePage::unpack(&bytes).is_ok());

    // Corrupt checksum should fail
    let mut corrupted = bytes.clone();
    corrupted[28] ^= 0xFF; // Flip bits in checksum field

    let result = NodePage::unpack(&corrupted);
    assert!(result.is_err(), "Corrupted checksum should fail validation");

    if let Err(NativeBackendError::InvalidHeader { field, .. }) = result {
        assert!(field.contains("checksum") || field.contains("node_page"));
    } else {
        panic!("Expected InvalidHeader error for checksum mismatch");
    }
}

#[test]
fn test_page_not_found_error() {
    // Test error handling for short page data
    let short_data = vec![0u8; 100];
    let result = NodePage::unpack(&short_data);
    assert!(result.is_err(), "Short page data should return error");

    // Verify it's the right error type
    if let Err(NativeBackendError::InvalidHeader { field, .. }) = result {
        assert!(
            field.contains("node_page"),
            "Expected node_page error for short data"
        );
    }
}

#[test]
fn test_page_overflow_handling() {
    // Test adding nodes until page is full
    let mut page = NodePage::new(1);
    let mut added_count = 0;

    // Add nodes with 50 bytes of data each
    // This should fit approximately 4064 / (12 + 50) ≈ 66 nodes
    for i in 0..100 {
        let node = NodeRecordV3::new_inline(
            i as i64,
            NodeFlags::empty(),
            0,
            0,
            vec![i as u8; 50],
            0,
            0,
            0,
            0,
        );

        match page.add_node(node) {
            Ok(_) => added_count += 1,
            Err(_) => {
                // Page is full
                break;
            }
        }
    }

    // Should fit at least 20 nodes
    assert!(
        added_count >= 20,
        "Should fit at least 20 nodes, got {}",
        added_count
    );

    // Verify round-trip works
    let bytes = page.pack().unwrap();
    let loaded = NodePage::unpack(&bytes).unwrap();
    assert_eq!(loaded.node_count() as usize, added_count);
}

#[test]
fn test_page_empty_data() {
    // Test loading page with minimal node data
    let mut page = NodePage::new(1);

    let node = NodeRecordV3::new_inline(1, NodeFlags::empty(), 0, 0, vec![], 0, 0, 0, 0);
    page.add_node(node).unwrap();

    let bytes = page.pack().unwrap();
    let loaded = NodePage::unpack(&bytes).unwrap();

    assert_eq!(loaded.node_count(), 1);
    assert_eq!(loaded.nodes[0].data_inline, Some(vec![]));
}

#[test]
fn test_page_with_external_data() {
    // Test nodes with external data references
    let mut page = NodePage::new(1);

    let node = NodeRecordV3::new_external(1, NodeFlags::empty(), 100, 200, 5000, 200, 0, 5, 0, 3);
    page.add_node(node).unwrap();

    let bytes = page.pack().unwrap();
    let loaded = NodePage::unpack(&bytes).unwrap();

    assert_eq!(loaded.node_count(), 1);
    assert!(loaded.nodes[0].is_external());
    assert_eq!(loaded.nodes[0].data_len(), 200);
}

// ============================================================================
// Traversal Cache Integration Tests
// ============================================================================

#[test]
fn test_cache_hit_miss_tracking() {
    let mut cache = TraversalCache::new(4);

    // Initial state: no hits, no misses
    assert_eq!(cache.len(), 0);
    assert_eq!(cache.hits(), 0);
    assert_eq!(cache.misses(), 0);

    // Cache miss on first access
    assert!(cache.get(1).is_none());
    assert_eq!(cache.misses(), 1);

    // Insert page
    let page = Arc::new(NodePage::new(1));
    cache.insert(1, page);

    // Cache hit on second access
    assert!(cache.get(1).is_some());
    assert_eq!(cache.hits(), 1);
    assert_eq!(cache.misses(), 1);
}

#[test]
fn test_cache_lru_eviction() {
    let mut cache = TraversalCache::new(2);

    // Insert two pages
    cache.insert(1, Arc::new(NodePage::new(1)));
    cache.insert(2, Arc::new(NodePage::new(2)));

    assert_eq!(cache.len(), 2);

    // Insert third page - should evict oldest
    cache.insert(3, Arc::new(NodePage::new(3)));

    assert_eq!(cache.len(), 2);

    // Page 1 should be evicted
    assert!(cache.get(1).is_none());
    assert!(cache.get(2).is_some());
    assert!(cache.get(3).is_some());
}

#[test]
fn test_cache_invalidation() {
    let mut cache = TraversalCache::new(4);

    cache.insert(1, Arc::new(NodePage::new(1)));
    cache.insert(2, Arc::new(NodePage::new(2)));

    assert_eq!(cache.len(), 2);

    // Invalidate page 1
    cache.invalidate(1);

    assert_eq!(cache.len(), 1);
    assert!(cache.get(1).is_none());
    assert!(cache.get(2).is_some());
}

#[test]
fn test_cache_clear() {
    let mut cache = TraversalCache::new(4);

    cache.insert(1, Arc::new(NodePage::new(1)));
    cache.insert(2, Arc::new(NodePage::new(2)));
    cache.insert(3, Arc::new(NodePage::new(3)));

    assert_eq!(cache.len(), 3);

    cache.clear();

    assert_eq!(cache.len(), 0);
    assert!(cache.get(1).is_none());
    assert!(cache.get(2).is_none());
    assert!(cache.get(3).is_none());
}

#[test]
fn test_cache_hit_rate_calculation() {
    let mut cache = TraversalCache::new(4);

    // No accesses
    assert_eq!(cache.hit_ratio(), 0.0);

    // Insert one page
    cache.insert(1, Arc::new(NodePage::new(1)));

    // All hits
    for _ in 0..10 {
        cache.get(1);
    }

    assert_eq!(cache.hit_ratio(), 1.0);

    // Mix of hits and misses
    cache.get(2); // miss
    cache.get(3); // miss

    // 10 hits, 2 misses = 10/12 ≈ 0.833
    let hit_rate = cache.hit_ratio();
    assert!(
        hit_rate > 0.8 && hit_rate < 0.9,
        "Expected hit rate ~0.833, got {}",
        hit_rate
    );
}

#[test]
fn test_cache_sequential_access_pattern() {
    // Simulate sequential traversal access pattern
    let mut cache = TraversalCache::new(4);

    // Simulate BFS traversal: access pages 1,2,3,4, then revisit 1,2
    for i in 1..=4 {
        cache.insert(i, Arc::new(NodePage::new(i)));
    }

    // Access all pages (all hits)
    for i in 1..=4 {
        cache.get(i);
    }

    assert_eq!(cache.hits(), 4);
    assert_eq!(cache.misses(), 0);
    assert_eq!(cache.hit_ratio(), 1.0);
}

// ============================================================================
// Error Handling Tests
// ============================================================================

#[test]
fn test_storage_error_propagation() {
    // Test that storage errors are properly propagated
    let short_data = vec![0u8; 10];
    let result = NodePage::unpack(&short_data);

    assert!(result.is_err());
    match result {
        Err(NativeBackendError::InvalidHeader { field, .. }) => {
            assert!(field.contains("node_page") || field.contains("insufficient"));
        }
        _ => panic!("Expected InvalidHeader error for insufficient bytes"),
    }
}

#[test]
fn test_compression_error_handling() {
    // Test handling of corrupted compressed data
    let mut page = NodePage::new(1);

    // Add a valid node
    let node = NodeRecordV3::new_inline(1, NodeFlags::empty(), 0, 0, vec![1, 2, 3, 4], 0, 0, 0, 0);
    page.add_node(node).unwrap();

    let bytes = page.pack().unwrap();

    // Corrupt the node data region
    let mut corrupted = bytes.clone();
    // Corrupt some bytes in the data region (after header)
    let data_start = 32; // PAGE_HEADER_SIZE
    if data_start + 10 < corrupted.len() {
        corrupted[data_start] ^= 0xFF;
        corrupted[data_start + 5] ^= 0xFF;
    }

    // Should fail during unpack
    let result = NodePage::unpack(&corrupted);
    assert!(result.is_err(), "Corrupted data should fail unpacking");
}

#[test]
fn test_corruption_recovery() {
    // Test recovery from corruption using checksums
    let mut page = NodePage::new(1);

    for i in 0..3 {
        let node = NodeRecordV3::new_inline(
            i as i64,
            NodeFlags::empty(),
            i as u16 * 10,
            i as u16 * 20,
            vec![i as u8; 20],
            i as u64 * 100,
            i as u32 * 2,
            i as u64 * 200,
            i as u32 * 3,
        );
        page.add_node(node).unwrap();
    }

    let valid_bytes = page.pack().unwrap();
    let _valid_checksum = u32::from_be_bytes([
        valid_bytes[28],
        valid_bytes[29],
        valid_bytes[30],
        valid_bytes[31],
    ]);

    // Corrupt data
    let mut corrupted = valid_bytes.clone();
    corrupted[100] ^= 0xFF;

    // Unpack should detect checksum mismatch
    let result = NodePage::unpack(&corrupted);
    assert!(result.is_err());
}

#[test]
fn test_invalid_node_id() {
    // Test handling of invalid node IDs
    let mut page = NodePage::new(1);

    // Test with i64::MIN (should work)
    let node = NodeRecordV3::new_inline(i64::MIN, NodeFlags::empty(), 0, 0, vec![], 0, 0, 0, 0);
    assert!(page.add_node(node).is_ok());

    // Test with i64::MAX (should work)
    let node2 = NodeRecordV3::new_inline(i64::MAX, NodeFlags::empty(), 0, 0, vec![], 0, 0, 0, 0);
    assert!(page.add_node(node2).is_ok());
}

#[test]
fn test_edge_case_empty_page() {
    // Test handling of completely empty page
    let page = NodePage::new(0);
    let bytes = page.pack().unwrap();

    assert_eq!(bytes.len(), 4096);

    let loaded = NodePage::unpack(&bytes).unwrap();
    assert_eq!(loaded.page_id, 0);
    assert_eq!(loaded.node_count(), 0);
    assert!(loaded.is_empty());
}

#[test]
fn test_edge_case_max_inline_data() {
    // Test node with maximum inline data
    let mut page = NodePage::new(1);

    let max_data = vec![0xABu8; 64]; // MAX_INLINE_DATA
    let node = NodeRecordV3::new_inline(
        1,
        NodeFlags::empty(),
        100,
        200,
        max_data.clone(),
        0,
        0,
        0,
        0,
    );

    page.add_node(node).unwrap();

    let bytes = page.pack().unwrap();
    let loaded = NodePage::unpack(&bytes).unwrap();

    assert_eq!(loaded.nodes[0].data_inline, Some(max_data));
}

// ============================================================================
// Test Helpers
// ============================================================================

/// Create a test node with default values
pub fn create_test_node(id: i64) -> NodeRecordV3 {
    NodeRecordV3::new_inline(
        id,
        NodeFlags::empty(),
        (id % 1000) as u16,
        ((id % 100) + 100) as u16,
        vec![id as u8; 32],
        (id as u64) * 1000,
        ((id % 10) + 1) as u32,
        (id as u64) * 2000,
        ((id % 5) + 1) as u32,
    )
}

/// Create a test page with specified number of nodes
pub fn create_test_page(page_id: u64, node_count: usize) -> NodePage {
    let mut page = NodePage::new(page_id);

    for i in 0..node_count {
        let node = create_test_node(i as i64);
        if page.add_node(node).is_err() {
            break; // Page full
        }
    }

    page
}

/// Verify round-trip serialization
pub fn verify_round_trip(page: &NodePage) -> NativeResult<()> {
    let bytes = page.pack()?;
    let loaded = NodePage::unpack(&bytes)?;

    assert_eq!(loaded.page_id, page.page_id);
    assert_eq!(loaded.node_count(), page.node_count());
    assert_eq!(loaded.nodes.len(), page.nodes.len());

    for (original, restored) in page.nodes.iter().zip(loaded.nodes.iter()) {
        assert_eq!(restored.id(), original.id());
        assert_eq!(restored.flags, original.flags);
        assert_eq!(restored.kind_offset, original.kind_offset);
        assert_eq!(restored.name_offset, original.name_offset);
    }

    Ok(())
}

// ============================================================================
// Integration-style Tests
// ============================================================================

#[test]
fn test_end_to_end_node_storage() {
    // Test full flow: create page -> add nodes -> pack -> unpack -> verify
    let page = create_test_page(1, 10);

    assert!(page.node_count() > 0);

    verify_round_trip(&page).unwrap();
}

#[test]
fn test_multiple_pages_round_trip() {
    // Test multiple pages can be independently round-tripped
    let pages: Vec<NodePage> = vec![
        create_test_page(1, 5),
        create_test_page(2, 10),
        create_test_page(3, 15),
    ];

    for page in &pages {
        verify_round_trip(page).unwrap();
    }
}

#[test]
fn test_node_flags_preservation() {
    // Test that all node flags are preserved through round-trip
    let flags_to_test = vec![NodeFlags::empty(), NodeFlags::DELETED, NodeFlags::NONE];

    for flags in flags_to_test {
        let mut page = NodePage::new(1);
        let node = NodeRecordV3::new_inline(1, flags, 10, 20, vec![], 0, 0, 0, 0);
        page.add_node(node).unwrap();

        let bytes = page.pack().unwrap();
        let loaded = NodePage::unpack(&bytes).unwrap();

        assert_eq!(loaded.nodes[0].flags, flags);
    }
}

#[test]
fn test_page_capacity_calculation() {
    // Test that page capacity calculations are correct
    let mut page = NodePage::new(1);

    // Initial capacity should be full usable size
    assert_eq!(page.remaining_capacity(), 4064);

    // Add a node that uses some space (max 64 bytes inline)
    let node = NodeRecordV3::new_inline(
        1,
        NodeFlags::empty(),
        0,
        0,
        vec![1u8; 50], // Use 50 bytes (under MAX_INLINE_DATA of 64)
        0,
        0,
        0,
        0,
    );
    page.add_node(node).unwrap();

    // Capacity should have decreased
    assert!(page.remaining_capacity() < 4064);
}

#[test]
fn test_space_efficiency_tracking() {
    // Test space efficiency calculation
    let mut page = NodePage::new(1);

    // Empty page has 0 efficiency
    assert_eq!(page.space_efficiency(), 0.0);

    // Add nodes to fill some space
    for i in 0..5 {
        let node = NodeRecordV3::new_inline(
            i as i64,
            NodeFlags::empty(),
            i as u16 * 10,
            i as u16 * 20,
            vec![i as u8; 50],
            i as u64 * 100,
            i as u32 * 2,
            i as u64 * 200,
            i as u32 * 3,
        );
        page.add_node(node).unwrap();
    }

    // Efficiency should be between 0 and 1
    let efficiency = page.space_efficiency();
    assert!(efficiency > 0.0);
    assert!(efficiency <= 1.0);
}

#[test]
fn test_cache_with_various_page_ids() {
    // Test cache works with various page ID values
    let mut cache = TraversalCache::new(16);

    let test_ids = vec![0, 1, 100, 1000, u64::MAX - 1];

    for &id in &test_ids {
        cache.insert(id, Arc::new(NodePage::new(id)));
    }

    assert_eq!(cache.len(), test_ids.len());

    for &id in &test_ids {
        assert!(cache.contains(&id));
        let retrieved = cache.get(id);
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().page_id, id);
    }
}

#[test]
fn test_large_scale_page_operations() {
    // Test scalability with many pages
    let page_count = 50;
    let pages: Vec<NodePage> = (0..page_count).map(|i| create_test_page(i, 10)).collect();

    // Verify all pages can round-trip
    for page in &pages {
        verify_round_trip(page).unwrap();
    }

    // Verify cache can handle all these pages
    let mut cache = TraversalCache::new(32);

    // Insert half of them (at capacity)
    for (i, page) in pages.iter().take(32).enumerate() {
        cache.insert(i as u64, Arc::new(page.clone()));
    }

    assert_eq!(cache.len(), 32);
    assert_eq!(cache.capacity(), 32);
}