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
//! Bulk migration tools for importing from other vector databases
//!
//! This module provides specialized importers for migrating data from
//! popular vector databases to VecStore:
//! - Pinecone export files
//! - Qdrant snapshots
//! - Weaviate backups
//! - ChromaDB exports
//! - Milvus dumps
//!
//! # Features
//!
//! - Batch processing with progress tracking
//! - Automatic schema mapping
//! - Resume capability for large migrations
//! - Validation and error reporting
//! - Memory-efficient streaming
//!
//! # Example
//!
//! ```rust
//! use vecstore::bulk_migration::{PineconeMigration, MigrationConfig};
//!
//! let config = MigrationConfig {
//!     batch_size: 1000,
//!     validate: true,
//!     resume_from: None,
//! };
//!
//! let migration = PineconeMigration::new(config);
//! let stats = migration.import_from_file("pinecone_export.json", &mut store)?;
//!
//! println!("Migrated {} vectors in {:?}", stats.total_vectors, stats.duration);
//! ```

use crate::store::{Metadata, VecStore};
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::time::{Duration, Instant};

/// Migration configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MigrationConfig {
    /// Batch size for bulk inserts
    pub batch_size: usize,
    /// Validate data before inserting
    pub validate: bool,
    /// Resume from specific offset
    pub resume_from: Option<usize>,
}

impl Default for MigrationConfig {
    fn default() -> Self {
        Self {
            batch_size: 1000,
            validate: true,
            resume_from: None,
        }
    }
}

/// Bulk migration statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BulkMigrationStats {
    /// Total vectors migrated
    pub total_vectors: usize,
    /// Number of errors encountered
    pub errors: usize,
    /// Duration of migration
    pub duration: Duration,
    /// Bytes processed
    pub bytes_processed: u64,
    /// Average throughput (vectors/sec)
    pub throughput: f64,
}

/// Progress callback function type
pub type ProgressCallback = Box<dyn Fn(usize, usize) + Send>;

/// Pinecone format migration
pub struct PineconeMigration {
    config: MigrationConfig,
    progress_callback: Option<ProgressCallback>,
}

impl PineconeMigration {
    /// Create new Pinecone migration
    pub fn new(config: MigrationConfig) -> Self {
        Self {
            config,
            progress_callback: None,
        }
    }

    /// Set progress callback
    pub fn with_progress<F>(mut self, callback: F) -> Self
    where
        F: Fn(usize, usize) + Send + 'static,
    {
        self.progress_callback = Some(Box::new(callback));
        self
    }

    /// Import from Pinecone JSON export file
    ///
    /// Expected format:
    /// ```json
    /// {
    ///   "vectors": [
    ///     {
    ///       "id": "vec1",
    ///       "values": [0.1, 0.2, 0.3],
    ///       "metadata": {"key": "value"}
    ///     }
    ///   ]
    /// }
    /// ```
    pub fn import_from_file(&self, path: &str, store: &mut VecStore) -> Result<BulkMigrationStats> {
        let start = Instant::now();
        let file = File::open(path).map_err(|e| anyhow::anyhow!("Failed to open file: {}", e))?;

        let reader = BufReader::new(file);
        let data: serde_json::Value = serde_json::from_reader(reader)
            .map_err(|e| anyhow::anyhow!("Failed to parse JSON: {}", e))?;

        let vectors = data["vectors"]
            .as_array()
            .ok_or_else(|| anyhow::anyhow!("No 'vectors' array found".to_string()))?;

        let total = vectors.len();
        let mut migrated = 0;
        let mut errors = 0;
        let mut bytes = 0u64;

        let start_offset = self.config.resume_from.unwrap_or(0);

        for (i, vector_data) in vectors.iter().enumerate().skip(start_offset) {
            if i % self.config.batch_size == 0 {
                if let Some(ref callback) = self.progress_callback {
                    callback(migrated, total);
                }
            }

            match self.import_vector(vector_data, store) {
                Ok(size) => {
                    migrated += 1;
                    bytes += size;
                }
                Err(_) => {
                    errors += 1;
                }
            }
        }

        let duration = start.elapsed();
        let throughput = migrated as f64 / duration.as_secs_f64();

        Ok(BulkMigrationStats {
            total_vectors: migrated,
            errors,
            duration,
            bytes_processed: bytes,
            throughput,
        })
    }

    /// Import a single Pinecone vector
    fn import_vector(&self, data: &serde_json::Value, store: &mut VecStore) -> Result<u64> {
        let id = data["id"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing 'id' field"))?
            .to_string();

        let values: Vec<f32> = data["values"]
            .as_array()
            .ok_or_else(|| anyhow::anyhow!("Missing 'values' field"))?
            .iter()
            .map(|v| v.as_f64().unwrap_or(0.0) as f32)
            .collect();

        if self.config.validate && values.is_empty() {
            return Err(anyhow::anyhow!("Empty vector values"));
        }

        let metadata = if let Some(meta) = data.get("metadata") {
            let fields: HashMap<String, serde_json::Value> =
                serde_json::from_value(meta.clone()).unwrap_or_default();
            Metadata { fields }
        } else {
            Metadata {
                fields: HashMap::new(),
            }
        };

        store.upsert(id.clone(), values.clone(), metadata)?;

        // Estimate size
        let size = id.len() + values.len() * 4 + 100; // Rough estimate
        Ok(size as u64)
    }
}

/// Qdrant format migration
pub struct QdrantMigration {
    config: MigrationConfig,
}

impl QdrantMigration {
    /// Create new Qdrant migration
    pub fn new(config: MigrationConfig) -> Self {
        Self { config }
    }

    /// Import from Qdrant snapshot (JSONL format)
    ///
    /// Expected format per line:
    /// ```json
    /// {
    ///   "id": 1,
    ///   "vector": [0.1, 0.2, 0.3],
    ///   "payload": {"key": "value"}
    /// }
    /// ```
    pub fn import_from_jsonl(
        &self,
        path: &str,
        store: &mut VecStore,
    ) -> Result<BulkMigrationStats> {
        let start = Instant::now();
        let file = File::open(path).map_err(|e| anyhow::anyhow!("Failed to open file: {}", e))?;

        let reader = BufReader::new(file);
        let mut migrated = 0;
        let mut errors = 0;
        let mut bytes = 0u64;

        for (i, line) in reader.lines().enumerate() {
            if let Some(offset) = self.config.resume_from {
                if i < offset {
                    continue;
                }
            }

            let line = line.map_err(|e| anyhow::anyhow!("Read error: {}", e))?;

            match self.import_point(&line, store) {
                Ok(size) => {
                    migrated += 1;
                    bytes += size;
                }
                Err(_) => {
                    errors += 1;
                }
            }
        }

        let duration = start.elapsed();
        let throughput = migrated as f64 / duration.as_secs_f64();

        Ok(BulkMigrationStats {
            total_vectors: migrated,
            errors,
            duration,
            bytes_processed: bytes,
            throughput,
        })
    }

    fn import_point(&self, line: &str, store: &mut VecStore) -> Result<u64> {
        let data: serde_json::Value =
            serde_json::from_str(line).map_err(|e| anyhow::anyhow!("Parse error: {}", e))?;

        let id = data["id"].to_string().trim_matches('"').to_string();

        let vector: Vec<f32> = data["vector"]
            .as_array()
            .ok_or_else(|| anyhow::anyhow!("Missing vector".to_string()))?
            .iter()
            .map(|v| v.as_f64().unwrap_or(0.0) as f32)
            .collect();

        let metadata = if let Some(payload) = data.get("payload") {
            let fields: HashMap<String, serde_json::Value> =
                serde_json::from_value(payload.clone()).unwrap_or_default();
            Metadata { fields }
        } else {
            Metadata {
                fields: HashMap::new(),
            }
        };

        store.upsert(id.clone(), vector.clone(), metadata)?;

        Ok((id.len() + vector.len() * 4 + 100) as u64)
    }
}

/// ChromaDB format migration
pub struct ChromaDBMigration {
    config: MigrationConfig,
}

impl ChromaDBMigration {
    /// Create new ChromaDB migration
    pub fn new(config: MigrationConfig) -> Self {
        Self { config }
    }

    /// Import from ChromaDB export
    ///
    /// Expected format:
    /// ```json
    /// {
    ///   "ids": ["id1", "id2"],
    ///   "embeddings": [[0.1, 0.2], [0.3, 0.4]],
    ///   "metadatas": [{"key": "value"}, {"key2": "value2"}]
    /// }
    /// ```
    pub fn import_from_file(&self, path: &str, store: &mut VecStore) -> Result<BulkMigrationStats> {
        let start = Instant::now();
        let file = File::open(path).map_err(|e| anyhow::anyhow!("Failed to open file: {}", e))?;

        let reader = BufReader::new(file);
        let data: serde_json::Value = serde_json::from_reader(reader)
            .map_err(|e| anyhow::anyhow!("Failed to parse JSON: {}", e))?;

        let ids: Vec<String> = data["ids"]
            .as_array()
            .ok_or_else(|| anyhow::anyhow!("Missing 'ids'".to_string()))?
            .iter()
            .map(|v| v.as_str().unwrap_or("").to_string())
            .collect();

        let embeddings: Vec<Vec<f32>> = data["embeddings"]
            .as_array()
            .ok_or_else(|| anyhow::anyhow!("Missing 'embeddings'".to_string()))?
            .iter()
            .map(|arr| {
                arr.as_array()
                    .unwrap_or(&Vec::new())
                    .iter()
                    .map(|v| v.as_f64().unwrap_or(0.0) as f32)
                    .collect()
            })
            .collect();

        let metadatas: Vec<HashMap<String, serde_json::Value>> = data
            .get("metadatas")
            .and_then(|m| m.as_array())
            .map(|arr| {
                arr.iter()
                    .map(|v| serde_json::from_value(v.clone()).unwrap_or_default())
                    .collect()
            })
            .unwrap_or_else(|| vec![HashMap::new(); ids.len()]);

        let mut migrated = 0;
        let mut errors = 0;
        let mut bytes = 0u64;

        for ((id, embedding), metadata) in ids.iter().zip(embeddings.iter()).zip(metadatas.iter()) {
            match store.upsert(
                id.clone(),
                embedding.clone(),
                Metadata {
                    fields: metadata.clone(),
                },
            ) {
                Ok(_) => {
                    migrated += 1;
                    bytes += (id.len() + embedding.len() * 4 + 100) as u64;
                }
                Err(_) => {
                    errors += 1;
                }
            }
        }

        let duration = start.elapsed();
        let throughput = migrated as f64 / duration.as_secs_f64();

        Ok(BulkMigrationStats {
            total_vectors: migrated,
            errors,
            duration,
            bytes_processed: bytes,
            throughput,
        })
    }
}

/// Universal format converter
pub struct FormatConverter;

impl FormatConverter {
    /// Convert Pinecone format to universal JSONL
    pub fn pinecone_to_jsonl(input: &str, output: &str) -> Result<usize> {
        let file = File::open(input).map_err(|e| anyhow::anyhow!("Failed to open input: {}", e))?;

        let reader = BufReader::new(file);
        let data: serde_json::Value = serde_json::from_reader(reader)
            .map_err(|e| anyhow::anyhow!("Failed to parse JSON: {}", e))?;

        let vectors = data["vectors"]
            .as_array()
            .ok_or_else(|| anyhow::anyhow!("No 'vectors' array".to_string()))?;

        let mut output_file =
            File::create(output).map_err(|e| anyhow::anyhow!("Failed to create output: {}", e))?;

        use std::io::Write;

        for vector in vectors {
            let universal = serde_json::json!({
                "id": vector["id"],
                "vector": vector["values"],
                "metadata": vector.get("metadata").unwrap_or(&serde_json::Value::Null)
            });

            writeln!(output_file, "{}", universal)
                .map_err(|e| anyhow::anyhow!("Write error: {}", e))?;
        }

        Ok(vectors.len())
    }

    /// Convert Qdrant format to universal JSONL (already in JSONL)
    pub fn qdrant_to_jsonl(input: &str, output: &str) -> Result<usize> {
        let in_file =
            File::open(input).map_err(|e| anyhow::anyhow!("Failed to open input: {}", e))?;

        let mut out_file =
            File::create(output).map_err(|e| anyhow::anyhow!("Failed to create output: {}", e))?;

        let reader = BufReader::new(in_file);
        let mut count = 0;

        use std::io::Write;

        for line in reader.lines() {
            let line = line.map_err(|e| anyhow::anyhow!("Read error: {}", e))?;
            let data: serde_json::Value =
                serde_json::from_str(&line).map_err(|e| anyhow::anyhow!("Parse error: {}", e))?;

            let universal = serde_json::json!({
                "id": data["id"].to_string(),
                "vector": data["vector"],
                "metadata": data.get("payload").unwrap_or(&serde_json::Value::Null)
            });

            writeln!(out_file, "{}", universal)
                .map_err(|e| anyhow::anyhow!("Write error: {}", e))?;

            count += 1;
        }

        Ok(count)
    }
}

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

    #[test]
    fn test_pinecone_migration() -> Result<()> {
        let temp_dir = TempDir::new().unwrap();
        let mut store = VecStore::open(temp_dir.path().join("test.db"))?;

        // Create test file
        let test_data = serde_json::json!({
            "vectors": [
                {
                    "id": "vec1",
                    "values": [0.1, 0.2, 0.3],
                    "metadata": {"category": "test"}
                },
                {
                    "id": "vec2",
                    "values": [0.4, 0.5, 0.6],
                    "metadata": {"category": "prod"}
                }
            ]
        });

        let test_file = temp_dir.path().join("pinecone.json");
        std::fs::write(&test_file, test_data.to_string()).unwrap();

        // Run migration
        let config = MigrationConfig::default();
        let migration = PineconeMigration::new(config);
        let stats = migration.import_from_file(test_file.to_str().unwrap(), &mut store)?;

        assert_eq!(stats.total_vectors, 2);
        assert_eq!(stats.errors, 0);
        assert_eq!(store.len(), 2);

        Ok(())
    }

    #[test]
    fn test_chromadb_migration() -> Result<()> {
        let temp_dir = TempDir::new().unwrap();
        let mut store = VecStore::open(temp_dir.path().join("test.db"))?;

        let test_data = serde_json::json!({
            "ids": ["id1", "id2"],
            "embeddings": [[0.1, 0.2], [0.3, 0.4]],
            "metadatas": [{"key": "val1"}, {"key": "val2"}]
        });

        let test_file = temp_dir.path().join("chroma.json");
        std::fs::write(&test_file, test_data.to_string()).unwrap();

        let config = MigrationConfig::default();
        let migration = ChromaDBMigration::new(config);
        let stats = migration.import_from_file(test_file.to_str().unwrap(), &mut store)?;

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

        Ok(())
    }
}