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
//! IVF-PQ search implementation.
use super::opq::OptimizedProductQuantizer;
use super::pq::ProductQuantizer;
use crate::pq_simd::{adc_batch_dispatch, PackedLUT};
use crate::RetrieveError;
use serde::{Deserialize, Serialize};
/// Minimum candidates in a partition to use SIMD batch ADC.
/// Below this threshold, scalar per-candidate lookup is used.
const SIMD_BATCH_THRESHOLD: usize = 16;
// flat_table_to_nested removed: PackedLUT::from_flat skips the intermediate allocation.
/// Quantizer strategy.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Quantizer {
/// Standard Product Quantization.
Product(ProductQuantizer),
/// Optimized Product Quantization (with rotation).
Optimized(OptimizedProductQuantizer),
}
impl Quantizer {
/// Quantize a vector into PQ codes.
pub fn quantize(&self, vector: &[f32]) -> Vec<u8> {
match self {
Self::Product(pq) => pq.quantize(vector),
Self::Optimized(opq) => opq.quantize(vector),
}
}
/// Compute an asymmetric distance computation (ADC) lookup table for a query.
pub fn compute_adc_table(&self, query: &[f32]) -> Result<Vec<f32>, RetrieveError> {
match self {
Self::Product(pq) => pq.compute_adc_table(query),
Self::Optimized(opq) => opq.approximate_distance_table(query),
}
}
/// Compute approximate distance using a pre-computed ADC table and PQ codes.
pub fn distance_with_table(&self, table: &[f32], codes: &[u8]) -> f32 {
match self {
Self::Product(pq) => pq.distance_with_table(table, codes),
Self::Optimized(opq) => opq.distance_with_table(table, codes),
}
}
}
/// IVF-PQ index for memory-efficient approximate nearest neighbor search.
#[derive(Debug)]
pub struct IVFPQIndex {
pub(crate) vectors: Vec<f32>,
pub(crate) dimension: usize,
pub(crate) num_vectors: usize,
params: IVFPQParams,
built: bool,
// IVF components
clusters: Vec<Cluster>,
/// Flat centroid storage: `[c0_d0, c0_d1, ..., c1_d0, ...]` with stride = dimension.
pub(crate) centroids: Vec<f32>,
// PQ components
pq: Option<Quantizer>,
// Flattened codes: [vector_0_codes, vector_1_codes, ...]
// Stride = num_codebooks
pub(crate) quantized_codes: Vec<u8>,
// Filtering support
/// Metadata store: doc_id -> category_id mapping
metadata: Option<crate::filtering::MetadataStore>,
/// Field name for filtering (e.g., "category")
filter_field: Option<String>,
}
/// IVF-PQ parameters.
#[derive(Clone, Debug)]
pub struct IVFPQParams {
/// Number of clusters (inverted lists)
pub num_clusters: usize,
/// Number of clusters to search (nprobe)
pub nprobe: usize,
/// Product quantization: number of codebooks
pub num_codebooks: usize,
/// Product quantization: codebook size
pub codebook_size: usize,
/// Use Optimized Product Quantization (OPQ)
pub use_opq: bool,
/// ID compression method (optional)
#[cfg(feature = "id-compression")]
pub id_compression: Option<crate::compression::IdCompressionMethod>,
/// Minimum cluster size to compress (smaller clusters use uncompressed storage)
#[cfg(feature = "id-compression")]
pub compression_threshold: usize,
}
impl Default for IVFPQParams {
fn default() -> Self {
Self {
num_clusters: 1024,
nprobe: 100,
num_codebooks: 8,
codebook_size: 256,
use_opq: false,
#[cfg(feature = "id-compression")]
id_compression: None,
#[cfg(feature = "id-compression")]
compression_threshold: 100, // Only compress clusters with > 100 IDs
}
}
}
/// Storage for cluster IDs (compressed or uncompressed).
#[derive(Clone, Debug, Serialize, Deserialize)]
enum ClusterStorage {
/// Uncompressed IDs (current implementation).
Uncompressed(Vec<u32>),
/// Compressed IDs using ROC.
#[cfg(feature = "id-compression")]
Compressed {
data: Vec<u8>,
num_ids: usize,
universe_size: u32,
},
}
/// Cluster (inverted list) containing vector indices.
#[derive(Clone, Debug, Serialize, Deserialize)]
struct Cluster {
storage: ClusterStorage,
/// Filter bitmask: set of category IDs present in this cluster
/// Bit i is set if any vector in cluster has category i
filter_bitmask: u64,
/// Cache for decompressed IDs (temporary, cleared after use)
#[cfg(feature = "id-compression")]
#[serde(skip)]
#[allow(dead_code)]
decompressed_cache: Option<Vec<u32>>,
}
impl Cluster {
/// Create uncompressed cluster.
fn new(ids: Vec<u32>, filter_bitmask: u64) -> Self {
Self {
storage: ClusterStorage::Uncompressed(ids),
filter_bitmask,
#[cfg(feature = "id-compression")]
decompressed_cache: None,
}
}
/// Create compressed cluster.
#[cfg(feature = "id-compression")]
fn new_compressed(
ids: Vec<u32>,
filter_bitmask: u64,
_compressor: &crate::compression::RocCompressor,
universe_size: u32,
) -> Result<Self, crate::compression::CompressionError> {
// Sort IDs (required for compression)
let mut sorted_ids = ids;
sorted_ids.sort();
sorted_ids.dedup();
// Compress (self-describing envelope)
let compressed = crate::compression::compress_set_enveloped(
&sorted_ids,
universe_size,
crate::compression::AutoConfig::default(),
)?;
Ok(Self {
storage: ClusterStorage::Compressed {
data: compressed,
num_ids: sorted_ids.len(),
universe_size,
},
filter_bitmask,
decompressed_cache: None,
})
}
/// Get IDs (decompress if needed).
#[cfg(feature = "id-compression")]
#[allow(dead_code)]
fn get_ids(&mut self) -> Result<&[u32], crate::compression::CompressionError> {
match &self.storage {
ClusterStorage::Uncompressed(ids) => Ok(ids),
ClusterStorage::Compressed {
data,
universe_size,
..
} => {
// Check cache first
if let Some(ref cached) = self.decompressed_cache {
return Ok(cached);
}
// Decompress
let (_choice, u2, decompressed) =
crate::compression::decompress_set_enveloped(data)?;
if u2 != *universe_size {
return Err(crate::compression::CompressionError::DecompressionFailed(
"universe mismatch in envelope".to_string(),
));
}
// Cache (will be cleared after search)
self.decompressed_cache = Some(decompressed);
// Safety: just assigned Some on the line above
#[allow(clippy::unwrap_used)]
Ok(self.decompressed_cache.as_ref().unwrap())
}
}
}
/// Get IDs (for immutable access, clones if compressed).
fn get_ids_immut(&self) -> Vec<u32> {
match &self.storage {
ClusterStorage::Uncompressed(ids) => ids.clone(),
#[cfg(feature = "id-compression")]
ClusterStorage::Compressed {
data,
universe_size,
..
} => {
// For immutable access, we need to decompress (no caching)
crate::compression::decompress_set_enveloped(data)
.map(|(_choice, u2, ids)| {
if u2 == *universe_size {
ids
} else {
Vec::new()
}
})
.unwrap_or_else(|_| Vec::new())
}
}
}
/// Get number of IDs.
#[allow(dead_code)]
fn len(&self) -> usize {
match &self.storage {
ClusterStorage::Uncompressed(ids) => ids.len(),
#[cfg(feature = "id-compression")]
ClusterStorage::Compressed { num_ids, .. } => *num_ids,
}
}
/// Clear decompression cache (call after search).
#[cfg(feature = "id-compression")]
#[allow(dead_code)]
fn clear_cache(&mut self) {
self.decompressed_cache = None;
}
}
impl IVFPQIndex {
/// Set the number of clusters to probe during search.
///
/// This can be changed after the index is built to sweep the
/// recall/latency trade-off without rebuilding.
pub fn set_nprobe(&mut self, nprobe: usize) {
self.params.nprobe = nprobe;
}
/// Create a new IVF-PQ index.
pub fn new(dimension: usize, params: IVFPQParams) -> Result<Self, RetrieveError> {
if dimension == 0 {
return Err(RetrieveError::InvalidParameter(
"dimension must be > 0".into(),
));
}
Ok(Self {
vectors: Vec::new(),
dimension,
num_vectors: 0,
params,
built: false,
clusters: Vec::new(),
centroids: Vec::new(),
pq: None,
quantized_codes: Vec::new(),
metadata: None,
filter_field: None,
})
}
/// Create a new IVF-PQ index with filtering support.
///
/// # Arguments
///
/// * `dimension` - Vector dimension
/// * `params` - IVF-PQ parameters
/// * `filter_field` - Field name for filtering (e.g., "category")
pub fn with_filtering(
dimension: usize,
params: IVFPQParams,
filter_field: impl Into<String>,
) -> Result<Self, RetrieveError> {
Ok(Self {
vectors: Vec::new(),
dimension,
num_vectors: 0,
params,
built: false,
clusters: Vec::new(),
centroids: Vec::new(),
pq: None,
quantized_codes: Vec::new(),
metadata: Some(crate::filtering::MetadataStore::new()),
filter_field: Some(filter_field.into()),
})
}
/// Add metadata for a document (required for filtering).
pub fn add_metadata(
&mut self,
doc_id: u32,
metadata: crate::filtering::DocumentMetadata,
) -> Result<(), RetrieveError> {
if let Some(ref mut store) = self.metadata {
store.add(doc_id, metadata);
Ok(())
} else {
Err(RetrieveError::InvalidParameter(
"filtering not enabled; use IVFPQIndex::with_filtering()".into(),
))
}
}
/// Add a vector to the index.
pub fn add(&mut self, _doc_id: u32, vector: Vec<f32>) -> Result<(), RetrieveError> {
self.add_slice(_doc_id, &vector)
}
/// Add a vector to the index from a borrowed slice.
///
/// Notes:
/// - The index stores vectors internally, so it must copy the slice into its own storage.
/// - IVF-PQ currently ignores `doc_id` and uses insertion order as the internal ID.
pub fn add_slice(&mut self, _doc_id: u32, vector: &[f32]) -> Result<(), RetrieveError> {
if self.built {
return Err(RetrieveError::InvalidParameter(
"cannot add vectors after index is built".into(),
));
}
if vector.len() != self.dimension {
return Err(RetrieveError::DimensionMismatch {
query_dim: vector.len(),
doc_dim: self.dimension,
});
}
self.vectors.extend_from_slice(vector);
self.num_vectors += 1;
Ok(())
}
/// Build the index.
pub fn build(&mut self) -> Result<(), RetrieveError> {
if self.built {
return Ok(());
}
if self.num_vectors == 0 {
return Err(RetrieveError::EmptyIndex);
}
// Stage 1: k-means clustering for IVF
let mut kmeans =
crate::partitioning::kmeans::KMeans::new(self.dimension, self.params.num_clusters)?;
kmeans.fit(&self.vectors, self.num_vectors)?;
// Flatten centroids to contiguous storage for cache-friendly access.
self.centroids = kmeans
.centroids()
.iter()
.flat_map(|c| c.iter().copied())
.collect();
// Assign vectors to clusters
let assignments = kmeans.assign_clusters(&self.vectors, self.num_vectors);
// Build temporary clusters with IDs
let mut temp_clusters: Vec<(Vec<u32>, u64)> =
vec![(Vec::new(), 0); self.params.num_clusters];
// Build clusters with filter bitmasks if filtering is enabled
if let Some(ref metadata_store) = self.metadata {
if let Some(ref field) = self.filter_field {
for (vector_idx, &cluster_idx) in assignments.iter().enumerate() {
let doc_id = vector_idx as u32;
temp_clusters[cluster_idx].0.push(doc_id);
// Update cluster bitmask with category
if let Some(metadata) = metadata_store.get(doc_id) {
if let Some(&category_id) = metadata.get(field) {
if category_id < 64 {
// Only support up to 64 categories (u64 bitmask)
temp_clusters[cluster_idx].1 |= 1u64 << category_id;
}
}
}
}
} else {
// No filter field, just add vectors
for (vector_idx, &cluster_idx) in assignments.iter().enumerate() {
temp_clusters[cluster_idx].0.push(vector_idx as u32);
}
}
} else {
// No metadata, just add vectors
for (vector_idx, &cluster_idx) in assignments.iter().enumerate() {
temp_clusters[cluster_idx].0.push(vector_idx as u32);
}
}
// Convert to Cluster structs with optional compression
self.clusters = temp_clusters
.into_iter()
.map(|(ids, bitmask)| {
#[cfg(feature = "id-compression")]
{
// Compress if enabled and cluster is large enough
if let Some(ref method) = self.params.id_compression {
if ids.len() >= self.params.compression_threshold {
match method {
crate::compression::IdCompressionMethod::Roc => {
let compressor = crate::compression::RocCompressor::new();
let universe_size = self.num_vectors as u32;
// Clone ids for fallback case since new_compressed takes ownership
let ids_clone = ids.clone();
Cluster::new_compressed(
ids,
bitmask,
&compressor,
universe_size,
)
.unwrap_or_else(|_| Cluster::new(ids_clone, bitmask))
}
_ => Cluster::new(ids, bitmask), // Other methods not implemented yet
}
} else {
Cluster::new(ids, bitmask)
}
} else {
Cluster::new(ids, bitmask)
}
}
#[cfg(not(feature = "id-compression"))]
{
Cluster::new(ids, bitmask)
}
})
.collect();
// Stage 2: Product Quantization on residual vectors
// Compute residuals: vector[i] - centroid[assignment[i]]
let mut residuals = Vec::with_capacity(self.num_vectors * self.dimension);
for i in 0..self.num_vectors {
let vec = self.get_vector(i);
let centroid = self.get_centroid(assignments[i]);
for (v, c) in vec.iter().zip(centroid.iter()) {
residuals.push(v - c);
}
}
// Train PQ or OPQ on residuals
let pq: Quantizer = if self.params.use_opq {
let mut opq = OptimizedProductQuantizer::new(
self.dimension,
self.params.num_codebooks,
self.params.codebook_size,
)?;
opq.fit(&residuals, self.num_vectors, 10)?; // 10 iterations
Quantizer::Optimized(opq)
} else {
let mut pq = ProductQuantizer::new(
self.dimension,
self.params.num_codebooks,
self.params.codebook_size,
)?;
pq.fit(&residuals, self.num_vectors)?;
Quantizer::Product(pq)
};
// Quantize residual vectors
self.quantized_codes = Vec::with_capacity(self.num_vectors * self.params.num_codebooks);
for i in 0..self.num_vectors {
let residual = &residuals[i * self.dimension..(i + 1) * self.dimension];
let codes = pq.quantize(residual);
self.quantized_codes.extend_from_slice(&codes);
}
self.pq = Some(pq);
self.built = true;
Ok(())
}
/// Search for k nearest neighbors.
pub fn search(&self, query: &[f32], k: usize) -> Result<Vec<(u32, f32)>, RetrieveError> {
if !self.built {
return Err(RetrieveError::InvalidParameter(
"index must be built before search".into(),
));
}
if query.len() != self.dimension {
return Err(RetrieveError::DimensionMismatch {
query_dim: query.len(),
doc_dim: self.dimension,
});
}
let pq = self
.pq
.as_ref()
.ok_or(RetrieveError::InvalidParameter("PQ not initialized".into()))?;
// Find closest clusters (partial sort: O(C log nprobe) instead of O(C log C))
let num_centroids = self.centroids.len() / self.dimension;
let mut cluster_distances: Vec<(usize, f32)> = (0..num_centroids)
.map(|idx| {
let centroid = self.get_centroid(idx);
let dist = crate::distance::cosine_distance_normalized(query, centroid);
(idx, dist)
})
.collect();
let nprobe = self.params.nprobe.min(cluster_distances.len());
if nprobe < cluster_distances.len() {
cluster_distances.select_nth_unstable_by(nprobe, |a, b| a.1.total_cmp(&b.1));
cluster_distances.truncate(nprobe);
}
cluster_distances.sort_unstable_by(|a, b| a.1.total_cmp(&b.1));
// Pre-allocate reusable buffers for the nprobe loop
let mut candidates = Vec::new();
let mut query_residual = vec![0.0f32; self.dimension];
let mut codes_batch = Vec::new();
for (cluster_idx, _) in &cluster_distances {
let cluster = &self.clusters[*cluster_idx];
let ids = cluster.get_ids_immut();
// Compute query residual in-place (no allocation)
let centroid = self.get_centroid(*cluster_idx);
for (i, (q, c)) in query.iter().zip(centroid.iter()).enumerate() {
query_residual[i] = q - c;
}
// Build ADC table from query residual
let adc_table = pq.compute_adc_table(&query_residual)?;
if ids.len() >= SIMD_BATCH_THRESHOLD {
// Build PackedLUT directly from flat table (no intermediate Vec<Vec<f32>>)
let packed_lut = PackedLUT::from_flat(
&adc_table,
self.params.num_codebooks,
self.params.codebook_size,
);
// SIMD batch path: gather codes into a reusable buffer
let num_cb = self.params.num_codebooks;
codes_batch.clear();
codes_batch.reserve(ids.len() * num_cb);
for &vector_idx in &ids {
let start = vector_idx as usize * num_cb;
codes_batch.extend_from_slice(&self.quantized_codes[start..start + num_cb]);
}
let distances = adc_batch_dispatch(&codes_batch, num_cb, &packed_lut);
for (i, &vector_idx) in ids.iter().enumerate() {
candidates.push((vector_idx, distances[i]));
}
} else {
// Scalar fallback for small clusters
for &vector_idx in &ids {
let start = vector_idx as usize * self.params.num_codebooks;
let end = start + self.params.num_codebooks;
let codes = &self.quantized_codes[start..end];
let dist = pq.distance_with_table(&adc_table, codes);
candidates.push((vector_idx, dist));
}
}
}
// Sort and return top k
candidates.sort_unstable_by(|a, b| a.1.total_cmp(&b.1));
Ok(candidates.into_iter().take(k).collect())
}
/// Search with filter using cluster tagging (integrated filtering).
///
/// Skips clusters that don't contain any vectors matching the filter,
/// reducing search space and improving performance.
///
/// # Arguments
///
/// * `query` - Query vector
/// * `k` - Number of results
/// * `filter` - Filter predicate (must be equality filter on filter_field)
///
/// # Returns
///
/// Vector of (doc_id, distance) pairs matching the filter
pub fn search_with_filter(
&self,
query: &[f32],
k: usize,
filter: &crate::filtering::MetadataFilter,
) -> Result<Vec<(u32, f32)>, RetrieveError> {
if !self.built {
return Err(RetrieveError::InvalidParameter(
"index must be built before search".into(),
));
}
if query.len() != self.dimension {
return Err(RetrieveError::DimensionMismatch {
query_dim: query.len(),
doc_dim: self.dimension,
});
}
// Extract category ID from filter (only supports equality on filter_field)
let desired_category = match filter {
crate::filtering::MetadataFilter::Equals { field, value } => {
if Some(field) != self.filter_field.as_ref() {
return Err(RetrieveError::InvalidParameter(format!(
"filter field '{}' doesn't match index filter field '{:?}'",
field, self.filter_field
)));
}
if *value >= 64 {
return Err(RetrieveError::InvalidParameter(
"category ID must be < 64 for bitmask filtering".into(),
));
}
*value
}
_ => {
return Err(RetrieveError::InvalidParameter(
"only equality filters on filter_field are supported".into(),
));
}
};
let filter_bit = 1u64 << desired_category;
// Find closest clusters (partial sort)
let num_centroids = self.centroids.len() / self.dimension;
let mut cluster_distances: Vec<(usize, f32)> = (0..num_centroids)
.map(|idx| {
let centroid = self.get_centroid(idx);
let dist = crate::distance::cosine_distance_normalized(query, centroid);
(idx, dist)
})
.collect();
let nprobe = self.params.nprobe.min(cluster_distances.len());
if nprobe < cluster_distances.len() {
cluster_distances.select_nth_unstable_by(nprobe, |a, b| a.1.total_cmp(&b.1));
cluster_distances.truncate(nprobe);
}
cluster_distances.sort_unstable_by(|a, b| a.1.total_cmp(&b.1));
// Search in top nprobe clusters, skipping those without matching vectors
let mut candidates = Vec::new();
let mut query_residual = vec![0.0f32; self.dimension];
let pq = self
.pq
.as_ref()
.ok_or(RetrieveError::InvalidParameter("PQ not initialized".into()))?;
for (cluster_idx, _) in &cluster_distances {
let cluster = &self.clusters[*cluster_idx];
// Skip cluster if it doesn't contain any vectors matching the filter
if (cluster.filter_bitmask & filter_bit) == 0 {
continue;
}
// Compute query residual in-place
let centroid = self.get_centroid(*cluster_idx);
for (i, (q, c)) in query.iter().zip(centroid.iter()).enumerate() {
query_residual[i] = q - c;
}
let adc_table = pq.compute_adc_table(&query_residual)?;
// Search vectors in this cluster, filtering by metadata
if let Some(ref metadata_store) = self.metadata {
let ids = cluster.get_ids_immut();
for &vector_idx in &ids {
if metadata_store.matches(vector_idx, filter) {
let start = vector_idx as usize * self.params.num_codebooks;
let end = start + self.params.num_codebooks;
let codes = &self.quantized_codes[start..end];
let dist = pq.distance_with_table(&adc_table, codes);
candidates.push((vector_idx, dist));
}
}
} else {
return Err(RetrieveError::InvalidParameter(
"metadata store not initialized".into(),
));
}
}
// Sort and return top k
candidates.sort_unstable_by(|a, b| a.1.total_cmp(&b.1));
Ok(candidates.into_iter().take(k).collect())
}
/// Get vector from SoA storage.
#[inline]
fn get_vector(&self, idx: usize) -> &[f32] {
let start = idx * self.dimension;
let end = start + self.dimension;
&self.vectors[start..end]
}
/// Get centroid from flat storage.
#[inline]
fn get_centroid(&self, idx: usize) -> &[f32] {
let start = idx * self.dimension;
let end = start + self.dimension;
&self.centroids[start..end]
}
}