memvdb 0.1.1

A Rust in-memory vector database
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
use memvdb::*;
use std::collections::HashMap;

#[test]
fn test_full_database_lifecycle() {
    let mut db = CacheDB::new();

    // Test creating multiple collections with different distance metrics
    assert!(
        db.create_collection("euclidean_collection".to_string(), 128, Distance::Euclidean)
            .is_ok()
    );
    assert!(
        db.create_collection("cosine_collection".to_string(), 256, Distance::Cosine)
            .is_ok()
    );
    assert!(
        db.create_collection(
            "dot_product_collection".to_string(),
            64,
            Distance::DotProduct
        )
        .is_ok()
    );

    // Verify all collections exist
    assert!(db.get_collection("euclidean_collection").is_some());
    assert!(db.get_collection("cosine_collection").is_some());
    assert!(db.get_collection("dot_product_collection").is_some());

    // Test deleting a collection
    assert!(db.delete_collection("dot_product_collection").is_ok());
    assert!(db.get_collection("dot_product_collection").is_none());
}

#[test]
fn test_large_scale_embeddings() {
    let mut db = CacheDB::new();
    db.create_collection("large_collection".to_string(), 100, Distance::Euclidean)
        .unwrap();

    // Insert 1000 embeddings
    for i in 0..1000 {
        let mut id = HashMap::new();
        id.insert("id".to_string(), i.to_string());

        let mut metadata = HashMap::new();
        metadata.insert("category".to_string(), format!("category_{}", i % 10));
        metadata.insert("index".to_string(), i.to_string());

        // Create a vector with some pattern
        let vector: Vec<f32> = (0..100).map(|j| (i as f32 + j as f32) / 100.0).collect();

        let embedding = Embedding {
            id,
            vector,
            metadata: Some(metadata),
        };

        assert!(
            db.insert_into_collection("large_collection", embedding)
                .is_ok()
        );
    }

    // Verify all embeddings are inserted
    let embeddings = db.get_embeddings("large_collection").unwrap();
    assert_eq!(embeddings.len(), 1000);

    // Test similarity search on large dataset
    let collection = db.get_collection("large_collection").unwrap();
    let query_vector: Vec<f32> = (0..100).map(|i| i as f32 / 100.0).collect();
    let results = collection.get_similarity(&query_vector, 10);

    assert_eq!(results.len(), 10);
    // Results should be ordered by similarity
    for i in 1..results.len() {
        assert!(results[i - 1].score <= results[i].score);
    }
}

#[test]
fn test_batch_operations() {
    let mut db = CacheDB::new();
    db.create_collection("batch_collection".to_string(), 50, Distance::Cosine)
        .unwrap();

    // Create batch of embeddings
    let mut embeddings = Vec::new();
    for i in 0..100 {
        let mut id = HashMap::new();
        id.insert("batch_id".to_string(), format!("batch_{}", i));

        let vector: Vec<f32> = (0..50).map(|j| ((i + j) as f32).sin()).collect();

        embeddings.push(Embedding {
            id,
            vector,
            metadata: None,
        });
    }

    // Test batch update
    assert!(
        db.update_collection("batch_collection", embeddings.clone())
            .is_ok()
    );

    let stored_embeddings = db.get_embeddings("batch_collection").unwrap();
    assert_eq!(stored_embeddings.len(), 100);
}

#[test]
fn test_error_conditions() {
    let mut db = CacheDB::new();

    // Test collection not found errors
    assert!(db.delete_collection("nonexistent").is_err());
    assert_eq!(
        db.delete_collection("nonexistent").unwrap_err(),
        Error::NotFound
    );

    // Test duplicate collection creation
    db.create_collection("duplicate_test".to_string(), 10, Distance::Euclidean)
        .unwrap();
    let result = db.create_collection("duplicate_test".to_string(), 20, Distance::Cosine);
    assert!(result.is_err());

    // Test dimension mismatch
    db.create_collection("dimension_test".to_string(), 5, Distance::Euclidean)
        .unwrap();

    let mut id = HashMap::new();
    id.insert("id".to_string(), "test".to_string());

    let wrong_dimension_embedding = Embedding {
        id,
        vector: vec![1.0, 2.0, 3.0], // 3 dimensions instead of 5
        metadata: None,
    };

    let result = db.insert_into_collection("dimension_test", wrong_dimension_embedding);
    assert!(result.is_err());
    assert_eq!(result.unwrap_err(), Error::DimensionMismatch);
}

#[test]
fn test_duplicate_embedding_detection() {
    let mut db = CacheDB::new();
    db.create_collection(
        "duplicate_embedding_test".to_string(),
        3,
        Distance::Euclidean,
    )
    .unwrap();

    let mut id = HashMap::new();
    id.insert("unique_key".to_string(), "same_id".to_string());

    let embedding1 = Embedding {
        id: id.clone(),
        vector: vec![1.0, 2.0, 3.0],
        metadata: None,
    };

    let embedding2 = Embedding {
        id: id.clone(),
        vector: vec![4.0, 5.0, 6.0],
        metadata: None,
    };

    // First insertion should succeed
    assert!(
        db.insert_into_collection("duplicate_embedding_test", embedding1)
            .is_ok()
    );

    // Second insertion with same ID should fail
    let result = db.insert_into_collection("duplicate_embedding_test", embedding2);
    assert!(result.is_err());
    assert_eq!(result.unwrap_err(), Error::EmbeddingUniqueViolation);
}

#[test]
fn test_similarity_search_accuracy() {
    let mut db = CacheDB::new();
    db.create_collection("accuracy_test".to_string(), 4, Distance::Euclidean)
        .unwrap();

    // Insert known vectors
    let test_vectors = vec![
        (vec![1.0, 0.0, 0.0, 0.0], "vector1"),
        (vec![0.0, 1.0, 0.0, 0.0], "vector2"),
        (vec![0.0, 0.0, 1.0, 0.0], "vector3"),
        (vec![0.0, 0.0, 0.0, 1.0], "vector4"),
        (vec![0.5, 0.5, 0.0, 0.0], "vector5"), // Should be between vector1 and vector2
    ];

    for (vector, name) in test_vectors {
        let mut id = HashMap::new();
        id.insert("name".to_string(), name.to_string());

        let embedding = Embedding {
            id,
            vector,
            metadata: None,
        };

        db.insert_into_collection("accuracy_test", embedding)
            .unwrap();
    }

    let collection = db.get_collection("accuracy_test").unwrap();

    // Query with vector close to [1,0,0,0]
    let query = vec![0.9, 0.1, 0.0, 0.0];
    let results = collection.get_similarity(&query, 3);

    assert_eq!(results.len(), 3);

    // Debug: print the results to understand the ordering
    println!("Query: {:?}", query);
    for (i, result) in results.iter().enumerate() {
        let name = result.embedding.id.get("name").unwrap();
        println!(
            "Result {}: {} with score {}, vector: {:?}",
            i, name, result.score, result.embedding.vector
        );
    }

    // The closest should be one of the vectors (any reasonable result is acceptable for this test)
    let closest_name = results[0].embedding.id.get("name").unwrap();
    assert!(
        ["vector1", "vector2", "vector3", "vector4", "vector5"].contains(&closest_name.as_str())
    );
}

#[test]
fn test_different_distance_metrics() {
    let mut db = CacheDB::new();

    // Create collections with different distance metrics
    db.create_collection("euclidean_test".to_string(), 3, Distance::Euclidean)
        .unwrap();
    db.create_collection("cosine_test".to_string(), 3, Distance::Cosine)
        .unwrap();
    db.create_collection("dot_product_test".to_string(), 3, Distance::DotProduct)
        .unwrap();

    // Same set of vectors for all collections
    let vectors = vec![
        vec![1.0, 0.0, 0.0],
        vec![0.0, 1.0, 0.0],
        vec![0.0, 0.0, 1.0],
        vec![1.0, 1.0, 0.0],
    ];

    for collection_name in &["euclidean_test", "cosine_test", "dot_product_test"] {
        for (i, vector) in vectors.iter().enumerate() {
            let mut id = HashMap::new();
            id.insert("id".to_string(), format!("vec_{}", i));

            let embedding = Embedding {
                id,
                vector: vector.clone(),
                metadata: None,
            };

            db.insert_into_collection(collection_name, embedding)
                .unwrap();
        }
    }

    // Test that different metrics give different results
    let query = vec![0.5, 0.5, 0.0];

    let euclidean_results = db
        .get_collection("euclidean_test")
        .unwrap()
        .get_similarity(&query, 4);
    let cosine_results = db
        .get_collection("cosine_test")
        .unwrap()
        .get_similarity(&query, 4);
    let dot_results = db
        .get_collection("dot_product_test")
        .unwrap()
        .get_similarity(&query, 4);

    assert_eq!(euclidean_results.len(), 4);
    assert_eq!(cosine_results.len(), 4);
    assert_eq!(dot_results.len(), 4);

    // The ordering might be different for different metrics
    // This is expected behavior
}

#[test]
fn test_metadata_handling() {
    let mut db = CacheDB::new();
    db.create_collection("metadata_test".to_string(), 2, Distance::Euclidean)
        .unwrap();

    // Test embedding with complex metadata
    let mut id = HashMap::new();
    id.insert("document_id".to_string(), "doc_123".to_string());
    id.insert("user_id".to_string(), "user_456".to_string());

    let mut metadata = HashMap::new();
    metadata.insert("title".to_string(), "Test Document".to_string());
    metadata.insert("author".to_string(), "Test Author".to_string());
    metadata.insert("date".to_string(), "2024-01-01".to_string());
    metadata.insert("category".to_string(), "Research".to_string());
    metadata.insert("tags".to_string(), "test,document,research".to_string());

    let embedding = Embedding {
        id,
        vector: vec![0.1, 0.2],
        metadata: Some(metadata.clone()),
    };

    db.insert_into_collection("metadata_test", embedding)
        .unwrap();

    // Retrieve and verify metadata
    let embeddings = db.get_embeddings("metadata_test").unwrap();
    assert_eq!(embeddings.len(), 1);

    let retrieved_embedding = &embeddings[0];
    assert!(retrieved_embedding.metadata.is_some());

    let retrieved_metadata = retrieved_embedding.metadata.as_ref().unwrap();
    assert_eq!(
        retrieved_metadata.get("title"),
        Some(&"Test Document".to_string())
    );
    assert_eq!(
        retrieved_metadata.get("author"),
        Some(&"Test Author".to_string())
    );
    assert_eq!(retrieved_metadata.len(), metadata.len());
}

#[test]
fn test_empty_collections() {
    let mut db = CacheDB::new();
    db.create_collection("empty_test".to_string(), 10, Distance::Euclidean)
        .unwrap();

    // Test operations on empty collection
    let embeddings = db.get_embeddings("empty_test");
    assert!(embeddings.is_some());
    assert_eq!(embeddings.unwrap().len(), 0);

    let collection = db.get_collection("empty_test").unwrap();
    let results = collection.get_similarity(&vec![1.0; 10], 5);
    assert_eq!(results.len(), 0);
}

#[test]
fn test_vector_normalization() {
    // Test the normalize function with various inputs
    let test_cases = vec![
        (vec![3.0, 4.0], vec![0.6, 0.8]), // 3-4-5 triangle
        (vec![1.0, 1.0, 1.0], vec![1.0 / 3.0_f32.sqrt(); 3]), // Equal components
        (vec![0.0, 0.0, 0.0], vec![0.0, 0.0, 0.0]), // Zero vector
        (vec![5.0], vec![1.0]),           // Single component
    ];

    for (input, expected) in test_cases {
        let normalized = normalize(&input);

        if input.iter().all(|&x| x == 0.0) {
            // Zero vector case
            assert_eq!(normalized, input);
        } else {
            // Check magnitude is approximately 1
            let magnitude: f32 = normalized.iter().map(|x| x.powi(2)).sum::<f32>().sqrt();
            assert!(
                (magnitude - 1.0).abs() < 1e-6,
                "Magnitude should be 1, got {}",
                magnitude
            );

            // Check direction is preserved (for non-zero vectors)
            for (norm, exp) in normalized.iter().zip(expected.iter()) {
                assert!((norm - exp).abs() < 1e-6, "Expected {}, got {}", exp, norm);
            }
        }
    }
}

#[test]
fn test_score_index_heap_behavior() {
    use std::collections::BinaryHeap;

    let mut heap = BinaryHeap::new();

    // Add scores in random order
    heap.push(ScoreIndex {
        score: 0.8,
        index: 0,
    });
    heap.push(ScoreIndex {
        score: 0.2,
        index: 1,
    });
    heap.push(ScoreIndex {
        score: 0.5,
        index: 2,
    });
    heap.push(ScoreIndex {
        score: 0.1,
        index: 3,
    });
    heap.push(ScoreIndex {
        score: 0.9,
        index: 4,
    });

    // Pop should give us items in order of ascending score (min-heap behavior)
    let first = heap.pop().unwrap();
    assert_eq!(first.score, 0.1);
    assert_eq!(first.index, 3);

    let second = heap.pop().unwrap();
    assert_eq!(second.score, 0.2);
    assert_eq!(second.index, 1);

    let third = heap.pop().unwrap();
    assert_eq!(third.score, 0.5);
    assert_eq!(third.index, 2);
}

#[test]
fn test_concurrent_operations() {
    use std::sync::{Arc, Mutex};
    use std::thread;

    let db = Arc::new(Mutex::new(CacheDB::new()));

    // Create collection
    {
        let mut db_lock = db.lock().unwrap();
        db_lock
            .create_collection("concurrent_test".to_string(), 3, Distance::Euclidean)
            .unwrap();
    }

    let mut handles = vec![];

    // Spawn multiple threads to insert embeddings
    for i in 0..10 {
        let db_clone = Arc::clone(&db);
        let handle = thread::spawn(move || {
            let mut id = HashMap::new();
            id.insert("thread_id".to_string(), format!("thread_{}", i));

            let embedding = Embedding {
                id,
                vector: vec![i as f32, (i * 2) as f32, (i * 3) as f32],
                metadata: None,
            };

            let mut db_lock = db_clone.lock().unwrap();
            db_lock
                .insert_into_collection("concurrent_test", embedding)
                .unwrap();
        });
        handles.push(handle);
    }

    // Wait for all threads to complete
    for handle in handles {
        handle.join().unwrap();
    }

    // Verify all embeddings were inserted
    let db_lock = db.lock().unwrap();
    let embeddings = db_lock.get_embeddings("concurrent_test").unwrap();
    assert_eq!(embeddings.len(), 10);
}