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
//! Health checks and diagnostics for VecStore
//!
//! Provides comprehensive health monitoring and diagnostics:
//! - Database health status
//! - Performance metrics
//! - Resource utilization
//! - Index integrity checks
//! - Alert conditions

use crate::store::VecStore;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::time::{Duration, SystemTime};

/// Overall health status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum HealthStatus {
    /// All systems operational
    Healthy,

    /// Minor issues, but functional
    Degraded,

    /// Critical issues affecting functionality
    Unhealthy,
}

impl HealthStatus {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Healthy => "healthy",
            Self::Degraded => "degraded",
            Self::Unhealthy => "unhealthy",
        }
    }
}

/// Comprehensive health report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthReport {
    /// Overall status
    pub status: HealthStatus,

    /// Timestamp of the check
    pub timestamp: SystemTime,

    /// Database statistics
    pub database: DatabaseHealth,

    /// Index health
    pub index: IndexHealth,

    /// Performance metrics
    pub performance: PerformanceHealth,

    /// Resource utilization
    pub resources: ResourceHealth,

    /// Active alerts
    pub alerts: Vec<Alert>,

    /// Uptime duration
    pub uptime: Option<Duration>,
}

/// Database health metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseHealth {
    /// Total number of vectors
    pub total_vectors: usize,

    /// Active (non-deleted) vectors
    pub active_vectors: usize,

    /// Deleted vectors pending compaction
    pub deleted_vectors: usize,

    /// Vector dimension
    pub dimension: usize,

    /// Deletion ratio (deleted/total)
    pub deletion_ratio: f64,

    /// Storage efficiency score (0-100)
    pub storage_efficiency: f64,
}

/// Index health metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexHealth {
    /// Index type (HNSW, IVF-PQ, etc.)
    pub index_type: String,

    /// Index integrity check result
    pub integrity_ok: bool,

    /// Average degree in HNSW graph
    pub avg_degree: Option<f64>,

    /// Fragmentation score (0-100, lower is better)
    pub fragmentation: f64,

    /// Last index rebuild time
    pub last_rebuild: Option<SystemTime>,
}

/// Performance health metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceHealth {
    /// Average query latency (ms)
    pub avg_query_latency_ms: Option<f64>,

    /// 95th percentile query latency (ms)
    pub p95_query_latency_ms: Option<f64>,

    /// Queries per second
    pub qps: Option<f64>,

    /// Insert throughput (vectors/sec)
    pub insert_throughput: Option<f64>,

    /// Performance score (0-100)
    pub performance_score: f64,
}

/// Resource utilization metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceHealth {
    /// Estimated memory usage (bytes)
    pub memory_bytes: usize,

    /// Disk usage (bytes)
    pub disk_bytes: usize,

    /// Memory per vector (bytes)
    pub memory_per_vector: f64,

    /// Memory utilization percentage (0-100)
    pub memory_utilization: f64,

    /// Disk utilization percentage (0-100)
    pub disk_utilization: f64,
}

/// Health alert
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Alert {
    /// Alert severity
    pub severity: AlertSeverity,

    /// Alert category
    pub category: AlertCategory,

    /// Human-readable message
    pub message: String,

    /// Optional metric value
    pub value: Option<f64>,

    /// Recommended action
    pub recommendation: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AlertSeverity {
    Info,
    Warning,
    Critical,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AlertCategory {
    Performance,
    Storage,
    Index,
    Resource,
    Capacity,
}

/// Health check configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthCheckConfig {
    /// Deletion ratio threshold for warning
    pub deletion_ratio_warning: f64,

    /// Deletion ratio threshold for critical
    pub deletion_ratio_critical: f64,

    /// Fragmentation threshold for warning
    pub fragmentation_warning: f64,

    /// Memory utilization warning threshold
    pub memory_warning: f64,

    /// Query latency warning threshold (ms)
    pub latency_warning_ms: f64,

    /// Minimum performance score
    pub min_performance_score: f64,
}

impl Default for HealthCheckConfig {
    fn default() -> Self {
        Self {
            deletion_ratio_warning: 0.3,
            deletion_ratio_critical: 0.5,
            fragmentation_warning: 50.0,
            memory_warning: 80.0,
            latency_warning_ms: 100.0,
            min_performance_score: 70.0,
        }
    }
}

/// Health checker
pub struct HealthChecker {
    config: HealthCheckConfig,
    start_time: SystemTime,
}

impl HealthChecker {
    /// Create a new health checker
    pub fn new(config: HealthCheckConfig) -> Self {
        Self {
            config,
            start_time: SystemTime::now(),
        }
    }

    /// Create with default configuration
    pub fn default() -> Self {
        Self::new(HealthCheckConfig::default())
    }

    /// Perform a comprehensive health check
    pub fn check(&self, store: &VecStore) -> Result<HealthReport> {
        let database = self.check_database(store);
        let index = self.check_index(store);
        let performance = self.check_performance(store);
        let resources = self.check_resources(store, &database);

        let mut alerts = Vec::new();

        // Generate alerts based on metrics
        self.generate_database_alerts(&database, &mut alerts);
        self.generate_index_alerts(&index, &mut alerts);
        self.generate_performance_alerts(&performance, &mut alerts);
        self.generate_resource_alerts(&resources, &mut alerts);

        // Determine overall status
        let status = self.determine_status(&alerts);

        let uptime = SystemTime::now().duration_since(self.start_time).ok();

        Ok(HealthReport {
            status,
            timestamp: SystemTime::now(),
            database,
            index,
            performance,
            resources,
            alerts,
            uptime,
        })
    }

    fn check_database(&self, store: &VecStore) -> DatabaseHealth {
        let total_vectors = store.len() + store.deleted_count();
        let active_vectors = store.active_count();
        let deleted_vectors = store.deleted_count();
        let dimension = store.dimension();

        let deletion_ratio = if total_vectors > 0 {
            deleted_vectors as f64 / total_vectors as f64
        } else {
            0.0
        };

        // Storage efficiency: how well we're using space
        let storage_efficiency = if total_vectors > 0 {
            (active_vectors as f64 / total_vectors as f64) * 100.0
        } else {
            100.0
        };

        DatabaseHealth {
            total_vectors,
            active_vectors,
            deleted_vectors,
            dimension,
            deletion_ratio,
            storage_efficiency,
        }
    }

    fn check_index(&self, _store: &VecStore) -> IndexHealth {
        // For now, return basic HNSW index health
        IndexHealth {
            index_type: "HNSW".to_string(),
            integrity_ok: true,
            avg_degree: None,
            fragmentation: 0.0,
            last_rebuild: None,
        }
    }

    fn check_performance(&self, _store: &VecStore) -> PerformanceHealth {
        // These would typically be collected from metrics over time
        PerformanceHealth {
            avg_query_latency_ms: None,
            p95_query_latency_ms: None,
            qps: None,
            insert_throughput: None,
            performance_score: 85.0, // Placeholder
        }
    }

    fn check_resources(&self, _store: &VecStore, db: &DatabaseHealth) -> ResourceHealth {
        // Estimate memory usage
        let vector_size = db.dimension * 4; // f32 = 4 bytes
        let vectors_memory = db.active_vectors * vector_size;
        let index_overhead = db.active_vectors * 64; // Rough HNSW overhead
        let memory_bytes = vectors_memory + index_overhead;

        let memory_per_vector = if db.active_vectors > 0 {
            memory_bytes as f64 / db.active_vectors as f64
        } else {
            0.0
        };

        ResourceHealth {
            memory_bytes,
            disk_bytes: 0, // Would need actual disk measurement
            memory_per_vector,
            memory_utilization: 0.0, // Placeholder
            disk_utilization: 0.0,
        }
    }

    fn generate_database_alerts(&self, db: &DatabaseHealth, alerts: &mut Vec<Alert>) {
        // Check deletion ratio
        if db.deletion_ratio >= self.config.deletion_ratio_critical {
            alerts.push(Alert {
                severity: AlertSeverity::Critical,
                category: AlertCategory::Storage,
                message: format!(
                    "High deletion ratio: {:.1}% of vectors are deleted",
                    db.deletion_ratio * 100.0
                ),
                value: Some(db.deletion_ratio * 100.0),
                recommendation: Some("Run compaction to reclaim space".to_string()),
            });
        } else if db.deletion_ratio >= self.config.deletion_ratio_warning {
            alerts.push(Alert {
                severity: AlertSeverity::Warning,
                category: AlertCategory::Storage,
                message: format!(
                    "Elevated deletion ratio: {:.1}% of vectors are deleted",
                    db.deletion_ratio * 100.0
                ),
                value: Some(db.deletion_ratio * 100.0),
                recommendation: Some("Consider running compaction soon".to_string()),
            });
        }

        // Check if database is empty
        if db.active_vectors == 0 && db.total_vectors > 0 {
            alerts.push(Alert {
                severity: AlertSeverity::Warning,
                category: AlertCategory::Capacity,
                message: "Database has no active vectors".to_string(),
                value: None,
                recommendation: Some("All vectors have been deleted".to_string()),
            });
        }

        // Check storage efficiency
        if db.storage_efficiency < 50.0 && db.total_vectors > 100 {
            alerts.push(Alert {
                severity: AlertSeverity::Warning,
                category: AlertCategory::Storage,
                message: format!("Low storage efficiency: {:.1}%", db.storage_efficiency),
                value: Some(db.storage_efficiency),
                recommendation: Some("Run compaction to improve efficiency".to_string()),
            });
        }
    }

    fn generate_index_alerts(&self, index: &IndexHealth, alerts: &mut Vec<Alert>) {
        if !index.integrity_ok {
            alerts.push(Alert {
                severity: AlertSeverity::Critical,
                category: AlertCategory::Index,
                message: "Index integrity check failed".to_string(),
                value: None,
                recommendation: Some("Rebuild index from scratch".to_string()),
            });
        }

        if index.fragmentation >= self.config.fragmentation_warning {
            alerts.push(Alert {
                severity: AlertSeverity::Warning,
                category: AlertCategory::Index,
                message: format!("High index fragmentation: {:.1}", index.fragmentation),
                value: Some(index.fragmentation),
                recommendation: Some("Consider rebuilding index".to_string()),
            });
        }
    }

    fn generate_performance_alerts(&self, perf: &PerformanceHealth, alerts: &mut Vec<Alert>) {
        if let Some(latency) = perf.p95_query_latency_ms {
            if latency >= self.config.latency_warning_ms {
                alerts.push(Alert {
                    severity: AlertSeverity::Warning,
                    category: AlertCategory::Performance,
                    message: format!("High query latency: {:.2}ms (p95)", latency),
                    value: Some(latency),
                    recommendation: Some(
                        "Check index parameters or consider upgrading hardware".to_string(),
                    ),
                });
            }
        }

        if perf.performance_score < self.config.min_performance_score {
            alerts.push(Alert {
                severity: AlertSeverity::Warning,
                category: AlertCategory::Performance,
                message: format!("Low performance score: {:.1}", perf.performance_score),
                value: Some(perf.performance_score),
                recommendation: Some("Review configuration and system resources".to_string()),
            });
        }
    }

    fn generate_resource_alerts(&self, resources: &ResourceHealth, alerts: &mut Vec<Alert>) {
        if resources.memory_utilization >= self.config.memory_warning {
            alerts.push(Alert {
                severity: AlertSeverity::Warning,
                category: AlertCategory::Resource,
                message: format!(
                    "High memory utilization: {:.1}%",
                    resources.memory_utilization
                ),
                value: Some(resources.memory_utilization),
                recommendation: Some("Consider adding memory or reducing dataset size".to_string()),
            });
        }

        if resources.disk_utilization >= 90.0 {
            alerts.push(Alert {
                severity: AlertSeverity::Critical,
                category: AlertCategory::Resource,
                message: format!(
                    "Critical disk utilization: {:.1}%",
                    resources.disk_utilization
                ),
                value: Some(resources.disk_utilization),
                recommendation: Some("Free up disk space immediately".to_string()),
            });
        }
    }

    fn determine_status(&self, alerts: &[Alert]) -> HealthStatus {
        let has_critical = alerts.iter().any(|a| a.severity == AlertSeverity::Critical);
        let has_warning = alerts.iter().any(|a| a.severity == AlertSeverity::Warning);

        if has_critical {
            HealthStatus::Unhealthy
        } else if has_warning {
            HealthStatus::Degraded
        } else {
            HealthStatus::Healthy
        }
    }
}

/// Print health report in human-readable format
pub fn print_health_report(report: &HealthReport) {
    println!("\n{}", "=".repeat(80));
    println!("VecStore Health Report");
    println!("{}", "=".repeat(80));

    // Overall status
    let status_icon = match report.status {
        HealthStatus::Healthy => "✅",
        HealthStatus::Degraded => "âš ī¸",
        HealthStatus::Unhealthy => "❌",
    };
    println!(
        "\n{} Overall Status: {}",
        status_icon,
        report.status.as_str().to_uppercase()
    );

    if let Some(uptime) = report.uptime {
        println!("âąī¸  Uptime: {:?}", uptime);
    }

    // Database
    println!("\n📊 Database:");
    println!("  Total vectors: {}", report.database.total_vectors);
    println!("  Active vectors: {}", report.database.active_vectors);
    println!("  Deleted vectors: {}", report.database.deleted_vectors);
    println!("  Dimension: {}", report.database.dimension);
    println!(
        "  Deletion ratio: {:.1}%",
        report.database.deletion_ratio * 100.0
    );
    println!(
        "  Storage efficiency: {:.1}%",
        report.database.storage_efficiency
    );

    // Index
    println!("\n🔍 Index:");
    println!("  Type: {}", report.index.index_type);
    println!(
        "  Integrity: {}",
        if report.index.integrity_ok {
            "✓ OK"
        } else {
            "✗ Failed"
        }
    );
    println!("  Fragmentation: {:.1}", report.index.fragmentation);

    // Performance
    println!("\n⚡ Performance:");
    println!(
        "  Performance score: {:.1}/100",
        report.performance.performance_score
    );
    if let Some(latency) = report.performance.avg_query_latency_ms {
        println!("  Avg query latency: {:.2}ms", latency);
    }
    if let Some(p95) = report.performance.p95_query_latency_ms {
        println!("  P95 query latency: {:.2}ms", p95);
    }
    if let Some(qps) = report.performance.qps {
        println!("  QPS: {:.0}", qps);
    }

    // Resources
    println!("\n💾 Resources:");
    println!(
        "  Memory usage: {:.2} MB",
        report.resources.memory_bytes as f64 / 1_000_000.0
    );
    println!(
        "  Memory per vector: {:.1} bytes",
        report.resources.memory_per_vector
    );

    // Alerts
    if !report.alerts.is_empty() {
        println!("\n🚨 Alerts ({}):", report.alerts.len());
        for alert in &report.alerts {
            let icon = match alert.severity {
                AlertSeverity::Info => "â„šī¸",
                AlertSeverity::Warning => "âš ī¸",
                AlertSeverity::Critical => "❌",
            };
            println!(
                "  {} [{}] {}",
                icon,
                format!("{:?}", alert.category),
                alert.message
            );
            if let Some(rec) = &alert.recommendation {
                println!("     → {}", rec);
            }
        }
    } else {
        println!("\n✅ No alerts");
    }

    println!("\n{}", "=".repeat(80));
}

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

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

        let checker = HealthChecker::default();
        let report = checker.check(&store)?;

        assert_eq!(report.status, HealthStatus::Healthy);
        assert_eq!(report.database.active_vectors, 0);
        assert!(report.alerts.is_empty());

        Ok(())
    }

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

        // Insert and delete vectors to trigger alert
        for i in 0..10 {
            store.upsert(
                format!("vec_{}", i),
                vec![1.0, 2.0, 3.0],
                crate::store::Metadata {
                    fields: std::collections::HashMap::new(),
                },
            )?;
        }

        // Delete 6 out of 10 (60% deletion ratio - this exceeds critical threshold of 50%)
        for i in 0..6 {
            store.soft_delete(&format!("vec_{}", i))?;
        }

        let checker = HealthChecker::default();
        let report = checker.check(&store)?;

        // 60% deletion ratio should trigger Unhealthy status
        assert_eq!(report.status, HealthStatus::Unhealthy);
        assert!(report
            .alerts
            .iter()
            .any(|a| a.severity == AlertSeverity::Critical));

        Ok(())
    }
}