lumosai_vector_memory/
lib.rs

1//! # Lumos Vector Memory Storage
2//!
3//! High-performance in-memory vector storage implementation for Lumos.
4//! This implementation provides fast vector operations with support for
5//! multiple similarity metrics and complex filtering.
6//!
7//! ## Features
8//!
9//! - **High Performance**: Optimized for speed with minimal memory overhead
10//! - **Multiple Metrics**: Support for cosine, euclidean, and dot product similarity
11//! - **Advanced Filtering**: Complex filter conditions with AND/OR/NOT logic
12//! - **Thread Safe**: Full async support with efficient locking
13//! - **Memory Efficient**: Configurable capacity and memory management
14//!
15//! ## Example
16//!
17//! ```rust
18//! use lumosai_vector_memory::MemoryVectorStorage;
19//! use lumosai_vector_core::prelude::*;
20//!
21//! #[tokio::main]
22//! async fn main() -> lumosai_vector_core::Result<()> {
23//!     // Create storage with initial capacity
24//!     let storage = MemoryVectorStorage::with_capacity(1000).await?;
25//!
26//!     // Create an index
27//!     let config = IndexConfig::new("documents", 384)
28//!         .with_metric(SimilarityMetric::Cosine);
29//!     storage.create_index(config).await?;
30//!
31//!     // Insert documents
32//!     let docs = vec![
33//!         Document::new("doc1", "Hello world")
34//!             .with_embedding(vec![0.1; 384])  // 384-dimensional vector
35//!             .with_metadata("type", "greeting"),
36//!     ];
37//!     storage.upsert_documents("documents", docs).await?;
38//!
39//!     // Search for similar documents
40//!     let request = SearchRequest::new("documents", vec![0.1; 384])
41//!         .with_top_k(5)
42//!         .with_include_metadata(true);
43//!
44//!     let response = storage.search(request).await?;
45//!     println!("Found {} results", response.results.len());
46//!
47//!     Ok(())
48//! }
49//! ```
50#![allow(dead_code, unused_imports, unused_variables, unused_mut)]
51#![allow(non_camel_case_types, ambiguous_glob_reexports, hidden_glob_reexports)]
52#![allow(unexpected_cfgs, unused_assignments)]
53
54use std::collections::HashMap;
55use std::sync::Arc;
56use tokio::sync::RwLock;
57use async_trait::async_trait;
58use uuid::Uuid;
59
60use lumosai_vector_core::prelude::*;
61
62mod storage;
63mod index;
64mod utils;
65
66pub use storage::MemoryVectorStorage;
67
68// Type alias for compatibility
69pub type MemoryVectorStore = MemoryVectorStorage;
70pub use index::MemoryIndex;
71
72/// Memory storage configuration
73#[derive(Debug, Clone)]
74pub struct MemoryConfig {
75    /// Initial capacity for indexes
76    pub initial_capacity: usize,
77    /// Maximum number of vectors per index
78    pub max_vectors_per_index: Option<usize>,
79    /// Enable approximate search for large datasets
80    pub enable_approximate: bool,
81    /// Memory usage threshold for triggering cleanup
82    pub memory_threshold_mb: Option<usize>,
83}
84
85impl Default for MemoryConfig {
86    fn default() -> Self {
87        Self {
88            initial_capacity: 1000,
89            max_vectors_per_index: None,
90            enable_approximate: false,
91            memory_threshold_mb: None,
92        }
93    }
94}
95
96impl MemoryConfig {
97    /// Create a new memory configuration
98    pub fn new() -> Self {
99        Self::default()
100    }
101    
102    /// Set initial capacity
103    pub fn with_initial_capacity(mut self, capacity: usize) -> Self {
104        self.initial_capacity = capacity;
105        self
106    }
107    
108    /// Set maximum vectors per index
109    pub fn with_max_vectors(mut self, max_vectors: usize) -> Self {
110        self.max_vectors_per_index = Some(max_vectors);
111        self
112    }
113    
114    /// Enable approximate search
115    pub fn with_approximate_search(mut self, enable: bool) -> Self {
116        self.enable_approximate = enable;
117        self
118    }
119    
120    /// Set memory threshold
121    pub fn with_memory_threshold(mut self, threshold_mb: usize) -> Self {
122        self.memory_threshold_mb = Some(threshold_mb);
123        self
124    }
125}