edgevec 0.9.0

High-performance embedded vector database for Browser, Node, and Edge
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
//! Flat (brute-force) index for binary vectors.
//!
//! This module provides a simple flat index optimized for binary vectors.
//! Unlike HNSW, it uses O(1) insert and O(n) search, which is faster for
//! small-to-medium datasets (< 100K vectors) due to the extremely fast
//! SIMD Hamming distance calculation.
//!
//! # Performance Characteristics
//!
//! | Operation | Complexity | Time (10K vectors) |
//! |-----------|------------|-------------------|
//! | Insert    | O(1)       | ~1 μs             |
//! | Search    | O(n)       | ~1ms (SIMD)       |
//!
//! # When to Use
//!
//! - Insert-heavy workloads (semantic caching)
//! - Datasets < 100K vectors
//! - When 100% recall (exact search) is required
//! - When insert latency is critical

use crate::hnsw::VectorId;
use crate::metric::{Hamming, Metric};
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Errors that can occur when using `BinaryFlatIndex`.
#[derive(Debug, Clone, Error)]
pub enum BinaryFlatIndexError {
    /// Dimensions must be divisible by 8.
    #[error("dimensions must be divisible by 8, got {0}")]
    InvalidDimensions(usize),

    /// Vector length doesn't match expected bytes.
    #[error("vector length {actual} doesn't match expected {expected}")]
    DimensionMismatch {
        /// Expected number of bytes.
        expected: usize,
        /// Actual number of bytes provided.
        actual: usize,
    },

    /// Capacity overflow when allocating storage.
    #[error("capacity overflow: {0} * {1} exceeds usize::MAX")]
    CapacityOverflow(usize, usize),
}

/// Search result from binary flat index.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct BinaryFlatSearchResult {
    /// Vector ID.
    pub id: VectorId,
    /// Hamming distance (lower is more similar).
    pub distance: f32,
}

/// Threshold ratio for choosing partial sort vs full sort in search.
///
/// When `k < count / PARTIAL_SORT_THRESHOLD`, we use `select_nth_unstable`
/// followed by sorting only the top k elements. This is more efficient for
/// small k values relative to the total count.
///
/// Value of 10 means: use partial sort when k is less than 10% of total vectors.
const PARTIAL_SORT_THRESHOLD: usize = 10;

/// Size of serialization header in bytes (dimensions u32 + count u32).
const SERIALIZATION_HEADER_SIZE: usize = 8;

/// A flat (brute-force) index for binary vectors.
///
/// Stores vectors in a contiguous array for cache-friendly linear scan.
/// Insert is O(1), search is O(n) with SIMD-accelerated Hamming distance.
///
/// # ID Convention
///
/// `BinaryFlatIndex` follows the `VectorStorage` convention and uses
/// **1-based IDs** where `0` is reserved as `INVALID_ID`. The first inserted
/// vector receives `VectorId(1)`, the second `VectorId(2)`, and so on.
/// This differs from [`FlatIndex`](crate::index::FlatIndex) which uses
/// 0-based direct indexing. The 1-based convention allows `VectorId(0)` to
/// serve as a sentinel value in graph structures (e.g., HNSW neighbor lists).
#[derive(Debug, Serialize, Deserialize)]
pub struct BinaryFlatIndex {
    /// Contiguous storage for all vectors.
    vectors: Vec<u8>,
    /// Number of bits per vector.
    dimensions: usize,
    /// Number of bytes per vector (dimensions / 8).
    bytes_per_vector: usize,
    /// Number of vectors stored.
    count: usize,
}

impl BinaryFlatIndex {
    /// Create a new binary flat index.
    ///
    /// # Arguments
    ///
    /// * `dimensions` - Number of bits per vector (must be divisible by 8)
    ///
    /// # Errors
    ///
    /// Returns [`BinaryFlatIndexError::InvalidDimensions`] if dimensions is not divisible by 8.
    #[must_use = "constructors return a Result that should be used"]
    pub fn new(dimensions: usize) -> Result<Self, BinaryFlatIndexError> {
        if dimensions % 8 != 0 {
            return Err(BinaryFlatIndexError::InvalidDimensions(dimensions));
        }
        Ok(Self {
            vectors: Vec::new(),
            dimensions,
            bytes_per_vector: dimensions / 8,
            count: 0,
        })
    }

    /// Create a new binary flat index with pre-allocated capacity.
    ///
    /// # Arguments
    ///
    /// * `dimensions` - Number of bits per vector (must be divisible by 8)
    /// * `capacity` - Number of vectors to pre-allocate space for
    ///
    /// # Errors
    ///
    /// Returns [`BinaryFlatIndexError::InvalidDimensions`] if dimensions is not divisible by 8.
    /// Returns [`BinaryFlatIndexError::CapacityOverflow`] if capacity * bytes_per_vector overflows.
    #[must_use = "constructors return a Result that should be used"]
    pub fn with_capacity(dimensions: usize, capacity: usize) -> Result<Self, BinaryFlatIndexError> {
        if dimensions % 8 != 0 {
            return Err(BinaryFlatIndexError::InvalidDimensions(dimensions));
        }
        let bytes_per_vector = dimensions / 8;
        let total_bytes = capacity.checked_mul(bytes_per_vector).ok_or(
            BinaryFlatIndexError::CapacityOverflow(capacity, bytes_per_vector),
        )?;
        Ok(Self {
            vectors: Vec::with_capacity(total_bytes),
            dimensions,
            bytes_per_vector,
            count: 0,
        })
    }

    /// Insert a binary vector into the index.
    ///
    /// # Arguments
    ///
    /// * `vector` - Binary vector as packed bytes
    ///
    /// # Returns
    ///
    /// The ID of the inserted vector.
    ///
    /// # Errors
    ///
    /// Returns [`BinaryFlatIndexError::DimensionMismatch`] if vector length doesn't match.
    #[inline]
    #[must_use = "insert returns the assigned VectorId"]
    pub fn insert(&mut self, vector: &[u8]) -> Result<VectorId, BinaryFlatIndexError> {
        if vector.len() != self.bytes_per_vector {
            return Err(BinaryFlatIndexError::DimensionMismatch {
                expected: self.bytes_per_vector,
                actual: vector.len(),
            });
        }

        self.vectors.extend_from_slice(vector);
        self.count += 1;
        // Start IDs at 1 to match EdgeVec/VectorStorage convention (0 is reserved sentinel)
        Ok(VectorId(self.count as u64))
    }

    /// Search for the k nearest neighbors to a query vector.
    ///
    /// Uses SIMD-accelerated Hamming distance for fast linear scan.
    ///
    /// # Arguments
    ///
    /// * `query` - Query vector as packed bytes
    /// * `k` - Number of nearest neighbors to return
    ///
    /// # Returns
    ///
    /// Vector of search results sorted by distance (ascending).
    ///
    /// # Errors
    ///
    /// Returns [`BinaryFlatIndexError::DimensionMismatch`] if query length doesn't match.
    #[must_use = "search returns the nearest neighbors"]
    pub fn search(
        &self,
        query: &[u8],
        k: usize,
    ) -> Result<Vec<BinaryFlatSearchResult>, BinaryFlatIndexError> {
        if query.len() != self.bytes_per_vector {
            return Err(BinaryFlatIndexError::DimensionMismatch {
                expected: self.bytes_per_vector,
                actual: query.len(),
            });
        }

        if self.count == 0 || k == 0 {
            return Ok(Vec::new());
        }

        let k = k.min(self.count);

        // For small k, use a simple approach: compute all distances, partial sort
        // For larger datasets, could use a heap for O(n log k) instead of O(n log n)
        let mut results: Vec<(VectorId, f32)> = Vec::with_capacity(self.count);

        for i in 0..self.count {
            let start = i * self.bytes_per_vector;
            let end = start + self.bytes_per_vector;
            let stored = &self.vectors[start..end];
            let dist = Hamming::distance(query, stored);
            // IDs are 1-based (i+1) to match EdgeVec convention
            results.push((VectorId((i + 1) as u64), dist));
        }

        // Partial sort to get top k (more efficient than full sort for small k)
        if k < self.count / PARTIAL_SORT_THRESHOLD {
            // Use partial sort for small k
            results.select_nth_unstable_by(k - 1, |a, b| a.1.total_cmp(&b.1));
            results.truncate(k);
            results.sort_by(|a, b| a.1.total_cmp(&b.1));
        } else {
            // Full sort for large k
            results.sort_by(|a, b| a.1.total_cmp(&b.1));
            results.truncate(k);
        }

        Ok(results
            .into_iter()
            .map(|(id, distance)| BinaryFlatSearchResult { id, distance })
            .collect())
    }

    /// Get a vector by ID.
    ///
    /// # Arguments
    ///
    /// * `id` - Vector ID
    ///
    /// # Returns
    ///
    /// The vector bytes, or None if ID is out of bounds.
    #[must_use]
    #[allow(clippy::cast_possible_truncation)]
    pub fn get(&self, id: VectorId) -> Option<&[u8]> {
        // IDs are 1-based, so subtract 1 to get 0-based index
        // SAFETY: VectorId.0 is u64 but in practice never exceeds usize::MAX
        // on any supported platform (WASM32 or x86_64).
        let idx = (id.0 as usize).checked_sub(1)?;
        if idx >= self.count {
            return None;
        }
        let start = idx * self.bytes_per_vector;
        let end = start + self.bytes_per_vector;
        Some(&self.vectors[start..end])
    }

    /// Get the number of vectors in the index.
    #[must_use]
    #[inline]
    pub fn len(&self) -> usize {
        self.count
    }

    /// Check if the index is empty.
    #[must_use]
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.count == 0
    }

    /// Get the dimensions (bits) per vector.
    #[must_use]
    #[inline]
    pub fn dimensions(&self) -> usize {
        self.dimensions
    }

    /// Get the bytes per vector.
    #[must_use]
    #[inline]
    pub fn bytes_per_vector(&self) -> usize {
        self.bytes_per_vector
    }

    /// Get approximate memory usage in bytes.
    #[must_use]
    pub fn memory_usage(&self) -> usize {
        std::mem::size_of::<Self>() + self.vectors.capacity()
    }

    /// Get the length of the internal vectors buffer.
    #[inline]
    #[must_use]
    pub fn vectors_len(&self) -> usize {
        self.vectors.len()
    }

    /// Estimate the serialized size in bytes.
    ///
    /// Format: header (dimensions u32 + count u32) + vector data.
    #[must_use]
    pub fn serialized_size(&self) -> usize {
        SERIALIZATION_HEADER_SIZE + self.vectors.len()
    }

    /// Clear all vectors from the index.
    pub fn clear(&mut self) {
        self.vectors.clear();
        self.count = 0;
    }

    /// Shrink the internal storage to fit the current number of vectors.
    pub fn shrink_to_fit(&mut self) {
        self.vectors.shrink_to_fit();
    }
}

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

    #[test]
    fn test_new() {
        let index = BinaryFlatIndex::new(1024).unwrap();
        assert_eq!(index.dimensions(), 1024);
        assert_eq!(index.bytes_per_vector(), 128);
        assert_eq!(index.len(), 0);
        assert!(index.is_empty());
    }

    #[test]
    fn test_insert_and_get() {
        let mut index = BinaryFlatIndex::new(64).unwrap(); // 8 bytes per vector
        let v1 = vec![0xFF; 8];
        let v2 = vec![0x00; 8];

        let id1 = index.insert(&v1).unwrap();
        let id2 = index.insert(&v2).unwrap();

        assert_eq!(id1, VectorId(1)); // IDs are 1-based
        assert_eq!(id2, VectorId(2));
        assert_eq!(index.len(), 2);
        assert_eq!(index.get(id1), Some(v1.as_slice()));
        assert_eq!(index.get(id2), Some(v2.as_slice()));
        assert_eq!(index.get(VectorId(99)), None);
    }

    #[test]
    fn test_search_exact_match() {
        let mut index = BinaryFlatIndex::new(64).unwrap();

        // Insert some vectors
        let v1 = vec![0xFF; 8];
        let v2 = vec![0x00; 8];
        let v3 = vec![0xAA; 8];

        index.insert(&v1).unwrap();
        index.insert(&v2).unwrap();
        index.insert(&v3).unwrap();

        // Search for exact match
        let results = index.search(&v2, 1).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].id, VectorId(2)); // v2 is second insert, ID=2
        assert!((results[0].distance - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_search_ordering() {
        let mut index = BinaryFlatIndex::new(64).unwrap();

        // Insert vectors with known distances from query
        let query = vec![0x00; 8]; // All zeros
        let v1 = vec![0xFF; 8]; // 64 bits different
        let v2 = vec![0x0F; 8]; // 32 bits different
        let v3 = vec![0x01; 8]; // 8 bits different

        index.insert(&v1).unwrap(); // id=1, dist=64
        index.insert(&v2).unwrap(); // id=2, dist=32
        index.insert(&v3).unwrap(); // id=3, dist=8

        let results = index.search(&query, 3).unwrap();

        // Should be ordered by distance (IDs are 1-based)
        assert_eq!(results[0].id, VectorId(3)); // Closest
        assert!((results[0].distance - 8.0).abs() < f32::EPSILON);
        assert_eq!(results[1].id, VectorId(2));
        assert!((results[1].distance - 32.0).abs() < f32::EPSILON);
        assert_eq!(results[2].id, VectorId(1)); // Farthest
        assert!((results[2].distance - 64.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_search_k_limit() {
        let mut index = BinaryFlatIndex::new(64).unwrap();

        for i in 0..100 {
            let v: Vec<u8> = (0..8)
                .map(|j| u8::try_from((i + j) % 256).unwrap())
                .collect();
            index.insert(&v).unwrap();
        }

        let query = vec![0x00; 8];
        let results = index.search(&query, 5).unwrap();

        assert_eq!(results.len(), 5);
        // Results should be sorted by distance
        for i in 1..results.len() {
            assert!(results[i - 1].distance <= results[i].distance);
        }
    }

    #[test]
    fn test_empty_search() {
        let index = BinaryFlatIndex::new(64).unwrap();
        let query = vec![0x00; 8];
        let results = index.search(&query, 10).unwrap();
        assert!(results.is_empty());
    }

    #[test]
    fn test_search_k_zero() {
        let mut index = BinaryFlatIndex::new(64).unwrap();
        // Insert enough vectors to trigger the partial sort path (count >= 10)
        for _ in 0..20 {
            index.insert(&[0xFF; 8]).unwrap();
        }
        // k=0 should return empty, not error
        let results = index.search(&[0x00; 8], 0).unwrap();
        assert!(results.is_empty());
    }

    #[test]
    fn test_clear() {
        let mut index = BinaryFlatIndex::new(64).unwrap();
        index.insert(&[0xFF; 8]).unwrap();
        index.insert(&[0x00; 8]).unwrap();

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

        index.clear();

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

    #[test]
    fn test_memory_usage() {
        let mut index = BinaryFlatIndex::with_capacity(1024, 1000).unwrap();
        assert!(index.memory_usage() > 0);

        for _ in 0..100 {
            index.insert(&[0xAA; 128]).unwrap();
        }

        let usage = index.memory_usage();
        // Should be at least 100 * 128 = 12,800 bytes
        assert!(usage >= 12_800);
    }

    #[test]
    fn test_invalid_dimensions() {
        let result = BinaryFlatIndex::new(100); // Not divisible by 8
        assert!(matches!(
            result,
            Err(BinaryFlatIndexError::InvalidDimensions(100))
        ));
    }

    #[test]
    fn test_invalid_vector_length() {
        let mut index = BinaryFlatIndex::new(64).unwrap();
        let result = index.insert(&[0xFF; 16]); // Wrong size
        assert!(matches!(
            result,
            Err(BinaryFlatIndexError::DimensionMismatch {
                expected: 8,
                actual: 16
            })
        ));
    }

    #[test]
    fn test_invalid_query_length() {
        let mut index = BinaryFlatIndex::new(64).unwrap();
        index.insert(&[0xFF; 8]).unwrap();
        let result = index.search(&[0x00; 16], 1); // Wrong query size
        assert!(matches!(
            result,
            Err(BinaryFlatIndexError::DimensionMismatch {
                expected: 8,
                actual: 16
            })
        ));
    }

    // ============= Phase 2 Coverage: vectors_len, serialized_size, shrink_to_fit =============

    #[test]
    fn test_vectors_len() {
        let mut index = BinaryFlatIndex::new(64).unwrap(); // 8 bytes per vector
        assert_eq!(index.vectors_len(), 0);

        index.insert(&[0xAA; 8]).unwrap();
        assert_eq!(index.vectors_len(), 8);
        assert_eq!(index.vectors_len(), index.len() * index.bytes_per_vector());

        index.insert(&[0xBB; 8]).unwrap();
        assert_eq!(index.vectors_len(), 16);
        assert_eq!(index.vectors_len(), index.len() * index.bytes_per_vector());

        index.insert(&[0xCC; 8]).unwrap();
        assert_eq!(index.vectors_len(), 24);
        assert_eq!(index.vectors_len(), index.len() * index.bytes_per_vector());

        // After clear, vectors_len must be 0
        index.clear();
        assert_eq!(index.vectors_len(), 0);
        assert_eq!(index.len(), 0);
        assert_eq!(index.vectors_len(), index.len() * index.bytes_per_vector());
    }

    #[test]
    fn test_serialized_size() {
        let mut index = BinaryFlatIndex::new(64).unwrap(); // 8 bytes per vector
                                                           // Empty index: header only (8 bytes for dimensions u32 + count u32)
        assert_eq!(index.serialized_size(), 8);

        index.insert(&[0xAA; 8]).unwrap();
        // Header (8) + 1 vector (8 bytes) = 16
        assert_eq!(index.serialized_size(), 8 + 8);

        index.insert(&[0xBB; 8]).unwrap();
        assert_eq!(index.serialized_size(), 8 + 16);

        // Verify the formula matches: SERIALIZATION_HEADER_SIZE + vectors.len()
        assert_eq!(
            index.serialized_size(),
            SERIALIZATION_HEADER_SIZE + index.vectors_len()
        );

        // After clear
        index.clear();
        assert_eq!(index.serialized_size(), SERIALIZATION_HEADER_SIZE);
    }

    #[test]
    fn test_shrink_to_fit() {
        // Create with large capacity, insert few vectors, then shrink
        let mut index = BinaryFlatIndex::with_capacity(1024, 10_000).unwrap();
        assert!(index.memory_usage() > 100_000); // Pre-allocated ~1.28MB

        // Insert a small number of vectors
        for _ in 0..10 {
            index.insert(&[0xAA; 128]).unwrap();
        }

        let before_shrink = index.memory_usage();
        index.shrink_to_fit();
        let after_shrink = index.memory_usage();

        // After shrink, memory usage should be <= before (likely much less)
        assert!(after_shrink <= before_shrink);

        // Data must still be intact
        assert_eq!(index.len(), 10);
        assert_eq!(index.vectors_len(), 10 * 128);
        for i in 1..=10 {
            assert_eq!(index.get(VectorId(i as u64)), Some([0xAA; 128].as_slice()));
        }

        // Shrink on empty index should not panic
        let mut empty = BinaryFlatIndex::new(64).unwrap();
        empty.shrink_to_fit();
        assert_eq!(empty.len(), 0);
    }

    #[test]
    fn test_serde_roundtrip() {
        let mut index = BinaryFlatIndex::new(64).unwrap();
        index.insert(&[0xFF; 8]).unwrap();
        index.insert(&[0x00; 8]).unwrap();
        index.insert(&[0xAA; 8]).unwrap();

        // Serialize with serde_json (available as dev-dependency)
        let json = serde_json::to_string(&index).expect("serialize failed");
        let restored: BinaryFlatIndex = serde_json::from_str(&json).expect("deserialize failed");

        assert_eq!(restored.dimensions(), index.dimensions());
        assert_eq!(restored.bytes_per_vector(), index.bytes_per_vector());
        assert_eq!(restored.len(), index.len());
        assert_eq!(restored.vectors_len(), index.vectors_len());

        // Verify all vectors match
        for i in 1..=3u64 {
            assert_eq!(
                restored.get(VectorId(i)),
                index.get(VectorId(i)),
                "vector {} mismatch after roundtrip",
                i
            );
        }
    }
}