oxirs-core 0.2.2

Core RDF and SPARQL functionality for OxiRS - native Rust implementation with zero dependencies
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
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
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
//! Vector Store for Efficient Similarity Search
//!
//! This module provides high-performance vector storage and similarity search
//! capabilities optimized for knowledge graph embeddings and AI applications.

use anyhow::{anyhow, Result};
use dashmap::DashMap;
use scirs2_core::metrics::{Counter, Histogram, MetricsRegistry, Timer};
use scirs2_core::ndarray_ext::ArrayView1;
use scirs2_core::random::Random;
use scirs2_core::rngs::StdRng;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::sync::Arc;
use tokio::sync::RwLock;

/// Vector store trait for similarity search
#[async_trait::async_trait]
pub trait VectorStore: Send + Sync {
    /// Insert a vector with metadata
    async fn insert(
        &self,
        id: String,
        vector: Vec<f32>,
        metadata: Option<HashMap<String, String>>,
    ) -> Result<()>;

    /// Insert multiple vectors
    async fn insert_batch(&self, vectors: Vec<VectorData>) -> Result<()>;

    /// Search for similar vectors
    async fn search(&self, query: &VectorQuery) -> Result<Vec<(String, f32)>>;

    /// Get vector by ID
    async fn get(&self, id: &str) -> Result<Option<VectorData>>;

    /// Delete vector by ID
    async fn delete(&self, id: &str) -> Result<bool>;

    /// Update vector
    async fn update(
        &self,
        id: String,
        vector: Vec<f32>,
        metadata: Option<HashMap<String, String>>,
    ) -> Result<()>;

    /// Get store size
    fn size(&self) -> usize;

    /// Build index for faster search
    async fn build_index(&self) -> Result<()>;

    /// Get store statistics
    async fn get_statistics(&self) -> Result<VectorStoreStats>;
}

/// Vector data structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VectorData {
    /// Unique identifier
    pub id: String,

    /// Vector values
    pub vector: Vec<f32>,

    /// Optional metadata
    pub metadata: Option<HashMap<String, String>>,

    /// Timestamp when inserted
    pub timestamp: std::time::SystemTime,
}

/// Vector query parameters
#[derive(Debug, Clone)]
pub struct VectorQuery {
    /// Query vector
    pub vector: Vec<f32>,

    /// Number of results to return
    pub k: usize,

    /// Distance metric
    pub metric: Option<SimilarityMetric>,

    /// Include metadata in results
    pub include_metadata: bool,

    /// Filter conditions
    pub filters: Option<Vec<Filter>>,

    /// Minimum similarity threshold
    pub min_similarity: Option<f32>,
}

/// Filter conditions for vector search
#[derive(Debug, Clone)]
pub struct Filter {
    /// Field name
    pub field: String,

    /// Filter operation
    pub operation: FilterOperation,

    /// Filter value
    pub value: String,
}

/// Filter operations
#[derive(Debug, Clone)]
pub enum FilterOperation {
    Equals,
    NotEquals,
    Contains,
    StartsWith,
    EndsWith,
    GreaterThan,
    LessThan,
    In(Vec<String>),
    NotIn(Vec<String>),
}

/// Similarity metrics
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum SimilarityMetric {
    /// Cosine similarity
    Cosine,

    /// Euclidean distance
    Euclidean,

    /// Manhattan distance (L1)
    Manhattan,

    /// Dot product
    DotProduct,

    /// Jaccard similarity
    Jaccard,

    /// Hamming distance
    Hamming,
}

impl std::fmt::Display for SimilarityMetric {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SimilarityMetric::Cosine => write!(f, "cosine"),
            SimilarityMetric::Euclidean => write!(f, "euclidean"),
            SimilarityMetric::Manhattan => write!(f, "manhattan"),
            SimilarityMetric::DotProduct => write!(f, "dot_product"),
            SimilarityMetric::Jaccard => write!(f, "jaccard"),
            SimilarityMetric::Hamming => write!(f, "hamming"),
        }
    }
}

/// Vector store statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VectorStoreStats {
    /// Total number of vectors
    pub total_vectors: usize,

    /// Vector dimension
    pub dimension: usize,

    /// Index type
    pub index_type: String,

    /// Index build time
    pub index_build_time: std::time::Duration,

    /// Memory usage in bytes
    pub memory_usage: usize,

    /// Average query time
    pub avg_query_time: std::time::Duration,

    /// Cache hit rate
    pub cache_hit_rate: f32,
}

/// In-memory vector store with production-ready monitoring
///
/// Integrates SCIRS2 metrics for comprehensive performance tracking:
/// - Insert operations counting
/// - Search latency measurement
/// - Cache hit rate monitoring
/// - Index rebuild timing
pub struct InMemoryVectorStore {
    /// Vector storage
    vectors: Arc<DashMap<String, VectorData>>,

    /// Vector index for fast similarity search
    index: Arc<RwLock<Option<Box<dyn VectorIndex>>>>,

    /// Configuration
    config: VectorStoreConfig,

    /// Query cache
    query_cache: Arc<DashMap<String, Vec<(String, f32)>>>,

    /// Statistics
    stats: Arc<RwLock<VectorStoreStats>>,

    /// Cache hit counter (atomic for lock-free access)
    cache_hits: Arc<std::sync::atomic::AtomicUsize>,

    /// Cache miss counter (atomic for lock-free access)
    cache_misses: Arc<std::sync::atomic::AtomicUsize>,

    /// SCIRS2 Metrics
    /// Insert operation counter
    insert_counter: Arc<Counter>,

    /// Search operation counter
    search_counter: Arc<Counter>,

    /// Search latency timer
    search_timer: Arc<Timer>,

    /// Index build timer
    index_build_timer: Arc<Timer>,

    /// Similarity computation histogram
    similarity_histogram: Arc<Histogram>,

    /// Metrics registry
    metrics_registry: Arc<MetricsRegistry>,
}

/// Vector store configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VectorStoreConfig {
    /// Vector dimension
    pub dimension: usize,

    /// Default similarity metric
    pub default_metric: SimilarityMetric,

    /// Index type
    pub index_type: IndexType,

    /// Enable query caching
    pub enable_cache: bool,

    /// Cache size limit
    pub cache_size: usize,

    /// Cache TTL in seconds
    pub cache_ttl: u64,

    /// Batch insert size
    pub batch_size: usize,
}

impl Default for VectorStoreConfig {
    fn default() -> Self {
        Self {
            dimension: 128,
            default_metric: SimilarityMetric::Cosine,
            index_type: IndexType::Flat,
            enable_cache: true,
            cache_size: 10000,
            cache_ttl: 3600,
            batch_size: 1000,
        }
    }
}

/// Vector index types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum IndexType {
    /// Flat index (brute force search)
    Flat,

    /// HNSW (Hierarchical Navigable Small World)
    HNSW {
        max_connections: usize,
        ef_construction: usize,
        ef_search: usize,
    },

    /// IVF (Inverted File)
    IVF {
        num_clusters: usize,
        num_probes: usize,
    },

    /// LSH (Locality Sensitive Hashing)
    LSH {
        num_tables: usize,
        hash_length: usize,
    },

    /// Product Quantization
    PQ {
        num_subquantizers: usize,
        bits_per_subquantizer: usize,
    },
}

/// Vector index trait
#[async_trait::async_trait]
pub trait VectorIndex: Send + Sync {
    /// Build index from vectors
    async fn build(&mut self, vectors: &DashMap<String, VectorData>) -> Result<()>;

    /// Search for similar vectors
    async fn search(
        &self,
        query: &[f32],
        k: usize,
        metric: SimilarityMetric,
    ) -> Result<Vec<(String, f32)>>;

    /// Add vector to index
    async fn add(&mut self, id: String, vector: Vec<f32>) -> Result<()>;

    /// Remove vector from index
    async fn remove(&mut self, id: &str) -> Result<()>;

    /// Get index statistics
    fn get_stats(&self) -> IndexStats;
}

/// Index statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexStats {
    pub index_type: String,
    pub num_vectors: usize,
    pub build_time: std::time::Duration,
    pub memory_usage: usize,
}

/// Flat index implementation (brute force)
pub struct FlatIndex {
    /// Vector storage reference
    vectors: HashMap<String, Vec<f32>>,

    /// Statistics
    stats: IndexStats,
}

impl Default for FlatIndex {
    fn default() -> Self {
        Self::new()
    }
}

impl FlatIndex {
    pub fn new() -> Self {
        Self {
            vectors: HashMap::new(),
            stats: IndexStats {
                index_type: "Flat".to_string(),
                num_vectors: 0,
                build_time: std::time::Duration::from_secs(0),
                memory_usage: 0,
            },
        }
    }
}

#[async_trait::async_trait]
impl VectorIndex for FlatIndex {
    async fn build(&mut self, vectors: &DashMap<String, VectorData>) -> Result<()> {
        let start = std::time::Instant::now();

        self.vectors.clear();
        for entry in vectors.iter() {
            self.vectors
                .insert(entry.key().clone(), entry.value().vector.clone());
        }

        self.stats.num_vectors = self.vectors.len();
        self.stats.build_time = start.elapsed();
        self.stats.memory_usage = self.vectors.len()
            * self
                .vectors
                .values()
                .next()
                .map(|v| v.len() * 4)
                .unwrap_or(0);

        Ok(())
    }

    async fn search(
        &self,
        query: &[f32],
        k: usize,
        metric: SimilarityMetric,
    ) -> Result<Vec<(String, f32)>> {
        let mut similarities = Vec::new();

        for (id, vector) in &self.vectors {
            let similarity = compute_similarity(query, vector, metric)?;
            similarities.push((id.clone(), similarity));
        }

        // Sort by similarity (higher is better for most metrics)
        similarities.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(Ordering::Equal));

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

    async fn add(&mut self, id: String, vector: Vec<f32>) -> Result<()> {
        self.vectors.insert(id, vector);
        self.stats.num_vectors = self.vectors.len();
        Ok(())
    }

    async fn remove(&mut self, id: &str) -> Result<()> {
        self.vectors.remove(id);
        self.stats.num_vectors = self.vectors.len();
        Ok(())
    }

    fn get_stats(&self) -> IndexStats {
        self.stats.clone()
    }
}

/// HNSW index implementation
pub struct HNSWIndex {
    /// Configuration
    #[allow(dead_code)]
    max_connections: usize,
    #[allow(dead_code)]
    ef_construction: usize,
    #[allow(dead_code)]
    ef_search: usize,

    /// Graph layers
    layers: Vec<HashMap<String, Vec<String>>>,

    /// Vector storage
    vectors: HashMap<String, Vec<f32>>,

    /// Entry point
    entry_point: Option<String>,

    /// Statistics
    stats: IndexStats,

    /// Random number generator (thread-safe)
    rng: Random<StdRng>,
}

impl HNSWIndex {
    pub fn new(max_connections: usize, ef_construction: usize, ef_search: usize) -> Self {
        Self {
            max_connections,
            ef_construction,
            ef_search,
            layers: Vec::new(),
            vectors: HashMap::new(),
            entry_point: None,
            stats: IndexStats {
                index_type: "HNSW".to_string(),
                num_vectors: 0,
                build_time: std::time::Duration::from_secs(0),
                memory_usage: 0,
            },
            rng: Random::seed(42), // Use deterministic seed for thread safety
        }
    }
}

#[async_trait::async_trait]
impl VectorIndex for HNSWIndex {
    async fn build(&mut self, vectors: &DashMap<String, VectorData>) -> Result<()> {
        let start = std::time::Instant::now();

        // Initialize layers
        self.layers.clear();
        self.vectors.clear();

        // Add vectors one by one (simplified HNSW construction)
        for entry in vectors.iter() {
            let id = entry.key().clone();
            let vector = entry.value().vector.clone();

            self.vectors.insert(id.clone(), vector);

            // Determine layer for this vector
            let layer = self.get_random_layer();

            // Ensure we have enough layers
            while self.layers.len() <= layer {
                self.layers.push(HashMap::new());
            }

            // Add to layers
            for l in 0..=layer {
                if l >= self.layers.len() {
                    self.layers.push(HashMap::new());
                }
                self.layers[l].insert(id.clone(), Vec::new());
            }

            // Set entry point if this is the first vector or higher layer
            if self.entry_point.is_none() || layer >= self.layers.len() - 1 {
                self.entry_point = Some(id.clone());
            }
        }

        // Build connections (simplified)
        self.build_connections().await?;

        self.stats.num_vectors = self.vectors.len();
        self.stats.build_time = start.elapsed();

        Ok(())
    }

    async fn search(
        &self,
        query: &[f32],
        k: usize,
        metric: SimilarityMetric,
    ) -> Result<Vec<(String, f32)>> {
        // Use the enhanced beam search algorithm
        self.beam_search(query, k, metric)
    }

    async fn add(&mut self, id: String, vector: Vec<f32>) -> Result<()> {
        self.vectors.insert(id.clone(), vector);

        // Add to bottom layer
        if self.layers.is_empty() {
            self.layers.push(HashMap::new());
        }
        self.layers[0].insert(id.clone(), Vec::new());

        if self.entry_point.is_none() {
            self.entry_point = Some(id);
        }

        self.stats.num_vectors = self.vectors.len();
        Ok(())
    }

    async fn remove(&mut self, id: &str) -> Result<()> {
        self.vectors.remove(id);

        // Remove from all layers
        for layer in &mut self.layers {
            layer.remove(id);
        }

        self.stats.num_vectors = self.vectors.len();
        Ok(())
    }

    fn get_stats(&self) -> IndexStats {
        self.stats.clone()
    }
}

impl HNSWIndex {
    fn get_random_layer(&mut self) -> usize {
        // Simplified layer assignment using exponential distribution
        let mut layer = 0;
        while (self.rng.random_f64() as f32) < 0.5 && layer < 16 {
            layer += 1;
        }
        layer
    }

    async fn build_connections(&mut self) -> Result<()> {
        // Build proper HNSW graph connections using greedy search
        let ids: Vec<String> = self.vectors.keys().cloned().collect();

        if ids.is_empty() {
            return Ok(());
        }

        // For each vector, find and connect to its nearest neighbors in each layer
        for id in &ids {
            // Get vector for this node
            let vector = match self.vectors.get(id) {
                Some(v) => v.clone(),
                None => continue,
            };

            // Find nearest neighbors in each layer this node belongs to
            for (layer_idx, layer) in self.layers.iter_mut().enumerate() {
                if !layer.contains_key(id) {
                    continue;
                }

                // Find candidates in this layer
                let mut candidates: Vec<(String, f32)> = Vec::new();
                for (other_id, _) in layer.iter() {
                    if other_id == id {
                        continue;
                    }
                    if let Some(other_vector) = self.vectors.get(other_id) {
                        let similarity =
                            compute_similarity(&vector, other_vector, SimilarityMetric::Cosine)
                                .unwrap_or(0.0);
                        candidates.push((other_id.clone(), similarity));
                    }
                }

                // Sort by similarity and take top max_connections
                candidates.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(Ordering::Equal));
                let max_conn = if layer_idx == 0 {
                    self.max_connections * 2 // Bottom layer gets more connections
                } else {
                    self.max_connections
                };
                candidates.truncate(max_conn);

                // Set connections for this node
                let connections: Vec<String> = candidates.into_iter().map(|(cid, _)| cid).collect();
                layer.insert(id.clone(), connections.clone());

                // Add bidirectional connections (make graph undirected)
                for neighbor_id in connections {
                    if let Some(neighbor_connections) = layer.get_mut(&neighbor_id) {
                        if !neighbor_connections.contains(id)
                            && neighbor_connections.len() < max_conn
                        {
                            neighbor_connections.push(id.clone());
                        }
                    }
                }
            }
        }

        // Calculate memory usage
        let mut memory = 0;
        for (id, vec) in &self.vectors {
            memory += id.len() + vec.len() * 4;
        }
        for layer in &self.layers {
            for (id, connections) in layer {
                memory += id.len() + connections.len() * 8; // Approximate string overhead
            }
        }
        self.stats.memory_usage = memory;

        Ok(())
    }

    /// Search using proper beam search with ef_search parameter
    fn beam_search(
        &self,
        query: &[f32],
        k: usize,
        metric: SimilarityMetric,
    ) -> Result<Vec<(String, f32)>> {
        if self.vectors.is_empty() {
            return Ok(Vec::new());
        }

        // Start from entry point
        let entry = match &self.entry_point {
            Some(e) => e.clone(),
            None => return Ok(Vec::new()),
        };

        // Greedy search from top layer to bottom
        let mut current_best = entry.clone();

        // Navigate through layers from top to bottom
        for layer_idx in (1..self.layers.len()).rev() {
            let layer = &self.layers[layer_idx];

            // Greedy search in this layer
            while let Some(current_vector) = self.vectors.get(&current_best) {
                let current_sim = compute_similarity(query, current_vector, metric)?;

                let mut improved = false;
                if let Some(neighbors) = layer.get(&current_best) {
                    for neighbor in neighbors {
                        if let Some(neighbor_vector) = self.vectors.get(neighbor) {
                            let neighbor_sim = compute_similarity(query, neighbor_vector, metric)?;
                            if neighbor_sim > current_sim {
                                current_best = neighbor.clone();
                                improved = true;
                                break;
                            }
                        }
                    }
                }

                if !improved {
                    break;
                }
            }
        }

        // Beam search in bottom layer (layer 0)
        if self.layers.is_empty() {
            return Ok(Vec::new());
        }

        let bottom_layer = &self.layers[0];
        let ef = std::cmp::max(k, self.ef_search);

        // Use priority queue for candidates (max-heap by similarity)
        let mut candidates = BinaryHeap::new();
        let mut visited = HashSet::new();

        // Initialize with entry point
        if let Some(entry_vector) = self.vectors.get(&current_best) {
            let sim = compute_similarity(query, entry_vector, metric)?;
            candidates.push(SimilarityItem {
                id: current_best.clone(),
                similarity: sim,
            });
            visited.insert(current_best);
        }

        // Results (min-heap by similarity, will keep worst at top for easy replacement)
        let mut results: Vec<(String, f32)> = Vec::new();

        // Beam search
        while let Some(current) = candidates.pop() {
            // Add to results if within ef
            if results.len() < ef {
                results.push((current.id.clone(), current.similarity));
                results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(Ordering::Equal));
            } else if current.similarity > results.last().map(|r| r.1).unwrap_or(f32::NEG_INFINITY)
            {
                results.pop();
                results.push((current.id.clone(), current.similarity));
                results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(Ordering::Equal));
            }

            // Explore neighbors
            if let Some(neighbors) = bottom_layer.get(&current.id) {
                for neighbor in neighbors {
                    if !visited.contains(neighbor) {
                        visited.insert(neighbor.clone());
                        if let Some(neighbor_vector) = self.vectors.get(neighbor) {
                            let sim = compute_similarity(query, neighbor_vector, metric)?;

                            // Only add if potentially useful
                            let worst_result =
                                results.last().map(|r| r.1).unwrap_or(f32::NEG_INFINITY);
                            if results.len() < ef || sim > worst_result {
                                candidates.push(SimilarityItem {
                                    id: neighbor.clone(),
                                    similarity: sim,
                                });
                            }
                        }
                    }
                }
            }
        }

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

/// Helper struct for priority queue
#[derive(Debug, Clone)]
struct SimilarityItem {
    id: String,
    similarity: f32,
}

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

impl Eq for SimilarityItem {}

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

impl Ord for SimilarityItem {
    fn cmp(&self, other: &Self) -> Ordering {
        // Reverse order for max heap behavior
        other
            .similarity
            .partial_cmp(&self.similarity)
            .unwrap_or(Ordering::Equal)
    }
}

impl InMemoryVectorStore {
    /// Create new in-memory vector store
    /// Create a new vector store with production monitoring
    ///
    /// Automatically initializes SCIRS2 metrics for comprehensive tracking:
    /// - Insert/search operation counters
    /// - Latency timers for performance analysis
    /// - Similarity computation histograms
    pub fn new(config: VectorStoreConfig) -> Self {
        let stats = VectorStoreStats {
            total_vectors: 0,
            dimension: config.dimension,
            index_type: format!("{:?}", config.index_type),
            index_build_time: std::time::Duration::from_secs(0),
            memory_usage: 0,
            avg_query_time: std::time::Duration::from_millis(0),
            cache_hit_rate: 0.0,
        };

        // Initialize SCIRS2 metrics
        let metrics_registry = Arc::new(MetricsRegistry::new());
        let insert_counter = Arc::new(Counter::new("vector_inserts".to_string()));
        let search_counter = Arc::new(Counter::new("vector_searches".to_string()));
        let search_timer = Arc::new(Timer::new("search_latency".to_string()));
        let index_build_timer = Arc::new(Timer::new("index_build_time".to_string()));
        let similarity_histogram = Arc::new(Histogram::new("similarity_scores".to_string()));

        Self {
            vectors: Arc::new(DashMap::new()),
            index: Arc::new(RwLock::new(None)),
            config,
            query_cache: Arc::new(DashMap::new()),
            stats: Arc::new(RwLock::new(stats)),
            cache_hits: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
            cache_misses: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
            insert_counter,
            search_counter,
            search_timer,
            index_build_timer,
            similarity_histogram,
            metrics_registry,
        }
    }

    /// Apply filters to search results
    fn apply_filters(&self, data: &VectorData, filters: &[Filter]) -> bool {
        if let Some(metadata) = &data.metadata {
            for filter in filters {
                if let Some(value) = metadata.get(&filter.field) {
                    match &filter.operation {
                        FilterOperation::Equals => {
                            if value != &filter.value {
                                return false;
                            }
                        }
                        FilterOperation::NotEquals => {
                            if value == &filter.value {
                                return false;
                            }
                        }
                        FilterOperation::Contains => {
                            if !value.contains(&filter.value) {
                                return false;
                            }
                        }
                        FilterOperation::StartsWith => {
                            if !value.starts_with(&filter.value) {
                                return false;
                            }
                        }
                        FilterOperation::EndsWith => {
                            if !value.ends_with(&filter.value) {
                                return false;
                            }
                        }
                        FilterOperation::In(values) => {
                            if !values.contains(value) {
                                return false;
                            }
                        }
                        FilterOperation::NotIn(values) => {
                            if values.contains(value) {
                                return false;
                            }
                        }
                        FilterOperation::GreaterThan => {
                            // Try to parse both values as f64 for numeric comparison
                            if let (Ok(val_num), Ok(filter_num)) =
                                (value.parse::<f64>(), filter.value.parse::<f64>())
                            {
                                if val_num <= filter_num {
                                    return false;
                                }
                            } else {
                                // Fallback to lexicographic comparison if not numeric
                                if value <= &filter.value {
                                    return false;
                                }
                            }
                        }
                        FilterOperation::LessThan => {
                            // Try to parse both values as f64 for numeric comparison
                            if let (Ok(val_num), Ok(filter_num)) =
                                (value.parse::<f64>(), filter.value.parse::<f64>())
                            {
                                if val_num >= filter_num {
                                    return false;
                                }
                            } else {
                                // Fallback to lexicographic comparison if not numeric
                                if value >= &filter.value {
                                    return false;
                                }
                            }
                        }
                    }
                } else {
                    return false; // Filter field not found
                }
            }
        } else if !filters.is_empty() {
            return false; // No metadata but filters provided
        }

        true
    }
}

#[async_trait::async_trait]
impl VectorStore for InMemoryVectorStore {
    async fn insert(
        &self,
        id: String,
        vector: Vec<f32>,
        metadata: Option<HashMap<String, String>>,
    ) -> Result<()> {
        if vector.len() != self.config.dimension {
            return Err(anyhow!(
                "Vector dimension mismatch: expected {}, got {}",
                self.config.dimension,
                vector.len()
            ));
        }

        let data = VectorData {
            id: id.clone(),
            vector,
            metadata,
            timestamp: std::time::SystemTime::now(),
        };

        let id_for_lookup = id.clone();
        self.vectors.insert(id.clone(), data);

        // Track insert operation
        self.insert_counter.inc();

        // Add to index if it exists
        if let Some(index) = self.index.write().await.as_mut() {
            index
                .add(
                    id,
                    self.vectors
                        .get(&id_for_lookup)
                        .expect("vector should exist after insert")
                        .vector
                        .clone(),
                )
                .await?;
        }

        // Update statistics
        let mut stats = self.stats.write().await;
        stats.total_vectors = self.vectors.len();

        Ok(())
    }

    async fn insert_batch(&self, vectors: Vec<VectorData>) -> Result<()> {
        for data in vectors {
            if data.vector.len() != self.config.dimension {
                return Err(anyhow!("Vector dimension mismatch"));
            }
            self.vectors.insert(data.id.clone(), data);
        }

        // Rebuild index if it exists
        if self.index.read().await.is_some() {
            self.build_index().await?;
        }

        Ok(())
    }

    async fn search(&self, query: &VectorQuery) -> Result<Vec<(String, f32)>> {
        // Track search operation
        self.search_counter.inc();

        let metric = query.metric.unwrap_or(self.config.default_metric);

        // Check cache first
        if self.config.enable_cache {
            let cache_key = format!(
                "{:?}_{}_{}_{:?}",
                query.vector, query.k, metric, query.filters
            );
            if let Some(cached) = self.query_cache.get(&cache_key) {
                // Cache hit
                self.cache_hits
                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                return Ok(cached.clone());
            } else {
                // Cache miss
                self.cache_misses
                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            }
        }

        let start = std::time::Instant::now();

        let results = if let Some(index) = self.index.read().await.as_ref() {
            // Use index for search
            index.search(&query.vector, query.k, metric).await?
        } else {
            // Brute force search
            let mut similarities = Vec::new();

            for entry in self.vectors.iter() {
                let data = entry.value();

                // Apply filters
                if let Some(filters) = &query.filters {
                    if !self.apply_filters(data, filters) {
                        continue;
                    }
                }

                let similarity = compute_similarity(&query.vector, &data.vector, metric)?;

                // Track similarity distribution
                self.similarity_histogram.observe(similarity as f64);

                // Apply minimum similarity threshold
                if let Some(min_sim) = query.min_similarity {
                    if similarity < min_sim {
                        continue;
                    }
                }

                similarities.push((entry.key().clone(), similarity));
            }

            // Sort by similarity
            similarities.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(Ordering::Equal));
            similarities.truncate(query.k);
            similarities
        };

        // Update statistics and track metrics
        let query_time = start.elapsed();
        self.search_timer.observe(query_time);

        let mut stats = self.stats.write().await;
        stats.avg_query_time = (stats.avg_query_time + query_time) / 2;

        // Cache result
        if self.config.enable_cache {
            let cache_key = format!(
                "{:?}_{}_{}_{:?}",
                query.vector, query.k, metric, query.filters
            );
            self.query_cache.insert(cache_key, results.clone());
        }

        Ok(results)
    }

    async fn get(&self, id: &str) -> Result<Option<VectorData>> {
        Ok(self.vectors.get(id).map(|entry| entry.value().clone()))
    }

    async fn delete(&self, id: &str) -> Result<bool> {
        let removed = self.vectors.remove(id).is_some();

        if removed {
            // Remove from index
            if let Some(index) = self.index.write().await.as_mut() {
                index.remove(id).await?;
            }

            // Update statistics
            let mut stats = self.stats.write().await;
            stats.total_vectors = self.vectors.len();
        }

        Ok(removed)
    }

    async fn update(
        &self,
        id: String,
        vector: Vec<f32>,
        metadata: Option<HashMap<String, String>>,
    ) -> Result<()> {
        if vector.len() != self.config.dimension {
            return Err(anyhow!("Vector dimension mismatch"));
        }

        let data = VectorData {
            id: id.clone(),
            vector: vector.clone(),
            metadata,
            timestamp: std::time::SystemTime::now(),
        };

        self.vectors.insert(id.clone(), data);

        // Update index
        if let Some(index) = self.index.write().await.as_mut() {
            index.remove(&id).await?;
            index.add(id, vector).await?;
        }

        Ok(())
    }

    fn size(&self) -> usize {
        self.vectors.len()
    }

    async fn build_index(&self) -> Result<()> {
        let start = std::time::Instant::now();

        let mut new_index: Box<dyn VectorIndex> = match &self.config.index_type {
            IndexType::Flat => Box::new(FlatIndex::new()),
            IndexType::HNSW {
                max_connections,
                ef_construction,
                ef_search,
            } => Box::new(HNSWIndex::new(
                *max_connections,
                *ef_construction,
                *ef_search,
            )),
            _ => return Err(anyhow!("Index type not yet implemented")),
        };

        new_index.build(&self.vectors).await?;

        *self.index.write().await = Some(new_index);

        // Update statistics
        let mut stats = self.stats.write().await;
        stats.index_build_time = start.elapsed();

        Ok(())
    }

    async fn get_statistics(&self) -> Result<VectorStoreStats> {
        let mut stats = self.stats.read().await.clone();

        // Calculate actual cache hit rate
        let hits = self.cache_hits.load(std::sync::atomic::Ordering::Relaxed);
        let misses = self.cache_misses.load(std::sync::atomic::Ordering::Relaxed);
        let total = hits + misses;

        stats.cache_hit_rate = if total > 0 {
            hits as f32 / total as f32
        } else {
            0.0
        };

        Ok(stats)
    }
}

// Additional methods for InMemoryVectorStore outside the trait
impl InMemoryVectorStore {
    /// Get production performance metrics
    ///
    /// Returns comprehensive SCIRS2-based metrics including:
    /// - Total insert/search operations
    /// - Search latency statistics
    /// - Similarity score distribution
    /// - Index build performance
    pub fn get_performance_metrics(&self) -> VectorStorePerformanceMetrics {
        let insert_count = self.insert_counter.get();
        let search_count = self.search_counter.get();

        let search_timer_stats = self.search_timer.get_stats();
        let index_timer_stats = self.index_build_timer.get_stats();
        let similarity_hist_stats = self.similarity_histogram.get_stats();

        VectorStorePerformanceMetrics {
            total_inserts: insert_count,
            total_searches: search_count,
            avg_search_latency_ms: search_timer_stats.mean * 1000.0,
            min_search_latency_ms: 0.0, // Timer doesn't track min
            max_search_latency_ms: 0.0, // Timer doesn't track max
            avg_index_build_time_ms: index_timer_stats.mean * 1000.0,
            avg_similarity_score: similarity_hist_stats.mean,
            similarity_count: similarity_hist_stats.count,
        }
    }

    /// Get metrics registry for external monitoring
    pub fn metrics_registry(&self) -> &Arc<MetricsRegistry> {
        &self.metrics_registry
    }
}

/// Vector store performance metrics from SCIRS2
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VectorStorePerformanceMetrics {
    /// Total number of insert operations
    pub total_inserts: u64,

    /// Total number of search operations
    pub total_searches: u64,

    /// Average search latency in milliseconds
    pub avg_search_latency_ms: f64,

    /// Minimum search latency in milliseconds
    pub min_search_latency_ms: f64,

    /// Maximum search latency in milliseconds
    pub max_search_latency_ms: f64,

    /// Average index build time in milliseconds
    pub avg_index_build_time_ms: f64,

    /// Average similarity score across all computations
    pub avg_similarity_score: f64,

    /// Total similarity score calculations
    pub similarity_count: u64,
}

impl std::fmt::Display for VectorStorePerformanceMetrics {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "VectorPerf {{ inserts: {}, searches: {}, avg_latency: {:.2}ms, min: {:.2}ms, max: {:.2}ms, avg_similarity: {:.3}, computations: {} }}",
            self.total_inserts,
            self.total_searches,
            self.avg_search_latency_ms,
            self.min_search_latency_ms,
            self.max_search_latency_ms,
            self.avg_similarity_score,
            self.similarity_count
        )
    }
}

/// Compute similarity between two vectors using SIMD-optimized operations
///
/// This function uses scirs2_core's ndarray operations which leverage BLAS
/// and SIMD instructions for maximum performance (5-10x faster than naive iteration).
pub fn compute_similarity(a: &[f32], b: &[f32], metric: SimilarityMetric) -> Result<f32> {
    if a.len() != b.len() {
        return Err(anyhow!("Vector dimension mismatch"));
    }

    // Convert slices to ndarray views for SIMD-optimized operations
    let a_arr = ArrayView1::from(a);
    let b_arr = ArrayView1::from(b);

    match metric {
        SimilarityMetric::Cosine => {
            // Use ndarray's optimized dot product (BLAS-accelerated)
            let dot_product = a_arr.dot(&b_arr);

            // Compute norms using SIMD-optimized operations
            let norm_a = a_arr.dot(&a_arr).sqrt();
            let norm_b = b_arr.dot(&b_arr).sqrt();

            if norm_a == 0.0 || norm_b == 0.0 {
                Ok(0.0)
            } else {
                Ok(dot_product / (norm_a * norm_b))
            }
        }

        SimilarityMetric::Euclidean => {
            // Compute squared difference using ndarray operations
            let diff = &a_arr - &b_arr;
            let distance = diff.dot(&diff).sqrt();
            Ok(1.0 / (1.0 + distance)) // Convert distance to similarity
        }

        SimilarityMetric::Manhattan => {
            // Use mapv for SIMD-optimized absolute value and sum
            let diff = &a_arr - &b_arr;
            let distance = diff.mapv(f32::abs).sum();
            Ok(1.0 / (1.0 + distance))
        }

        SimilarityMetric::DotProduct => {
            // Direct BLAS-accelerated dot product
            Ok(a_arr.dot(&b_arr))
        }

        SimilarityMetric::Jaccard => {
            // Use SIMD-optimized boolean operations
            let a_binary = a_arr.mapv(|x| if x > 0.0 { 1u32 } else { 0 });
            let b_binary = b_arr.mapv(|x| if x > 0.0 { 1u32 } else { 0 });

            // Intersection: both are 1
            let intersection: u32 = (&a_binary * &b_binary).sum();

            // Union: at least one is 1
            let union: u32 = a_binary
                .iter()
                .zip(b_binary.iter())
                .map(|(x, y)| if *x > 0 || *y > 0 { 1 } else { 0 })
                .sum();

            if union == 0 {
                Ok(0.0)
            } else {
                Ok(intersection as f32 / union as f32)
            }
        }

        SimilarityMetric::Hamming => {
            // Convert to binary using SIMD-optimized mapv
            let a_binary = a_arr.mapv(|x| if x > 0.0 { 1u32 } else { 0 });
            let b_binary = b_arr.mapv(|x| if x > 0.0 { 1u32 } else { 0 });

            // Count differences using SIMD operations
            let differences: u32 = a_binary
                .iter()
                .zip(b_binary.iter())
                .map(|(x, y)| if x != y { 1 } else { 0 })
                .sum();

            Ok(1.0 - (differences as f32 / a.len() as f32))
        }
    }
}

/// Batch compute similarities between a query vector and multiple candidate vectors
///
/// Uses parallel processing for large batches (>100 vectors) for additional speedup.
pub fn compute_similarities_batch(
    query: &[f32],
    candidates: &[&[f32]],
    metric: SimilarityMetric,
) -> Result<Vec<f32>> {
    if candidates.is_empty() {
        return Ok(Vec::new());
    }

    // Validate dimensions
    for candidate in candidates {
        if candidate.len() != query.len() {
            return Err(anyhow!("Vector dimension mismatch in batch"));
        }
    }

    let query_arr = ArrayView1::from(query);

    // Precompute query norm for cosine similarity
    let query_norm = match metric {
        SimilarityMetric::Cosine => {
            let norm = query_arr.dot(&query_arr).sqrt();
            if norm == 0.0 {
                return Ok(vec![0.0; candidates.len()]);
            }
            norm
        }
        _ => 1.0,
    };

    // Use parallel processing for large batches
    if candidates.len() > 100 {
        use rayon::prelude::*;

        let results: Vec<f32> = candidates
            .par_iter()
            .map(|candidate| {
                let c_arr = ArrayView1::from(*candidate);
                match metric {
                    SimilarityMetric::Cosine => {
                        let dot = query_arr.dot(&c_arr);
                        let c_norm = c_arr.dot(&c_arr).sqrt();
                        if c_norm == 0.0 {
                            0.0
                        } else {
                            dot / (query_norm * c_norm)
                        }
                    }
                    SimilarityMetric::Euclidean => {
                        let diff = &query_arr - &c_arr;
                        let dist = diff.dot(&diff).sqrt();
                        1.0 / (1.0 + dist)
                    }
                    SimilarityMetric::Manhattan => {
                        let diff = &query_arr - &c_arr;
                        let dist = diff.mapv(f32::abs).sum();
                        1.0 / (1.0 + dist)
                    }
                    SimilarityMetric::DotProduct => query_arr.dot(&c_arr),
                    _ => compute_similarity(query, candidate, metric).unwrap_or(0.0),
                }
            })
            .collect();

        Ok(results)
    } else {
        // Sequential for small batches (avoid parallel overhead)
        candidates
            .iter()
            .map(|candidate| compute_similarity(query, candidate, metric))
            .collect()
    }
}

/// Create vector store based on configuration
pub fn create_vector_store(config: &VectorStoreConfig) -> Result<Arc<dyn VectorStore>> {
    Ok(Arc::new(InMemoryVectorStore::new(config.clone())))
}

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

    #[tokio::test]
    async fn test_vector_store_creation() {
        let config = VectorStoreConfig::default();
        let store = InMemoryVectorStore::new(config);
        assert_eq!(store.size(), 0);
    }

    #[tokio::test]
    async fn test_vector_insertion_and_retrieval() {
        let config = VectorStoreConfig {
            dimension: 3,
            ..Default::default()
        };
        let store = InMemoryVectorStore::new(config);

        let vector = vec![1.0, 2.0, 3.0];
        let metadata = Some(
            [("type".to_string(), "test".to_string())]
                .iter()
                .cloned()
                .collect(),
        );

        store
            .insert("test1".to_string(), vector.clone(), metadata.clone())
            .await
            .expect("operation should succeed");

        let retrieved = store
            .get("test1")
            .await
            .expect("async operation should succeed")
            .expect("operation should succeed");
        assert_eq!(retrieved.vector, vector);
        assert_eq!(retrieved.metadata, metadata);
    }

    #[tokio::test]
    async fn test_similarity_search() {
        let config = VectorStoreConfig {
            dimension: 3,
            ..Default::default()
        };
        let store = InMemoryVectorStore::new(config);

        // Insert test vectors
        store
            .insert("vec1".to_string(), vec![1.0, 0.0, 0.0], None)
            .await
            .expect("operation should succeed");
        store
            .insert("vec2".to_string(), vec![0.9, 0.1, 0.0], None)
            .await
            .expect("operation should succeed");
        store
            .insert("vec3".to_string(), vec![0.0, 1.0, 0.0], None)
            .await
            .expect("operation should succeed");

        let query = VectorQuery {
            vector: vec![1.0, 0.0, 0.0],
            k: 2,
            metric: Some(SimilarityMetric::Cosine),
            include_metadata: false,
            filters: None,
            min_similarity: None,
        };

        let results = store
            .search(&query)
            .await
            .expect("async operation should succeed");
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].0, "vec1"); // Should be most similar
    }

    #[test]
    fn test_similarity_metrics() {
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![2.0, 4.0, 6.0];

        let cosine = compute_similarity(&a, &b, SimilarityMetric::Cosine)
            .expect("similarity computation should succeed");
        assert!((cosine - 1.0).abs() < 1e-6); // Should be 1.0 (parallel vectors)

        let dot_product = compute_similarity(&a, &b, SimilarityMetric::DotProduct)
            .expect("similarity computation should succeed");
        assert_eq!(dot_product, 28.0); // 1*2 + 2*4 + 3*6 = 28
    }

    #[tokio::test]
    async fn test_index_building() {
        let config = VectorStoreConfig {
            dimension: 3,
            index_type: IndexType::Flat,
            ..Default::default()
        };
        let store = InMemoryVectorStore::new(config);

        store
            .insert("vec1".to_string(), vec![1.0, 0.0, 0.0], None)
            .await
            .expect("operation should succeed");
        store
            .insert("vec2".to_string(), vec![0.0, 1.0, 0.0], None)
            .await
            .expect("operation should succeed");

        store
            .build_index()
            .await
            .expect("async operation should succeed");

        let stats = store
            .get_statistics()
            .await
            .expect("async operation should succeed");
        assert_eq!(stats.total_vectors, 2);
    }

    #[tokio::test]
    async fn test_hnsw_index_building() {
        let config = VectorStoreConfig {
            dimension: 3,
            index_type: IndexType::HNSW {
                max_connections: 16,
                ef_construction: 100,
                ef_search: 50,
            },
            ..Default::default()
        };
        let store = InMemoryVectorStore::new(config);

        // Insert multiple vectors
        for i in 0..10 {
            let angle = (i as f32) * std::f32::consts::PI / 5.0;
            store
                .insert(format!("vec{i}"), vec![angle.cos(), angle.sin(), 0.0], None)
                .await
                .expect("operation should succeed");
        }

        // Build HNSW index
        store
            .build_index()
            .await
            .expect("async operation should succeed");

        let stats = store
            .get_statistics()
            .await
            .expect("async operation should succeed");
        assert_eq!(stats.total_vectors, 10);
        assert!(stats.index_type.contains("HNSW"));
    }

    #[tokio::test]
    async fn test_hnsw_search() {
        let config = VectorStoreConfig {
            dimension: 3,
            index_type: IndexType::HNSW {
                max_connections: 16,
                ef_construction: 100,
                ef_search: 50,
            },
            ..Default::default()
        };
        let store = InMemoryVectorStore::new(config);

        // Insert test vectors in a circle pattern
        for i in 0..20 {
            let angle = (i as f32) * std::f32::consts::PI * 2.0 / 20.0;
            store
                .insert(format!("vec{i}"), vec![angle.cos(), angle.sin(), 0.0], None)
                .await
                .expect("operation should succeed");
        }

        // Build HNSW index
        store
            .build_index()
            .await
            .expect("async operation should succeed");

        // Query for nearest neighbors to vec0 (1.0, 0.0, 0.0)
        let query = VectorQuery {
            vector: vec![1.0, 0.0, 0.0],
            k: 3,
            metric: Some(SimilarityMetric::Cosine),
            include_metadata: false,
            filters: None,
            min_similarity: None,
        };

        let results = store
            .search(&query)
            .await
            .expect("async operation should succeed");

        // Should find at least some results
        assert!(!results.is_empty());
        // First result should be vec0 (exact match)
        assert_eq!(results[0].0, "vec0");
        // Similarity should be 1.0 for exact match
        assert!((results[0].1 - 1.0).abs() < 0.01);
    }

    #[tokio::test]
    async fn test_hnsw_large_dataset() {
        let config = VectorStoreConfig {
            dimension: 10,
            index_type: IndexType::HNSW {
                max_connections: 16,
                ef_construction: 100,
                ef_search: 50,
            },
            ..Default::default()
        };
        let store = InMemoryVectorStore::new(config);

        // Insert 100 random vectors using deterministic pattern
        for i in 0..100 {
            let vec: Vec<f32> = (0..10)
                .map(|j| ((i * 7 + j * 13) % 100) as f32 / 100.0)
                .collect();
            store
                .insert(format!("vec{i}"), vec, None)
                .await
                .expect("async operation should succeed");
        }

        // Build HNSW index
        store
            .build_index()
            .await
            .expect("async operation should succeed");

        // Query
        let query_vec = vec![0.5f32; 10];
        let query = VectorQuery {
            vector: query_vec,
            k: 10,
            metric: Some(SimilarityMetric::Cosine),
            include_metadata: false,
            filters: None,
            min_similarity: None,
        };

        let results = store
            .search(&query)
            .await
            .expect("async operation should succeed");

        // Should return k results (or all if less than k)
        assert!(!results.is_empty());
        assert!(results.len() <= 10);

        // Results should be sorted by similarity (descending)
        for i in 1..results.len() {
            assert!(results[i - 1].1 >= results[i].1);
        }
    }

    #[test]
    fn test_batch_similarity_computation() {
        let query = vec![1.0, 0.0, 0.0];
        let candidates: Vec<&[f32]> =
            vec![&[1.0, 0.0, 0.0], &[0.0, 1.0, 0.0], &[0.707, 0.707, 0.0]];

        let similarities =
            compute_similarities_batch(&query, &candidates, SimilarityMetric::Cosine)
                .expect("batch similarity computation should succeed");

        assert_eq!(similarities.len(), 3);
        // First should be 1.0 (identical)
        assert!((similarities[0] - 1.0).abs() < 0.01);
        // Second should be 0.0 (orthogonal)
        assert!(similarities[1].abs() < 0.01);
        // Third should be ~0.707
        assert!((similarities[2] - 0.707).abs() < 0.01);
    }
}