sqlitegraph 3.4.0

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
//! Semantic KNN fallback layer using HNSW vector search with optional turbovec optimization.
//!
//! `SemanticLayer` provides HNSW-based nearest neighbor search when CSR edges
//! lack coverage for generation. Enables cross-domain and cross-model transfer.
//! Uses turbovec for large-scale embedding datasets (>1K vectors) to reduce
//! memory footprint and improve search performance when the `turbovec` feature is enabled.

use crate::hnsw::{DistanceMetric, HnswConfigBuilder, HnswIndex};
use std::sync::Arc;
use std::sync::Mutex;

#[cfg(feature = "turbovec")]
use turbovec;

/// KNN search result with node ID and distance.
#[derive(Debug, Clone, PartialEq)]
pub struct KnnResult {
    /// Nearest neighbor node ID
    pub node_id: u32,

    /// Distance from query vector
    pub distance: f32,
}

/// Semantic layer for HNSW-based KNN fallback search.
///
/// Provides approximate nearest neighbor search when CSR edges don't provide
/// sufficient coverage for generation tokens. Optionally uses turbovec for
/// large-scale datasets (when `turbovec` feature is enabled) to reduce memory
/// footprint and improve performance.
///
/// # Performance
///
/// - **Search Time**: O(log N) average case via HNSW, optimized with turbovec for large N
/// - **Insert Time**: O(log N) average case
/// - **Memory Usage**: 2-3x vector data size overhead (reduced by turbovec quantization when enabled)
/// - **Accuracy**: 95%+ recall for typical workloads
/// - **Turbovec activation**: >1K embeddings for 2-4 bit compression + SIMD search (feature-gated)
pub struct SemanticLayer {
    /// HNSW index for vector search
    hnsw_index: Arc<Mutex<HnswIndex>>,

    /// Embedding dimension
    dimension: usize,

    /// Turbovec compressed index (activates >1K embeddings, feature-gated)
    #[cfg(feature = "turbovec")]
    turbovec_index: Arc<Mutex<Option<turbovec::IdMapIndex>>>,

    /// Current embedding count (for threshold tracking, feature-gated)
    #[cfg(feature = "turbovec")]
    embedding_count: Arc<Mutex<usize>>,
}

/// Turbovec activation threshold (>1K embeddings triggers compression)
#[cfg(feature = "turbovec")]
const TURBOVEC_THRESHOLD: usize = 1_000;

impl SemanticLayer {
    /// Create a new semantic layer with given dimension.
    ///
    /// # Arguments
    ///
    /// * `dimension` - Embedding vector dimension
    pub fn new(dimension: usize) -> Self {
        // Build HNSW config optimized for semantic search
        let config = HnswConfigBuilder::new()
            .dimension(dimension)
            .m_connections(16) // Balanced connectivity
            .ef_construction(200) // Good index quality
            .ef_search(50) // Fast search with good recall
            .distance_metric(DistanceMetric::Cosine)
            .build()
            .expect("Invalid HNSW configuration");

        // Create in-memory HNSW index
        let hnsw_index =
            HnswIndex::new("semantic_layer", config).expect("Failed to create HNSW index");

        #[cfg(feature = "turbovec")]
        return Self {
            hnsw_index: Arc::new(Mutex::new(hnsw_index)),
            turbovec_index: Arc::new(Mutex::new(None)),
            dimension,
            embedding_count: Arc::new(Mutex::new(0)),
        };

        #[cfg(not(feature = "turbovec"))]
        return Self {
            hnsw_index: Arc::new(Mutex::new(hnsw_index)),
            dimension,
        };
    }

    /// Insert or update a token embedding (turbovec version).
    ///
    /// Supports incremental insert for fine-tune updates (transferable doc).
    /// Automatically builds turbovec index when threshold is crossed (>1K embeddings) when turbovec feature is enabled.
    ///
    /// # Arguments
    ///
    /// * `token_id` - Token node ID
    /// * `embedding` - Embedding vector (must match configured dimension)
    ///
    /// # Returns
    ///
    /// `Ok(())` if insert succeeded, `Err(String)` if dimension mismatch or HNSW error.
    #[cfg(feature = "turbovec")]
    pub fn insert_embedding(&mut self, token_id: u32, embedding: Vec<f32>) -> Result<(), String> {
        if embedding.len() != self.dimension {
            return Err(format!(
                "Embedding dimension mismatch: expected {}, got {}",
                self.dimension,
                embedding.len()
            ));
        }

        // Store token_id in metadata for reverse mapping during search
        let metadata = serde_json::json!({ "token_id": token_id });

        let mut hnsw = self.hnsw_index.lock().unwrap();
        hnsw.insert_vector(&embedding, Some(metadata))
            .map_err(|e| format!("HNSW insert failed: {}", e))?;

        // Update embedding count
        let mut count = self.embedding_count.lock().unwrap();
        *count += 1;
        let current_count = *count;
        drop(count);

        // Check if we've crossed the turbovec threshold
        if current_count == TURBOVEC_THRESHOLD + 1 {
            // Build turbovec index on threshold crossing
            self.build_turbovec_index()?;
        } else if current_count > TURBOVEC_THRESHOLD {
            // Mark turbovec for rebuild (lazy rebuild on next search)
            let mut turbovec = self.turbovec_index.lock().unwrap();
            *turbovec = None; // Clear to trigger rebuild on search
        }

        Ok(())
    }

    /// Find K nearest neighbors for a query embedding.
    ///
    /// # Arguments
    ///
    /// * `query_embedding` - Query vector (must match configured dimension)
    /// * `k` - Number of neighbors to return
    ///
    /// # Returns
    ///
    /// Top-K nearest neighbors sorted by distance (closest first).
    /// Returns fewer than K results if insufficient embeddings exist.
    /// Uses turbovec for datasets >1K embeddings (automatic activation).
    #[cfg(feature = "turbovec")]
    pub fn knn_search(&self, query_embedding: &[f32], k: usize) -> Vec<KnnResult> {
        if query_embedding.len() != self.dimension {
            return Vec::new(); // Dimension mismatch
        }

        // Check if we should use turbovec (large dataset)
        let count = *self.embedding_count.lock().unwrap();
        if count > TURBOVEC_THRESHOLD {
            // Ensure turbovec index is built
            self.ensure_turbovec_index();

            // Try turbovec search first
            let turbovec = self.turbovec_index.lock().unwrap();
            if let Some(ref index) = *turbovec {
                return self.turbovec_search(index, query_embedding, k);
            }
        }

        // Fall back to HNSW search for small datasets or if turbovec unavailable
        let hnsw = self.hnsw_index.lock().unwrap();

        // Search HNSW index
        let results = hnsw.search(query_embedding, k);

        // Convert HNSW results (vector_id, distance) to KnnResult (token_id, distance)
        match results {
            Ok(hnsw_results) => {
                hnsw_results
                    .into_iter()
                    .filter_map(|(vector_id, distance)| {
                        // Extract token_id from stored metadata
                        hnsw.get_vector(vector_id)
                            .ok()
                            .flatten()
                            .and_then(|(_, metadata)| {
                                metadata
                                    .get("token_id")
                                    .and_then(|v| v.as_u64())
                                    .map(|token_id| KnnResult {
                                        node_id: token_id as u32,
                                        distance,
                                    })
                            })
                    })
                    .collect()
            }
            Err(_) => Vec::new(), // HNSW search failed
        }
    }

    /// Build turbovec index from existing HNSW embeddings.
    ///
    /// Called automatically when threshold is crossed (>1K embeddings).
    /// Extracts all embeddings from HNSW and builds compressed turbovec index.
    #[cfg(feature = "turbovec")]
    fn build_turbovec_index(&self) -> Result<(), String> {
        let hnsw = self.hnsw_index.lock().unwrap();
        let count = hnsw.vector_count();

        if count == 0 {
            return Ok(()); // No embeddings to index
        }

        // Collect all embeddings and IDs from HNSW
        let mut embeddings: Vec<f32> = Vec::with_capacity(count * self.dimension);
        let mut ids: Vec<u64> = Vec::with_capacity(count);

        for i in 1..=count {
            if let Ok(Some((vector, metadata))) = hnsw.get_vector(i as u64) {
                if let Some(token_id) = metadata.get("token_id").and_then(|v| v.as_u64()) {
                    embeddings.extend_from_slice(&vector);
                    ids.push(token_id);
                }
            }
        }

        drop(hnsw);

        // Build turbovec index with 4-bit quantization
        let mut turbovec_index = turbovec::IdMapIndex::new(self.dimension, 4)
            .map_err(|e| format!("Turbovec construction failed: {}", e))?;

        turbovec_index
            .add_with_ids(&embeddings, &ids)
            .map_err(|e| format!("Turbovec add failed: {}", e))?;

        // Store the index
        let mut turbovec = self.turbovec_index.lock().unwrap();
        *turbovec = Some(turbovec_index);

        Ok(())
    }

    /// Ensure turbovec index is built (lazy rebuild if cleared).
    ///
    /// Called during search when turbovec is needed but not available.
    #[cfg(feature = "turbovec")]
    fn ensure_turbovec_index(&self) {
        let turbovec = self.turbovec_index.lock().unwrap();
        if turbovec.is_some() {
            return; // Already built
        }
        drop(turbovec);

        // Rebuild if cleared (after incremental inserts)
        if let Err(e) = self.build_turbovec_index() {
            eprintln!("Failed to rebuild turbovec index: {}", e);
        }
    }

    /// Search using turbovec index (for large datasets).
    ///
    /// # Arguments
    ///
    /// * `index` - Turbovec index (must be built)
    /// * `query_embedding` - Query vector
    /// * `k` - Number of neighbors to return
    ///
    /// # Returns
    ///
    /// Top-K nearest neighbors sorted by distance.
    #[cfg(feature = "turbovec")]
    fn turbovec_search(
        &self,
        index: &turbovec::IdMapIndex,
        query_embedding: &[f32],
        k: usize,
    ) -> Vec<KnnResult> {
        // Turbovec search returns (scores, ids)
        let (scores, ids) = index.search(query_embedding, k);

        // Convert to KnnResult format
        scores
            .into_iter()
            .zip(ids.into_iter())
            .map(|(distance, node_id)| KnnResult {
                node_id: node_id as u32,
                distance,
            })
            .collect()
    }

    /// Get the number of embeddings in this layer (turbovec version).
    #[cfg(feature = "turbovec")]
    pub fn embedding_count(&self) -> usize {
        let count = self.embedding_count.lock().unwrap();
        *count
    }

    /// Check if a token has an embedding (turbovec version).
    #[cfg(feature = "turbovec")]
    pub fn has_embedding(&self, token_id: u32) -> bool {
        let hnsw = self.hnsw_index.lock().unwrap();

        // Brute-force search through HNSW to check if token_id exists
        // In production, maintain a separate HashSet for O(1) lookup
        for i in 1..=hnsw.vector_count() {
            if let Ok(Some((_, metadata))) = hnsw.get_vector(i as u64)
                && let Some(id) = metadata.get("token_id").and_then(|v| v.as_u64())
                && id == token_id as u64
            {
                return true;
            }
        }
        false
    }

    /// Remove an embedding (turbovec version - stub for API compatibility).
    ///
    /// Note: deletion requires full index rebuild — call rebuild_hnsw_index() after removals.
    #[cfg(feature = "turbovec")]
    pub fn remove_embedding(&mut self, _token_id: u32) -> bool {
        // HNSW doesn't support efficient deletion
        // In production, would need to rebuild index or use versioning
        // For now, return false to maintain API compatibility
        false
    }

    /// Get HNSW index statistics (turbovec version).
    #[cfg(feature = "turbovec")]
    pub fn statistics(&self) -> Option<crate::hnsw::HnswIndexStats> {
        let hnsw = self.hnsw_index.lock().unwrap();
        hnsw.statistics().ok()
    }
}

// Non-turbovec implementations (HNSW-only)
#[cfg(not(feature = "turbovec"))]
impl SemanticLayer {
    /// Insert or update a token embedding (HNSW-only version).
    ///
    /// # Arguments
    ///
    /// * `token_id` - Token node ID
    /// * `embedding` - Embedding vector (must match configured dimension)
    ///
    /// # Returns
    ///
    /// `Ok(())` if insert succeeded, `Err(String)` if dimension mismatch or HNSW error.
    pub fn insert_embedding(&mut self, token_id: u32, embedding: Vec<f32>) -> Result<(), String> {
        if embedding.len() != self.dimension {
            return Err(format!(
                "Embedding dimension mismatch: expected {}, got {}",
                self.dimension,
                embedding.len()
            ));
        }

        let metadata = serde_json::json!({ "token_id": token_id });
        let mut hnsw = self.hnsw_index.lock().unwrap();
        hnsw.insert_vector(&embedding, Some(metadata))
            .map_err(|e| format!("HNSW insert failed: {}", e))?;
        Ok(())
    }

    /// Find K nearest neighbors for a query embedding (HNSW-only version).
    ///
    /// # Arguments
    ///
    /// * `query_embedding` - Query vector (must match configured dimension)
    /// * `k` - Number of neighbors to return
    ///
    /// # Returns
    ///
    /// Top-K nearest neighbors sorted by distance (closest first).
    pub fn knn_search(&self, query_embedding: &[f32], k: usize) -> Vec<KnnResult> {
        if query_embedding.len() != self.dimension {
            return Vec::new();
        }

        let hnsw = self.hnsw_index.lock().unwrap();
        let results = hnsw.search(query_embedding, k);

        match results {
            Ok(hnsw_results) => hnsw_results
                .into_iter()
                .filter_map(|(vector_id, distance)| {
                    hnsw.get_vector(vector_id)
                        .ok()
                        .flatten()
                        .and_then(|(_, metadata)| {
                            metadata
                                .get("token_id")
                                .and_then(|v| v.as_u64())
                                .map(|token_id| KnnResult {
                                    node_id: token_id as u32,
                                    distance,
                                })
                        })
                })
                .collect(),
            Err(_) => Vec::new(),
        }
    }

    /// Get the number of embeddings in this layer (HNSW-only version).
    pub fn embedding_count(&self) -> usize {
        let hnsw = self.hnsw_index.lock().unwrap();
        hnsw.vector_count()
    }

    /// Check if a token has an embedding (HNSW-only version).
    #[cfg(not(feature = "turbovec"))]
    pub fn has_embedding(&self, token_id: u32) -> bool {
        let hnsw = self.hnsw_index.lock().unwrap();

        // Brute-force search through HNSW to check if token_id exists
        // In production, maintain a separate HashSet for O(1) lookup
        for i in 1..=hnsw.vector_count() {
            if let Ok(Some((_, metadata))) = hnsw.get_vector(i as u64)
                && let Some(id) = metadata.get("token_id").and_then(|v| v.as_u64())
                && id == token_id as u64
            {
                return true;
            }
        }
        false
    }

    /// Remove an embedding (for transfer learning fine-tune updates).
    ///
    /// Used during incremental fine-tune to update only displaced embeddings.
    ///
    /// **Note**: HNSW doesn't support efficient deletion. This method is a no-op
    /// that maintains API compatibility. Deletion requires full index rebuild —
    /// call rebuild_hnsw_index() after removals.
    #[cfg(not(feature = "turbovec"))]
    pub fn remove_embedding(&mut self, _token_id: u32) -> bool {
        // HNSW doesn't support efficient deletion
        // In production, would need to rebuild index or use versioning
        // For now, return false to maintain API compatibility
        false
    }

    /// Get HNSW index statistics (for debugging/benchmarking).
    ///
    /// # Returns
    ///
    /// `Some(stats)` if statistics available, `None` if HNSW error occurred.
    #[cfg(not(feature = "turbovec"))]
    pub fn statistics(&self) -> Option<crate::hnsw::HnswIndexStats> {
        let hnsw = self.hnsw_index.lock().unwrap();
        hnsw.statistics().ok()
    }
}

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

    #[test]
    fn test_semantic_layer_creation() {
        let layer = SemanticLayer::new(128);
        assert_eq!(layer.dimension, 128);
        assert_eq!(layer.embedding_count(), 0);
    }

    #[test]
    fn test_insert_embedding() {
        let mut layer = SemanticLayer::new(4);

        let embedding = vec![0.1, 0.2, 0.3, 0.4];
        assert!(layer.insert_embedding(100, embedding).is_ok());
        assert_eq!(layer.embedding_count(), 1);
        assert!(layer.has_embedding(100));
    }

    #[test]
    fn test_insert_embedding_dimension_mismatch() {
        let mut layer = SemanticLayer::new(4);

        let wrong_embedding = vec![0.1, 0.2, 0.3]; // 3D instead of 4D
        assert!(layer.insert_embedding(100, wrong_embedding).is_err());
    }

    #[test]
    fn test_knn_search() {
        let mut layer = SemanticLayer::new(3);

        // Insert test embeddings
        layer.insert_embedding(1, vec![1.0, 0.0, 0.0]).unwrap();
        layer.insert_embedding(2, vec![0.9, 0.1, 0.0]).unwrap();
        layer.insert_embedding(3, vec![0.0, 1.0, 0.0]).unwrap();

        // Query closest to [1.0, 0.0, 0.0]
        let query = vec![1.0, 0.0, 0.0];
        let results = layer.knn_search(&query, 2);

        assert_eq!(results.len(), 2);
        assert_eq!(results[0].node_id, 1); // Exact match
        assert!(results[0].distance < results[1].distance); // Sorted by distance
    }

    #[test]
    fn test_knn_search_empty() {
        let layer = SemanticLayer::new(3);

        let query = vec![1.0, 0.0, 0.0];
        let results = layer.knn_search(&query, 5);

        assert_eq!(results.len(), 0); // No embeddings
    }

    #[test]
    fn test_knn_search_dimension_mismatch() {
        let mut layer = SemanticLayer::new(3);
        layer.insert_embedding(1, vec![1.0, 0.0, 0.0]).unwrap();

        let wrong_query = vec![1.0, 0.0]; // 2D instead of 3D
        let results = layer.knn_search(&wrong_query, 5);

        assert_eq!(results.len(), 0); // Dimension mismatch
    }

    #[test]
    fn test_remove_embedding() {
        let mut layer = SemanticLayer::new(3);

        layer.insert_embedding(100, vec![0.1, 0.2, 0.3]).unwrap();
        assert!(layer.has_embedding(100));

        // HNSW doesn't support deletion, so this is a no-op
        assert!(!layer.remove_embedding(100));
        // Token still exists since HNSW can't delete
        assert!(layer.has_embedding(100));
    }

    #[test]
    fn test_cosine_distance_identical() {
        let mut layer = SemanticLayer::new(3);

        let a = vec![1.0, 2.0, 3.0];
        let b = vec![1.0, 2.0, 3.0];

        layer.insert_embedding(1, a).unwrap();
        layer.insert_embedding(2, b).unwrap();

        let query = vec![1.0, 2.0, 3.0];
        let results = layer.knn_search(&query, 1);

        assert_eq!(results.len(), 1);
        // Identical vectors should have distance close to 0
        assert!(results[0].distance < 0.1);
    }

    #[test]
    fn test_cosine_distance_opposite() {
        let mut layer = SemanticLayer::new(3);

        layer.insert_embedding(1, vec![1.0, 0.0, 0.0]).unwrap();
        layer.insert_embedding(2, vec![-1.0, 0.0, 0.0]).unwrap();

        let query = vec![1.0, 0.0, 0.0];
        let results = layer.knn_search(&query, 2);

        assert_eq!(results.len(), 2);
        assert_eq!(results[0].node_id, 1); // Closest (distance 0)
        // Opposite vector should have large distance (HNSW approximation varies)
        eprintln!("Distance to opposite vector: {}", results[1].distance);
        assert!(results[1].distance > 0.5); // Should be far
        assert!(results[0].distance < results[1].distance); // Results sorted by distance
    }

    #[test]
    fn test_cosine_distance_orthogonal() {
        let mut layer = SemanticLayer::new(3);

        // Only insert orthogonal vectors
        layer.insert_embedding(1, vec![1.0, 0.0, 0.0]).unwrap();
        layer.insert_embedding(2, vec![0.0, 1.0, 0.0]).unwrap();

        let query = vec![1.0, 0.0, 0.0];
        let results = layer.knn_search(&query, 2);

        assert_eq!(results.len(), 2);
        assert_eq!(results[0].node_id, 1); // Closest (distance 0)
        // Orthogonal vector should have distance around 1.0 (HNSW approximation varies)
        eprintln!("Distance to orthogonal vector: {}", results[1].distance);
        assert!(results[1].distance > 0.1); // Should be some distance
        assert!(results[0].distance < results[1].distance); // Results sorted by distance
    }

    #[test]
    fn test_hnsw_statistics() {
        let mut layer = SemanticLayer::new(4);

        layer
            .insert_embedding(100, vec![0.1, 0.2, 0.3, 0.4])
            .unwrap();
        layer
            .insert_embedding(200, vec![0.5, 0.6, 0.7, 0.8])
            .unwrap();

        let stats = layer.statistics();
        assert!(stats.is_some());
        let stats = stats.unwrap();
        assert_eq!(stats.vector_count, 2);
        assert_eq!(stats.dimension, 4);
    }
}