heliosdb-nano 4.0.0

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
//! HNSW index implementation for vector similarity search
//!
//! Uses the hnsw_rs library which implements the HNSW algorithm
//! from "Efficient and robust approximate nearest neighbor search using
//! Hierarchical Navigable Small World graphs" (<https://arxiv.org/abs/1603.09320>)

#![allow(clippy::similar_names)]
#![allow(unused_variables)]

use super::{DistanceMetric, Vector};
use crate::{Error, Result};
use hnsw_rs::prelude::*;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// HNSW index configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HnswConfig {
    /// Maximum number of connections per layer (M parameter)
    pub max_connections: usize,
    /// Size of the dynamic candidate list (ef_construction parameter)
    pub ef_construction: usize,
    /// Vector dimension
    pub dimension: usize,
    /// Distance metric
    pub distance_metric: DistanceMetric,
    /// Base ef_search parameter (dynamically adjusted at query time)
    pub ef_search_base: usize,
    /// Enable dynamic ef_search adjustment based on k and index size
    pub dynamic_ef_search: bool,
    /// Minimum ef_search value
    pub ef_search_min: usize,
    /// Maximum ef_search value
    pub ef_search_max: usize,
}

impl Default for HnswConfig {
    fn default() -> Self {
        Self {
            max_connections: 16,
            ef_construction: 200,
            dimension: 1536, // Default for OpenAI embeddings
            distance_metric: DistanceMetric::L2,
            // Dynamic ef_search configuration
            ef_search_base: 200,
            dynamic_ef_search: true,
            ef_search_min: 50,
            ef_search_max: 500,
        }
    }
}

/// HNSW index wrapper
pub struct HnswIndex {
    /// The underlying HNSW graph
    index: Arc<RwLock<Hnsw<'static, f32, DistL2>>>,
    /// Index configuration
    config: HnswConfig,
    /// Mapping from internal HNSW id to external row id
    id_mapping: Arc<RwLock<Vec<u64>>>,
    /// Reverse mapping from row id to HNSW id
    reverse_mapping: Arc<RwLock<std::collections::HashMap<u64, usize>>>,
}

impl HnswIndex {
    /// Create a new HNSW index
    pub fn new(config: HnswConfig) -> Result<Self> {
        let max_nb_connection = config.max_connections;
        let ef_construction = config.ef_construction;

        let index = Hnsw::<f32, DistL2>::new(
            max_nb_connection,
            config.dimension,
            ef_construction,
            100, // max_layer
            DistL2,
        );

        Ok(Self {
            index: Arc::new(RwLock::new(index)),
            config,
            id_mapping: Arc::new(RwLock::new(Vec::new())),
            reverse_mapping: Arc::new(RwLock::new(std::collections::HashMap::new())),
        })
    }

    /// Insert a vector into the index
    pub fn insert(&self, row_id: u64, vector: &Vector) -> Result<()> {
        // Validate dimension
        if vector.len() != self.config.dimension {
            return Err(Error::query_execution(format!(
                "Vector dimension mismatch: expected {}, got {}",
                self.config.dimension,
                vector.len()
            )));
        }

        let mut id_mapping = self.id_mapping.write();
        let mut reverse_mapping = self.reverse_mapping.write();

        // Get internal HNSW id
        let hnsw_id = id_mapping.len();
        id_mapping.push(row_id);
        reverse_mapping.insert(row_id, hnsw_id);

        // Insert into HNSW index
        let index = self.index.write();
        let data_id = DataId::from(hnsw_id);
        index.insert((vector.as_slice(), data_id));

        Ok(())
    }

    /// Bulk-insert a batch of `(row_id, vector)` pairs, building the HNSW
    /// graph in parallel across all available cores.
    ///
    /// This is the fast path for `CREATE INDEX ... USING hnsw` backfill over
    /// a populated table. The naive `insert`-per-row loop is single-threaded
    /// and dominated by the O(log N) graph-link work per vector. Here we:
    ///   1. Take the id-mapping locks once, pre-assign every contiguous
    ///      `hnsw_id`, and update both mappings. This is O(N) but cheap (a
    ///      vec push + hashmap insert per row) and runs under a single short
    ///      critical section instead of N lock acquisitions.
    ///   2. Hand the whole batch to `hnsw_rs::parallel_insert`, which uses
    ///      Rayon to build the graph concurrently. The underlying `Hnsw`
    ///      synchronises its own internal structures, so concurrent inserts
    ///      are correct.
    ///
    /// Result ordering and search results are identical to inserting the same
    /// rows sequentially (data_ids are assigned in input order); only the
    /// wall-clock build time changes.
    pub fn insert_batch(&self, batch: &[(u64, Vector)]) -> Result<()> {
        for (_, vector) in batch {
            if vector.len() != self.config.dimension {
                return Err(Error::query_execution(format!(
                    "Vector dimension mismatch: expected {}, got {}",
                    self.config.dimension,
                    vector.len()
                )));
            }
        }
        if batch.is_empty() {
            return Ok(());
        }

        let mut id_mapping = self.id_mapping.write();
        let mut reverse_mapping = self.reverse_mapping.write();
        let base = id_mapping.len();
        let mut datas: Vec<(&Vec<f32>, usize)> = Vec::with_capacity(batch.len());
        for (offset, (row_id, vector)) in batch.iter().enumerate() {
            let hnsw_id = base + offset;
            id_mapping.push(*row_id);
            reverse_mapping.insert(*row_id, hnsw_id);
            datas.push((vector, hnsw_id));
        }

        let index = self.index.write();
        index.parallel_insert(&datas);

        Ok(())
    }

    /// Search for k nearest neighbors
    ///
    /// Performance optimization: Uses dynamic ef_search adjustment based on:
    /// - k (number of neighbors requested): higher k needs larger ef_search
    /// - Index size: larger indices benefit from slightly higher ef_search
    /// - Recall requirements: ef_search = max(k * multiplier, base)
    pub fn search(&self, query: &Vector, k: usize) -> Result<Vec<(u64, f32)>> {
        // Validate dimension
        if query.len() != self.config.dimension {
            return Err(Error::query_execution(format!(
                "Query vector dimension mismatch: expected {}, got {}",
                self.config.dimension,
                query.len()
            )));
        }

        let index = self.index.read();
        let id_mapping = self.id_mapping.read();
        let reverse_mapping = self.reverse_mapping.read();

        // Calculate dynamic ef_search
        let ef_search = self.calculate_ef_search(k, id_mapping.len());

        // Perform search with dynamic ef_search
        let results = index.search(query.as_slice(), k, ef_search);

        // Convert internal ids to row ids
        let mapped_results: Vec<(u64, f32)> = results
            .into_iter()
            .filter_map(|neighbor| {
                let hnsw_id = neighbor.d_id as usize;
                id_mapping.get(hnsw_id).and_then(|&row_id| {
                    (reverse_mapping.get(&row_id) == Some(&hnsw_id)).then_some((row_id, neighbor.distance))
                })
            })
            .collect();

        // Recall guard: hnsw_rs 0.3.3 can leave an early-inserted, high-level
        // point with an empty layer-0 neighbour list (the rank-1 point gets no
        // forward links, and back-links land at the later point's own level).
        // A search that descends onto such a pivot under-returns even though
        // the vectors exist. When the graph returns fewer than the achievable
        // k on a small index, recover exactness by brute force. Capped so a
        // tombstone-heavy large index degrades as before instead of paying an
        // O(N) scan per query.
        let live = reverse_mapping.len();
        if mapped_results.len() < k.min(live) && live <= BRUTE_FORCE_RESCUE_MAX {
            return Ok(brute_force_rescue(&index, query, k, &id_mapping, &reverse_mapping));
        }

        Ok(mapped_results)
    }

    /// Calculate optimal ef_search based on k and index size
    fn calculate_ef_search(&self, k: usize, index_size: usize) -> usize {
        if !self.config.dynamic_ef_search {
            return self.config.ef_search_base;
        }

        // Base: at minimum, ef_search should be 2x k for good recall
        let k_based = k * 2;

        // Size factor: larger indices may need slightly higher ef_search
        // log2(size) provides diminishing returns scaling
        let size_factor = if index_size > 1000 {
            (index_size as f64).log2() / 10.0 // ~1.0 at 1K, ~1.3 at 10K, ~1.6 at 100K
        } else {
            1.0
        };

        // Calculate ef_search with size adjustment
        let adjusted = ((self.config.ef_search_base as f64 * size_factor) as usize).max(k_based);

        // Clamp to configured bounds
        adjusted.clamp(self.config.ef_search_min, self.config.ef_search_max)
    }

    /// Search with custom ef_search (for fine-tuned queries)
    pub fn search_with_ef(&self, query: &Vector, k: usize, ef_search: usize) -> Result<Vec<(u64, f32)>> {
        // Validate dimension
        if query.len() != self.config.dimension {
            return Err(Error::query_execution(format!(
                "Query vector dimension mismatch: expected {}, got {}",
                self.config.dimension,
                query.len()
            )));
        }

        let index = self.index.read();
        let id_mapping = self.id_mapping.read();
        let reverse_mapping = self.reverse_mapping.read();

        // Clamp ef_search to valid range
        let ef_search = ef_search.clamp(k, self.config.ef_search_max);

        let results = index.search(query.as_slice(), k, ef_search);

        let mapped_results: Vec<(u64, f32)> = results
            .into_iter()
            .filter_map(|neighbor| {
                let hnsw_id = neighbor.d_id as usize;
                id_mapping.get(hnsw_id).and_then(|&row_id| {
                    (reverse_mapping.get(&row_id) == Some(&hnsw_id)).then_some((row_id, neighbor.distance))
                })
            })
            .collect();

        Ok(mapped_results)
    }

    /// Delete a vector from the index
    pub fn delete(&self, row_id: u64) -> Result<()> {
        let mut reverse_mapping = self.reverse_mapping.write();

        if let Some(&hnsw_id) = reverse_mapping.get(&row_id) {
            // Remove from reverse mapping
            reverse_mapping.remove(&row_id);

            // Note: hnsw_rs doesn't support true deletion, so we just mark it as deleted
            // In a production system, you'd need to rebuild the index periodically
            // or use a deleted tombstone list

            Ok(())
        } else {
            Err(Error::query_execution(format!(
                "Vector with row_id {} not found in index",
                row_id
            )))
        }
    }

    /// Get the number of vectors in the index
    pub fn len(&self) -> usize {
        // Physical entry count (includes tombstones): hnsw_rs cannot truly
        // delete, so `delete` only drops the live `reverse_mapping` entry and the
        // vector stays in the graph. `id_mapping` keeps one slot per insert and is
        // never shrunk by delete, so it reflects the tombstone-inclusive size.
        self.id_mapping.read().len()
    }

    /// Live (tombstone-excluded) entry count: one entry per row currently
    /// visible to `search`. `delete` removes the `reverse_mapping` entry, so
    /// its size is the number of vectors a search can still return.
    pub fn live_len(&self) -> usize {
        self.reverse_mapping.read().len()
    }

    /// Check if the index is empty
    pub fn is_empty(&self) -> bool {
        self.id_mapping.read().is_empty()
    }

    /// Get the dimension of vectors in this index
    pub fn dimension(&self) -> usize {
        self.config.dimension
    }

    /// R4.2: dump the graph via `hnsw_rs` (`{basename}.hnsw.graph` +
    /// `{basename}.hnsw.data` inside `dir`).
    pub fn dump_graph(&self, dir: &std::path::Path, basename: &str) -> Result<()> {
        dump_graph_impl(&self.index.read(), dir, basename)
    }

    /// R4.2: snapshot the row-id mappings for the persistence sidecar.
    pub fn export_mappings(&self) -> (Vec<u64>, Vec<(u64, u64)>) {
        export_mappings_impl(&self.id_mapping.read(), &self.reverse_mapping.read())
    }

    /// R4.2: reconstruct the index from a graph dump + sidecar mappings.
    pub fn reload_from_dump(
        config: HnswConfig,
        dir: &std::path::Path,
        basename: &str,
        id_mapping: Vec<u64>,
        reverse_pairs: &[(u64, u64)],
    ) -> Result<Self> {
        let graph: Hnsw<'static, f32, DistL2> = reload_graph(dir, basename)?;
        Ok(Self {
            index: Arc::new(RwLock::new(graph)),
            config,
            id_mapping: Arc::new(RwLock::new(id_mapping)),
            reverse_mapping: Arc::new(RwLock::new(reverse_pairs_to_map(reverse_pairs))),
        })
    }
}

// ---- R4.2 graph persistence helpers (shared by the three metric wrappers) ----

fn dump_graph_impl<D>(index: &Hnsw<'static, f32, D>, dir: &std::path::Path, basename: &str) -> Result<()>
where
    D: Distance<f32> + Send + Sync,
{
    use hnsw_rs::api::AnnT;
    let dumped = index
        .file_dump(dir, basename)
        .map_err(|e| Error::storage(format!("HNSW graph dump failed: {e}")))?;
    if dumped != basename {
        // file_dump uniquifies the basename instead of overwriting when the
        // datamap option is active; we always build in-memory graphs, so this
        // signals an unexpected configuration rather than a partial dump.
        return Err(Error::storage(format!(
            "HNSW graph dump wrote basename '{dumped}' instead of '{basename}'"
        )));
    }
    Ok(())
}

fn export_mappings_impl(
    id_mapping: &[u64],
    reverse_mapping: &std::collections::HashMap<u64, usize>,
) -> (Vec<u64>, Vec<(u64, u64)>) {
    let reverse = reverse_mapping.iter().map(|(row, hnsw)| (*row, *hnsw as u64)).collect();
    (id_mapping.to_vec(), reverse)
}

fn reverse_pairs_to_map(pairs: &[(u64, u64)]) -> std::collections::HashMap<u64, usize> {
    pairs.iter().map(|(row, hnsw)| (*row, *hnsw as usize)).collect()
}

/// Upper bound on live entries for the under-return brute-force rescue in the
/// search paths. The layer-0-isolation pathology (see `brute_force_rescue`) is
/// a small-index phenomenon; beyond this size an under-full result is almost
/// always tombstone filtering, which brute force would turn into a per-query
/// O(N) scan.
const BRUTE_FORCE_RESCUE_MAX: usize = 10_000;

/// Exact k-NN over every live point — the recall rescue for hnsw_rs 0.3.3's
/// layer-0 isolation defect: an early-inserted point that drew a high level
/// can end up with an empty layer-0 neighbour list (the rank-1 point gets no
/// forward links; back-links land at the later point's own level), making it
/// unreachable from a search that descends onto it, which then under-returns
/// even though the vectors exist. Uses the same distance functor as the graph
/// search so scores are directly comparable. Caller must already hold the
/// index/mapping read locks (guards are passed in, not re-taken — parking_lot
/// locks are not reentrant under a queued writer).
fn brute_force_rescue<D>(
    index: &Hnsw<'static, f32, D>,
    query: &Vector,
    k: usize,
    id_mapping: &[u64],
    reverse_mapping: &std::collections::HashMap<u64, usize>,
) -> Vec<(u64, f32)>
where
    D: Distance<f32> + Default + Send + Sync,
{
    let dist = D::default();
    let mut all: Vec<(u64, f32)> = index
        .get_point_indexation()
        .into_iter()
        .filter_map(|point| {
            let hnsw_id = point.get_origin_id();
            let row_id = *id_mapping.get(hnsw_id)?;
            // Same tombstone rule as the graph-result filter.
            (reverse_mapping.get(&row_id) == Some(&hnsw_id))
                .then(|| (row_id, dist.eval(query.as_slice(), point.get_v())))
        })
        .collect();
    all.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
    all.truncate(k);
    all
}

/// Reload a dumped graph. The `HnswIo` loader owns buffers the reloaded
/// graph borrows from, so it is intentionally leaked to give the graph the
/// `'static` lifetime the wrappers require — one small leak per index per
/// process open (the reload path runs only at startup).
fn reload_graph<D>(dir: &std::path::Path, basename: &str) -> Result<Hnsw<'static, f32, D>>
where
    D: Distance<f32> + Default + Send + Sync,
{
    let io: &'static mut hnsw_rs::hnswio::HnswIo = Box::leak(Box::new(hnsw_rs::hnswio::HnswIo::new(dir, basename)));
    io.load_hnsw::<f32, D>()
        .map_err(|e| Error::storage(format!("HNSW graph reload failed: {e}")))
}

/// Multi-metric HNSW index that supports different distance metrics
pub enum MultiMetricHnswIndex {
    L2(HnswIndex),
    Cosine(CosineHnswIndex),
    InnerProduct(InnerProductHnswIndex),
}

impl MultiMetricHnswIndex {
    /// Create a new multi-metric HNSW index
    pub fn new(config: HnswConfig) -> Result<Self> {
        match config.distance_metric {
            DistanceMetric::L2 => Ok(Self::L2(HnswIndex::new(config)?)),
            DistanceMetric::Cosine => Ok(Self::Cosine(CosineHnswIndex::new(config)?)),
            DistanceMetric::InnerProduct => Ok(Self::InnerProduct(InnerProductHnswIndex::new(config)?)),
        }
    }

    /// Insert a vector
    pub fn insert(&self, row_id: u64, vector: &Vector) -> Result<()> {
        match self {
            Self::L2(index) => index.insert(row_id, vector),
            Self::Cosine(index) => index.insert(row_id, vector),
            Self::InnerProduct(index) => index.insert(row_id, vector),
        }
    }

    /// Bulk-insert a batch of `(row_id, vector)` pairs, building the graph in
    /// parallel. Used by `CREATE INDEX` backfill; see [`HnswIndex::insert_batch`].
    pub fn insert_batch(&self, batch: &[(u64, Vector)]) -> Result<()> {
        match self {
            Self::L2(index) => index.insert_batch(batch),
            Self::Cosine(index) => index.insert_batch(batch),
            Self::InnerProduct(index) => index.insert_batch(batch),
        }
    }

    /// Search for k nearest neighbors
    pub fn search(&self, query: &Vector, k: usize) -> Result<Vec<(u64, f32)>> {
        match self {
            Self::L2(index) => index.search(query, k),
            Self::Cosine(index) => index.search(query, k),
            Self::InnerProduct(index) => index.search(query, k),
        }
    }

    /// Delete a vector
    pub fn delete(&self, row_id: u64) -> Result<()> {
        match self {
            Self::L2(index) => index.delete(row_id),
            Self::Cosine(index) => index.delete(row_id),
            Self::InnerProduct(index) => index.delete(row_id),
        }
    }

    /// Get dimension
    pub fn dimension(&self) -> usize {
        match self {
            Self::L2(index) => index.dimension(),
            Self::Cosine(index) => index.dimension(),
            Self::InnerProduct(index) => index.dimension(),
        }
    }

    /// Get number of vectors in the index
    pub fn len(&self) -> usize {
        match self {
            Self::L2(index) => index.len(),
            Self::Cosine(index) => index.len(),
            Self::InnerProduct(index) => index.len(),
        }
    }

    /// Live (tombstone-excluded) entry count — see [`HnswIndex::live_len`].
    pub fn live_len(&self) -> usize {
        match self {
            Self::L2(index) => index.live_len(),
            Self::Cosine(index) => index.live_len(),
            Self::InnerProduct(index) => index.live_len(),
        }
    }

    /// Check if the index is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// R4.2: dump the graph to `{dir}/{basename}.hnsw.{graph,data}`.
    pub fn dump_graph(&self, dir: &std::path::Path, basename: &str) -> Result<()> {
        match self {
            Self::L2(index) => index.dump_graph(dir, basename),
            Self::Cosine(index) => index.dump_graph(dir, basename),
            Self::InnerProduct(index) => index.dump_graph(dir, basename),
        }
    }

    /// R4.2: snapshot the row-id mappings for the persistence sidecar.
    pub fn export_mappings(&self) -> (Vec<u64>, Vec<(u64, u64)>) {
        match self {
            Self::L2(index) => index.export_mappings(),
            Self::Cosine(index) => index.export_mappings(),
            Self::InnerProduct(index) => index.export_mappings(),
        }
    }

    /// R4.2: reconstruct an index from a graph dump + sidecar mappings.
    /// The metric is taken from `config.distance_metric` and must match the
    /// metric the graph was built with (the caller persists it in the sidecar).
    pub fn reload_from_dump(
        config: HnswConfig,
        dir: &std::path::Path,
        basename: &str,
        id_mapping: Vec<u64>,
        reverse_pairs: &[(u64, u64)],
    ) -> Result<Self> {
        match config.distance_metric {
            DistanceMetric::L2 => Ok(Self::L2(HnswIndex::reload_from_dump(
                config,
                dir,
                basename,
                id_mapping,
                reverse_pairs,
            )?)),
            DistanceMetric::Cosine => Ok(Self::Cosine(CosineHnswIndex::reload_from_dump(
                config,
                dir,
                basename,
                id_mapping,
                reverse_pairs,
            )?)),
            DistanceMetric::InnerProduct => Ok(Self::InnerProduct(InnerProductHnswIndex::reload_from_dump(
                config,
                dir,
                basename,
                id_mapping,
                reverse_pairs,
            )?)),
        }
    }
}

/// HNSW index for cosine distance
pub struct CosineHnswIndex {
    index: Arc<RwLock<Hnsw<'static, f32, DistCosine>>>,
    config: HnswConfig,
    id_mapping: Arc<RwLock<Vec<u64>>>,
    reverse_mapping: Arc<RwLock<std::collections::HashMap<u64, usize>>>,
}

impl CosineHnswIndex {
    pub fn new(config: HnswConfig) -> Result<Self> {
        let index = Hnsw::<f32, DistCosine>::new(
            config.max_connections,
            config.dimension,
            config.ef_construction,
            100,
            DistCosine,
        );

        Ok(Self {
            index: Arc::new(RwLock::new(index)),
            config,
            id_mapping: Arc::new(RwLock::new(Vec::new())),
            reverse_mapping: Arc::new(RwLock::new(std::collections::HashMap::new())),
        })
    }

    pub fn insert(&self, row_id: u64, vector: &Vector) -> Result<()> {
        if vector.len() != self.config.dimension {
            return Err(Error::query_execution(format!(
                "Vector dimension mismatch: expected {}, got {}",
                self.config.dimension,
                vector.len()
            )));
        }

        let mut id_mapping = self.id_mapping.write();
        let mut reverse_mapping = self.reverse_mapping.write();

        let hnsw_id = id_mapping.len();
        id_mapping.push(row_id);
        reverse_mapping.insert(row_id, hnsw_id);

        let index = self.index.write();
        let data_id = DataId::from(hnsw_id);
        index.insert((vector.as_slice(), data_id));

        Ok(())
    }

    /// Bulk-insert a batch, building the cosine HNSW graph in parallel.
    /// See [`HnswIndex::insert_batch`] for the rationale and correctness notes.
    pub fn insert_batch(&self, batch: &[(u64, Vector)]) -> Result<()> {
        for (_, vector) in batch {
            if vector.len() != self.config.dimension {
                return Err(Error::query_execution(format!(
                    "Vector dimension mismatch: expected {}, got {}",
                    self.config.dimension,
                    vector.len()
                )));
            }
        }
        if batch.is_empty() {
            return Ok(());
        }

        let mut id_mapping = self.id_mapping.write();
        let mut reverse_mapping = self.reverse_mapping.write();
        let base = id_mapping.len();
        let mut datas: Vec<(&Vec<f32>, usize)> = Vec::with_capacity(batch.len());
        for (offset, (row_id, vector)) in batch.iter().enumerate() {
            let hnsw_id = base + offset;
            id_mapping.push(*row_id);
            reverse_mapping.insert(*row_id, hnsw_id);
            datas.push((vector, hnsw_id));
        }

        let index = self.index.write();
        index.parallel_insert(&datas);

        Ok(())
    }

    pub fn search(&self, query: &Vector, k: usize) -> Result<Vec<(u64, f32)>> {
        if query.len() != self.config.dimension {
            return Err(Error::query_execution(format!(
                "Query vector dimension mismatch: expected {}, got {}",
                self.config.dimension,
                query.len()
            )));
        }

        let index = self.index.read();
        let id_mapping = self.id_mapping.read();
        let reverse_mapping = self.reverse_mapping.read();

        let results = index.search(query.as_slice(), k, 200);
        let mapped_results: Vec<(u64, f32)> = results
            .into_iter()
            .filter_map(|neighbor| {
                let hnsw_id = neighbor.d_id as usize;
                id_mapping.get(hnsw_id).and_then(|&row_id| {
                    (reverse_mapping.get(&row_id) == Some(&hnsw_id)).then_some((row_id, neighbor.distance))
                })
            })
            .collect();

        // Recall guard — see `brute_force_rescue`.
        let live = reverse_mapping.len();
        if mapped_results.len() < k.min(live) && live <= BRUTE_FORCE_RESCUE_MAX {
            return Ok(brute_force_rescue(&index, query, k, &id_mapping, &reverse_mapping));
        }

        Ok(mapped_results)
    }

    pub fn delete(&self, row_id: u64) -> Result<()> {
        let mut reverse_mapping = self.reverse_mapping.write();
        if reverse_mapping.remove(&row_id).is_some() {
            Ok(())
        } else {
            Err(Error::query_execution(format!(
                "Vector with row_id {} not found in index",
                row_id
            )))
        }
    }

    pub fn dimension(&self) -> usize {
        self.config.dimension
    }

    pub fn len(&self) -> usize {
        // Physical entry count (includes tombstones): hnsw_rs cannot truly
        // delete, so `delete` only drops the live `reverse_mapping` entry and the
        // vector stays in the graph. `id_mapping` keeps one slot per insert and is
        // never shrunk by delete, so it reflects the tombstone-inclusive size.
        self.id_mapping.read().len()
    }

    /// Live (tombstone-excluded) entry count — see [`HnswIndex::live_len`].
    pub fn live_len(&self) -> usize {
        self.reverse_mapping.read().len()
    }

    pub fn is_empty(&self) -> bool {
        self.id_mapping.read().is_empty()
    }

    /// R4.2: dump the graph — see [`HnswIndex::dump_graph`].
    pub fn dump_graph(&self, dir: &std::path::Path, basename: &str) -> Result<()> {
        dump_graph_impl(&self.index.read(), dir, basename)
    }

    /// R4.2: snapshot the row-id mappings — see [`HnswIndex::export_mappings`].
    pub fn export_mappings(&self) -> (Vec<u64>, Vec<(u64, u64)>) {
        export_mappings_impl(&self.id_mapping.read(), &self.reverse_mapping.read())
    }

    /// R4.2: reconstruct from a graph dump — see [`HnswIndex::reload_from_dump`].
    pub fn reload_from_dump(
        config: HnswConfig,
        dir: &std::path::Path,
        basename: &str,
        id_mapping: Vec<u64>,
        reverse_pairs: &[(u64, u64)],
    ) -> Result<Self> {
        let graph: Hnsw<'static, f32, DistCosine> = reload_graph(dir, basename)?;
        Ok(Self {
            index: Arc::new(RwLock::new(graph)),
            config,
            id_mapping: Arc::new(RwLock::new(id_mapping)),
            reverse_mapping: Arc::new(RwLock::new(reverse_pairs_to_map(reverse_pairs))),
        })
    }
}

/// HNSW index for inner product (dot product)
pub struct InnerProductHnswIndex {
    index: Arc<RwLock<Hnsw<'static, f32, DistDot>>>,
    config: HnswConfig,
    id_mapping: Arc<RwLock<Vec<u64>>>,
    reverse_mapping: Arc<RwLock<std::collections::HashMap<u64, usize>>>,
}

impl InnerProductHnswIndex {
    pub fn new(config: HnswConfig) -> Result<Self> {
        let index = Hnsw::<f32, DistDot>::new(
            config.max_connections,
            config.dimension,
            config.ef_construction,
            100,
            DistDot,
        );

        Ok(Self {
            index: Arc::new(RwLock::new(index)),
            config,
            id_mapping: Arc::new(RwLock::new(Vec::new())),
            reverse_mapping: Arc::new(RwLock::new(std::collections::HashMap::new())),
        })
    }

    pub fn insert(&self, row_id: u64, vector: &Vector) -> Result<()> {
        if vector.len() != self.config.dimension {
            return Err(Error::query_execution(format!(
                "Vector dimension mismatch: expected {}, got {}",
                self.config.dimension,
                vector.len()
            )));
        }

        let mut id_mapping = self.id_mapping.write();
        let mut reverse_mapping = self.reverse_mapping.write();

        let hnsw_id = id_mapping.len();
        id_mapping.push(row_id);
        reverse_mapping.insert(row_id, hnsw_id);

        let index = self.index.write();
        let data_id = DataId::from(hnsw_id);
        index.insert((vector.as_slice(), data_id));

        Ok(())
    }

    /// Bulk-insert a batch, building the inner-product HNSW graph in parallel.
    /// See [`HnswIndex::insert_batch`] for the rationale and correctness notes.
    pub fn insert_batch(&self, batch: &[(u64, Vector)]) -> Result<()> {
        for (_, vector) in batch {
            if vector.len() != self.config.dimension {
                return Err(Error::query_execution(format!(
                    "Vector dimension mismatch: expected {}, got {}",
                    self.config.dimension,
                    vector.len()
                )));
            }
        }
        if batch.is_empty() {
            return Ok(());
        }

        let mut id_mapping = self.id_mapping.write();
        let mut reverse_mapping = self.reverse_mapping.write();
        let base = id_mapping.len();
        let mut datas: Vec<(&Vec<f32>, usize)> = Vec::with_capacity(batch.len());
        for (offset, (row_id, vector)) in batch.iter().enumerate() {
            let hnsw_id = base + offset;
            id_mapping.push(*row_id);
            reverse_mapping.insert(*row_id, hnsw_id);
            datas.push((vector, hnsw_id));
        }

        let index = self.index.write();
        index.parallel_insert(&datas);

        Ok(())
    }

    pub fn search(&self, query: &Vector, k: usize) -> Result<Vec<(u64, f32)>> {
        if query.len() != self.config.dimension {
            return Err(Error::query_execution(format!(
                "Query vector dimension mismatch: expected {}, got {}",
                self.config.dimension,
                query.len()
            )));
        }

        let index = self.index.read();
        let id_mapping = self.id_mapping.read();
        let reverse_mapping = self.reverse_mapping.read();

        let results = index.search(query.as_slice(), k, 200);
        let mapped_results: Vec<(u64, f32)> = results
            .into_iter()
            .filter_map(|neighbor| {
                let hnsw_id = neighbor.d_id as usize;
                id_mapping.get(hnsw_id).and_then(|&row_id| {
                    (reverse_mapping.get(&row_id) == Some(&hnsw_id)).then_some((row_id, neighbor.distance))
                })
            })
            .collect();

        // Recall guard — see `brute_force_rescue`.
        let live = reverse_mapping.len();
        if mapped_results.len() < k.min(live) && live <= BRUTE_FORCE_RESCUE_MAX {
            return Ok(brute_force_rescue(&index, query, k, &id_mapping, &reverse_mapping));
        }

        Ok(mapped_results)
    }

    pub fn delete(&self, row_id: u64) -> Result<()> {
        let mut reverse_mapping = self.reverse_mapping.write();
        if reverse_mapping.remove(&row_id).is_some() {
            Ok(())
        } else {
            Err(Error::query_execution(format!(
                "Vector with row_id {} not found in index",
                row_id
            )))
        }
    }

    pub fn dimension(&self) -> usize {
        self.config.dimension
    }

    pub fn len(&self) -> usize {
        // Physical entry count (includes tombstones): hnsw_rs cannot truly
        // delete, so `delete` only drops the live `reverse_mapping` entry and the
        // vector stays in the graph. `id_mapping` keeps one slot per insert and is
        // never shrunk by delete, so it reflects the tombstone-inclusive size.
        self.id_mapping.read().len()
    }

    /// Live (tombstone-excluded) entry count — see [`HnswIndex::live_len`].
    pub fn live_len(&self) -> usize {
        self.reverse_mapping.read().len()
    }

    pub fn is_empty(&self) -> bool {
        self.id_mapping.read().is_empty()
    }

    /// R4.2: dump the graph — see [`HnswIndex::dump_graph`].
    pub fn dump_graph(&self, dir: &std::path::Path, basename: &str) -> Result<()> {
        dump_graph_impl(&self.index.read(), dir, basename)
    }

    /// R4.2: snapshot the row-id mappings — see [`HnswIndex::export_mappings`].
    pub fn export_mappings(&self) -> (Vec<u64>, Vec<(u64, u64)>) {
        export_mappings_impl(&self.id_mapping.read(), &self.reverse_mapping.read())
    }

    /// R4.2: reconstruct from a graph dump — see [`HnswIndex::reload_from_dump`].
    pub fn reload_from_dump(
        config: HnswConfig,
        dir: &std::path::Path,
        basename: &str,
        id_mapping: Vec<u64>,
        reverse_pairs: &[(u64, u64)],
    ) -> Result<Self> {
        let graph: Hnsw<'static, f32, DistDot> = reload_graph(dir, basename)?;
        Ok(Self {
            index: Arc::new(RwLock::new(graph)),
            config,
            id_mapping: Arc::new(RwLock::new(id_mapping)),
            reverse_mapping: Arc::new(RwLock::new(reverse_pairs_to_map(reverse_pairs))),
        })
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    #[test]
    fn test_hnsw_basic() {
        let config = HnswConfig {
            dimension: 3,
            max_connections: 16,
            ef_construction: 200,
            distance_metric: DistanceMetric::L2,
            ef_search_base: 200,
            dynamic_ef_search: true,
            ef_search_min: 50,
            ef_search_max: 500,
        };

        let index = HnswIndex::new(config).unwrap();

        // Insert vectors. The query's nearest neighbor (id 1) goes LAST:
        // hnsw_rs 0.3.3 gives the first-inserted point no forward links, and
        // when its (OS-entropy-seeded) level draw puts it above the later
        // points it can end up layer-0-isolated — search then returns 1 of 2
        // results (~1/800 runs). A non-first insert always gets layer-0 links.
        index.insert(2, &vec![0.0, 1.0, 0.0]).unwrap();
        index.insert(3, &vec![0.0, 0.0, 1.0]).unwrap();
        index.insert(1, &vec![1.0, 0.0, 0.0]).unwrap();

        // Search
        let query = vec![1.0, 0.1, 0.0];
        let results = index.search(&query, 2).unwrap();

        assert_eq!(results.len(), 2);
        assert_eq!(results[0].0, 1); // Closest to [1,0,0]
    }

    #[test]
    fn test_first_insert_isolation_rescued() {
        // Historical flake shape: nearest vector inserted FIRST. In ~1/800
        // seeds hnsw_rs leaves it layer-0-isolated and the graph search
        // under-returns; the brute-force rescue must make k=2 exact every
        // time. Loop enough that a regression reappears at a visible rate.
        for _ in 0..800 {
            let config = HnswConfig {
                dimension: 3,
                max_connections: 16,
                ef_construction: 200,
                distance_metric: DistanceMetric::L2,
                ef_search_base: 200,
                dynamic_ef_search: true,
                ef_search_min: 50,
                ef_search_max: 500,
            };
            let index = HnswIndex::new(config).unwrap();
            index.insert(1, &vec![1.0, 0.0, 0.0]).unwrap();
            index.insert(2, &vec![0.0, 1.0, 0.0]).unwrap();
            index.insert(3, &vec![0.0, 0.0, 1.0]).unwrap();

            let results = index.search(&vec![1.0, 0.1, 0.0], 2).unwrap();
            assert_eq!(results.len(), 2, "under-returned despite rescue");
            assert_eq!(results[0].0, 1);
        }
    }

    #[test]
    fn test_rescue_respects_tombstones() {
        let config = HnswConfig {
            dimension: 3,
            ..Default::default()
        };
        let index = HnswIndex::new(config).unwrap();
        index.insert(1, &vec![1.0, 0.0, 0.0]).unwrap();
        index.insert(2, &vec![0.0, 1.0, 0.0]).unwrap();
        index.delete(1).unwrap();

        // k=2 but only 1 live vector: must return exactly the live one —
        // the rescue must not resurrect the tombstoned row.
        let results = index.search(&vec![1.0, 0.0, 0.0], 2).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].0, 2);
    }

    #[test]
    fn test_dimension_validation() {
        let config = HnswConfig {
            dimension: 3,
            ..Default::default()
        };

        let index = HnswIndex::new(config).unwrap();

        // Wrong dimension should fail
        let result = index.insert(1, &vec![1.0, 0.0]);
        assert!(result.is_err());
    }

    #[test]
    fn test_multi_metric_index() {
        let config = HnswConfig {
            dimension: 2,
            distance_metric: DistanceMetric::Cosine,
            ..Default::default()
        };

        let index = MultiMetricHnswIndex::new(config).unwrap();

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

        let results = index.search(&vec![0.7, 0.7], 1).unwrap();
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn test_vector_count_tracking() {
        // Test all three metric types
        let test_configs = vec![
            (DistanceMetric::L2, "L2"),
            (DistanceMetric::Cosine, "Cosine"),
            (DistanceMetric::InnerProduct, "InnerProduct"),
        ];

        for (metric, name) in test_configs {
            let config = HnswConfig {
                dimension: 3,
                distance_metric: metric,
                ..Default::default()
            };

            let index = MultiMetricHnswIndex::new(config).unwrap();

            // Initially empty
            assert_eq!(index.len(), 0, "{} index should start empty", name);
            assert!(index.is_empty(), "{} index should be empty", name);

            // Insert vectors
            index.insert(1, &vec![1.0, 0.0, 0.0]).unwrap();
            assert_eq!(index.len(), 1, "{} index should have 1 vector", name);
            assert!(!index.is_empty(), "{} index should not be empty", name);

            index.insert(2, &vec![0.0, 1.0, 0.0]).unwrap();
            assert_eq!(index.len(), 2, "{} index should have 2 vectors", name);

            index.insert(3, &vec![0.0, 0.0, 1.0]).unwrap();
            assert_eq!(index.len(), 3, "{} index should have 3 vectors", name);

            // Delete a vector
            index.delete(2).unwrap();
            assert_eq!(index.len(), 3, "{} index length should remain 3 (tombstone)", name);
        }
    }

    #[test]
    fn test_index_len_methods() {
        // Test that individual index types track length correctly
        let config = HnswConfig {
            dimension: 2,
            max_connections: 16,
            ef_construction: 200,
            distance_metric: DistanceMetric::L2,
            ef_search_base: 200,
            dynamic_ef_search: true,
            ef_search_min: 50,
            ef_search_max: 500,
        };

        let index = HnswIndex::new(config).unwrap();
        assert_eq!(index.len(), 0);
        assert!(index.is_empty());

        index.insert(1, &vec![1.0, 0.0]).unwrap();
        assert_eq!(index.len(), 1);
        assert!(!index.is_empty());

        index.insert(2, &vec![0.0, 1.0]).unwrap();
        assert_eq!(index.len(), 2);
    }
}