ipfrs-semantic 0.2.0

Semantic search with HNSW vector indexing for content-addressed data
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
//! DiskANN: Disk-based Approximate Nearest Neighbor Search
//!
//! This module provides on-disk graph-based indexing for handling
//! datasets too large to fit in memory (100M+ vectors).
//!
//! Key features:
//! - Memory-mapped graph access for constant memory usage
//! - Vamana algorithm for efficient graph construction
//! - Page cache optimization for fast queries
//! - Index compaction and optimization

use ipfrs_core::{Cid, Error, Result};
use memmap2::MmapMut;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::OpenOptions;
use std::path::Path;
use std::sync::{Arc, RwLock};

/// DiskANN index configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiskANNConfig {
    /// Vector dimension
    pub dimension: usize,
    /// Max degree of graph nodes (R parameter in Vamana)
    pub max_degree: usize,
    /// Queue size for graph construction (L parameter)
    pub queue_size: usize,
    /// Alpha parameter for pruning (typically 1.2)
    pub alpha: f32,
    /// Number of entry points for search
    pub num_entry_points: usize,
}

impl Default for DiskANNConfig {
    fn default() -> Self {
        Self {
            dimension: 768,
            max_degree: 64,
            queue_size: 100,
            alpha: 1.2,
            num_entry_points: 4,
        }
    }
}

/// On-disk index format header
#[derive(Debug, Clone, Serialize, Deserialize)]
struct IndexHeader {
    /// Magic bytes for format validation
    magic: [u8; 8],
    /// Format version
    version: u32,
    /// Configuration
    config: DiskANNConfig,
    /// Number of vectors in index
    num_vectors: usize,
    /// Offset to graph data
    graph_offset: u64,
    /// Offset to vector data
    vector_offset: u64,
    /// Offset to CID mapping
    cid_mapping_offset: u64,
}

impl IndexHeader {
    const MAGIC: [u8; 8] = *b"DISKANN1";

    fn new(config: DiskANNConfig) -> Self {
        Self {
            magic: Self::MAGIC,
            version: 1,
            config,
            num_vectors: 0,
            graph_offset: 0,
            vector_offset: 0,
            cid_mapping_offset: 0,
        }
    }

    fn validate(&self) -> Result<()> {
        if self.magic != Self::MAGIC {
            return Err(Error::InvalidInput(
                "Invalid DiskANN index file format".to_string(),
            ));
        }
        if self.version != 1 {
            return Err(Error::InvalidInput(format!(
                "Unsupported DiskANN version: {}",
                self.version
            )));
        }
        Ok(())
    }
}

/// Node in the graph stored on disk
#[allow(dead_code)]
#[derive(Debug, Clone)]
struct GraphNode {
    /// Node ID
    id: usize,
    /// Neighbor IDs
    neighbors: Vec<usize>,
}

/// Vector file header for memory-mapped storage
#[repr(C)]
#[derive(Debug, Clone, Copy)]
struct VectorFileHeader {
    /// Magic bytes for validation
    magic: [u8; 8],
    /// Number of vectors stored
    num_vectors: u64,
    /// Vector dimension
    dimension: u64,
}

impl VectorFileHeader {
    const MAGIC: [u8; 8] = *b"VECDATA1";
    const SIZE: usize = 24; // 8 + 8 + 8 bytes

    fn new(dimension: usize) -> Self {
        Self {
            magic: Self::MAGIC,
            num_vectors: 0,
            dimension: dimension as u64,
        }
    }

    #[allow(dead_code)]
    fn validate(&self, expected_dim: usize) -> Result<()> {
        if self.magic != Self::MAGIC {
            return Err(Error::InvalidInput(
                "Invalid vector file format".to_string(),
            ));
        }
        if self.dimension != expected_dim as u64 {
            return Err(Error::InvalidInput(format!(
                "Vector dimension mismatch: expected {}, got {}",
                expected_dim, self.dimension
            )));
        }
        Ok(())
    }

    fn as_bytes(&self) -> [u8; Self::SIZE] {
        let mut bytes = [0u8; Self::SIZE];
        bytes[0..8].copy_from_slice(&self.magic);
        bytes[8..16].copy_from_slice(&self.num_vectors.to_le_bytes());
        bytes[16..24].copy_from_slice(&self.dimension.to_le_bytes());
        bytes
    }

    #[allow(dead_code)]
    fn from_bytes(bytes: &[u8]) -> Result<Self> {
        if bytes.len() < Self::SIZE {
            return Err(Error::InvalidInput(
                "Vector file header too small".to_string(),
            ));
        }

        let mut magic = [0u8; 8];
        magic.copy_from_slice(&bytes[0..8]);

        let num_vectors = u64::from_le_bytes(
            bytes[8..16]
                .try_into()
                .expect("bytes[8..16] is exactly 8 bytes after bounds check"),
        );
        let dimension = u64::from_le_bytes(
            bytes[16..24]
                .try_into()
                .expect("bytes[16..24] is exactly 8 bytes after bounds check"),
        );

        Ok(Self {
            magic,
            num_vectors,
            dimension,
        })
    }
}

/// DiskANN index for large-scale vector search
pub struct DiskANNIndex {
    /// Configuration
    config: DiskANNConfig,
    /// Index file path
    index_path: Arc<RwLock<Option<String>>>,
    /// Memory-mapped graph data
    graph_mmap: Arc<RwLock<Option<MmapMut>>>,
    /// Memory-mapped vector data (true disk-based storage)
    vector_mmap: Arc<RwLock<Option<MmapMut>>>,
    /// Vector file path
    vector_file_path: Arc<RwLock<Option<String>>>,
    /// In-memory CID mapping (relatively small)
    id_to_cid: Arc<RwLock<HashMap<usize, Cid>>>,
    cid_to_id: Arc<RwLock<HashMap<Cid, usize>>>,
    /// In-memory graph (adjacency list)
    graph: Arc<RwLock<Vec<Vec<usize>>>>,
    /// Entry points for search
    entry_points: Arc<RwLock<Vec<usize>>>,
    /// Next available ID
    next_id: Arc<RwLock<usize>>,
    /// Whether index is loaded
    loaded: Arc<RwLock<bool>>,
}

impl DiskANNIndex {
    /// Create a new DiskANN index
    pub fn new(config: DiskANNConfig) -> Self {
        Self {
            config,
            index_path: Arc::new(RwLock::new(None)),
            graph_mmap: Arc::new(RwLock::new(None)),
            vector_mmap: Arc::new(RwLock::new(None)),
            vector_file_path: Arc::new(RwLock::new(None)),
            id_to_cid: Arc::new(RwLock::new(HashMap::new())),
            cid_to_id: Arc::new(RwLock::new(HashMap::new())),
            graph: Arc::new(RwLock::new(Vec::new())),
            entry_points: Arc::new(RwLock::new(Vec::new())),
            next_id: Arc::new(RwLock::new(0)),
            loaded: Arc::new(RwLock::new(false)),
        }
    }

    /// Helper: Get vector file path from index path
    fn get_vector_file_path(index_path: &str) -> String {
        format!("{}.vectors", index_path)
    }

    /// Helper: Calculate byte offset for a vector in the mmap file
    fn vector_offset(&self, vector_id: usize) -> usize {
        VectorFileHeader::SIZE + (vector_id * self.config.dimension * std::mem::size_of::<f32>())
    }

    /// Helper: Read a vector from the memory-mapped file
    fn read_vector(&self, vector_id: usize) -> Result<Vec<f32>> {
        let mmap = self.vector_mmap.read().unwrap_or_else(|e| e.into_inner());
        let mmap = mmap
            .as_ref()
            .ok_or_else(|| Error::InvalidInput("Vector file not mapped".to_string()))?;

        let offset = self.vector_offset(vector_id);
        let vec_size_bytes = self.config.dimension * std::mem::size_of::<f32>();

        if offset + vec_size_bytes > mmap.len() {
            return Err(Error::InvalidInput(format!(
                "Vector {} out of bounds",
                vector_id
            )));
        }

        let bytes = &mmap[offset..offset + vec_size_bytes];
        let floats: Vec<f32> = bytes
            .chunks_exact(4)
            .map(|chunk| {
                f32::from_le_bytes(
                    chunk
                        .try_into()
                        .expect("chunks_exact(4) guarantees exactly 4 bytes"),
                )
            })
            .collect();

        Ok(floats)
    }

    /// Helper: Write a vector to the memory-mapped file
    fn write_vector(&self, vector_id: usize, vector: &[f32]) -> Result<()> {
        if vector.len() != self.config.dimension {
            return Err(Error::InvalidInput(format!(
                "Vector dimension {} doesn't match expected {}",
                vector.len(),
                self.config.dimension
            )));
        }

        let mut mmap = self.vector_mmap.write().unwrap_or_else(|e| e.into_inner());
        let mmap = mmap
            .as_mut()
            .ok_or_else(|| Error::InvalidInput("Vector file not mapped".to_string()))?;

        let offset = self.vector_offset(vector_id);
        let vec_size_bytes = self.config.dimension * std::mem::size_of::<f32>();

        if offset + vec_size_bytes > mmap.len() {
            return Err(Error::InvalidInput(format!(
                "Vector {} out of bounds (mmap size: {}, needed: {})",
                vector_id,
                mmap.len(),
                offset + vec_size_bytes
            )));
        }

        let bytes = &mut mmap[offset..offset + vec_size_bytes];
        for (i, &val) in vector.iter().enumerate() {
            let val_bytes = val.to_le_bytes();
            bytes[i * 4..(i + 1) * 4].copy_from_slice(&val_bytes);
        }

        Ok(())
    }

    /// Helper: Update the vector count in the header
    fn update_vector_count(&self, count: usize) -> Result<()> {
        let mut mmap = self.vector_mmap.write().unwrap_or_else(|e| e.into_inner());
        let mmap = mmap
            .as_mut()
            .ok_or_else(|| Error::InvalidInput("Vector file not mapped".to_string()))?;

        let count_bytes = (count as u64).to_le_bytes();
        mmap[8..16].copy_from_slice(&count_bytes);

        Ok(())
    }

    /// Helper: Get current vector count from mmap header
    fn get_vector_count(&self) -> Result<usize> {
        let mmap = self.vector_mmap.read().unwrap_or_else(|e| e.into_inner());
        let mmap = mmap
            .as_ref()
            .ok_or_else(|| Error::InvalidInput("Vector file not mapped".to_string()))?;

        let count_bytes: [u8; 8] = mmap[8..16]
            .try_into()
            .expect("mmap[8..16] is exactly 8 bytes; mmap size checked above");
        Ok(u64::from_le_bytes(count_bytes) as usize)
    }

    /// Helper: Ensure vector file has capacity for n vectors (expand if needed)
    fn ensure_vector_capacity(&self, required_count: usize) -> Result<()> {
        let mmap = self.vector_mmap.read().unwrap_or_else(|e| e.into_inner());
        let current_size = mmap
            .as_ref()
            .ok_or_else(|| Error::InvalidInput("Vector file not mapped".to_string()))?
            .len();
        drop(mmap);

        let required_size = VectorFileHeader::SIZE
            + (required_count * self.config.dimension * std::mem::size_of::<f32>());

        if required_size > current_size {
            // Need to expand the file
            let new_capacity = (required_count * 2).max(required_count + 1000); // Double capacity or add 1000
            let new_size = VectorFileHeader::SIZE
                + (new_capacity * self.config.dimension * std::mem::size_of::<f32>());

            // Get file path and reopen/remap
            let vec_path = self
                .vector_file_path
                .read()
                .unwrap_or_else(|e| e.into_inner())
                .clone()
                .ok_or_else(|| Error::InvalidInput("No vector file path set".to_string()))?;

            // Drop the current mmap before resizing
            *self.vector_mmap.write().unwrap_or_else(|e| e.into_inner()) = None;

            // Resize the file
            let vec_file = OpenOptions::new()
                .read(true)
                .write(true)
                .open(&vec_path)
                .map_err(Error::Io)?;
            vec_file.set_len(new_size as u64).map_err(Error::Io)?;

            // Remap
            let new_mmap = unsafe {
                MmapMut::map_mut(&vec_file)
                    .map_err(|e| Error::Io(std::io::Error::other(e.to_string())))?
            };

            *self.vector_mmap.write().unwrap_or_else(|e| e.into_inner()) = Some(new_mmap);
        }

        Ok(())
    }

    /// Helper: Get number of vectors (from mmap header or fallback to next_id)
    fn num_vectors(&self) -> usize {
        self.get_vector_count()
            .unwrap_or_else(|_| *self.next_id.read().unwrap_or_else(|e| e.into_inner()))
    }

    /// Create with default configuration
    pub fn with_defaults(dimension: usize) -> Self {
        let config = DiskANNConfig {
            dimension,
            ..Default::default()
        };
        Self::new(config)
    }

    /// Create index file on disk
    pub fn create(&mut self, path: impl AsRef<Path>) -> Result<()> {
        let path = path.as_ref();
        let path_str = path.to_string_lossy().to_string();

        // Create index file
        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(true)
            .open(path)
            .map_err(Error::Io)?;

        // Write header
        let header = IndexHeader::new(self.config.clone());
        let header_bytes = oxicode::serde::encode_to_vec(&header, oxicode::config::standard())
            .map_err(|e| Error::Serialization(e.to_string()))?;

        // Initial file size: header + some space for growth
        let initial_size = header_bytes.len() + 1024 * 1024; // 1MB initial
        file.set_len(initial_size as u64).map_err(Error::Io)?;

        // Memory-map the file
        let mut mmap = unsafe {
            MmapMut::map_mut(&file).map_err(|e| Error::Io(std::io::Error::other(e.to_string())))?
        };

        // Write header to mmap
        mmap[..header_bytes.len()].copy_from_slice(&header_bytes);

        // Create vector file
        let vec_path = Self::get_vector_file_path(&path_str);
        let vec_file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(true)
            .open(&vec_path)
            .map_err(Error::Io)?;

        // Initial vector file size: header + space for 1000 vectors
        let vec_header = VectorFileHeader::new(self.config.dimension);
        let initial_vec_count = 1000;
        let vec_file_size = VectorFileHeader::SIZE
            + (initial_vec_count * self.config.dimension * std::mem::size_of::<f32>());
        vec_file.set_len(vec_file_size as u64).map_err(Error::Io)?;

        // Memory-map the vector file
        let mut vec_mmap = unsafe {
            MmapMut::map_mut(&vec_file)
                .map_err(|e| Error::Io(std::io::Error::other(e.to_string())))?
        };

        // Write vector header
        let header_bytes = vec_header.as_bytes();
        vec_mmap[..VectorFileHeader::SIZE].copy_from_slice(&header_bytes);

        *self.index_path.write().unwrap_or_else(|e| e.into_inner()) = Some(path_str.clone());
        *self
            .vector_file_path
            .write()
            .unwrap_or_else(|e| e.into_inner()) = Some(vec_path);
        *self.graph_mmap.write().unwrap_or_else(|e| e.into_inner()) = Some(mmap);
        *self.vector_mmap.write().unwrap_or_else(|e| e.into_inner()) = Some(vec_mmap);
        *self.loaded.write().unwrap_or_else(|e| e.into_inner()) = true;

        Ok(())
    }

    /// Load existing index from disk
    pub fn load(path: impl AsRef<Path>) -> Result<Self> {
        let path = path.as_ref();

        // Open index file
        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .open(path)
            .map_err(Error::Io)?;

        // Memory-map the file
        let mmap = unsafe {
            MmapMut::map_mut(&file).map_err(|e| Error::Io(std::io::Error::other(e.to_string())))?
        };

        // Read header
        let header: IndexHeader =
            oxicode::serde::decode_owned_from_slice(&mmap[..1024], oxicode::config::standard())
                .map(|(v, _)| v)
                .map_err(|e| Error::Serialization(e.to_string()))?;

        header.validate()?;

        // Create index
        let index = Self::new(header.config);
        *index.index_path.write().unwrap_or_else(|e| e.into_inner()) =
            Some(path.to_string_lossy().to_string());
        *index.graph_mmap.write().unwrap_or_else(|e| e.into_inner()) = Some(mmap);
        *index.next_id.write().unwrap_or_else(|e| e.into_inner()) = header.num_vectors;
        *index.loaded.write().unwrap_or_else(|e| e.into_inner()) = true;

        Ok(index)
    }

    /// Insert a vector using Vamana algorithm
    pub fn insert(&mut self, cid: &Cid, vector: &[f32]) -> Result<()> {
        if !*self.loaded.read().unwrap_or_else(|e| e.into_inner()) {
            return Err(Error::InvalidInput(
                "Index not created or loaded".to_string(),
            ));
        }

        if vector.len() != self.config.dimension {
            return Err(Error::InvalidInput(format!(
                "Vector dimension {} doesn't match index dimension {}",
                vector.len(),
                self.config.dimension
            )));
        }

        // Check if CID already exists
        if self
            .cid_to_id
            .read()
            .unwrap_or_else(|e| e.into_inner())
            .contains_key(cid)
        {
            return Err(Error::InvalidInput(format!(
                "CID already in index: {}",
                cid
            )));
        }

        // Get new ID
        let id = *self.next_id.read().unwrap_or_else(|e| e.into_inner());

        // Ensure vector file has enough space (expand if needed)
        self.ensure_vector_capacity(id + 1)?;

        // Write vector to memory-mapped file
        self.write_vector(id, vector)?;

        // Update vector count and next ID
        *self.next_id.write().unwrap_or_else(|e| e.into_inner()) += 1;
        self.update_vector_count(id + 1)?;

        // Add CID mapping
        self.id_to_cid
            .write()
            .unwrap_or_else(|e| e.into_inner())
            .insert(id, *cid);
        self.cid_to_id
            .write()
            .unwrap_or_else(|e| e.into_inner())
            .insert(*cid, id);

        // Initialize graph node
        self.graph
            .write()
            .unwrap_or_else(|e| e.into_inner())
            .push(Vec::new());

        // If this is the first vector, make it an entry point
        if id == 0 {
            self.entry_points
                .write()
                .unwrap_or_else(|e| e.into_inner())
                .push(0);
            return Ok(());
        }

        // Vamana graph construction
        self.vamana_insert(id, vector)?;

        // Update entry points if needed
        if id.is_multiple_of(1000) && id < 10000 {
            self.entry_points
                .write()
                .unwrap_or_else(|e| e.into_inner())
                .push(id);
            // Keep only num_entry_points
            let mut eps = self.entry_points.write().unwrap_or_else(|e| e.into_inner());
            let num_to_drain = if eps.len() > self.config.num_entry_points {
                eps.len() - self.config.num_entry_points
            } else {
                0
            };
            if num_to_drain > 0 {
                eps.drain(0..num_to_drain);
            }
        }

        Ok(())
    }

    /// Vamana graph construction for a new node
    fn vamana_insert(&self, new_id: usize, new_vec: &[f32]) -> Result<()> {
        // 1. Greedy search to find L nearest neighbors
        let neighbors =
            self.greedy_search_internal(new_vec, self.config.queue_size, self.config.queue_size)?;

        // 2. Prune to R neighbors using robust pruning
        let pruned = self.robust_prune(new_id, new_vec, &neighbors)?;

        // 3. Add bidirectional edges
        let mut graph = self.graph.write().unwrap_or_else(|e| e.into_inner());
        graph[new_id] = pruned.clone();

        // Add reverse edges and prune if needed
        for &neighbor_id in &pruned {
            if neighbor_id >= graph.len() {
                continue;
            }

            // Add reverse edge
            if !graph[neighbor_id].contains(&new_id) {
                graph[neighbor_id].push(new_id);

                // Prune if neighbor exceeds max degree
                if graph[neighbor_id].len() > self.config.max_degree {
                    let neighbor_vec = self.read_vector(neighbor_id)?;
                    let candidates = graph[neighbor_id].clone();

                    let pruned_neighbors =
                        self.robust_prune(neighbor_id, &neighbor_vec, &candidates)?;
                    graph[neighbor_id] = pruned_neighbors;
                }
            }
        }

        Ok(())
    }

    /// Robust pruning algorithm (RobustPrune from Vamana paper)
    fn robust_prune(
        &self,
        node_id: usize,
        node_vec: &[f32],
        candidates: &[usize],
    ) -> Result<Vec<usize>> {
        let alpha = self.config.alpha;
        let max_degree = self.config.max_degree;
        let num_vecs = self.num_vectors();

        // Compute distances from node to all candidates
        let mut dists: Vec<(usize, f32)> = candidates
            .iter()
            .filter(|&&c| c != node_id && c < num_vecs)
            .filter_map(|&c| {
                self.read_vector(c).ok().map(|vec| {
                    let dist = self.l2_distance(node_vec, &vec);
                    (c, dist)
                })
            })
            .collect();

        // Sort by distance
        dists.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));

        let mut pruned = Vec::new();

        for (cand_id, cand_dist) in dists {
            if pruned.len() >= max_degree {
                break;
            }

            // Check if candidate is alpha-close to any already selected neighbor
            let mut should_add = true;
            let cand_vec = self.read_vector(cand_id).ok();
            if let Some(ref c_vec) = cand_vec {
                for &selected_id in &pruned {
                    if let Ok(sel_vec) = self.read_vector(selected_id) {
                        let selected_dist = self.l2_distance(c_vec, &sel_vec);
                        if alpha * selected_dist < cand_dist {
                            should_add = false;
                            break;
                        }
                    }
                }
            } else {
                should_add = false;
            }

            if should_add {
                pruned.push(cand_id);
            }
        }

        Ok(pruned)
    }

    /// L2 distance between two vectors
    fn l2_distance<T: AsRef<[f32]>, U: AsRef<[f32]>>(&self, a: T, b: U) -> f32 {
        a.as_ref()
            .iter()
            .zip(b.as_ref().iter())
            .map(|(x, y)| (x - y) * (x - y))
            .sum::<f32>()
            .sqrt()
    }

    /// Search for k nearest neighbors using greedy search
    pub fn search(&self, query: &[f32], k: usize) -> Result<Vec<SearchResult>> {
        if !*self.loaded.read().unwrap_or_else(|e| e.into_inner()) {
            return Err(Error::InvalidInput(
                "Index not created or loaded".to_string(),
            ));
        }

        if query.len() != self.config.dimension {
            return Err(Error::InvalidInput(format!(
                "Query dimension {} doesn't match index dimension {}",
                query.len(),
                self.config.dimension
            )));
        }

        let num_vectors = self.num_vectors();
        if num_vectors == 0 {
            return Ok(Vec::new());
        }

        // Greedy search with L = max(k, queue_size)
        let search_list_size = k.max(self.config.queue_size);
        let result_ids = self.greedy_search_internal(query, k, search_list_size)?;

        // Convert to SearchResult with CIDs
        let id_to_cid = self.id_to_cid.read().unwrap_or_else(|e| e.into_inner());
        let results: Vec<SearchResult> = result_ids
            .iter()
            .filter_map(|&id| {
                id_to_cid.get(&id).and_then(|cid| {
                    self.read_vector(id).ok().map(|vec| SearchResult {
                        cid: *cid,
                        distance: self.l2_distance(query, &vec),
                    })
                })
            })
            .collect();

        Ok(results)
    }

    /// Internal greedy search returning node IDs
    fn greedy_search_internal(
        &self,
        query: &[f32],
        k: usize,
        search_list_size: usize,
    ) -> Result<Vec<usize>> {
        let graph = self.graph.read().unwrap_or_else(|e| e.into_inner());
        let entry_points = self.entry_points.read().unwrap_or_else(|e| e.into_inner());
        let num_vecs = self.num_vectors();

        if num_vecs == 0 {
            return Ok(Vec::new());
        }

        // Start from entry points
        let start_nodes: Vec<usize> = if entry_points.is_empty() {
            vec![0]
        } else {
            entry_points.clone()
        };

        // Visited set
        let mut visited = vec![false; num_vecs];

        // Priority queue: (distance, node_id)
        let mut candidates: Vec<(f32, usize)> = Vec::new();
        let mut results: Vec<(f32, usize)> = Vec::new();

        // Initialize with entry points
        for &node_id in &start_nodes {
            if node_id >= num_vecs {
                continue;
            }
            if let Ok(vec) = self.read_vector(node_id) {
                let dist = self.l2_distance(query, &vec);
                candidates.push((dist, node_id));
                results.push((dist, node_id));
                visited[node_id] = true;
            }
        }

        // Sort by distance (ascending)
        candidates.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
        results.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));

        // Greedy search
        while !candidates.is_empty() {
            // Get closest unvisited neighbor
            let (current_dist, current_id) = candidates.remove(0);

            // Stop if current is farther than the k-th result
            if results.len() >= search_list_size {
                let furthest_dist = results[search_list_size - 1].0;
                if current_dist > furthest_dist {
                    break;
                }
            }

            // Explore neighbors
            if current_id >= graph.len() {
                continue;
            }

            for &neighbor_id in &graph[current_id] {
                if neighbor_id >= num_vecs || visited[neighbor_id] {
                    continue;
                }

                visited[neighbor_id] = true;
                let dist = if let Ok(vec) = self.read_vector(neighbor_id) {
                    self.l2_distance(query, &vec)
                } else {
                    continue;
                };

                // Add to candidates
                candidates.push((dist, neighbor_id));
                candidates
                    .sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));

                // Add to results
                results.push((dist, neighbor_id));
                results.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));

                // Keep only search_list_size best results
                if results.len() > search_list_size {
                    results.truncate(search_list_size);
                }
            }
        }

        // Return top k
        Ok(results.iter().take(k).map(|(_, id)| *id).collect())
    }

    /// Get index statistics
    pub fn stats(&self) -> DiskANNStats {
        DiskANNStats {
            num_vectors: *self.next_id.read().unwrap_or_else(|e| e.into_inner()),
            dimension: self.config.dimension,
            max_degree: self.config.max_degree,
            index_loaded: *self.loaded.read().unwrap_or_else(|e| e.into_inner()),
            estimated_disk_size: self.estimate_disk_size(),
        }
    }

    /// Estimate disk usage
    fn estimate_disk_size(&self) -> usize {
        let num_vectors = *self.next_id.read().unwrap_or_else(|e| e.into_inner());

        // Header: ~1KB
        let header_size = 1024;

        // Vectors: num_vectors * dimension * 4 bytes
        let vector_size = num_vectors * self.config.dimension * 4;

        // Graph: num_vectors * max_degree * 4 bytes (assuming u32 node IDs)
        let graph_size = num_vectors * self.config.max_degree * 4;

        // CID mapping: num_vectors * ~40 bytes (CID size)
        let mapping_size = num_vectors * 40;

        header_size + vector_size + graph_size + mapping_size
    }

    /// Check if index is loaded
    pub fn is_loaded(&self) -> bool {
        *self.loaded.read().unwrap_or_else(|e| e.into_inner())
    }

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

    /// Save index to disk (persist all in-memory data)
    pub fn save(&self) -> Result<()> {
        if !*self.loaded.read().unwrap_or_else(|e| e.into_inner()) {
            return Err(Error::InvalidInput("Index not loaded".to_string()));
        }

        let path = self
            .index_path
            .read()
            .unwrap_or_else(|e| e.into_inner())
            .clone()
            .ok_or_else(|| Error::InvalidInput("No index path set".to_string()))?;

        // Serialize all data (read all vectors from mmap)
        let num_vecs = self.num_vectors();
        let mut vectors = Vec::with_capacity(num_vecs);
        for i in 0..num_vecs {
            if let Ok(vec) = self.read_vector(i) {
                vectors.push(vec);
            }
        }

        let graph = self.graph.read().unwrap_or_else(|e| e.into_inner());
        let id_to_cid = self.id_to_cid.read().unwrap_or_else(|e| e.into_inner());
        let entry_points = self.entry_points.read().unwrap_or_else(|e| e.into_inner());

        let data = DiskANNData::from_index(
            vectors,
            graph.clone(),
            id_to_cid.clone(),
            entry_points.clone(),
        );

        // Serialize to file
        let serialized = oxicode::serde::encode_to_vec(&data, oxicode::config::standard())
            .map_err(|e| Error::Serialization(e.to_string()))?;

        // Write to a temp file first, then rename (atomic)
        let temp_path = format!("{}.tmp", path);
        std::fs::write(&temp_path, &serialized).map_err(Error::Io)?;
        std::fs::rename(&temp_path, &path).map_err(Error::Io)?;

        Ok(())
    }

    /// Flush changes to disk
    pub fn flush(&self) -> Result<()> {
        if let Some(ref mut mmap) = *self.graph_mmap.write().unwrap_or_else(|e| e.into_inner()) {
            mmap.flush()
                .map_err(|e| Error::Io(std::io::Error::other(e.to_string())))?;
        }
        Ok(())
    }

    /// Compact the index by removing fragmentation
    ///
    /// This method:
    /// - Removes gaps in the ID space
    /// - Rebuilds the graph with contiguous IDs
    /// - Optimizes memory layout
    pub fn compact(&mut self) -> Result<CompactionStats> {
        if !*self.loaded.read().unwrap_or_else(|e| e.into_inner()) {
            return Err(Error::InvalidInput("Index not loaded".to_string()));
        }

        let start_time = std::time::Instant::now();
        let old_size = self.num_vectors();
        let graph = self.graph.read().unwrap_or_else(|e| e.into_inner());

        let old_graph_edges: usize = graph.iter().map(|neighbors| neighbors.len()).sum();

        // For now, just report stats since we don't have fragmentation yet
        // In a real implementation, we'd rebuild with contiguous IDs
        let stats = CompactionStats {
            duration_ms: start_time.elapsed().as_millis() as u64,
            vectors_before: old_size,
            vectors_after: old_size,
            graph_edges_before: old_graph_edges,
            graph_edges_after: old_graph_edges,
            bytes_saved: 0,
        };

        Ok(stats)
    }

    /// Prune the graph to remove low-quality edges
    ///
    /// This helps reduce memory usage and can improve query performance
    /// by removing edges that don't contribute to search quality.
    pub fn prune_graph(&mut self, quality_threshold: f32) -> Result<usize> {
        if !*self.loaded.read().unwrap_or_else(|e| e.into_inner()) {
            return Err(Error::InvalidInput("Index not loaded".to_string()));
        }

        let mut graph = self.graph.write().unwrap_or_else(|e| e.into_inner());
        let num_vecs = self.num_vectors();
        let mut total_pruned = 0;

        for node_id in 0..graph.len() {
            if node_id >= num_vecs {
                continue;
            }

            let node_vec = match self.read_vector(node_id) {
                Ok(v) => v,
                Err(_) => continue,
            };
            let neighbors = &graph[node_id];

            // Compute distances to all neighbors
            let mut neighbor_dists: Vec<(usize, f32)> = neighbors
                .iter()
                .filter(|&&n| n < num_vecs)
                .filter_map(|&n| {
                    self.read_vector(n).ok().map(|vec| {
                        let dist = self.l2_distance(&node_vec, &vec);
                        (n, dist)
                    })
                })
                .collect();

            // Sort by distance
            neighbor_dists
                .sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));

            // Keep only neighbors within quality threshold of the best
            if let Some(&(_, best_dist)) = neighbor_dists.first() {
                let threshold_dist = best_dist * (1.0 + quality_threshold);
                let keep_count = neighbor_dists
                    .iter()
                    .filter(|(_, d)| *d <= threshold_dist)
                    .count();

                if keep_count < neighbors.len() {
                    total_pruned += neighbors.len() - keep_count;
                    graph[node_id] = neighbor_dists
                        .iter()
                        .take(keep_count)
                        .map(|(n, _)| *n)
                        .collect();
                }
            }
        }

        Ok(total_pruned)
    }

    /// Get number of vectors in the index
    pub fn len(&self) -> usize {
        *self.next_id.read().unwrap_or_else(|e| e.into_inner())
    }

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

/// Data stored in DiskANN index file (serializable version)
#[derive(Debug, Clone, Serialize, Deserialize)]
struct DiskANNData {
    vectors: Vec<Vec<f32>>,
    graph: Vec<Vec<usize>>,
    id_to_cid: HashMap<usize, String>,
    entry_points: Vec<usize>,
}

impl DiskANNData {
    fn from_index(
        vectors: Vec<Vec<f32>>,
        graph: Vec<Vec<usize>>,
        id_to_cid: HashMap<usize, Cid>,
        entry_points: Vec<usize>,
    ) -> Self {
        let id_to_cid_str = id_to_cid
            .into_iter()
            .map(|(k, v)| (k, v.to_string()))
            .collect();
        Self {
            vectors,
            graph,
            id_to_cid: id_to_cid_str,
            entry_points,
        }
    }

    #[allow(dead_code)]
    fn to_cid_map(&self) -> Result<HashMap<usize, Cid>> {
        self.id_to_cid
            .iter()
            .map(|(k, v)| {
                v.parse::<Cid>()
                    .map(|cid| (*k, cid))
                    .map_err(|e| Error::InvalidInput(format!("Invalid CID: {}", e)))
            })
            .collect()
    }
}

/// Statistics from index compaction
#[derive(Debug, Clone)]
pub struct CompactionStats {
    /// Time taken for compaction
    pub duration_ms: u64,
    /// Number of vectors before compaction
    pub vectors_before: usize,
    /// Number of vectors after compaction
    pub vectors_after: usize,
    /// Number of graph edges before
    pub graph_edges_before: usize,
    /// Number of graph edges after
    pub graph_edges_after: usize,
    /// Bytes saved by compaction
    pub bytes_saved: usize,
}

/// Search result from DiskANN
#[derive(Debug, Clone)]
pub struct SearchResult {
    /// Content ID
    pub cid: Cid,
    /// Distance to query
    pub distance: f32,
}

/// DiskANN index statistics
#[derive(Debug, Clone)]
pub struct DiskANNStats {
    /// Number of vectors in index
    pub num_vectors: usize,
    /// Vector dimension
    pub dimension: usize,
    /// Maximum graph degree
    pub max_degree: usize,
    /// Whether index is loaded
    pub index_loaded: bool,
    /// Estimated disk size in bytes
    pub estimated_disk_size: usize,
}

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

    #[test]
    fn test_diskann_create() {
        let config = DiskANNConfig::default();
        let mut index = DiskANNIndex::new(config);

        let temp_file_path = std::env::temp_dir().join("test_diskann_index.dat");
        let temp_file = temp_file_path
            .to_str()
            .expect("temp dir path is valid UTF-8");
        assert!(index.create(temp_file).is_ok());
        assert!(index.is_loaded());

        // Cleanup
        std::fs::remove_file(temp_file).ok();
    }

    #[test]
    fn test_diskann_stats() {
        let index = DiskANNIndex::with_defaults(128);
        let stats = index.stats();

        assert_eq!(stats.dimension, 128);
        assert_eq!(stats.num_vectors, 0);
        assert!(!stats.index_loaded);
    }

    #[test]
    fn test_index_header() {
        let config = DiskANNConfig::default();
        let header = IndexHeader::new(config);

        assert_eq!(header.magic, IndexHeader::MAGIC);
        assert_eq!(header.version, 1);
        assert!(header.validate().is_ok());

        // Test invalid magic
        let mut bad_header = header.clone();
        bad_header.magic = [0; 8];
        assert!(bad_header.validate().is_err());
    }

    #[test]
    fn test_diskann_insert_and_search() {
        let config = DiskANNConfig {
            dimension: 4,
            max_degree: 16,
            queue_size: 50,
            ..Default::default()
        };

        let mut index = DiskANNIndex::new(config);
        let temp_file_path = std::env::temp_dir().join("test_diskann_vamana.dat");
        let temp_file = temp_file_path
            .to_str()
            .expect("temp dir path is valid UTF-8");
        index
            .create(temp_file)
            .expect("test: index creation should succeed");

        // Create test vectors
        let vectors = [
            vec![1.0, 0.0, 0.0, 0.0],
            vec![0.9, 0.1, 0.0, 0.0],
            vec![0.0, 1.0, 0.0, 0.0],
            vec![0.0, 0.0, 1.0, 0.0],
            vec![0.0, 0.0, 0.9, 0.1],
        ];

        // Insert vectors
        let base_cids = [
            "bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
            "bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354",
            "bafybeihvvulpp6bcs5kum72jh5tkfo35dz2ow3lrqw4hmqyqbmfyvdqvdq",
            "bafybeiakou6e7kkxc5qycjkqwucq4zfkfvzmlbf2vlihvqqnfjfzpqrkmq",
            "bafybeibscyh5z3uk6fvdidffhybzsxmckblkjhajy4y4uzcglmfwqx67b4",
        ];
        for (i, vec) in vectors.iter().enumerate() {
            let cid: Cid = base_cids[i].parse().expect("test: CID string is valid");
            index
                .insert(&cid, vec)
                .expect("test: vector insertion should succeed");
        }

        assert_eq!(index.stats().num_vectors, 5);

        // Search for nearest to first vector
        let query = vec![1.0, 0.0, 0.0, 0.0];
        let results = index
            .search(&query, 2)
            .expect("test: search should succeed");

        assert!(!results.is_empty());
        assert!(results.len() <= 2);
        // First result should be closest
        assert!(results[0].distance < 0.2);

        // Cleanup
        std::fs::remove_file(temp_file).ok();
    }

    #[test]
    fn test_vamana_graph_construction() {
        let config = DiskANNConfig {
            dimension: 8,
            max_degree: 8,
            queue_size: 20,
            alpha: 1.2,
            ..Default::default()
        };

        let max_degree = config.max_degree;
        let mut index = DiskANNIndex::new(config);
        let temp_file_path = std::env::temp_dir().join("test_vamana_graph.dat");
        let temp_file = temp_file_path
            .to_str()
            .expect("temp dir path is valid UTF-8");
        index
            .create(temp_file)
            .expect("test: index creation should succeed");

        // Insert 20 vectors
        let base_cids: Vec<&str> = vec![
            "bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
            "bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354",
            "bafybeihvvulpp6bcs5kum72jh5tkfo35dz2ow3lrqw4hmqyqbmfyvdqvdq",
            "bafybeiakou6e7kkxc5qycjkqwucq4zfkfvzmlbf2vlihvqqnfjfzpqrkmq",
            "bafybeibscyh5z3uk6fvdidffhybzsxmckblkjhajy4y4uzcglmfwqx67b4",
            "bafybeiezkzpo2uy4teyix63fjc3vgpxlvhbmwjicxhxx6vaf3ywvkyz5ia",
            "bafybeifmyetvpv2uovt7ncnvjcwvshwqrr7zmyh5wpqwmf5mwy3m42xkre",
            "bafybeia7lv6vknr6fqjq2jlj3ygbdgzdqxqt7xo3u7dzz6ihfzd3zhd6pi",
            "bafybeif2ewg3nqa33yvecifp7jw7p2utbnkh34j7ku44mzs3lpmcbdkjzq",
            "bafybeid5cg74fzlh7okcaabfwexdvkiuocwbqhwrqc4x65jyplwsxzvvdq",
            "bafybeicy6rxfqlcdadwjfjjvvb7wlbnlrzuzsogpv5snwt46zpqrmihtnq",
            "bafybeie2kj53f4wmefncg3rvrvfegwk265iw2psfszftvq3slajlwkjfpm",
            "bafybeigk7gjp4y4m4gwvmblvf7mlufsqtfgwyjdqwvwudytucvx7wtnz4e",
            "bafybeihbsq7kdawlkzvfj7xttx27t4p52pkllmfevn5l2scgbvmgqcfmfy",
            "bafybeiej5vfvbkjbzyeouqxkn25yb2xzdz2igdwmawcbhv66kwfwqnvhzi",
            "bafybeigbkbpcxqbrvx56fqf7jb25r5wunzowl45uwmzcbxkwdtixlbtwim",
            "bafybeihyfvtf3uiilqvqsvhbphfdudqy7qrjkxqglh26xxvjhtxrkhhbxe",
            "bafybeicflzm3r35m4kj5chxjvdwgajq6ljhqpsjq6wdyqnlpfjwwb5nowi",
            "bafybeic73hjrp52jxz33zxlz5qthfxumqpyuvqfvawdcskqiqlpuww3vxi",
            "bafybeicbh5dkdyiq3gqufk46cktiwwucwl6mzhv6e5xhzmuvzojvykokpy",
        ];
        for (i, &cid_str) in base_cids.iter().enumerate() {
            let cid: Cid = cid_str.parse().expect("test: CID string is valid");
            let vec: Vec<f32> = (0..8).map(|j| (i as f32 + j as f32) * 0.1).collect();
            index
                .insert(&cid, &vec)
                .expect("test: vector insertion should succeed");
        }

        // Check graph structure
        let graph = index.graph.read().unwrap_or_else(|e| e.into_inner());
        assert_eq!(graph.len(), 20);

        // Each node (except possibly the first) should have some neighbors
        for (i, neighbors) in graph.iter().enumerate().skip(1) {
            if i < 19 {
                // Not the last node
                assert!(!neighbors.is_empty(), "Node {} should have neighbors", i);
                assert!(
                    neighbors.len() <= max_degree,
                    "Node {} has too many neighbors: {}",
                    i,
                    neighbors.len()
                );
            }
        }

        // Cleanup
        std::fs::remove_file(temp_file).ok();
    }

    #[test]
    fn test_robust_pruning() {
        let config = DiskANNConfig {
            dimension: 4,
            max_degree: 3,
            alpha: 1.2,
            ..Default::default()
        };

        let max_degree = config.max_degree;
        let mut index = DiskANNIndex::new(config);
        let temp_file_path = std::env::temp_dir().join("test_robust_prune.dat");
        let temp_file = temp_file_path
            .to_str()
            .expect("temp dir path is valid UTF-8");
        index
            .create(temp_file)
            .expect("test: index creation should succeed");

        // Add some vectors manually (write to mmap)
        index
            .ensure_vector_capacity(4)
            .expect("test: capacity expansion for 4 vectors should succeed");
        index
            .write_vector(0, &[1.0, 0.0, 0.0, 0.0])
            .expect("test: writing vector 0 should succeed");
        index
            .write_vector(1, &[0.9, 0.1, 0.0, 0.0])
            .expect("test: writing vector 1 should succeed");
        index
            .write_vector(2, &[0.8, 0.2, 0.0, 0.0])
            .expect("test: writing vector 2 should succeed");
        index
            .write_vector(3, &[0.0, 1.0, 0.0, 0.0])
            .expect("test: writing vector 3 should succeed");
        index
            .update_vector_count(4)
            .expect("test: updating vector count should succeed");

        let node_vec = vec![1.0, 0.0, 0.0, 0.0];
        let candidates = vec![1, 2, 3];

        let pruned = index
            .robust_prune(0, &node_vec, &candidates)
            .expect("test: robust_prune should succeed");

        // Should prune to max_degree neighbors
        assert!(pruned.len() <= max_degree);
        // Should include the closest neighbor
        assert!(pruned.contains(&1));

        // Cleanup
        std::fs::remove_file(temp_file).ok();
    }

    #[test]
    fn test_diskann_save_and_load() {
        let config = DiskANNConfig {
            dimension: 4,
            max_degree: 16,
            ..Default::default()
        };

        let mut index = DiskANNIndex::new(config);
        let temp_file_path = std::env::temp_dir().join("test_diskann_save.dat");
        let temp_file = temp_file_path
            .to_str()
            .expect("temp dir path is valid UTF-8");
        index
            .create(temp_file)
            .expect("test: index creation should succeed");

        // Insert some vectors
        let vectors = [
            vec![1.0, 0.0, 0.0, 0.0],
            vec![0.0, 1.0, 0.0, 0.0],
            vec![0.0, 0.0, 1.0, 0.0],
        ];

        let base_cids = [
            "bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
            "bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354",
            "bafybeihvvulpp6bcs5kum72jh5tkfo35dz2ow3lrqw4hmqyqbmfyvdqvdq",
        ];

        for (i, vec) in vectors.iter().enumerate() {
            let cid: Cid = base_cids[i].parse().expect("test: CID string is valid");
            index
                .insert(&cid, vec)
                .expect("test: vector insertion should succeed");
        }

        // Save the index
        assert!(index.save().is_ok());

        // The save method overwrites the file, so we can't really test loading
        // without a proper load implementation that deserializes DiskANNData
        // For now, just verify save doesn't error

        // Cleanup
        std::fs::remove_file(temp_file).ok();
    }

    #[test]
    fn test_diskann_flush() {
        let config = DiskANNConfig {
            dimension: 4,
            ..Default::default()
        };

        let mut index = DiskANNIndex::new(config);
        let temp_file_path = std::env::temp_dir().join("test_diskann_flush.dat");
        let temp_file = temp_file_path
            .to_str()
            .expect("temp dir path is valid UTF-8");
        index
            .create(temp_file)
            .expect("test: index creation should succeed");

        // Flush should succeed
        assert!(index.flush().is_ok());

        // Cleanup
        std::fs::remove_file(temp_file).ok();
    }

    #[test]
    fn test_diskann_compact() {
        let config = DiskANNConfig {
            dimension: 4,
            max_degree: 16,
            ..Default::default()
        };

        let mut index = DiskANNIndex::new(config);
        let temp_file_path = std::env::temp_dir().join("test_diskann_compact.dat");
        let temp_file = temp_file_path
            .to_str()
            .expect("temp dir path is valid UTF-8");
        index
            .create(temp_file)
            .expect("test: index creation should succeed");

        // Insert some vectors
        let vectors = [
            vec![1.0, 0.0, 0.0, 0.0],
            vec![0.0, 1.0, 0.0, 0.0],
            vec![0.0, 0.0, 1.0, 0.0],
        ];

        let base_cids = [
            "bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
            "bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354",
            "bafybeihvvulpp6bcs5kum72jh5tkfo35dz2ow3lrqw4hmqyqbmfyvdqvdq",
        ];

        for (i, vec) in vectors.iter().enumerate() {
            let cid: Cid = base_cids[i].parse().expect("test: CID string is valid");
            index
                .insert(&cid, vec)
                .expect("test: vector insertion should succeed");
        }

        // Compact the index
        let stats = index.compact().expect("test: compact should succeed");
        assert_eq!(stats.vectors_before, 3);
        assert_eq!(stats.vectors_after, 3);

        // Cleanup
        std::fs::remove_file(temp_file).ok();
    }

    #[test]
    fn test_diskann_prune_graph() {
        let config = DiskANNConfig {
            dimension: 4,
            max_degree: 16,
            ..Default::default()
        };

        let mut index = DiskANNIndex::new(config);
        let temp_file_path = std::env::temp_dir().join("test_diskann_prune.dat");
        let temp_file = temp_file_path
            .to_str()
            .expect("temp dir path is valid UTF-8");
        index
            .create(temp_file)
            .expect("test: index creation should succeed");

        // Insert some vectors
        let vectors = [
            vec![1.0, 0.0, 0.0, 0.0],
            vec![0.9, 0.1, 0.0, 0.0],
            vec![0.8, 0.2, 0.0, 0.0],
            vec![0.0, 0.0, 1.0, 0.0],
        ];

        let base_cids = [
            "bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
            "bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354",
            "bafybeihvvulpp6bcs5kum72jh5tkfo35dz2ow3lrqw4hmqyqbmfyvdqvdq",
            "bafybeiakou6e7kkxc5qycjkqwucq4zfkfvzmlbf2vlihvqqnfjfzpqrkmq",
        ];

        for (i, vec) in vectors.iter().enumerate() {
            let cid: Cid = base_cids[i].parse().expect("test: CID string is valid");
            index
                .insert(&cid, vec)
                .expect("test: vector insertion should succeed");
        }

        // Prune with a quality threshold
        let _pruned = index
            .prune_graph(0.5)
            .expect("test: prune_graph should succeed");
        // Should prune some edges (pruned is usize, always >= 0)

        // Cleanup
        std::fs::remove_file(temp_file).ok();
    }

    #[test]
    fn test_diskann_len_and_is_empty() {
        let config = DiskANNConfig {
            dimension: 4,
            ..Default::default()
        };

        let mut index = DiskANNIndex::new(config);
        let temp_file_path = std::env::temp_dir().join("test_diskann_len.dat");
        let temp_file = temp_file_path
            .to_str()
            .expect("temp dir path is valid UTF-8");
        index
            .create(temp_file)
            .expect("test: index creation should succeed");

        assert_eq!(index.len(), 0);
        assert!(index.is_empty());

        // Insert a vector
        let cid: Cid = "bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi"
            .parse()
            .expect("test: CID string is valid");
        let vec = vec![1.0, 0.0, 0.0, 0.0];
        index
            .insert(&cid, &vec)
            .expect("test: vector insertion should succeed");

        assert_eq!(index.len(), 1);
        assert!(!index.is_empty());

        // Cleanup
        std::fs::remove_file(temp_file).ok();
    }
}