use std::sync::Arc;
use async_trait::async_trait;
use common::{NamespaceId, Result, Vector, VectorId};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum IndexType {
Hnsw,
Pq,
Ivf,
SpFresh,
FullText,
}
impl IndexType {
pub fn as_str(&self) -> &'static str {
match self {
IndexType::Hnsw => "hnsw",
IndexType::Pq => "pq",
IndexType::Ivf => "ivf",
IndexType::SpFresh => "spfresh",
IndexType::FullText => "fulltext",
}
}
}
#[async_trait]
pub trait IndexStorage: Send + Sync {
async fn save_index(
&self,
namespace: &NamespaceId,
index_type: IndexType,
data: Vec<u8>,
) -> Result<()>;
async fn load_index(
&self,
namespace: &NamespaceId,
index_type: IndexType,
) -> Result<Option<Vec<u8>>>;
async fn delete_index(&self, namespace: &NamespaceId, index_type: IndexType) -> Result<bool>;
async fn index_exists(&self, namespace: &NamespaceId, index_type: IndexType) -> Result<bool>;
async fn list_indexes(&self, namespace: &NamespaceId) -> Result<Vec<IndexType>>;
}
#[async_trait]
impl<T: IndexStorage + ?Sized> IndexStorage for Arc<T> {
async fn save_index(
&self,
namespace: &NamespaceId,
index_type: IndexType,
data: Vec<u8>,
) -> Result<()> {
(**self).save_index(namespace, index_type, data).await
}
async fn load_index(
&self,
namespace: &NamespaceId,
index_type: IndexType,
) -> Result<Option<Vec<u8>>> {
(**self).load_index(namespace, index_type).await
}
async fn delete_index(&self, namespace: &NamespaceId, index_type: IndexType) -> Result<bool> {
(**self).delete_index(namespace, index_type).await
}
async fn index_exists(&self, namespace: &NamespaceId, index_type: IndexType) -> Result<bool> {
(**self).index_exists(namespace, index_type).await
}
async fn list_indexes(&self, namespace: &NamespaceId) -> Result<Vec<IndexType>> {
(**self).list_indexes(namespace).await
}
}
#[async_trait]
pub trait VectorStorage: Send + Sync {
async fn upsert(&self, namespace: &NamespaceId, vectors: Vec<Vector>) -> Result<usize>;
async fn get(&self, namespace: &NamespaceId, ids: &[VectorId]) -> Result<Vec<Vector>>;
async fn get_all(&self, namespace: &NamespaceId) -> Result<Vec<Vector>>;
async fn delete(&self, namespace: &NamespaceId, ids: &[VectorId]) -> Result<usize>;
async fn namespace_exists(&self, namespace: &NamespaceId) -> Result<bool>;
async fn ensure_namespace(&self, namespace: &NamespaceId) -> Result<()>;
async fn count(&self, namespace: &NamespaceId) -> Result<usize>;
async fn dimension(&self, namespace: &NamespaceId) -> Result<Option<usize>>;
async fn list_namespaces(&self) -> Result<Vec<NamespaceId>>;
async fn delete_namespace(&self, namespace: &NamespaceId) -> Result<bool>;
async fn cleanup_expired(&self, namespace: &NamespaceId) -> Result<usize>;
async fn cleanup_all_expired(&self) -> Result<usize>;
}
#[async_trait]
impl<T: VectorStorage + ?Sized> VectorStorage for Arc<T> {
async fn upsert(&self, namespace: &NamespaceId, vectors: Vec<Vector>) -> Result<usize> {
(**self).upsert(namespace, vectors).await
}
async fn get(&self, namespace: &NamespaceId, ids: &[VectorId]) -> Result<Vec<Vector>> {
(**self).get(namespace, ids).await
}
async fn get_all(&self, namespace: &NamespaceId) -> Result<Vec<Vector>> {
(**self).get_all(namespace).await
}
async fn delete(&self, namespace: &NamespaceId, ids: &[VectorId]) -> Result<usize> {
(**self).delete(namespace, ids).await
}
async fn namespace_exists(&self, namespace: &NamespaceId) -> Result<bool> {
(**self).namespace_exists(namespace).await
}
async fn ensure_namespace(&self, namespace: &NamespaceId) -> Result<()> {
(**self).ensure_namespace(namespace).await
}
async fn count(&self, namespace: &NamespaceId) -> Result<usize> {
(**self).count(namespace).await
}
async fn dimension(&self, namespace: &NamespaceId) -> Result<Option<usize>> {
(**self).dimension(namespace).await
}
async fn list_namespaces(&self) -> Result<Vec<NamespaceId>> {
(**self).list_namespaces().await
}
async fn delete_namespace(&self, namespace: &NamespaceId) -> Result<bool> {
(**self).delete_namespace(namespace).await
}
async fn cleanup_expired(&self, namespace: &NamespaceId) -> Result<usize> {
(**self).cleanup_expired(namespace).await
}
async fn cleanup_all_expired(&self) -> Result<usize> {
(**self).cleanup_all_expired().await
}
}