use crate::error::{Error, Result, TransactionErrorKind};
use crate::graph_model::{Node, NodeId, Relationship, RelationshipId};
use crate::value::{Properties, Value};
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct DatabaseMetadata {
pub name: String,
pub external_store_id: [u8; 16],
pub creation_time_millis: i64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum SchemaEntityType {
Node,
Relationship,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct SchemaDescriptor {
pub entity_type: SchemaEntityType,
pub entity_tokens: Vec<String>,
pub property_keys: Vec<String>,
pub matching_type: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct SchemaIndexDescriptor {
pub id: u64,
pub name: String,
pub schema: SchemaDescriptor,
pub index_type: String,
pub provider: String,
pub unique: bool,
pub owning_constraint: Option<u64>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct SchemaConstraintDescriptor {
pub id: u64,
pub name: String,
pub schema: SchemaDescriptor,
pub constraint_type: String,
pub graph_type_dependence: String,
pub index_type: Option<String>,
pub owned_index: Option<u64>,
pub property_types: Vec<String>,
pub endpoint_type: Option<String>,
pub endpoint_label: Option<String>,
pub required_label: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct SchemaMetadata {
pub indexes: Vec<SchemaIndexDescriptor>,
pub constraints: Vec<SchemaConstraintDescriptor>,
}
pub(crate) trait StorageInterface {
fn check_query_control(&self) -> Result<()> {
Ok(())
}
fn charge_query_memory(&self, _bytes: usize) -> Result<()> {
Ok(())
}
fn database_metadata(&self) -> Option<&DatabaseMetadata> {
None
}
fn schema_metadata(&self) -> Option<&SchemaMetadata> {
None
}
fn node_ids(&self) -> Vec<NodeId>;
fn relationship_ids(&self) -> Vec<RelationshipId>;
fn node(&self, id: NodeId) -> Option<&Node>;
fn relationship(&self, id: RelationshipId) -> Option<&Relationship>;
fn outgoing_relationships(&self, id: NodeId) -> &[RelationshipId];
fn incoming_relationships(&self, id: NodeId) -> &[RelationshipId];
fn nodes_with_label(&self, label: &str) -> &[NodeId];
fn node_by_external_id(&self, external_id: &str) -> Option<NodeId>;
fn relationship_by_external_id(&self, external_id: &str) -> Option<RelationshipId>;
fn label_tokens(&self) -> Vec<String>;
fn relationship_type_tokens(&self) -> Vec<String>;
fn property_key_tokens(&self) -> Vec<String>;
fn node_property_type(&self, _id: NodeId, _property_key: &str) -> Option<String> {
None
}
fn relationship_property_type(
&self,
_id: RelationshipId,
_property_key: &str,
) -> Option<String> {
None
}
}
pub(crate) struct EmptyEntityLookup;
impl StorageInterface for EmptyEntityLookup {
fn node_ids(&self) -> Vec<NodeId> {
Vec::new()
}
fn relationship_ids(&self) -> Vec<RelationshipId> {
Vec::new()
}
fn node(&self, _id: NodeId) -> Option<&Node> {
None
}
fn relationship(&self, _id: RelationshipId) -> Option<&Relationship> {
None
}
fn outgoing_relationships(&self, _id: NodeId) -> &[RelationshipId] {
&[]
}
fn incoming_relationships(&self, _id: NodeId) -> &[RelationshipId] {
&[]
}
fn nodes_with_label(&self, _label: &str) -> &[NodeId] {
&[]
}
fn node_by_external_id(&self, _external_id: &str) -> Option<NodeId> {
None
}
fn relationship_by_external_id(&self, _external_id: &str) -> Option<RelationshipId> {
None
}
fn label_tokens(&self) -> Vec<String> {
Vec::new()
}
fn relationship_type_tokens(&self) -> Vec<String> {
Vec::new()
}
fn property_key_tokens(&self) -> Vec<String> {
Vec::new()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum TransactionState {
Active,
Failed,
}
pub(crate) trait TransactionalStorage: StorageInterface {
fn begin_transaction(&mut self) -> Result<()>;
fn commit_transaction(&mut self) -> Result<()>;
fn rollback_transaction(&mut self) -> Result<()>;
fn mark_transaction_failed(&mut self) {
if let Some(state) = self.transaction_state_mut() {
*state = TransactionState::Failed;
}
}
fn transaction_state(&self) -> Option<TransactionState>;
fn transaction_state_mut(&mut self) -> Option<&mut TransactionState>;
fn require_active_transaction(&self) -> Result<()> {
match self.transaction_state() {
Some(TransactionState::Active) => Ok(()),
Some(TransactionState::Failed) => {
Err(Error::Transaction(TransactionErrorKind::RollbackOnly))
}
None => Err(Error::Transaction(TransactionErrorKind::NotActive)),
}
}
}
pub(crate) trait WritableStorage: TransactionalStorage {
fn ensure_label_token(&mut self, label: &str) -> Result<()>;
fn ensure_relationship_type_token(&mut self, relationship_type: &str) -> Result<()>;
fn ensure_property_key_token(&mut self, property_key: &str) -> Result<()>;
fn create_node(&mut self, labels: Vec<String>, properties: Properties) -> Result<NodeId>;
fn create_relationship(
&mut self,
start: NodeId,
end: NodeId,
r#type: String,
properties: Properties,
) -> Result<RelationshipId>;
fn set_node_property(&mut self, id: NodeId, key: String, value: Value) -> Result<()>;
fn remove_node_property(&mut self, id: NodeId, key: &str) -> Result<()>;
fn replace_node_properties(&mut self, id: NodeId, properties: Properties) -> Result<()>;
fn set_node_label(&mut self, id: NodeId, label: String) -> Result<()>;
fn remove_node_label(&mut self, id: NodeId, label: &str) -> Result<()>;
fn set_relationship_property(
&mut self,
id: RelationshipId,
key: String,
value: Value,
) -> Result<()>;
fn remove_relationship_property(&mut self, id: RelationshipId, key: &str) -> Result<()>;
fn replace_relationship_properties(
&mut self,
id: RelationshipId,
properties: Properties,
) -> Result<()>;
fn delete_relationship(&mut self, id: RelationshipId) -> Result<()>;
fn delete_node(&mut self, id: NodeId) -> Result<()>;
fn detach_delete_node(&mut self, id: NodeId) -> Result<()>;
}