tapirus 1.2.1

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
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
//! 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};
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>,
}

const PAGED_NODE_MAGIC: &[u8; 4] = b"PHN1";

impl PagedNodeRecord {
    /// Serialize node record into a compact binary payload (`PHN1`).
    pub fn to_bytes(&self) -> Result<Vec<u8>> {
        let mut out = Vec::with_capacity(
            24 + self.vector.len() * 4
                + self
                    .quantized
                    .as_ref()
                    .map_or(0, |q| 16 + q.quantized.len())
                + self.neighbors_l0.len() * 8,
        );
        out.extend_from_slice(PAGED_NODE_MAGIC);
        out.extend_from_slice(&self.id.to_le_bytes());
        out.extend_from_slice(&(self.vector.len() as u32).to_le_bytes());
        for &v in &self.vector {
            out.extend_from_slice(&v.to_le_bytes());
        }
        match &self.quantized {
            Some(q) => {
                out.push(1);
                out.extend_from_slice(&q.min_val.to_le_bytes());
                out.extend_from_slice(&q.max_val.to_le_bytes());
                out.extend_from_slice(&q.step.to_le_bytes());
                out.extend_from_slice(&(q.quantized.len() as u32).to_le_bytes());
                out.extend_from_slice(&q.quantized);
            }
            None => out.push(0),
        }
        out.extend_from_slice(&(self.neighbors_l0.len() as u32).to_le_bytes());
        for &n in &self.neighbors_l0 {
            out.extend_from_slice(&n.to_le_bytes());
        }
        Ok(out)
    }

    /// Deserialize node record from compact binary (`PHN1`) or legacy JSON payload.
    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
        if !bytes.starts_with(PAGED_NODE_MAGIC) {
            return serde_json::from_slice(bytes).map_err(|e| Error::Serialization(e.to_string()));
        }
        let corrupt = |m: &str| Error::Serialization(format!("Corrupted PagedNodeRecord: {m}"));
        if bytes.len() < 16 {
            return Err(corrupt("truncated header"));
        }
        let id = u64::from_le_bytes(bytes[4..12].try_into().map_err(|_| corrupt("id"))?);
        let vec_len =
            u32::from_le_bytes(bytes[12..16].try_into().map_err(|_| corrupt("vec_len"))?) as usize;
        let mut pos = 16usize;
        let vec_bytes_end = pos
            .checked_add(vec_len.saturating_mul(4))
            .ok_or_else(|| corrupt("vec_len overflow"))?;
        if vec_bytes_end > bytes.len() {
            return Err(corrupt("truncated vector"));
        }
        let mut vector = Vec::with_capacity(vec_len);
        for i in 0..vec_len {
            let off = pos + i * 4;
            vector.push(f32::from_le_bytes([
                bytes[off],
                bytes[off + 1],
                bytes[off + 2],
                bytes[off + 3],
            ]));
        }
        pos = vec_bytes_end;

        let has_q = *bytes.get(pos).ok_or_else(|| corrupt("quantized flag"))?;
        pos += 1;
        let quantized = if has_q == 1 {
            if pos + 16 > bytes.len() {
                return Err(corrupt("truncated sq8 header"));
            }
            let min_val =
                f32::from_le_bytes([bytes[pos], bytes[pos + 1], bytes[pos + 2], bytes[pos + 3]]);
            let max_val = f32::from_le_bytes([
                bytes[pos + 4],
                bytes[pos + 5],
                bytes[pos + 6],
                bytes[pos + 7],
            ]);
            let step = f32::from_le_bytes([
                bytes[pos + 8],
                bytes[pos + 9],
                bytes[pos + 10],
                bytes[pos + 11],
            ]);
            let qlen = u32::from_le_bytes([
                bytes[pos + 12],
                bytes[pos + 13],
                bytes[pos + 14],
                bytes[pos + 15],
            ]) as usize;
            pos += 16;
            let qend = pos
                .checked_add(qlen)
                .ok_or_else(|| corrupt("qlen overflow"))?;
            let qbytes = bytes
                .get(pos..qend)
                .ok_or_else(|| corrupt("truncated sq8"))?;
            pos = qend;
            Some(crate::vector::QuantizedVector8 {
                quantized: qbytes.to_vec(),
                min_val,
                max_val,
                step,
            })
        } else if has_q == 0 {
            None
        } else {
            return Err(corrupt("invalid quantized flag"));
        };

        if pos + 4 > bytes.len() {
            return Err(corrupt("truncated neighbors len"));
        }
        let nlen = u32::from_le_bytes([bytes[pos], bytes[pos + 1], bytes[pos + 2], bytes[pos + 3]])
            as usize;
        pos += 4;
        let nend = pos
            .checked_add(nlen.saturating_mul(8))
            .ok_or_else(|| corrupt("neighbors overflow"))?;
        if nend != bytes.len() {
            return Err(corrupt("neighbors size mismatch"));
        }
        let mut neighbors_l0 = Vec::with_capacity(nlen);
        for i in 0..nlen {
            let off = pos + i * 8;
            neighbors_l0.push(u64::from_le_bytes([
                bytes[off],
                bytes[off + 1],
                bytes[off + 2],
                bytes[off + 3],
                bytes[off + 4],
                bytes[off + 5],
                bytes[off + 6],
                bytes[off + 7],
            ]));
        }

        Ok(Self {
            id,
            vector,
            quantized,
            neighbors_l0,
        })
    }
}

#[derive(Debug)]
struct LruEntry {
    record: PagedNodeRecord,
    prev: Option<u64>,
    next: Option<u64>,
}

/// O(1) Doubly-Linked LRU cache keyed by vector node ID (head = LRU, tail = MRU).
#[derive(Debug, Default)]
struct LruState {
    entries: HashMap<u64, LruEntry>,
    head: Option<u64>,
    tail: Option<u64>,
}

impl LruState {
    fn contains_key(&self, id: &u64) -> bool {
        self.entries.contains_key(id)
    }

    fn detach(&mut self, id: u64) -> Option<(Option<u64>, Option<u64>)> {
        let entry = self.entries.get(&id)?;
        let prev = entry.prev;
        let next = entry.next;
        if let Some(p) = prev {
            if let Some(pe) = self.entries.get_mut(&p) {
                pe.next = next;
            }
        } else {
            self.head = next;
        }
        if let Some(n) = next {
            if let Some(ne) = self.entries.get_mut(&n) {
                ne.prev = prev;
            }
        } else {
            self.tail = prev;
        }
        if let Some(e) = self.entries.get_mut(&id) {
            e.prev = None;
            e.next = None;
        }
        Some((prev, next))
    }

    fn attach_tail(&mut self, id: u64) {
        let old_tail = self.tail;
        if let Some(e) = self.entries.get_mut(&id) {
            e.prev = old_tail;
            e.next = None;
        }
        if let Some(t) = old_tail {
            if let Some(te) = self.entries.get_mut(&t) {
                te.next = Some(id);
            }
        } else {
            self.head = Some(id);
        }
        self.tail = Some(id);
    }

    fn touch(&mut self, id: u64) {
        if self.tail == Some(id) || !self.entries.contains_key(&id) {
            return;
        }
        self.detach(id);
        self.attach_tail(id);
    }

    fn remove(&mut self, id: u64) {
        if self.entries.contains_key(&id) {
            self.detach(id);
            self.entries.remove(&id);
        }
    }

    fn insert_cache(&mut self, id: u64, record: PagedNodeRecord, capacity: usize) {
        if let Some(existing) = self.entries.get_mut(&id) {
            existing.record = record;
            self.touch(id);
            return;
        }
        if self.entries.len() >= capacity
            && let Some(evicted_id) = self.head
        {
            self.remove(evicted_id);
        }
        self.entries.insert(
            id,
            LruEntry {
                record,
                prev: None,
                next: None,
            },
        );
        self.attach_tail(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.entries.get(&id).map(|e| e.record.clone()) {
                lru.touch(id);
                return Some(rec);
            }
        }

        let disk_opt = self.disk_pages.read().get(&id).cloned();
        if let Some(bytes) = disk_opt
            && 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().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().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)
                && !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)
                    && 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);
    }
}