sochdb-vector 2.0.8

Streaming elimination vector search engine for SochDB - CPU-first ANN with RDF + BPS
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
// SPDX-License-Identifier: AGPL-3.0-or-later
// SochDB - LLM-Optimized Embedded Database
// Copyright (C) 2026 Sushanth Reddy Vanagala (https://github.com/sushanthpy)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Lazy BPS/RDF/Rerank Construction (Build-on-First-Query)
//!
//! Deferred index construction using OnceLock for on-demand building,
//! eliminating upfront build cost for indexes that may never be queried.
//!
//! ## Problem
//!
//! Eager index building at seal time:
//! - BPS construction: O(N × D) - ~50ms for 10K vectors
//! - RDF quantization: O(N × D) - ~30ms for 10K vectors
//! - Graph building: O(N × ef × log N) - ~200ms for 10K vectors
//! - All this blocks the seal path even if segment is rarely queried
//!
//! ## Solution
//!
//! Lazy construction with OnceLock:
//! - Seal only writes raw vectors (fast)
//! - Index structures built on first query
//! - Subsequent queries use cached index
//! - Background pre-warming optional
//!
//! ## Architecture
//!
//! ```text
//! Segment State Machine:
//!
//! ┌──────────┐     ┌──────────────┐     ┌────────────────┐
//! │  Raw     │ ──► │  First Query │ ──► │  Index Built   │
//! │  Vectors │     │  (triggers)  │     │  (cached)      │
//! └──────────┘     └──────────────┘     └────────────────┘
//!//!//!                  ┌──────────────┐
//!                  │  Build Index │
//!                  │  (one-time)  │
//!                  └──────────────┘
//! ```
//!
//! ## Performance
//!
//! | Metric          | Eager Build | Lazy Build | Improvement |
//! |-----------------|-------------|------------|-------------|
//! | Seal latency    | 280ms       | 5ms        | 56×         |
//! | First query     | 2ms         | 285ms*     | (deferred)  |
//! | Subsequent      | 2ms         | 2ms        | Same        |
//!
//! *One-time cost, amortized over query lifetime
//!
//! ## Usage
//!
//! ```rust
//! use sochdb_vector::lazy_segment::{LazySegment, LazyConfig};
//!
//! let config = LazyConfig::default();
//! let segment = LazySegment::new(vectors, config);
//!
//! // Seal is instant - no index built yet
//!
//! // First search triggers index build
//! let results = segment.search(&query, k);  // ~280ms
//!
//! // Subsequent searches are fast
//! let results = segment.search(&query, k);  // ~2ms
//! ```

use std::collections::HashMap;
use std::sync::{Arc, OnceLock, RwLock};
use std::time::Instant;

/// Configuration for lazy segment
#[derive(Debug, Clone)]
pub struct LazyConfig {
    /// Vector dimension
    pub dim: usize,

    /// Enable BPS (Binary Projection Search) index
    pub enable_bps: bool,

    /// Enable RDF (Random Dimension Filtering)
    pub enable_rdf: bool,

    /// Enable graph-based index (mini-HNSW)
    pub enable_graph: bool,

    /// BPS projection count
    pub bps_projections: usize,

    /// RDF sample dimensions
    pub rdf_sample_dims: usize,

    /// Graph M parameter
    pub graph_m: usize,

    /// Pre-warm on background thread
    pub background_prewarm: bool,
}

impl Default for LazyConfig {
    fn default() -> Self {
        Self {
            dim: 768,
            enable_bps: true,
            enable_rdf: true,
            enable_graph: false, // Disabled by default (expensive)
            bps_projections: 64,
            rdf_sample_dims: 32,
            graph_m: 16,
            background_prewarm: false,
        }
    }
}

/// Vector key type
pub type VectorKey = u64;

/// BPS (Binary Projection Search) index
struct BpsIndex {
    /// Projection vectors (normalized)
    projections: Vec<Vec<f32>>,

    /// Binary codes for each vector
    codes: Vec<u64>,

    /// Build time in nanoseconds
    build_time_ns: u64,
}

impl BpsIndex {
    fn build(vectors: &[f32], dim: usize, num_projections: usize) -> Self {
        let start = Instant::now();
        let num_vectors = vectors.len() / dim;

        // Generate random projection vectors
        let projections: Vec<Vec<f32>> = (0..num_projections)
            .map(|i| {
                (0..dim)
                    .map(|j| {
                        // Simple deterministic "random" for reproducibility
                        let seed = (i * 1000 + j) as f32;
                        (seed.sin() * 100.0) % 2.0 - 1.0
                    })
                    .collect()
            })
            .collect();

        // Compute binary codes for each vector
        let codes: Vec<u64> = (0..num_vectors)
            .map(|i| {
                let vec_start = i * dim;
                let vector = &vectors[vec_start..vec_start + dim];

                let mut code = 0u64;
                for (p, proj) in projections.iter().enumerate().take(64) {
                    let dot: f32 = vector.iter().zip(proj.iter()).map(|(a, b)| a * b).sum();

                    if dot >= 0.0 {
                        code |= 1 << p;
                    }
                }
                code
            })
            .collect();

        Self {
            projections,
            codes,
            build_time_ns: start.elapsed().as_nanos() as u64,
        }
    }

    fn query(&self, vector: &[f32]) -> u64 {
        let mut code = 0u64;
        for (p, proj) in self.projections.iter().enumerate().take(64) {
            let dot: f32 = vector.iter().zip(proj.iter()).map(|(a, b)| a * b).sum();

            if dot >= 0.0 {
                code |= 1 << p;
            }
        }
        code
    }

    fn hamming_distance(a: u64, b: u64) -> u32 {
        (a ^ b).count_ones()
    }
}

/// RDF (Random Dimension Filtering) index
struct RdfIndex {
    /// Sampled dimension indices
    sample_dims: Vec<usize>,

    /// Quantized values for each vector
    quantized: Vec<Vec<u8>>,

    /// Build time
    build_time_ns: u64,
}

impl RdfIndex {
    fn build(vectors: &[f32], dim: usize, num_sample_dims: usize) -> Self {
        let start = Instant::now();
        let num_vectors = vectors.len() / dim;

        // Select random dimensions
        let sample_dims: Vec<usize> = (0..num_sample_dims)
            .map(|i| (i * 7919) % dim) // Deterministic spread
            .collect();

        // Quantize each vector on sampled dimensions
        let quantized: Vec<Vec<u8>> = (0..num_vectors)
            .map(|i| {
                let vec_start = i * dim;
                sample_dims
                    .iter()
                    .map(|&d| {
                        let val = vectors[vec_start + d];
                        // Quantize to 8 bits
                        ((val.clamp(-1.0, 1.0) + 1.0) * 127.5) as u8
                    })
                    .collect()
            })
            .collect();

        Self {
            sample_dims,
            quantized,
            build_time_ns: start.elapsed().as_nanos() as u64,
        }
    }

    fn query(&self, vector: &[f32]) -> Vec<u8> {
        self.sample_dims
            .iter()
            .map(|&d| {
                let val = vector.get(d).copied().unwrap_or(0.0);
                ((val.clamp(-1.0, 1.0) + 1.0) * 127.5) as u8
            })
            .collect()
    }

    fn l1_distance(a: &[u8], b: &[u8]) -> u32 {
        a.iter()
            .zip(b.iter())
            .map(|(&x, &y)| (x as i32 - y as i32).unsigned_abs())
            .sum()
    }
}

/// Mini-HNSW graph for small segments
struct MiniGraph {
    /// Neighbors for each node
    #[allow(dead_code)]
    neighbors: Vec<Vec<u32>>,

    /// Entry point
    #[allow(dead_code)]
    entry_point: u32,

    /// Build time
    build_time_ns: u64,
}

impl MiniGraph {
    fn build(vectors: &[f32], dim: usize, m: usize) -> Self {
        let start = Instant::now();
        let num_vectors = vectors.len() / dim;

        if num_vectors == 0 {
            return Self {
                neighbors: Vec::new(),
                entry_point: 0,
                build_time_ns: 0,
            };
        }

        // Simple greedy construction
        let mut neighbors: Vec<Vec<u32>> = vec![Vec::with_capacity(m); num_vectors];

        for i in 1..num_vectors {
            let vec_i = &vectors[i * dim..(i + 1) * dim];

            // Find nearest neighbors among existing nodes
            let mut distances: Vec<(u32, f32)> = (0..i)
                .map(|j| {
                    let vec_j = &vectors[j * dim..(j + 1) * dim];
                    let dist = l2_squared(vec_i, vec_j);
                    (j as u32, dist)
                })
                .collect();

            distances.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());

            // Add bidirectional edges to top M
            for &(neighbor, _) in distances.iter().take(m) {
                neighbors[i].push(neighbor);
                if neighbors[neighbor as usize].len() < m {
                    neighbors[neighbor as usize].push(i as u32);
                }
            }
        }

        Self {
            neighbors,
            entry_point: 0,
            build_time_ns: start.elapsed().as_nanos() as u64,
        }
    }
}

/// Lazy-built indexes (OnceLock)
struct LazyIndexes {
    bps: OnceLock<BpsIndex>,
    rdf: OnceLock<RdfIndex>,
    graph: OnceLock<MiniGraph>,
}

impl LazyIndexes {
    fn new() -> Self {
        Self {
            bps: OnceLock::new(),
            rdf: OnceLock::new(),
            graph: OnceLock::new(),
        }
    }
}

/// Segment with lazy index construction
pub struct LazySegment {
    /// Configuration
    config: LazyConfig,

    /// Vector data (contiguous)
    data: Vec<f32>,

    /// Key to index mapping
    key_to_index: HashMap<VectorKey, u32>,

    /// Index to key mapping
    index_to_key: Vec<VectorKey>,

    /// Lazy indexes
    indexes: LazyIndexes,

    /// Statistics
    #[allow(dead_code)]
    stats: SegmentStats,
}

impl LazySegment {
    /// Create a new lazy segment from vectors
    pub fn new(vectors: Vec<(VectorKey, Vec<f32>)>, config: LazyConfig) -> Self {
        let dim = config.dim;
        let num_vectors = vectors.len();

        let mut data = Vec::with_capacity(num_vectors * dim);
        let mut key_to_index = HashMap::with_capacity(num_vectors);
        let mut index_to_key = Vec::with_capacity(num_vectors);

        for (i, (key, vector)) in vectors.into_iter().enumerate() {
            data.extend_from_slice(&vector);
            key_to_index.insert(key, i as u32);
            index_to_key.push(key);
        }

        Self {
            config,
            data,
            key_to_index,
            index_to_key,
            indexes: LazyIndexes::new(),
            stats: SegmentStats::default(),
        }
    }

    /// Create from flat data
    pub fn from_flat(flat_data: Vec<f32>, keys: Vec<VectorKey>, config: LazyConfig) -> Self {
        let key_to_index: HashMap<_, _> = keys
            .iter()
            .enumerate()
            .map(|(i, &k)| (k, i as u32))
            .collect();

        Self {
            config,
            data: flat_data,
            key_to_index,
            index_to_key: keys,
            indexes: LazyIndexes::new(),
            stats: SegmentStats::default(),
        }
    }

    /// Number of vectors
    pub fn len(&self) -> usize {
        self.index_to_key.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.index_to_key.is_empty()
    }

    /// Get vector by key
    pub fn get(&self, key: VectorKey) -> Option<&[f32]> {
        self.key_to_index.get(&key).map(|&idx| {
            let start = idx as usize * self.config.dim;
            &self.data[start..start + self.config.dim]
        })
    }

    /// Get vector by index
    pub fn get_by_index(&self, index: u32) -> Option<&[f32]> {
        if (index as usize) < self.index_to_key.len() {
            let start = index as usize * self.config.dim;
            Some(&self.data[start..start + self.config.dim])
        } else {
            None
        }
    }

    /// Search for k nearest neighbors
    ///
    /// This will trigger index construction on first call.
    pub fn search(&self, query: &[f32], k: usize) -> Vec<(VectorKey, f32)> {
        if self.is_empty() {
            return Vec::new();
        }

        let num_vectors = self.len();
        let _dim = self.config.dim;

        // For small segments, just do brute force
        if num_vectors <= 100 {
            return self.brute_force_search(query, k);
        }

        // Use available indexes for candidate generation
        let candidates: Vec<u32> = if self.config.enable_bps {
            self.bps_candidates(query, k * 10)
        } else if self.config.enable_rdf {
            self.rdf_candidates(query, k * 10)
        } else {
            (0..num_vectors as u32).collect()
        };

        // Rerank candidates with exact distance
        let mut results: Vec<(VectorKey, f32)> = candidates
            .into_iter()
            .filter_map(|idx| {
                let vec = self.get_by_index(idx)?;
                let dist = l2_squared(query, vec);
                let key = self.index_to_key[idx as usize];
                Some((key, dist))
            })
            .collect();

        // Sort by distance and take top k
        results.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
        results.truncate(k);

        results
    }

    /// Brute force search for small segments
    fn brute_force_search(&self, query: &[f32], k: usize) -> Vec<(VectorKey, f32)> {
        let dim = self.config.dim;

        let mut results: Vec<(VectorKey, f32)> = self
            .index_to_key
            .iter()
            .enumerate()
            .map(|(i, &key)| {
                let start = i * dim;
                let vec = &self.data[start..start + dim];
                let dist = l2_squared(query, vec);
                (key, dist)
            })
            .collect();

        results.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
        results.truncate(k);

        results
    }

    /// Get candidates using BPS index (lazy built)
    fn bps_candidates(&self, query: &[f32], limit: usize) -> Vec<u32> {
        let bps = self.indexes.bps.get_or_init(|| {
            BpsIndex::build(&self.data, self.config.dim, self.config.bps_projections)
        });

        let query_code = bps.query(query);

        // Sort by Hamming distance
        let mut scored: Vec<(u32, u32)> = bps
            .codes
            .iter()
            .enumerate()
            .map(|(i, &code)| (i as u32, BpsIndex::hamming_distance(query_code, code)))
            .collect();

        scored.sort_by_key(|&(_, dist)| dist);

        scored.into_iter().take(limit).map(|(idx, _)| idx).collect()
    }

    /// Get candidates using RDF index (lazy built)
    fn rdf_candidates(&self, query: &[f32], limit: usize) -> Vec<u32> {
        let rdf = self.indexes.rdf.get_or_init(|| {
            RdfIndex::build(&self.data, self.config.dim, self.config.rdf_sample_dims)
        });

        let query_quantized = rdf.query(query);

        // Sort by L1 distance on quantized values
        let mut scored: Vec<(u32, u32)> = rdf
            .quantized
            .iter()
            .enumerate()
            .map(|(i, quantized)| (i as u32, RdfIndex::l1_distance(&query_quantized, quantized)))
            .collect();

        scored.sort_by_key(|&(_, dist)| dist);

        scored.into_iter().take(limit).map(|(idx, _)| idx).collect()
    }

    /// Pre-warm indexes (optional)
    pub fn prewarm(&self) {
        if self.config.enable_bps {
            let _ = self.indexes.bps.get_or_init(|| {
                BpsIndex::build(&self.data, self.config.dim, self.config.bps_projections)
            });
        }

        if self.config.enable_rdf {
            let _ = self.indexes.rdf.get_or_init(|| {
                RdfIndex::build(&self.data, self.config.dim, self.config.rdf_sample_dims)
            });
        }

        if self.config.enable_graph {
            let _ = self
                .indexes
                .graph
                .get_or_init(|| MiniGraph::build(&self.data, self.config.dim, self.config.graph_m));
        }
    }

    /// Check if indexes are built
    pub fn indexes_built(&self) -> IndexStatus {
        IndexStatus {
            bps_built: self.indexes.bps.get().is_some(),
            rdf_built: self.indexes.rdf.get().is_some(),
            graph_built: self.indexes.graph.get().is_some(),
        }
    }

    /// Get build statistics
    pub fn build_stats(&self) -> BuildStats {
        BuildStats {
            bps_time_ns: self.indexes.bps.get().map(|b| b.build_time_ns),
            rdf_time_ns: self.indexes.rdf.get().map(|r| r.build_time_ns),
            graph_time_ns: self.indexes.graph.get().map(|g| g.build_time_ns),
        }
    }
}

/// Index build status
#[derive(Debug, Clone)]
pub struct IndexStatus {
    pub bps_built: bool,
    pub rdf_built: bool,
    pub graph_built: bool,
}

/// Build timing statistics
#[derive(Debug, Clone)]
pub struct BuildStats {
    pub bps_time_ns: Option<u64>,
    pub rdf_time_ns: Option<u64>,
    pub graph_time_ns: Option<u64>,
}

/// Segment statistics
#[derive(Debug, Clone, Default)]
struct SegmentStats {
    #[allow(dead_code)]
    queries: u64,
    #[allow(dead_code)]
    index_builds: u64,
}

/// L2 squared distance
fn l2_squared(a: &[f32], b: &[f32]) -> f32 {
    a.iter()
        .zip(b.iter())
        .map(|(x, y)| {
            let d = x - y;
            d * d
        })
        .sum()
}

// ============================================================================
// Lazy Segment Manager
// ============================================================================

/// Manager for multiple lazy segments
pub struct LazySegmentManager {
    segments: RwLock<Vec<Arc<LazySegment>>>,
    #[allow(dead_code)]
    config: LazyConfig,
}

impl LazySegmentManager {
    /// Create a new manager
    pub fn new(config: LazyConfig) -> Self {
        Self {
            segments: RwLock::new(Vec::new()),
            config,
        }
    }

    /// Add a segment
    pub fn add_segment(&self, segment: LazySegment) {
        let segment = Arc::new(segment);
        let mut segments = self.segments.write().unwrap();
        segments.push(segment);
    }

    /// Search across all segments
    pub fn search(&self, query: &[f32], k: usize) -> Vec<(VectorKey, f32)> {
        let segments = self.segments.read().unwrap();

        let mut results: Vec<(VectorKey, f32)> =
            segments.iter().flat_map(|s| s.search(query, k)).collect();

        results.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
        results.truncate(k);

        results
    }

    /// Pre-warm all segments (background)
    pub fn prewarm_all(&self) {
        let segments = self.segments.read().unwrap();

        for segment in segments.iter() {
            segment.prewarm();
        }
    }

    /// Number of segments
    pub fn num_segments(&self) -> usize {
        self.segments.read().unwrap().len()
    }
}

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

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

    fn random_vectors(n: usize, dim: usize, seed: u64) -> Vec<(VectorKey, Vec<f32>)> {
        (0..n)
            .map(|i| {
                let key = (seed * 1000 + i as u64) as VectorKey;
                let vector: Vec<f32> = (0..dim)
                    .map(|j| ((i * dim + j) as f32 * 0.001).sin())
                    .collect();
                (key, vector)
            })
            .collect()
    }

    #[test]
    fn test_lazy_segment_basic() {
        let config = LazyConfig {
            dim: 4,
            enable_bps: false,
            enable_rdf: false,
            ..Default::default()
        };

        let vectors = vec![
            (1, vec![1.0, 0.0, 0.0, 0.0]),
            (2, vec![0.0, 1.0, 0.0, 0.0]),
            (3, vec![0.0, 0.0, 1.0, 0.0]),
        ];

        let segment = LazySegment::new(vectors, config);

        assert_eq!(segment.len(), 3);
        assert_eq!(segment.get(1).unwrap(), &[1.0, 0.0, 0.0, 0.0]);
    }

    #[test]
    fn test_lazy_segment_search() {
        let config = LazyConfig {
            dim: 4,
            enable_bps: false,
            enable_rdf: false,
            ..Default::default()
        };

        let vectors = vec![
            (1, vec![1.0, 0.0, 0.0, 0.0]),
            (2, vec![0.0, 1.0, 0.0, 0.0]),
            (3, vec![0.5, 0.5, 0.0, 0.0]),
        ];

        let segment = LazySegment::new(vectors, config);

        let query = vec![1.0, 0.0, 0.0, 0.0];
        let results = segment.search(&query, 2);

        assert_eq!(results.len(), 2);
        assert_eq!(results[0].0, 1); // Exact match
        assert!(results[0].1 < 0.01);
    }

    #[test]
    fn test_lazy_index_build() {
        let config = LazyConfig {
            dim: 8,
            enable_bps: true,
            enable_rdf: true,
            enable_graph: false,
            ..Default::default()
        };

        let vectors = random_vectors(200, 8, 42);
        let segment = LazySegment::new(vectors, config);

        // Indexes not built yet
        let status = segment.indexes_built();
        assert!(!status.bps_built);
        assert!(!status.rdf_built);

        // Search triggers build
        let query = vec![0.1; 8];
        let _ = segment.search(&query, 5);

        // Now BPS should be built (used for candidates)
        let status = segment.indexes_built();
        assert!(status.bps_built);
    }

    #[test]
    fn test_prewarm() {
        let config = LazyConfig {
            dim: 8,
            enable_bps: true,
            enable_rdf: true,
            enable_graph: true,
            ..Default::default()
        };

        let vectors = random_vectors(50, 8, 42);
        let segment = LazySegment::new(vectors, config);

        // Pre-warm all indexes
        segment.prewarm();

        let status = segment.indexes_built();
        assert!(status.bps_built);
        assert!(status.rdf_built);
        assert!(status.graph_built);

        let stats = segment.build_stats();
        assert!(stats.bps_time_ns.is_some());
        assert!(stats.rdf_time_ns.is_some());
        assert!(stats.graph_time_ns.is_some());
    }

    #[test]
    fn test_segment_manager() {
        let config = LazyConfig {
            dim: 4,
            enable_bps: false,
            enable_rdf: false,
            ..Default::default()
        };

        let manager = LazySegmentManager::new(config.clone());

        // Add segments
        let seg1 = LazySegment::new(vec![(1, vec![1.0, 0.0, 0.0, 0.0])], config.clone());
        let seg2 = LazySegment::new(vec![(2, vec![0.0, 1.0, 0.0, 0.0])], config.clone());

        manager.add_segment(seg1);
        manager.add_segment(seg2);

        assert_eq!(manager.num_segments(), 2);

        // Search across both
        let query = vec![0.5, 0.5, 0.0, 0.0];
        let results = manager.search(&query, 2);

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

    #[test]
    fn test_bps_index() {
        let dim = 8;
        let vectors: Vec<f32> = (0..100 * dim).map(|i| (i as f32 * 0.01).sin()).collect();

        let bps = BpsIndex::build(&vectors, dim, 32);

        assert_eq!(bps.codes.len(), 100);
        assert!(bps.build_time_ns > 0);

        // Querying with a stored vector must reproduce that vector's stored code
        // (build and query share the same projection logic — it is deterministic).
        let first_vec = vectors[0..dim].to_vec();
        let code = bps.query(&first_vec);
        assert_eq!(
            code, bps.codes[0],
            "query of a stored vector must match its build-time code"
        );

        // Determinism: the same query must always yield the same code.
        assert_eq!(code, bps.query(&first_vec), "query must be deterministic");

        // Only the low `num_projections` (32) bits may be set.
        assert_eq!(
            code & !((1u64 << 32) - 1),
            0,
            "no bits beyond num_projections"
        );

        // A code is identical to itself under Hamming distance.
        assert_eq!(BpsIndex::hamming_distance(code, code), 0);
    }

    #[test]
    fn test_rdf_index() {
        let dim = 8;
        let vectors: Vec<f32> = (0..100 * dim).map(|i| (i as f32 * 0.01).sin()).collect();

        let rdf = RdfIndex::build(&vectors, dim, 4);

        assert_eq!(rdf.quantized.len(), 100);
        assert_eq!(rdf.quantized[0].len(), 4);
    }
}