1use std::collections::{BTreeMap, BTreeSet};
12
13use uqa_core::{Edge, EdgeId, Vertex, VertexId};
14
15use crate::posting_list::GraphPostingListError;
16use crate::types::Direction;
17
18#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
19pub enum GraphStoreError {
20 #[error("graph {0:?} does not exist")]
21 UnknownGraph(String),
22 #[error("graph id space exhausted: {0}")]
23 IdExhausted(String),
24 #[error("invalid graph mutation: {0}")]
25 InvalidMutation(String),
26 #[error("invalid graph query: {0}")]
27 InvalidQuery(String),
28 #[error("corrupt graph state: {0}")]
29 CorruptGraph(String),
30 #[error(transparent)]
31 InvalidPostingList(#[from] GraphPostingListError),
32}
33
34pub type GraphStoreResult<T> = Result<T, GraphStoreError>;
35
36pub trait GraphStore {
42 fn create_graph(&mut self, name: &str);
46
47 fn drop_graph(&mut self, name: &str);
51
52 fn graph_names(&self) -> Vec<String>;
54
55 fn has_graph(&self, name: &str) -> bool;
56
57 fn union_graphs(&mut self, g1: &str, g2: &str, target: &str) -> GraphStoreResult<()>;
61
62 fn intersect_graphs(&mut self, g1: &str, g2: &str, target: &str) -> GraphStoreResult<()>;
64
65 fn difference_graphs(&mut self, g1: &str, g2: &str, target: &str) -> GraphStoreResult<()>;
67
68 fn copy_graph(&mut self, source: &str, target: &str) -> GraphStoreResult<()>;
69
70 fn add_vertex(&mut self, vertex: Vertex, graph: &str) -> GraphStoreResult<()>;
73
74 fn add_edge(&mut self, edge: Edge, graph: &str) -> GraphStoreResult<()>;
75
76 fn remove_vertex(&mut self, vertex_id: VertexId, graph: &str) -> GraphStoreResult<()>;
77
78 fn remove_edge(&mut self, edge_id: EdgeId, graph: &str) -> GraphStoreResult<()>;
79
80 fn neighbors(
86 &self,
87 vertex_id: VertexId,
88 label: Option<&str>,
89 direction: Direction,
90 graph: &str,
91 ) -> GraphStoreResult<Vec<VertexId>>;
92
93 fn vertices_by_label(&self, label: &str, graph: &str) -> GraphStoreResult<Vec<Vertex>>;
94
95 fn vertex_ids_by_label(&self, label: &str, graph: &str) -> GraphStoreResult<Vec<VertexId>> {
97 Ok(self
98 .vertices_by_label(label, graph)?
99 .into_iter()
100 .map(|vertex| vertex.vertex_id)
101 .collect())
102 }
103
104 fn vertices_in_graph(&self, graph: &str) -> GraphStoreResult<Vec<Vertex>>;
105
106 fn edges_in_graph(&self, graph: &str) -> GraphStoreResult<Vec<Edge>>;
107
108 fn vertex_graphs(&self, vertex_id: VertexId) -> BTreeSet<String>;
109
110 fn out_edge_ids(&self, vertex_id: VertexId, graph: &str) -> GraphStoreResult<BTreeSet<EdgeId>>;
113
114 fn in_edge_ids(&self, vertex_id: VertexId, graph: &str) -> GraphStoreResult<BTreeSet<EdgeId>>;
115
116 fn edge_ids_by_label(&self, label: &str, graph: &str) -> GraphStoreResult<BTreeSet<EdgeId>>;
117
118 fn vertex_ids_in_graph(&self, graph: &str) -> GraphStoreResult<BTreeSet<VertexId>>;
119
120 fn require_vertex_in_graph(&self, vertex_id: VertexId, graph: &str) -> GraphStoreResult<()> {
125 if !self.vertex_ids_in_graph(graph)?.contains(&vertex_id) {
126 return Err(GraphStoreError::InvalidQuery(format!(
127 "vertex {vertex_id} is not a member of graph {graph:?}"
128 )));
129 }
130 if self.get_vertex(vertex_id).is_none() {
131 return Err(GraphStoreError::CorruptGraph(format!(
132 "graph {graph:?} references missing vertex {vertex_id}"
133 )));
134 }
135 Ok(())
136 }
137
138 fn degree_distribution(&self, graph: &str) -> GraphStoreResult<BTreeMap<VertexId, u64>>;
141
142 fn label_degree(&self, label: &str, graph: &str) -> GraphStoreResult<f64>;
143
144 fn vertex_label_counts(&self, graph: &str) -> GraphStoreResult<BTreeMap<String, u64>>;
145
146 fn get_vertex(&self, vertex_id: VertexId) -> Option<&Vertex>;
149
150 fn get_edge(&self, edge_id: EdgeId) -> Option<&Edge>;
151
152 fn next_vertex_id(&mut self) -> GraphStoreResult<VertexId>;
154
155 fn next_edge_id(&mut self) -> GraphStoreResult<EdgeId>;
157
158 fn allocate_vertex_id(&mut self, _label: &str, _graph: &str) -> GraphStoreResult<VertexId> {
163 self.next_vertex_id()
164 }
165
166 fn allocate_edge_id(&mut self, _label: &str, _graph: &str) -> GraphStoreResult<EdgeId> {
169 self.next_edge_id()
170 }
171
172 fn clear(&mut self);
173
174 fn vertices(&self) -> BTreeMap<VertexId, Vertex>;
179
180 fn edges(&self) -> BTreeMap<EdgeId, Edge>;
183}