synadb 1.3.0

An AI-native embedded database
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
// Copyright (c) 2025 SynaDB Contributors
// Licensed under the SynaDB License. See LICENSE file for details.

//! Hierarchical Navigable Small World (HNSW) index for approximate nearest neighbor search
//!
//! HNSW provides O(log N) search time vs O(N) for brute force, enabling
//! million-scale vector search with <10ms latency.
//!
//! # Algorithm Overview
//!
//! HNSW builds a multi-layer graph where:
//! - Layer 0 contains all nodes with M connections each
//! - Higher layers contain exponentially fewer nodes with M_max connections
//! - Search starts at the top layer and greedily descends
//!
//! # References
//!
//! - Malkov, Y. A., & Yashunin, D. A. (2018). Efficient and robust approximate
//!   nearest neighbor search using Hierarchical Navigable Small World graphs.

use crate::distance::DistanceMetric;
use crate::error::{Result, SynaError};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufReader, BufWriter, Read, Write};
use std::path::Path;

// =============================================================================
// Configuration
// =============================================================================

/// HNSW index configuration
///
/// These parameters control the trade-off between index quality, build time,
/// and search performance.
///
/// # Default Values
///
/// The defaults are tuned for a good balance of recall and performance:
/// - `m = 16`: Good for most use cases
/// - `m_max = 32`: 2x M for higher layers
/// - `ef_construction = 200`: High quality index
/// - `ef_search = 100`: Good recall with fast search
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HnswConfig {
    /// Max connections per node at layer 0.
    ///
    /// Higher values improve recall but increase memory and build time.
    /// Typical range: 8-64, default: 16
    pub m: usize,

    /// Max connections per node at higher layers.
    ///
    /// Usually set to 2*M. Controls the "highway" connectivity.
    pub m_max: usize,

    /// Size of dynamic candidate list during construction.
    ///
    /// Higher values build a better index but take longer.
    /// Typical range: 100-500, default: 200
    pub ef_construction: usize,

    /// Size of dynamic candidate list during search.
    ///
    /// Higher values improve recall but slow down search.
    /// Typical range: 50-500, default: 100
    pub ef_search: usize,

    /// Normalization factor for level generation.
    ///
    /// Controls the probability distribution of node levels.
    /// Default: 1/ln(M)
    pub ml: f64,
}

impl Default for HnswConfig {
    fn default() -> Self {
        Self {
            m: 16,
            m_max: 32,
            ef_construction: 200,
            ef_search: 100,
            ml: 1.0 / (16.0_f64).ln(),
        }
    }
}

impl HnswConfig {
    /// Create a new configuration with custom M value.
    ///
    /// Other parameters are derived from M:
    /// - m_max = 2 * m
    /// - ml = 1 / ln(m)
    pub fn with_m(m: usize) -> Self {
        Self {
            m,
            m_max: 2 * m,
            ml: 1.0 / (m as f64).ln(),
            ..Default::default()
        }
    }

    /// Set ef_construction (build quality).
    pub fn ef_construction(mut self, ef: usize) -> Self {
        self.ef_construction = ef;
        self
    }

    /// Set ef_search (search quality).
    pub fn ef_search(mut self, ef: usize) -> Self {
        self.ef_search = ef;
        self
    }
}

// =============================================================================
// Helper Types
// =============================================================================

/// A candidate node during search, ordered by distance.
///
/// Used in the priority queue for greedy search.
/// Ordered so that BinaryHeap (max-heap) returns the *farthest* node first,
/// which is useful for maintaining a bounded candidate set.
#[derive(Debug, Clone)]
pub struct Candidate {
    /// Node ID in the index
    pub node_id: usize,
    /// Distance from query vector
    pub distance: f32,
}

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

impl Eq for Candidate {}

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

impl Ord for Candidate {
    fn cmp(&self, other: &Self) -> Ordering {
        // Reverse ordering: larger distance = higher priority (for max-heap)
        // This lets us efficiently pop the farthest candidate
        match self.distance.partial_cmp(&other.distance) {
            Some(ord) => ord,
            None => Ordering::Equal, // Handle NaN
        }
    }
}

/// A candidate ordered for min-heap (closest first).
///
/// Used when we want to iterate from closest to farthest.
#[derive(Debug, Clone)]
pub struct MinCandidate {
    /// Node ID in the index
    pub node_id: usize,
    /// Distance from query vector
    pub distance: f32,
}

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

impl Eq for MinCandidate {}

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

impl Ord for MinCandidate {
    fn cmp(&self, other: &Self) -> Ordering {
        // Reverse of Candidate: smaller distance = higher priority
        match other.distance.partial_cmp(&self.distance) {
            Some(ord) => ord,
            None => Ordering::Equal,
        }
    }
}

// =============================================================================
// Node Structure
// =============================================================================

/// A node in the HNSW graph.
///
/// Each node stores:
/// - The key (for mapping back to the database)
/// - The vector data (cached for fast distance computation)
/// - Neighbor lists for each level the node exists in
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HnswNode {
    /// Key in the database (e.g., "vec/doc1")
    pub key: String,

    /// Vector data (cached for search performance)
    pub vector: Vec<f32>,

    /// Neighbors at each level: level -> [(neighbor_id, distance)]
    ///
    /// Level 0 has up to M neighbors, higher levels have up to M_max.
    pub neighbors: Vec<Vec<(usize, f32)>>,
}

impl HnswNode {
    /// Create a new node with the given key, vector, and level.
    pub fn new(key: String, vector: Vec<f32>, level: usize) -> Self {
        Self {
            key,
            vector,
            neighbors: vec![Vec::new(); level + 1],
        }
    }

    /// Get the maximum level this node exists at.
    pub fn level(&self) -> usize {
        self.neighbors.len().saturating_sub(1)
    }
}

// =============================================================================
// HNSW Index
// =============================================================================

/// HNSW index for approximate nearest neighbor search.
///
/// This is the main data structure for vector similarity search. It maintains
/// a multi-layer graph where each node is connected to its nearest neighbors.
///
/// # Example
///
/// ```rust,ignore
/// use synadb::hnsw::{HnswIndex, HnswConfig};
/// use synadb::distance::DistanceMetric;
///
/// // Create an index for 128-dimensional vectors
/// let config = HnswConfig::default();
/// let mut index = HnswIndex::new(128, DistanceMetric::Cosine, config);
///
/// // Insert vectors (implemented in task 6.2)
/// index.insert("doc1", &[0.1; 128]).unwrap();
/// index.insert("doc2", &[0.2; 128]).unwrap();
///
/// // Search for nearest neighbors (implemented in task 6.3)
/// let results = index.search(&[0.15; 128], 5).unwrap();
/// ```
pub struct HnswIndex {
    /// Index configuration
    config: HnswConfig,

    /// Distance metric for similarity computation
    metric: DistanceMetric,

    /// Number of dimensions for vectors in this index
    dimensions: u16,

    /// All nodes in the index.
    /// Public to allow VectorStore to build the index incrementally.
    pub nodes: Vec<HnswNode>,

    /// Entry point node ID (top of the graph).
    /// Public to allow VectorStore to set the entry point.
    pub entry_point: Option<usize>,

    /// Maximum level in the current graph
    max_level: usize,

    /// Map from key to node ID for fast lookup.
    /// Public to allow VectorStore to register keys.
    pub key_to_id: HashMap<String, usize>,

    /// Random number generator state for level selection
    rng_state: u64,
}

impl HnswIndex {
    /// Create a new empty HNSW index.
    ///
    /// # Arguments
    ///
    /// * `dimensions` - Number of dimensions for vectors (64-8192)
    /// * `metric` - Distance metric for similarity computation
    /// * `config` - Index configuration parameters
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use synadb::hnsw::{HnswIndex, HnswConfig};
    /// use synadb::distance::DistanceMetric;
    ///
    /// let index = HnswIndex::new(768, DistanceMetric::Cosine, HnswConfig::default());
    /// ```
    pub fn new(dimensions: u16, metric: DistanceMetric, config: HnswConfig) -> Self {
        Self {
            config,
            metric,
            dimensions,
            nodes: Vec::new(),
            entry_point: None,
            max_level: 0,
            key_to_id: HashMap::new(),
            // Initialize RNG with a simple seed based on current time
            rng_state: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos() as u64)
                .unwrap_or(42),
        }
    }

    /// Get the number of vectors in the index.
    pub fn len(&self) -> usize {
        self.nodes.len()
    }

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

    /// Get the configured dimensions.
    pub fn dimensions(&self) -> u16 {
        self.dimensions
    }

    /// Get the distance metric.
    pub fn metric(&self) -> DistanceMetric {
        self.metric
    }

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

    /// Get the entry point node ID.
    pub fn entry_point(&self) -> Option<usize> {
        self.entry_point
    }

    /// Get the maximum level in the graph.
    pub fn max_level(&self) -> usize {
        self.max_level
    }

    /// Set the maximum level in the graph.
    /// Used when adding nodes with higher levels than the current max.
    pub fn set_max_level(&mut self, level: usize) {
        if level > self.max_level {
            self.max_level = level;
        }
    }

    /// Check if a key exists in the index.
    pub fn contains_key(&self, key: &str) -> bool {
        self.key_to_id.contains_key(key)
    }

    /// Get the node ID for a key.
    pub fn get_node_id(&self, key: &str) -> Option<usize> {
        self.key_to_id.get(key).copied()
    }

    /// Get a node by ID.
    pub fn get_node(&self, node_id: usize) -> Option<&HnswNode> {
        self.nodes.get(node_id)
    }

    /// Get all keys in the index.
    pub fn keys(&self) -> impl Iterator<Item = &str> {
        self.key_to_id.keys().map(|s| s.as_str())
    }

    /// Generate a random level for a new node.
    ///
    /// Uses the formula: floor(-ln(uniform(0,1)) * ml)
    /// This gives an exponential distribution where most nodes are at level 0.
    pub fn random_level(&mut self) -> usize {
        // Simple xorshift64 PRNG
        self.rng_state ^= self.rng_state << 13;
        self.rng_state ^= self.rng_state >> 7;
        self.rng_state ^= self.rng_state << 17;

        // Convert to uniform [0, 1)
        let uniform = (self.rng_state as f64) / (u64::MAX as f64);

        // Compute level using inverse transform sampling
        let level = (-uniform.ln() * self.config.ml).floor() as usize;

        // Cap at a reasonable maximum to prevent pathological cases
        level.min(32)
    }

    /// Compute distance between a query vector and a node.
    pub fn distance_to_node(&self, query: &[f32], node_id: usize) -> f32 {
        self.metric.distance(query, &self.nodes[node_id].vector)
    }

    /// Get all node IDs at a given level.
    pub fn nodes_at_level(&self, level: usize) -> Vec<usize> {
        self.nodes
            .iter()
            .enumerate()
            .filter(|(_, node)| node.level() >= level)
            .map(|(id, _)| id)
            .collect()
    }

    /// Get statistics about the index.
    pub fn stats(&self) -> HnswStats {
        let mut level_counts = vec![0usize; self.max_level + 1];
        let mut total_edges = 0usize;

        for node in &self.nodes {
            for (level, neighbors) in node.neighbors.iter().enumerate() {
                if level < level_counts.len() {
                    level_counts[level] += 1;
                }
                total_edges += neighbors.len();
            }
        }

        HnswStats {
            num_nodes: self.nodes.len(),
            max_level: self.max_level,
            level_counts,
            total_edges,
            avg_edges_per_node: if self.nodes.is_empty() {
                0.0
            } else {
                total_edges as f64 / self.nodes.len() as f64
            },
        }
    }
}

/// Statistics about an HNSW index.
#[derive(Debug, Clone)]
pub struct HnswStats {
    /// Total number of nodes
    pub num_nodes: usize,
    /// Maximum level in the graph
    pub max_level: usize,
    /// Number of nodes at each level
    pub level_counts: Vec<usize>,
    /// Total number of edges (neighbor connections)
    pub total_edges: usize,
    /// Average edges per node
    pub avg_edges_per_node: f64,
}

// =============================================================================
// Search Implementation
// =============================================================================

/// Helper struct for min-heap ordering (closest first).
/// Used in search_layer to process candidates from closest to farthest.
struct MinHeapEntry(f32, usize);

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

impl Eq for MinHeapEntry {}

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

impl Ord for MinHeapEntry {
    fn cmp(&self, other: &Self) -> Ordering {
        // Reverse ordering: smaller distance = higher priority (for min-heap via BinaryHeap)
        other.0.total_cmp(&self.0)
    }
}

/// Helper struct for max-heap ordering (farthest first).
/// Used to maintain the worst result in the candidate set.
struct MaxHeapEntry(f32, usize);

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

impl Eq for MaxHeapEntry {}

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

impl Ord for MaxHeapEntry {
    fn cmp(&self, other: &Self) -> Ordering {
        // Normal ordering: larger distance = higher priority (for max-heap)
        self.0.total_cmp(&other.0)
    }
}

impl HnswIndex {
    /// Search for k nearest neighbors.
    ///
    /// This is the main search method for the HNSW index. It performs a greedy
    /// search starting from the entry point at the top level, descending through
    /// layers until reaching level 0 where the final candidates are selected.
    ///
    /// # Arguments
    ///
    /// * `query` - Query vector to search for
    /// * `k` - Number of nearest neighbors to return
    ///
    /// # Returns
    ///
    /// A vector of (key, distance) pairs, sorted by distance (closest first).
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let results = index.search(&query_vector, 10);
    /// for (key, distance) in results {
    ///     println!("{}: {:.4}", key, distance);
    /// }
    /// ```
    ///
    /// **Requirements:** 1.4, 1.5
    pub fn search(&self, query: &[f32], k: usize) -> Vec<(String, f32)> {
        // Return empty if no entry point
        let mut ep = match self.entry_point {
            Some(ep) => ep,
            None => return Vec::new(),
        };

        // Traverse from top level to level 1, finding the closest node at each level
        for lc in (1..=self.max_level).rev() {
            let results = self.search_layer(query, ep, 1, lc);
            if !results.is_empty() {
                ep = results[0].0;
            }
        }

        // Search at level 0 with ef_search candidates for better recall
        let candidates = self.search_layer(query, ep, self.config.ef_search, 0);

        // Return top k results with keys
        candidates
            .into_iter()
            .take(k)
            .map(|(id, dist)| (self.nodes[id].key.clone(), dist))
            .collect()
    }

    /// Search within a single layer of the HNSW graph.
    ///
    /// This implements the greedy search algorithm that explores neighbors
    /// starting from an entry point, maintaining a dynamic candidate list.
    ///
    /// # Arguments
    ///
    /// * `query` - Query vector
    /// * `entry_point` - Starting node ID for the search
    /// * `ef` - Size of the dynamic candidate list (controls recall vs speed)
    /// * `level` - The layer to search in
    ///
    /// # Returns
    ///
    /// A vector of (node_id, distance) pairs, sorted by distance (closest first).
    pub fn search_layer(
        &self,
        query: &[f32],
        entry_point: usize,
        ef: usize,
        level: usize,
    ) -> Vec<(usize, f32)> {
        use std::collections::{BinaryHeap, HashSet};

        let mut visited = HashSet::new();
        let mut candidates = BinaryHeap::new(); // min-heap by distance (closest first)
        let mut results = BinaryHeap::new(); // max-heap for tracking worst result

        // Initialize with entry point
        let ep_dist = self.metric.distance(query, &self.nodes[entry_point].vector);
        visited.insert(entry_point);
        candidates.push(MinHeapEntry(ep_dist, entry_point));
        results.push(MaxHeapEntry(ep_dist, entry_point));

        // Greedy search: explore candidates until no improvement possible
        while let Some(MinHeapEntry(c_dist, c_id)) = candidates.pop() {
            // Get the worst distance in our result set
            let worst_dist = results.peek().map(|e| e.0).unwrap_or(f32::MAX);

            // If current candidate is farther than our worst result, we're done
            if c_dist > worst_dist {
                break;
            }

            // Explore neighbors at this level
            if level < self.nodes[c_id].neighbors.len() {
                for (neighbor_id, _) in &self.nodes[c_id].neighbors[level] {
                    // Skip already visited nodes
                    if visited.insert(*neighbor_id) {
                        let dist = self
                            .metric
                            .distance(query, &self.nodes[*neighbor_id].vector);
                        let worst_dist = results.peek().map(|e| e.0).unwrap_or(f32::MAX);

                        // Add to candidates if better than worst or we haven't filled ef yet
                        if results.len() < ef || dist < worst_dist {
                            candidates.push(MinHeapEntry(dist, *neighbor_id));
                            results.push(MaxHeapEntry(dist, *neighbor_id));

                            // Maintain ef size by removing worst
                            if results.len() > ef {
                                results.pop();
                            }
                        }
                    }
                }
            }
        }

        // Convert to sorted vec (closest first)
        let mut result_vec: Vec<_> = results
            .into_iter()
            .map(|MaxHeapEntry(d, id)| (id, d))
            .collect();
        result_vec.sort_by(|a, b| a.1.total_cmp(&b.1));
        result_vec
    }

    /// Search for k nearest neighbors with a custom ef_search value.
    ///
    /// This allows overriding the configured ef_search for specific queries
    /// where you need higher recall or faster search.
    ///
    /// # Arguments
    ///
    /// * `query` - Query vector to search for
    /// * `k` - Number of nearest neighbors to return
    /// * `ef_search` - Size of dynamic candidate list (higher = better recall, slower)
    ///
    /// # Returns
    ///
    /// A vector of (key, distance) pairs, sorted by distance (closest first).
    pub fn search_with_ef(&self, query: &[f32], k: usize, ef_search: usize) -> Vec<(String, f32)> {
        // Return empty if no entry point
        let mut ep = match self.entry_point {
            Some(ep) => ep,
            None => return Vec::new(),
        };

        // Traverse from top level to level 1
        for lc in (1..=self.max_level).rev() {
            let results = self.search_layer(query, ep, 1, lc);
            if !results.is_empty() {
                ep = results[0].0;
            }
        }

        // Search at level 0 with custom ef_search
        let candidates = self.search_layer(query, ep, ef_search, 0);

        // Return top k results with keys
        candidates
            .into_iter()
            .take(k)
            .map(|(id, dist)| (self.nodes[id].key.clone(), dist))
            .collect()
    }
}

// =============================================================================
// Persistence
// =============================================================================

/// Magic bytes for HNSW index files
const HNSW_MAGIC: &[u8; 4] = b"HNSW";

/// Current version of the HNSW file format
const HNSW_VERSION: u16 = 1;

impl HnswIndex {
    /// Save the HNSW index to a file.
    ///
    /// The index is saved in a binary format that can be loaded later with
    /// [`HnswIndex::load`]. The format includes:
    /// - Magic bytes ("HNSW")
    /// - Version number (u16)
    /// - Configuration
    /// - Dimensions (u16)
    /// - Metric (u8)
    /// - All nodes with their vectors and neighbor lists
    /// - Entry point and max level
    ///
    /// # Arguments
    ///
    /// * `path` - Path to save the index file
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let index = HnswIndex::new(128, DistanceMetric::Cosine, HnswConfig::default());
    /// // ... insert vectors ...
    /// index.save("vectors.hnsw")?;
    /// ```
    ///
    /// **Requirements:** 1.5
    pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let file = File::create(path)?;
        let mut writer = BufWriter::new(file);

        // Write magic bytes
        writer.write_all(HNSW_MAGIC)?;

        // Write version
        writer.write_all(&HNSW_VERSION.to_le_bytes())?;

        // Write dimensions
        writer.write_all(&self.dimensions.to_le_bytes())?;

        // Write metric as u8
        let metric_byte: u8 = match self.metric {
            DistanceMetric::Cosine => 0,
            DistanceMetric::Euclidean => 1,
            DistanceMetric::DotProduct => 2,
        };
        writer.write_all(&[metric_byte])?;

        // Serialize config
        bincode::serialize_into(&mut writer, &self.config)?;

        // Serialize nodes
        bincode::serialize_into(&mut writer, &self.nodes)?;

        // Serialize entry point
        bincode::serialize_into(&mut writer, &self.entry_point)?;

        // Serialize max level
        bincode::serialize_into(&mut writer, &self.max_level)?;

        // Serialize RNG state for reproducibility
        bincode::serialize_into(&mut writer, &self.rng_state)?;

        // Flush to ensure all data is written
        writer.flush()?;

        Ok(())
    }

    /// Load an HNSW index from a file.
    ///
    /// Loads an index that was previously saved with [`HnswIndex::save`].
    /// The metric and dimensions are read from the file and must match
    /// the expected values if provided.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the index file
    ///
    /// # Returns
    ///
    /// The loaded HNSW index, or an error if the file is corrupted or
    /// incompatible.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let index = HnswIndex::load("vectors.hnsw")?;
    /// let results = index.search(&query, 10);
    /// ```
    ///
    /// **Requirements:** 1.5
    pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
        let file = File::open(path)?;
        let mut reader = BufReader::new(file);

        // Read and verify magic bytes
        let mut magic = [0u8; 4];
        reader.read_exact(&mut magic)?;
        if &magic != HNSW_MAGIC {
            return Err(SynaError::CorruptedIndex(
                "Invalid magic bytes - not an HNSW index file".to_string(),
            ));
        }

        // Read and verify version
        let mut version_bytes = [0u8; 2];
        reader.read_exact(&mut version_bytes)?;
        let version = u16::from_le_bytes(version_bytes);
        if version != HNSW_VERSION {
            return Err(SynaError::CorruptedIndex(format!(
                "Unsupported version: {} (expected {})",
                version, HNSW_VERSION
            )));
        }

        // Read dimensions
        let mut dims_bytes = [0u8; 2];
        reader.read_exact(&mut dims_bytes)?;
        let dimensions = u16::from_le_bytes(dims_bytes);

        // Read metric
        let mut metric_byte = [0u8; 1];
        reader.read_exact(&mut metric_byte)?;
        let metric = match metric_byte[0] {
            0 => DistanceMetric::Cosine,
            1 => DistanceMetric::Euclidean,
            2 => DistanceMetric::DotProduct,
            _ => {
                return Err(SynaError::CorruptedIndex(format!(
                    "Invalid metric byte: {}",
                    metric_byte[0]
                )))
            }
        };

        // Deserialize config
        let config: HnswConfig = bincode::deserialize_from(&mut reader)?;

        // Deserialize nodes
        let nodes: Vec<HnswNode> = bincode::deserialize_from(&mut reader)?;

        // Deserialize entry point
        let entry_point: Option<usize> = bincode::deserialize_from(&mut reader)?;

        // Deserialize max level
        let max_level: usize = bincode::deserialize_from(&mut reader)?;

        // Deserialize RNG state
        let rng_state: u64 = bincode::deserialize_from(&mut reader)?;

        // Rebuild key_to_id map from nodes
        let mut key_to_id = HashMap::new();
        for (id, node) in nodes.iter().enumerate() {
            key_to_id.insert(node.key.clone(), id);
        }

        Ok(Self {
            config,
            metric,
            dimensions,
            nodes,
            entry_point,
            max_level,
            key_to_id,
            rng_state,
        })
    }

    /// Load an HNSW index from a file with validation.
    ///
    /// Like [`HnswIndex::load`], but validates that the loaded index has
    /// the expected dimensions and metric.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the index file
    /// * `expected_dims` - Expected number of dimensions
    /// * `expected_metric` - Expected distance metric
    ///
    /// # Returns
    ///
    /// The loaded HNSW index, or an error if validation fails.
    ///
    /// **Requirements:** 1.5
    pub fn load_validated<P: AsRef<Path>>(
        path: P,
        expected_dims: u16,
        expected_metric: DistanceMetric,
    ) -> Result<Self> {
        let index = Self::load(path)?;

        if index.dimensions != expected_dims {
            return Err(SynaError::DimensionMismatch {
                expected: expected_dims,
                got: index.dimensions,
            });
        }

        if index.metric != expected_metric {
            return Err(SynaError::CorruptedIndex(format!(
                "Metric mismatch: expected {:?}, got {:?}",
                expected_metric, index.metric
            )));
        }

        Ok(index)
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_hnsw_config_default() {
        let config = HnswConfig::default();
        assert_eq!(config.m, 16);
        assert_eq!(config.m_max, 32);
        assert_eq!(config.ef_construction, 200);
        assert_eq!(config.ef_search, 100);
        assert!((config.ml - 1.0 / 16.0_f64.ln()).abs() < 1e-10);
    }

    #[test]
    fn test_hnsw_config_with_m() {
        let config = HnswConfig::with_m(32);
        assert_eq!(config.m, 32);
        assert_eq!(config.m_max, 64);
        assert!((config.ml - 1.0 / 32.0_f64.ln()).abs() < 1e-10);
    }

    #[test]
    fn test_hnsw_config_builder() {
        let config = HnswConfig::with_m(8).ef_construction(100).ef_search(50);
        assert_eq!(config.m, 8);
        assert_eq!(config.ef_construction, 100);
        assert_eq!(config.ef_search, 50);
    }

    #[test]
    fn test_candidate_ordering() {
        // Max-heap: larger distance = higher priority
        let mut heap = BinaryHeap::new();
        heap.push(Candidate {
            node_id: 0,
            distance: 1.0,
        });
        heap.push(Candidate {
            node_id: 1,
            distance: 3.0,
        });
        heap.push(Candidate {
            node_id: 2,
            distance: 2.0,
        });

        // Should pop in order: 3.0, 2.0, 1.0 (farthest first)
        assert_eq!(heap.pop().unwrap().distance, 3.0);
        assert_eq!(heap.pop().unwrap().distance, 2.0);
        assert_eq!(heap.pop().unwrap().distance, 1.0);
    }

    #[test]
    fn test_min_candidate_ordering() {
        // Min-heap: smaller distance = higher priority
        let mut heap = BinaryHeap::new();
        heap.push(MinCandidate {
            node_id: 0,
            distance: 1.0,
        });
        heap.push(MinCandidate {
            node_id: 1,
            distance: 3.0,
        });
        heap.push(MinCandidate {
            node_id: 2,
            distance: 2.0,
        });

        // Should pop in order: 1.0, 2.0, 3.0 (closest first)
        assert_eq!(heap.pop().unwrap().distance, 1.0);
        assert_eq!(heap.pop().unwrap().distance, 2.0);
        assert_eq!(heap.pop().unwrap().distance, 3.0);
    }

    #[test]
    fn test_hnsw_node_creation() {
        let node = HnswNode::new("test_key".to_string(), vec![1.0, 2.0, 3.0], 2);
        assert_eq!(node.key, "test_key");
        assert_eq!(node.vector, vec![1.0, 2.0, 3.0]);
        assert_eq!(node.level(), 2);
        assert_eq!(node.neighbors.len(), 3); // Levels 0, 1, 2
    }

    #[test]
    fn test_hnsw_index_creation() {
        let index = HnswIndex::new(128, DistanceMetric::Cosine, HnswConfig::default());
        assert_eq!(index.dimensions(), 128);
        assert_eq!(index.metric(), DistanceMetric::Cosine);
        assert!(index.is_empty());
        assert_eq!(index.len(), 0);
        assert!(index.entry_point().is_none());
    }

    #[test]
    fn test_random_level_distribution() {
        let mut index = HnswIndex::new(128, DistanceMetric::Cosine, HnswConfig::default());

        // Generate many levels and check distribution
        let mut level_counts = [0usize; 10];
        for _ in 0..10000 {
            let level = index.random_level();
            if level < 10 {
                level_counts[level] += 1;
            }
        }

        // Most nodes should be at level 0 (exponential distribution)
        assert!(level_counts[0] > level_counts[1]);
        assert!(level_counts[1] > level_counts[2]);

        // Level 0 should have the majority
        assert!(level_counts[0] > 5000);
    }

    #[test]
    fn test_hnsw_stats_empty() {
        let index = HnswIndex::new(128, DistanceMetric::Cosine, HnswConfig::default());
        let stats = index.stats();
        assert_eq!(stats.num_nodes, 0);
        assert_eq!(stats.total_edges, 0);
        assert_eq!(stats.avg_edges_per_node, 0.0);
    }

    #[test]
    fn test_search_empty_index() {
        let index = HnswIndex::new(3, DistanceMetric::Euclidean, HnswConfig::default());
        let query = vec![1.0, 2.0, 3.0];
        let results = index.search(&query, 5);
        assert!(results.is_empty());
    }

    #[test]
    fn test_search_single_node() {
        let mut index = HnswIndex::new(3, DistanceMetric::Euclidean, HnswConfig::default());

        // Manually add a single node
        let node = HnswNode::new("node1".to_string(), vec![1.0, 0.0, 0.0], 0);
        index.nodes.push(node);
        index.key_to_id.insert("node1".to_string(), 0);
        index.entry_point = Some(0);

        // Search should return the single node
        let query = vec![1.0, 0.0, 0.0];
        let results = index.search(&query, 5);

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].0, "node1");
        assert!(results[0].1 < 0.001); // Distance should be ~0
    }

    #[test]
    fn test_search_multiple_nodes_sorted() {
        let mut index = HnswIndex::new(3, DistanceMetric::Euclidean, HnswConfig::default());

        // Add three nodes at different distances from origin
        let node1 = HnswNode::new("close".to_string(), vec![1.0, 0.0, 0.0], 0);
        let node2 = HnswNode::new("medium".to_string(), vec![2.0, 0.0, 0.0], 0);
        let node3 = HnswNode::new("far".to_string(), vec![3.0, 0.0, 0.0], 0);

        index.nodes.push(node1);
        index.nodes.push(node2);
        index.nodes.push(node3);

        index.key_to_id.insert("close".to_string(), 0);
        index.key_to_id.insert("medium".to_string(), 1);
        index.key_to_id.insert("far".to_string(), 2);

        // Connect nodes at level 0 (all connected to each other)
        index.nodes[0].neighbors[0] = vec![(1, 1.0), (2, 2.0)];
        index.nodes[1].neighbors[0] = vec![(0, 1.0), (2, 1.0)];
        index.nodes[2].neighbors[0] = vec![(0, 2.0), (1, 1.0)];

        index.entry_point = Some(0);

        // Query from origin - should return nodes sorted by distance
        let query = vec![0.0, 0.0, 0.0];
        let results = index.search(&query, 3);

        assert_eq!(results.len(), 3);
        assert_eq!(results[0].0, "close"); // Distance 1.0
        assert_eq!(results[1].0, "medium"); // Distance 2.0
        assert_eq!(results[2].0, "far"); // Distance 3.0

        // Verify distances are sorted
        assert!(results[0].1 < results[1].1);
        assert!(results[1].1 < results[2].1);
    }

    #[test]
    fn test_search_k_limit() {
        let mut index = HnswIndex::new(3, DistanceMetric::Euclidean, HnswConfig::default());

        // Add 5 nodes
        for i in 0..5 {
            let node = HnswNode::new(format!("node{}", i), vec![i as f32, 0.0, 0.0], 0);
            index.nodes.push(node);
            index.key_to_id.insert(format!("node{}", i), i);
        }

        // Connect all nodes
        for i in 0..5 {
            let mut neighbors = Vec::new();
            for j in 0..5 {
                if i != j {
                    neighbors.push((j, (i as f32 - j as f32).abs()));
                }
            }
            index.nodes[i].neighbors[0] = neighbors;
        }

        index.entry_point = Some(0);

        // Request only 2 results
        let query = vec![0.0, 0.0, 0.0];
        let results = index.search(&query, 2);

        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_search_with_ef() {
        let mut index = HnswIndex::new(3, DistanceMetric::Euclidean, HnswConfig::default());

        // Add a single node
        let node = HnswNode::new("node1".to_string(), vec![1.0, 0.0, 0.0], 0);
        index.nodes.push(node);
        index.key_to_id.insert("node1".to_string(), 0);
        index.entry_point = Some(0);

        // Search with custom ef_search
        let query = vec![1.0, 0.0, 0.0];
        let results = index.search_with_ef(&query, 5, 50);

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].0, "node1");
    }

    #[test]
    fn test_min_heap_entry_ordering() {
        // Test the MinHeapEntry ordering for search_layer
        let mut heap = BinaryHeap::new();
        heap.push(MinHeapEntry(3.0, 0));
        heap.push(MinHeapEntry(1.0, 1));
        heap.push(MinHeapEntry(2.0, 2));

        // Should pop in order: 1.0, 2.0, 3.0 (closest first)
        assert_eq!(heap.pop().unwrap().0, 1.0);
        assert_eq!(heap.pop().unwrap().0, 2.0);
        assert_eq!(heap.pop().unwrap().0, 3.0);
    }

    #[test]
    fn test_max_heap_entry_ordering() {
        // Test the MaxHeapEntry ordering for search_layer
        let mut heap = BinaryHeap::new();
        heap.push(MaxHeapEntry(1.0, 0));
        heap.push(MaxHeapEntry(3.0, 1));
        heap.push(MaxHeapEntry(2.0, 2));

        // Should pop in order: 3.0, 2.0, 1.0 (farthest first)
        assert_eq!(heap.pop().unwrap().0, 3.0);
        assert_eq!(heap.pop().unwrap().0, 2.0);
        assert_eq!(heap.pop().unwrap().0, 1.0);
    }

    // =========================================================================
    // Persistence Tests
    // =========================================================================

    #[test]
    fn test_save_load_empty_index() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("empty.hnsw");

        // Create and save empty index
        let index = HnswIndex::new(128, DistanceMetric::Cosine, HnswConfig::default());
        index.save(&path).unwrap();

        // Load and verify
        let loaded = HnswIndex::load(&path).unwrap();
        assert_eq!(loaded.dimensions(), 128);
        assert_eq!(loaded.metric(), DistanceMetric::Cosine);
        assert!(loaded.is_empty());
        assert!(loaded.entry_point().is_none());
    }

    #[test]
    fn test_save_load_with_nodes() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("nodes.hnsw");

        // Create index with nodes
        let mut index = HnswIndex::new(3, DistanceMetric::Euclidean, HnswConfig::default());

        // Add nodes manually
        let node1 = HnswNode::new("node1".to_string(), vec![1.0, 0.0, 0.0], 0);
        let node2 = HnswNode::new("node2".to_string(), vec![0.0, 1.0, 0.0], 0);
        let node3 = HnswNode::new("node3".to_string(), vec![0.0, 0.0, 1.0], 0);

        index.nodes.push(node1);
        index.nodes.push(node2);
        index.nodes.push(node3);

        index.key_to_id.insert("node1".to_string(), 0);
        index.key_to_id.insert("node2".to_string(), 1);
        index.key_to_id.insert("node3".to_string(), 2);

        // Connect nodes
        index.nodes[0].neighbors[0] = vec![(1, 1.414), (2, 1.414)];
        index.nodes[1].neighbors[0] = vec![(0, 1.414), (2, 1.414)];
        index.nodes[2].neighbors[0] = vec![(0, 1.414), (1, 1.414)];

        index.entry_point = Some(0);

        // Save
        index.save(&path).unwrap();

        // Load and verify
        let loaded = HnswIndex::load(&path).unwrap();
        assert_eq!(loaded.dimensions(), 3);
        assert_eq!(loaded.metric(), DistanceMetric::Euclidean);
        assert_eq!(loaded.len(), 3);
        assert_eq!(loaded.entry_point(), Some(0));

        // Verify nodes
        assert!(loaded.contains_key("node1"));
        assert!(loaded.contains_key("node2"));
        assert!(loaded.contains_key("node3"));

        // Verify vectors
        let node1 = loaded.get_node(0).unwrap();
        assert_eq!(node1.vector, vec![1.0, 0.0, 0.0]);

        // Verify neighbors
        assert_eq!(node1.neighbors[0].len(), 2);
    }

    #[test]
    fn test_save_load_search_consistency() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("search.hnsw");

        // Create index with nodes
        let mut index = HnswIndex::new(3, DistanceMetric::Euclidean, HnswConfig::default());

        // Add nodes
        let node1 = HnswNode::new("close".to_string(), vec![1.0, 0.0, 0.0], 0);
        let node2 = HnswNode::new("medium".to_string(), vec![2.0, 0.0, 0.0], 0);
        let node3 = HnswNode::new("far".to_string(), vec![3.0, 0.0, 0.0], 0);

        index.nodes.push(node1);
        index.nodes.push(node2);
        index.nodes.push(node3);

        index.key_to_id.insert("close".to_string(), 0);
        index.key_to_id.insert("medium".to_string(), 1);
        index.key_to_id.insert("far".to_string(), 2);

        // Connect nodes
        index.nodes[0].neighbors[0] = vec![(1, 1.0), (2, 2.0)];
        index.nodes[1].neighbors[0] = vec![(0, 1.0), (2, 1.0)];
        index.nodes[2].neighbors[0] = vec![(0, 2.0), (1, 1.0)];

        index.entry_point = Some(0);

        // Search before save
        let query = vec![0.0, 0.0, 0.0];
        let results_before = index.search(&query, 3);

        // Save and load
        index.save(&path).unwrap();
        let loaded = HnswIndex::load(&path).unwrap();

        // Search after load
        let results_after = loaded.search(&query, 3);

        // Results should be identical
        assert_eq!(results_before.len(), results_after.len());
        for (before, after) in results_before.iter().zip(results_after.iter()) {
            assert_eq!(before.0, after.0); // Same keys
            assert!((before.1 - after.1).abs() < 1e-6); // Same distances
        }
    }

    #[test]
    fn test_load_invalid_magic() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("invalid.hnsw");

        // Write invalid magic bytes
        std::fs::write(&path, b"XXXX").unwrap();

        // Load should fail
        let result = HnswIndex::load(&path);
        assert!(result.is_err());
        match result {
            Err(crate::error::SynaError::CorruptedIndex(msg)) => {
                assert!(msg.contains("Invalid magic bytes"));
            }
            _ => panic!("Expected CorruptedIndex error"),
        }
    }

    #[test]
    fn test_load_invalid_version() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("invalid_version.hnsw");

        // Write valid magic but invalid version
        let mut data = Vec::new();
        data.extend_from_slice(b"HNSW");
        data.extend_from_slice(&99u16.to_le_bytes()); // Invalid version

        std::fs::write(&path, &data).unwrap();

        // Load should fail
        let result = HnswIndex::load(&path);
        assert!(result.is_err());
        match result {
            Err(crate::error::SynaError::CorruptedIndex(msg)) => {
                assert!(msg.contains("Unsupported version"));
            }
            _ => panic!("Expected CorruptedIndex error"),
        }
    }

    #[test]
    fn test_load_validated_success() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("validated.hnsw");

        // Create and save index
        let index = HnswIndex::new(128, DistanceMetric::Cosine, HnswConfig::default());
        index.save(&path).unwrap();

        // Load with correct validation
        let loaded = HnswIndex::load_validated(&path, 128, DistanceMetric::Cosine).unwrap();
        assert_eq!(loaded.dimensions(), 128);
        assert_eq!(loaded.metric(), DistanceMetric::Cosine);
    }

    #[test]
    fn test_load_validated_dimension_mismatch() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("dim_mismatch.hnsw");

        // Create and save index with 128 dimensions
        let index = HnswIndex::new(128, DistanceMetric::Cosine, HnswConfig::default());
        index.save(&path).unwrap();

        // Load with wrong dimensions
        let result = HnswIndex::load_validated(&path, 256, DistanceMetric::Cosine);
        assert!(result.is_err());
        match result {
            Err(crate::error::SynaError::DimensionMismatch { expected, got }) => {
                assert_eq!(expected, 256);
                assert_eq!(got, 128);
            }
            _ => panic!("Expected DimensionMismatch error"),
        }
    }

    #[test]
    fn test_load_validated_metric_mismatch() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("metric_mismatch.hnsw");

        // Create and save index with Cosine metric
        let index = HnswIndex::new(128, DistanceMetric::Cosine, HnswConfig::default());
        index.save(&path).unwrap();

        // Load with wrong metric
        let result = HnswIndex::load_validated(&path, 128, DistanceMetric::Euclidean);
        assert!(result.is_err());
        match result {
            Err(crate::error::SynaError::CorruptedIndex(msg)) => {
                assert!(msg.contains("Metric mismatch"));
            }
            _ => panic!("Expected CorruptedIndex error"),
        }
    }

    #[test]
    fn test_save_load_preserves_config() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.hnsw");

        // Create index with custom config
        let config = HnswConfig::with_m(32).ef_construction(300).ef_search(150);
        let index = HnswIndex::new(64, DistanceMetric::DotProduct, config);
        index.save(&path).unwrap();

        // Load and verify config
        let loaded = HnswIndex::load(&path).unwrap();
        assert_eq!(loaded.config().m, 32);
        assert_eq!(loaded.config().m_max, 64);
        assert_eq!(loaded.config().ef_construction, 300);
        assert_eq!(loaded.config().ef_search, 150);
        assert_eq!(loaded.metric(), DistanceMetric::DotProduct);
    }

    #[test]
    fn test_save_load_all_metrics() {
        let dir = tempfile::tempdir().unwrap();

        for metric in [
            DistanceMetric::Cosine,
            DistanceMetric::Euclidean,
            DistanceMetric::DotProduct,
        ] {
            let path = dir.path().join(format!("{:?}.hnsw", metric));
            let index = HnswIndex::new(64, metric, HnswConfig::default());
            index.save(&path).unwrap();

            let loaded = HnswIndex::load(&path).unwrap();
            assert_eq!(loaded.metric(), metric);
        }
    }
}