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

//! Memory-mapped vector store for ultra-high-throughput embedding storage.
//!
//! This module provides an alternative vector store implementation that uses
//! memory-mapped I/O for writes, achieving 500K-1M vectors/sec throughput.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │ MmapVectorStore                                              │
//! ├─────────────────────────────────────────────────────────────┤
//! │ mmap region (pre-allocated)                                  │
//! │ ┌─────────────────────────────────────────────────────────┐ │
//! │ │ Header (64 bytes)                                        │ │
//! │ │ - magic: u32                                             │ │
//! │ │ - version: u32                                           │ │
//! │ │ - dimensions: u16                                        │ │
//! │ │ - metric: u8                                             │ │
//! │ │ - vector_count: u64                                      │ │
//! │ │ - write_offset: u64                                      │ │
//! │ │ - reserved: [u8; 37]                                     │ │
//! │ ├─────────────────────────────────────────────────────────┤ │
//! │ │ Vector Data (contiguous f32 arrays)                      │ │
//! │ │ [key_len: u16][key: bytes][vector: f32 × dims]           │ │
//! │ │ [key_len: u16][key: bytes][vector: f32 × dims]           │ │
//! │ │ ...                                                      │ │
//! │ └─────────────────────────────────────────────────────────┘ │
//! ├─────────────────────────────────────────────────────────────┤
//! │ In-memory index                                              │
//! │ - keys: HashSet<String>        (O(1) existence check)       │
//! │ - key_to_offset: HashMap       (key → mmap offset)          │
//! │ - hnsw: Option<HnswIndex>      (similarity search)          │
//! └─────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Performance
//!
//! | Operation | VectorStore | MmapVectorStore | Improvement |
//! |-----------|-------------|-----------------|-------------|
//! | Write     | 130K/sec    | 500K-1M/sec     | 4-8x        |
//! | Search    | <1ms        | <1ms            | Same        |
//!
//! # Example
//!
//! ```rust,no_run
//! use synadb::mmap_vector::{MmapVectorStore, MmapVectorConfig};
//!
//! let config = MmapVectorConfig {
//!     dimensions: 768,
//!     initial_capacity: 100_000,  // Pre-allocate for 100K vectors
//!     ..Default::default()
//! };
//!
//! let mut store = MmapVectorStore::new("vectors.mmap", config).unwrap();
//!
//! // Ultra-fast writes (no syscalls!)
//! let embedding = vec![0.1f32; 768];
//! store.insert("doc1", &embedding).unwrap();
//!
//! // Build index for search
//! store.build_index().unwrap();
//!
//! // Fast similarity search
//! let results = store.search(&embedding, 10).unwrap();
//! ```

use std::collections::{HashMap, HashSet};
use std::fs::{File, OpenOptions};

use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};

use memmap2::MmapMut;

use crate::distance::DistanceMetric;
use crate::error::{Result, SynaError};
use crate::hnsw::{HnswConfig, HnswIndex, HnswNode};

/// Magic number for mmap vector store files.
const MMAP_MAGIC: u32 = 0x4D564543; // "MVEC"

/// Current file format version.
const MMAP_VERSION: u32 = 1;

/// Header size in bytes.
const HEADER_SIZE: usize = 64;

/// Default initial capacity (number of vectors).
const DEFAULT_INITIAL_CAPACITY: usize = 100_000;

/// Default checkpoint interval in seconds.
const DEFAULT_CHECKPOINT_SECS: u64 = 30;

/// Configuration for MmapVectorStore.
#[derive(Debug, Clone)]
pub struct MmapVectorConfig {
    /// Number of dimensions (64-8192).
    pub dimensions: u16,
    /// Distance metric for similarity search.
    pub metric: DistanceMetric,
    /// Initial capacity in number of vectors.
    /// The file will be pre-allocated to hold this many vectors.
    pub initial_capacity: usize,
    /// Number of vectors at which to automatically build HNSW index.
    pub index_threshold: usize,
    /// HNSW configuration for similarity search.
    pub hnsw_config: HnswConfig,
    /// Checkpoint interval in seconds (0 = only on close).
    pub checkpoint_interval_secs: u64,
}

impl Default for MmapVectorConfig {
    fn default() -> Self {
        Self {
            dimensions: 768,
            metric: DistanceMetric::Cosine,
            initial_capacity: DEFAULT_INITIAL_CAPACITY,
            index_threshold: 10_000,
            hnsw_config: HnswConfig::default(),
            checkpoint_interval_secs: DEFAULT_CHECKPOINT_SECS,
        }
    }
}

/// Result of a similarity search.
#[derive(Debug, Clone)]
pub struct MmapSearchResult {
    /// Key of the matching vector.
    pub key: String,
    /// Distance/similarity score (lower = more similar).
    pub score: f32,
    /// The vector data.
    pub vector: Vec<f32>,
}

/// Memory-mapped vector store for ultra-high-throughput writes.
///
/// This implementation uses memory-mapped I/O to achieve 500K-1M vectors/sec
/// write throughput by eliminating syscall overhead.
///
/// # Key Features
///
/// - **Direct memory writes**: No `write()` syscalls, just `memcpy`
/// - **Pre-allocated storage**: File is pre-sized to avoid remapping
/// - **Checkpoint-based durability**: `msync()` called periodically, not per-write
/// - **HNSW index**: Same O(log N) search as regular VectorStore
///
/// # Trade-offs
///
/// - Higher memory usage (file is memory-mapped)
/// - Checkpoint-bounded durability (may lose recent writes on crash)
/// - Requires pre-allocation (must estimate capacity)
pub struct MmapVectorStore {
    /// Path to the mmap file.
    path: PathBuf,
    /// Memory-mapped region (writable).
    mmap: MmapMut,
    /// Underlying file handle.
    file: File,
    /// Configuration.
    config: MmapVectorConfig,
    /// Current write offset (atomic for potential future concurrency).
    write_offset: AtomicU64,
    /// Number of vectors stored.
    vector_count: AtomicU64,
    /// Key existence check (O(1)).
    keys: HashSet<String>,
    /// Key to mmap offset mapping.
    key_to_offset: HashMap<String, u64>,
    /// HNSW index for similarity search.
    hnsw_index: Option<HnswIndex>,
    /// Whether index has unsaved changes.
    index_dirty: bool,
    /// Last checkpoint time.
    last_checkpoint: Instant,
    /// Checkpoint interval.
    checkpoint_interval: Duration,
}

impl MmapVectorStore {
    /// Creates or opens a memory-mapped vector store.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the mmap file
    /// * `config` - Configuration for the store
    ///
    /// # Errors
    ///
    /// * `SynaError::InvalidDimensions` - If dimensions are not in range 64-8192
    /// * `SynaError::Io` - If file operations fail
    pub fn new<P: AsRef<Path>>(path: P, config: MmapVectorConfig) -> Result<Self> {
        // Validate dimensions
        if config.dimensions < 64 || config.dimensions > 8192 {
            return Err(SynaError::InvalidDimensions(config.dimensions));
        }

        let path = path.as_ref().to_path_buf();
        let exists = path.exists();

        // Calculate required file size
        let vector_size = Self::vector_entry_size(config.dimensions, 256); // Assume max key len 256
        let file_size = HEADER_SIZE + (config.initial_capacity * vector_size);

        // Open or create file
        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(&path)?;

        // Set file size if new or smaller
        let current_size = file.metadata()?.len() as usize;
        if current_size < file_size {
            file.set_len(file_size as u64)?;
        }

        // Memory-map the file
        let mmap = unsafe { MmapMut::map_mut(&file)? };

        let checkpoint_interval = Duration::from_secs(config.checkpoint_interval_secs);

        let mut store = Self {
            path,
            mmap,
            file,
            config,
            write_offset: AtomicU64::new(HEADER_SIZE as u64),
            vector_count: AtomicU64::new(0),
            keys: HashSet::new(),
            key_to_offset: HashMap::new(),
            hnsw_index: None,
            index_dirty: false,
            last_checkpoint: Instant::now(),
            checkpoint_interval,
        };

        if exists {
            // Load existing data
            store.load_existing()?;
        } else {
            // Initialize header
            store.write_header()?;
        }

        // Try to load HNSW index
        store.try_load_hnsw_index();

        Ok(store)
    }

    /// Calculates the size of a vector entry in bytes.
    #[inline]
    fn vector_entry_size(dimensions: u16, key_len: usize) -> usize {
        2 + key_len + (dimensions as usize * 4) // key_len (u16) + key + vector
    }

    /// Writes the file header.
    fn write_header(&mut self) -> Result<()> {
        let header = &mut self.mmap[0..HEADER_SIZE];

        // Magic number (4 bytes)
        header[0..4].copy_from_slice(&MMAP_MAGIC.to_le_bytes());
        // Version (4 bytes)
        header[4..8].copy_from_slice(&MMAP_VERSION.to_le_bytes());
        // Dimensions (2 bytes)
        header[8..10].copy_from_slice(&self.config.dimensions.to_le_bytes());
        // Metric (1 byte)
        header[10] = self.config.metric as u8;
        // Vector count (8 bytes) - at offset 16
        header[16..24].copy_from_slice(&0u64.to_le_bytes());
        // Write offset (8 bytes) - at offset 24
        header[24..32].copy_from_slice(&(HEADER_SIZE as u64).to_le_bytes());

        Ok(())
    }

    /// Loads existing data from the mmap file.
    fn load_existing(&mut self) -> Result<()> {
        // Validate header
        let magic = u32::from_le_bytes([self.mmap[0], self.mmap[1], self.mmap[2], self.mmap[3]]);
        if magic != MMAP_MAGIC {
            return Err(SynaError::CorruptedIndex(
                "Invalid mmap vector file magic".to_string(),
            ));
        }

        let version = u32::from_le_bytes([self.mmap[4], self.mmap[5], self.mmap[6], self.mmap[7]]);
        if version != MMAP_VERSION {
            return Err(SynaError::CorruptedIndex(format!(
                "Unsupported mmap vector file version: {}",
                version
            )));
        }

        let dimensions = u16::from_le_bytes([self.mmap[8], self.mmap[9]]);
        if dimensions != self.config.dimensions {
            return Err(SynaError::DimensionMismatch {
                expected: self.config.dimensions,
                got: dimensions,
            });
        }

        let vector_count = u64::from_le_bytes([
            self.mmap[16],
            self.mmap[17],
            self.mmap[18],
            self.mmap[19],
            self.mmap[20],
            self.mmap[21],
            self.mmap[22],
            self.mmap[23],
        ]);
        let write_offset = u64::from_le_bytes([
            self.mmap[24],
            self.mmap[25],
            self.mmap[26],
            self.mmap[27],
            self.mmap[28],
            self.mmap[29],
            self.mmap[30],
            self.mmap[31],
        ]);

        self.vector_count.store(vector_count, Ordering::SeqCst);
        self.write_offset.store(write_offset, Ordering::SeqCst);

        // Rebuild in-memory index by scanning entries
        self.rebuild_index_from_mmap()?;

        Ok(())
    }

    /// Rebuilds the in-memory index by scanning the mmap region.
    fn rebuild_index_from_mmap(&mut self) -> Result<()> {
        let mut offset = HEADER_SIZE as u64;
        let write_offset = self.write_offset.load(Ordering::SeqCst);
        let dims = self.config.dimensions as usize;

        while offset < write_offset {
            // Read key length
            let key_len = u16::from_le_bytes(
                self.mmap[offset as usize..(offset as usize + 2)]
                    .try_into()
                    .map_err(|_| {
                        SynaError::CorruptedIndex("Failed to read key length".to_string())
                    })?,
            ) as usize;

            // Read key
            let key_start = offset as usize + 2;
            let key_end = key_start + key_len;
            let key = String::from_utf8(self.mmap[key_start..key_end].to_vec())
                .map_err(|_| SynaError::CorruptedIndex("Invalid UTF-8 key".to_string()))?;

            // Store key and offset
            self.keys.insert(key.clone());
            self.key_to_offset.insert(key, offset);

            // Move to next entry
            let entry_size = 2 + key_len + (dims * 4);
            offset += entry_size as u64;
        }

        Ok(())
    }

    /// Tries to load an existing HNSW index.
    fn try_load_hnsw_index(&mut self) {
        let hnsw_path = self.hnsw_index_path();
        if hnsw_path.exists() {
            if let Ok(index) =
                HnswIndex::load_validated(&hnsw_path, self.config.dimensions, self.config.metric)
            {
                if index.len() == self.keys.len() {
                    self.hnsw_index = Some(index);
                }
            }
        }
    }

    /// Returns the path to the HNSW index file.
    fn hnsw_index_path(&self) -> PathBuf {
        let mut path = self.path.clone();
        let ext = match path.extension() {
            Some(e) => format!("{}.hnsw", e.to_string_lossy()),
            None => "hnsw".to_string(),
        };
        path.set_extension(ext);
        path
    }

    /// Inserts a vector with the given key.
    ///
    /// This is an ultra-fast operation because it writes directly to memory
    /// without any syscalls. The data is persisted via periodic checkpoints.
    ///
    /// # Arguments
    ///
    /// * `key` - Unique identifier for the vector
    /// * `vector` - The vector data (must match configured dimensions)
    ///
    /// # Errors
    ///
    /// * `SynaError::DimensionMismatch` - If vector length doesn't match
    /// * `SynaError::Io` - If the mmap region is full
    pub fn insert(&mut self, key: &str, vector: &[f32]) -> Result<()> {
        // Validate dimensions
        if vector.len() != self.config.dimensions as usize {
            return Err(SynaError::DimensionMismatch {
                expected: self.config.dimensions,
                got: vector.len() as u16,
            });
        }

        // Check if key already exists
        if self.keys.contains(key) {
            return Ok(()); // Silently skip duplicates
        }

        let key_bytes = key.as_bytes();
        let key_len = key_bytes.len();
        let entry_size = 2 + key_len + (vector.len() * 4);

        // Get current write offset
        let offset = self.write_offset.load(Ordering::SeqCst) as usize;

        // Check capacity
        if offset + entry_size > self.mmap.len() {
            self.grow_file(entry_size)?;
        }

        // Write entry directly to mmap (NO SYSCALL!)
        // Key length (2 bytes)
        self.mmap[offset..offset + 2].copy_from_slice(&(key_len as u16).to_le_bytes());
        // Key bytes
        self.mmap[offset + 2..offset + 2 + key_len].copy_from_slice(key_bytes);
        // Vector data (f32 array, safe byte-level write)
        let vector_start = offset + 2 + key_len;
        for (i, &val) in vector.iter().enumerate() {
            let byte_offset = vector_start + i * 4;
            self.mmap[byte_offset..byte_offset + 4].copy_from_slice(&val.to_le_bytes());
        }

        // Update write offset
        self.write_offset
            .store((offset + entry_size) as u64, Ordering::SeqCst);
        self.vector_count.fetch_add(1, Ordering::SeqCst);

        // Update in-memory index
        self.keys.insert(key.to_string());
        self.key_to_offset.insert(key.to_string(), offset as u64);

        // Update HNSW index if present
        if self.hnsw_index.is_some() {
            self.insert_to_hnsw_incremental(key, vector);
            self.index_dirty = true;
        } else if self.config.index_threshold > 0 && self.keys.len() >= self.config.index_threshold
        {
            self.build_index()?;
        }

        // Checkpoint if needed
        if self.index_dirty
            && self.checkpoint_interval.as_secs() > 0
            && self.last_checkpoint.elapsed() >= self.checkpoint_interval
        {
            self.checkpoint()?;
        }

        Ok(())
    }

    /// Inserts multiple vectors in a batch (maximum throughput).
    ///
    /// This is the fastest way to load vectors, achieving 500K-1M vectors/sec.
    ///
    /// # Arguments
    ///
    /// * `keys` - Slice of key strings
    /// * `vectors` - Slice of vector slices
    ///
    /// # Returns
    ///
    /// Number of vectors successfully inserted.
    pub fn insert_batch(&mut self, keys: &[&str], vectors: &[&[f32]]) -> Result<usize> {
        if keys.len() != vectors.len() {
            return Err(SynaError::ShapeMismatch {
                data_size: vectors.len(),
                expected_size: keys.len(),
            });
        }

        let dims = self.config.dimensions as usize;
        let mut inserted = 0;
        let mut offset = self.write_offset.load(Ordering::SeqCst) as usize;

        for (key, vector) in keys.iter().zip(vectors.iter()) {
            // Validate dimensions
            if vector.len() != dims {
                return Err(SynaError::DimensionMismatch {
                    expected: self.config.dimensions,
                    got: vector.len() as u16,
                });
            }

            // Skip duplicates
            if self.keys.contains(*key) {
                continue;
            }

            let key_bytes = key.as_bytes();
            let key_len = key_bytes.len();
            let entry_size = 2 + key_len + (dims * 4);

            // Check capacity
            if offset + entry_size > self.mmap.len() {
                // Update offset before growing
                self.write_offset.store(offset as u64, Ordering::SeqCst);
                self.grow_file(entry_size)?;
            }

            // Write entry directly to mmap
            self.mmap[offset..offset + 2].copy_from_slice(&(key_len as u16).to_le_bytes());
            self.mmap[offset + 2..offset + 2 + key_len].copy_from_slice(key_bytes);

            let vector_start = offset + 2 + key_len;
            for (i, &val) in vector.iter().enumerate() {
                let byte_offset = vector_start + i * 4;
                self.mmap[byte_offset..byte_offset + 4].copy_from_slice(&val.to_le_bytes());
            }

            // Update in-memory index
            self.keys.insert(key.to_string());
            self.key_to_offset.insert(key.to_string(), offset as u64);

            offset += entry_size;
            inserted += 1;
        }

        // Update counters
        self.write_offset.store(offset as u64, Ordering::SeqCst);
        self.vector_count
            .fetch_add(inserted as u64, Ordering::SeqCst);

        Ok(inserted)
    }

    /// Grows the mmap file to accommodate more data.
    fn grow_file(&mut self, additional: usize) -> Result<()> {
        let current_size = self.mmap.len();
        let required = self.write_offset.load(Ordering::SeqCst) as usize + additional;

        // Double the size or add required space, whichever is larger
        let new_size = (current_size * 2).max(required + 1024 * 1024);

        // Flush current mmap
        self.mmap.flush()?;

        // Resize file
        self.file.set_len(new_size as u64)?;

        // Remap
        self.mmap = unsafe { MmapMut::map_mut(&self.file)? };

        Ok(())
    }

    /// Retrieves a vector by key.
    pub fn get(&self, key: &str) -> Result<Option<Vec<f32>>> {
        let offset = match self.key_to_offset.get(key) {
            Some(&o) => o as usize,
            None => return Ok(None),
        };

        let dims = self.config.dimensions as usize;

        // Read key length
        let key_len = u16::from_le_bytes([self.mmap[offset], self.mmap[offset + 1]]) as usize;

        // Read vector
        let vector_start = offset + 2 + key_len;
        let vector_bytes = &self.mmap[vector_start..vector_start + dims * 4];

        // Convert bytes to f32 slice
        let vector: Vec<f32> = vector_bytes
            .chunks_exact(4)
            .map(|chunk| f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
            .collect();

        Ok(Some(vector))
    }

    /// Gets a vector as a slice reference (zero-copy).
    ///
    /// # Safety
    ///
    /// The returned slice is valid only as long as the MmapVectorStore exists
    /// and no grow operations occur.
    pub fn get_slice(&self, key: &str) -> Option<&[f32]> {
        let offset = *self.key_to_offset.get(key)? as usize;
        let dims = self.config.dimensions as usize;

        let key_len = u16::from_le_bytes(self.mmap[offset..offset + 2].try_into().ok()?) as usize;

        let vector_start = offset + 2 + key_len;
        let vector_bytes = &self.mmap[vector_start..vector_start + dims * 4];

        // Safety: align_to checks alignment at runtime. If the mmap region
        // isn't 4-byte aligned at this offset, the prefix/suffix will be
        // non-empty and we fall back to None. In practice, our write layout
        // ensures f32-aligned vector data.
        let (prefix, floats, suffix) = unsafe { vector_bytes.align_to::<f32>() };
        if prefix.is_empty() && suffix.is_empty() && floats.len() == dims {
            Some(floats)
        } else {
            // Misaligned — caller should use get() which copies
            None
        }
    }

    /// Searches for the k nearest neighbors.
    pub fn search(&self, query: &[f32], k: usize) -> Result<Vec<MmapSearchResult>> {
        if query.len() != self.config.dimensions as usize {
            return Err(SynaError::DimensionMismatch {
                expected: self.config.dimensions,
                got: query.len() as u16,
            });
        }

        // Use HNSW if available and above threshold
        if let Some(ref index) = self.hnsw_index {
            if self.keys.len() >= self.config.index_threshold {
                return self.search_hnsw(index, query, k);
            }
        }

        // Fall back to brute force
        self.search_brute_force(query, k)
    }

    /// Brute-force search (O(N)).
    fn search_brute_force(&self, query: &[f32], k: usize) -> Result<Vec<MmapSearchResult>> {
        let mut results: Vec<MmapSearchResult> = Vec::with_capacity(self.keys.len());

        for key in &self.keys {
            if let Some(vector) = self.get_slice(key) {
                let score = self.config.metric.distance(query, vector);
                results.push(MmapSearchResult {
                    key: key.clone(),
                    score,
                    vector: vector.to_vec(),
                });
            }
        }

        // Sort by score (ascending)
        results.sort_by(|a, b| a.score.total_cmp(&b.score));
        results.truncate(k);

        Ok(results)
    }

    /// HNSW search (O(log N)).
    fn search_hnsw(
        &self,
        index: &HnswIndex,
        query: &[f32],
        k: usize,
    ) -> Result<Vec<MmapSearchResult>> {
        let hnsw_results = index.search(query, k);

        let mut results = Vec::with_capacity(hnsw_results.len());
        for (key, score) in hnsw_results {
            if let Some(vector) = self.get_slice(&key) {
                results.push(MmapSearchResult {
                    key,
                    score,
                    vector: vector.to_vec(),
                });
            }
        }

        Ok(results)
    }

    /// Builds the HNSW index from all stored vectors.
    pub fn build_index(&mut self) -> Result<()> {
        let mut index = HnswIndex::new(
            self.config.dimensions,
            self.config.metric,
            self.config.hnsw_config.clone(),
        );

        for key in &self.keys {
            if let Some(vector) = self.get_slice(key) {
                self.add_node_to_index(&mut index, key, vector);
            }
        }

        // Save index
        let hnsw_path = self.hnsw_index_path();
        index.save(&hnsw_path)?;

        self.hnsw_index = Some(index);
        self.index_dirty = false;
        self.last_checkpoint = Instant::now();

        Ok(())
    }

    /// Adds a node to the HNSW index during build using search-based neighbor finding.
    /// This uses brute-force neighbor finding during construction for correctness.
    fn add_node_to_index(&self, index: &mut HnswIndex, key: &str, vector: &[f32]) {
        if index.key_to_id.contains_key(key) {
            return;
        }

        let level = index.random_level();
        let node = HnswNode::new(key.to_string(), vector.to_vec(), level);
        let node_id = index.nodes.len();

        index.nodes.push(node);
        index.key_to_id.insert(key.to_string(), node_id);

        // Update entry point if this is the first node or has higher level
        let current_max_level = index.max_level();
        if index.entry_point.is_none() || level > current_max_level {
            index.entry_point = Some(node_id);
            index.set_max_level(level);
        }

        // Connect to existing nodes at each level this node exists at
        if node_id > 0 {
            let m = index.config().m;
            let m_max = index.config().m_max;

            for l in 0..=level {
                let max_neighbors = if l == 0 { m } else { m_max };
                let mut neighbors = Vec::new();

                // Find closest nodes at this level using brute-force (correct for construction)
                let mut distances: Vec<(usize, f32)> = index
                    .nodes
                    .iter()
                    .enumerate()
                    .filter(|(id, n)| *id != node_id && n.neighbors.len() > l)
                    .map(|(id, n)| (id, index.metric().distance(vector, &n.vector)))
                    .collect();
                distances.sort_by(|a, b| a.1.total_cmp(&b.1));

                for (neighbor_id, dist) in distances.into_iter().take(max_neighbors) {
                    neighbors.push((neighbor_id, dist));

                    // Add bidirectional connection
                    if l < index.nodes[neighbor_id].neighbors.len() {
                        index.nodes[neighbor_id].neighbors[l].push((node_id, dist));

                        // Prune neighbor's connections if exceeding limit
                        if index.nodes[neighbor_id].neighbors[l].len() > max_neighbors {
                            index.nodes[neighbor_id].neighbors[l]
                                .sort_by(|a, b| a.1.total_cmp(&b.1));
                            index.nodes[neighbor_id].neighbors[l].truncate(max_neighbors);
                        }
                    }
                }

                if l < index.nodes[node_id].neighbors.len() {
                    index.nodes[node_id].neighbors[l] = neighbors;
                }
            }
        }
    }

    /// Inserts a vector into the HNSW index incrementally.
    fn insert_to_hnsw_incremental(&mut self, key: &str, vector: &[f32]) {
        let index = match self.hnsw_index.as_mut() {
            Some(idx) => idx,
            None => return,
        };

        if index.key_to_id.contains_key(key) {
            return;
        }

        let level = index.random_level();
        let node = HnswNode::new(key.to_string(), vector.to_vec(), level);
        let node_id = index.nodes.len();

        index.nodes.push(node);
        index.key_to_id.insert(key.to_string(), node_id);

        // Update entry point if this is the first node or has higher level
        let current_max_level = index.max_level();
        if index.entry_point.is_none() || level > current_max_level {
            index.entry_point = Some(node_id);
            index.set_max_level(level);
        }

        if node_id == 0 {
            return;
        }

        let m = index.config().m;
        let m_max = index.config().m_max;
        let ef_construction = index.config().ef_construction;

        let mut ep = index.entry_point.unwrap_or(0);

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

        // Connect at each level from min(level, max_level) down to 0
        let start_level = level.min(index.max_level());
        for l in (0..=start_level).rev() {
            let candidates = index.search_layer(vector, ep, ef_construction, l);
            let max_neighbors = if l == 0 { m } else { m_max };
            let neighbors: Vec<(usize, f32)> = candidates.into_iter().take(max_neighbors).collect();

            if !neighbors.is_empty() {
                ep = neighbors[0].0;
            }

            if l < index.nodes[node_id].neighbors.len() {
                index.nodes[node_id].neighbors[l] = neighbors.clone();
            }

            for (neighbor_id, dist) in neighbors {
                if l < index.nodes[neighbor_id].neighbors.len() {
                    index.nodes[neighbor_id].neighbors[l].push((node_id, dist));

                    if index.nodes[neighbor_id].neighbors[l].len() > max_neighbors {
                        index.nodes[neighbor_id].neighbors[l].sort_by(|a, b| a.1.total_cmp(&b.1));
                        index.nodes[neighbor_id].neighbors[l].truncate(max_neighbors);
                    }
                }
            }
        }
    }

    /// Checkpoints the store to disk (flushes mmap and saves index).
    pub fn checkpoint(&mut self) -> Result<()> {
        // Update header with current counts
        let count = self.vector_count.load(Ordering::SeqCst);
        let offset = self.write_offset.load(Ordering::SeqCst);

        self.mmap[16..24].copy_from_slice(&count.to_le_bytes());
        self.mmap[24..32].copy_from_slice(&offset.to_le_bytes());

        // Flush mmap to disk
        self.mmap.flush()?;

        // Save HNSW index if dirty
        if self.index_dirty {
            if let Some(ref index) = self.hnsw_index {
                let hnsw_path = self.hnsw_index_path();
                index.save(&hnsw_path)?;
            }
            self.index_dirty = false;
        }

        self.last_checkpoint = Instant::now();
        Ok(())
    }

    /// Flushes any pending changes to disk.
    pub fn flush(&mut self) -> Result<()> {
        self.checkpoint()
    }

    /// Returns the number of vectors stored.
    pub fn len(&self) -> usize {
        self.keys.len()
    }

    /// Returns true if the store is empty.
    pub fn is_empty(&self) -> bool {
        self.keys.is_empty()
    }

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

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

    /// Returns whether an HNSW index is built.
    pub fn has_index(&self) -> bool {
        self.hnsw_index.is_some()
    }

    /// Returns whether the index has unsaved changes.
    pub fn is_dirty(&self) -> bool {
        self.index_dirty
    }

    /// Returns all keys in the store.
    pub fn keys(&self) -> Vec<String> {
        self.keys.iter().cloned().collect()
    }
}

impl Drop for MmapVectorStore {
    fn drop(&mut self) {
        if let Err(e) = self.checkpoint() {
            eprintln!(
                "Warning: Failed to checkpoint MmapVectorStore on drop: {}",
                e
            );
        }
    }
}

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

    #[test]
    fn test_mmap_vector_store_basic() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mmap");

        let config = MmapVectorConfig {
            dimensions: 128,
            initial_capacity: 1000,
            ..Default::default()
        };

        let mut store = MmapVectorStore::new(&path, config).unwrap();

        // Insert a vector
        let vec1: Vec<f32> = (0..128).map(|i| i as f32 * 0.01).collect();
        store.insert("v1", &vec1).unwrap();

        // Retrieve it
        let retrieved = store.get("v1").unwrap().unwrap();
        assert_eq!(retrieved.len(), 128);
        assert!((retrieved[0] - 0.0).abs() < 0.001);
        assert!((retrieved[1] - 0.01).abs() < 0.001);

        assert_eq!(store.len(), 1);
    }

    #[test]
    fn test_mmap_vector_store_batch_insert() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mmap");

        let config = MmapVectorConfig {
            dimensions: 64,
            initial_capacity: 10000,
            ..Default::default()
        };

        let mut store = MmapVectorStore::new(&path, config).unwrap();

        // Create batch data
        let keys: Vec<String> = (0..100).map(|i| format!("v{}", i)).collect();
        let key_refs: Vec<&str> = keys.iter().map(|s| s.as_str()).collect();
        let vectors: Vec<Vec<f32>> = (0..100)
            .map(|i| (0..64).map(|j| (i * 64 + j) as f32 * 0.001).collect())
            .collect();
        let vec_refs: Vec<&[f32]> = vectors.iter().map(|v| v.as_slice()).collect();

        // Batch insert
        let inserted = store.insert_batch(&key_refs, &vec_refs).unwrap();
        assert_eq!(inserted, 100);
        assert_eq!(store.len(), 100);
    }

    #[test]
    fn test_mmap_vector_store_search() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mmap");

        let config = MmapVectorConfig {
            dimensions: 64,
            initial_capacity: 1000,
            index_threshold: 0, // Disable auto-index
            metric: DistanceMetric::Euclidean,
            ..Default::default()
        };

        let mut store = MmapVectorStore::new(&path, config).unwrap();

        // Insert vectors
        for i in 0..10 {
            let vec: Vec<f32> = (0..64).map(|j| (i * 64 + j) as f32 * 0.001).collect();
            store.insert(&format!("v{}", i), &vec).unwrap();
        }

        // Search (brute force)
        let query: Vec<f32> = (0..64).map(|j| j as f32 * 0.001).collect();
        let results = store.search(&query, 3).unwrap();

        assert_eq!(results.len(), 3);
        assert_eq!(results[0].key, "v0");
        assert!(results[0].score < 0.001);
    }

    #[test]
    fn test_mmap_vector_store_persistence() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mmap");

        // Create and populate store
        {
            let config = MmapVectorConfig {
                dimensions: 64,
                initial_capacity: 1000,
                ..Default::default()
            };

            let mut store = MmapVectorStore::new(&path, config).unwrap();

            for i in 0..10 {
                let vec: Vec<f32> = (0..64).map(|j| (i * 64 + j) as f32 * 0.001).collect();
                store.insert(&format!("v{}", i), &vec).unwrap();
            }

            store.flush().unwrap();
        }

        // Reopen and verify
        {
            let config = MmapVectorConfig {
                dimensions: 64,
                initial_capacity: 1000,
                ..Default::default()
            };

            let store = MmapVectorStore::new(&path, config).unwrap();
            assert_eq!(store.len(), 10);

            let vec = store.get("v0").unwrap().unwrap();
            assert_eq!(vec.len(), 64);
        }
    }

    #[test]
    fn test_mmap_vector_store_dimension_validation() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mmap");

        // Invalid dimensions (too small)
        let config = MmapVectorConfig {
            dimensions: 32,
            ..Default::default()
        };
        assert!(MmapVectorStore::new(&path, config).is_err());

        // Valid dimensions
        let config = MmapVectorConfig {
            dimensions: 128,
            ..Default::default()
        };
        let mut store = MmapVectorStore::new(&path, config).unwrap();

        // Wrong vector size
        let wrong_vec = vec![0.1f32; 64];
        assert!(store.insert("v1", &wrong_vec).is_err());
    }

    #[test]
    fn test_mmap_vector_store_hnsw_index() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mmap");

        let config = MmapVectorConfig {
            dimensions: 64,
            initial_capacity: 1000,
            index_threshold: 5,
            metric: DistanceMetric::Euclidean,
            ..Default::default()
        };

        let mut store = MmapVectorStore::new(&path, config).unwrap();

        // Insert vectors (triggers auto-build at 5)
        for i in 0..10 {
            let vec: Vec<f32> = (0..64).map(|j| (i * 64 + j) as f32 * 0.001).collect();
            store.insert(&format!("v{}", i), &vec).unwrap();
        }

        assert!(store.has_index());

        // Search should use HNSW - verify it returns results
        let query: Vec<f32> = (0..64).map(|j| j as f32 * 0.001).collect();
        let results = store.search(&query, 3).unwrap();

        // HNSW is approximate - just verify we get results and they're valid keys
        assert!(!results.is_empty());
        assert!(results[0].key.starts_with("v"));
    }
}