1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use std::{collections::HashMap, mem};
use bincode::{Decode, Encode, config};
use crate::{
PreparedQuery,
errors::LatticeError,
graph::graph_builder::GraphBuilder,
graph_builder::{GlobalVertexId, GraphId},
properties::PropertyHandle,
utils::{generational_vector::Handle, values::Primitive},
};
#[derive(Encode, Decode)]
pub(crate) struct PreparedVertex {
pub(crate) id: GlobalVertexId,
pub(crate) attrs: Vec<(PropertyHandle, Primitive)>,
}
#[derive(Encode, Decode, PartialEq)]
pub(crate) struct PreparedEdge {
pub(crate) from: GlobalVertexId,
pub(crate) label: PropertyHandle,
pub(crate) to: GlobalVertexId,
}
#[derive(Encode, Decode)]
pub(crate) struct PreparedGraph {
pub(crate) id: GraphId,
pub(crate) vertices: Vec<PreparedVertex>,
pub(crate) edges: Vec<PreparedEdge>,
}
#[derive(Debug)]
pub(crate) struct GraphCommitData {
pub(crate) graph_id: u64,
pub(crate) prepared_graph: Vec<u8>, // serialized PreparedGraph
pub(crate) add_attrs: Vec<(u64, u64, u64)>, // (vertex id, prop id, value hash)
pub(crate) rem_attrs: Vec<(u64, u64, u64)>,
pub(crate) add_edges: Vec<(u64, u64, u64)>, // (from, label, to)
pub(crate) rem_edges: Vec<(u64, u64, u64)>,
pub(crate) deleted_vertices: Vec<u64>, // vertex id
}
impl PreparedGraph {
// relies on GraphBuilder populating the vertices and edges in the same order as the PreparedGraph's edges and vertices
pub(crate) fn commit_data_from_builder(
graph: GraphBuilder,
start_id: u64,
graph_id: u64,
_auto_queries: &[PreparedQuery], // future implementation to add a query check to an item automatically
) -> Result<GraphCommitData, LatticeError> {
let mut global_id_cursor = start_id;
let GraphBuilder {
mut vertices,
edges,
old_graph_data,
..
} = graph;
let (old_vertices, old_edges) = if let Some(ogd) = old_graph_data {
(Some(ogd.graph.vertices), Some(ogd.graph.edges))
} else {
(None, None)
};
let mut add_attrs = vec![];
let mut rem_attrs = vec![];
let mut add_edges = vec![];
let mut rem_edges = vec![];
let mut deleted_vertices = vec![];
let mut proc_vertices = vec![];
let mut proc_edges = vec![];
let mut new_vertices_start = 0;
// maps vertex GenVec index to global id
let mut idx_to_global = HashMap::new();
// iterates through vertices that might be old
if let Some(mut old_vertices) = old_vertices {
new_vertices_start = old_vertices.len();
// iterate through the vertexes that might be continued from the old_graph
for (idx, old_vertex) in old_vertices.iter_mut().enumerate() {
if let Some(continued_vertex) = vertices.get_mut(Handle {
generation: 0,
index: idx,
}) {
// vertex was not deleted
if continued_vertex.attributes == old_vertex.attrs {
// vertex was unchanged
idx_to_global.insert(idx, continued_vertex.global_id.unwrap());
proc_vertices.push(PreparedVertex {
id: continued_vertex.global_id.unwrap(),
attrs: mem::take(&mut old_vertex.attrs),
});
} else {
// vertex was changed
let mut new_attrs = mem::take(&mut continued_vertex.attributes);
new_attrs.sort_unstable_by_key(|(attr, val)| (attr.0, val.hash())); // sort by attribute id
let global_id = continued_vertex.global_id.unwrap();
// find which attributes were changed
let mut old_iter = old_vertex.attrs.iter().peekable();
let mut new_iter = new_attrs.iter().peekable();
loop {
match (old_iter.peek(), new_iter.peek()) {
// both iters done
(None, None) => break,
// no more new attrs, so remaining old attrs were removed
(Some((old_attr, old_val)), None) => {
rem_attrs.push((global_id, old_attr.0, old_val.hash()));
old_iter.next();
}
// no more old attrs, so remaining new attrs were added
(None, Some((new_attr, new_val))) => {
add_attrs.push((global_id, new_attr.0, new_val.hash()));
new_iter.next();
}
// compare attrs
(Some((old_attr, old_val)), Some((new_attr, new_val))) => {
let old_key = (old_attr.0, old_val.hash());
let new_key = (new_attr.0, new_val.hash());
if old_key == new_key {
// old and new attrs are the same, so the attr was unchanged
old_iter.next();
new_iter.next();
} else if old_key < new_key {
// old attr doesn't have a match (new attr past match value, so no match value exists), so old value was removed
rem_attrs.push((global_id, old_attr.0, old_val.hash()));
old_iter.next();
} else {
// new attr doesn't have a match (old attr past match value, so no match value exists), so new value was added
add_attrs.push((global_id, new_attr.0, new_val.hash()));
new_iter.next();
}
}
}
}
idx_to_global.insert(idx, global_id);
proc_vertices.push(PreparedVertex {
id: global_id,
attrs: new_attrs,
});
}
} else {
// vertex was deleted
deleted_vertices.push(old_vertex.id);
for (attr, value) in &old_vertex.attrs {
rem_attrs.push((old_vertex.id, attr.0, value.hash())); // remove all old attributes
}
if let Some(new_vertex) = vertices.get_mut_index(idx) {
// new vertex created in freed slot
let mut new_attrs = mem::take(&mut new_vertex.attributes);
new_attrs.sort_unstable_by_key(|(attr, val)| (attr.0, val.hash()));
for (attr, value) in &new_attrs {
add_attrs.push((global_id_cursor, attr.0, value.hash())); // add all new attributes
}
idx_to_global.insert(idx, global_id_cursor);
proc_vertices.push(PreparedVertex {
id: global_id_cursor,
attrs: new_attrs,
});
global_id_cursor += 1; // next new vertex will have a new global id
}
}
}
}
// iterates through vertices that are guarenteed new
for (h, new_vertex) in vertices.iter_mut_from(new_vertices_start) {
let global_id = global_id_cursor;
global_id_cursor += 1;
let mut new_attrs = mem::take(&mut new_vertex.attributes);
new_attrs.sort_unstable_by_key(|(attr, val)| (attr.0, val.hash()));
for (attr, value) in &new_attrs {
add_attrs.push((global_id, attr.0, value.hash())); // add all new attributes
}
idx_to_global.insert(h.index, global_id);
proc_vertices.push(PreparedVertex {
id: global_id,
attrs: new_attrs,
});
}
// iterates through edges that might be old
let mut new_edges_start = 0;
if let Some(old_edges) = old_edges {
new_edges_start = old_edges.len();
// iterate through the edges that might be continued from the old_graph
for (idx, old_edge) in old_edges.iter().enumerate() {
if let Some(continued_edge) = edges.get(Handle {
generation: 0,
index: idx,
}) {
// get global ids
let from = *idx_to_global
.get(&continued_edge.from.0.index)
.expect("Source vertex missing");
let to = *idx_to_global
.get(&continued_edge.to.0.index)
.expect("Destination vertex missing");
let current_label = continued_edge.label.0;
// compare global ids
let unchanged = from == old_edge.from
&& current_label == old_edge.label.0
&& to == old_edge.to;
if unchanged {
// edge was unchanged
proc_edges.push(PreparedEdge {
from: old_edge.from,
label: old_edge.label,
to: old_edge.to,
});
} else {
// edge was changed
rem_edges.push((old_edge.from, old_edge.label.0, old_edge.to)); // remove old edge
add_edges.push((from, current_label, to)); // add new edge
proc_edges.push(PreparedEdge {
from,
label: continued_edge.label,
to,
});
}
} else {
// edge was delted
rem_edges.push((old_edge.from, old_edge.label.0, old_edge.to));
if let Some(new_edge) = edges.get_index(idx) {
// new edge created in freed slot
let from = *idx_to_global
.get(&new_edge.from.0.index)
.expect("Source vertex missing");
let to = *idx_to_global
.get(&new_edge.to.0.index)
.expect("Destination vertex missing");
add_edges.push((from, new_edge.label.0, to));
proc_edges.push(PreparedEdge {
from,
label: new_edge.label,
to,
});
}
}
}
}
// iterates through edges that are guarenteed new
for (_, new_edge) in edges.iter_from(new_edges_start) {
let from = *idx_to_global
.get(&new_edge.from.0.index)
.expect("Source vertex missing");
let to = *idx_to_global
.get(&new_edge.to.0.index)
.expect("Destination vertex missing");
add_edges.push((from, new_edge.label.0, to));
proc_edges.push(PreparedEdge {
from,
label: new_edge.label,
to,
});
}
// return computed changes
let prepared_graph = bincode::encode_to_vec(
PreparedGraph {
id: graph_id,
vertices: proc_vertices,
edges: proc_edges,
},
config::standard(),
)?;
Ok(GraphCommitData {
graph_id,
prepared_graph,
add_attrs,
rem_attrs,
add_edges,
rem_edges,
deleted_vertices,
})
}
}