use async_trait::async_trait;
use crate::error::Result;
use crate::vector::core::vector::Vector;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VectorIndexWriterConfig {
pub max_buffered_vectors: usize,
pub max_buffer_memory: usize,
pub segment_prefix: String,
pub parallel_build: bool,
pub memory_limit: Option<usize>,
pub auto_flush_threshold: f32,
}
impl Default for VectorIndexWriterConfig {
fn default() -> Self {
Self {
max_buffered_vectors: 10000,
max_buffer_memory: 512 * 1024 * 1024, segment_prefix: "segment".to_string(),
parallel_build: true,
memory_limit: None,
auto_flush_threshold: 0.9,
}
}
}
#[async_trait]
pub trait VectorIndexWriter: Send + Sync + std::fmt::Debug {
fn next_vector_id(&self) -> u64;
fn build(&mut self, vectors: Vec<(u64, String, Vector)>) -> Result<()>;
fn add_vectors(&mut self, vectors: Vec<(u64, String, Vector)>) -> Result<()>;
fn finalize(&mut self) -> Result<()>;
fn progress(&self) -> f32;
fn estimated_memory_usage(&self) -> usize;
fn vectors(&self) -> &[(u64, String, Vector)];
fn write(&self) -> Result<()>;
fn has_storage(&self) -> bool;
fn delete_document(&mut self, doc_id: u64) -> Result<()>;
fn delete_documents(&mut self, field: &str, value: &str) -> Result<usize> {
let _ = field;
let _ = value;
Err(crate::error::LaurusError::InvalidOperation(
"delete_documents not implemented for this writer".to_string(),
))
}
fn commit(&mut self) -> Result<()> {
self.finalize()?;
self.write()
}
async fn add_value(
&mut self,
doc_id: u64,
field_name: String,
value: crate::data::DataValue,
) -> Result<()> {
if let crate::data::DataValue::Vector(v) = value {
self.add_vectors(vec![(doc_id, field_name, Vector::new(v))])
} else {
Err(crate::error::LaurusError::invalid_argument(
"Auto-embedding not supported by this index writer. Wrap it in EmbeddingVectorIndexWriter.",
))
}
}
fn rollback(&mut self) -> Result<()>;
fn pending_docs(&self) -> u64;
fn close(&mut self) -> Result<()>;
fn is_closed(&self) -> bool;
fn optimize(&mut self) -> Result<()> {
Ok(())
}
fn build_reader(&self) -> Result<std::sync::Arc<dyn crate::vector::reader::VectorIndexReader>>;
}