laurus 0.10.0

Unified search library for lexical, vector, and semantic retrieval
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
//! Vector indexing module for building and maintaining vector indexes.
//!
//! This module handles all vector index construction, maintenance, and optimization:
//! - Building HNSW, Flat, and IVF indexes
//! - Text embedding generation
//! - Vector quantization and compression
//! - Index optimization and maintenance

pub(crate) mod alloc_bounds;
pub mod config;
pub mod factory;
pub mod field;
pub mod flat;
pub mod format;
pub mod hnsw;
pub mod io;
pub mod ivf;
pub mod multi_field;
pub mod pq_codebook;
pub mod pq_fastscan_avx2;
#[cfg(feature = "pq-fastscan")]
pub mod pq_fastscan_io;
pub mod pq_fastscan_neon;
pub mod pq_fastscan_storage;
pub mod pq_io;
pub mod pq_storage;
pub mod quantized_io;
pub mod quantized_segment;
pub mod quantized_storage;
pub mod rerank_sidecar;
pub mod rerank_storage;
pub mod segment;
pub mod segmented_field;
pub mod storage;
pub mod wal;

use std::sync::Arc;

use parking_lot::RwLock;

use crate::embedding::embedder::Embedder;
use crate::error::{LaurusError, Result};
use crate::storage::Storage;
use crate::vector::core::vector::Vector;
use crate::vector::index::config::{
    FlatIndexConfig, HnswIndexConfig, IvfIndexConfig, VectorIndexTypeConfig,
};
use crate::vector::reader::VectorIndexReader;
use crate::vector::search::searcher::VectorIndexSearcher;
use crate::vector::writer::VectorIndexWriter;

/// Trait for vector index implementations.
///
/// This trait defines the low-level interface for individual vector indexes
/// (Flat, HNSW, IVF, etc.). Each index type implements this trait to provide
/// reader/writer access and basic lifecycle management.
///
/// This is analogous to [`crate::lexical::index::LexicalIndex`] in the lexical module.
/// For high-level, document-centric operations, see
/// [`crate::vector::store::VectorStore`] which manages multiple
/// vector fields and handles document-level operations.
pub trait VectorIndex: Send + Sync + std::fmt::Debug {
    /// Get a reader for this index.
    ///
    /// Returns a reader that can be used to query the index.
    fn reader(&self) -> Result<Arc<dyn VectorIndexReader>>;

    /// Get a writer for this index.
    ///
    /// Returns a writer that can be used to add or update vectors.
    fn writer(&self) -> Result<Box<dyn VectorIndexWriter>>;

    /// Get the storage backend for this index.
    ///
    /// Returns a reference to the underlying storage.
    fn storage(&self) -> &Arc<dyn Storage>;

    /// Close the index and release resources.
    ///
    /// This should flush any pending writes and release all resources.
    /// Uses interior mutability for thread-safe access.
    fn close(&self) -> Result<()>;

    /// Check if the index is closed.
    ///
    /// Returns true if the index has been closed.
    fn is_closed(&self) -> bool;

    /// Get index statistics.
    ///
    /// Returns statistics about the index such as vector count, dimension, etc.
    fn stats(&self) -> Result<VectorIndexStats>;

    /// Optimize the index.
    ///
    /// Performs index optimization to improve query performance.
    /// Uses interior mutability for thread-safe access.
    fn optimize(&self) -> Result<()>;

    /// Refresh the index metadata from storage.
    ///
    /// Should be called after external writes (e.g., by a Writer) to ensure
    /// the index state is up-to-date.
    fn refresh(&self) -> Result<()> {
        Ok(())
    }

    /// Whether a store may keep its cached writer across `commit()` calls
    /// (Issue #572 / #864) instead of dropping it, so the first upsert after
    /// a commit does not reload the whole index from storage.
    ///
    /// Retention is sound only when **both** hold for this index type:
    ///
    /// 1. The writer's post-commit in-memory state is equivalent to the file
    ///    it just wrote (idempotent `finalize()`, non-consuming `write()`,
    ///    incremental graph/buffer reuse), so subsequent commits from the
    ///    retained writer produce the same bytes a freshly loaded writer
    ///    would.
    /// 2. Every disk mutation that bypasses the writer — compaction via
    ///    [`Self::maybe_auto_compact`], [`Self::optimize`] — invalidates the
    ///    store's writer cache, otherwise a stale retained writer would
    ///    rewrite physically reclaimed (deleted) vectors back into the index
    ///    with no deletion bitmap marking them.
    ///
    /// Defaults to `false` (the store drops the writer on commit, today's
    /// behavior); `HnswIndex` opts in after auditing both conditions.
    fn retain_writer_after_commit(&self) -> bool {
        false
    }

    /// Create a searcher tailored for this index implementation.
    ///
    /// Returns a boxed [`VectorIndexSearcher`] capable of executing search/count operations.
    fn searcher(&self) -> Result<Box<dyn VectorIndexSearcher>>;

    /// Get the embedder associated with this index.
    ///
    /// Returns the embedder used to convert documents to vectors.
    fn embedder(&self) -> Arc<dyn Embedder>;

    /// Get the last processed WAL sequence number.
    fn last_wal_seq(&self) -> u64 {
        0
    }

    /// Set the last processed WAL sequence number.
    fn set_last_wal_seq(&self, _seq: u64) -> Result<()> {
        Ok(())
    }

    /// Whether this index supports logical (soft) deletion.
    ///
    /// When `true`, callers should route deletions through
    /// [`Self::soft_delete_document`] (mark a deletion bitmap, filtered at
    /// search time, physically reclaimed by [`Self::optimize`]) instead of the
    /// writer-side deletion path that rebuilds the whole index. Defaults to
    /// `false`, so existing index types keep their current behaviour.
    fn supports_soft_delete(&self) -> bool {
        false
    }

    /// Logically delete a document by its internal ID (Issue #624).
    ///
    /// Marks `doc_id` in the index's deletion bitmap so that subsequent
    /// searches exclude it, without rebuilding the underlying graph. The
    /// vector is physically removed only by a later [`Self::optimize`]
    /// (compaction). Implementations must persist durably no later than the
    /// next [`Self::persist_deletions`] call.
    ///
    /// # Arguments
    ///
    /// * `doc_id` - The internal document ID to mark as deleted.
    ///
    /// # Errors
    ///
    /// The default implementation returns an error because soft deletion is
    /// unsupported; callers should gate on [`Self::supports_soft_delete`].
    fn soft_delete_document(&self, _doc_id: u64) -> Result<()> {
        Err(crate::error::LaurusError::InvalidOperation(
            "soft deletion is not supported by this index".to_string(),
        ))
    }

    /// Persist any pending logical deletions to storage (Issue #624).
    ///
    /// Called by the store on commit so the deletion bitmap survives restarts.
    /// Defaults to a no-op for indexes that do not buffer soft deletions.
    ///
    /// # Errors
    ///
    /// Returns an error if writing the deletion bitmap to storage fails.
    fn persist_deletions(&self) -> Result<()> {
        Ok(())
    }

    /// Compact the index automatically if the deletion ratio warrants it (Issue
    /// #782).
    ///
    /// Called by the store after a commit. Implementations that buffer logical
    /// deletions (Issue #624) should physically reclaim them via
    /// [`Self::optimize`] when the deleted fraction crosses their configured
    /// threshold, so tombstones do not accumulate unboundedly. Returns whether
    /// a compaction actually ran. Defaults to a no-op (`Ok(false)`).
    ///
    /// # Errors
    ///
    /// Returns an error if the triggered compaction fails.
    fn maybe_auto_compact(&self) -> Result<bool> {
        Ok(false)
    }

    /// Whether this index supports adding/removing vector fields after
    /// construction (Issue [#948](https://github.com/mosuka/laurus/issues/948)).
    ///
    /// Defaults to `false`; only
    /// [`MultiFieldVectorIndex`](crate::vector::index::multi_field::MultiFieldVectorIndex)
    /// (one independent sub-index per field) can grow a genuinely new field
    /// without disturbing the others. A monolithic single-field index
    /// (Flat/HNSW/IVF `open_or_create`) has no field boundary to add to.
    fn supports_dynamic_fields(&self) -> bool {
        false
    }

    /// Add a new vector field to this index (Issue #948: dynamic schema
    /// evolution).
    ///
    /// # Errors
    ///
    /// The default implementation always errors; callers must gate on
    /// [`Self::supports_dynamic_fields`] first.
    fn add_field(&self, _name: &str, _config: VectorIndexTypeConfig) -> Result<()> {
        Err(LaurusError::InvalidOperation(
            "this index type does not support adding fields dynamically".to_string(),
        ))
    }

    /// Remove a vector field from this index's routing (Issue #948: dynamic
    /// schema evolution). Does NOT delete the field's on-disk data --
    /// mirrors [`crate::vector::store::VectorStore`]'s existing
    /// "unregister only" contract for `delete_field`, so the data can be
    /// recovered by re-adding the field with the same name.
    ///
    /// Defaults to `Ok(())` (a no-op): index types without field boundaries
    /// have nothing to unregister.
    fn remove_field(&self, _name: &str) -> Result<()> {
        Ok(())
    }

    /// The configured vector dimension of every field, keyed by field name
    /// (Issue #948: multi-field stats/routing need per-field dimensions
    /// without paying for a full `reader()`/`stats()` round trip).
    ///
    /// Defaults to empty: index types without field boundaries report their
    /// single dimension through [`Self::stats`] instead.
    fn field_dimensions(&self) -> std::collections::BTreeMap<String, usize> {
        std::collections::BTreeMap::new()
    }
}

/// Statistics about a vector index.
#[derive(Debug, Clone)]
pub struct VectorIndexStats {
    /// Number of vectors in the index.
    pub vector_count: u64,

    /// Dimension of vectors.
    pub dimension: usize,

    /// Total size of the index in bytes.
    pub total_size: u64,

    /// Number of deleted vectors.
    pub deleted_count: u64,

    /// Last modified time (seconds since epoch).
    pub last_modified: u64,
}

// Add imports for readers
use crate::vector::index::flat::reader::FlatVectorIndexReader;
use crate::vector::index::hnsw::reader::HnswIndexReader;
use crate::vector::index::ivf::reader::IvfIndexReader;

/// Internal implementation for managing vector index lifecycle.
///
/// This structure wraps a vector index writer and manages its state.
/// For most use cases, prefer using `VectorStore` which provides a higher-level interface.
///
/// # Note
///
/// This is an internal implementation detail. The public API for vector indexes
/// is defined by the `VectorIndex` trait and `VectorIndexFactory`.
pub struct ManagedVectorIndex {
    config: VectorIndexTypeConfig,
    builder: Arc<RwLock<Box<dyn VectorIndexWriter>>>,
    is_finalized: Arc<RwLock<bool>>,
    storage: Option<Arc<dyn Storage>>,
    index_path: Arc<RwLock<Option<String>>>,
}

impl ManagedVectorIndex {
    /// Create a new vector index with the given configuration and storage.
    ///
    /// # Arguments
    ///
    /// * `config` - Vector index type configuration (Flat, HNSW, or IVF)
    /// * `storage` - Storage backend (MemoryStorage, FileStorage, etc.)
    /// * `path` - Base path/name for the index files
    pub fn new(
        mut config: VectorIndexTypeConfig,
        storage: Arc<dyn Storage>,
        path: impl Into<String>,
    ) -> Result<Self> {
        let path = path.into();
        // Resolve a configured shared PQ codebook (Issue #631) before
        // constructing the writer -- mirrors `factory.rs`'s
        // `dispatch_hnsw` resolution point for the `VectorIndexFactory`
        // path; this constructor is the other (bench/direct-use) path
        // into an HNSW writer.
        if let VectorIndexTypeConfig::HNSW(hnsw_config) = &mut config {
            hnsw_config.resolve_pq_codebook(storage.as_ref())?;
        }
        // Create builder based on config type
        let builder: Box<dyn VectorIndexWriter> = match &config {
            VectorIndexTypeConfig::Flat(flat_config) => {
                let writer_config = Self::default_writer_config();
                Box::new(flat::writer::FlatIndexWriter::with_storage(
                    flat_config.clone(),
                    writer_config,
                    path.clone(),
                    storage.clone(),
                )?)
            }
            VectorIndexTypeConfig::HNSW(hnsw_config) => {
                let writer_config = Self::default_writer_config();
                Box::new(hnsw::writer::HnswIndexWriter::with_storage(
                    hnsw_config.clone(),
                    writer_config,
                    path.clone(),
                    storage.clone(),
                )?)
            }
            VectorIndexTypeConfig::IVF(ivf_config) => {
                let writer_config = Self::default_writer_config();
                Box::new(ivf::writer::IvfIndexWriter::with_storage(
                    ivf_config.clone(),
                    writer_config,
                    path.clone(),
                    storage.clone(),
                )?)
            }
        };

        Ok(Self {
            config,
            builder: Arc::new(RwLock::new(builder)),
            is_finalized: Arc::new(RwLock::new(false)),
            storage: Some(storage),
            index_path: Arc::new(RwLock::new(Some(path))),
        })
    }

    /// Helper to create a default writer config.
    fn default_writer_config() -> crate::vector::writer::VectorIndexWriterConfig {
        crate::vector::writer::VectorIndexWriterConfig::default()
    }

    /// Add vectors to the index.
    pub fn add_vectors(&mut self, vectors: Vec<(u64, String, Vector)>) -> Result<()> {
        let finalized = *self.is_finalized.read();
        if finalized {
            return Err(LaurusError::InvalidOperation(
                "Cannot add vectors to finalized index".to_string(),
            ));
        }

        let mut builder = self.builder.write();
        builder.add_vectors(vectors)?;
        Ok(())
    }

    /// Finalize the index construction.
    pub fn finalize(&mut self) -> Result<()> {
        let mut builder = self.builder.write();
        builder.finalize()?;
        *self.is_finalized.write() = true;
        Ok(())
    }

    /// Delete a document by its ID.
    pub fn delete_document(&self, doc_id: u64) -> Result<()> {
        let mut builder = self.builder.write();
        builder.delete_document(doc_id)
    }

    /// Get the configuration.
    pub fn config(&self) -> &VectorIndexTypeConfig {
        &self.config
    }

    /// Get build progress (0.0 to 1.0).
    pub fn progress(&self) -> f32 {
        let builder = self.builder.read();
        builder.progress()
    }

    /// Get estimated memory usage.
    pub fn estimated_memory_usage(&self) -> usize {
        let builder = self.builder.read();
        builder.estimated_memory_usage()
    }

    /// Check if the index is finalized.
    pub fn is_finalized(&self) -> bool {
        *self.is_finalized.read()
    }

    /// Get vectors from this index.
    /// Returns a copy of all vectors stored in the index.
    pub fn vectors(&self) -> Result<Vec<(u64, String, Vector)>> {
        let finalized = *self.is_finalized.read();
        if !finalized {
            return Err(LaurusError::InvalidOperation(
                "Index must be finalized before accessing vectors".to_string(),
            ));
        }

        let builder = self.builder.read();
        Ok(builder.vectors().to_vec())
    }

    /// Write the index to storage.
    /// The index must be finalized before calling this method.
    pub fn write(&self) -> Result<()> {
        let finalized = *self.is_finalized.read();
        if !finalized {
            return Err(LaurusError::InvalidOperation(
                "Index must be finalized before writing".to_string(),
            ));
        }

        let builder = self.builder.read();
        if !builder.has_storage() {
            return Err(LaurusError::InvalidOperation(
                "Index was not created with storage support".to_string(),
            ));
        }

        builder.write()?;
        Ok(())
    }

    /// Check if this index has storage configured.
    pub fn has_storage(&self) -> bool {
        self.storage.is_some()
    }

    /// Create a reader for this index.
    /// Returns a boxed VectorIndexReader that can be used for searching.
    pub fn reader(&self) -> Result<Arc<dyn crate::vector::reader::VectorIndexReader>> {
        let finalized = *self.is_finalized.read();
        if !finalized {
            return Err(LaurusError::InvalidOperation(
                "Index must be finalized before creating a reader".to_string(),
            ));
        }

        // Try loading from storage if available (index was written via write()).
        if let Some(storage) = &self.storage {
            let path_guard = self.index_path.read();
            if let Some(path) = &*path_guard {
                let storage_result: Result<Arc<dyn crate::vector::reader::VectorIndexReader>> =
                    match &self.config {
                        VectorIndexTypeConfig::Flat(c) => {
                            FlatVectorIndexReader::load(storage.clone(), path, c.distance_metric)
                                .map(|r| Arc::new(r) as _)
                        }
                        VectorIndexTypeConfig::HNSW(c) => {
                            HnswIndexReader::load(storage.clone(), path, c.distance_metric)
                                .map(|r| Arc::new(r) as _)
                        }
                        VectorIndexTypeConfig::IVF(c) => {
                            IvfIndexReader::load(storage.clone(), path, c.distance_metric)
                                .map(|r| Arc::new(r) as _)
                        }
                    };
                if let Ok(reader) = storage_result {
                    return Ok(reader);
                }
                // Fall through to in-memory reader if storage load fails
                // (e.g., finalize() was called but write() was not).
            }
        }

        // Create reader from in-memory vectors held by the writer.
        let vectors = self.vectors()?;
        let reader = crate::vector::reader::SimpleVectorReader::new(
            vectors,
            self.config.dimension(),
            self.config.distance_metric(),
        )?;
        Ok(Arc::new(reader))
    }
}