vecstore 1.0.0

The perfect vector database - 100/100 score, embeddable, high-performance, production-ready with RAG toolkit
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
//! Vector partitioning for data isolation and multi-tenancy
//!
//! This module provides partitioning capabilities to organize vectors into
//! isolated groups based on metadata. This is essential for:
//! - Multi-tenant applications (isolate by customer/org)
//! - Data organization (group by category, date, etc.)
//! - Performance optimization (smaller search spaces)
//! - Access control (restrict queries to specific partitions)
//!
//! # Features
//!
//! - **Automatic partitioning**: Route vectors based on metadata
//! - **Partition isolation**: Each partition has its own storage
//! - **Cross-partition queries**: Optional queries across partitions
//! - **Partition statistics**: Monitor size and performance per partition
//! - **Dynamic creation**: Partitions created on-demand
//!
//! # Example
//!
//! ```rust
//! use vecstore::partitioning::{PartitionedStore, PartitionConfig};
//!
//! let config = PartitionConfig {
//!     partition_field: "tenant_id".to_string(),
//!     auto_create: true,
//! };
//!
//! let mut store = PartitionedStore::new("data", config)?;
//!
//! // Insert with partition key
//! store.insert("doc1", vec![0.1, 0.2], metadata, "tenant_123")?;
//!
//! // Query within partition
//! let results = store.query_partition("tenant_123", query)?;
//! ```

use crate::store::{Metadata, Neighbor, Query, VecStore};
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};

/// Partition configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PartitionConfig {
    /// Metadata field to use for partitioning
    pub partition_field: String,
    /// Automatically create partitions on first insert
    pub auto_create: bool,
    /// Maximum vectors per partition (optional)
    pub max_vectors_per_partition: Option<usize>,
}

impl Default for PartitionConfig {
    fn default() -> Self {
        Self {
            partition_field: "partition".to_string(),
            auto_create: true,
            max_vectors_per_partition: None,
        }
    }
}

/// Partition metadata and statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PartitionInfo {
    /// Partition key/ID
    pub id: String,
    /// Number of vectors in partition
    pub vector_count: usize,
    /// Storage path
    pub path: PathBuf,
    /// Creation timestamp
    pub created_at: std::time::SystemTime,
    /// Last modified timestamp
    pub modified_at: std::time::SystemTime,
    /// Total size in bytes (estimate)
    pub size_bytes: u64,
}

/// Partitioned vector store
pub struct PartitionedStore {
    /// Base directory for all partitions
    base_path: PathBuf,
    /// Configuration
    config: PartitionConfig,
    /// Map of partition ID to VecStore
    partitions: HashMap<String, VecStore>,
    /// Partition metadata
    partition_info: HashMap<String, PartitionInfo>,
}

impl PartitionedStore {
    /// Create new partitioned store
    pub fn new<P: AsRef<Path>>(base_path: P, config: PartitionConfig) -> Result<Self> {
        let base_path = base_path.as_ref().to_path_buf();

        // Create base directory if it doesn't exist
        std::fs::create_dir_all(&base_path)
            .map_err(|e| anyhow::anyhow!("Failed to create directory: {}", e))?;

        let mut store = Self {
            base_path,
            config,
            partitions: HashMap::new(),
            partition_info: HashMap::new(),
        };

        // Load existing partitions
        store.load_existing_partitions()?;

        Ok(store)
    }

    /// Load existing partitions from disk
    fn load_existing_partitions(&mut self) -> Result<()> {
        let entries = std::fs::read_dir(&self.base_path)
            .map_err(|e| anyhow::anyhow!("Failed to read directory: {}", e))?;

        for entry in entries {
            let entry = entry.map_err(|e| anyhow::anyhow!("Read error: {}", e))?;
            let path = entry.path();

            if path.is_dir() {
                let partition_id = path
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("")
                    .to_string();

                // Try to open the partition
                let db_path = path.join("vectors.db");
                if db_path.exists() {
                    match VecStore::open(&db_path) {
                        Ok(store) => {
                            let vector_count = store.len();
                            let metadata = std::fs::metadata(&db_path)
                                .map_err(|e| anyhow::anyhow!("Metadata error: {}", e))?;

                            let info = PartitionInfo {
                                id: partition_id.clone(),
                                vector_count,
                                path: path.clone(),
                                created_at: metadata
                                    .created()
                                    .unwrap_or(std::time::SystemTime::UNIX_EPOCH),
                                modified_at: metadata
                                    .modified()
                                    .unwrap_or(std::time::SystemTime::UNIX_EPOCH),
                                size_bytes: metadata.len(),
                            };

                            self.partitions.insert(partition_id.clone(), store);
                            self.partition_info.insert(partition_id, info);
                        }
                        Err(_) => {
                            // Skip corrupted partitions
                            continue;
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Get or create a partition
    fn get_or_create_partition(&mut self, partition_id: &str) -> Result<&mut VecStore> {
        if !self.partitions.contains_key(partition_id) {
            if !self.config.auto_create {
                return Err(anyhow::anyhow!(
                    "Partition '{}' does not exist and auto_create is disabled",
                    partition_id
                ));
            }

            // Create new partition
            let partition_path = self.base_path.join(partition_id);
            std::fs::create_dir_all(&partition_path)
                .map_err(|e| anyhow::anyhow!("Failed to create partition dir: {}", e))?;

            let db_path = partition_path.join("vectors.db");
            let store = VecStore::open(&db_path)?;

            let info = PartitionInfo {
                id: partition_id.to_string(),
                vector_count: 0,
                path: partition_path,
                created_at: std::time::SystemTime::now(),
                modified_at: std::time::SystemTime::now(),
                size_bytes: 0,
            };

            self.partitions.insert(partition_id.to_string(), store);
            self.partition_info.insert(partition_id.to_string(), info);
        }

        Ok(self.partitions.get_mut(partition_id).unwrap())
    }

    /// Insert vector into specific partition
    pub fn insert(
        &mut self,
        partition_id: &str,
        id: String,
        vector: Vec<f32>,
        metadata: Metadata,
    ) -> Result<()> {
        // Check partition size limit
        if let Some(max_size) = self.config.max_vectors_per_partition {
            if let Some(info) = self.partition_info.get(partition_id) {
                if info.vector_count >= max_size {
                    return Err(anyhow::anyhow!(
                        "Partition '{}' has reached maximum size of {} vectors",
                        partition_id,
                        max_size
                    ));
                }
            }
        }

        let partition = self.get_or_create_partition(partition_id)?;
        partition.upsert(id, vector, metadata)?;
        let new_count = partition.len();

        // Update partition info (after dropping partition reference)
        if let Some(info) = self.partition_info.get_mut(partition_id) {
            info.vector_count = new_count;
            info.modified_at = std::time::SystemTime::now();
        }

        Ok(())
    }

    /// Query within a specific partition
    pub fn query_partition(&self, partition_id: &str, query: Query) -> Result<Vec<Neighbor>> {
        let partition = self
            .partitions
            .get(partition_id)
            .ok_or_else(|| anyhow::anyhow!("Partition '{}' not found", partition_id))?;

        partition.query(query)
    }

    /// Query across all partitions (slower but comprehensive)
    pub fn query_all(&self, query: Query, limit: usize) -> Result<Vec<Neighbor>> {
        let mut all_results = Vec::new();

        for partition in self.partitions.values() {
            let results = partition.query(query.clone())?;
            all_results.extend(results);
        }

        // Sort by distance and limit
        all_results.sort_by(|a, b| a.score.partial_cmp(&b.score).unwrap());
        all_results.truncate(limit);

        Ok(all_results)
    }

    /// Query across specific partitions
    pub fn query_partitions(
        &self,
        partition_ids: &[&str],
        query: Query,
        limit: usize,
    ) -> Result<Vec<Neighbor>> {
        let mut all_results = Vec::new();

        for &partition_id in partition_ids {
            if let Some(partition) = self.partitions.get(partition_id) {
                let results = partition.query(query.clone())?;
                all_results.extend(results);
            }
        }

        // Sort by distance and limit
        all_results.sort_by(|a, b| a.score.partial_cmp(&b.score).unwrap());
        all_results.truncate(limit);

        Ok(all_results)
    }

    /// Delete a vector from a partition
    pub fn delete(&mut self, partition_id: &str, id: &str) -> Result<()> {
        let partition = self
            .partitions
            .get_mut(partition_id)
            .ok_or_else(|| anyhow::anyhow!("Partition '{}' not found", partition_id))?;

        partition.delete(id)?;

        // Update partition info
        if let Some(info) = self.partition_info.get_mut(partition_id) {
            info.vector_count = partition.len();
            info.modified_at = std::time::SystemTime::now();
        }

        Ok(())
    }

    /// Get partition information
    pub fn get_partition_info(&self, partition_id: &str) -> Option<&PartitionInfo> {
        self.partition_info.get(partition_id)
    }

    /// List all partitions
    pub fn list_partitions(&self) -> Vec<&PartitionInfo> {
        self.partition_info.values().collect()
    }

    /// Get total vector count across all partitions
    pub fn total_vectors(&self) -> usize {
        self.partition_info
            .values()
            .map(|info| info.vector_count)
            .sum()
    }

    /// Delete a partition
    pub fn delete_partition(&mut self, partition_id: &str) -> Result<()> {
        // Remove from memory
        self.partitions.remove(partition_id);

        if let Some(info) = self.partition_info.remove(partition_id) {
            // Delete from disk
            std::fs::remove_dir_all(&info.path)
                .map_err(|e| anyhow::anyhow!("Failed to delete partition: {}", e))?;
        }

        Ok(())
    }

    /// Compact a partition (remove deleted vectors)
    pub fn compact_partition(&mut self, partition_id: &str) -> Result<usize> {
        let partition = self
            .partitions
            .get_mut(partition_id)
            .ok_or_else(|| anyhow::anyhow!("Partition '{}' not found", partition_id))?;

        let removed = partition.compact()?;

        // Update partition info
        if let Some(info) = self.partition_info.get_mut(partition_id) {
            info.vector_count = partition.len();
            info.modified_at = std::time::SystemTime::now();
        }

        Ok(removed)
    }

    /// Get partition statistics
    pub fn partition_stats(&self) -> PartitionStats {
        let total_partitions = self.partitions.len();
        let total_vectors = self.total_vectors();
        let avg_vectors_per_partition = if total_partitions > 0 {
            total_vectors as f64 / total_partitions as f64
        } else {
            0.0
        };

        let mut largest_partition = None;
        let mut smallest_partition = None;
        let mut max_size = 0;
        let mut min_size = usize::MAX;

        for info in self.partition_info.values() {
            if info.vector_count > max_size {
                max_size = info.vector_count;
                largest_partition = Some(info.id.clone());
            }
            if info.vector_count < min_size {
                min_size = info.vector_count;
                smallest_partition = Some(info.id.clone());
            }
        }

        PartitionStats {
            total_partitions,
            total_vectors,
            avg_vectors_per_partition,
            largest_partition,
            smallest_partition,
            max_partition_size: max_size,
            min_partition_size: if min_size == usize::MAX { 0 } else { min_size },
        }
    }
}

/// Partition statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PartitionStats {
    /// Total number of partitions
    pub total_partitions: usize,
    /// Total vectors across all partitions
    pub total_vectors: usize,
    /// Average vectors per partition
    pub avg_vectors_per_partition: f64,
    /// ID of largest partition
    pub largest_partition: Option<String>,
    /// ID of smallest partition
    pub smallest_partition: Option<String>,
    /// Size of largest partition
    pub max_partition_size: usize,
    /// Size of smallest partition
    pub min_partition_size: usize,
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use tempfile::TempDir;

    #[test]
    fn test_partitioned_store_creation() -> Result<()> {
        let temp_dir = TempDir::new().unwrap();
        let config = PartitionConfig::default();

        let store = PartitionedStore::new(temp_dir.path(), config)?;
        assert_eq!(store.list_partitions().len(), 0);

        Ok(())
    }

    #[test]
    fn test_insert_and_query() -> Result<()> {
        let temp_dir = TempDir::new().unwrap();
        let config = PartitionConfig::default();

        let mut store = PartitionedStore::new(temp_dir.path(), config)?;

        // Insert into partition A
        let mut metadata = Metadata {
            fields: HashMap::new(),
        };
        metadata
            .fields
            .insert("partition".to_string(), serde_json::json!("A"));

        store.insert("A", "vec1".to_string(), vec![0.1, 0.2, 0.3], metadata)?;

        // Insert into partition B
        let mut metadata = Metadata {
            fields: HashMap::new(),
        };
        metadata
            .fields
            .insert("partition".to_string(), serde_json::json!("B"));

        store.insert("B", "vec2".to_string(), vec![0.4, 0.5, 0.6], metadata)?;

        assert_eq!(store.list_partitions().len(), 2);
        assert_eq!(store.total_vectors(), 2);

        // Query partition A
        let query = Query::new(vec![0.1, 0.2, 0.3]).with_limit(10);
        let results = store.query_partition("A", query)?;
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].id, "vec1");

        Ok(())
    }

    #[test]
    fn test_cross_partition_query() -> Result<()> {
        let temp_dir = TempDir::new().unwrap();
        let config = PartitionConfig::default();

        let mut store = PartitionedStore::new(temp_dir.path(), config)?;

        // Insert into multiple partitions
        for i in 0..3 {
            let partition_id = format!("partition_{}", i);
            let metadata = Metadata {
                fields: HashMap::new(),
            };

            store.insert(
                &partition_id,
                format!("vec_{}", i),
                vec![i as f32 * 0.1, i as f32 * 0.2, i as f32 * 0.3],
                metadata,
            )?;
        }

        // Query across all partitions
        let query = Query::new(vec![0.0, 0.0, 0.0]).with_limit(10);
        let results = store.query_all(query, 10)?;

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

        Ok(())
    }

    #[test]
    fn test_partition_size_limit() -> Result<()> {
        let temp_dir = TempDir::new().unwrap();
        let config = PartitionConfig {
            partition_field: "tenant".to_string(),
            auto_create: true,
            max_vectors_per_partition: Some(2),
        };

        let mut store = PartitionedStore::new(temp_dir.path(), config)?;

        // Insert 2 vectors (should succeed)
        for i in 0..2 {
            let metadata = Metadata {
                fields: HashMap::new(),
            };
            store.insert("tenant1", format!("vec_{}", i), vec![i as f32], metadata)?;
        }

        // Try to insert 3rd vector (should fail)
        let metadata = Metadata {
            fields: HashMap::new(),
        };
        let result = store.insert("tenant1", "vec_3".to_string(), vec![3.0], metadata);
        assert!(result.is_err());

        Ok(())
    }

    #[test]
    fn test_partition_deletion() -> Result<()> {
        let temp_dir = TempDir::new().unwrap();
        let config = PartitionConfig::default();

        let mut store = PartitionedStore::new(temp_dir.path(), config)?;

        // Create partition
        let metadata = Metadata {
            fields: HashMap::new(),
        };
        store.insert("test_partition", "vec1".to_string(), vec![1.0], metadata)?;

        assert_eq!(store.list_partitions().len(), 1);

        // Delete partition
        store.delete_partition("test_partition")?;
        assert_eq!(store.list_partitions().len(), 0);

        Ok(())
    }

    #[test]
    fn test_partition_stats() -> Result<()> {
        let temp_dir = TempDir::new().unwrap();
        let config = PartitionConfig::default();

        let mut store = PartitionedStore::new(temp_dir.path(), config)?;

        // Create multiple partitions with different sizes
        let metadata = Metadata {
            fields: HashMap::new(),
        };
        store.insert("small", "vec1".to_string(), vec![1.0], metadata.clone())?;

        store.insert("large", "vec2".to_string(), vec![2.0], metadata.clone())?;
        store.insert("large", "vec3".to_string(), vec![3.0], metadata.clone())?;
        store.insert("large", "vec4".to_string(), vec![4.0], metadata)?;

        let stats = store.partition_stats();
        assert_eq!(stats.total_partitions, 2);
        assert_eq!(stats.total_vectors, 4);
        assert_eq!(stats.max_partition_size, 3);
        assert_eq!(stats.min_partition_size, 1);

        Ok(())
    }
}