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
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
//! Real-time Index Updates
//!
//! This module provides real-time indexing capabilities that allow continuous
//! data ingestion without blocking queries. Updates are applied incrementally
//! to the HNSW index while maintaining high query performance.
//!
//! ## Features
//!
//! - **Non-blocking writes**: Inserts don't block queries
//! - **Concurrent reads**: Multiple queries can run during updates
//! - **Background compaction**: Periodic index optimization
//! - **Write-Ahead Log integration**: Durability and crash recovery
//! - **Snapshot isolation**: Queries see consistent index state
//! - **Soft deletes**: Fast deletion without immediate index rebuild
//!
//! ## Architecture
//!
//! ```text
//! ┌─────────────┐
//! │   Writes    │──→ WAL ──→ Write Buffer ──→ Background Worker
//! └─────────────┘                                      │
//!//! ┌─────────────┐                              ┌───────────────┐
//! │   Queries   │────→ Read Snapshot ←────────│  Main Index   │
//! └─────────────┘                              └───────────────┘
//! ```
//!
//! ## Example
//!
//! ```no_run
//! use vecstore::realtime::{RealtimeIndex, RealtimeConfig};
//!
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()> {
//! // Create real-time index
//! let config = RealtimeConfig::default()
//!     .with_buffer_size(1000)
//!     .with_compaction_interval_secs(60);
//!
//! let mut index = RealtimeIndex::open("vectors.db", config)?;
//!
//! // Start background worker
//! index.start_background_worker().await?;
//!
//! // Insert vectors (non-blocking)
//! index.insert("doc1", vec![0.1, 0.2, 0.3]).await?;
//! index.insert("doc2", vec![0.2, 0.3, 0.4]).await?;
//!
//! // Queries see latest data
//! let results = index.query(vec![0.15, 0.25, 0.35], 10).await?;
//!
//! // Soft delete (fast)
//! index.delete("doc1").await?;
//!
//! // Periodic compaction removes deleted items
//! index.compact().await?;
//! # Ok(())
//! # }
//! ```

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::time::Instant;

#[cfg(feature = "async")]
use std::sync::Arc;
#[cfg(feature = "async")]
use std::time::Duration;

#[cfg(feature = "async")]
use tokio::sync::{Mutex, RwLock};
#[cfg(feature = "async")]
use tokio::time::interval;

/// Configuration for real-time indexing
#[derive(Debug, Clone)]
pub struct RealtimeConfig {
    /// Maximum size of write buffer before forcing flush
    pub buffer_size: usize,

    /// Interval between automatic compactions (seconds)
    pub compaction_interval_secs: u64,

    /// Threshold for triggering compaction (fraction of deleted items)
    pub compaction_threshold: f32,

    /// Enable write-ahead logging
    pub enable_wal: bool,

    /// Sync WAL to disk on every write
    pub sync_wal: bool,

    /// Number of background workers
    pub num_workers: usize,
}

impl Default for RealtimeConfig {
    fn default() -> Self {
        Self {
            buffer_size: 1000,
            compaction_interval_secs: 60,
            compaction_threshold: 0.1, // Compact when 10% deleted
            enable_wal: true,
            sync_wal: false, // Async by default for performance
            num_workers: 1,
        }
    }
}

impl RealtimeConfig {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_buffer_size(mut self, size: usize) -> Self {
        self.buffer_size = size;
        self
    }

    pub fn with_compaction_interval_secs(mut self, secs: u64) -> Self {
        self.compaction_interval_secs = secs;
        self
    }

    pub fn with_compaction_threshold(mut self, threshold: f32) -> Self {
        self.compaction_threshold = threshold;
        self
    }

    pub fn with_wal(mut self, enabled: bool) -> Self {
        self.enable_wal = enabled;
        self
    }

    pub fn with_sync_wal(mut self, sync: bool) -> Self {
        self.sync_wal = sync;
        self
    }

    pub fn with_num_workers(mut self, workers: usize) -> Self {
        self.num_workers = workers;
        self
    }
}

/// Write buffer entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BufferEntry {
    Insert {
        id: String,
        vector: Vec<f32>,
        timestamp: u64,
    },
    Update {
        id: String,
        vector: Vec<f32>,
        timestamp: u64,
    },
    Delete {
        id: String,
        timestamp: u64,
    },
}

impl BufferEntry {
    pub fn id(&self) -> &str {
        match self {
            BufferEntry::Insert { id, .. } => id,
            BufferEntry::Update { id, .. } => id,
            BufferEntry::Delete { id, .. } => id,
        }
    }

    pub fn timestamp(&self) -> u64 {
        match self {
            BufferEntry::Insert { timestamp, .. } => *timestamp,
            BufferEntry::Update { timestamp, .. } => *timestamp,
            BufferEntry::Delete { timestamp, .. } => *timestamp,
        }
    }

    pub fn is_delete(&self) -> bool {
        matches!(self, BufferEntry::Delete { .. })
    }
}

/// Write buffer for batching updates
#[derive(Debug, Default)]
pub struct WriteBuffer {
    entries: Vec<BufferEntry>,
    deleted_ids: HashSet<String>,
    next_timestamp: u64,
}

impl WriteBuffer {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn insert(&mut self, id: String, vector: Vec<f32>) {
        let timestamp = self.next_timestamp;
        self.next_timestamp += 1;

        self.entries.push(BufferEntry::Insert {
            id: id.clone(),
            vector,
            timestamp,
        });

        self.deleted_ids.remove(&id);
    }

    pub fn update(&mut self, id: String, vector: Vec<f32>) {
        let timestamp = self.next_timestamp;
        self.next_timestamp += 1;

        self.entries.push(BufferEntry::Update {
            id,
            vector,
            timestamp,
        });
    }

    pub fn delete(&mut self, id: String) {
        let timestamp = self.next_timestamp;
        self.next_timestamp += 1;

        self.entries.push(BufferEntry::Delete {
            id: id.clone(),
            timestamp,
        });

        self.deleted_ids.insert(id);
    }

    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    pub fn is_deleted(&self, id: &str) -> bool {
        self.deleted_ids.contains(id)
    }

    pub fn drain(&mut self) -> Vec<BufferEntry> {
        let entries = std::mem::take(&mut self.entries);
        self.deleted_ids.clear();
        entries
    }

    pub fn clear(&mut self) {
        self.entries.clear();
        self.deleted_ids.clear();
    }

    pub fn deleted_count(&self) -> usize {
        self.deleted_ids.len()
    }
}

/// Snapshot for read consistency
#[derive(Debug, Clone)]
pub struct Snapshot {
    pub timestamp: u64,
    pub deleted_ids: HashSet<String>,
}

impl Snapshot {
    pub fn new(timestamp: u64) -> Self {
        Self {
            timestamp,
            deleted_ids: HashSet::new(),
        }
    }

    pub fn is_visible(&self, id: &str) -> bool {
        !self.deleted_ids.contains(id)
    }

    pub fn mark_deleted(&mut self, id: String) {
        self.deleted_ids.insert(id);
    }
}

/// Compaction statistics
#[derive(Debug, Clone, Default)]
pub struct CompactionStats {
    pub items_removed: usize,
    pub items_reindexed: usize,
    pub duration_ms: u64,
    pub bytes_freed: usize,
}

/// Real-time indexing metrics
#[derive(Debug, Clone, Default)]
pub struct RealtimeMetrics {
    pub total_inserts: u64,
    pub total_updates: u64,
    pub total_deletes: u64,
    pub total_queries: u64,
    pub buffer_flushes: u64,
    pub compactions: u64,
    pub avg_insert_latency_ms: f64,
    pub avg_query_latency_ms: f64,
    pub buffer_size: usize,
    pub index_size: usize,
    pub deleted_count: usize,
}

impl RealtimeMetrics {
    pub fn deletion_ratio(&self) -> f32 {
        if self.index_size == 0 {
            0.0
        } else {
            self.deleted_count as f32 / self.index_size as f32
        }
    }

    pub fn needs_compaction(&self, threshold: f32) -> bool {
        self.deletion_ratio() > threshold
    }
}

/// Lock-free update strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UpdateStrategy {
    /// Acquire write lock (blocking)
    Blocking,

    /// Try write lock, buffer if locked
    TryLock,

    /// Always buffer, never block
    AlwaysBuffer,
}

/// Background worker configuration
#[derive(Debug, Clone)]
pub struct WorkerConfig {
    /// Flush interval (milliseconds)
    pub flush_interval_ms: u64,

    /// Maximum buffer size before forcing flush
    pub max_buffer_size: usize,

    /// Update strategy
    pub strategy: UpdateStrategy,
}

impl Default for WorkerConfig {
    fn default() -> Self {
        Self {
            flush_interval_ms: 100,
            max_buffer_size: 1000,
            strategy: UpdateStrategy::TryLock,
        }
    }
}

/// Mock implementation for sync builds
#[cfg(not(feature = "async"))]
pub struct RealtimeIndex {
    config: RealtimeConfig,
    buffer: WriteBuffer,
    snapshot: Snapshot,
    metrics: RealtimeMetrics,
    #[allow(dead_code)]
    path: PathBuf,
}

#[cfg(not(feature = "async"))]
impl RealtimeIndex {
    pub fn open<P: AsRef<Path>>(path: P, config: RealtimeConfig) -> Result<Self> {
        Ok(Self {
            config,
            buffer: WriteBuffer::new(),
            snapshot: Snapshot::new(0),
            metrics: RealtimeMetrics::default(),
            path: path.as_ref().to_path_buf(),
        })
    }

    pub fn insert(&mut self, id: &str, vector: Vec<f32>) -> Result<()> {
        self.buffer.insert(id.to_string(), vector);
        self.metrics.total_inserts += 1;

        if self.buffer.len() >= self.config.buffer_size {
            self.flush()?;
        }

        Ok(())
    }

    pub fn update(&mut self, id: &str, vector: Vec<f32>) -> Result<()> {
        self.buffer.update(id.to_string(), vector);
        self.metrics.total_updates += 1;

        if self.buffer.len() >= self.config.buffer_size {
            self.flush()?;
        }

        Ok(())
    }

    pub fn delete(&mut self, id: &str) -> Result<()> {
        self.buffer.delete(id.to_string());
        self.metrics.total_deletes += 1;
        self.metrics.deleted_count += 1;

        if self.buffer.len() >= self.config.buffer_size {
            self.flush()?;
        }

        Ok(())
    }

    pub fn flush(&mut self) -> Result<()> {
        let entries = self.buffer.drain();

        // In real implementation, would apply to HNSW index
        // For now, just update snapshot
        for entry in &entries {
            if entry.is_delete() {
                self.snapshot.mark_deleted(entry.id().to_string());
            }
        }

        self.metrics.buffer_flushes += 1;
        Ok(())
    }

    pub fn compact(&mut self) -> Result<CompactionStats> {
        let start = Instant::now();

        // In real implementation, would rebuild HNSW without deleted items
        let items_reindexed = self
            .metrics
            .index_size
            .saturating_sub(self.metrics.deleted_count);

        let stats = CompactionStats {
            items_removed: self.metrics.deleted_count,
            items_reindexed,
            duration_ms: start.elapsed().as_millis() as u64,
            bytes_freed: self.metrics.deleted_count * 512, // Estimate
        };

        self.metrics.deleted_count = 0;
        self.metrics.compactions += 1;

        Ok(stats)
    }

    pub fn metrics(&self) -> &RealtimeMetrics {
        &self.metrics
    }

    pub fn snapshot(&self) -> &Snapshot {
        &self.snapshot
    }
}

/// Async implementation
#[cfg(feature = "async")]
pub struct RealtimeIndex {
    config: RealtimeConfig,
    buffer: Arc<Mutex<WriteBuffer>>,
    snapshot: Arc<RwLock<Snapshot>>,
    metrics: Arc<Mutex<RealtimeMetrics>>,
    path: PathBuf,
    worker_handle: Option<tokio::task::JoinHandle<()>>,
}

#[cfg(feature = "async")]
impl RealtimeIndex {
    pub fn open<P: AsRef<Path>>(path: P, config: RealtimeConfig) -> Result<Self> {
        Ok(Self {
            config,
            buffer: Arc::new(Mutex::new(WriteBuffer::new())),
            snapshot: Arc::new(RwLock::new(Snapshot::new(0))),
            metrics: Arc::new(Mutex::new(RealtimeMetrics::default())),
            path: path.as_ref().to_path_buf(),
            worker_handle: None,
        })
    }

    pub async fn insert(&mut self, id: &str, vector: Vec<f32>) -> Result<()> {
        let start = Instant::now();

        {
            let mut buffer = self.buffer.lock().await;
            buffer.insert(id.to_string(), vector);
        }

        {
            let mut metrics = self.metrics.lock().await;
            metrics.total_inserts += 1;
            metrics.buffer_size = self.buffer.lock().await.len();

            let latency_ms = start.elapsed().as_millis() as f64;
            metrics.avg_insert_latency_ms =
                (metrics.avg_insert_latency_ms * (metrics.total_inserts - 1) as f64 + latency_ms)
                    / metrics.total_inserts as f64;
        }

        // Auto-flush if buffer is full
        if self.buffer.lock().await.len() >= self.config.buffer_size {
            self.flush().await?;
        }

        Ok(())
    }

    pub async fn update(&mut self, id: &str, vector: Vec<f32>) -> Result<()> {
        let mut buffer = self.buffer.lock().await;
        buffer.update(id.to_string(), vector);

        let mut metrics = self.metrics.lock().await;
        metrics.total_updates += 1;

        Ok(())
    }

    pub async fn delete(&mut self, id: &str) -> Result<()> {
        let mut buffer = self.buffer.lock().await;
        buffer.delete(id.to_string());

        let mut metrics = self.metrics.lock().await;
        metrics.total_deletes += 1;
        metrics.deleted_count += 1;

        Ok(())
    }

    pub async fn flush(&mut self) -> Result<()> {
        let entries = {
            let mut buffer = self.buffer.lock().await;
            buffer.drain()
        };

        // Apply to snapshot
        {
            let mut snapshot = self.snapshot.write().await;
            for entry in &entries {
                if entry.is_delete() {
                    snapshot.mark_deleted(entry.id().to_string());
                }
            }
        }

        let mut metrics = self.metrics.lock().await;
        metrics.buffer_flushes += 1;
        metrics.buffer_size = 0;

        Ok(())
    }

    pub async fn compact(&mut self) -> Result<CompactionStats> {
        let start = Instant::now();

        let stats = {
            let metrics = self.metrics.lock().await;
            let items_reindexed = metrics.index_size.saturating_sub(metrics.deleted_count);

            CompactionStats {
                items_removed: metrics.deleted_count,
                items_reindexed,
                duration_ms: 0, // Will update
                bytes_freed: metrics.deleted_count * 512,
            }
        };

        let mut final_stats = stats;
        final_stats.duration_ms = start.elapsed().as_millis() as u64;

        {
            let mut metrics = self.metrics.lock().await;
            metrics.deleted_count = 0;
            metrics.compactions += 1;
        }

        Ok(final_stats)
    }

    pub async fn start_background_worker(&mut self) -> Result<()> {
        let buffer = Arc::clone(&self.buffer);
        let snapshot = Arc::clone(&self.snapshot);
        let metrics = Arc::clone(&self.metrics);
        let config = self.config.clone();

        let handle = tokio::spawn(async move {
            let mut flush_interval = interval(Duration::from_millis(100));
            let mut compact_interval =
                interval(Duration::from_secs(config.compaction_interval_secs));

            loop {
                tokio::select! {
                    _ = flush_interval.tick() => {
                        // Periodic flush
                        let entries = {
                            let mut buf = buffer.lock().await;
                            if buf.is_empty() {
                                continue;
                            }
                            buf.drain()
                        };

                        // Apply entries
                        let mut snap = snapshot.write().await;
                        for entry in &entries {
                            if entry.is_delete() {
                                snap.mark_deleted(entry.id().to_string());
                            }
                        }

                        let mut m = metrics.lock().await;
                        m.buffer_flushes += 1;
                        m.buffer_size = 0;
                    }

                    _ = compact_interval.tick() => {
                        // Periodic compaction
                        let m = metrics.lock().await;
                        if m.needs_compaction(config.compaction_threshold) {
                            drop(m);

                            // Would trigger compaction here
                            let mut m = metrics.lock().await;
                            m.compactions += 1;
                        }
                    }
                }
            }
        });

        self.worker_handle = Some(handle);

        Ok(())
    }

    pub async fn stop_background_worker(&mut self) {
        if let Some(handle) = self.worker_handle.take() {
            handle.abort();
        }
    }

    pub async fn metrics(&self) -> RealtimeMetrics {
        self.metrics.lock().await.clone()
    }

    pub async fn snapshot(&self) -> Snapshot {
        self.snapshot.read().await.clone()
    }
}

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

    #[test]
    fn test_write_buffer() {
        let mut buffer = WriteBuffer::new();

        buffer.insert("doc1".to_string(), vec![0.1, 0.2]);
        buffer.insert("doc2".to_string(), vec![0.2, 0.3]);

        assert_eq!(buffer.len(), 2);

        buffer.delete("doc1".to_string());
        assert!(buffer.is_deleted("doc1"));
        assert!(!buffer.is_deleted("doc2"));

        let entries = buffer.drain();
        assert_eq!(entries.len(), 3);
        assert_eq!(buffer.len(), 0);
    }

    #[test]
    fn test_snapshot() {
        let mut snapshot = Snapshot::new(0);

        assert!(snapshot.is_visible("doc1"));

        snapshot.mark_deleted("doc1".to_string());

        assert!(!snapshot.is_visible("doc1"));
        assert!(snapshot.is_visible("doc2"));
    }

    #[test]
    fn test_realtime_metrics() {
        let mut metrics = RealtimeMetrics::default();
        metrics.index_size = 1000;
        metrics.deleted_count = 150;

        assert_eq!(metrics.deletion_ratio(), 0.15);
        assert!(metrics.needs_compaction(0.1));
        assert!(!metrics.needs_compaction(0.2));
    }

    #[test]
    fn test_realtime_config() {
        let config = RealtimeConfig::default()
            .with_buffer_size(500)
            .with_compaction_threshold(0.2);

        assert_eq!(config.buffer_size, 500);
        assert_eq!(config.compaction_threshold, 0.2);
    }

    #[cfg(not(feature = "async"))]
    #[test]
    fn test_realtime_index_sync() {
        let config = RealtimeConfig::default();
        let mut index = RealtimeIndex::open("test.db", config).unwrap();

        index.insert("doc1", vec![0.1, 0.2, 0.3]).unwrap();
        index.insert("doc2", vec![0.2, 0.3, 0.4]).unwrap();

        assert_eq!(index.metrics().total_inserts, 2);

        index.delete("doc1").unwrap();
        assert_eq!(index.metrics().total_deletes, 1);

        index.flush().unwrap();
        assert!(index.snapshot().deleted_ids.contains("doc1"));
    }

    #[cfg(feature = "async")]
    #[tokio::test]
    async fn test_realtime_index_async() {
        let config = RealtimeConfig::default();
        let mut index = RealtimeIndex::open("test.db", config).unwrap();

        index.insert("doc1", vec![0.1, 0.2, 0.3]).await.unwrap();
        index.insert("doc2", vec![0.2, 0.3, 0.4]).await.unwrap();

        let metrics = index.metrics().await;
        assert_eq!(metrics.total_inserts, 2);

        index.delete("doc1").await.unwrap();
        index.flush().await.unwrap();

        let snapshot = index.snapshot().await;
        assert!(snapshot.deleted_ids.contains("doc1"));
    }
}