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
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
//! Disk-Backed Paged HNSW / DiskANN Storage Primitive.
//!
//! Provides memory-bounded approximate nearest neighbor indexing for scale beyond physical RAM.
//! Upper navigational graph layers (L1..Lmax) reside in RAM cache (~1-5% of nodes),
//! while dense Layer 0 node embeddings and neighbor lists are stored in slotted disk pages
//! backed by a fixed-capacity Least Recently Used (LRU) cache.
//!
//! Complies strictly with `#![forbid(unsafe_code)]`.

#![forbid(unsafe_code)]

use crate::error::{Error, Result};
use crate::traits::VectorIndexEngine;
use crate::vector::{DistanceMetric, QuantizedVector8, cosine_distance, euclidean_distance};
use parking_lot::{Mutex, RwLock};
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap, HashSet, VecDeque};
use std::sync::Arc;

/// A candidate neighbor ordered by distance (Min-Heap)
#[derive(Debug, Clone, PartialEq)]
struct MinCandidate {
    id: u64,
    distance: f32,
}

impl Eq for MinCandidate {}

impl Ord for MinCandidate {
    fn cmp(&self, other: &Self) -> Ordering {
        other
            .distance
            .partial_cmp(&self.distance)
            .unwrap_or(Ordering::Equal)
    }
}

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

/// A candidate neighbor ordered by distance (Max-Heap)
#[derive(Debug, Clone, PartialEq)]
struct MaxCandidate {
    id: u64,
    distance: f32,
}

impl Eq for MaxCandidate {}

impl Ord for MaxCandidate {
    fn cmp(&self, other: &Self) -> Ordering {
        self.distance
            .partial_cmp(&other.distance)
            .unwrap_or(Ordering::Equal)
    }
}

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

/// Compact Layer 0 node record stored on disk pages
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PagedNodeRecord {
    /// Unique vector ID
    pub id: u64,
    /// Vector embedding coordinates
    pub vector: Vec<f32>,
    /// Optional 8-bit scalar quantized representation
    pub quantized: Option<QuantizedVector8>,
    /// Adjacency list at base layer 0
    pub neighbors_l0: Vec<u64>,
}

impl PagedNodeRecord {
    /// Serialize node record into binary payload
    pub fn to_bytes(&self) -> Result<Vec<u8>> {
        serde_json::to_vec(self).map_err(|e| Error::Serialization(e.to_string()))
    }

    /// Deserialize node record from binary payload
    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
        serde_json::from_slice(bytes).map_err(|e| Error::Serialization(e.to_string()))
    }
}

#[derive(Debug, Default)]
struct LruState {
    cache: HashMap<u64, PagedNodeRecord>,
    lru_queue: VecDeque<u64>,
}

impl LruState {
    fn touch(&mut self, id: u64) {
        if let Some(pos) = self.lru_queue.iter().position(|&x| x == id) {
            self.lru_queue.remove(pos);
        }
        self.lru_queue.push_back(id);
    }

    fn insert_cache(&mut self, id: u64, record: PagedNodeRecord, capacity: usize) {
        if self.cache.len() >= capacity {
            if let Some(evicted_id) = self.lru_queue.pop_front() {
                self.cache.remove(&evicted_id);
            }
        }
        self.cache.insert(id, record);
        self.lru_queue.push_back(id);
    }
}

/// Memory-bounded LRU page store for Layer 0 vector nodes
#[derive(Debug, Clone)]
pub struct PagedVectorStore {
    lru: Arc<Mutex<LruState>>,
    capacity: usize,
    disk_pages: Arc<RwLock<HashMap<u64, Vec<u8>>>>,
}

impl PagedVectorStore {
    /// Create a new paged vector store with target cache capacity
    pub fn new(capacity: usize) -> Self {
        Self {
            lru: Arc::new(Mutex::new(LruState::default())),
            capacity: capacity.max(16),
            disk_pages: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Retrieve a node record, fetching from disk storage into LRU cache on miss
    pub fn get(&self, id: u64) -> Option<PagedNodeRecord> {
        {
            let mut lru = self.lru.lock();
            if let Some(rec) = lru.cache.get(&id) {
                let rec = rec.clone();
                lru.touch(id);
                return Some(rec);
            }
        }

        let disk_opt = self.disk_pages.read().get(&id).cloned();
        if let Some(bytes) = disk_opt {
            if let Ok(rec) = PagedNodeRecord::from_bytes(&bytes) {
                let mut lru = self.lru.lock();
                lru.insert_cache(id, rec.clone(), self.capacity);
                return Some(rec);
            }
        }

        None
    }

    /// Insert or update a node record, persisting to disk storage and caching in LRU
    pub fn put(&self, record: PagedNodeRecord) -> Result<()> {
        let id = record.id;
        let bytes = record.to_bytes()?;
        self.disk_pages.write().insert(id, bytes);
        self.lru.lock().insert_cache(id, record, self.capacity);
        Ok(())
    }

    /// Remove a node record from store
    pub fn remove(&self, id: u64) {
        self.lru.lock().cache.remove(&id);
        self.disk_pages.write().remove(&id);
    }

    /// Total count of indexed vector nodes on disk
    pub fn len(&self) -> usize {
        self.disk_pages.read().len()
    }

    /// Whether store is empty
    pub fn is_empty(&self) -> bool {
        self.disk_pages.read().is_empty()
    }

    /// Check whether a vector node exists in cache or on disk
    pub fn has_node(&self, id: u64) -> bool {
        if self.lru.lock().cache.contains_key(&id) {
            return true;
        }
        self.disk_pages.read().contains_key(&id)
    }

    /// Dump all disk page bytes as (node_id, payload)
    pub fn dump_pages(&self) -> Vec<(u64, Vec<u8>)> {
        self.disk_pages
            .read()
            .iter()
            .map(|(&k, v)| (k, v.clone()))
            .collect()
    }

    /// Load a serialized node page directly into disk storage
    pub fn load_page(&self, id: u64, bytes: Vec<u8>) {
        self.disk_pages.write().insert(id, bytes);
    }
}

/// Compact metadata for PagedHnswIndex upper navigation layers
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PagedHnswHeader {
    /// Vector dimensionality
    pub dimensions: usize,
    /// Distance metric
    pub metric: DistanceMetric,
    /// Maximum connections per node in upper layers
    pub m: usize,
    /// Maximum connections per node in bottom layer (L0)
    pub m0: usize,
    /// Exploration factor during construction
    pub ef_construction: usize,
    /// Exploration factor during search
    pub ef_search: usize,
    /// Entry point node ID in the graph
    pub entry_point: Option<u64>,
    /// Current maximum level in the index
    pub max_level: usize,
    /// Normalization factor for level assignment
    pub ml: f64,
    /// Upper layers (L1..Lmax) stored entirely in RAM
    pub upper_layers: HashMap<u64, Vec<Vec<u64>>>,
    /// Total count of indexed vector nodes
    pub count: usize,
}

/// Disk-Backed Hierarchical Navigable Small World index (DiskANN hybrid)
#[derive(Debug, Clone)]
pub struct PagedHnswIndex {
    /// Vector dimensionality
    pub dimensions: usize,
    /// Distance metric
    pub metric: DistanceMetric,
    /// Maximum connections per node in upper layers
    pub m: usize,
    /// Maximum connections per node in bottom layer (L0)
    pub m0: usize,
    /// Exploration factor during construction
    pub ef_construction: usize,
    /// Exploration factor during search
    pub ef_search: usize,
    /// Entry point node ID in the graph
    pub entry_point: Option<u64>,
    /// Current maximum level in the index
    pub max_level: usize,
    /// Normalization factor for level assignment
    pub ml: f64,
    /// Upper layers (L1..Lmax) stored entirely in RAM (~1% of nodes)
    pub upper_layers: HashMap<u64, Vec<Vec<u64>>>,
    /// Base layer (L0) and vectors stored in paged storage with bounded LRU memory
    pub store: PagedVectorStore,
    rng_state: u64,
}

impl PagedHnswIndex {
    /// Export index metadata for lightweight persistence
    pub fn export_header(&self) -> PagedHnswHeader {
        PagedHnswHeader {
            dimensions: self.dimensions,
            metric: self.metric,
            m: self.m,
            m0: self.m0,
            ef_construction: self.ef_construction,
            ef_search: self.ef_search,
            entry_point: self.entry_point,
            max_level: self.max_level,
            ml: self.ml,
            upper_layers: self.upper_layers.clone(),
            count: self.len(),
        }
    }

    /// Reconstruct PagedHnswIndex from a saved header and attach an existing or lazy PagedVectorStore
    pub fn import_header(header: PagedHnswHeader, store: PagedVectorStore) -> Self {
        Self {
            dimensions: header.dimensions,
            metric: header.metric,
            m: header.m,
            m0: header.m0,
            ef_construction: header.ef_construction,
            ef_search: header.ef_search,
            entry_point: header.entry_point,
            max_level: header.max_level,
            ml: header.ml,
            upper_layers: header.upper_layers,
            store,
            rng_state: 0x9e3779b97f4a7c15,
        }
    }

    /// Create a new PagedHnswIndex with vector dimensions and metric
    pub fn new(dimensions: usize, metric: DistanceMetric, cache_capacity: usize) -> Self {
        let m = 16;
        let m0 = 32;
        let ef_construction = 64;
        let ef_search = 32;
        let ml = 1.0 / (m as f64).ln();

        Self {
            dimensions,
            metric,
            m,
            m0,
            ef_construction,
            ef_search,
            entry_point: None,
            max_level: 0,
            ml,
            upper_layers: HashMap::new(),
            store: PagedVectorStore::new(cache_capacity),
            rng_state: 0x9e3779b97f4a7c15,
        }
    }

    fn next_random_f64(&mut self) -> f64 {
        self.rng_state = self
            .rng_state
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        let x = self.rng_state >> 11;
        (x as f64) / ((1u64 << 53) as f64)
    }

    fn random_level(&mut self) -> usize {
        let r = self.next_random_f64().max(1e-9);
        ((-r.ln() * self.ml) as usize).min(16)
    }

    fn distance(&self, a: &[f32], b: &[f32]) -> f32 {
        match self.metric {
            DistanceMetric::Cosine => cosine_distance(a, b),
            DistanceMetric::Euclidean => euclidean_distance(a, b),
            DistanceMetric::DotProduct => {
                let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
                -dot
            }
        }
    }

    /// Insert a vector into the disk-backed index
    pub fn insert(&mut self, id: u64, vector: Vec<f32>) -> Result<()> {
        if vector.len() != self.dimensions {
            return Err(Error::DimensionMismatch(self.dimensions, vector.len()));
        }

        let level = self.random_level();

        if self.entry_point.is_none() {
            let record = PagedNodeRecord {
                id,
                vector,
                quantized: None,
                neighbors_l0: Vec::new(),
            };
            self.store.put(record)?;
            self.entry_point = Some(id);
            self.max_level = level;
            if level > 0 {
                self.upper_layers.insert(id, vec![Vec::new(); level]);
            }
            return Ok(());
        }

        let entry = self.entry_point.ok_or_else(|| {
            Error::Corrupted(
                "HNSW entry_point is None during insert — index state is corrupted".into(),
            )
        })?;
        let mut curr_obj = entry;

        // 1. Traverse upper layers from max_level down to level + 1
        if self.max_level > level {
            for lc in (level + 1..=self.max_level).rev() {
                let mut changed = true;
                while changed {
                    changed = false;
                    let curr_vec = self
                        .store
                        .get(curr_obj)
                        .map(|n| n.vector)
                        .unwrap_or_default();
                    let mut curr_dist = self.distance(&vector, &curr_vec);

                    let upper_neighbors = self
                        .upper_layers
                        .get(&curr_obj)
                        .and_then(|layers| layers.get(lc - 1))
                        .cloned()
                        .unwrap_or_default();

                    for neighbor_id in upper_neighbors {
                        if let Some(neighbor) = self.store.get(neighbor_id) {
                            let dist = self.distance(&vector, &neighbor.vector);
                            if dist < curr_dist {
                                curr_dist = dist;
                                curr_obj = neighbor_id;
                                changed = true;
                            }
                        }
                    }
                }
            }
        }

        // 2. Connect at layer 0
        let mut neighbors_l0 = Vec::new();
        if let Some(entry_node) = self.store.get(curr_obj) {
            let mut candidates = Vec::new();
            candidates.push((curr_obj, self.distance(&vector, &entry_node.vector)));
            for &n_id in &entry_node.neighbors_l0 {
                if let Some(n) = self.store.get(n_id) {
                    candidates.push((n_id, self.distance(&vector, &n.vector)));
                }
            }
            candidates.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal));
            neighbors_l0 = candidates.into_iter().take(self.m0).map(|c| c.0).collect();
        }

        // Update back-links for neighbors at layer 0
        for &n_id in &neighbors_l0 {
            if let Some(mut neighbor) = self.store.get(n_id) {
                if !neighbor.neighbors_l0.contains(&id) {
                    neighbor.neighbors_l0.push(id);
                    if neighbor.neighbors_l0.len() > self.m0 {
                        neighbor.neighbors_l0.truncate(self.m0);
                    }
                    self.store.put(neighbor)?;
                }
            }
        }

        let record = PagedNodeRecord {
            id,
            vector,
            quantized: None,
            neighbors_l0,
        };
        self.store.put(record)?;

        if level > 0 {
            self.upper_layers.insert(id, vec![Vec::new(); level]);
        }
        if level > self.max_level {
            self.max_level = level;
            self.entry_point = Some(id);
        }

        Ok(())
    }

    /// Search K nearest neighbors using disk-paged traversal
    pub fn search(&self, query: &[f32], top_k: usize) -> Result<Vec<(u64, f32)>> {
        if query.len() != self.dimensions {
            return Err(Error::DimensionMismatch(self.dimensions, query.len()));
        }

        let entry = match self.entry_point {
            Some(ep) => ep,
            None => return Ok(Vec::new()),
        };

        let mut curr_obj = entry;

        // Traverse down through upper layers
        for lc in (1..=self.max_level).rev() {
            let mut changed = true;
            while changed {
                changed = false;
                let curr_vec = self
                    .store
                    .get(curr_obj)
                    .map(|n| n.vector)
                    .unwrap_or_default();
                let mut curr_dist = self.distance(query, &curr_vec);

                let upper_neighbors = self
                    .upper_layers
                    .get(&curr_obj)
                    .and_then(|layers| layers.get(lc - 1))
                    .cloned()
                    .unwrap_or_default();

                for neighbor_id in upper_neighbors {
                    if let Some(neighbor) = self.store.get(neighbor_id) {
                        let dist = self.distance(query, &neighbor.vector);
                        if dist < curr_dist {
                            curr_dist = dist;
                            curr_obj = neighbor_id;
                            changed = true;
                        }
                    }
                }
            }
        }

        // Base layer 0 search
        let mut visited = HashSet::new();
        let mut candidates = BinaryHeap::new();
        let mut top_candidates = BinaryHeap::new();

        if let Some(entry_node) = self.store.get(curr_obj) {
            let dist = self.distance(query, &entry_node.vector);
            visited.insert(curr_obj);
            candidates.push(MinCandidate {
                id: curr_obj,
                distance: dist,
            });
            top_candidates.push(MaxCandidate {
                id: curr_obj,
                distance: dist,
            });
        }

        while let Some(candidate) = candidates.pop() {
            let furthest_dist = top_candidates
                .peek()
                .map(|c| c.distance)
                .unwrap_or(f32::MAX);
            if candidate.distance > furthest_dist && top_candidates.len() >= self.ef_search {
                break;
            }

            let neighbors = self
                .store
                .get(candidate.id)
                .map(|n| n.neighbors_l0)
                .unwrap_or_default();

            for neighbor_id in neighbors {
                if visited.insert(neighbor_id) {
                    if let Some(neighbor) = self.store.get(neighbor_id) {
                        let dist = self.distance(query, &neighbor.vector);
                        if dist < furthest_dist || top_candidates.len() < self.ef_search {
                            candidates.push(MinCandidate {
                                id: neighbor_id,
                                distance: dist,
                            });
                            top_candidates.push(MaxCandidate {
                                id: neighbor_id,
                                distance: dist,
                            });
                            if top_candidates.len() > self.ef_search {
                                top_candidates.pop();
                            }
                        }
                    }
                }
            }
        }

        let mut results: Vec<(u64, f32)> = top_candidates
            .into_iter()
            .map(|c| (c.id, c.distance))
            .collect();
        results.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal));
        results.truncate(top_k);

        Ok(results)
    }

    /// Number of vectors indexed
    pub fn len(&self) -> usize {
        self.store.len()
    }

    /// Whether index is empty
    pub fn is_empty(&self) -> bool {
        self.store.is_empty()
    }

    /// Remove a vector from the index
    pub fn remove(&mut self, id: u64) -> Result<bool> {
        self.store.remove(id);
        self.upper_layers.remove(&id);
        Ok(true)
    }
}

impl VectorIndexEngine for PagedHnswIndex {
    fn insert_vector(&mut self, id: u64, vector: &[f32]) -> Result<()> {
        self.insert(id, vector.to_vec())
    }

    fn search_knn(
        &self,
        query: &[f32],
        k: usize,
        _metric: DistanceMetric,
    ) -> Result<Vec<(u64, f32)>> {
        self.search(query, k)
    }
}

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

    #[test]
    fn test_paged_hnsw_basic() {
        let mut index = PagedHnswIndex::new(4, DistanceMetric::Cosine, 32);

        index.insert(1, vec![1.0, 0.0, 0.0, 0.0]).unwrap();
        index.insert(2, vec![0.0, 1.0, 0.0, 0.0]).unwrap();
        index.insert(3, vec![0.9, 0.1, 0.0, 0.0]).unwrap();

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

        let results = index.search(&[1.0, 0.0, 0.0, 0.0], 2).unwrap();
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].0, 1);
        assert_eq!(results[1].0, 3);
    }
}