* chore: Add proptest regression data from test run Records edge cases found during property testing that cause integer overflow failures. These will help reproduce and fix the boundary condition bugs in distance calculations. * fix: Resolve property test failures with overflow handling - Fix ScalarQuantized::distance() i16 overflow: use i32 for diff*diff (255*255=65025 overflows i16 max of 32767) - Fix ScalarQuantized::quantize() division by zero when all values equal (handle scale=0 case by defaulting to 1.0) - Bound vector_strategy() to -1000..1000 range to prevent overflow in distance calculations with extreme float values All 177 tests now pass in ruvector-core. * fix(cli): Resolve short option conflicts in clap argument definitions - Change --dimensions from -d to -D to avoid conflict with global --debug - Change --db from -d to -b across all subcommands (Insert, Search, Info, Benchmark, Export, Import) to avoid conflict with global --debug Fixes clap panic in debug builds: "Short option names must be unique" Note: 4 CLI integration tests still fail due to pre-existing issue where VectorDB doesn't persist its configuration to disk. When reopening a database, dimensions are read from config defaults (384) instead of from the stored database metadata. This is an architectural issue requiring VectorDB changes to implement proper metadata persistence. * feat(core): Add database configuration persistence and fix CLI test - Add CONFIG_TABLE to storage.rs for persisting DbOptions - Implement save_config() and load_config() methods in VectorStorage - Modify VectorDB::new() to load stored config for existing databases - Fix dimension mismatch by recreating storage with correct dimensions - Fix test_error_handling CLI test to use /dev/null/db.db path This ensures database settings (dimensions, distance metric, HNSW config, quantization) are preserved across restarts. Previously opening an existing database would use default settings instead of stored configuration. * fix(ruvLLM): Guard against edge cases in HNSW and softmax - memory.rs: Fix random_level() to handle r=0 (ln(0) = -inf) - memory.rs: Fix ml calculation when hnsw_m=1 (ln(1) = 0 → div by zero) - router.rs: Add division-by-zero guard in softmax for larger arrays These edge cases could cause undefined behavior or NaN propagation. --------- Co-authored-by: Claude <noreply@anthropic.com> |
||
|---|---|---|
| .. | ||
| benches | ||
| src | ||
| tests | ||
| Cargo.toml | ||
| README.md | ||
Ruvector Core
High-performance Rust vector database engine with HNSW indexing, quantization, and SIMD optimizations.
ruvector-core is the foundational Rust library powering Ruvector—a next-generation vector database built for extreme performance and universal deployment. This crate provides the core vector database engine with state-of-the-art algorithms optimized for modern hardware.
🌟 Why Ruvector Core?
- ⚡ Blazing Fast: <0.5ms p50 query latency with HNSW indexing
- 🧠 Memory Efficient: 4-32x compression via quantization techniques
- 🎯 High Accuracy: 95%+ recall with HNSW + Product Quantization
- 🚀 SIMD Accelerated: Hardware-optimized distance calculations using SimSIMD
- 🔧 Zero Dependencies: Minimal external dependencies, pure Rust implementation
- 📦 Production Ready: Battle-tested algorithms with comprehensive benchmarks
🚀 Features
Core Capabilities
- HNSW Indexing: Hierarchical Navigable Small World graphs for O(log n) approximate nearest neighbor search
- Multiple Distance Metrics: Euclidean, Cosine, Dot Product, Manhattan
- Advanced Quantization: Scalar (4x), Product (8-32x), and Binary (32x) quantization
- SIMD Optimizations: Hardware-accelerated distance calculations via
simsimd - Zero-Copy I/O: Memory-mapped storage for instant loading
- Concurrent Operations: Lock-free data structures and parallel batch processing
- Flexible Storage: Persistent storage with
redband memory-mapped files
Advanced Features
- Hybrid Search: Combine dense vector search with sparse BM25 text search
- Filtered Search: Apply metadata filters during vector search
- MMR Diversification: Maximal Marginal Relevance for diverse result sets
- Conformal Prediction: Uncertainty quantification for search results
- Product Quantization: Memory-efficient vector compression with high accuracy
- Cache Optimization: Multi-level caching for improved performance
- Lock-Free Indexing: High-concurrency operations without blocking
📦 Installation
Add ruvector-core to your Cargo.toml:
[dependencies]
ruvector-core = "0.1.0"
Feature Flags
[dependencies]
ruvector-core = { version = "0.1.0", features = ["simd", "uuid-support"] }
Available features:
simd(default): Enable SIMD-optimized distance calculationsuuid-support(default): Enable UUID generation for vector IDs
⚡ Quick Start
Basic Usage
use ruvector_core::{VectorDB, DbOptions, VectorEntry, SearchQuery, DistanceMetric};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new vector database
let mut options = DbOptions::default();
options.dimensions = 384; // Vector dimensions
options.storage_path = "./my_vectors.db".to_string();
options.distance_metric = DistanceMetric::Cosine;
let db = VectorDB::new(options)?;
// Insert vectors
db.insert(VectorEntry {
id: Some("doc1".to_string()),
vector: vec![0.1, 0.2, 0.3, /* ... 384 dimensions */],
metadata: None,
})?;
db.insert(VectorEntry {
id: Some("doc2".to_string()),
vector: vec![0.4, 0.5, 0.6, /* ... 384 dimensions */],
metadata: None,
})?;
// Search for similar vectors
let results = db.search(SearchQuery {
vector: vec![0.1, 0.2, 0.3, /* ... 384 dimensions */],
k: 10, // Return top 10 results
filter: None,
ef_search: None,
})?;
for result in results {
println!("ID: {}, Score: {}", result.id, result.score);
}
Ok(())
}
Batch Operations
use ruvector_core::{VectorDB, VectorEntry};
// Insert multiple vectors efficiently
let entries = vec![
VectorEntry {
id: Some("doc1".to_string()),
vector: vec![0.1, 0.2, 0.3],
metadata: None,
},
VectorEntry {
id: Some("doc2".to_string()),
vector: vec![0.4, 0.5, 0.6],
metadata: None,
},
];
let ids = db.insert_batch(entries)?;
println!("Inserted {} vectors", ids.len());
With Metadata Filtering
use std::collections::HashMap;
use serde_json::json;
// Insert with metadata
db.insert(VectorEntry {
id: Some("product1".to_string()),
vector: vec![0.1, 0.2, 0.3],
metadata: Some(HashMap::from([
("category".to_string(), json!("electronics")),
("price".to_string(), json!(299.99)),
])),
})?;
// Search with metadata filter
let results = db.search(SearchQuery {
vector: vec![0.1, 0.2, 0.3],
k: 10,
filter: Some(HashMap::from([
("category".to_string(), json!("electronics")),
])),
ef_search: None,
})?;
HNSW Configuration
use ruvector_core::{DbOptions, HnswConfig, DistanceMetric};
let mut options = DbOptions::default();
options.dimensions = 384;
options.distance_metric = DistanceMetric::Cosine;
// Configure HNSW index parameters
options.hnsw_config = Some(HnswConfig {
m: 32, // Connections per layer (16-64 typical)
ef_construction: 200, // Build-time accuracy (100-500 typical)
ef_search: 100, // Search-time accuracy (50-200 typical)
max_elements: 10_000_000, // Maximum vectors
});
let db = VectorDB::new(options)?;
Quantization
use ruvector_core::{DbOptions, QuantizationConfig};
let mut options = DbOptions::default();
options.dimensions = 384;
// Enable scalar quantization (4x compression)
options.quantization = Some(QuantizationConfig::Scalar);
// Or product quantization (8-32x compression)
options.quantization = Some(QuantizationConfig::Product {
subspaces: 8, // Number of subspaces
k: 256, // Codebook size
});
let db = VectorDB::new(options)?;
📊 API Overview
Core Types
// Main database interface
pub struct VectorDB { /* ... */ }
// Vector entry with optional ID and metadata
pub struct VectorEntry {
pub id: Option<VectorId>,
pub vector: Vec<f32>,
pub metadata: Option<HashMap<String, serde_json::Value>>,
}
// Search query parameters
pub struct SearchQuery {
pub vector: Vec<f32>,
pub k: usize,
pub filter: Option<HashMap<String, serde_json::Value>>,
pub ef_search: Option<usize>,
}
// Search result with score
pub struct SearchResult {
pub id: VectorId,
pub score: f32,
pub vector: Option<Vec<f32>>,
pub metadata: Option<HashMap<String, serde_json::Value>>,
}
Main Operations
impl VectorDB {
// Create new database with options
pub fn new(options: DbOptions) -> Result<Self>;
// Create with just dimensions (uses defaults)
pub fn with_dimensions(dimensions: usize) -> Result<Self>;
// Insert single vector
pub fn insert(&self, entry: VectorEntry) -> Result<VectorId>;
// Insert multiple vectors
pub fn insert_batch(&self, entries: Vec<VectorEntry>) -> Result<Vec<VectorId>>;
// Search for similar vectors
pub fn search(&self, query: SearchQuery) -> Result<Vec<SearchResult>>;
// Delete vector by ID
pub fn delete(&self, id: &str) -> Result<bool>;
// Get vector by ID
pub fn get(&self, id: &str) -> Result<Option<VectorEntry>>;
// Get total count
pub fn len(&self) -> Result<usize>;
// Check if empty
pub fn is_empty(&self) -> Result<bool>;
}
Distance Metrics
pub enum DistanceMetric {
Euclidean, // L2 distance - default for embeddings
Cosine, // Cosine similarity (1 - similarity)
DotProduct, // Negative dot product (for maximization)
Manhattan, // L1 distance
}
Advanced Features
// Hybrid search (dense + sparse)
use ruvector_core::{HybridSearch, HybridConfig};
let hybrid = HybridSearch::new(HybridConfig {
alpha: 0.7, // Balance between dense (0.7) and sparse (0.3)
..Default::default()
});
// Filtered search with expressions
use ruvector_core::{FilteredSearch, FilterExpression};
let filtered = FilteredSearch::new(db);
let expr = FilterExpression::And(vec![
FilterExpression::Equals("category".to_string(), json!("books")),
FilterExpression::GreaterThan("price".to_string(), json!(10.0)),
]);
// MMR diversification
use ruvector_core::{MMRSearch, MMRConfig};
let mmr = MMRSearch::new(MMRConfig {
lambda: 0.5, // Balance relevance (0.5) and diversity (0.5)
..Default::default()
});
🎯 Performance Characteristics
Latency (Single Query)
Operation Flat Index HNSW Index
─────────────────────────────────────────────
Search (1K vecs) ~0.1ms ~0.2ms
Search (100K vecs) ~10ms ~0.5ms
Search (1M vecs) ~100ms <1ms
Insert ~0.1ms ~1ms
Batch (1000) ~50ms ~500ms
Memory Usage (1M Vectors, 384 Dimensions)
Configuration Memory Recall
─────────────────────────────────────────────
Full Precision (f32) ~1.5GB 100%
Scalar Quantization ~400MB 98%
Product Quantization ~200MB 95%
Binary Quantization ~50MB 85%
Throughput (Queries Per Second)
Configuration QPS Latency (p50)
─────────────────────────────────────────────────
Single Thread ~2,000 ~0.5ms
Multi-Thread (8 cores) ~50,000 <0.5ms
With SIMD ~80,000 <0.3ms
With Quantization ~100,000 <0.2ms
🔧 Configuration Guide
For Maximum Accuracy
let options = DbOptions {
dimensions: 384,
distance_metric: DistanceMetric::Cosine,
hnsw_config: Some(HnswConfig {
m: 64,
ef_construction: 500,
ef_search: 200,
max_elements: 10_000_000,
}),
quantization: None, // Full precision
..Default::default()
};
For Maximum Speed
let options = DbOptions {
dimensions: 384,
distance_metric: DistanceMetric::DotProduct,
hnsw_config: Some(HnswConfig {
m: 16,
ef_construction: 100,
ef_search: 50,
max_elements: 10_000_000,
}),
quantization: Some(QuantizationConfig::Binary),
..Default::default()
};
For Balanced Performance
let options = DbOptions::default(); // Recommended defaults
🔨 Building and Testing
Build
# Build with default features
cargo build --release
# Build without SIMD
cargo build --release --no-default-features --features uuid-support
# Build for specific target with optimizations
RUSTFLAGS="-C target-cpu=native" cargo build --release
Testing
# Run all tests
cargo test
# Run with specific features
cargo test --features simd
# Run with logging
RUST_LOG=debug cargo test
Benchmarks
# Run all benchmarks
cargo bench
# Run specific benchmark
cargo bench --bench hnsw_search
# Run with features
cargo bench --features simd
Available benchmarks:
distance_metrics- SIMD-optimized distance calculationshnsw_search- HNSW index search performancequantization_bench- Quantization techniquesbatch_operations- Batch insert/search operationscomprehensive_bench- Full system benchmarks
📚 Documentation
Complete Ruvector Documentation
This crate is part of the larger Ruvector project:
- Main README - Complete project overview
- Getting Started Guide - Quick start tutorial
- Rust API Reference - Detailed API documentation
- Advanced Features Guide - Quantization, indexing, tuning
- Performance Tuning - Optimization strategies
- Benchmarking Guide - Running benchmarks
API Documentation
Generate and view the full API documentation:
cargo doc --open --no-deps
🌐 Related Crates
ruvector-core is the foundation for platform-specific bindings:
- ruvector-node - Node.js bindings via NAPI-RS
- ruvector-wasm - WebAssembly bindings for browsers
- ruvector-cli - Command-line interface
- ruvector-bench - Performance benchmarks
🤝 Contributing
We welcome contributions! See the main Contributing Guidelines for details.
Areas for Contribution
- 🐛 Bug fixes and stability improvements
- ✨ New distance metrics or quantization techniques
- 📈 Performance optimizations
- 🧪 Additional test coverage
- 📝 Documentation and examples
📊 Comparison
Why Ruvector Core vs. Alternatives?
| Feature | Ruvector Core | hnswlib-rs | faiss-rs | qdrant |
|---|---|---|---|---|
| Pure Rust | ✅ | ✅ | ❌ (C++) | ✅ |
| SIMD | ✅ SimSIMD | ❌ | ✅ | ✅ |
| Quantization | ✅ Multiple | ❌ | ✅ | ✅ |
| Zero-Copy I/O | ✅ | ❌ | ✅ | ✅ |
| Metadata Filter | ✅ | ❌ | ❌ | ✅ |
| Hybrid Search | ✅ | ❌ | ❌ | ✅ |
| P50 Latency | <0.5ms | ~1ms | ~0.5ms | ~1ms |
| Dependencies | Minimal | Minimal | Heavy | Heavy |
📜 License
MIT License - see LICENSE for details.
🙏 Acknowledgments
Built with state-of-the-art algorithms and libraries:
- hnsw_rs - HNSW implementation
- simsimd - SIMD distance calculations
- redb - Embedded database
- rayon - Data parallelism
- memmap2 - Memory-mapped files