vectradb_components/
lib.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use thiserror::Error;
4
5// Re-export commonly used types
6pub use ndarray::{Array1, ArrayView1};
7
8/// Vector database error types
9#[derive(Error, Debug)]
10pub enum VectraDBError {
11    #[error("Vector dimension mismatch: expected {expected}, got {actual}")]
12    DimensionMismatch { expected: usize, actual: usize },
13    #[error("Vector not found: {id}")]
14    VectorNotFound { id: String },
15    #[error("Invalid vector data")]
16    InvalidVector,
17    #[error("Database error: {0}")]
18    DatabaseError(#[from] anyhow::Error),
19}
20
21/// Vector metadata structure
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct VectorMetadata {
24    pub id: String,
25    pub dimension: usize,
26    pub created_at: u64,
27    pub updated_at: u64,
28    pub tags: HashMap<String, String>,
29}
30
31/// Vector document structure
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct VectorDocument {
34    pub metadata: VectorMetadata,
35    pub data: Array1<f32>,
36}
37
38/// Vector similarity result
39#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct SimilarityResult {
41    pub id: String,
42    pub score: f32,
43    pub metadata: VectorMetadata,
44}
45
46/// Vector database trait for different implementations
47pub trait VectorDatabase {
48    /// Create a new vector in the database
49    fn create_vector(
50        &mut self,
51        id: String,
52        vector: Array1<f32>,
53        tags: Option<HashMap<String, String>>,
54    ) -> Result<(), VectraDBError>;
55
56    /// Fetch a vector by ID
57    fn get_vector(&self, id: &str) -> Result<VectorDocument, VectraDBError>;
58
59    /// Update an existing vector
60    fn update_vector(
61        &mut self,
62        id: &str,
63        vector: Array1<f32>,
64        tags: Option<HashMap<String, String>>,
65    ) -> Result<(), VectraDBError>;
66
67    /// Delete a vector by ID
68    fn delete_vector(&mut self, id: &str) -> Result<(), VectraDBError>;
69
70    /// Upsert (insert or update) a vector
71    fn upsert_vector(
72        &mut self,
73        id: String,
74        vector: Array1<f32>,
75        tags: Option<HashMap<String, String>>,
76    ) -> Result<(), VectraDBError>;
77
78    /// Search for similar vectors
79    fn search_similar(
80        &self,
81        query_vector: Array1<f32>,
82        top_k: usize,
83    ) -> Result<Vec<SimilarityResult>, VectraDBError>;
84
85    /// Get all vector IDs
86    fn list_vectors(&self) -> Result<Vec<String>, VectraDBError>;
87
88    /// Get database statistics
89    fn get_stats(&self) -> Result<DatabaseStats, VectraDBError>;
90}
91
92/// Database statistics
93#[derive(Debug, Clone, Serialize, Deserialize, Default)]
94pub struct DatabaseStats {
95    pub total_vectors: usize,
96    pub dimension: usize,
97    pub memory_usage: u64,
98}
99
100// Module declarations
101pub mod indexing;
102pub mod similarity;
103pub mod storage;
104pub mod vector_operations;
105
106// Re-export main functionality
107pub use similarity::*;
108pub use vector_operations::*;