Skip to main content

synwire_core/vectorstores/
traits.rs

1//! Trait definition for vector stores.
2
3use crate::BoxFuture;
4use crate::documents::Document;
5use crate::embeddings::Embeddings;
6use crate::error::SynwireError;
7
8/// Trait for vector stores that persist and query document embeddings.
9///
10/// Implementations manage storage of documents alongside their embedding
11/// vectors and support similarity-based retrieval.
12///
13/// # Cancel safety
14///
15/// [`add_documents`](Self::add_documents) is **not cancel-safe**: dropping
16/// the future mid-write may leave a partial set of documents persisted.
17/// [`similarity_search`](Self::similarity_search) and
18/// [`similarity_search_with_score`](Self::similarity_search_with_score) are
19/// cancel-safe for read-only stores, but dropping mid-query may waste
20/// compute. [`delete`](Self::delete) is **not cancel-safe**: partial
21/// deletions are possible.
22pub trait VectorStore: Send + Sync {
23    /// Add documents to the store, returning their assigned IDs.
24    fn add_documents<'a>(
25        &'a self,
26        documents: &'a [Document],
27        embeddings: &'a dyn Embeddings,
28    ) -> BoxFuture<'a, Result<Vec<String>, SynwireError>>;
29
30    /// Retrieve the `k` most similar documents to the query.
31    fn similarity_search<'a>(
32        &'a self,
33        query: &'a str,
34        k: usize,
35        embeddings: &'a dyn Embeddings,
36    ) -> BoxFuture<'a, Result<Vec<Document>, SynwireError>>;
37
38    /// Retrieve the `k` most similar documents with their similarity scores.
39    fn similarity_search_with_score<'a>(
40        &'a self,
41        query: &'a str,
42        k: usize,
43        embeddings: &'a dyn Embeddings,
44    ) -> BoxFuture<'a, Result<Vec<(Document, f32)>, SynwireError>>;
45
46    /// Delete documents by their IDs.
47    fn delete<'a>(&'a self, ids: &'a [String]) -> BoxFuture<'a, Result<(), SynwireError>>;
48}