next_plaid/
update.rs

1//! Index update functionality for adding new documents.
2//!
3//! This module provides functions to incrementally update an existing PLAID index
4//! with new documents, matching fast-plaid's behavior:
5//! - Buffer mechanism for small updates
6//! - Centroid expansion for outliers
7//! - Cluster threshold updates
8
9use std::collections::HashMap;
10use std::fs;
11use std::fs::File;
12use std::io::{BufReader, BufWriter};
13use std::path::Path;
14
15use serde::{Deserialize, Serialize};
16
17use ndarray::{s, Array1, Array2, Axis};
18use rayon::prelude::*;
19
20use crate::codec::ResidualCodec;
21use crate::error::Error;
22use crate::error::Result;
23use crate::index::Metadata;
24use crate::kmeans::compute_kmeans;
25use crate::kmeans::ComputeKmeansConfig;
26use crate::utils::quantile;
27
28/// Default batch size for processing documents (matches fast-plaid).
29const DEFAULT_BATCH_SIZE: usize = 50_000;
30
31/// Configuration for index updates.
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct UpdateConfig {
34    /// Batch size for processing documents (default: 50,000)
35    pub batch_size: usize,
36    /// Number of K-means iterations for centroid expansion (default: 4)
37    pub kmeans_niters: usize,
38    /// Max points per centroid for K-means (default: 256)
39    pub max_points_per_centroid: usize,
40    /// Number of samples for K-means (default: auto-calculated)
41    pub n_samples_kmeans: Option<usize>,
42    /// Random seed (default: 42)
43    pub seed: u64,
44    /// If index has fewer docs than this, rebuild from scratch (default: 999)
45    pub start_from_scratch: usize,
46    /// Buffer size before triggering centroid expansion (default: 100)
47    pub buffer_size: usize,
48}
49
50impl Default for UpdateConfig {
51    fn default() -> Self {
52        Self {
53            batch_size: DEFAULT_BATCH_SIZE,
54            kmeans_niters: 4,
55            max_points_per_centroid: 256,
56            n_samples_kmeans: None,
57            seed: 42,
58            start_from_scratch: 999,
59            buffer_size: 100,
60        }
61    }
62}
63
64impl UpdateConfig {
65    /// Convert to ComputeKmeansConfig for centroid expansion.
66    pub fn to_kmeans_config(&self) -> ComputeKmeansConfig {
67        ComputeKmeansConfig {
68            kmeans_niters: self.kmeans_niters,
69            max_points_per_centroid: self.max_points_per_centroid,
70            seed: self.seed,
71            n_samples_kmeans: self.n_samples_kmeans,
72            num_partitions: None,
73        }
74    }
75}
76
77// ============================================================================
78// Buffer Management
79// ============================================================================
80
81/// Load buffered embeddings from buffer.npy.
82///
83/// Returns an empty vector if buffer.npy doesn't exist.
84/// Uses buffer_lengths.json to split the flattened array back into per-document arrays.
85pub fn load_buffer(index_path: &Path) -> Result<Vec<Array2<f32>>> {
86    use ndarray_npy::ReadNpyExt;
87
88    let buffer_path = index_path.join("buffer.npy");
89    let lengths_path = index_path.join("buffer_lengths.json");
90
91    if !buffer_path.exists() {
92        return Ok(Vec::new());
93    }
94
95    // Load the flattened embeddings array
96    let flat: Array2<f32> = match Array2::read_npy(File::open(&buffer_path)?) {
97        Ok(arr) => arr,
98        Err(_) => return Ok(Vec::new()),
99    };
100
101    // Load lengths to split back into per-document arrays
102    if lengths_path.exists() {
103        let lengths: Vec<i64> =
104            serde_json::from_reader(BufReader::new(File::open(&lengths_path)?))?;
105
106        let mut result = Vec::with_capacity(lengths.len());
107        let mut offset = 0;
108
109        for &len in &lengths {
110            let len_usize = len as usize;
111            if offset + len_usize > flat.nrows() {
112                break;
113            }
114            let doc_emb = flat.slice(s![offset..offset + len_usize, ..]).to_owned();
115            result.push(doc_emb);
116            offset += len_usize;
117        }
118
119        return Ok(result);
120    }
121
122    // Fallback: if no lengths file, return as single document (legacy behavior)
123    Ok(vec![flat])
124}
125
126/// Save embeddings to buffer.npy.
127///
128/// Also saves buffer_info.json with the number of documents for deletion tracking.
129pub fn save_buffer(index_path: &Path, embeddings: &[Array2<f32>]) -> Result<()> {
130    use ndarray_npy::WriteNpyExt;
131
132    let buffer_path = index_path.join("buffer.npy");
133
134    // For simplicity, concatenate all embeddings into one array
135    // and store the lengths separately
136    if embeddings.is_empty() {
137        return Ok(());
138    }
139
140    let dim = embeddings[0].ncols();
141    let total_rows: usize = embeddings.iter().map(|e| e.nrows()).sum();
142
143    let mut flat = Array2::<f32>::zeros((total_rows, dim));
144    let mut offset = 0;
145    let mut lengths = Vec::new();
146
147    for emb in embeddings {
148        let n = emb.nrows();
149        flat.slice_mut(s![offset..offset + n, ..]).assign(emb);
150        lengths.push(n as i64);
151        offset += n;
152    }
153
154    flat.write_npy(File::create(&buffer_path)?)?;
155
156    // Save lengths
157    let lengths_path = index_path.join("buffer_lengths.json");
158    serde_json::to_writer(BufWriter::new(File::create(&lengths_path)?), &lengths)?;
159
160    // Save buffer info for deletion tracking (number of documents)
161    let info_path = index_path.join("buffer_info.json");
162    let buffer_info = serde_json::json!({ "num_docs": embeddings.len() });
163    serde_json::to_writer(BufWriter::new(File::create(&info_path)?), &buffer_info)?;
164
165    Ok(())
166}
167
168/// Load buffer info (number of buffered documents).
169///
170/// Returns 0 if buffer_info.json doesn't exist.
171pub fn load_buffer_info(index_path: &Path) -> Result<usize> {
172    let info_path = index_path.join("buffer_info.json");
173    if !info_path.exists() {
174        return Ok(0);
175    }
176
177    let info: serde_json::Value = serde_json::from_reader(BufReader::new(File::open(&info_path)?))?;
178
179    Ok(info.get("num_docs").and_then(|v| v.as_u64()).unwrap_or(0) as usize)
180}
181
182/// Clear buffer files.
183pub fn clear_buffer(index_path: &Path) -> Result<()> {
184    let buffer_path = index_path.join("buffer.npy");
185    let lengths_path = index_path.join("buffer_lengths.json");
186    let info_path = index_path.join("buffer_info.json");
187
188    if buffer_path.exists() {
189        fs::remove_file(&buffer_path)?;
190    }
191    if lengths_path.exists() {
192        fs::remove_file(&lengths_path)?;
193    }
194    if info_path.exists() {
195        fs::remove_file(&info_path)?;
196    }
197
198    Ok(())
199}
200
201/// Load embeddings stored for rebuild (embeddings.npy + embeddings_lengths.json).
202///
203/// This function loads raw embeddings that were saved for start-from-scratch rebuilds.
204/// The embeddings are stored in a flat 2D array with a separate lengths file.
205pub fn load_embeddings_npy(index_path: &Path) -> Result<Vec<Array2<f32>>> {
206    use ndarray_npy::ReadNpyExt;
207
208    let emb_path = index_path.join("embeddings.npy");
209    let lengths_path = index_path.join("embeddings_lengths.json");
210
211    if !emb_path.exists() {
212        return Ok(Vec::new());
213    }
214
215    // Load flat embeddings array
216    let flat: Array2<f32> = Array2::read_npy(File::open(&emb_path)?)?;
217
218    // Load lengths to split back into per-document arrays
219    if lengths_path.exists() {
220        let lengths: Vec<i64> =
221            serde_json::from_reader(BufReader::new(File::open(&lengths_path)?))?;
222
223        let mut result = Vec::with_capacity(lengths.len());
224        let mut offset = 0;
225
226        for &len in &lengths {
227            let len_usize = len as usize;
228            if offset + len_usize > flat.nrows() {
229                break;
230            }
231            let doc_emb = flat.slice(s![offset..offset + len_usize, ..]).to_owned();
232            result.push(doc_emb);
233            offset += len_usize;
234        }
235
236        return Ok(result);
237    }
238
239    // Fallback: if no lengths file, return as single document
240    Ok(vec![flat])
241}
242
243/// Save embeddings for potential rebuild (start-from-scratch mode).
244///
245/// Stores embeddings in embeddings.npy (flat array) + embeddings_lengths.json.
246/// This matches fast-plaid's behavior of storing raw embeddings when the index
247/// is below the start_from_scratch threshold.
248pub fn save_embeddings_npy(index_path: &Path, embeddings: &[Array2<f32>]) -> Result<()> {
249    use ndarray_npy::WriteNpyExt;
250
251    if embeddings.is_empty() {
252        return Ok(());
253    }
254
255    let dim = embeddings[0].ncols();
256    let total_rows: usize = embeddings.iter().map(|e| e.nrows()).sum();
257
258    let mut flat = Array2::<f32>::zeros((total_rows, dim));
259    let mut offset = 0;
260    let mut lengths = Vec::with_capacity(embeddings.len());
261
262    for emb in embeddings {
263        let n = emb.nrows();
264        flat.slice_mut(s![offset..offset + n, ..]).assign(emb);
265        lengths.push(n as i64);
266        offset += n;
267    }
268
269    // Save flat embeddings
270    let emb_path = index_path.join("embeddings.npy");
271    flat.write_npy(File::create(&emb_path)?)?;
272
273    // Save lengths for reconstruction
274    let lengths_path = index_path.join("embeddings_lengths.json");
275    serde_json::to_writer(BufWriter::new(File::create(&lengths_path)?), &lengths)?;
276
277    Ok(())
278}
279
280/// Clear embeddings.npy and embeddings_lengths.json.
281pub fn clear_embeddings_npy(index_path: &Path) -> Result<()> {
282    let emb_path = index_path.join("embeddings.npy");
283    let lengths_path = index_path.join("embeddings_lengths.json");
284
285    if emb_path.exists() {
286        fs::remove_file(&emb_path)?;
287    }
288    if lengths_path.exists() {
289        fs::remove_file(&lengths_path)?;
290    }
291    Ok(())
292}
293
294/// Check if embeddings.npy exists for start-from-scratch mode.
295pub fn embeddings_npy_exists(index_path: &Path) -> bool {
296    index_path.join("embeddings.npy").exists()
297}
298
299// ============================================================================
300// Cluster Threshold Management
301// ============================================================================
302
303/// Load cluster threshold from cluster_threshold.npy.
304pub fn load_cluster_threshold(index_path: &Path) -> Result<f32> {
305    use ndarray_npy::ReadNpyExt;
306
307    let thresh_path = index_path.join("cluster_threshold.npy");
308    if !thresh_path.exists() {
309        return Err(Error::Update("cluster_threshold.npy not found".into()));
310    }
311
312    let arr: Array1<f32> = Array1::read_npy(File::open(&thresh_path)?)?;
313    Ok(arr[0])
314}
315
316/// Update cluster_threshold.npy with weighted average.
317pub fn update_cluster_threshold(
318    index_path: &Path,
319    new_residual_norms: &Array1<f32>,
320    old_total_embeddings: usize,
321) -> Result<()> {
322    use ndarray_npy::{ReadNpyExt, WriteNpyExt};
323
324    let new_count = new_residual_norms.len();
325    if new_count == 0 {
326        return Ok(());
327    }
328
329    let new_threshold = quantile(new_residual_norms, 0.75);
330
331    let thresh_path = index_path.join("cluster_threshold.npy");
332    let final_threshold = if thresh_path.exists() {
333        let old_arr: Array1<f32> = Array1::read_npy(File::open(&thresh_path)?)?;
334        let old_threshold = old_arr[0];
335        let total = old_total_embeddings + new_count;
336        (old_threshold * old_total_embeddings as f32 + new_threshold * new_count as f32)
337            / total as f32
338    } else {
339        new_threshold
340    };
341
342    Array1::from_vec(vec![final_threshold]).write_npy(File::create(&thresh_path)?)?;
343
344    Ok(())
345}
346
347// ============================================================================
348// Centroid Expansion
349// ============================================================================
350
351/// Find outlier embeddings that are far from all existing centroids.
352///
353/// Returns indices of embeddings where min L2² distance > threshold².
354fn find_outliers(
355    flat_embeddings: &Array2<f32>,
356    centroids: &Array2<f32>,
357    threshold_sq: f32,
358) -> Vec<usize> {
359    flat_embeddings
360        .axis_iter(Axis(0))
361        .into_par_iter()
362        .enumerate()
363        .filter_map(|(i, emb)| {
364            // Find minimum squared distance to any centroid
365            let min_dist_sq = centroids
366                .axis_iter(Axis(0))
367                .map(|c| {
368                    // L2 squared distance
369                    emb.iter()
370                        .zip(c.iter())
371                        .map(|(a, b)| (a - b).powi(2))
372                        .sum::<f32>()
373                })
374                .fold(f32::INFINITY, f32::min);
375
376            if min_dist_sq > threshold_sq {
377                Some(i)
378            } else {
379                None
380            }
381        })
382        .collect()
383}
384
385/// Expand centroids by clustering embeddings far from existing centroids.
386///
387/// This implements fast-plaid's update_centroids() function:
388/// 1. Flatten all new embeddings
389/// 2. Find outliers (distance > cluster_threshold²)
390/// 3. Cluster outliers to get new centroids
391/// 4. Append new centroids to centroids.npy
392/// 5. Extend ivf_lengths.npy with zeros
393/// 6. Update metadata.json num_partitions
394///
395/// Returns the number of new centroids added.
396pub fn update_centroids(
397    index_path: &Path,
398    new_embeddings: &[Array2<f32>],
399    cluster_threshold: f32,
400    config: &UpdateConfig,
401) -> Result<usize> {
402    use ndarray_npy::{ReadNpyExt, WriteNpyExt};
403
404    let centroids_path = index_path.join("centroids.npy");
405    if !centroids_path.exists() {
406        return Ok(0);
407    }
408
409    // Load existing centroids
410    let existing_centroids: Array2<f32> = Array2::read_npy(File::open(&centroids_path)?)?;
411
412    // Flatten all new embeddings
413    let dim = existing_centroids.ncols();
414    let total_tokens: usize = new_embeddings.iter().map(|e| e.nrows()).sum();
415
416    if total_tokens == 0 {
417        return Ok(0);
418    }
419
420    let mut flat_embeddings = Array2::<f32>::zeros((total_tokens, dim));
421    let mut offset = 0;
422
423    for emb in new_embeddings {
424        let n = emb.nrows();
425        flat_embeddings
426            .slice_mut(s![offset..offset + n, ..])
427            .assign(emb);
428        offset += n;
429    }
430
431    // Find outliers
432    let threshold_sq = cluster_threshold * cluster_threshold;
433    let outlier_indices = find_outliers(&flat_embeddings, &existing_centroids, threshold_sq);
434
435    let num_outliers = outlier_indices.len();
436    if num_outliers == 0 {
437        return Ok(0);
438    }
439
440    // Extract outlier embeddings
441    let mut outliers = Array2::<f32>::zeros((num_outliers, dim));
442    for (i, &idx) in outlier_indices.iter().enumerate() {
443        outliers.row_mut(i).assign(&flat_embeddings.row(idx));
444    }
445
446    // Compute number of new centroids
447    // k_update = max(1, ceil(num_outliers / max_points_per_centroid) * 4)
448    let target_k =
449        ((num_outliers as f64 / config.max_points_per_centroid as f64).ceil() as usize).max(1) * 4;
450    let k_update = target_k.min(num_outliers); // Can't have more centroids than points
451
452    // Cluster outliers to get new centroids
453    let kmeans_config = ComputeKmeansConfig {
454        kmeans_niters: config.kmeans_niters,
455        max_points_per_centroid: config.max_points_per_centroid,
456        seed: config.seed,
457        n_samples_kmeans: config.n_samples_kmeans,
458        num_partitions: Some(k_update),
459    };
460
461    // Convert outliers to vector of single-token "documents" for compute_kmeans
462    let outlier_docs: Vec<Array2<f32>> = outlier_indices
463        .iter()
464        .map(|&idx| flat_embeddings.slice(s![idx..idx + 1, ..]).to_owned())
465        .collect();
466
467    let new_centroids = compute_kmeans(&outlier_docs, &kmeans_config)?;
468    let k_new = new_centroids.nrows();
469
470    // Concatenate centroids
471    let new_num_centroids = existing_centroids.nrows() + k_new;
472    let mut final_centroids = Array2::<f32>::zeros((new_num_centroids, dim));
473    final_centroids
474        .slice_mut(s![..existing_centroids.nrows(), ..])
475        .assign(&existing_centroids);
476    final_centroids
477        .slice_mut(s![existing_centroids.nrows().., ..])
478        .assign(&new_centroids);
479
480    // Save updated centroids
481    final_centroids.write_npy(File::create(&centroids_path)?)?;
482
483    // Extend ivf_lengths.npy with zeros for new centroids
484    let ivf_lengths_path = index_path.join("ivf_lengths.npy");
485    if ivf_lengths_path.exists() {
486        let old_lengths: Array1<i32> = Array1::read_npy(File::open(&ivf_lengths_path)?)?;
487        let mut new_lengths = Array1::<i32>::zeros(new_num_centroids);
488        new_lengths
489            .slice_mut(s![..old_lengths.len()])
490            .assign(&old_lengths);
491        new_lengths.write_npy(File::create(&ivf_lengths_path)?)?;
492    }
493
494    // Update metadata.json num_partitions
495    let meta_path = index_path.join("metadata.json");
496    if meta_path.exists() {
497        let mut meta: serde_json::Value =
498            serde_json::from_reader(BufReader::new(File::open(&meta_path)?))?;
499
500        if let Some(obj) = meta.as_object_mut() {
501            obj.insert("num_partitions".to_string(), new_num_centroids.into());
502        }
503
504        serde_json::to_writer_pretty(BufWriter::new(File::create(&meta_path)?), &meta)?;
505    }
506
507    Ok(k_new)
508}
509
510// ============================================================================
511// Low-Level Index Update
512// ============================================================================
513
514/// Update an existing index with new documents.
515///
516/// # Arguments
517///
518/// * `embeddings` - List of new document embeddings, each of shape `[num_tokens, dim]`
519/// * `index_path` - Path to the existing index directory
520/// * `codec` - The loaded ResidualCodec for compression
521/// * `batch_size` - Optional batch size for processing (default: 50,000)
522/// * `update_threshold` - Whether to update the cluster threshold
523///
524/// # Returns
525///
526/// The number of new documents added
527pub fn update_index(
528    embeddings: &[Array2<f32>],
529    index_path: &str,
530    codec: &ResidualCodec,
531    batch_size: Option<usize>,
532    update_threshold: bool,
533) -> Result<usize> {
534    let batch_size = batch_size.unwrap_or(DEFAULT_BATCH_SIZE);
535    let index_dir = Path::new(index_path);
536
537    // Load existing metadata (infers num_documents from doclens if not present)
538    let metadata_path = index_dir.join("metadata.json");
539    let metadata = Metadata::load_from_path(index_dir)?;
540
541    let num_existing_chunks = metadata.num_chunks;
542    let old_num_documents = metadata.num_documents;
543    let old_total_embeddings = metadata.num_embeddings;
544    let num_centroids = codec.num_centroids();
545    let embedding_dim = codec.embedding_dim();
546    let nbits = metadata.nbits;
547
548    // Determine starting chunk index
549    let mut start_chunk_idx = num_existing_chunks;
550    let mut append_to_last = false;
551    let mut current_emb_offset = old_total_embeddings;
552
553    // Check if we should append to the last chunk (if it has < 2000 documents)
554    if start_chunk_idx > 0 {
555        let last_idx = start_chunk_idx - 1;
556        let last_meta_path = index_dir.join(format!("{}.metadata.json", last_idx));
557
558        if last_meta_path.exists() {
559            let last_meta: serde_json::Value =
560                serde_json::from_reader(BufReader::new(File::open(&last_meta_path).map_err(
561                    |e| Error::IndexLoad(format!("Failed to open chunk metadata: {}", e)),
562                )?))?;
563
564            if let Some(nd) = last_meta.get("num_documents").and_then(|x| x.as_u64()) {
565                if nd < 2000 {
566                    start_chunk_idx = last_idx;
567                    append_to_last = true;
568
569                    if let Some(off) = last_meta.get("embedding_offset").and_then(|x| x.as_u64()) {
570                        current_emb_offset = off as usize;
571                    } else {
572                        let embs_in_last = last_meta
573                            .get("num_embeddings")
574                            .and_then(|x| x.as_u64())
575                            .unwrap_or(0) as usize;
576                        current_emb_offset = old_total_embeddings - embs_in_last;
577                    }
578                }
579            }
580        }
581    }
582
583    // Process new documents
584    let num_new_documents = embeddings.len();
585    let n_new_chunks = (num_new_documents as f64 / batch_size as f64).ceil() as usize;
586
587    let mut new_codes_accumulated: Vec<Vec<usize>> = Vec::new();
588    let mut new_doclens_accumulated: Vec<i64> = Vec::new();
589    let mut all_residual_norms: Vec<f32> = Vec::new();
590
591    let packed_dim = embedding_dim * nbits / 8;
592
593    for i in 0..n_new_chunks {
594        let global_chunk_idx = start_chunk_idx + i;
595        let chk_offset = i * batch_size;
596        let chk_end = (chk_offset + batch_size).min(num_new_documents);
597        let chunk_docs = &embeddings[chk_offset..chk_end];
598
599        // Collect document lengths
600        let mut chk_doclens: Vec<i64> = chunk_docs.iter().map(|d| d.nrows() as i64).collect();
601        let total_tokens: usize = chk_doclens.iter().sum::<i64>() as usize;
602
603        // Concatenate all embeddings in the chunk for batch processing
604        let mut batch_embeddings = ndarray::Array2::<f32>::zeros((total_tokens, embedding_dim));
605        let mut offset = 0;
606        for doc in chunk_docs {
607            let n = doc.nrows();
608            batch_embeddings
609                .slice_mut(s![offset..offset + n, ..])
610                .assign(doc);
611            offset += n;
612        }
613
614        // BATCH: Compress all embeddings at once
615        let batch_codes = codec.compress_into_codes(&batch_embeddings);
616
617        // BATCH: Compute residuals using parallel subtraction
618        let mut batch_residuals = batch_embeddings;
619        {
620            let centroids = &codec.centroids;
621            batch_residuals
622                .axis_iter_mut(Axis(0))
623                .into_par_iter()
624                .zip(batch_codes.as_slice().unwrap().par_iter())
625                .for_each(|(mut row, &code)| {
626                    let centroid = centroids.row(code);
627                    row.iter_mut()
628                        .zip(centroid.iter())
629                        .for_each(|(r, c)| *r -= c);
630                });
631        }
632
633        // Collect residual norms if updating threshold
634        if update_threshold {
635            for row in batch_residuals.axis_iter(Axis(0)) {
636                let norm = row.dot(&row).sqrt();
637                all_residual_norms.push(norm);
638            }
639        }
640
641        // BATCH: Quantize all residuals at once
642        let batch_packed = codec.quantize_residuals(&batch_residuals)?;
643
644        // Convert to lists for chunk saving
645        let mut chk_codes_list: Vec<usize> = batch_codes.iter().copied().collect();
646        let mut chk_residuals_list: Vec<u8> = batch_packed.iter().copied().collect();
647
648        // Split codes back into per-document arrays for IVF building
649        let mut code_offset = 0;
650        for &len in &chk_doclens {
651            let len_usize = len as usize;
652            let codes: Vec<usize> = batch_codes
653                .slice(s![code_offset..code_offset + len_usize])
654                .iter()
655                .copied()
656                .collect();
657            new_codes_accumulated.push(codes);
658            new_doclens_accumulated.push(len);
659            code_offset += len_usize;
660        }
661
662        // Handle appending to last chunk
663        if i == 0 && append_to_last {
664            use ndarray_npy::ReadNpyExt;
665
666            let old_doclens_path = index_dir.join(format!("doclens.{}.json", global_chunk_idx));
667
668            if old_doclens_path.exists() {
669                let old_doclens: Vec<i64> =
670                    serde_json::from_reader(BufReader::new(File::open(&old_doclens_path)?))?;
671
672                let old_codes_path = index_dir.join(format!("{}.codes.npy", global_chunk_idx));
673                let old_residuals_path =
674                    index_dir.join(format!("{}.residuals.npy", global_chunk_idx));
675
676                let old_codes: Array1<i64> = Array1::read_npy(File::open(&old_codes_path)?)?;
677                let old_residuals: Array2<u8> = Array2::read_npy(File::open(&old_residuals_path)?)?;
678
679                // Prepend old data
680                let mut combined_codes: Vec<usize> =
681                    old_codes.iter().map(|&x| x as usize).collect();
682                combined_codes.extend(chk_codes_list);
683                chk_codes_list = combined_codes;
684
685                let mut combined_residuals: Vec<u8> = old_residuals.iter().copied().collect();
686                combined_residuals.extend(chk_residuals_list);
687                chk_residuals_list = combined_residuals;
688
689                let mut combined_doclens = old_doclens;
690                combined_doclens.extend(chk_doclens);
691                chk_doclens = combined_doclens;
692            }
693        }
694
695        // Save chunk data
696        {
697            use ndarray_npy::WriteNpyExt;
698
699            let codes_arr: Array1<i64> = chk_codes_list.iter().map(|&x| x as i64).collect();
700            let codes_path = index_dir.join(format!("{}.codes.npy", global_chunk_idx));
701            codes_arr.write_npy(File::create(&codes_path)?)?;
702
703            let num_tokens = chk_codes_list.len();
704            let residuals_arr =
705                Array2::from_shape_vec((num_tokens, packed_dim), chk_residuals_list)
706                    .map_err(|e| Error::Shape(format!("Failed to reshape residuals: {}", e)))?;
707            let residuals_path = index_dir.join(format!("{}.residuals.npy", global_chunk_idx));
708            residuals_arr.write_npy(File::create(&residuals_path)?)?;
709        }
710
711        // Save doclens
712        let doclens_path = index_dir.join(format!("doclens.{}.json", global_chunk_idx));
713        serde_json::to_writer(BufWriter::new(File::create(&doclens_path)?), &chk_doclens)?;
714
715        // Save chunk metadata
716        let chk_meta = serde_json::json!({
717            "num_documents": chk_doclens.len(),
718            "num_embeddings": chk_codes_list.len(),
719            "embedding_offset": current_emb_offset,
720        });
721        current_emb_offset += chk_codes_list.len();
722
723        let meta_path = index_dir.join(format!("{}.metadata.json", global_chunk_idx));
724        serde_json::to_writer_pretty(BufWriter::new(File::create(&meta_path)?), &chk_meta)?;
725    }
726
727    // Update cluster threshold if requested
728    if update_threshold && !all_residual_norms.is_empty() {
729        let norms = Array1::from_vec(all_residual_norms);
730        update_cluster_threshold(index_dir, &norms, old_total_embeddings)?;
731    }
732
733    // Build new partial IVF
734    let mut partition_pids_map: HashMap<usize, Vec<i64>> = HashMap::new();
735    let mut pid_counter = old_num_documents as i64;
736
737    for doc_codes in &new_codes_accumulated {
738        for &code in doc_codes {
739            partition_pids_map
740                .entry(code)
741                .or_default()
742                .push(pid_counter);
743        }
744        pid_counter += 1;
745    }
746
747    // Load old IVF and merge
748    {
749        use ndarray_npy::{ReadNpyExt, WriteNpyExt};
750
751        let ivf_path = index_dir.join("ivf.npy");
752        let ivf_lengths_path = index_dir.join("ivf_lengths.npy");
753
754        let old_ivf: Array1<i64> = if ivf_path.exists() {
755            Array1::read_npy(File::open(&ivf_path)?)?
756        } else {
757            Array1::zeros(0)
758        };
759
760        let old_ivf_lengths: Array1<i32> = if ivf_lengths_path.exists() {
761            Array1::read_npy(File::open(&ivf_lengths_path)?)?
762        } else {
763            Array1::zeros(num_centroids)
764        };
765
766        // Compute old offsets
767        let mut old_offsets = vec![0i64];
768        for &len in old_ivf_lengths.iter() {
769            old_offsets.push(old_offsets.last().unwrap() + len as i64);
770        }
771
772        // Merge IVF
773        let mut new_ivf_data: Vec<i64> = Vec::new();
774        let mut new_ivf_lengths: Vec<i32> = Vec::with_capacity(num_centroids);
775
776        for centroid_id in 0..num_centroids {
777            // Get old PIDs for this centroid
778            let old_start = old_offsets[centroid_id] as usize;
779            let old_len = if centroid_id < old_ivf_lengths.len() {
780                old_ivf_lengths[centroid_id] as usize
781            } else {
782                0
783            };
784
785            let mut pids: Vec<i64> = if old_len > 0 && old_start + old_len <= old_ivf.len() {
786                old_ivf.slice(s![old_start..old_start + old_len]).to_vec()
787            } else {
788                Vec::new()
789            };
790
791            // Add new PIDs
792            if let Some(new_pids) = partition_pids_map.get(&centroid_id) {
793                pids.extend(new_pids);
794            }
795
796            // Deduplicate and sort
797            pids.sort_unstable();
798            pids.dedup();
799
800            new_ivf_lengths.push(pids.len() as i32);
801            new_ivf_data.extend(pids);
802        }
803
804        // Save updated IVF
805        let new_ivf = Array1::from_vec(new_ivf_data);
806        new_ivf.write_npy(File::create(&ivf_path)?)?;
807
808        let new_lengths = Array1::from_vec(new_ivf_lengths);
809        new_lengths.write_npy(File::create(&ivf_lengths_path)?)?;
810    }
811
812    // Update global metadata
813    let new_total_chunks = start_chunk_idx + n_new_chunks;
814    let new_tokens_count: i64 = new_doclens_accumulated.iter().sum();
815    let num_embeddings = old_total_embeddings + new_tokens_count as usize;
816    let total_num_documents = old_num_documents + num_new_documents;
817
818    let new_avg_doclen = if total_num_documents > 0 {
819        let old_sum = metadata.avg_doclen * old_num_documents as f64;
820        (old_sum + new_tokens_count as f64) / total_num_documents as f64
821    } else {
822        0.0
823    };
824
825    let new_metadata = Metadata {
826        num_chunks: new_total_chunks,
827        nbits,
828        num_partitions: num_centroids,
829        num_embeddings,
830        avg_doclen: new_avg_doclen,
831        num_documents: total_num_documents,
832        next_plaid_compatible: true,
833    };
834
835    serde_json::to_writer_pretty(BufWriter::new(File::create(&metadata_path)?), &new_metadata)?;
836
837    Ok(num_new_documents)
838}
839
840#[cfg(test)]
841mod tests {
842    use super::*;
843
844    #[test]
845    fn test_update_config_default() {
846        let config = UpdateConfig::default();
847        assert_eq!(config.batch_size, 50_000);
848        assert_eq!(config.buffer_size, 100);
849        assert_eq!(config.start_from_scratch, 999);
850    }
851
852    #[test]
853    fn test_find_outliers() {
854        // Create centroids at (0,0), (1,1)
855        let centroids = Array2::from_shape_vec((2, 2), vec![0.0, 0.0, 1.0, 1.0]).unwrap();
856
857        // Create embeddings: one close to (0,0), one close to (1,1), one far away at (5,5)
858        let embeddings =
859            Array2::from_shape_vec((3, 2), vec![0.1, 0.1, 0.9, 0.9, 5.0, 5.0]).unwrap();
860
861        // Threshold of 1.0 squared = 1.0
862        let outliers = find_outliers(&embeddings, &centroids, 1.0);
863
864        // Only the point at (5,5) should be an outlier
865        assert_eq!(outliers.len(), 1);
866        assert_eq!(outliers[0], 2);
867    }
868
869    #[test]
870    fn test_buffer_roundtrip() {
871        use tempfile::TempDir;
872
873        let dir = TempDir::new().unwrap();
874
875        // Create 3 documents with different numbers of embeddings
876        let embeddings = vec![
877            Array2::from_shape_vec((3, 4), (0..12).map(|x| x as f32).collect()).unwrap(),
878            Array2::from_shape_vec((2, 4), (12..20).map(|x| x as f32).collect()).unwrap(),
879            Array2::from_shape_vec((5, 4), (20..40).map(|x| x as f32).collect()).unwrap(),
880        ];
881
882        // Save buffer
883        save_buffer(dir.path(), &embeddings).unwrap();
884
885        // Load buffer and verify we get 3 documents, not 1
886        let loaded = load_buffer(dir.path()).unwrap();
887
888        assert_eq!(loaded.len(), 3, "Should have 3 documents, not 1");
889        assert_eq!(loaded[0].nrows(), 3, "First doc should have 3 rows");
890        assert_eq!(loaded[1].nrows(), 2, "Second doc should have 2 rows");
891        assert_eq!(loaded[2].nrows(), 5, "Third doc should have 5 rows");
892
893        // Verify content matches
894        assert_eq!(loaded[0], embeddings[0]);
895        assert_eq!(loaded[1], embeddings[1]);
896        assert_eq!(loaded[2], embeddings[2]);
897    }
898
899    #[test]
900    fn test_buffer_info_matches_buffer_len() {
901        use tempfile::TempDir;
902
903        let dir = TempDir::new().unwrap();
904
905        // Create 5 documents
906        let embeddings: Vec<Array2<f32>> = (0..5)
907            .map(|i| {
908                let rows = i + 2; // 2, 3, 4, 5, 6 rows
909                Array2::from_shape_fn((rows, 4), |(r, c)| (r * 4 + c) as f32)
910            })
911            .collect();
912
913        save_buffer(dir.path(), &embeddings).unwrap();
914
915        // Verify buffer_info.json matches actual document count
916        let info_count = load_buffer_info(dir.path()).unwrap();
917        let loaded = load_buffer(dir.path()).unwrap();
918
919        assert_eq!(info_count, 5, "buffer_info should report 5 docs");
920        assert_eq!(
921            loaded.len(),
922            5,
923            "load_buffer should return 5 docs to match buffer_info"
924        );
925    }
926}