Skip to main content

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    self.order() == 0
23  }
24
25  /// Returns the number of nodes in this graph.
26  fn order(&self) -> usize;
27
28  /// Returns the number of edges in this graph.
29  fn size(&self) -> usize;
30
31  /// Iterates the nodes of this graph
32  fn nodes(&'a self) -> Self::NodeIterator;
33
34  /// Returns true if node is a member, or false otherwise.
35  fn has_node(&self, node: &N) -> bool;
36
37  /// Iterates the neighbors of node.
38  fn neighbors(&'a self, node: &N) -> Result<Self::NeighborIterator, E>;
39
40  /// Returns the number of neighbors connected to node.
41  fn degree(&self, node: &N) -> Result<usize, E>;
42
43  /// Iterates the edges of this graph.
44  fn edges(&'a self) -> Self::EdgeIterator;
45
46  /// Returns true if an edge exists between source and target.
47  fn has_edge(&self, source: &N, target: &N) -> Result<bool, E>;
48}
49
50pub trait DirectedGraph<'a, N: 'a, E>: Graph<'a, N, E> {
51  type OutIterator: Iterator<Item = &'a N>;
52  type InIterator: Iterator<Item = &'a N>;
53
54  /// Iterates the outgoing neighbors of node.
55  fn outgoing(&'a self, node: &N) -> Result<Self::OutIterator, E>;
56
57  /// Iterates the incoming neighbors of node.
58  fn incoming(&'a self, node: &N) -> Result<Self::InIterator, E>;
59}
60
61pub trait WeightedGraph<'a, N:'a, P, E> : Graph<'a, N, E> {
62  /// Returns the weight between source and target.
63  fn weight(&self, source: &'a N, target: &'a N) -> Result<Option<&P>, E>;
64}
65
66pub trait GraphBuilder<N, P, E> {
67  /// Add a new node to the graph
68  fn add_node(&mut self, node: N) -> Result<(), E>;
69  /// Add an edge to the graph
70  ///
71  /// Edges are expected to have properties. If an Implementation
72  /// does not have them it should use ().
73  fn add_edge(&mut self, n1: &N, n2: &N, p: &P) -> Result<(), E>;
74  fn remove_node(&mut self, node: &N) -> Result<(), E>;
75  fn remove_edge(&mut self, n1: &N, n2: &N, p: &P) -> Result<(), E>;
76}
77
78/// Filter an iterator depending on the connected properties
79pub enum PropertyFilter<PropKey> {
80  /// Get only the elements connected with this property
81  Only(PropKey),
82  /// Get all the elements connected with any property in between (and
83  /// including) the start and end properties
84  FromTo(PropKey, PropKey),
85  /// Get all elements (don't filter by property)
86  All,
87}
88
89/// Low level reading and traversing interface for a graph
90pub trait PropertyGraphReader<NodeKey, Node, EdgeKey, Edge, PropKey, Prop, E> {
91  /// List nodes
92  fn nodes(&self, filter: PropertyFilter<PropKey>) -> Result<impl Iterator<Item=NodeKey>, E>;
93  /// List edges
94  fn edges(&self, filter: PropertyFilter<PropKey>) -> Result<impl Iterator<Item=EdgeKey>, E>;
95  /// List properties
96  fn properties(&self, filter: PropertyFilter<PropKey>) -> Result<impl Iterator<Item=PropKey>, E>;
97
98  /// Get a node by its key
99  fn read_node(&self, id: NodeKey) -> Result<Node, E>;
100  /// Get an edge by its key
101  fn read_edge(&self, id: &EdgeKey) -> Result<Edge, E>;
102  /// Get a property by its key
103  fn read_property(&self, id: &PropKey) -> Result<Prop, E>;
104
105  // Query functions
106  //       TODO these functions should have a default implementation
107  //fn query(&self, q: BasicQuery) -> Result<QueryResult, E>;
108}
109
110/// The Interface for a Graph DB
111pub trait GraphStore<NodeKey, EdgeKey, PropKey, Prop, E> {
112  // CRUD functions
113  fn create_node(&mut self, id: NodeKey, properties: &Prop) -> Result<NodeKey, E>;
114  fn update_node(&mut self, id: NodeKey, properties: &Prop) -> Result<NodeKey, E>;
115  fn delete_node(&mut self, id: NodeKey) -> Result<NodeKey, E>;
116  fn create_edge(&mut self, n1: NodeKey, n2: NodeKey, properties: &Prop) -> Result<EdgeKey, E>;
117  fn delete_edge(&mut self, id: &EdgeKey) -> Result<(), E>;
118  fn create_property(&mut self, properties: &Prop) -> Result<PropKey, E>;
119  fn delete_property(&mut self, id: &PropKey) -> Result<(), E>;
120}
121
122/// A Key-Value Store Backend Interface.
123///
124/// Any Type that implements this interface can be used to run a graph
125/// database.
126pub trait KVStore<E> {
127  /// creates a new bucket
128  fn create_bucket(&mut self, key: &[u8]) -> Result<(), E>;
129  /// delete a data record (could also be a bucket)
130  fn delete_record(&mut self, key: &[u8]) -> Result<(), E>;
131  /// list all records in between `from` and `to`
132  ///
133  /// if `to` is empty it searches everything till the first change of `from`
134  fn list_records(&self, from: &[u8], to: &[u8]) -> Result<Vec<Vec<u8>>, E>;
135  /// store a data record
136  fn store_record(&mut self, key: &[u8], value: &[u8]) -> Result<(), E>;
137  /// fetch a data record
138  fn fetch_record(&self, key: &[u8]) -> Result<Vec<u8>, E>;
139  /// check if an entry exists in the database
140  fn exists(&self, key: &[u8]) -> Result<bool, E>;
141}