use async_trait::async_trait;
use semtree_embed::Embedding;
use serde::{Deserialize, Serialize};
use crate::StoreError;
#[derive(Debug, Clone)]
pub struct Hit {
pub id: String,
pub score: f32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Metric {
Cosine,
Euclidean,
DotProduct,
}
impl std::fmt::Display for Metric {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Metric::Cosine => "cosine",
Metric::Euclidean => "euclidean",
Metric::DotProduct => "dot_product",
};
f.write_str(s)
}
}
#[async_trait]
pub trait VectorStore: Send + Sync {
async fn insert(&self, id: &str, embedding: &Embedding) -> Result<(), StoreError>;
async fn search(&self, query: &Embedding, top_k: usize) -> Result<Vec<Hit>, StoreError>;
async fn delete(&self, id: &str) -> Result<(), StoreError>;
async fn clear(&self) -> Result<(), StoreError>;
fn save(&self, path: &std::path::Path) -> Result<(), StoreError>;
fn load(&self, path: &std::path::Path) -> Result<(), StoreError>;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn metric(&self) -> Metric;
}