sz-orm-ai 2.0.0

SZ-ORM AI Extension - Embedding, Vector Store, RAG Engine
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
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
//! HNSW(Hierarchical Navigable Small World)向量索引
//!
//! 提供近似最近邻搜索(ANN)能力,相比暴力搜索在大规模数据集上有显著性能优势。
//!
//! # 算法概述
//!
//! HNSW 是一种基于多层图的近似最近邻搜索算法:
//! - 每个节点以概率 `1/ML^level` 出现在第 `level` 层(ML 为层级因子,通常为 `ln(2)`)
//! - 顶层节点稀疏,底层包含全部节点
//! - 搜索时从顶层开始贪心搜索,逐层下降到第 0 层
//! - 在第 0 层进行 `ef` 次扩展的束搜索,返回 top-k 结果
//!
//! # 特性
//!
//! - 支持 Cosine / Euclidean / DotProduct 三种距离度量
//! - 支持插入、删除(标记删除)、搜索
//! - 纯内存实现,无外部依赖
//! - 线程安全(内部使用 `RwLock`)
//!
//! # 示例
//!
//! ```ignore
//! use sz_orm_ai::vector::hnsw::{HnswIndex, HnswConfig, VectorMetric};
//!
//! let mut index = HnswIndex::new(HnswConfig::default().with_metric(VectorMetric::Cosine));
//! index.insert("v1", vec![1.0, 0.0, 0.0]).unwrap();
//! index.insert("v2", vec![0.0, 1.0, 0.0]).unwrap();
//! index.insert("v3", vec![1.0, 1.0, 0.0]).unwrap();
//!
//! let results = index.search(&[1.0, 0.0, 0.0], 2, 50).unwrap();
//! assert_eq!(results.len(), 2);
//! assert_eq!(results[0].id, "v1");
//! ```

use crate::error::AiError;
use crate::vector::VectorMetric;
use parking_lot::RwLock;
use std::collections::{BinaryHeap, HashMap, HashSet};

// ---------------------------------------------------------------------------
// 配置
// ---------------------------------------------------------------------------

/// HNSW 索引配置
#[derive(Debug, Clone)]
pub struct HnswConfig {
    /// 每个节点的最大邻居数(M 参数)
    /// 第 0 层的实际最大邻居数为 `2 * M`
    pub max_connections: usize,
    /// 搜索时的候选列表大小(efConstruction / efSearch)
    pub ef_construction: usize,
    /// 层级因子(level generation factor),通常为 `ln(2)`
    pub level_factor: f64,
    /// 距离度量
    pub metric: VectorMetric,
    /// 随机种子(用于可复现的层级生成)
    pub seed: u64,
}

impl Default for HnswConfig {
    fn default() -> Self {
        Self {
            max_connections: 16,
            ef_construction: 200,
            level_factor: std::f64::consts::LN_2,
            metric: VectorMetric::Cosine,
            seed: 42,
        }
    }
}

impl HnswConfig {
    /// 创建配置并指定度量方式
    pub fn with_metric(mut self, metric: VectorMetric) -> Self {
        self.metric = metric;
        self
    }

    /// 设置最大邻居数
    pub fn with_max_connections(mut self, m: usize) -> Self {
        self.max_connections = m;
        self
    }

    /// 设置构建时候选列表大小
    pub fn with_ef_construction(mut self, ef: usize) -> Self {
        self.ef_construction = ef;
        self
    }

    /// 设置随机种子
    pub fn with_seed(mut self, seed: u64) -> Self {
        self.seed = seed;
        self
    }
}

// ---------------------------------------------------------------------------
// 节点与搜索结果
// ---------------------------------------------------------------------------

/// 索引中的向量节点
#[derive(Debug, Clone)]
struct Node {
    /// 节点 ID
    id: String,
    /// 向量数据
    vector: Vec<f32>,
    /// 每层的邻居列表(layer 0 是底层,包含全部节点)
    neighbors: Vec<Vec<usize>>,
    /// 是否被标记删除(软删除)
    deleted: bool,
}

/// 搜索结果
#[derive(Debug, Clone)]
pub struct HnswSearchResult {
    pub id: String,
    pub score: f32,
    pub vector: Vec<f32>,
}

impl HnswSearchResult {
    pub fn new(id: impl Into<String>, score: f32, vector: Vec<f32>) -> Self {
        Self {
            id: id.into(),
            score,
            vector,
        }
    }
}

// ---------------------------------------------------------------------------
// 内部辅助:带距离的候选(用于 BinaryHeap,最大堆)
// ---------------------------------------------------------------------------

/// 候选节点:距离越大优先级越低(用于最大堆,先出队的是距离最大的)
#[derive(Debug, Clone, Copy)]
struct Candidate {
    distance: f32,
    node_idx: usize,
}

impl PartialEq for Candidate {
    fn eq(&self, other: &Self) -> bool {
        self.distance == other.distance && self.node_idx == other.node_idx
    }
}

impl Eq for Candidate {}

impl PartialOrd for Candidate {
    /// 最大堆:距离更大者优先
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

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

// ---------------------------------------------------------------------------
// 简单的确定性随机数生成器(xorshift64)
// ---------------------------------------------------------------------------

struct Rng {
    state: u64,
}

impl Rng {
    fn new(seed: u64) -> Self {
        Self {
            state: if seed == 0 { 0xdeadbeef } else { seed },
        }
    }

    fn next_u64(&mut self) -> u64 {
        let mut x = self.state;
        x ^= x << 13;
        x ^= x >> 7;
        x ^= x << 17;
        self.state = x;
        x
    }

    /// 生成 [0, 1) 范围的浮点数
    fn next_f64(&mut self) -> f64 {
        (self.next_u64() >> 11) as f64 / (1u64 << 53) as f64
    }
}

// ---------------------------------------------------------------------------
// HNSW 索引
// ---------------------------------------------------------------------------

/// HNSW 向量索引
///
/// 线程安全的多层图向量索引,支持近似最近邻搜索。
pub struct HnswIndex {
    /// 索引配置
    config: HnswConfig,
    /// 所有节点(按插入顺序索引)
    nodes: RwLock<Vec<Node>>,
    /// ID 到节点索引的映射
    id_to_idx: RwLock<HashMap<String, usize>>,
    /// 当前最大层级
    max_level: RwLock<usize>,
    /// 入口节点索引(顶层入口)
    entry_point: RwLock<Option<usize>>,
    /// 随机数生成器(用于层级生成)
    rng: RwLock<Rng>,
    /// 向量维度
    dimension: RwLock<usize>,
}

impl HnswIndex {
    /// 创建新的 HNSW 索引
    pub fn new(config: HnswConfig) -> Self {
        Self {
            config,
            nodes: RwLock::new(Vec::new()),
            id_to_idx: RwLock::new(HashMap::new()),
            max_level: RwLock::new(0),
            entry_point: RwLock::new(None),
            rng: RwLock::new(Rng::new(42)),
            dimension: RwLock::new(0),
        }
    }

    /// 获取当前索引的节点数量(不含已删除)
    pub fn len(&self) -> usize {
        let nodes = self.nodes.read();
        nodes.iter().filter(|n| !n.deleted).count()
    }

    /// 索引是否为空
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// 获取已配置的维度
    pub fn dimension(&self) -> usize {
        *self.dimension.read()
    }

    /// 计算两个向量之间的距离(根据配置的度量方式)
    fn distance(metric: VectorMetric, a: &[f32], b: &[f32]) -> f32 {
        match metric {
            VectorMetric::Cosine => {
                // 距离 = 1 - 相似度
                let sim = cosine_similarity(a, b);
                1.0 - sim
            }
            VectorMetric::Euclidean => {
                // 欧氏距离
                a.iter()
                    .zip(b.iter())
                    .map(|(x, y)| (x - y) * (x - y))
                    .sum::<f32>()
                    .sqrt()
            }
            VectorMetric::DotProduct => {
                // 负点积(越大越远)
                -a.iter().zip(b.iter()).map(|(x, y)| x * y).sum::<f32>()
            }
        }
    }

    /// 为新节点生成随机层级
    fn generate_level(&self) -> usize {
        let mut rng = self.rng.write();
        let factor = self.config.level_factor;
        if factor <= 0.0 {
            return 0;
        }
        let r = rng.next_f64();
        -(r.ln() / factor) as usize
    }

    /// 插入一个向量到索引中
    ///
    /// # 参数
    /// - `id`: 节点 ID(若已存在则更新向量)
    /// - `vector`: 向量数据
    pub fn insert(&self, id: &str, vector: Vec<f32>) -> Result<(), AiError> {
        if vector.is_empty() {
            return Err(AiError::Vector("向量不能为空".to_string()));
        }

        // 设置维度(首次插入时)
        {
            let mut dim = self.dimension.write();
            if *dim == 0 {
                *dim = vector.len();
            } else if *dim != vector.len() {
                return Err(AiError::Vector(format!(
                    "维度不匹配: 期望 {}, 实际 {}",
                    *dim,
                    vector.len()
                )));
            }
        }

        let level = self.generate_level();
        let new_node_idx;

        // 检查是否已存在同 ID 的节点
        {
            let id_map = self.id_to_idx.read();
            if let Some(&existing_idx) = id_map.get(id) {
                // 更新现有节点
                let mut nodes = self.nodes.write();
                let node = &mut nodes[existing_idx];
                node.vector = vector.clone();
                node.deleted = false;
                return Ok(());
            }
        }

        // 创建新节点
        {
            let mut nodes = self.nodes.write();
            new_node_idx = nodes.len();
            let neighbors = vec![Vec::new(); level + 1];
            nodes.push(Node {
                id: id.to_string(),
                vector: vector.clone(),
                neighbors,
                deleted: false,
            });
        }

        // 更新 ID 映射
        {
            let mut id_map = self.id_to_idx.write();
            id_map.insert(id.to_string(), new_node_idx);
        }

        // 连接到现有图
        let entry = *self.entry_point.read();
        if let Some(entry_idx) = entry {
            self.connect_node(new_node_idx, level, entry_idx)?;
        }

        // 更新入口点(如果新节点的层级更高)
        {
            let mut max_level = self.max_level.write();
            if level >= *max_level {
                *max_level = level + 1;
                let mut entry = self.entry_point.write();
                *entry = Some(new_node_idx);
            }
        }

        Ok(())
    }

    /// 将新节点连接到图中
    fn connect_node(
        &self,
        new_idx: usize,
        new_level: usize,
        entry_idx: usize,
    ) -> Result<(), AiError> {
        let max_level = *self.max_level.read();
        let ef = self.config.ef_construction;
        let m = self.config.max_connections;
        let metric = self.config.metric;

        // 从顶层贪心下降到 new_level + 1 层
        let mut current_entry = entry_idx;
        for layer in (new_level + 1..max_level).rev() {
            let result = self.search_layer(new_idx, current_entry, 1, layer, metric)?;
            if let Some((idx, _)) = result.into_iter().next() {
                current_entry = idx;
            }
        }

        // 从 new_level 层开始连接
        for layer in (0..=new_level).rev() {
            // 搜索 ef 个最近邻居
            let candidates = self.search_layer(new_idx, current_entry, ef, layer, metric)?;

            // 选择 M 个最近的作为邻居
            let max_neighbors = if layer == 0 { 2 * m } else { m };
            let selected: Vec<usize> = candidates
                .into_iter()
                .take(max_neighbors)
                .map(|(idx, _)| idx)
                .collect();

            // 设置新节点的邻居
            {
                let mut nodes = self.nodes.write();
                if new_idx < nodes.len() && layer < nodes[new_idx].neighbors.len() {
                    nodes[new_idx].neighbors[layer] = selected.clone();
                }
            }

            // 反向连接:将新节点添加到邻居的邻居列表中
            for &neighbor_idx in &selected {
                let mut nodes = self.nodes.write();
                if neighbor_idx < nodes.len() && layer < nodes[neighbor_idx].neighbors.len() {
                    let neighbor_list = &mut nodes[neighbor_idx].neighbors[layer];
                    if !neighbor_list.contains(&new_idx) {
                        neighbor_list.push(new_idx);
                        // 如果邻居列表超过最大值,修剪到 max_neighbors
                        let max = if layer == 0 { 2 * m } else { m };
                        if neighbor_list.len() > max {
                            // 简单修剪:保留前 max 个(更优实现应保留距离最近的)
                            neighbor_list.truncate(max);
                        }
                    }
                }
            }

            current_entry = selected.first().copied().unwrap_or(current_entry);
        }

        Ok(())
    }

    /// 在单层中搜索最近的 ef 个节点
    ///
    /// 返回值:Vec<(节点索引, 距离)>,按距离升序排列
    fn search_layer(
        &self,
        query_idx: usize,
        entry_idx: usize,
        ef: usize,
        layer: usize,
        metric: VectorMetric,
    ) -> Result<Vec<(usize, f32)>, AiError> {
        let nodes = self.nodes.read();

        if query_idx >= nodes.len() || entry_idx >= nodes.len() {
            return Ok(Vec::new());
        }

        let query_vector = nodes[query_idx].vector.clone();
        drop(nodes);

        let mut visited = HashSet::new();
        visited.insert(entry_idx);

        // 候选堆(最小堆,距离最小者优先)
        let mut candidates: BinaryHeap<std::cmp::Reverse<Candidate>> = BinaryHeap::new();
        // 结果堆(最大堆,距离最大者优先,便于淘汰)
        let mut results: BinaryHeap<Candidate> = BinaryHeap::new();

        let entry_dist = {
            let nodes = self.nodes.read();
            Self::distance(metric, &query_vector, &nodes[entry_idx].vector)
        };
        candidates.push(std::cmp::Reverse(Candidate {
            distance: entry_dist,
            node_idx: entry_idx,
        }));
        results.push(Candidate {
            distance: entry_dist,
            node_idx: entry_idx,
        });

        while let Some(std::cmp::Reverse(cand)) = candidates.pop() {
            // 如果候选比结果中最差的还要远,停止
            if let Some(worst) = results.peek() {
                if cand.distance > worst.distance && results.len() >= ef {
                    break;
                }
            }

            // 检查候选的邻居
            let neighbor_ids = {
                let nodes = self.nodes.read();
                if cand.node_idx >= nodes.len() || layer >= nodes[cand.node_idx].neighbors.len() {
                    continue;
                }
                nodes[cand.node_idx].neighbors[layer].clone()
            };

            for &neighbor_idx in &neighbor_ids {
                if visited.contains(&neighbor_idx) {
                    continue;
                }
                visited.insert(neighbor_idx);

                let dist = {
                    let nodes = self.nodes.read();
                    if neighbor_idx >= nodes.len() {
                        continue;
                    }
                    Self::distance(metric, &query_vector, &nodes[neighbor_idx].vector)
                };

                // 如果结果未满,或比最差结果好
                let should_add =
                    results.len() < ef || results.peek().map(|w| dist < w.distance).unwrap_or(true);

                if should_add {
                    candidates.push(std::cmp::Reverse(Candidate {
                        distance: dist,
                        node_idx: neighbor_idx,
                    }));
                    results.push(Candidate {
                        distance: dist,
                        node_idx: neighbor_idx,
                    });

                    // 维持结果集大小
                    while results.len() > ef {
                        results.pop();
                    }
                }
            }
        }

        // 转换为升序排列的结果
        let mut result_vec: Vec<(usize, f32)> = results
            .into_iter()
            .map(|c| (c.node_idx, c.distance))
            .collect();
        result_vec.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
        Ok(result_vec)
    }

    /// 搜索最近邻
    ///
    /// # 参数
    /// - `query`: 查询向量
    /// - `top_k`: 返回结果数量
    /// - `ef_search`: 搜索时候选列表大小(越大越精确但越慢)
    pub fn search(
        &self,
        query: &[f32],
        top_k: usize,
        ef_search: usize,
    ) -> Result<Vec<HnswSearchResult>, AiError> {
        if query.is_empty() {
            return Err(AiError::Vector("查询向量不能为空".to_string()));
        }

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

        let dim = *self.dimension.read();
        if dim != 0 && dim != query.len() {
            return Err(AiError::Vector(format!(
                "查询向量维度不匹配: 期望 {}, 实际 {}",
                dim,
                query.len()
            )));
        }

        let metric = self.config.metric;
        let max_level = *self.max_level.read();
        let ef = ef_search.max(top_k);

        // 从顶层贪心下降到第 1 层
        let mut current_entry = entry_idx;
        for layer in (1..max_level).rev() {
            let result = self.search_layer_query(query, current_entry, 1, layer, metric)?;
            if let Some((idx, _)) = result.into_iter().next() {
                current_entry = idx;
            }
        }

        // 在第 0 层进行 ef 搜索
        let candidates = self.search_layer_query(query, current_entry, ef, 0, metric)?;

        // 转换为搜索结果
        let nodes = self.nodes.read();
        let results: Vec<HnswSearchResult> = candidates
            .into_iter()
            .filter(|(idx, _)| !nodes.get(*idx).map(|n| n.deleted).unwrap_or(true))
            .take(top_k)
            .filter_map(|(idx, dist)| {
                let node = nodes.get(idx)?;
                if node.deleted {
                    return None;
                }
                // 将距离转换为分数(cosine: 1 - dist; euclidean: 1/(1+dist); dotproduct: -dist)
                let score = match metric {
                    VectorMetric::Cosine => 1.0 - dist,
                    VectorMetric::Euclidean => 1.0 / (1.0 + dist),
                    VectorMetric::DotProduct => -dist,
                };
                Some(HnswSearchResult {
                    id: node.id.clone(),
                    score,
                    vector: node.vector.clone(),
                })
            })
            .collect();

        Ok(results)
    }

    /// 在单层中搜索(用于查询,query 是外部向量而非索引内节点)
    fn search_layer_query(
        &self,
        query: &[f32],
        entry_idx: usize,
        ef: usize,
        layer: usize,
        metric: VectorMetric,
    ) -> Result<Vec<(usize, f32)>, AiError> {
        let nodes = self.nodes.read();

        if entry_idx >= nodes.len() {
            return Ok(Vec::new());
        }
        if layer >= nodes[entry_idx].neighbors.len() {
            return Ok(vec![(
                entry_idx,
                Self::distance(metric, query, &nodes[entry_idx].vector),
            )]);
        }

        let entry_dist = Self::distance(metric, query, &nodes[entry_idx].vector);
        drop(nodes);

        let mut visited = HashSet::new();
        visited.insert(entry_idx);

        let mut candidates: BinaryHeap<std::cmp::Reverse<Candidate>> = BinaryHeap::new();
        let mut results: BinaryHeap<Candidate> = BinaryHeap::new();

        candidates.push(std::cmp::Reverse(Candidate {
            distance: entry_dist,
            node_idx: entry_idx,
        }));
        results.push(Candidate {
            distance: entry_dist,
            node_idx: entry_idx,
        });

        while let Some(std::cmp::Reverse(cand)) = candidates.pop() {
            if let Some(worst) = results.peek() {
                if cand.distance > worst.distance && results.len() >= ef {
                    break;
                }
            }

            let neighbor_ids = {
                let nodes = self.nodes.read();
                if cand.node_idx >= nodes.len() || layer >= nodes[cand.node_idx].neighbors.len() {
                    continue;
                }
                nodes[cand.node_idx].neighbors[layer].clone()
            };

            for &neighbor_idx in &neighbor_ids {
                if visited.contains(&neighbor_idx) {
                    continue;
                }
                visited.insert(neighbor_idx);

                let dist = {
                    let nodes = self.nodes.read();
                    if neighbor_idx >= nodes.len() || nodes[neighbor_idx].deleted {
                        continue;
                    }
                    Self::distance(metric, query, &nodes[neighbor_idx].vector)
                };

                let should_add =
                    results.len() < ef || results.peek().map(|w| dist < w.distance).unwrap_or(true);

                if should_add {
                    candidates.push(std::cmp::Reverse(Candidate {
                        distance: dist,
                        node_idx: neighbor_idx,
                    }));
                    results.push(Candidate {
                        distance: dist,
                        node_idx: neighbor_idx,
                    });

                    while results.len() > ef {
                        results.pop();
                    }
                }
            }
        }

        let mut result_vec: Vec<(usize, f32)> = results
            .into_iter()
            .map(|c| (c.node_idx, c.distance))
            .collect();
        result_vec.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
        Ok(result_vec)
    }

    /// 标记删除节点(软删除)
    ///
    /// 被删除的节点不会出现在搜索结果中,但仍保留在图中以维持连接性。
    pub fn delete(&self, id: &str) -> Result<bool, AiError> {
        let id_map = self.id_to_idx.read();
        let idx = match id_map.get(id) {
            Some(&idx) => idx,
            None => return Ok(false),
        };
        drop(id_map);

        let mut nodes = self.nodes.write();
        if idx < nodes.len() {
            let was_deleted = nodes[idx].deleted;
            nodes[idx].deleted = true;
            Ok(!was_deleted)
        } else {
            Ok(false)
        }
    }

    /// 获取指定 ID 的向量
    pub fn get(&self, id: &str) -> Option<Vec<f32>> {
        let id_map = self.id_to_idx.read();
        let idx = id_map.get(id)?;
        let nodes = self.nodes.read();
        nodes
            .get(*idx)
            .filter(|n| !n.deleted)
            .map(|n| n.vector.clone())
    }

    /// 获取索引中所有有效 ID
    pub fn ids(&self) -> Vec<String> {
        let nodes = self.nodes.read();
        nodes
            .iter()
            .filter(|n| !n.deleted)
            .map(|n| n.id.clone())
            .collect()
    }
}

// ---------------------------------------------------------------------------
// 辅助函数
// ---------------------------------------------------------------------------

/// 计算余弦相似度
fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() || a.is_empty() {
        return 0.0;
    }
    let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
    let na: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let nb: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();
    if na == 0.0 || nb == 0.0 {
        return 0.0;
    }
    dot / (na * nb)
}

// ---------------------------------------------------------------------------
// 单元测试
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_hnsw_config_default() {
        let config = HnswConfig::default();
        assert_eq!(config.max_connections, 16);
        assert_eq!(config.ef_construction, 200);
        assert_eq!(config.metric, VectorMetric::Cosine);
    }

    #[test]
    fn test_hnsw_config_builder() {
        let config = HnswConfig::default()
            .with_metric(VectorMetric::Euclidean)
            .with_max_connections(8)
            .with_ef_construction(100)
            .with_seed(123);
        assert_eq!(config.max_connections, 8);
        assert_eq!(config.ef_construction, 100);
        assert_eq!(config.metric, VectorMetric::Euclidean);
        assert_eq!(config.seed, 123);
    }

    #[test]
    fn test_hnsw_empty_index() {
        let index = HnswIndex::new(HnswConfig::default());
        assert!(index.is_empty());
        assert_eq!(index.len(), 0);
        assert_eq!(index.dimension(), 0);
    }

    #[test]
    fn test_hnsw_insert_single() {
        let index = HnswIndex::new(HnswConfig::default());
        index.insert("v1", vec![1.0, 0.0, 0.0]).unwrap();
        assert_eq!(index.len(), 1);
        assert_eq!(index.dimension(), 3);
        assert!(!index.is_empty());
    }

    #[test]
    fn test_hnsw_insert_multiple() {
        let index = HnswIndex::new(HnswConfig::default());
        for i in 0..10 {
            index
                .insert(&format!("v{}", i), vec![i as f32, 1.0, 0.0])
                .unwrap();
        }
        assert_eq!(index.len(), 10);
        assert_eq!(index.dimension(), 3);
    }

    #[test]
    fn test_hnsw_insert_empty_vector_fails() {
        let index = HnswIndex::new(HnswConfig::default());
        let result = index.insert("v1", vec![]);
        assert!(result.is_err());
    }

    #[test]
    fn test_hnsw_insert_dimension_mismatch_fails() {
        let index = HnswIndex::new(HnswConfig::default());
        index.insert("v1", vec![1.0, 0.0, 0.0]).unwrap();
        let result = index.insert("v2", vec![1.0, 0.0]);
        assert!(result.is_err());
    }

    #[test]
    fn test_hnsw_insert_duplicate_id_updates() {
        let index = HnswIndex::new(HnswConfig::default());
        index.insert("v1", vec![1.0, 0.0, 0.0]).unwrap();
        index.insert("v1", vec![0.0, 1.0, 0.0]).unwrap();
        // 应该只保留一个节点
        assert_eq!(index.len(), 1);
        let vector = index.get("v1").unwrap();
        assert_eq!(vector, vec![0.0, 1.0, 0.0]);
    }

    #[test]
    fn test_hnsw_search_empty_index() {
        let index = HnswIndex::new(HnswConfig::default());
        let results = index.search(&[1.0, 0.0], 5, 50).unwrap();
        assert!(results.is_empty());
    }

    #[test]
    fn test_hnsw_search_single_node() {
        let index = HnswIndex::new(HnswConfig::default());
        index.insert("v1", vec![1.0, 0.0, 0.0]).unwrap();
        let results = index.search(&[1.0, 0.0, 0.0], 1, 50).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].id, "v1");
    }

    #[test]
    fn test_hnsw_search_cosine_metric() {
        let mut config = HnswConfig::default();
        config = config.with_metric(VectorMetric::Cosine);
        let index = HnswIndex::new(config);

        // 插入 3 个向量
        index.insert("a", vec![1.0, 0.0, 0.0]).unwrap();
        index.insert("b", vec![0.0, 1.0, 0.0]).unwrap();
        index.insert("c", vec![1.0, 1.0, 0.0]).unwrap();

        // 搜索与 [1, 0, 0] 最相似的 2 个
        let results = index.search(&[1.0, 0.0, 0.0], 2, 50).unwrap();
        assert_eq!(results.len(), 2);
        // 最相似的应该是 "a"
        assert_eq!(results[0].id, "a");
        // cosine similarity 应接近 1.0
        assert!((results[0].score - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_hnsw_search_euclidean_metric() {
        let config = HnswConfig::default().with_metric(VectorMetric::Euclidean);
        let index = HnswIndex::new(config);

        index.insert("a", vec![1.0, 0.0]).unwrap();
        index.insert("b", vec![5.0, 0.0]).unwrap();
        index.insert("c", vec![10.0, 0.0]).unwrap();

        let results = index.search(&[1.0, 0.0], 2, 50).unwrap();
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].id, "a");
    }

    #[test]
    fn test_hnsw_search_dotproduct_metric() {
        let config = HnswConfig::default().with_metric(VectorMetric::DotProduct);
        let index = HnswIndex::new(config);

        index.insert("a", vec![1.0, 1.0]).unwrap();
        index.insert("b", vec![2.0, 2.0]).unwrap();
        index.insert("c", vec![0.1, 0.1]).unwrap();

        let results = index.search(&[1.0, 1.0], 3, 50).unwrap();
        // 点积最大的应该是 b (dot=4),其次是 a (dot=2),最后是 c (dot=0.2)
        assert_eq!(results[0].id, "b");
    }

    #[test]
    fn test_hnsw_search_top_k_limit() {
        let index = HnswIndex::new(HnswConfig::default());
        for i in 0..20 {
            index
                .insert(&format!("v{}", i), vec![i as f32, 0.0, 0.0])
                .unwrap();
        }

        let results = index.search(&[5.0, 0.0, 0.0], 5, 50).unwrap();
        assert_eq!(results.len(), 5);
    }

    #[test]
    fn test_hnsw_search_larger_dataset() {
        let index = HnswIndex::new(HnswConfig::default().with_max_connections(8));

        // 插入 100 个随机向量
        for i in 0..100 {
            let x = (i as f32).sin();
            let y = (i as f32).cos();
            index.insert(&format!("v{}", i), vec![x, y, 0.0]).unwrap();
        }

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

        // 搜索
        let results = index.search(&[0.0, 1.0, 0.0], 5, 100).unwrap();
        assert_eq!(results.len(), 5);
        // 第一个结果应该是与 [0, 1, 0] 最接近的(i=0 时 cos(0)=1)
        assert_eq!(results[0].id, "v0");
    }

    #[test]
    fn test_hnsw_delete_node() {
        let index = HnswIndex::new(HnswConfig::default());
        index.insert("a", vec![1.0, 0.0]).unwrap();
        index.insert("b", vec![0.0, 1.0]).unwrap();
        index.insert("c", vec![1.0, 1.0]).unwrap();

        // 删除 b
        let deleted = index.delete("b").unwrap();
        assert!(deleted);

        // 长度应减少
        assert_eq!(index.len(), 2);

        // 搜索不应返回 b
        let results = index.search(&[0.0, 1.0], 3, 50).unwrap();
        for r in &results {
            assert_ne!(r.id, "b");
        }
    }

    #[test]
    fn test_hnsw_delete_nonexistent() {
        let index = HnswIndex::new(HnswConfig::default());
        let result = index.delete("nonexistent").unwrap();
        assert!(!result);
    }

    #[test]
    fn test_hnsw_get_vector() {
        let index = HnswIndex::new(HnswConfig::default());
        index.insert("v1", vec![1.0, 2.0, 3.0]).unwrap();

        let vector = index.get("v1").unwrap();
        assert_eq!(vector, vec![1.0, 2.0, 3.0]);

        // 不存在的 ID
        assert!(index.get("nonexistent").is_none());
    }

    #[test]
    fn test_hnsw_get_deleted_returns_none() {
        let index = HnswIndex::new(HnswConfig::default());
        index.insert("v1", vec![1.0, 0.0]).unwrap();
        index.delete("v1").unwrap();

        assert!(index.get("v1").is_none());
    }

    #[test]
    fn test_hnsw_ids() {
        let index = HnswIndex::new(HnswConfig::default());
        index.insert("a", vec![1.0, 0.0]).unwrap();
        index.insert("b", vec![0.0, 1.0]).unwrap();
        index.insert("c", vec![1.0, 1.0]).unwrap();

        let mut ids = index.ids();
        ids.sort();
        assert_eq!(ids, vec!["a".to_string(), "b".to_string(), "c".to_string()]);

        // 删除后
        index.delete("b").unwrap();
        let mut ids = index.ids();
        ids.sort();
        assert_eq!(ids, vec!["a".to_string(), "c".to_string()]);
    }

    #[test]
    fn test_hnsw_search_empty_query_fails() {
        let index = HnswIndex::new(HnswConfig::default());
        index.insert("v1", vec![1.0, 0.0]).unwrap();
        let result = index.search(&[], 1, 50);
        assert!(result.is_err());
    }

    #[test]
    fn test_hnsw_search_dimension_mismatch_fails() {
        let index = HnswIndex::new(HnswConfig::default());
        index.insert("v1", vec![1.0, 0.0, 0.0]).unwrap();
        let result = index.search(&[1.0, 0.0], 1, 50);
        assert!(result.is_err());
    }

    #[test]
    fn test_hnsw_rng_deterministic() {
        let mut rng1 = Rng::new(42);
        let mut rng2 = Rng::new(42);
        assert_eq!(rng1.next_u64(), rng2.next_u64());
        assert_eq!(rng1.next_u64(), rng2.next_u64());
    }

    #[test]
    fn test_hnsw_rng_zero_seed() {
        let mut rng = Rng::new(0);
        // 不应 panic,且应产生不同的值
        let v1 = rng.next_u64();
        let v2 = rng.next_u64();
        assert_ne!(v1, v2);
    }

    #[test]
    fn test_hnsw_candidate_ord() {
        let c1 = Candidate {
            distance: 0.5,
            node_idx: 0,
        };
        let c2 = Candidate {
            distance: 1.0,
            node_idx: 1,
        };
        // 距离更大的应排在前面(最大堆)
        assert!(c2 > c1);
    }

    #[test]
    fn test_cosine_similarity_identical() {
        let sim = cosine_similarity(&[1.0, 0.0, 0.0], &[1.0, 0.0, 0.0]);
        assert!((sim - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_cosine_similarity_orthogonal() {
        let sim = cosine_similarity(&[1.0, 0.0], &[0.0, 1.0]);
        assert!(sim.abs() < 1e-6);
    }

    #[test]
    fn test_cosine_similarity_opposite() {
        let sim = cosine_similarity(&[1.0, 0.0], &[-1.0, 0.0]);
        assert!((sim + 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_cosine_similarity_empty() {
        let sim = cosine_similarity(&[], &[]);
        assert_eq!(sim, 0.0);
    }

    #[test]
    fn test_cosine_similarity_different_lengths() {
        let sim = cosine_similarity(&[1.0, 0.0], &[1.0]);
        assert_eq!(sim, 0.0);
    }

    #[test]
    fn test_cosine_similarity_zero_vector() {
        let sim = cosine_similarity(&[0.0, 0.0], &[1.0, 0.0]);
        assert_eq!(sim, 0.0);
    }

    #[test]
    fn test_hnsw_search_after_reinsert() {
        let index = HnswIndex::new(HnswConfig::default());
        index.insert("a", vec![1.0, 0.0]).unwrap();
        index.insert("b", vec![0.0, 1.0]).unwrap();

        // 删除 a
        index.delete("a").unwrap();
        assert_eq!(index.len(), 1);

        // 重新插入 a
        index.insert("a", vec![1.0, 0.0]).unwrap();
        assert_eq!(index.len(), 2);

        // 搜索应能找到 a
        let results = index.search(&[1.0, 0.0], 2, 50).unwrap();
        assert!(results.iter().any(|r| r.id == "a"));
    }

    #[test]
    fn test_hnsw_insert_high_dimensional() {
        let config = HnswConfig::default().with_max_connections(4);
        let index = HnswIndex::new(config);

        // 插入 10 维向量
        for i in 0..20 {
            let mut vector = vec![0.0f32; 10];
            vector[i % 10] = 1.0;
            index.insert(&format!("v{}", i), vector).unwrap();
        }

        assert_eq!(index.len(), 20);
        assert_eq!(index.dimension(), 10);

        let query = {
            let mut v = vec![0.0f32; 10];
            v[0] = 1.0;
            v
        };
        let results = index.search(&query, 3, 50).unwrap();
        assert_eq!(results.len(), 3);
    }
}