solidb 1.0.2

A lightweight, high-performance structured database server written in Rust.
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
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
//! Vector index module for similarity search.
//!
//! This module provides vector similarity search capabilities for documents
//! containing embedding vectors. Supports two modes:
//!
//! - **Brute-force**: Accurate linear search, fast for small datasets (< 10K vectors)
//! - **HNSW**: Approximate nearest neighbor search for large datasets (10K+ vectors)
//!
//! The index automatically switches to HNSW when the vector count exceeds the
//! configured threshold (default: 10,000).

use crate::error::{DbError, DbResult};
use crate::storage::index::{
    VectorIndexConfig, VectorIndexStats, VectorMetric, VectorQuantization,
};
use rand::prelude::*;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::sync::RwLock;

/// Result of a vector similarity search
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VectorSearchResult {
    /// Document key
    pub doc_key: String,
    /// Similarity/distance score (interpretation depends on metric)
    pub score: f32,
}

/// Default threshold for auto-switching to HNSW
const DEFAULT_HNSW_THRESHOLD: usize = 10_000;

// =============================================================================
// Scalar Quantization
// =============================================================================

/// Parameters for scalar quantization (per-dimension min/max for reconstruction)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScalarQuantParams {
    /// Minimum value per dimension
    pub min_vals: Vec<f32>,
    /// Maximum value per dimension
    pub max_vals: Vec<f32>,
}

/// Quantized vector storage using scalar quantization (u8 per dimension)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantizedVectors {
    /// Quantized vectors: doc_key -> u8 array
    vectors: HashMap<String, Vec<u8>>,
    /// Quantization parameters for reconstruction
    params: ScalarQuantParams,
}

impl QuantizedVectors {
    /// Create new quantized storage from full-precision vectors
    pub fn from_full_vectors(vectors: &HashMap<String, Vec<f32>>, dimension: usize) -> Self {
        if vectors.is_empty() {
            return Self {
                vectors: HashMap::new(),
                params: ScalarQuantParams {
                    min_vals: vec![0.0; dimension],
                    max_vals: vec![1.0; dimension],
                },
            };
        }

        // Compute min/max per dimension across all vectors
        let mut min_vals = vec![f32::MAX; dimension];
        let mut max_vals = vec![f32::MIN; dimension];

        for vec in vectors.values() {
            for (i, &v) in vec.iter().enumerate() {
                min_vals[i] = min_vals[i].min(v);
                max_vals[i] = max_vals[i].max(v);
            }
        }

        // Quantize all vectors
        let quantized: HashMap<String, Vec<u8>> = vectors
            .iter()
            .map(|(k, v)| (k.clone(), Self::quantize_vector(v, &min_vals, &max_vals)))
            .collect();

        Self {
            vectors: quantized,
            params: ScalarQuantParams { min_vals, max_vals },
        }
    }

    /// Quantize a single f32 vector to u8
    fn quantize_vector(vec: &[f32], min_vals: &[f32], max_vals: &[f32]) -> Vec<u8> {
        vec.iter()
            .enumerate()
            .map(|(i, &v)| {
                let range = max_vals[i] - min_vals[i];
                if range < 1e-10 {
                    127u8 // Middle value if no range
                } else {
                    ((v - min_vals[i]) / range * 255.0).clamp(0.0, 255.0) as u8
                }
            })
            .collect()
    }

    /// Dequantize a single u8 vector back to f32
    #[allow(dead_code)]
    pub fn dequantize_vector(&self, quantized: &[u8]) -> Vec<f32> {
        quantized
            .iter()
            .enumerate()
            .map(|(i, &q)| {
                let range = self.params.max_vals[i] - self.params.min_vals[i];
                self.params.min_vals[i] + (q as f32 / 255.0) * range
            })
            .collect()
    }

    /// Insert a new vector (quantize on insert)
    pub fn insert(&mut self, doc_key: &str, vec: &[f32]) {
        let quantized = Self::quantize_vector(vec, &self.params.min_vals, &self.params.max_vals);
        self.vectors.insert(doc_key.to_string(), quantized);
    }

    /// Remove a vector
    pub fn remove(&mut self, doc_key: &str) {
        self.vectors.remove(doc_key);
    }

    /// Get number of quantized vectors
    pub fn len(&self) -> usize {
        self.vectors.len()
    }

    /// Check if empty
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.vectors.is_empty()
    }

    /// Get parameters for distance calculation
    pub fn params(&self) -> &ScalarQuantParams {
        &self.params
    }

    /// Get all quantized vectors
    pub fn vectors(&self) -> &HashMap<String, Vec<u8>> {
        &self.vectors
    }

    /// Clear all quantized vectors
    pub fn clear(&mut self) {
        self.vectors.clear();
    }
}

// =============================================================================
// Asymmetric Distance Functions (query f32, DB u8)
// =============================================================================

/// Calculate cosine similarity between full-precision query and quantized vector
pub fn cosine_similarity_asymmetric(
    query: &[f32],
    quantized: &[u8],
    params: &ScalarQuantParams,
) -> f32 {
    let query_norm_sq: f32 = query.iter().map(|x| x * x).sum();
    if query_norm_sq < 1e-10 {
        return 0.0;
    }

    let mut dot = 0.0f32;
    let mut quantized_norm_sq = 0.0f32;

    for i in 0..query.len() {
        let range = params.max_vals[i] - params.min_vals[i];
        let dequant = params.min_vals[i] + (quantized[i] as f32 / 255.0) * range;
        dot += query[i] * dequant;
        quantized_norm_sq += dequant * dequant;
    }

    let query_norm = query_norm_sq.sqrt();
    let quantized_norm = quantized_norm_sq.sqrt().max(1e-10);
    dot / (query_norm * quantized_norm)
}

/// Calculate Euclidean distance between full-precision query and quantized vector
pub fn euclidean_distance_asymmetric(
    query: &[f32],
    quantized: &[u8],
    params: &ScalarQuantParams,
) -> f32 {
    let mut sum_sq = 0.0f32;

    for i in 0..query.len() {
        let range = params.max_vals[i] - params.min_vals[i];
        let dequant = params.min_vals[i] + (quantized[i] as f32 / 255.0) * range;
        let diff = query[i] - dequant;
        sum_sq += diff * diff;
    }

    sum_sq.sqrt()
}

/// Calculate dot product between full-precision query and quantized vector
pub fn dot_product_asymmetric(query: &[f32], quantized: &[u8], params: &ScalarQuantParams) -> f32 {
    let mut dot = 0.0f32;

    for i in 0..query.len() {
        let range = params.max_vals[i] - params.min_vals[i];
        let dequant = params.min_vals[i] + (quantized[i] as f32 / 255.0) * range;
        dot += query[i] * dequant;
    }

    dot
}

// =============================================================================
// HNSW Data Structures
// =============================================================================

/// HNSW node representing a vector in the graph
#[derive(Debug, Clone, Serialize, Deserialize)]
struct HnswNode {
    /// Connections at each level: level -> vec of neighbor doc_keys
    neighbors: Vec<Vec<String>>,
    /// Level this node was assigned during insertion
    level: usize,
}

/// HNSW graph structure for approximate nearest neighbor search
#[derive(Debug, Clone, Serialize, Deserialize)]
struct HnswGraph {
    /// All nodes in the graph: doc_key -> HnswNode
    nodes: HashMap<String, HnswNode>,
    /// Current entry point (highest level node)
    entry_point: Option<String>,
    /// Current maximum level in the graph
    max_level: usize,
    /// M parameter: max connections per node per level (except level 0)
    m: usize,
    /// M0 parameter: max connections at level 0 (typically 2*M)
    m0: usize,
    /// ef_construction: search quality during build
    ef_construction: usize,
    /// Level multiplier: 1/ln(M), used for random level assignment
    level_mult: f64,
}

/// Neighbor entry for priority queue operations during HNSW search
#[derive(Clone)]
struct Neighbor {
    doc_key: String,
    distance: f32,
}

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

impl Eq for Neighbor {}

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

impl Ord for Neighbor {
    fn cmp(&self, other: &Self) -> Ordering {
        // Reverse ordering: smaller distance = higher priority (max-heap becomes min-heap)
        other
            .distance
            .partial_cmp(&self.distance)
            .unwrap_or(Ordering::Equal)
    }
}

/// Wrapper for max-heap behavior (largest distance first)
#[derive(Clone)]
struct MaxNeighbor(Neighbor);

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

impl Eq for MaxNeighbor {}

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

impl Ord for MaxNeighbor {
    fn cmp(&self, other: &Self) -> Ordering {
        // Normal ordering: larger distance = higher priority
        self.0
            .distance
            .partial_cmp(&other.0.distance)
            .unwrap_or(Ordering::Equal)
    }
}

// =============================================================================
// HnswGraph Implementation
// =============================================================================

impl HnswGraph {
    /// Create a new HNSW graph with the given configuration
    fn new(m: usize, ef_construction: usize) -> Self {
        let m = m.max(2); // Minimum M of 2
        Self {
            nodes: HashMap::new(),
            entry_point: None,
            max_level: 0,
            m,
            m0: m * 2, // Level 0 has double the connections
            ef_construction,
            level_mult: 1.0 / (m as f64).ln(),
        }
    }

    /// Generate a random level for a new node using exponential distribution
    fn random_level(&self) -> usize {
        let mut rng = rand::thread_rng();
        let r: f64 = rng.gen();
        let level = (-r.ln() * self.level_mult).floor() as usize;
        level.min(16)
    }

    /// Calculate distance between query and a document's vector
    fn calculate_distance(
        query: &[f32],
        doc_key: &str,
        vectors: &HashMap<String, Vec<f32>>,
        metric: VectorMetric,
    ) -> f32 {
        match vectors.get(doc_key) {
            Some(vec) => match metric {
                // For HNSW we need distance (lower is better), so convert similarity
                VectorMetric::Cosine => 1.0 - cosine_similarity(query, vec),
                VectorMetric::Euclidean => euclidean_distance(query, vec),
                VectorMetric::DotProduct => -dot_product(query, vec), // Negate for "lower is better"
            },
            None => f32::MAX,
        }
    }

    /// Search within a single layer, returning up to ef nearest neighbors
    fn search_layer(
        &self,
        query: &[f32],
        entry_points: &[String],
        ef: usize,
        level: usize,
        vectors: &HashMap<String, Vec<f32>>,
        metric: VectorMetric,
    ) -> Vec<Neighbor> {
        let mut visited: HashSet<String> = HashSet::new();
        // Min-heap: best candidates (smallest distance first)
        let mut candidates: BinaryHeap<Neighbor> = BinaryHeap::new();
        // Max-heap: worst of the current best (largest distance first)
        let mut result: BinaryHeap<MaxNeighbor> = BinaryHeap::new();

        // Initialize with entry points
        for ep in entry_points {
            if visited.insert(ep.clone()) {
                let dist = Self::calculate_distance(query, ep, vectors, metric);
                let neighbor = Neighbor {
                    doc_key: ep.clone(),
                    distance: dist,
                };
                candidates.push(neighbor.clone());
                result.push(MaxNeighbor(neighbor));
            }
        }

        while let Some(current) = candidates.pop() {
            // Get the furthest element in result
            let furthest_dist = result.peek().map(|n| n.0.distance).unwrap_or(f32::MAX);

            // If current is further than the furthest result, we're done
            if current.distance > furthest_dist {
                break;
            }

            // Get neighbors at this level
            if let Some(node) = self.nodes.get(&current.doc_key) {
                if level < node.neighbors.len() {
                    for neighbor_key in &node.neighbors[level] {
                        if visited.insert(neighbor_key.clone()) {
                            let dist =
                                Self::calculate_distance(query, neighbor_key, vectors, metric);
                            let furthest_dist =
                                result.peek().map(|n| n.0.distance).unwrap_or(f32::MAX);

                            if dist < furthest_dist || result.len() < ef {
                                let neighbor = Neighbor {
                                    doc_key: neighbor_key.clone(),
                                    distance: dist,
                                };
                                candidates.push(neighbor.clone());
                                result.push(MaxNeighbor(neighbor));

                                // Keep only ef best
                                while result.len() > ef {
                                    result.pop();
                                }
                            }
                        }
                    }
                }
            }
        }

        // Convert result to vec, sorted by distance (best first)
        let mut results: Vec<Neighbor> = result.into_iter().map(|mn| mn.0).collect();
        results.sort_by(|a, b| {
            a.distance
                .partial_cmp(&b.distance)
                .unwrap_or(Ordering::Equal)
        });
        results
    }

    /// Select M best neighbors using the simple heuristic
    fn select_neighbors(&self, candidates: &[Neighbor], m: usize) -> Vec<String> {
        // Simple heuristic: just take the M closest
        candidates
            .iter()
            .take(m)
            .map(|n| n.doc_key.clone())
            .collect()
    }

    /// Insert a new vector into the graph
    fn insert(
        &mut self,
        doc_key: &str,
        _vector: &[f32],
        vectors: &HashMap<String, Vec<f32>>,
        metric: VectorMetric,
    ) {
        // If this key already exists, remove it first (update case)
        if self.nodes.contains_key(doc_key) {
            self.remove(doc_key);
        }

        // Get the vector for distance calculations
        let query = match vectors.get(doc_key) {
            Some(v) => v,
            None => return, // Vector not found, skip
        };

        // Assign random level to this node
        let node_level = self.random_level();

        // Create the new node with empty neighbor lists
        let mut new_node = HnswNode {
            neighbors: vec![Vec::new(); node_level + 1],
            level: node_level,
        };

        // If graph is empty, this becomes the entry point
        if self.entry_point.is_none() {
            self.entry_point = Some(doc_key.to_string());
            self.max_level = node_level;
            self.nodes.insert(doc_key.to_string(), new_node);
            return;
        }

        let entry_point = self.entry_point.clone().unwrap();
        let mut current_ep = vec![entry_point];

        // Phase 1: Descend from top to node_level+1, using greedy search (ef=1)
        for level in (node_level + 1..=self.max_level).rev() {
            let nearest = self.search_layer(query, &current_ep, 1, level, vectors, metric);
            if !nearest.is_empty() {
                current_ep = vec![nearest[0].doc_key.clone()];
            }
        }

        // Phase 2: From node_level down to 0, find and connect neighbors
        for level in (0..=node_level.min(self.max_level)).rev() {
            let m_level = if level == 0 { self.m0 } else { self.m };

            // Search for nearest neighbors at this level
            let candidates = self.search_layer(
                query,
                &current_ep,
                self.ef_construction,
                level,
                vectors,
                metric,
            );

            // Select best neighbors
            let selected = self.select_neighbors(&candidates, m_level);

            // Add connections from new node to selected neighbors
            if level < new_node.neighbors.len() {
                new_node.neighbors[level] = selected.clone();
            }

            // Add reverse connections from neighbors to new node
            for neighbor_key in &selected {
                if let Some(neighbor_node) = self.nodes.get_mut(neighbor_key) {
                    if level < neighbor_node.neighbors.len() {
                        neighbor_node.neighbors[level].push(doc_key.to_string());

                        // Prune if exceeds limit
                        if neighbor_node.neighbors[level].len() > m_level {
                            // Keep only the best M neighbors
                            let mut neighbor_candidates: Vec<Neighbor> = neighbor_node.neighbors
                                [level]
                                .iter()
                                .filter_map(|k| {
                                    vectors.get(neighbor_key).map(|nv| Neighbor {
                                        doc_key: k.clone(),
                                        distance: Self::calculate_distance(nv, k, vectors, metric),
                                    })
                                })
                                .collect();
                            neighbor_candidates.sort_by(|a, b| {
                                a.distance
                                    .partial_cmp(&b.distance)
                                    .unwrap_or(Ordering::Equal)
                            });
                            // Select best M neighbors (inline to avoid borrow conflict)
                            neighbor_node.neighbors[level] = neighbor_candidates
                                .iter()
                                .take(m_level)
                                .map(|n| n.doc_key.clone())
                                .collect();
                        }
                    }
                }
            }

            // Update entry points for next level
            if !candidates.is_empty() {
                current_ep = candidates.iter().map(|n| n.doc_key.clone()).collect();
            }
        }

        // Insert the new node
        self.nodes.insert(doc_key.to_string(), new_node);

        // Update entry point if new node has higher level
        if node_level > self.max_level {
            self.entry_point = Some(doc_key.to_string());
            self.max_level = node_level;
        }
    }

    /// Remove a node from the graph (lazy removal - just delete the node)
    fn remove(&mut self, doc_key: &str) {
        // Remove the node
        if let Some(removed_node) = self.nodes.remove(doc_key) {
            // Remove references from neighbors
            for level in 0..removed_node.neighbors.len() {
                for neighbor_key in &removed_node.neighbors[level] {
                    if let Some(neighbor_node) = self.nodes.get_mut(neighbor_key) {
                        if level < neighbor_node.neighbors.len() {
                            neighbor_node.neighbors[level].retain(|k| k != doc_key);
                        }
                    }
                }
            }

            // Update entry point if removed
            if self.entry_point.as_ref() == Some(&doc_key.to_string()) {
                // Find a new entry point (any node at max_level, or reduce max_level)
                self.entry_point = self.nodes.keys().next().cloned();
                if let Some(ep) = &self.entry_point {
                    if let Some(node) = self.nodes.get(ep) {
                        self.max_level = node.level;
                    }
                } else {
                    self.max_level = 0;
                }
            }
        }
    }

    /// Search for k nearest neighbors
    fn search(
        &self,
        query: &[f32],
        k: usize,
        ef: usize,
        vectors: &HashMap<String, Vec<f32>>,
        metric: VectorMetric,
    ) -> Vec<Neighbor> {
        if self.entry_point.is_none() {
            return vec![];
        }

        let entry_point = self.entry_point.clone().unwrap();
        let mut current_ep = vec![entry_point];

        // Descend from top level to level 1 using greedy search
        for level in (1..=self.max_level).rev() {
            let nearest = self.search_layer(query, &current_ep, 1, level, vectors, metric);
            if !nearest.is_empty() {
                current_ep = vec![nearest[0].doc_key.clone()];
            }
        }

        // Search at level 0 with ef
        let ef_search = ef.max(k);
        let mut results = self.search_layer(query, &current_ep, ef_search, 0, vectors, metric);

        // Return top k
        results.truncate(k);
        results
    }

    /// Clear all nodes from the graph
    fn clear(&mut self) {
        self.nodes.clear();
        self.entry_point = None;
        self.max_level = 0;
    }
}

// =============================================================================
// VectorIndex
// =============================================================================

/// Vector index for similarity search
///
/// Supports two modes:
/// - Brute-force: Linear scan, accurate, fast for < 10K vectors
/// - HNSW: Approximate nearest neighbor, fast for 10K+ vectors
///
/// Automatically switches to HNSW when vector count exceeds threshold.
///
/// Supports optional scalar quantization for 4x memory reduction:
/// - Full precision vectors: f32 (4 bytes/dim)
/// - Quantized vectors: u8 (1 byte/dim)
/// - Asymmetric search: query stays f32, DB vectors are quantized
pub struct VectorIndex {
    /// Index configuration
    config: VectorIndexConfig,
    /// Stored vectors: doc_key -> vector (full precision)
    vectors: RwLock<HashMap<String, Vec<f32>>>,
    /// Quantized vectors for memory-efficient storage (optional)
    quantized_vectors: RwLock<Option<QuantizedVectors>>,
    /// HNSW graph for approximate search (built when threshold exceeded)
    hnsw_graph: RwLock<Option<HnswGraph>>,
}

impl VectorIndex {
    /// Create a new vector index with the given configuration
    pub fn new(config: VectorIndexConfig) -> DbResult<Self> {
        if config.dimension == 0 {
            return Err(DbError::BadRequest(
                "Vector dimension must be greater than 0".to_string(),
            ));
        }

        Ok(Self {
            config,
            vectors: RwLock::new(HashMap::new()),
            quantized_vectors: RwLock::new(None),
            hnsw_graph: RwLock::new(None),
        })
    }

    /// Get the HNSW threshold for this index
    fn hnsw_threshold(&self) -> usize {
        // Use config threshold if available, otherwise default
        DEFAULT_HNSW_THRESHOLD
    }

    /// Build the HNSW graph from existing vectors
    fn build_hnsw_graph(&self) {
        let vectors = self.vectors.read().unwrap();
        if vectors.len() < self.hnsw_threshold() {
            return;
        }

        let mut graph = HnswGraph::new(self.config.m, self.config.ef_construction);

        // Insert all vectors into the graph
        for (doc_key, vector) in vectors.iter() {
            graph.insert(doc_key, vector, &vectors, self.config.metric);
        }

        // Store the graph
        let mut hnsw = self.hnsw_graph.write().unwrap();
        *hnsw = Some(graph);
    }

    /// Get the index configuration
    pub fn config(&self) -> &VectorIndexConfig {
        &self.config
    }

    /// Get the number of indexed vectors
    pub fn len(&self) -> usize {
        self.vectors.read().unwrap().len()
    }

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

    /// Clear all vectors from the index
    pub fn clear(&self) {
        let mut vectors = self.vectors.write().unwrap();
        vectors.clear();

        // Also clear quantized vectors
        let mut quantized = self.quantized_vectors.write().unwrap();
        if let Some(q) = quantized.as_mut() {
            q.clear();
        }
        *quantized = None;

        // Also clear HNSW graph
        let mut hnsw = self.hnsw_graph.write().unwrap();
        if let Some(graph) = hnsw.as_mut() {
            graph.clear();
        }
        *hnsw = None;
    }

    /// Insert a vector for a document
    pub fn insert(&self, doc_key: &str, vector: &[f32]) -> DbResult<()> {
        // Validate dimension
        if vector.len() != self.config.dimension {
            return Err(DbError::BadRequest(format!(
                "Vector dimension mismatch: expected {}, got {}",
                self.config.dimension,
                vector.len()
            )));
        }

        // Insert into vectors HashMap
        {
            let mut vectors = self.vectors.write().unwrap();
            vectors.insert(doc_key.to_string(), vector.to_vec());
        }

        // Also insert into quantized vectors if they exist
        {
            let mut quantized = self.quantized_vectors.write().unwrap();
            if let Some(q) = quantized.as_mut() {
                q.insert(doc_key, vector);
            }
        }

        // Update HNSW graph if it exists or threshold is reached
        let vectors = self.vectors.read().unwrap();
        let count = vectors.len();
        drop(vectors);

        let mut hnsw = self.hnsw_graph.write().unwrap();
        if let Some(graph) = hnsw.as_mut() {
            // Graph exists, insert into it
            let vectors = self.vectors.read().unwrap();
            graph.insert(doc_key, vector, &vectors, self.config.metric);
        } else if count >= self.hnsw_threshold() {
            // Threshold reached, build the graph
            drop(hnsw);
            self.build_hnsw_graph();
        }

        Ok(())
    }

    /// Remove a vector by document key
    pub fn remove(&self, doc_key: &str) -> DbResult<bool> {
        // Remove from vectors HashMap
        let removed = {
            let mut vectors = self.vectors.write().unwrap();
            vectors.remove(doc_key).is_some()
        };

        if removed {
            // Also remove from quantized vectors if they exist
            {
                let mut quantized = self.quantized_vectors.write().unwrap();
                if let Some(q) = quantized.as_mut() {
                    q.remove(doc_key);
                }
            }

            // Also remove from HNSW graph if it exists
            let mut hnsw = self.hnsw_graph.write().unwrap();
            if let Some(graph) = hnsw.as_mut() {
                graph.remove(doc_key);
            }
        }

        Ok(removed)
    }

    /// Check if a document has a vector in the index
    pub fn contains(&self, doc_key: &str) -> bool {
        self.vectors.read().unwrap().contains_key(doc_key)
    }

    /// Get a vector by document key
    pub fn get(&self, doc_key: &str) -> Option<Vec<f32>> {
        self.vectors.read().unwrap().get(doc_key).cloned()
    }

    /// Search for similar vectors
    ///
    /// Uses HNSW approximate search when the graph is built (vector count >= threshold),
    /// otherwise falls back to brute-force linear search.
    ///
    /// # Arguments
    /// * `query` - Query vector
    /// * `limit` - Maximum number of results to return
    /// * `ef` - HNSW search quality parameter (higher = better recall, slower)
    ///
    /// # Returns
    /// Vector of search results sorted by similarity (best first)
    pub fn search(
        &self,
        query: &[f32],
        limit: usize,
        ef: usize,
    ) -> DbResult<Vec<VectorSearchResult>> {
        // Validate dimension
        if query.len() != self.config.dimension {
            return Err(DbError::BadRequest(format!(
                "Query vector dimension mismatch: expected {}, got {}",
                self.config.dimension,
                query.len()
            )));
        }

        // Try HNSW search first if graph exists
        {
            let hnsw = self.hnsw_graph.read().unwrap();
            if let Some(graph) = hnsw.as_ref() {
                let vectors = self.vectors.read().unwrap();
                let ef_search = ef.max(limit * 2).max(40);
                let hnsw_results =
                    graph.search(query, limit, ef_search, &vectors, self.config.metric);

                // Convert HNSW results (distance) back to score format
                let results: Vec<VectorSearchResult> = hnsw_results
                    .into_iter()
                    .map(|n| {
                        // Convert distance back to original score
                        let score = match self.config.metric {
                            VectorMetric::Cosine => 1.0 - n.distance, // distance was 1-similarity
                            VectorMetric::Euclidean => n.distance,    // keep as distance
                            VectorMetric::DotProduct => -n.distance,  // distance was -dot_product
                        };
                        VectorSearchResult {
                            doc_key: n.doc_key,
                            score,
                        }
                    })
                    .collect();

                return Ok(results);
            }
        }

        // Fall back to brute-force search
        self.brute_force_search(query, limit)
    }

    /// Perform brute-force linear search
    fn brute_force_search(&self, query: &[f32], limit: usize) -> DbResult<Vec<VectorSearchResult>> {
        // Check if we have quantized vectors - use asymmetric search if so
        {
            let quantized = self.quantized_vectors.read().unwrap();
            if let Some(q) = quantized.as_ref() {
                return self.brute_force_search_quantized(query, limit, q);
            }
        }

        // Fall back to full-precision search
        let vectors = self.vectors.read().unwrap();

        // Calculate scores for all vectors
        let mut results: Vec<VectorSearchResult> = vectors
            .iter()
            .map(|(doc_key, vec)| {
                let score = match self.config.metric {
                    VectorMetric::Cosine => cosine_similarity(query, vec),
                    VectorMetric::Euclidean => euclidean_distance(query, vec),
                    VectorMetric::DotProduct => dot_product(query, vec),
                };
                VectorSearchResult {
                    doc_key: doc_key.clone(),
                    score,
                }
            })
            .collect();

        // Sort by score
        match self.config.metric {
            VectorMetric::Cosine | VectorMetric::DotProduct => {
                // Higher is better for similarity/dot product
                results.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap_or(Ordering::Equal));
            }
            VectorMetric::Euclidean => {
                // Lower is better for distance
                results.sort_by(|a, b| a.score.partial_cmp(&b.score).unwrap_or(Ordering::Equal));
            }
        }

        // Limit results
        results.truncate(limit);
        Ok(results)
    }

    /// Perform brute-force search using quantized vectors (asymmetric search)
    fn brute_force_search_quantized(
        &self,
        query: &[f32],
        limit: usize,
        quantized: &QuantizedVectors,
    ) -> DbResult<Vec<VectorSearchResult>> {
        let params = quantized.params();

        // Calculate scores for all quantized vectors using asymmetric distance
        let mut results: Vec<VectorSearchResult> = quantized
            .vectors()
            .iter()
            .map(|(doc_key, quantized_vec)| {
                let score = match self.config.metric {
                    VectorMetric::Cosine => {
                        cosine_similarity_asymmetric(query, quantized_vec, params)
                    }
                    VectorMetric::Euclidean => {
                        euclidean_distance_asymmetric(query, quantized_vec, params)
                    }
                    VectorMetric::DotProduct => {
                        dot_product_asymmetric(query, quantized_vec, params)
                    }
                };
                VectorSearchResult {
                    doc_key: doc_key.clone(),
                    score,
                }
            })
            .collect();

        // Sort by score
        match self.config.metric {
            VectorMetric::Cosine | VectorMetric::DotProduct => {
                // Higher is better for similarity/dot product
                results.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap_or(Ordering::Equal));
            }
            VectorMetric::Euclidean => {
                // Lower is better for distance
                results.sort_by(|a, b| a.score.partial_cmp(&b.score).unwrap_or(Ordering::Equal));
            }
        }

        // Limit results
        results.truncate(limit);
        Ok(results)
    }

    /// Calculate similarity between a query vector and a document's vector
    pub fn similarity(&self, doc_key: &str, query: &[f32]) -> DbResult<Option<f32>> {
        if query.len() != self.config.dimension {
            return Err(DbError::BadRequest(format!(
                "Query vector dimension mismatch: expected {}, got {}",
                self.config.dimension,
                query.len()
            )));
        }

        let vectors = self.vectors.read().unwrap();
        let doc_vec = match vectors.get(doc_key) {
            Some(v) => v,
            None => return Ok(None),
        };

        let score = match self.config.metric {
            VectorMetric::Cosine => cosine_similarity(query, doc_vec),
            VectorMetric::Euclidean => euclidean_distance(query, doc_vec),
            VectorMetric::DotProduct => dot_product(query, doc_vec),
        };

        Ok(Some(score))
    }

    /// Serialize the index to bytes for persistence
    pub fn serialize(&self) -> DbResult<Vec<u8>> {
        let vectors = self.vectors.read().unwrap();
        let quantized = self.quantized_vectors.read().unwrap();
        let hnsw = self.hnsw_graph.read().unwrap();

        let data = VectorIndexDataV3 {
            config: self.config.clone(),
            vectors: vectors.clone(),
            quantized_vectors: quantized.clone(),
            hnsw_graph: hnsw.clone(),
        };
        bincode::serialize(&data)
            .map_err(|e| DbError::InternalError(format!("Serialization error: {}", e)))
    }

    /// Deserialize a vector index from bytes
    pub fn deserialize(bytes: &[u8]) -> DbResult<Self> {
        // Try V3 format first (with quantization)
        if let Ok(data) = bincode::deserialize::<VectorIndexDataV3>(bytes) {
            return Ok(Self {
                config: data.config,
                vectors: RwLock::new(data.vectors),
                quantized_vectors: RwLock::new(data.quantized_vectors),
                hnsw_graph: RwLock::new(data.hnsw_graph),
            });
        }

        // Try V2 format (with HNSW graph)
        if let Ok(data) = bincode::deserialize::<VectorIndexDataV2>(bytes) {
            return Ok(Self {
                config: data.config,
                vectors: RwLock::new(data.vectors),
                quantized_vectors: RwLock::new(None),
                hnsw_graph: RwLock::new(data.hnsw_graph),
            });
        }

        // Fall back to V1 format (without HNSW graph)
        let data: VectorIndexData = bincode::deserialize(bytes)
            .map_err(|e| DbError::InternalError(format!("Deserialization error: {}", e)))?;

        Ok(Self {
            config: data.config,
            vectors: RwLock::new(data.vectors),
            quantized_vectors: RwLock::new(None),
            hnsw_graph: RwLock::new(None),
        })
    }

    /// Check if HNSW graph is currently active
    pub fn is_hnsw_active(&self) -> bool {
        self.hnsw_graph.read().unwrap().is_some()
    }

    /// Check if scalar quantization is active
    pub fn is_quantized(&self) -> bool {
        self.quantized_vectors.read().unwrap().is_some()
    }

    /// Get the current quantization type
    pub fn quantization_type(&self) -> VectorQuantization {
        if self.quantized_vectors.read().unwrap().is_some() {
            VectorQuantization::Scalar
        } else {
            VectorQuantization::None
        }
    }

    /// Quantize all vectors using scalar quantization
    ///
    /// Computes per-dimension min/max values from all existing vectors,
    /// then quantizes each vector to u8 values. Future searches will use
    /// asymmetric distance computation (query f32, DB u8).
    ///
    /// Returns the number of vectors quantized.
    pub fn quantize(&self) -> DbResult<usize> {
        let vectors = self.vectors.read().unwrap();
        let count = vectors.len();

        if count == 0 {
            return Ok(0);
        }

        // Build quantized storage from full-precision vectors
        let quantized = QuantizedVectors::from_full_vectors(&vectors, self.config.dimension);
        drop(vectors);

        // Store the quantized vectors
        let mut quantized_lock = self.quantized_vectors.write().unwrap();
        *quantized_lock = Some(quantized);

        Ok(count)
    }

    /// Remove quantization (revert to full-precision search)
    pub fn dequantize(&self) {
        let mut quantized = self.quantized_vectors.write().unwrap();
        *quantized = None;
    }

    /// Get quantization statistics
    pub fn quantization_stats(&self) -> Option<QuantizationStats> {
        let quantized = self.quantized_vectors.read().unwrap();
        quantized.as_ref().map(|q| {
            let vector_count = q.len();
            let memory_bytes = vector_count * self.config.dimension; // u8 per dim
            let full_memory_bytes =
                vector_count * self.config.dimension * std::mem::size_of::<f32>();
            QuantizationStats {
                vector_count,
                memory_bytes,
                full_memory_bytes,
                compression_ratio: if memory_bytes > 0 {
                    full_memory_bytes as f32 / memory_bytes as f32
                } else {
                    1.0
                },
            }
        })
    }

    /// Get index statistics
    pub fn stats(&self) -> VectorIndexStats {
        let (memory_bytes, compression_ratio) = if let Some(stats) = self.quantization_stats() {
            (stats.memory_bytes, stats.compression_ratio)
        } else {
            (0, 1.0)
        };

        VectorIndexStats {
            name: self.config.name.clone(),
            field: self.config.field.clone(),
            dimension: self.config.dimension,
            metric: self.config.metric,
            m: self.config.m,
            ef_construction: self.config.ef_construction,
            indexed_vectors: self.len(),
            quantization: self.quantization_type(),
            memory_bytes,
            compression_ratio,
        }
    }
}

/// Statistics about quantized vector storage
#[derive(Debug, Clone, Serialize)]
pub struct QuantizationStats {
    /// Number of quantized vectors
    pub vector_count: usize,
    /// Memory usage in bytes (quantized)
    pub memory_bytes: usize,
    /// Memory usage in bytes if full precision
    pub full_memory_bytes: usize,
    /// Compression ratio (full / quantized)
    pub compression_ratio: f32,
}

/// Serializable vector index data (V1 format - backward compat)
#[derive(Serialize, Deserialize)]
struct VectorIndexData {
    config: VectorIndexConfig,
    vectors: HashMap<String, Vec<f32>>,
}

/// Serializable vector index data (V2 format - with HNSW)
#[derive(Serialize, Deserialize)]
struct VectorIndexDataV2 {
    config: VectorIndexConfig,
    vectors: HashMap<String, Vec<f32>>,
    #[serde(default)]
    hnsw_graph: Option<HnswGraph>,
}

/// Serializable vector index data (V3 format - with quantization)
#[derive(Serialize, Deserialize)]
struct VectorIndexDataV3 {
    config: VectorIndexConfig,
    vectors: HashMap<String, Vec<f32>>,
    #[serde(default)]
    quantized_vectors: Option<QuantizedVectors>,
    #[serde(default)]
    hnsw_graph: Option<HnswGraph>,
}

/// Calculate cosine similarity between two vectors
///
/// Returns a value between -1 and 1, where 1 means identical direction,
/// 0 means orthogonal, and -1 means opposite direction.
pub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() || a.is_empty() {
        return 0.0;
    }

    let mut dot = 0.0f32;
    let mut norm_a = 0.0f32;
    let mut norm_b = 0.0f32;

    for i in 0..a.len() {
        dot += a[i] * b[i];
        norm_a += a[i] * a[i];
        norm_b += b[i] * b[i];
    }

    let norm = (norm_a.sqrt() * norm_b.sqrt()).max(1e-10);
    dot / norm
}

/// Calculate Euclidean distance between two vectors
///
/// Returns a non-negative value where 0 means identical vectors.
pub fn euclidean_distance(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() {
        return f32::MAX;
    }

    let mut sum = 0.0f32;
    for i in 0..a.len() {
        let diff = a[i] - b[i];
        sum += diff * diff;
    }
    sum.sqrt()
}

/// Calculate dot product between two vectors
pub fn dot_product(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() {
        return 0.0;
    }

    let mut dot = 0.0f32;
    for i in 0..a.len() {
        dot += a[i] * b[i];
    }
    dot
}

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

    #[test]
    fn test_cosine_similarity() {
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![1.0, 0.0, 0.0];
        assert!((cosine_similarity(&a, &b) - 1.0).abs() < 1e-6);

        let c = vec![0.0, 1.0, 0.0];
        assert!(cosine_similarity(&a, &c).abs() < 1e-6);

        let d = vec![-1.0, 0.0, 0.0];
        assert!((cosine_similarity(&a, &d) + 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_euclidean_distance() {
        let a = vec![0.0, 0.0, 0.0];
        let b = vec![1.0, 0.0, 0.0];
        assert!((euclidean_distance(&a, &b) - 1.0).abs() < 1e-6);

        let c = vec![3.0, 4.0, 0.0];
        assert!((euclidean_distance(&a, &c) - 5.0).abs() < 1e-6);
    }

    #[test]
    fn test_dot_product() {
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![4.0, 5.0, 6.0];
        assert!((dot_product(&a, &b) - 32.0).abs() < 1e-6);
    }

    #[test]
    fn test_vector_index_creation() {
        let config = VectorIndexConfig::new("test_idx".to_string(), "embedding".to_string(), 3);
        let index = VectorIndex::new(config).unwrap();
        assert!(index.is_empty());
    }

    #[test]
    fn test_vector_index_insert_and_search() {
        let config = VectorIndexConfig::new("test_idx".to_string(), "embedding".to_string(), 3);
        let index = VectorIndex::new(config).unwrap();

        // Insert some vectors
        index.insert("doc1", &[1.0, 0.0, 0.0]).unwrap();
        index.insert("doc2", &[0.0, 1.0, 0.0]).unwrap();
        index.insert("doc3", &[0.9, 0.1, 0.0]).unwrap();

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

        // Search for similar vectors
        let results = index.search(&[1.0, 0.0, 0.0], 2, 10).unwrap();
        assert_eq!(results.len(), 2);

        // doc1 should be most similar to [1, 0, 0]
        assert_eq!(results[0].doc_key, "doc1");
        assert!((results[0].score - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_vector_index_dimension_validation() {
        let config = VectorIndexConfig::new("test_idx".to_string(), "embedding".to_string(), 3);
        let index = VectorIndex::new(config).unwrap();

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

    #[test]
    fn test_vector_index_remove() {
        let config = VectorIndexConfig::new("test_idx".to_string(), "embedding".to_string(), 3);
        let index = VectorIndex::new(config).unwrap();

        index.insert("doc1", &[1.0, 0.0, 0.0]).unwrap();
        assert_eq!(index.len(), 1);

        let removed = index.remove("doc1").unwrap();
        assert!(removed);
        assert_eq!(index.len(), 0);

        // Removing non-existent doc should return false
        let removed = index.remove("nonexistent").unwrap();
        assert!(!removed);
    }

    #[test]
    fn test_vector_index_with_euclidean() {
        let config = VectorIndexConfig::new("test_idx".to_string(), "embedding".to_string(), 3)
            .with_metric(VectorMetric::Euclidean);
        let index = VectorIndex::new(config).unwrap();

        index.insert("doc1", &[0.0, 0.0, 0.0]).unwrap();
        index.insert("doc2", &[1.0, 0.0, 0.0]).unwrap();
        index.insert("doc3", &[10.0, 0.0, 0.0]).unwrap();

        let results = index.search(&[0.0, 0.0, 0.0], 3, 10).unwrap();

        // doc1 should be closest (distance 0)
        assert_eq!(results[0].doc_key, "doc1");
        assert!(results[0].score < 0.1);
    }

    #[test]
    fn test_vector_index_similarity() {
        let config = VectorIndexConfig::new("test_idx".to_string(), "embedding".to_string(), 3);
        let index = VectorIndex::new(config).unwrap();

        index.insert("doc1", &[1.0, 0.0, 0.0]).unwrap();

        let sim = index.similarity("doc1", &[1.0, 0.0, 0.0]).unwrap();
        assert!(sim.is_some());
        assert!((sim.unwrap() - 1.0).abs() < 1e-6);

        let sim = index.similarity("nonexistent", &[1.0, 0.0, 0.0]).unwrap();
        assert!(sim.is_none());
    }

    #[test]
    fn test_vector_index_serialize_deserialize() {
        let config = VectorIndexConfig::new("test_idx".to_string(), "embedding".to_string(), 3);
        let index = VectorIndex::new(config).unwrap();

        index.insert("doc1", &[1.0, 0.0, 0.0]).unwrap();
        index.insert("doc2", &[0.0, 1.0, 0.0]).unwrap();

        // Serialize
        let bytes = index.serialize().unwrap();

        // Deserialize
        let restored = VectorIndex::deserialize(&bytes).unwrap();
        assert_eq!(restored.len(), 2);
        assert!(restored.contains("doc1"));
        assert!(restored.contains("doc2"));
    }

    #[test]
    fn test_vector_index_update() {
        let config = VectorIndexConfig::new("test_idx".to_string(), "embedding".to_string(), 3);
        let index = VectorIndex::new(config).unwrap();

        index.insert("doc1", &[1.0, 0.0, 0.0]).unwrap();

        // Update the vector
        index.insert("doc1", &[0.0, 1.0, 0.0]).unwrap();

        // Should still have only one entry
        assert_eq!(index.len(), 1);

        // Check the vector was updated
        let vec = index.get("doc1").unwrap();
        assert!((vec[0] - 0.0).abs() < 1e-6);
        assert!((vec[1] - 1.0).abs() < 1e-6);
    }
}