use crate::errors::{Error, Result};
use crate::models;
use crate::models::{EdgeQueryExt, VertexQueryExt};
use serde_json::value::Value as JsonValue;
use std::vec::Vec;
use uuid::Uuid;
pub trait Datastore {
type Trans: Transaction;
fn transaction(&self) -> Result<Self::Trans>;
fn bulk_insert<I>(&self, items: I) -> Result<()>
where
I: Iterator<Item = models::BulkInsertItem>,
{
let trans = self.transaction()?;
for item in items {
match item {
models::BulkInsertItem::Vertex(vertex) => {
trans.create_vertex(&vertex)?;
}
models::BulkInsertItem::Edge(edge_key) => {
trans.create_edge(&edge_key)?;
}
models::BulkInsertItem::VertexProperty(id, name, value) => {
let query = models::SpecificVertexQuery::single(id).property(name);
trans.set_vertex_properties(query, &value)?;
}
models::BulkInsertItem::EdgeProperty(edge_key, name, value) => {
let query = models::SpecificEdgeQuery::single(edge_key).property(name);
trans.set_edge_properties(query, &value)?;
}
}
}
Ok(())
}
}
pub trait Transaction {
fn create_vertex(&self, vertex: &models::Vertex) -> Result<bool>;
fn create_vertex_from_type(&self, t: models::Type) -> Result<Uuid> {
let v = models::Vertex::new(t);
if !self.create_vertex(&v)? {
Err(Error::UuidTaken)
} else {
Ok(v.id)
}
}
fn get_vertices<Q: Into<models::VertexQuery>>(&self, q: Q) -> Result<Vec<models::Vertex>>;
fn delete_vertices<Q: Into<models::VertexQuery>>(&self, q: Q) -> Result<()>;
fn get_vertex_count(&self) -> Result<u64>;
fn create_edge(&self, key: &models::EdgeKey) -> Result<bool>;
fn get_edges<Q: Into<models::EdgeQuery>>(&self, q: Q) -> Result<Vec<models::Edge>>;
fn delete_edges<Q: Into<models::EdgeQuery>>(&self, q: Q) -> Result<()>;
fn get_edge_count(&self, id: Uuid, t: Option<&models::Type>, direction: models::EdgeDirection) -> Result<u64>;
fn get_vertex_properties(&self, q: models::VertexPropertyQuery) -> Result<Vec<models::VertexProperty>>;
fn get_all_vertex_properties<Q: Into<models::VertexQuery>>(&self, q: Q) -> Result<Vec<models::VertexProperties>>;
fn set_vertex_properties(&self, q: models::VertexPropertyQuery, value: &JsonValue) -> Result<()>;
fn delete_vertex_properties(&self, q: models::VertexPropertyQuery) -> Result<()>;
fn get_edge_properties(&self, q: models::EdgePropertyQuery) -> Result<Vec<models::EdgeProperty>>;
fn get_all_edge_properties<Q: Into<models::EdgeQuery>>(&self, q: Q) -> Result<Vec<models::EdgeProperties>>;
fn set_edge_properties(&self, q: models::EdgePropertyQuery, value: &JsonValue) -> Result<()>;
fn delete_edge_properties(&self, q: models::EdgePropertyQuery) -> Result<()>;
}