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
//! Main VectorDB interface
use crate::error::Result;
use crate::index::flat::FlatIndex;
#[cfg(feature = "hnsw")]
use crate::index::hnsw::HnswIndex;
use crate::index::VectorIndex;
use crate::types::*;
use parking_lot::RwLock;
use std::sync::Arc;
// Import appropriate storage backend based on features
#[cfg(feature = "storage")]
use crate::storage::VectorStorage;
#[cfg(not(feature = "storage"))]
use crate::storage_memory::MemoryStorage as VectorStorage;
/// Main vector database
pub struct VectorDB {
storage: Arc<VectorStorage>,
index: Arc<RwLock<Box<dyn VectorIndex>>>,
options: DbOptions,
}
impl VectorDB {
/// Create a new vector database with the given options
///
/// If a storage path is provided and contains persisted vectors,
/// the HNSW index will be automatically rebuilt from storage.
/// If opening an existing database, the stored configuration (dimensions,
/// distance metric, etc.) will be used instead of the provided options.
#[allow(unused_mut)] // `options` is mutated only when feature = "storage"
pub fn new(mut options: DbOptions) -> Result<Self> {
#[cfg(feature = "storage")]
let storage = {
// First, try to load existing configuration from the database
// We create a temporary storage to check for config
let temp_storage = VectorStorage::new(&options.storage_path, options.dimensions)?;
let stored_config = temp_storage.load_config()?;
if let Some(config) = stored_config {
// Existing database - use stored configuration
tracing::info!(
"Loading existing database with {} dimensions",
config.dimensions
);
options = DbOptions {
// Keep the provided storage path (may have changed)
storage_path: options.storage_path.clone(),
// Use stored configuration for everything else
dimensions: config.dimensions,
distance_metric: config.distance_metric,
hnsw_config: config.hnsw_config,
quantization: config.quantization,
};
// Recreate storage with correct dimensions
Arc::new(VectorStorage::new(
&options.storage_path,
options.dimensions,
)?)
} else {
// New database - save the configuration
tracing::info!(
"Creating new database with {} dimensions",
options.dimensions
);
temp_storage.save_config(&options)?;
Arc::new(temp_storage)
}
};
#[cfg(not(feature = "storage"))]
let storage = Arc::new(VectorStorage::new(options.dimensions)?);
// Choose index based on configuration and available features
#[allow(unused_mut)] // `index` is mutated only when feature = "storage"
let mut index: Box<dyn VectorIndex> = if let Some(hnsw_config) = &options.hnsw_config {
#[cfg(feature = "hnsw")]
{
Box::new(HnswIndex::new(
options.dimensions,
options.distance_metric,
hnsw_config.clone(),
)?)
}
#[cfg(not(feature = "hnsw"))]
{
// Fall back to flat index if HNSW is not available
tracing::warn!("HNSW requested but not available (WASM build), using flat index");
Box::new(FlatIndex::new(options.dimensions, options.distance_metric))
}
} else {
Box::new(FlatIndex::new(options.dimensions, options.distance_metric))
};
// Rebuild index from persisted vectors if storage is not empty
// This fixes the bug where search() returns empty results after restart
#[cfg(feature = "storage")]
{
let stored_ids = storage.all_ids()?;
if !stored_ids.is_empty() {
tracing::info!(
"Rebuilding index from {} persisted vectors",
stored_ids.len()
);
// Batch load all vectors for efficient index rebuilding
let mut entries = Vec::with_capacity(stored_ids.len());
for id in stored_ids {
if let Some(entry) = storage.get(&id)? {
entries.push((id, entry.vector));
}
}
// Add all vectors to index in batch for better performance
index.add_batch(entries)?;
tracing::info!("Index rebuilt successfully");
}
}
Ok(Self {
storage,
index: Arc::new(RwLock::new(index)),
options,
})
}
/// Create with default options
pub fn with_dimensions(dimensions: usize) -> Result<Self> {
let options = DbOptions {
dimensions,
..DbOptions::default()
};
Self::new(options)
}
/// Insert a vector entry
pub fn insert(&self, entry: VectorEntry) -> Result<VectorId> {
let id = self.storage.insert(&entry)?;
// Add to index
let mut index = self.index.write();
index.add(id.clone(), entry.vector)?;
Ok(id)
}
/// Insert multiple vectors in a batch
pub fn insert_batch(&self, entries: impl AsRef<[VectorEntry]>) -> Result<Vec<VectorId>> {
let entries = entries.as_ref();
let ids = self.storage.insert_batch(entries)?;
// Add to index
let mut index = self.index.write();
let index_entries: Vec<_> = ids
.iter()
.zip(entries.iter())
.map(|(id, entry)| (id.clone(), entry.vector.clone()))
.collect();
index.add_batch(index_entries)?;
Ok(ids)
}
/// Search for similar vectors
pub fn search(&self, query: SearchQuery) -> Result<Vec<SearchResult>> {
let index = self.index.read();
let mut results = index.search(&query.vector, query.k)?;
// Enrich results with full data if needed
for result in &mut results {
if let Ok(Some(entry)) = self.storage.get(&result.id) {
result.vector = Some(entry.vector);
result.metadata = entry.metadata;
}
}
// Apply metadata filters if specified
if let Some(filter) = &query.filter {
results.retain(|r| {
if let Some(metadata) = &r.metadata {
filter
.iter()
.all(|(key, value)| metadata.get(key).is_some_and(|v| v == value))
} else {
false
}
});
}
Ok(results)
}
/// Delete a vector by ID
pub fn delete(&self, id: &str) -> Result<bool> {
let deleted_storage = self.storage.delete(id)?;
if deleted_storage {
let mut index = self.index.write();
let _ = index.remove(&id.to_string())?;
}
Ok(deleted_storage)
}
/// Get a vector by ID
pub fn get(&self, id: &str) -> Result<Option<VectorEntry>> {
self.storage.get(id)
}
/// Get the number of vectors
pub fn len(&self) -> Result<usize> {
self.storage.len()
}
/// Check if database is empty
pub fn is_empty(&self) -> Result<bool> {
self.storage.is_empty()
}
/// Get database options
pub fn options(&self) -> &DbOptions {
&self.options
}
/// Get all vector IDs (for iteration/serialization)
pub fn keys(&self) -> Result<Vec<String>> {
self.storage.all_ids()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;
use tempfile::tempdir;
#[test]
fn test_vector_db_creation() -> Result<()> {
let dir = tempdir().unwrap();
let mut options = DbOptions::default();
options.storage_path = dir.path().join("test.db").to_string_lossy().to_string();
options.dimensions = 3;
let db = VectorDB::new(options)?;
assert!(db.is_empty()?);
Ok(())
}
#[test]
fn test_insert_and_search() -> Result<()> {
let dir = tempdir().unwrap();
let mut options = DbOptions::default();
options.storage_path = dir.path().join("test.db").to_string_lossy().to_string();
options.dimensions = 3;
options.distance_metric = DistanceMetric::Euclidean; // Use Euclidean for clearer test
options.hnsw_config = None; // Use flat index for testing
let db = VectorDB::new(options)?;
// Insert vectors
db.insert(VectorEntry {
id: Some("v1".to_string()),
vector: vec![1.0, 0.0, 0.0],
metadata: None,
})?;
db.insert(VectorEntry {
id: Some("v2".to_string()),
vector: vec![0.0, 1.0, 0.0],
metadata: None,
})?;
db.insert(VectorEntry {
id: Some("v3".to_string()),
vector: vec![0.0, 0.0, 1.0],
metadata: None,
})?;
// Search for exact match
let results = db.search(SearchQuery {
vector: vec![1.0, 0.0, 0.0],
k: 2,
filter: None,
ef_search: None,
})?;
assert!(results.len() >= 1);
assert_eq!(results[0].id, "v1", "First result should be exact match");
assert!(
results[0].score < 0.01,
"Exact match should have ~0 distance"
);
Ok(())
}
/// Test that search works after simulated restart (new VectorDB instance)
/// This verifies the fix for issue #30: HNSW index not rebuilt from storage
#[test]
#[cfg(feature = "storage")]
fn test_search_after_restart() -> Result<()> {
let dir = tempdir().unwrap();
let db_path = dir.path().join("persist.db").to_string_lossy().to_string();
// Phase 1: Create database and insert vectors
{
let mut options = DbOptions::default();
options.storage_path = db_path.clone();
options.dimensions = 3;
options.distance_metric = DistanceMetric::Euclidean;
options.hnsw_config = None;
let db = VectorDB::new(options)?;
db.insert(VectorEntry {
id: Some("v1".to_string()),
vector: vec![1.0, 0.0, 0.0],
metadata: None,
})?;
db.insert(VectorEntry {
id: Some("v2".to_string()),
vector: vec![0.0, 1.0, 0.0],
metadata: None,
})?;
db.insert(VectorEntry {
id: Some("v3".to_string()),
vector: vec![0.7, 0.7, 0.0],
metadata: None,
})?;
// Verify search works before "restart"
let results = db.search(SearchQuery {
vector: vec![0.8, 0.6, 0.0],
k: 3,
filter: None,
ef_search: None,
})?;
assert_eq!(results.len(), 3, "Should find all 3 vectors before restart");
}
// db is dropped here, simulating application shutdown
// Phase 2: Create new database instance (simulates restart)
{
let mut options = DbOptions::default();
options.storage_path = db_path.clone();
options.dimensions = 3;
options.distance_metric = DistanceMetric::Euclidean;
options.hnsw_config = None;
let db = VectorDB::new(options)?;
// Verify vectors are still accessible
assert_eq!(db.len()?, 3, "Should have 3 vectors after restart");
// Verify get() works
let v1 = db.get("v1")?;
assert!(v1.is_some(), "get() should work after restart");
// Verify search() works - THIS WAS THE BUG
let results = db.search(SearchQuery {
vector: vec![0.8, 0.6, 0.0],
k: 3,
filter: None,
ef_search: None,
})?;
assert_eq!(
results.len(),
3,
"search() should return results after restart (was returning 0 before fix)"
);
// v3 should be closest to query [0.8, 0.6, 0.0]
assert_eq!(
results[0].id, "v3",
"v3 [0.7, 0.7, 0.0] should be closest to query [0.8, 0.6, 0.0]"
);
}
Ok(())
}
}