gravitydb/
lib.rs

1pub mod ql;
2pub mod schema;
3pub mod kv_graph_store;
4pub mod mem_kv_store;
5#[cfg(feature="lua")]
6pub mod lua;
7#[cfg(feature="derive")]
8pub use gravitydb_derive::Schema;
9
10trait GraphFilter<GIN, GOUT>
11{
12  fn filter(&mut self, graph: GIN) -> GOUT;
13}
14
15pub trait Graph<'a, N: 'a, E> {
16  type NodeIterator: Iterator<Item=&'a N>;
17  type NeighborIterator: Iterator<Item=&'a N>;
18  type EdgeIterator: Iterator<Item=(&'a N, &'a N)>;
19
20  /// Returns true if there are no nodes, or false otherwise.
21  fn is_empty(&self) -> bool;
22
23  /// Returns the number of nodes in this graph.
24  fn order(&self) -> usize;
25
26  /// Returns the number of edges in this graph.
27  fn size(&self) -> usize;
28
29  /// Iterates the nodes of this graph
30  fn nodes(&'a self) -> Self::NodeIterator;
31
32  /// Returns true if node is a member, or false otherwise.
33  fn has_node(&self, node: &N) -> bool;
34
35  /// Iterates the neighbors of node.
36  fn neighbors(&'a self, node: &N) -> Result<Self::NeighborIterator, E>;
37
38  /// Returns the number of neighbors connected to node.
39  fn degree(&self, node: &N) -> Result<usize, E>;
40
41  /// Iterates the edges of this graph.
42  fn edges(&'a self) -> Self::EdgeIterator;
43
44  /// Returns true if an edge exists between source and target.
45  fn has_edge(&self, source: &N, target: &N) -> Result<bool, E>;
46}
47
48pub trait DirectedGraph<'a, N: 'a, E>: Graph<'a, N, E> {
49  type OutIterator: Iterator<Item = &'a N>;
50  type InIterator: Iterator<Item = &'a N>;
51
52  /// Iterates the outgoing neighbors of node.
53  fn outgoing(&'a self, node: &N) -> Result<Self::OutIterator, E>;
54
55  /// Iterates the incoming neighbors of node.
56  fn incoming(&'a self, node: &N) -> Result<Self::InIterator, E>;
57}
58
59pub trait WeightedGraph<'a, N:'a, P, E> : Graph<'a, N, E> {
60  /// Returns the weight between source and target.
61  fn weight(&self, source: &'a N, target: &'a N) -> Result<Option<&P>, E>;
62}
63
64pub trait GraphBuilder<N, P, E> {
65  /// Add a new node to the graph
66  fn add_node(&mut self, node: N) -> Result<(), E>;
67  /// Add an edge to the graph
68  ///
69  /// Edges are expected to have properties. If an Implementation
70  /// does not have them it should use ().
71  fn add_edge(&mut self, n1: &N, n2: &N, p: &P) -> Result<(), E>;
72  fn remove_node(&mut self, node: &N) -> Result<(), E>;
73  fn remove_edge(&mut self, n1: &N, n2: &N, p: &P) -> Result<(), E>;
74}
75
76/// A Key-Value Store Backend Interface.
77///
78/// Any Type that implements this interface can be used to run a graph
79/// database.
80pub trait KVStore<E> {
81  /// creates a new bucket
82  fn create_bucket(&mut self, key: &[u8]) -> Result<(), E>;
83  /// delete a data record (could also be a bucket)
84  fn delete_record(&mut self, key: &[u8]) -> Result<(), E>;
85  /// list all records and buckets inside a bucket
86  fn list_records(&self, key: &[u8]) -> Result<Vec<Vec<u8>>, E>;
87  /// store a data record
88  fn store_record(&mut self, key: &[u8], value: &[u8]) -> Result<(), E>;
89  /// fetch a data record
90  fn fetch_record(&self, key: &[u8]) -> Result<Vec<u8>, E>;
91  /// check if an entry exists in the database
92  fn exists(&self, key: &[u8]) -> Result<bool, E>;
93}
94
95/// The Interface for a Graph DB
96pub trait GraphStore<NodeK, Node, EdgeKey, Edge, PropKey, T, E> {
97  // CRUD functions
98  fn create_node(&mut self, id: NodeK, properties: &T) -> Result<NodeK, E>;
99  fn read_node(&self, id: NodeK) -> Result<Node, E>;
100  fn update_node(&mut self, id: NodeK, properties: &T) -> Result<NodeK, E>;
101  fn delete_node(&mut self, id: NodeK) -> Result<NodeK, E>;
102  fn create_edge(&mut self, n1: NodeK, n2: NodeK, properties: &T) -> Result<EdgeKey, E>;
103  fn read_edge(&self, id: &EdgeKey) -> Result<Edge, E>;
104  fn delete_edge(&mut self, id: &EdgeKey) -> Result<(), E>;
105  fn create_property(&mut self, properties: &T) -> Result<PropKey, E>;
106  fn read_property(&self, id: &PropKey) -> Result<T, E>;
107  fn delete_property(&mut self, id: &PropKey) -> Result<(), E>;
108
109  // Query functions
110  //       TODO these functions should have a default implementation
111  //fn query(&self, q: BasicQuery) -> Result<QueryResult, E>;
112}