foxstash-core 0.5.0

High-performance local RAG library - SIMD-accelerated vector search, HNSW indexing
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
//! HNSW Index with Product Quantization
//!
//! Combines HNSW's fast approximate search with PQ's extreme compression.
//!
//! # Compression
//!
//! For 384-dim vectors with M=8, K=256:
//! - Original: 1.5 GB per million vectors
//! - PQ compressed: 8 MB per million vectors (192x compression!)
//!
//! # Usage
//!
//! ```no_run
//! use foxstash_core::index::hnsw_pq::{PQHNSWIndex, PQHNSWConfig};
//! use foxstash_core::vector::product_quantize::PQConfig;
//! use foxstash_core::Document;
//!
//! fn main() -> Result<(), foxstash_core::RagError> {
//!     let dim = 64;
//!
//!     // Configure PQ: 8 subvectors, 256 centroids each
//!     let pq_config = PQConfig::new(dim, 8, 8).with_seed(42).with_kmeans_iterations(10);
//!
//!     // Train PQ on sample vectors (at least num_centroids vectors needed)
//!     let training_data: Vec<Vec<f32>> = (0..300)
//!         .map(|i| (0..dim).map(|d| ((i * d) as f32).sin()).collect())
//!         .collect();
//!     let mut index = PQHNSWIndex::train(pq_config, &training_data, PQHNSWConfig::default())?;
//!
//!     // Add documents
//!     for i in 0..10 {
//!         let doc = Document {
//!             id: format!("doc{}", i),
//!             content: format!("Content {}", i),
//!             embedding: training_data[i].clone(),
//!             metadata: None,
//!         };
//!         index.add(doc)?;
//!     }
//!
//!     // Search (uses ADC for accurate results)
//!     let query = vec![0.1; dim];
//!     let results = index.search(&query, 5)?;
//!     assert!(results.len() <= 5);
//!     Ok(())
//! }
//! ```

use crate::vector::product_quantize::{PQCode, PQConfig, PQDistanceCache, ProductQuantizer};
use crate::{Document, RagError, Result, SearchResult};
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashSet};

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

/// Configuration for PQ HNSW index
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PQHNSWConfig {
    /// Number of bidirectional links per element (except layer 0)
    pub m: usize,
    /// Number of bidirectional links for layer 0
    pub m0: usize,
    /// Candidate list size during construction
    pub ef_construction: usize,
    /// Candidate list size during search
    pub ef_search: usize,
    /// Level normalization factor
    pub ml: f32,
    /// Whether to use distance cache for faster symmetric search
    pub use_distance_cache: bool,
    /// Store original vectors for reranking
    pub store_original: bool,
    /// Number of candidates to rerank with original vectors
    pub rerank_candidates: usize,
}

impl Default for PQHNSWConfig {
    fn default() -> Self {
        let m = 16;
        Self {
            m,
            m0: m * 2,
            ef_construction: 200,
            ef_search: 50,
            ml: 1.0 / (m as f32).ln(),
            use_distance_cache: true,
            store_original: false,
            rerank_candidates: 0,
        }
    }
}

impl PQHNSWConfig {
    /// Enable reranking with original vectors for better accuracy
    ///
    /// Trades memory for accuracy: stores original vectors and reranks
    /// top candidates using exact distance.
    pub fn with_reranking(mut self, candidates: usize) -> Self {
        self.store_original = true;
        self.rerank_candidates = candidates;
        self
    }

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

// ============================================================================
// PQ HNSW Node
// ============================================================================

/// Node in PQ HNSW graph
#[derive(Debug, Clone)]
struct PQNode {
    /// Document ID
    id: String,
    /// Document content
    content: String,
    /// PQ-encoded embedding
    code: PQCode,
    /// Original embedding (if store_original is true)
    original: Option<Vec<f32>>,
    /// Metadata
    metadata: Option<serde_json::Value>,
    /// Connections per layer
    connections: Vec<HashSet<usize>>,
}

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

/// HNSW index with Product Quantization
///
/// Achieves extreme compression (up to 192x) while maintaining good search quality.
/// Uses Asymmetric Distance Computation (ADC) for accurate search with full-precision queries.
pub struct PQHNSWIndex {
    config: PQHNSWConfig,
    pq: ProductQuantizer,
    distance_cache: Option<PQDistanceCache>,
    nodes: Vec<PQNode>,
    entry_point: Option<usize>,
    max_layer: usize,
}

impl PQHNSWIndex {
    /// Train a new PQ HNSW index on sample vectors
    ///
    /// # Arguments
    /// * `pq_config` - Product quantization configuration
    /// * `training_data` - Sample vectors for codebook training
    /// * `config` - HNSW configuration
    pub fn train(
        pq_config: PQConfig,
        training_data: &[Vec<f32>],
        config: PQHNSWConfig,
    ) -> Result<Self> {
        let pq = ProductQuantizer::train(training_data, pq_config)?;

        let distance_cache = if config.use_distance_cache {
            Some(PQDistanceCache::build(&pq))
        } else {
            None
        };

        Ok(Self {
            config,
            pq,
            distance_cache,
            nodes: Vec::new(),
            entry_point: None,
            max_layer: 0,
        })
    }

    /// Create index from pre-trained quantizer
    pub fn from_quantizer(pq: ProductQuantizer, config: PQHNSWConfig) -> Self {
        let distance_cache = if config.use_distance_cache {
            Some(PQDistanceCache::build(&pq))
        } else {
            None
        };

        Self {
            config,
            pq,
            distance_cache,
            nodes: Vec::new(),
            entry_point: None,
            max_layer: 0,
        }
    }

    /// Add a document to the index
    pub fn add(&mut self, document: Document) -> Result<()> {
        let dim = self.pq.config().dim;
        if document.embedding.len() != dim {
            return Err(RagError::DimensionMismatch {
                expected: dim,
                actual: document.embedding.len(),
            });
        }

        if document.embedding.iter().any(|v| !v.is_finite()) {
            return Err(RagError::InvalidInput(
                "embedding contains non-finite values (NaN or Inf)".to_string(),
            ));
        }

        let node_id = self.nodes.len();
        let node_level = self.random_level();

        let mut connections = Vec::with_capacity(node_level + 1);
        for _ in 0..=node_level {
            connections.push(HashSet::new());
        }

        let code = self.pq.encode(&document.embedding);
        let original = if self.config.store_original {
            Some(document.embedding)
        } else {
            None
        };

        let node = PQNode {
            id: document.id,
            content: document.content,
            code,
            original,
            metadata: document.metadata,
            connections,
        };

        self.nodes.push(node);

        if self.entry_point.is_none() {
            self.entry_point = Some(node_id);
            self.max_layer = node_level;
            return Ok(());
        }

        self.insert_node(node_id, node_level);

        if node_level > self.max_layer {
            self.max_layer = node_level;
            self.entry_point = Some(node_id);
        }

        Ok(())
    }

    /// Search for k nearest neighbors using ADC
    ///
    /// Uses Asymmetric Distance Computation for best accuracy:
    /// full precision query vs PQ-compressed database vectors.
    pub fn search(&self, query: &[f32], k: usize) -> Result<Vec<SearchResult>> {
        let dim = self.pq.config().dim;
        if query.len() != dim {
            return Err(RagError::DimensionMismatch {
                expected: dim,
                actual: query.len(),
            });
        }

        if self.nodes.is_empty() {
            return Ok(Vec::new());
        }

        // Precompute distance table for fast ADC
        let distance_table = self.pq.compute_distance_table(query);

        let entry_point = self.entry_point.unwrap();
        let mut current_nearest = vec![entry_point];

        // Search upper layers
        for layer in (1..=self.max_layer).rev() {
            current_nearest = self.search_layer_adc(&distance_table, &current_nearest, 1, layer);
        }

        // Search layer 0 with ef_search candidates
        let ef = self.config.ef_search.max(k);
        let candidates = if self.config.rerank_candidates > 0 {
            self.config.ef_search.max(self.config.rerank_candidates)
        } else {
            ef
        };
        current_nearest = self.search_layer_adc(&distance_table, &current_nearest, candidates, 0);

        // Optionally rerank with original vectors
        let results = if self.config.store_original && self.config.rerank_candidates > 0 {
            self.rerank_with_original(query, &current_nearest, k)
        } else {
            self.to_search_results_adc(query, &current_nearest, &distance_table, k)
        };

        Ok(results)
    }

    /// Search using symmetric distance (fastest, lower accuracy)
    pub fn search_symmetric(&self, query: &[f32], k: usize) -> Result<Vec<SearchResult>> {
        let dim = self.pq.config().dim;
        if query.len() != dim {
            return Err(RagError::DimensionMismatch {
                expected: dim,
                actual: query.len(),
            });
        }

        if self.nodes.is_empty() {
            return Ok(Vec::new());
        }

        // Quantize query for symmetric search
        let query_code = self.pq.encode(query);

        let entry_point = self.entry_point.unwrap();
        let mut current_nearest = vec![entry_point];

        for layer in (1..=self.max_layer).rev() {
            current_nearest = self.search_layer_symmetric(&query_code, &current_nearest, 1, layer);
        }

        let ef = self.config.ef_search.max(k);
        current_nearest = self.search_layer_symmetric(&query_code, &current_nearest, ef, 0);

        let results = self.to_search_results_symmetric(&query_code, &current_nearest, k);

        Ok(results)
    }

    /// Get number of documents
    pub fn len(&self) -> usize {
        self.nodes.len()
    }

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

    /// Clear all documents
    pub fn clear(&mut self) {
        self.nodes.clear();
        self.entry_point = None;
        self.max_layer = 0;
    }

    /// Get the embedding dimension
    pub fn embedding_dim(&self) -> usize {
        self.pq.config().dim
    }

    /// Get the product quantizer
    pub fn quantizer(&self) -> &ProductQuantizer {
        &self.pq
    }

    /// Memory usage estimate in bytes
    pub fn memory_usage(&self) -> usize {
        if self.nodes.is_empty() {
            return 0;
        }

        let code_size = self.pq.config().num_subvectors;
        let original_size = if self.config.store_original {
            self.pq.config().dim * 4
        } else {
            0
        };
        let overhead_per_node = 100;

        // Codebook size
        let codebook_size = self.pq.config().num_subvectors
            * self.pq.config().num_centroids()
            * self.pq.config().subvector_dim()
            * 4;

        // Distance cache size
        let cache_size = if self.distance_cache.is_some() {
            self.pq.config().num_subvectors
                * self.pq.config().num_centroids()
                * self.pq.config().num_centroids()
                * 4
        } else {
            0
        };

        self.nodes.len() * (code_size + original_size + overhead_per_node)
            + codebook_size
            + cache_size
    }

    /// Get compression ratio vs full f32 storage
    pub fn compression_ratio(&self) -> f32 {
        if self.nodes.is_empty() {
            return 0.0;
        }

        let full_size = self.nodes.len() * self.pq.config().dim * 4;
        let actual_size = self.memory_usage();

        full_size as f32 / actual_size as f32
    }

    fn random_level(&self) -> usize {
        let mut rng = rand::thread_rng();
        let uniform: f32 = rng.gen::<f32>().max(f32::EPSILON);
        (-uniform.ln() * self.config.ml).floor() as usize
    }

    fn insert_node(&mut self, node_id: usize, node_level: usize) {
        let entry_point = self.entry_point.unwrap();
        let mut current_nearest = vec![entry_point];
        let node_code = self.nodes[node_id].code.clone();

        for layer in (node_level + 1..=self.max_layer).rev() {
            current_nearest = self.search_layer_symmetric(&node_code, &current_nearest, 1, layer);
        }

        for layer in (0..=node_level).rev() {
            current_nearest = self.search_layer_symmetric(
                &node_code,
                &current_nearest,
                self.config.ef_construction,
                layer,
            );

            let m = if layer == 0 {
                self.config.m0
            } else {
                self.config.m
            };
            let neighbors = self.select_neighbors(&current_nearest, &node_code, m);

            for &neighbor_id in &neighbors {
                self.nodes[node_id].connections[layer].insert(neighbor_id);

                if layer < self.nodes[neighbor_id].connections.len() {
                    self.nodes[neighbor_id].connections[layer].insert(node_id);

                    let neighbor_m = if layer == 0 {
                        self.config.m0
                    } else {
                        self.config.m
                    };
                    if self.nodes[neighbor_id].connections[layer].len() > neighbor_m {
                        let neighbor_code = self.nodes[neighbor_id].code.clone();
                        let neighbor_connections: Vec<usize> = self.nodes[neighbor_id].connections
                            [layer]
                            .iter()
                            .copied()
                            .collect();
                        let pruned = self.select_neighbors(
                            &neighbor_connections,
                            &neighbor_code,
                            neighbor_m,
                        );
                        self.nodes[neighbor_id].connections[layer] = pruned.into_iter().collect();
                    }
                }
            }
        }
    }

    fn search_layer_adc(
        &self,
        distance_table: &[Vec<f32>],
        entry_points: &[usize],
        ef: usize,
        layer: usize,
    ) -> Vec<usize> {
        let mut visited = HashSet::new();
        let mut candidates = BinaryHeap::new();
        let mut best = BinaryHeap::new();

        for &ep in entry_points {
            let dist = self
                .pq
                .distance_with_table(distance_table, &self.nodes[ep].code);
            candidates.push(Reverse((OrderedFloat(dist), ep)));
            best.push((OrderedFloat(dist), ep));
            visited.insert(ep);
        }

        while let Some(Reverse((current_dist, current_id))) = candidates.pop() {
            if best.len() >= ef {
                if let Some(&(furthest_dist, _)) = best.peek() {
                    if current_dist > furthest_dist {
                        break;
                    }
                }
            }

            if layer < self.nodes[current_id].connections.len() {
                for &neighbor_id in &self.nodes[current_id].connections[layer] {
                    if !visited.contains(&neighbor_id) {
                        visited.insert(neighbor_id);
                        let dist = self
                            .pq
                            .distance_with_table(distance_table, &self.nodes[neighbor_id].code);
                        let dist_ord = OrderedFloat(dist);

                        if best.len() < ef {
                            candidates.push(Reverse((dist_ord, neighbor_id)));
                            best.push((dist_ord, neighbor_id));
                        } else if let Some(&(furthest_dist, _)) = best.peek() {
                            if dist_ord < furthest_dist {
                                candidates.push(Reverse((dist_ord, neighbor_id)));
                                best.push((dist_ord, neighbor_id));
                                if best.len() > ef {
                                    best.pop();
                                }
                            }
                        }
                    }
                }
            }
        }

        let mut results: Vec<(f32, usize)> = best
            .into_iter()
            .map(|(OrderedFloat(dist), id)| (dist, id))
            .collect();
        results.sort_by(|a, b| a.0.total_cmp(&b.0));
        results.into_iter().map(|(_, id)| id).collect()
    }

    fn search_layer_symmetric(
        &self,
        query_code: &PQCode,
        entry_points: &[usize],
        ef: usize,
        layer: usize,
    ) -> Vec<usize> {
        let mut visited = HashSet::new();
        let mut candidates = BinaryHeap::new();
        let mut best = BinaryHeap::new();

        for &ep in entry_points {
            let dist = self.distance_symmetric(query_code, &self.nodes[ep].code);
            candidates.push(Reverse((OrderedFloat(dist), ep)));
            best.push((OrderedFloat(dist), ep));
            visited.insert(ep);
        }

        while let Some(Reverse((current_dist, current_id))) = candidates.pop() {
            if best.len() >= ef {
                if let Some(&(furthest_dist, _)) = best.peek() {
                    if current_dist > furthest_dist {
                        break;
                    }
                }
            }

            if layer < self.nodes[current_id].connections.len() {
                for &neighbor_id in &self.nodes[current_id].connections[layer] {
                    if !visited.contains(&neighbor_id) {
                        visited.insert(neighbor_id);
                        let dist =
                            self.distance_symmetric(query_code, &self.nodes[neighbor_id].code);
                        let dist_ord = OrderedFloat(dist);

                        if best.len() < ef {
                            candidates.push(Reverse((dist_ord, neighbor_id)));
                            best.push((dist_ord, neighbor_id));
                        } else if let Some(&(furthest_dist, _)) = best.peek() {
                            if dist_ord < furthest_dist {
                                candidates.push(Reverse((dist_ord, neighbor_id)));
                                best.push((dist_ord, neighbor_id));
                                if best.len() > ef {
                                    best.pop();
                                }
                            }
                        }
                    }
                }
            }
        }

        let mut results: Vec<(f32, usize)> = best
            .into_iter()
            .map(|(OrderedFloat(dist), id)| (dist, id))
            .collect();
        results.sort_by(|a, b| a.0.total_cmp(&b.0));
        results.into_iter().map(|(_, id)| id).collect()
    }

    fn select_neighbors(&self, candidates: &[usize], query_code: &PQCode, m: usize) -> Vec<usize> {
        let mut scored: Vec<(f32, usize)> = candidates
            .iter()
            .map(|&id| {
                let dist = self.distance_symmetric(query_code, &self.nodes[id].code);
                (dist, id)
            })
            .collect();

        scored.sort_by(|a, b| a.0.total_cmp(&b.0));
        scored.truncate(m);
        scored.into_iter().map(|(_, id)| id).collect()
    }

    fn distance_symmetric(&self, a: &PQCode, b: &PQCode) -> f32 {
        if let Some(ref cache) = self.distance_cache {
            cache.distance(a, b)
        } else {
            self.pq.symmetric_distance(a, b)
        }
    }

    fn to_search_results_adc(
        &self,
        _query: &[f32],
        node_ids: &[usize],
        distance_table: &[Vec<f32>],
        k: usize,
    ) -> Vec<SearchResult> {
        let mut results: Vec<SearchResult> = node_ids
            .iter()
            .map(|&id| {
                let node = &self.nodes[id];
                let dist = self.pq.distance_with_table(distance_table, &node.code);
                // Convert distance to similarity score
                let score = 1.0 / (1.0 + dist);
                SearchResult {
                    id: node.id.clone(),
                    content: node.content.clone(),
                    score,
                    metadata: node.metadata.clone(),
                }
            })
            .collect();

        results.sort_by(|a, b| b.score.total_cmp(&a.score));
        results.truncate(k);
        results
    }

    fn to_search_results_symmetric(
        &self,
        query_code: &PQCode,
        node_ids: &[usize],
        k: usize,
    ) -> Vec<SearchResult> {
        let mut results: Vec<SearchResult> = node_ids
            .iter()
            .map(|&id| {
                let node = &self.nodes[id];
                let dist = self.distance_symmetric(query_code, &node.code);
                let score = 1.0 / (1.0 + dist);
                SearchResult {
                    id: node.id.clone(),
                    content: node.content.clone(),
                    score,
                    metadata: node.metadata.clone(),
                }
            })
            .collect();

        results.sort_by(|a, b| b.score.total_cmp(&a.score));
        results.truncate(k);
        results
    }

    fn rerank_with_original(
        &self,
        query: &[f32],
        node_ids: &[usize],
        k: usize,
    ) -> Vec<SearchResult> {
        let mut results: Vec<SearchResult> = node_ids
            .iter()
            .filter_map(|&id| {
                let node = &self.nodes[id];
                node.original.as_ref().map(|original| {
                    let score = crate::vector::cosine_similarity(query, original).unwrap_or(0.0);
                    SearchResult {
                        id: node.id.clone(),
                        content: node.content.clone(),
                        score,
                        metadata: node.metadata.clone(),
                    }
                })
            })
            .collect();

        results.sort_by(|a, b| b.score.total_cmp(&a.score));
        results.truncate(k);
        results
    }
}

impl crate::index::VectorIndex for PQHNSWIndex {
    fn add(&mut self, document: Document) -> Result<()> {
        self.add(document)
    }

    fn search(&self, query: &[f32], k: usize) -> Result<Vec<SearchResult>> {
        self.search(query, k)
    }

    fn len(&self) -> usize {
        self.len()
    }

    fn clear(&mut self) {
        self.clear()
    }

    fn embedding_dim(&self) -> usize {
        self.embedding_dim()
    }
}

/// OrderedFloat for BinaryHeap
#[derive(Debug, Clone, Copy, PartialEq)]
struct OrderedFloat(f32);

impl Eq for OrderedFloat {}

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

impl Ord for OrderedFloat {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.0.total_cmp(&other.0)
    }
}

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

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

    fn generate_random_vectors(n: usize, dim: usize, seed: u64) -> Vec<Vec<f32>> {
        use rand::SeedableRng;
        let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
        (0..n)
            .map(|_| {
                (0..dim)
                    .map(|_| rand::Rng::gen_range(&mut rng, -1.0..1.0))
                    .collect()
            })
            .collect()
    }

    fn create_test_document(id: &str, embedding: Vec<f32>) -> Document {
        Document {
            id: id.to_string(),
            content: format!("Content for {}", id),
            embedding,
            metadata: None,
        }
    }

    #[test]
    fn test_pq_hnsw_config() {
        let config = PQHNSWConfig::default();
        assert_eq!(config.m, 16);
        assert_eq!(config.ef_construction, 200);
        assert!(!config.store_original);
    }

    #[test]
    fn test_pq_hnsw_basic() {
        let dim = 64;
        let pq_config = PQConfig::new(dim, 8, 8)
            .with_seed(42)
            .with_kmeans_iterations(10);
        let training_data = generate_random_vectors(500, dim, 42);

        let mut index =
            PQHNSWIndex::train(pq_config, &training_data, PQHNSWConfig::default()).unwrap();
        assert!(index.is_empty());

        let doc = create_test_document("doc1", generate_random_vectors(1, dim, 100)[0].clone());
        index.add(doc).unwrap();

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

    #[test]
    fn test_pq_hnsw_search() {
        let dim = 64;
        let pq_config = PQConfig::new(dim, 8, 8)
            .with_seed(42)
            .with_kmeans_iterations(10);
        let training_data = generate_random_vectors(500, dim, 42);

        let mut index =
            PQHNSWIndex::train(pq_config, &training_data, PQHNSWConfig::default()).unwrap();

        // Add documents
        for i in 0..100 {
            let doc = create_test_document(
                &format!("doc{}", i),
                generate_random_vectors(1, dim, i)[0].clone(),
            );
            index.add(doc).unwrap();
        }

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

        // Search
        let query = generate_random_vectors(1, dim, 999)[0].clone();
        let results = index.search(&query, 10).unwrap();

        assert_eq!(results.len(), 10);

        // Results should be sorted by score descending
        for i in 0..results.len() - 1 {
            assert!(results[i].score >= results[i + 1].score);
        }
    }

    #[test]
    fn test_pq_hnsw_compression() {
        let dim = 384;
        let pq_config = PQConfig::new(dim, 8, 8)
            .with_seed(42)
            .with_kmeans_iterations(10);
        let training_data = generate_random_vectors(500, dim, 42);

        let mut index =
            PQHNSWIndex::train(pq_config, &training_data, PQHNSWConfig::default()).unwrap();

        // Use enough vectors to amortize fixed overhead (codebook + distance cache)
        for i in 0..5000 {
            let doc = create_test_document(
                &format!("doc{}", i),
                generate_random_vectors(1, dim, i)[0].clone(),
            );
            index.add(doc).unwrap();
        }

        let memory = index.memory_usage();
        let full_size = 5000 * dim * 4;

        println!("PQ HNSW memory: {} bytes", memory);
        println!("Full f32 would be: {} bytes", full_size);
        println!("Compression ratio: {:.1}x", index.compression_ratio());

        // PQ codes are 8 bytes vs 1536 bytes (192x for vectors alone)
        // But we have fixed overhead from codebook + distance cache + HNSW graph
        // For 5K vectors, should still achieve > 2x compression
        assert!(
            memory < full_size / 2,
            "Memory should be < 50% of full size, got {}%",
            (memory as f64 / full_size as f64 * 100.0) as u32
        );
    }

    #[test]
    fn test_pq_hnsw_with_reranking() {
        let dim = 64;
        let pq_config = PQConfig::new(dim, 8, 8)
            .with_seed(42)
            .with_kmeans_iterations(10);
        let training_data = generate_random_vectors(500, dim, 42);

        let config = PQHNSWConfig::default().with_reranking(50);
        let mut index = PQHNSWIndex::train(pq_config, &training_data, config).unwrap();

        for i in 0..100 {
            let doc = create_test_document(
                &format!("doc{}", i),
                generate_random_vectors(1, dim, i)[0].clone(),
            );
            index.add(doc).unwrap();
        }

        let query = generate_random_vectors(1, dim, 999)[0].clone();
        let results = index.search(&query, 10).unwrap();

        assert_eq!(results.len(), 10);

        // With reranking, scores should be cosine similarity values
        for result in &results {
            assert!(result.score >= -1.0 && result.score <= 1.0);
        }
    }

    #[test]
    fn test_pq_hnsw_dimension_mismatch() {
        let dim = 64;
        let pq_config = PQConfig::new(dim, 8, 8)
            .with_seed(42)
            .with_kmeans_iterations(10);
        let training_data = generate_random_vectors(100, dim, 42);

        let mut index =
            PQHNSWIndex::train(pq_config, &training_data, PQHNSWConfig::default()).unwrap();

        // Wrong dimension
        let doc = create_test_document("doc1", vec![0.5; 32]);
        assert!(index.add(doc).is_err());
    }

    #[test]
    fn test_pq_hnsw_symmetric_search() {
        let dim = 64;
        let pq_config = PQConfig::new(dim, 8, 8)
            .with_seed(42)
            .with_kmeans_iterations(10);
        let training_data = generate_random_vectors(500, dim, 42);

        let mut index =
            PQHNSWIndex::train(pq_config, &training_data, PQHNSWConfig::default()).unwrap();

        for i in 0..100 {
            let doc = create_test_document(
                &format!("doc{}", i),
                generate_random_vectors(1, dim, i)[0].clone(),
            );
            index.add(doc).unwrap();
        }

        let query = generate_random_vectors(1, dim, 999)[0].clone();

        // Symmetric should be faster but less accurate
        let symmetric_results = index.search_symmetric(&query, 10).unwrap();
        assert_eq!(symmetric_results.len(), 10);

        // ADC should generally give better results
        let adc_results = index.search(&query, 10).unwrap();
        assert_eq!(adc_results.len(), 10);
    }

    #[test]
    fn test_pq_hnsw_add_nan_embedding_rejected() {
        let dim = 64;
        let pq_config = PQConfig::new(dim, 8, 8)
            .with_seed(42)
            .with_kmeans_iterations(10);
        let training_data = generate_random_vectors(100, dim, 42);

        let mut index =
            PQHNSWIndex::train(pq_config, &training_data, PQHNSWConfig::default()).unwrap();

        let mut embedding = vec![0.5; dim];
        embedding[10] = f32::NAN;
        let doc = create_test_document("nan_doc", embedding);
        assert!(index.add(doc).is_err());
        assert_eq!(index.len(), 0);
    }

    #[test]
    fn test_pq_search_with_nan_query_does_not_panic() {
        let dim = 64;
        let pq_config = PQConfig::new(dim, 8, 8)
            .with_seed(7)
            .with_kmeans_iterations(5);
        let training_data = generate_random_vectors(256, dim, 11);

        let mut index =
            PQHNSWIndex::train(pq_config, &training_data, PQHNSWConfig::default()).unwrap();
        index
            .add(create_test_document(
                "doc1",
                generate_random_vectors(1, dim, 99)[0].clone(),
            ))
            .unwrap();

        let query = vec![f32::NAN; dim];
        let outcome = std::panic::catch_unwind(|| index.search(&query, 1));
        assert!(
            outcome.is_ok(),
            "PQ search panicked when query contains NaN"
        );
    }
}