vectorlite 0.1.5

A high-performance, in-memory vector database optimized for AI agent workloads
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
//! # Client Module
//!
//! This module provides the main client interface for VectorLite, including collection management,
//! vector operations, and search functionality.
//!
//! The `VectorLiteClient` is the primary entry point for interacting with the vector database.
//! It manages collections of vectors and provides thread-safe operations for adding, searching,
//! and deleting vectors.
//!
//! # Examples
//!
//! ```rust
//! use vectorlite::{VectorLiteClient, EmbeddingGenerator, IndexType, SimilarityMetric};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a client with an embedding function
//! let mut client = VectorLiteClient::new(Box::new(EmbeddingGenerator::new()?));
//!
//! // Create a collection
//! client.create_collection("documents", IndexType::HNSW)?;
//!
//! // Add text (auto-generates embedding)
//! let id = client.add_text_to_collection("documents", "Hello world", None)?;
//!
//! // Search for similar text
//! let results = client.search_text_in_collection(
//!     "documents", 
//!     "hello", 
//!     5, 
//!     SimilarityMetric::Cosine
//! )?;
//! # Ok(())
//! # }
//! ```

use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::sync::atomic::{AtomicU64, Ordering};
use std::path::Path;
use crate::{VectorIndexWrapper, VectorIndex, Vector, SearchResult, SimilarityMetric, EmbeddingFunction, PersistenceError, save_collection_to_file, load_collection_from_file};
use crate::errors::{VectorLiteError, VectorLiteResult};

/// Main client for interacting with VectorLite
///
/// The `VectorLiteClient` provides a high-level interface for managing vector collections,
/// performing searches, and handling embeddings. It's designed to be thread-safe and
/// efficient for AI agent workloads.
///
/// # Thread Safety
///
/// The client uses `Arc<RwLock<>>` for collections and atomic counters for ID generation,
/// making it safe to use across multiple threads.
///
/// # Examples
///
/// ```rust
/// use vectorlite::{VectorLiteClient, EmbeddingGenerator, IndexType};
///
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client = VectorLiteClient::new(Box::new(EmbeddingGenerator::new()?));
/// client.create_collection("docs", IndexType::HNSW)?;
/// # Ok(())
/// # }
/// ```
pub struct VectorLiteClient {
    collections: HashMap<String, CollectionRef>,
    embedding_function: Arc<dyn EmbeddingFunction>,
}

/// Configuration settings for VectorLite
///
/// Currently unused but reserved for future configuration options.
pub struct Settings {}

impl VectorLiteClient {
    pub fn new(embedding_function: Box<dyn EmbeddingFunction>) -> Self {
        Self {
            collections: HashMap::new(),
            embedding_function: Arc::from(embedding_function),
        }
    }

    pub fn create_collection(&mut self, name: &str, index_type: IndexType) -> VectorLiteResult<()> {
        if self.collections.contains_key(name) {
            return Err(VectorLiteError::CollectionAlreadyExists { name: name.to_string() });
        }

        let dimension = self.embedding_function.dimension();
        let index = match index_type {
            IndexType::Flat => VectorIndexWrapper::Flat(crate::FlatIndex::new(dimension, Vec::new())),
            IndexType::HNSW => VectorIndexWrapper::HNSW(Box::new(crate::HNSWIndex::new(dimension))),
        };

        let collection = Collection {
            name: name.to_string(),
            index: Arc::new(RwLock::new(index)),
            next_id: Arc::new(AtomicU64::new(0)),
        };

        self.collections.insert(name.to_string(), Arc::new(collection));
        Ok(())
    }

    pub fn get_collection(&self, name: &str) -> Option<&CollectionRef> {
        self.collections.get(name)
    }

    pub fn list_collections(&self) -> Vec<String> {
        self.collections.keys().cloned().collect()
    }

    pub fn delete_collection(&mut self, name: &str) -> VectorLiteResult<()> {
        if self.collections.remove(name).is_some() {
            Ok(())
        } else {
            Err(VectorLiteError::CollectionNotFound { name: name.to_string() })
        }
    }

    pub fn has_collection(&self, name: &str) -> bool {
        self.collections.contains_key(name)
    }

    pub fn add_text_to_collection(&self, collection_name: &str, text: &str, metadata: Option<serde_json::Value>) -> VectorLiteResult<u64> {
        let collection = self.collections.get(collection_name)
            .ok_or_else(|| VectorLiteError::CollectionNotFound { name: collection_name.to_string() })?;
        
        collection.add_text_with_metadata(text, metadata, self.embedding_function.as_ref())
    }


    pub fn search_text_in_collection(&self, collection_name: &str, query_text: &str, k: usize, similarity_metric: SimilarityMetric) -> VectorLiteResult<Vec<SearchResult>> {
        let collection = self.collections.get(collection_name)
            .ok_or_else(|| VectorLiteError::CollectionNotFound { name: collection_name.to_string() })?;
        
        collection.search_text(query_text, k, similarity_metric, self.embedding_function.as_ref())
    }


    pub fn delete_from_collection(&self, collection_name: &str, id: u64) -> VectorLiteResult<()> {
        let collection = self.collections.get(collection_name)
            .ok_or_else(|| VectorLiteError::CollectionNotFound { name: collection_name.to_string() })?;
        
        collection.delete(id)
    }

    pub fn get_vector_from_collection(&self, collection_name: &str, id: u64) -> VectorLiteResult<Option<Vector>> {
        let collection = self.collections.get(collection_name)
            .ok_or_else(|| VectorLiteError::CollectionNotFound { name: collection_name.to_string() })?;
        
        collection.get_vector(id)
    }

    pub fn get_collection_info(&self, collection_name: &str) -> VectorLiteResult<CollectionInfo> {
        let collection = self.collections.get(collection_name)
            .ok_or_else(|| VectorLiteError::CollectionNotFound { name: collection_name.to_string() })?;
        
        collection.get_info()
    }

    /// Add a collection directly (used for loading from files)
    pub fn add_collection(&mut self, collection: Collection) -> VectorLiteResult<()> {
        let name = collection.name().to_string();
        if self.collections.contains_key(&name) {
            return Err(VectorLiteError::CollectionAlreadyExists { name });
        }
        self.collections.insert(name, Arc::new(collection));
        Ok(())
    }

}

/// Index types available for vector collections
///
/// Different index types offer different trade-offs between search speed, memory usage,
/// and accuracy. Choose based on your dataset size and performance requirements.
///
/// # Examples
///
/// ```rust
/// use vectorlite::{VectorLiteClient, EmbeddingGenerator, IndexType};
///
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client = VectorLiteClient::new(Box::new(EmbeddingGenerator::new()?));
/// 
/// // For small datasets with exact search requirements
/// client.create_collection("small_data", IndexType::Flat)?;
/// 
/// // For large datasets with approximate search tolerance
/// client.create_collection("large_data", IndexType::HNSW)?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Copy)]
pub enum IndexType {
    /// Flat index - exact search with O(n) complexity
    /// 
    /// Best for:
    /// - Small datasets (< 10K vectors)
    /// - Exact search requirements
    /// - Memory-constrained environments
    Flat,
    /// HNSW index - approximate search with O(log n) complexity
    /// 
    /// Best for:
    /// - Large datasets (> 10K vectors)
    /// - Approximate search tolerance
    /// - High-performance requirements
    HNSW,
}

/// Collection structure containing the vector index and metadata
///
/// This struct wraps the actual vector index with thread-safe primitives
/// and provides atomic ID generation for new vectors.
///
/// # Thread Safety
///
/// Uses `Arc<RwLock<>>` for the index to allow concurrent reads and exclusive writes,
/// and `Arc<AtomicU64>` for thread-safe ID generation.
pub struct Collection {
    name: String,
    index: Arc<RwLock<VectorIndexWrapper>>,
    next_id: Arc<AtomicU64>,
}

/// Type alias for a thread-safe collection reference
type CollectionRef = Arc<Collection>;

/// Information about a collection
///
/// Contains metadata about a vector collection including its name, size,
/// vector dimension, and whether it's empty.
///
/// # Examples
///
/// ```rust
/// use vectorlite::{VectorLiteClient, EmbeddingGenerator, IndexType};
///
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client = VectorLiteClient::new(Box::new(EmbeddingGenerator::new()?));
/// client.create_collection("docs", IndexType::HNSW)?;
/// 
/// let info = client.get_collection_info("docs")?;
/// println!("Collection '{}' has {} vectors of dimension {}", 
///          info.name, info.count, info.dimension);
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, serde::Serialize)]
pub struct CollectionInfo {
    /// Name of the collection
    pub name: String,
    /// Number of vectors in the collection
    pub count: usize,
    /// Whether the collection is empty
    pub is_empty: bool,
    /// Dimension of vectors in this collection
    pub dimension: usize,
}

impl std::fmt::Debug for Collection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Collection")
            .field("name", &self.name)
            .field("next_id", &self.next_id.load(Ordering::Relaxed))
            .finish()
    }
}

impl Collection {
    /// Create a new collection with the given name and index
    pub fn new(name: String, index: VectorIndexWrapper) -> Self {
        // Calculate next_id from the maximum ID in the index + 1
        let next_id = match &index {
            VectorIndexWrapper::Flat(flat_index) => {
                flat_index.max_id()
                    .map(|max_id| max_id + 1)
                    .unwrap_or(0)
            }
            VectorIndexWrapper::HNSW(hnsw_index) => {
                hnsw_index.max_id()
                    .map(|max_id| max_id + 1)
                    .unwrap_or(0)
            }
        };

        Self {
            name,
            index: Arc::new(RwLock::new(index)),
            next_id: Arc::new(AtomicU64::new(next_id)),
        }
    }

    pub fn add_text(&self, text: &str, embedding_function: &dyn EmbeddingFunction) -> VectorLiteResult<u64> {
        let id = self.next_id.fetch_add(1, Ordering::Relaxed);
        
        // Generate embedding outside the lock
        let embedding = embedding_function.generate_embedding(text)?;
        
        let vector = Vector { 
            id, 
            values: embedding, 
            text: text.to_string(),
            metadata: None 
        };
        let vector_dimension = vector.values.len();
        let vector_id = vector.id;
        
        // Acquire write lock only for the index operation
        let mut index = self.index.write().map_err(|_| VectorLiteError::LockError("Failed to acquire write lock for add_text".to_string()))?;
        index.add(vector).map_err(|e| {
            if e.contains("dimension") {
                VectorLiteError::DimensionMismatch { 
                    expected: index.dimension(), 
                    actual: vector_dimension 
                }
            } else if e.contains("already exists") {
                VectorLiteError::DuplicateVectorId { id: vector_id }
            } else {
                VectorLiteError::InternalError(e)
            }
        })?;
        Ok(id)
    }

    pub fn add_text_with_metadata(&self, text: &str, metadata: Option<serde_json::Value>, embedding_function: &dyn EmbeddingFunction) -> VectorLiteResult<u64> {
        let id = self.next_id.fetch_add(1, Ordering::Relaxed);
        
        // Generate embedding outside the lock
        let embedding = embedding_function.generate_embedding(text)?;
        
        let vector = Vector { 
            id, 
            values: embedding, 
            text: text.to_string(),
            metadata 
        };
        let vector_dimension = vector.values.len();
        let vector_id = vector.id;
        
        // Acquire write lock only for the index operation
        let mut index = self.index.write().map_err(|_| VectorLiteError::LockError("Failed to acquire write lock for add_text_with_metadata".to_string()))?;
        index.add(vector).map_err(|e| {
            if e.contains("dimension") {
                VectorLiteError::DimensionMismatch { 
                    expected: index.dimension(), 
                    actual: vector_dimension 
                }
            } else if e.contains("already exists") {
                VectorLiteError::DuplicateVectorId { id: vector_id }
            } else {
                VectorLiteError::InternalError(e)
            }
        })?;
        Ok(id)
    }


    pub fn delete(&self, id: u64) -> VectorLiteResult<()> {
        let mut index = self.index.write().map_err(|_| VectorLiteError::LockError("Failed to acquire write lock for delete".to_string()))?;
        index.delete(id).map_err(|e| {
            if e.contains("does not exist") {
                VectorLiteError::VectorNotFound { id }
            } else {
                VectorLiteError::InternalError(e)
            }
        })
    }

    pub fn search_text(&self, query_text: &str, k: usize, similarity_metric: SimilarityMetric, embedding_function: &dyn EmbeddingFunction) -> VectorLiteResult<Vec<SearchResult>> {
        // Generate embedding outside the lock
        let query_embedding = embedding_function.generate_embedding(query_text)?;
        
        // Acquire read lock for search
        let index = self.index.read().map_err(|_| VectorLiteError::LockError("Failed to acquire read lock for search_text".to_string()))?;
        Ok(index.search(&query_embedding, k, similarity_metric))
    }


    pub fn get_vector(&self, id: u64) -> VectorLiteResult<Option<Vector>> {
        let index = self.index.read().map_err(|_| VectorLiteError::LockError("Failed to acquire read lock for get_vector".to_string()))?;
        Ok(index.get_vector(id).cloned())
    }

    pub fn get_info(&self) -> VectorLiteResult<CollectionInfo> {
        let index = self.index.read().map_err(|_| VectorLiteError::LockError("Failed to acquire read lock for get_info".to_string()))?;
        Ok(CollectionInfo {
            name: self.name.clone(),
            count: index.len(),
            is_empty: index.is_empty(),
            dimension: index.dimension(),
        })
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the current next ID value
    pub fn next_id(&self) -> u64 {
        self.next_id.load(Ordering::Relaxed)
    }

    /// Get a read lock on the index
    pub fn index_read(&self) -> Result<std::sync::RwLockReadGuard<'_, VectorIndexWrapper>, String> {
        self.index.read().map_err(|_| "Failed to acquire read lock".to_string())
    }

    /// Save the collection to a file
    ///
    /// This method saves the entire collection state to disk, including all vectors,
    /// the index structure, and the next ID counter. The file format is JSON-based
    /// and includes metadata for version compatibility.
    ///
    /// # Arguments
    ///
    /// * `path` - The file path where the collection should be saved
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` on success, or a `PersistenceError` if the operation fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use vectorlite::{VectorLiteClient, EmbeddingGenerator, IndexType};
    /// use std::path::Path;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut client = VectorLiteClient::new(Box::new(EmbeddingGenerator::new()?));
    /// client.create_collection("docs", IndexType::HNSW)?;
    /// client.add_text_to_collection("docs", "Hello world", None)?;
    ///
    /// let collection = client.get_collection("docs").unwrap();
    /// collection.save_to_file(Path::new("./docs.vlc"))?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn save_to_file(&self, path: &Path) -> Result<(), PersistenceError> {
        save_collection_to_file(self, path)
    }

    /// Load a collection from a file
    ///
    /// This method creates a new collection by loading its state from disk.
    /// The loaded collection will completely replace any existing collection
    /// with the same name (override strategy).
    ///
    /// # Arguments
    ///
    /// * `path` - The file path from which to load the collection
    ///
    /// # Returns
    ///
    /// Returns the loaded `Collection` on success, or a `PersistenceError` if the operation fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use vectorlite::Collection;
    /// use std::path::Path;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let collection = Collection::load_from_file(Path::new("./docs.vlc"))?;
    /// println!("Loaded collection '{}' with {} vectors", 
    ///          collection.name(), collection.get_info()?.count);
    /// # Ok(())
    /// # }
    /// ```
    pub fn load_from_file(path: &Path) -> Result<Self, PersistenceError> {
        load_collection_from_file(path)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Mock embedding function for testing
    struct MockEmbeddingFunction {
        dimension: usize,
    }

    impl MockEmbeddingFunction {
        fn new(dimension: usize) -> Self {
            Self { dimension }
        }
    }

    impl EmbeddingFunction for MockEmbeddingFunction {
        fn generate_embedding(&self, _text: &str) -> crate::embeddings::Result<Vec<f64>> {
            // Return a simple mock embedding with the correct dimension
            Ok(vec![1.0; self.dimension])
        }

        fn dimension(&self) -> usize {
            self.dimension
        }
    }

    #[test]
    fn test_client_creation() {
        let embedding_fn = MockEmbeddingFunction::new(3);
        let client = VectorLiteClient::new(Box::new(embedding_fn));
        
        assert!(client.collections.is_empty());
        assert!(client.list_collections().is_empty());
    }

    #[test]
    fn test_create_collection() {
        let embedding_fn = MockEmbeddingFunction::new(3);
        let mut client = VectorLiteClient::new(Box::new(embedding_fn));
        
        // Create collection
        let result = client.create_collection("test_collection", IndexType::Flat);
        assert!(result.is_ok());
        
        // Check collection exists
        assert!(client.has_collection("test_collection"));
        assert_eq!(client.list_collections(), vec!["test_collection"]);
    }

    #[test]
    fn test_create_duplicate_collection() {
        let embedding_fn = MockEmbeddingFunction::new(3);
        let mut client = VectorLiteClient::new(Box::new(embedding_fn));
        
        // Create first collection
        client.create_collection("test_collection", IndexType::Flat).unwrap();
        
        // Try to create duplicate
        let result = client.create_collection("test_collection", IndexType::Flat);
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), VectorLiteError::CollectionAlreadyExists { .. }));
    }

    #[test]
    fn test_get_collection() {
        let embedding_fn = MockEmbeddingFunction::new(3);
        let mut client = VectorLiteClient::new(Box::new(embedding_fn));
        
        // Create collection
        client.create_collection("test_collection", IndexType::Flat).unwrap();
        
        // Get collection
        let collection = client.get_collection("test_collection");
        assert!(collection.is_some());
        assert_eq!(collection.unwrap().name(), "test_collection");
        
        // Get non-existent collection
        let collection = client.get_collection("non_existent");
        assert!(collection.is_none());
    }

    #[test]
    fn test_delete_collection() {
        let embedding_fn = MockEmbeddingFunction::new(3);
        let mut client = VectorLiteClient::new(Box::new(embedding_fn));
        
        // Create collection
        client.create_collection("test_collection", IndexType::Flat).unwrap();
        assert!(client.has_collection("test_collection"));
        
        // Delete collection
        let result = client.delete_collection("test_collection");
        assert!(result.is_ok());
        assert!(!client.has_collection("test_collection"));
        
        // Try to delete non-existent collection
        let result = client.delete_collection("non_existent");
        assert!(result.is_err());
    }

    #[test]
    fn test_add_text_to_collection() {
        let embedding_fn = MockEmbeddingFunction::new(3);
        let mut client = VectorLiteClient::new(Box::new(embedding_fn));
        
        // Create collection
        client.create_collection("test_collection", IndexType::Flat).unwrap();
        
        // Add text
        let result = client.add_text_to_collection("test_collection", "Hello world", None);
        assert!(result.is_ok());
        let id = result.unwrap();
        assert_eq!(id, 0); // First ID is 0
        
        // Add another text
        let result = client.add_text_to_collection("test_collection", "Another text", None);
        assert!(result.is_ok());
        let id = result.unwrap();
        assert_eq!(id, 1);
        
        // Check collection count
        let info = client.get_collection_info("test_collection").unwrap();
        assert_eq!(info.count, 2);
    }

    #[test]
    fn test_add_text_to_nonexistent_collection() {
        let embedding_fn = MockEmbeddingFunction::new(3);
        let client = VectorLiteClient::new(Box::new(embedding_fn));
        
        // Try to add to non-existent collection
        let result = client.add_text_to_collection("non_existent", "Hello world", None);
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), VectorLiteError::CollectionNotFound { .. }));
    }

    #[test]
    fn test_collection_operations() {
        let embedding_fn = MockEmbeddingFunction::new(3);
        let mut client = VectorLiteClient::new(Box::new(embedding_fn));
        
        // Create collection
        client.create_collection("test_collection", IndexType::Flat).unwrap();
        
        // Test initial state
        let info = client.get_collection_info("test_collection").unwrap();
        assert!(info.is_empty);
        assert_eq!(info.count, 0);
        assert_eq!(info.name, "test_collection");
        
        // Add text
        let id = client.add_text_to_collection("test_collection", "Hello world", None).unwrap();
        assert_eq!(id, 0);
        
        let info = client.get_collection_info("test_collection").unwrap();
        assert!(!info.is_empty);
        assert_eq!(info.count, 1);
        
        // Add another text
        let id = client.add_text_to_collection("test_collection", "Another text", None).unwrap();
        assert_eq!(id, 1);
        
        let info = client.get_collection_info("test_collection").unwrap();
        assert_eq!(info.count, 2);
        
        // Test search
        let results = client.search_text_in_collection("test_collection", "Hello", 1, SimilarityMetric::Cosine).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].id, 0);
        
        // Test get vector
        let vector = client.get_vector_from_collection("test_collection", 0).unwrap();
        assert!(vector.is_some());
        assert_eq!(vector.unwrap().id, 0);
        
        // Test delete
        client.delete_from_collection("test_collection", 0).unwrap();
        
        let info = client.get_collection_info("test_collection").unwrap();
        assert_eq!(info.count, 1);
        
        // Verify vector is gone
        let vector = client.get_vector_from_collection("test_collection", 0).unwrap();
        assert!(vector.is_none());
    }

    #[test]
    fn test_collection_with_hnsw_index() {
        let embedding_fn = MockEmbeddingFunction::new(3);
        let mut client = VectorLiteClient::new(Box::new(embedding_fn));
        
        // Create HNSW collection
        client.create_collection("hnsw_collection", IndexType::HNSW).unwrap();
        
        // Add some text
        let id1 = client.add_text_to_collection("hnsw_collection", "First document", None).unwrap();
        let id2 = client.add_text_to_collection("hnsw_collection", "Second document", None).unwrap();
        
        assert_eq!(id1, 0);
        assert_eq!(id2, 1);
        
        let info = client.get_collection_info("hnsw_collection").unwrap();
        assert_eq!(info.count, 2);
        
        // Test search
        let results = client.search_text_in_collection("hnsw_collection", "First", 1, SimilarityMetric::Cosine).unwrap();
        assert_eq!(results.len(), 1);
    }



    #[test]
    fn test_collection_save_and_load() {
        let embedding_fn = MockEmbeddingFunction::new(3);
        let mut client = VectorLiteClient::new(Box::new(embedding_fn));
        
        // Create collection and add some data
        client.create_collection("test_collection", IndexType::Flat).unwrap();
        client.add_text_to_collection("test_collection", "Hello world", None).unwrap();
        client.add_text_to_collection("test_collection", "Another text", None).unwrap();
        
        let collection = client.get_collection("test_collection").unwrap();
        
        // Save to temporary file
        let temp_dir = tempfile::TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test_collection.vlc");
        
        collection.save_to_file(&file_path).unwrap();
        assert!(file_path.exists());
        
        // Load the collection
        let loaded_collection = Collection::load_from_file(&file_path).unwrap();
        
        // Verify basic properties
        assert_eq!(loaded_collection.name(), "test_collection");
        
        // Verify the index works
        let info = loaded_collection.get_info().unwrap();
        assert_eq!(info.count, 2);
        assert_eq!(info.dimension, 3);
        assert!(!info.is_empty);
        
        // Test search functionality using text search
        let test_embedding_fn = MockEmbeddingFunction::new(3);
        let results = loaded_collection.search_text("Hello", 2, SimilarityMetric::Cosine, &test_embedding_fn).unwrap();
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_collection_save_and_load_hnsw() {
        let embedding_fn = MockEmbeddingFunction::new(3);
        let mut client = VectorLiteClient::new(Box::new(embedding_fn));
        
        // Create HNSW collection and add some data
        client.create_collection("test_hnsw_collection", IndexType::HNSW).unwrap();
        client.add_text_to_collection("test_hnsw_collection", "First document", None).unwrap();
        client.add_text_to_collection("test_hnsw_collection", "Second document", None).unwrap();
        
        let collection = client.get_collection("test_hnsw_collection").unwrap();
        
        // Verify the original collection works
        let info = collection.get_info().unwrap();
        assert_eq!(info.count, 2);
        assert_eq!(info.dimension, 3);
        
        // Create a separate embedding function for testing
        let test_embedding_fn = MockEmbeddingFunction::new(3);
        
        // Test search on original collection using text search (like the working test)
        let results = collection.search_text("First", 1, SimilarityMetric::Cosine, &test_embedding_fn).unwrap();
        assert_eq!(results.len(), 1);
        
        // Save to temporary file
        let temp_dir = tempfile::TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test_hnsw_collection.vlc");
        
        collection.save_to_file(&file_path).unwrap();
        assert!(file_path.exists());
        
        // Load the collection
        let loaded_collection = Collection::load_from_file(&file_path).unwrap();
        
        // Verify basic properties
        assert_eq!(loaded_collection.name(), "test_hnsw_collection");
        
        // Verify the index works
        let info = loaded_collection.get_info().unwrap();
        assert_eq!(info.count, 2);
        assert_eq!(info.dimension, 3);
        assert!(!info.is_empty);
        
        // Test search functionality using text search
        let results = loaded_collection.search_text("First", 1, SimilarityMetric::Cosine, &test_embedding_fn).unwrap();
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn test_collection_save_nonexistent_directory() {
        let embedding_fn = MockEmbeddingFunction::new(3);
        let mut client = VectorLiteClient::new(Box::new(embedding_fn));
        
        client.create_collection("test_collection", IndexType::Flat).unwrap();
        client.add_text_to_collection("test_collection", "Hello world", None).unwrap();
        
        let collection = client.get_collection("test_collection").unwrap();
        
        // Try to save to a non-existent directory (should create it)
        let temp_dir = tempfile::TempDir::new().unwrap();
        let file_path = temp_dir.path().join("nonexistent").join("test_collection.vlc");
        
        let result = collection.save_to_file(&file_path);
        assert!(result.is_ok());
        assert!(file_path.exists());
    }

    #[test]
    fn test_collection_load_nonexistent_file() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let file_path = temp_dir.path().join("nonexistent.vlc");
        
        let result = Collection::load_from_file(&file_path);
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), PersistenceError::Io(_)));
    }

    #[test]
    fn test_collection_load_invalid_json() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let file_path = temp_dir.path().join("invalid.vlc");
        
        // Write invalid JSON
        std::fs::write(&file_path, "invalid json content").unwrap();
        
        let result = Collection::load_from_file(&file_path);
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), PersistenceError::Serialization(_)));
    }
}