nodedb_vector/collection/codec_build.rs
1// SPDX-License-Identifier: Apache-2.0
2
3//! Methods on `VectorCollection` for building the collection-level
4//! codec-dispatch index (RaBitQ, BBQ).
5//!
6//! All `impl VectorCollection` blocks in `collection/` extend the same type.
7
8use super::codec_dispatch::{CollectionCodec, build_collection_codec};
9use super::lifecycle::VectorCollection;
10
11use crate::error::VectorError;
12
13impl VectorCollection {
14 /// Every live FP32 vector of the sealed segments, keyed by its global
15 /// vector id. The collection-level codec index covers the sealed
16 /// segments only: search reads the growing and building segments by
17 /// brute force beside it.
18 pub(crate) fn gather_sealed_vectors_fp32(&self) -> Vec<(u32, Vec<f32>)> {
19 let mut out = Vec::new();
20 for seg in &self.sealed {
21 let n = seg.index.len();
22 for i in 0..n as u32 {
23 if !seg.index.is_deleted(i)
24 && let Some(v) = seg.index.get_vector(i)
25 {
26 out.push((seg.base_id + i, v.to_vec()));
27 }
28 }
29 }
30 out
31 }
32
33 /// Build a codec-dispatched index over the sealed vectors using the
34 /// requested quantization. Replaces any existing dispatch index for
35 /// this collection. Idempotent.
36 ///
37 /// Returns a reference to the new index, or `Ok(None)` if the
38 /// quantization tag is not supported (falls back to per-segment Sq8/PQ
39 /// paths) or there are no vectors to train on.
40 pub fn build_codec_dispatch(
41 &mut self,
42 quantization: &str,
43 ) -> Result<Option<&CollectionCodec>, VectorError> {
44 let vectors = self.gather_sealed_vectors_fp32();
45 let dim = self.dim;
46 let m = self.params.m;
47 let ef_construction = self.params.ef_construction;
48 let seed = 42_u64;
49 self.codec_dispatch =
50 build_collection_codec(quantization, &vectors, dim, m, ef_construction, seed)?;
51 Ok(self.codec_dispatch.as_ref())
52 }
53}