tapirus 1.0.4

Embedded multi-model database with SQL, vector search and local AI memory
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
//! # High-Performance Graph-to-Vector & Vector-to-Graph Chaining Pipeline
//!
//! Provides an ultra-fast, zero-allocation fluent query builder for traversing
//! entity relationships and executing vector similarity ranking over candidate neighborhoods.
//!
//! # Architecture
//! Rather than searching the global vector space ($O(N \log N)$), graph-vector chaining
//! restricts vector distance calculations strictly to the local graph neighborhood ($O(M \cdot D)$
//! where $M \ll N$, typically $M \in [10, 100]$). This enables sub-microsecond retrieval
//! with 100% exact Recall (zero approximation loss).

use crate::error::Result;
use crate::graph::{Direction, Edge, GraphEngine, Node};
use crate::vector::{DistanceMetric, cosine_similarity, euclidean_distance};
use std::collections::{BinaryHeap, HashMap, HashSet};

/// Traversal step configuration
#[derive(Debug, Clone)]
pub struct TraversalStep {
    /// Outgoing, Incoming, or Both
    pub direction: Direction,
    /// Optional edge label to follow (e.g. "KNOWS", "TREATS")
    pub edge_label: Option<String>,
    /// Minimum edge weight threshold
    pub min_weight: Option<f32>,
}

/// Filter condition applied to candidate nodes
#[derive(Debug, Clone)]
pub enum NodeFilter {
    /// Filter by exact or case-insensitive node label
    Label(String),
    /// Filter by key-value substring presence in properties JSON
    PropertyContains {
        /// The JSON property key to match
        key: String,
        /// The expected property value
        val: String,
    },
}

/// A matched result from a chained graph-vector query
#[derive(Debug, Clone, PartialEq)]
pub struct ChainMatch {
    /// The matched target node
    pub node: Node,
    /// Distance or similarity score (if ranking was performed)
    pub score: f32,
    /// The traversal path of edges leading to this node
    pub path: Vec<Edge>,
}

/// Min-Heap item for $O(M \log k)$ top-k candidate selection
#[derive(Debug)]
struct ScoredCandidate<'b> {
    node: &'b Node,
    score: f32,
    path: Vec<Edge>,
}

impl<'b> PartialEq for ScoredCandidate<'b> {
    fn eq(&self, other: &Self) -> bool {
        self.cmp(other).is_eq()
    }
}

impl<'b> Eq for ScoredCandidate<'b> {}

impl<'b> PartialOrd for ScoredCandidate<'b> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<'b> Ord for ScoredCandidate<'b> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // Reverse ordering keeps the lowest score at the top for eviction.
        other
            .score
            .total_cmp(&self.score)
            .then_with(|| other.node.id.cmp(&self.node.id))
    }
}

/// Fluent Graph-Vector Chaining Query Builder
pub struct GraphChain<'a> {
    graph: &'a GraphEngine,
    external_vectors: Option<&'a HashMap<u64, Vec<f32>>>,
    external_resolver: Option<&'a dyn Fn(u64) -> Option<Vec<f32>>>,
    start_nodes: Vec<u64>,
    steps: Vec<TraversalStep>,
    node_filters: Vec<NodeFilter>,
    max_hops: usize,
}

impl<'a> GraphChain<'a> {
    /// Initialize a new GraphChain from a set of starting node IDs
    pub fn new(graph: &'a GraphEngine, start_nodes: Vec<u64>) -> Self {
        Self {
            graph,
            external_vectors: None,
            external_resolver: None,
            start_nodes,
            steps: Vec::new(),
            node_filters: Vec::new(),
            max_hops: 10,
        }
    }

    /// Provide external vectors (e.g. from memory engine or table columns)
    pub fn with_external_vectors(mut self, vectors: &'a HashMap<u64, Vec<f32>>) -> Self {
        self.external_vectors = Some(vectors);
        self
    }

    /// Provide on-demand lazy vector resolver (e.g. from memory engine)
    pub fn with_vector_resolver(mut self, resolver: &'a dyn Fn(u64) -> Option<Vec<f32>>) -> Self {
        self.external_resolver = Some(resolver);
        self
    }

    /// Traverse outgoing edges: `(A) -[label]-> (B)`
    pub fn out<S: Into<String>>(mut self, label: Option<S>) -> Self {
        self.steps.push(TraversalStep {
            direction: Direction::Outgoing,
            edge_label: label.map(Into::into),
            min_weight: None,
        });
        self
    }

    /// Traverse incoming edges: `(A) <-[label]- (B)`
    pub fn in_<S: Into<String>>(mut self, label: Option<S>) -> Self {
        self.steps.push(TraversalStep {
            direction: Direction::Incoming,
            edge_label: label.map(Into::into),
            min_weight: None,
        });
        self
    }

    /// Traverse edges in either direction: `(A) --[label]-- (B)`
    pub fn both<S: Into<String>>(mut self, label: Option<S>) -> Self {
        self.steps.push(TraversalStep {
            direction: Direction::Both,
            edge_label: label.map(Into::into),
            min_weight: None,
        });
        self
    }

    /// Set minimum relationship weight required for traversal
    pub fn min_weight(mut self, weight: f32) -> Self {
        if let Some(last) = self.steps.last_mut() {
            last.min_weight = Some(weight);
        }
        self
    }

    /// Filter candidate nodes by label
    pub fn filter_label<S: Into<String>>(mut self, label: S) -> Self {
        self.node_filters.push(NodeFilter::Label(label.into()));
        self
    }

    /// Filter candidate nodes by property presence
    pub fn filter_property<S1: Into<String>, S2: Into<String>>(mut self, key: S1, val: S2) -> Self {
        self.node_filters.push(NodeFilter::PropertyContains {
            key: key.into(),
            val: val.into(),
        });
        self
    }

    /// Set maximum traversal hop depth
    pub fn max_hops(mut self, hops: usize) -> Self {
        self.max_hops = hops;
        self
    }

    /// Execute graph traversal and resolve all candidate paths
    fn resolve_candidates(&self) -> Vec<(&'a Node, Vec<Edge>)> {
        if self.start_nodes.is_empty() {
            return Vec::new();
        }

        // Current frontier of (&'a Node, PathOfEdges) - zero node clones!
        let mut frontier: Vec<(&'a Node, Vec<Edge>)> = self
            .start_nodes
            .iter()
            .filter_map(|&id| self.graph.get_node(id).map(|n| (n, Vec::new())))
            .collect();

        for step in &self.steps {
            let mut next_frontier = Vec::new();
            let mut visited_step = HashSet::new();

            for (curr_node, path) in frontier {
                let neighbors = self.graph.neighbor_refs(
                    curr_node.id,
                    step.direction,
                    step.edge_label.as_deref(),
                );

                for (neighbor, edge) in neighbors {
                    if let Some(min_w) = step.min_weight {
                        if edge.weight < min_w {
                            continue;
                        }
                    }

                    // Prevent immediate back-tracking cycles within the current step
                    if visited_step.insert((curr_node.id, neighbor.id, edge.id)) {
                        let mut new_path = path.clone();
                        new_path.push((*edge).clone());
                        next_frontier.push((neighbor, new_path));
                    }
                }
            }

            frontier = next_frontier;
            if frontier.is_empty() {
                break;
            }
        }

        // Apply node-level filters directly on borrowed nodes
        frontier
            .into_iter()
            .filter(|(node, _)| {
                for filter in &self.node_filters {
                    match filter {
                        NodeFilter::Label(expected) => {
                            if !node.label.eq_ignore_ascii_case(expected) {
                                return false;
                            }
                        }
                        NodeFilter::PropertyContains { key, val } => {
                            let needle = format!("\"{key}\":\"{val}\"");
                            let needle_unquoted = format!("\"{key}\":{val}");
                            if !node.properties.contains(&needle)
                                && !node.properties.contains(&needle_unquoted)
                            {
                                return false;
                            }
                        }
                    }
                }
                true
            })
            .collect()
    }

    /// Retrieve the vector representation of a node (borrowed or on-demand)
    fn get_node_vector<'b>(&'b self, node: &'b Node) -> Option<std::borrow::Cow<'b, [f32]>> {
        if let Some(ref v) = node.vector {
            return Some(std::borrow::Cow::Borrowed(v.as_slice()));
        }
        if let Some(ext) = self.external_vectors {
            if let Some(v) = ext.get(&node.id) {
                return Some(std::borrow::Cow::Borrowed(v.as_slice()));
            }
        }
        if let Some(ref resolver) = self.external_resolver {
            if let Some(v) = resolver(node.id) {
                return Some(std::borrow::Cow::Owned(v));
            }
        }
        None
    }

    /// Rank candidate neighborhood nodes by Vector Similarity using SIMD-unrolled metrics
    pub fn vector_near(
        &self,
        query_vec: &[f32],
        top_k: usize,
        metric: DistanceMetric,
    ) -> Result<Vec<ChainMatch>> {
        if top_k == 0 {
            return Ok(Vec::new());
        }

        let candidates = self.resolve_candidates();
        if candidates.is_empty() {
            return Ok(Vec::new());
        }

        let mut heap: BinaryHeap<ScoredCandidate<'a>> = BinaryHeap::with_capacity(top_k + 1);

        for (node, path) in candidates {
            if let Some(node_vec) = self.get_node_vector(node) {
                let s = node_vec.as_ref();
                if s.len() != query_vec.len() {
                    continue; // Skip dimension mismatch safely
                }

                let score = match metric {
                    DistanceMetric::Cosine => cosine_similarity(query_vec, s),
                    DistanceMetric::DotProduct => crate::vector::dot_product(query_vec, s),
                    DistanceMetric::Euclidean => -euclidean_distance(query_vec, s),
                };

                heap.push(ScoredCandidate { node, score, path });
                if heap.len() > top_k {
                    heap.pop();
                }
            }
        }

        // Drain heap and sort in descending order of similarity score
        let mut results: Vec<ChainMatch> = heap
            .into_iter()
            .map(|c| ChainMatch {
                node: (*c.node).clone(),
                score: c.score,
                path: c.path,
            })
            .collect();

        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        Ok(results)
    }

    /// Lexically rank candidate neighborhood nodes using BM25
    pub fn bm25_search(&self, query_text: &str, top_k: usize) -> Result<Vec<ChainMatch>> {
        if top_k == 0 {
            return Ok(Vec::new());
        }

        let candidates = self.resolve_candidates();
        if candidates.is_empty() {
            return Ok(Vec::new());
        }

        let query_tokens = crate::memory::bm25::tokenize(query_text);
        if query_tokens.is_empty() {
            return Ok(Vec::new());
        }

        let n = candidates.len() as f32;
        let mut doc_tokens_list = Vec::with_capacity(candidates.len());
        let mut total_len = 0usize;

        for (node, _) in &candidates {
            let text = format!("{} {}", node.label, node.properties);
            let toks = crate::memory::bm25::tokenize(&text);
            total_len += toks.len();
            doc_tokens_list.push(toks);
        }

        let avgdl = if n > 0.0 { total_len as f32 / n } else { 1.0 };
        let k1 = 1.2f32;
        let b = 0.75f32;

        let mut heap: BinaryHeap<ScoredCandidate<'a>> = BinaryHeap::with_capacity(top_k + 1);

        for (idx, (node, path)) in candidates.into_iter().enumerate() {
            let doc_tokens = &doc_tokens_list[idx];
            let doc_len = doc_tokens.len() as f32;

            let mut score = 0.0f32;
            for q_term in &query_tokens {
                let tf = doc_tokens.iter().filter(|t| *t == q_term).count() as f32;
                if tf > 0.0 {
                    let df = doc_tokens_list
                        .iter()
                        .filter(|d| d.contains(q_term))
                        .count() as f32;

                    let idf = ((n - df + 0.5) / (df + 0.5) + 1.0).ln().max(0.0);
                    let tf_norm =
                        (tf * (k1 + 1.0)) / (tf + k1 * (1.0 - b + b * (doc_len / avgdl.max(1.0))));
                    score += idf * tf_norm;
                }
            }

            if score > 0.0 {
                heap.push(ScoredCandidate { node, score, path });
                if heap.len() > top_k {
                    heap.pop();
                }
            }
        }

        let mut results: Vec<ChainMatch> = heap
            .into_iter()
            .map(|c| ChainMatch {
                node: (*c.node).clone(),
                score: c.score,
                path: c.path,
            })
            .collect();

        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        Ok(results)
    }

    /// Perform Hybrid GraphRAG Ranking (Vector Similarity + BM25 Lexical Matching) over candidate neighborhood
    pub fn hybrid_rank(
        &self,
        query_text: &str,
        query_vec: &[f32],
        top_k: usize,
        vector_weight: f32,
    ) -> Result<Vec<ChainMatch>> {
        if top_k == 0 {
            return Ok(Vec::new());
        }

        let candidates = self.resolve_candidates();
        if candidates.is_empty() {
            return Ok(Vec::new());
        }

        let vw = vector_weight.clamp(0.0, 1.0);
        let bw = 1.0 - vw;

        let query_tokens = crate::memory::bm25::tokenize(query_text);
        let n = candidates.len() as f32;
        let mut doc_tokens_list = Vec::with_capacity(candidates.len());
        let mut total_len = 0usize;

        for (node, _) in &candidates {
            let text = format!("{} {}", node.label, node.properties);
            let toks = crate::memory::bm25::tokenize(&text);
            total_len += toks.len();
            doc_tokens_list.push(toks);
        }

        let avgdl = if n > 0.0 { total_len as f32 / n } else { 1.0 };
        let k1 = 1.2f32;
        let b = 0.75f32;

        let mut heap: BinaryHeap<ScoredCandidate<'a>> = BinaryHeap::with_capacity(top_k + 1);

        for (idx, (node, path)) in candidates.into_iter().enumerate() {
            let mut bm25_raw = 0.0f32;
            if !query_tokens.is_empty() {
                let doc_tokens = &doc_tokens_list[idx];
                let doc_len = doc_tokens.len() as f32;

                for q_term in &query_tokens {
                    let tf = doc_tokens.iter().filter(|t| *t == q_term).count() as f32;
                    if tf > 0.0 {
                        let df = doc_tokens_list
                            .iter()
                            .filter(|d| d.contains(q_term))
                            .count() as f32;

                        let idf = ((n - df + 0.5) / (df + 0.5) + 1.0).ln().max(0.0);
                        let tf_norm = (tf * (k1 + 1.0))
                            / (tf + k1 * (1.0 - b + b * (doc_len / avgdl.max(1.0))));
                        bm25_raw += idf * tf_norm;
                    }
                }
            }

            let bm25_norm = if bm25_raw > 0.0 {
                bm25_raw / (bm25_raw + 1.0)
            } else {
                0.0
            };

            let vec_sim = if let Some(node_vec) = self.get_node_vector(node) {
                let s = node_vec.as_ref();
                if s.len() == query_vec.len() {
                    cosine_similarity(query_vec, s).max(0.0)
                } else {
                    0.0
                }
            } else {
                0.0
            };

            let combined_score = (vw * vec_sim) + (bw * bm25_norm);

            heap.push(ScoredCandidate {
                node,
                score: combined_score,
                path,
            });
            if heap.len() > top_k {
                heap.pop();
            }
        }

        let mut results: Vec<ChainMatch> = heap
            .into_iter()
            .map(|c| ChainMatch {
                node: (*c.node).clone(),
                score: c.score,
                path: c.path,
            })
            .collect();

        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        Ok(results)
    }

    /// Collect all surviving nodes without vector scoring
    pub fn collect_nodes(&self) -> Vec<Node> {
        let mut seen = HashSet::new();
        self.resolve_candidates()
            .into_iter()
            .map(|(n, _)| n)
            .filter(|n| seen.insert(n.id))
            .cloned()
            .collect()
    }

    /// Collect all surviving node IDs
    pub fn collect_ids(&self) -> Vec<u64> {
        let mut seen = HashSet::new();
        self.resolve_candidates()
            .into_iter()
            .map(|(n, _)| n.id)
            .filter(|&id| seen.insert(id))
            .collect()
    }

    /// Collect matches with empty score
    pub fn collect_matches(&self) -> Vec<ChainMatch> {
        self.resolve_candidates()
            .into_iter()
            .map(|(node, path)| ChainMatch {
                node: (*node).clone(),
                score: 1.0,
                path,
            })
            .collect()
    }
}