use std::collections::HashMap;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use crate::error::Result;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutputData {
pub id: String,
pub score: Option<f32>,
pub payload: HashMap<String, serde_json::Value>,
}
impl OutputData {
pub fn new(
id: String,
score: Option<f32>,
payload: HashMap<String, serde_json::Value>,
) -> Self {
Self { id, score, payload }
}
pub fn get_string(&self, key: &str) -> Option<String> {
self.payload
.get(key)
.and_then(|v| v.as_str())
.map(String::from)
}
pub fn get_data(&self) -> Option<String> {
self.get_string("data")
}
}
pub type Filters = HashMap<String, serde_json::Value>;
#[async_trait]
pub trait VectorStoreBase: Send + Sync {
async fn create_collection(&self, name: &str) -> Result<()>;
async fn insert(
&self,
vectors: Vec<Vec<f32>>,
payloads: Option<Vec<HashMap<String, serde_json::Value>>>,
ids: Option<Vec<String>>,
) -> Result<()>;
async fn search(
&self,
query: &str,
vectors: &[f32],
limit: usize,
filters: Option<Filters>,
) -> Result<Vec<OutputData>>;
async fn delete(&self, vector_id: &str) -> Result<()>;
async fn delete_batch(&self, vector_ids: &[String]) -> Result<()> {
use futures::future;
let futures: Vec<_> = vector_ids.iter().map(|id| self.delete(id)).collect();
future::try_join_all(futures).await?;
Ok(())
}
async fn update(
&self,
vector_id: &str,
vector: Option<Vec<f32>>,
payload: Option<HashMap<String, serde_json::Value>>,
) -> Result<()>;
async fn get(&self, vector_id: &str) -> Result<Option<OutputData>>;
async fn get_batch(&self, vector_ids: &[String]) -> Result<Vec<OutputData>> {
use futures::future;
let futures: Vec<_> = vector_ids.iter().map(|id| self.get(id)).collect();
let results = future::try_join_all(futures).await?;
Ok(results.into_iter().flatten().collect())
}
async fn list_collections(&self) -> Result<Vec<String>>;
async fn delete_collection(&self) -> Result<()>;
async fn collection_info(&self) -> Result<serde_json::Value>;
async fn list(&self, filters: Option<Filters>, limit: usize) -> Result<Vec<OutputData>>;
async fn reset(&self) -> Result<()>;
}