use std::collections::HashSet;
use async_trait::async_trait;
use nodedb_types::document::Document;
use nodedb_types::dropped_collection::DroppedCollection;
use nodedb_types::error::NodeDbResult;
use nodedb_types::filter::{EdgeFilter, MetadataFilter};
use nodedb_types::graph::GraphStats;
use nodedb_types::id::{EdgeId, NodeId};
use nodedb_types::protocol::Limits;
use nodedb_types::result::{QueryResult, SearchResult, SubGraph};
use nodedb_types::text_search::TextSearchParams;
use nodedb_types::value::Value;
use super::default_impls;
use super::marker::NodeDbMarker;
use crate::traits::document::CollectionPurgedHandler;
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
pub trait NodeDb: NodeDbMarker {
async fn vector_search(
&self,
collection: &str,
query: &[f32],
k: usize,
filter: Option<&MetadataFilter>,
allowed_ids: Option<&HashSet<String>>,
) -> NodeDbResult<Vec<SearchResult>>;
async fn vector_insert(
&self,
collection: &str,
id: &str,
embedding: &[f32],
metadata: Option<Document>,
) -> NodeDbResult<()>;
async fn vector_delete(&self, collection: &str, id: &str) -> NodeDbResult<()>;
async fn graph_traverse(
&self,
collection: &str,
start: &NodeId,
depth: u8,
direction: nodedb_types::graph::Direction,
edge_filter: Option<&EdgeFilter>,
) -> NodeDbResult<SubGraph>;
async fn graph_insert_edge(
&self,
collection: &str,
from: &NodeId,
to: &NodeId,
edge_type: &str,
properties: Option<Document>,
) -> NodeDbResult<EdgeId>;
async fn graph_delete_edge(&self, collection: &str, edge_id: &EdgeId) -> NodeDbResult<()>;
async fn graph_stats(
&self,
collection: Option<&str>,
as_of: Option<i64>,
) -> NodeDbResult<Vec<GraphStats>>;
async fn graph_pagerank(
&self,
collection: &str,
personalization: Option<std::collections::HashMap<String, f64>>,
damping: Option<f64>,
max_iterations: Option<u32>,
) -> NodeDbResult<Vec<(String, f64)>> {
default_impls::graph_pagerank_default(collection, personalization, damping, max_iterations)
}
async fn document_get(&self, collection: &str, id: &str) -> NodeDbResult<Option<Document>>;
async fn document_put(&self, collection: &str, doc: Document) -> NodeDbResult<()>;
async fn document_delete(&self, collection: &str, id: &str) -> NodeDbResult<()>;
async fn document_put_with_vector(
&self,
doc_collection: &str,
doc: Document,
vector_collection: &str,
id: &str,
embedding: &[f32],
) -> NodeDbResult<()> {
default_impls::document_put_with_vector_default(
self,
doc_collection,
doc,
vector_collection,
id,
embedding,
)
.await
}
async fn document_get_as_of(
&self,
collection: &str,
id: &str,
as_of_ms: Option<i64>,
valid_time_ms: Option<i64>,
) -> NodeDbResult<Option<Document>> {
default_impls::document_get_as_of_default(collection, id, as_of_ms, valid_time_ms)
}
async fn document_put_with_valid_time(
&self,
collection: &str,
doc: Document,
valid_from_ms: Option<i64>,
valid_until_ms: Option<i64>,
) -> NodeDbResult<()> {
default_impls::document_put_with_valid_time_default(
collection,
doc,
valid_from_ms,
valid_until_ms,
)
}
async fn list_insert(
&self,
collection: &str,
document_id: &str,
list_path: &str,
index: usize,
fields: &Value,
) -> NodeDbResult<()>;
async fn list_delete(
&self,
collection: &str,
document_id: &str,
list_path: &str,
index: usize,
) -> NodeDbResult<()>;
async fn list_move(
&self,
collection: &str,
document_id: &str,
list_path: &str,
from_index: usize,
to_index: usize,
) -> NodeDbResult<()>;
async fn vector_insert_field(
&self,
collection: &str,
field_name: &str,
id: &str,
embedding: &[f32],
metadata: Option<Document>,
) -> NodeDbResult<()> {
default_impls::vector_insert_field_default(collection, field_name, id, embedding, metadata)
}
async fn vector_search_field(
&self,
collection: &str,
field_name: &str,
query: &[f32],
k: usize,
filter: Option<&MetadataFilter>,
) -> NodeDbResult<Vec<SearchResult>> {
default_impls::vector_search_field_default(collection, field_name, query, k, filter)
}
async fn graph_shortest_path(
&self,
collection: &str,
from: &NodeId,
to: &NodeId,
max_depth: u8,
edge_filter: Option<&EdgeFilter>,
) -> NodeDbResult<Option<Vec<NodeId>>> {
default_impls::graph_shortest_path_default(
self,
collection,
from,
to,
max_depth,
edge_filter,
)
.await
}
async fn text_search(
&self,
collection: &str,
field: &str,
query: &str,
top_k: usize,
params: TextSearchParams,
allowed_ids: Option<&HashSet<String>>,
) -> NodeDbResult<Vec<SearchResult>>;
async fn batch_vector_insert(
&self,
collection: &str,
vectors: &[(&str, &[f32])],
) -> NodeDbResult<()> {
default_impls::batch_vector_insert_default(self, collection, vectors).await
}
async fn batch_graph_insert_edges(
&self,
collection: &str,
edges: &[(&str, &str, &str)],
) -> NodeDbResult<()> {
default_impls::batch_graph_insert_edges_default(self, collection, edges).await
}
fn proto_version(&self) -> u16 {
0
}
fn capabilities(&self) -> u64 {
0
}
fn server_version(&self) -> String {
String::new()
}
fn limits(&self) -> Limits {
Limits::default()
}
async fn execute_sql(&self, query: &str, params: &[Value]) -> NodeDbResult<QueryResult>;
async fn undrop_collection(&self, name: &str) -> NodeDbResult<()> {
default_impls::undrop_collection_default(self, name).await
}
async fn drop_collection_purge(&self, name: &str) -> NodeDbResult<()> {
default_impls::drop_collection_purge_default(self, name).await
}
async fn list_dropped_collections(&self) -> NodeDbResult<Vec<DroppedCollection>> {
default_impls::list_dropped_collections_default(self).await
}
async fn on_collection_purged(&self, _handler: CollectionPurgedHandler) -> NodeDbResult<()> {
default_impls::on_collection_purged_default()
}
}