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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use backend::*;
use patch::*;
use rand;
use std::collections::HashSet;
use Result;
impl<'env, T: rand::Rng> MutTxn<'env, T> {
pub fn apply(
&mut self,
branch: &mut Branch,
patch: &Patch,
patch_id: PatchId,
timestamp: ApplyTimestamp,
) -> Result<()> {
assert!(self.put_patches(&mut branch.patches, patch_id, timestamp)?);
assert!(self.put_revpatches(&mut branch.revpatches, timestamp, patch_id)?);
debug!("apply_raw");
for ch in patch.changes().iter() {
if let Change::NewNodes {
ref up_context,
ref down_context,
ref line_num,
flag,
ref nodes,
..
} = *ch
{
assert!(!nodes.is_empty());
debug!("apply: newnodes");
self.add_new_nodes(
branch,
patch_id,
up_context,
down_context,
line_num,
flag,
nodes,
)?;
}
}
let mut parents: HashSet<Key<PatchId>> = HashSet::new();
let mut children: HashSet<Edge> = HashSet::new();
for ch in patch.changes().iter() {
if let Change::NewEdges {
previous,
flag,
ref edges,
..
} = *ch
{
self.add_new_edges(
branch,
patch_id,
Some(previous),
flag,
edges,
&mut parents,
&mut children,
)?;
debug!("apply_raw:edges.done");
}
}
self.repair_deleted_contexts(branch, patch, patch_id)?;
Ok(())
}
fn delete_old_edge(
&mut self,
branch: &mut Branch,
patch_id: PatchId,
previous: EdgeFlags,
from: Key<PatchId>,
to: Key<PatchId>,
introduced_by: PatchId,
) -> Result<()> {
debug!(
"delete {:?} -> {:?} ({:?}) {:?}",
from, to, previous, introduced_by
);
let mut deleted_e = Edge {
flag: previous,
dest: to,
introduced_by,
};
self.put_cemetery(from, &deleted_e, patch_id)?;
if !self.del_nodes_with_rev(branch, from, deleted_e)? {
debug!("killing pseudo instead {:?} {:?}", from, deleted_e);
deleted_e.flag |= EdgeFlags::PSEUDO_EDGE;
let result = self.del_nodes_with_rev(branch, from, deleted_e)?;
debug!("killed ? {:?}", result);
}
Ok(())
}
fn add_new_edges(
&mut self,
branch: &mut Branch,
patch_id: PatchId,
previous: Option<EdgeFlags>,
flag: EdgeFlags,
edges: &[NewEdge],
parents: &mut HashSet<Key<PatchId>>,
children: &mut HashSet<Edge>,
) -> Result<()> {
for e in edges {
debug!("add_new_edges {:?}", e);
let e_from = self.internal_key(&e.from, patch_id);
let e_to = self.internal_key(&e.to, patch_id);
assert!(e_from != e_to);
let to = if flag.contains(EdgeFlags::PARENT_EDGE) {
e_from
} else {
e_to
};
if flag.contains(EdgeFlags::DELETED_EDGE) && !flag.contains(EdgeFlags::FOLDER_EDGE) {
self.reconnect_parents_children(branch, patch_id, to, parents, children)?;
}
let introduced_by = self.internal_hash(&e.introduced_by, patch_id);
if let Some(previous) = previous {
self.delete_old_edge(branch, patch_id, previous, e_from, e_to, introduced_by)?
}
if flag.contains(EdgeFlags::DELETED_EDGE) && !flag.contains(EdgeFlags::FOLDER_EDGE) {
self.delete_old_pseudo_edges(branch, patch_id, to, children)?
}
let e = Edge {
flag,
dest: e_to,
introduced_by: patch_id.clone(),
};
self.put_nodes_with_rev(branch, e_from, e)?;
}
Ok(())
}
pub fn reconnect_parents_children(
&mut self,
branch: &mut Branch,
patch_id: PatchId,
to: Key<PatchId>,
parents: &mut HashSet<Key<PatchId>>,
children: &mut HashSet<Edge>,
) -> Result<()> {
let mut edge = Edge::zero(EdgeFlags::PARENT_EDGE);
parents.clear();
parents.extend(
self.iter_nodes(&branch, Some((to, Some(&edge))))
.take_while(|&(k, v)| {
k == to && v.flag <= EdgeFlags::PARENT_EDGE | EdgeFlags::PSEUDO_EDGE
})
.filter_map(|(_, v)| {
if !v.flag.contains(EdgeFlags::EPSILON_EDGE)
&& self.is_alive_or_zombie(branch, v.dest)
{
Some(v.dest)
} else {
None
}
}),
);
edge.flag = EdgeFlags::empty();
children.clear();
children.extend(
self.iter_nodes(&branch, Some((to, Some(&edge))))
.take_while(|&(k, v)| {
k == to && v.flag <= EdgeFlags::PSEUDO_EDGE | EdgeFlags::FOLDER_EDGE
})
.map(|(_, e)| *e),
);
debug!("reconnecting {:?} {:?}", parents, children);
for &parent in parents.iter() {
for e in children.iter() {
if parent != e.dest && !self.is_connected(branch, parent, e.dest) {
let pseudo_edge = Edge {
flag: e.flag | EdgeFlags::PSEUDO_EDGE,
dest: e.dest,
introduced_by: patch_id.clone(),
};
debug!("reconnect_parents_children: {:?} {:?}", parent, pseudo_edge);
self.put_nodes_with_rev(branch, parent, pseudo_edge)?;
}
}
}
Ok(())
}
fn delete_old_pseudo_edges(
&mut self,
branch: &mut Branch,
patch_id: PatchId,
to: Key<PatchId>,
pseudo_edges: &mut HashSet<Edge>,
) -> Result<()> {
pseudo_edges.clear();
for (_, to_edge) in self.iter_nodes(branch, Some((to, None)))
.take_while(|&(k, v)| k == to && v.flag < EdgeFlags::DELETED_EDGE)
.filter(|&(_, v)| v.flag.contains(EdgeFlags::PSEUDO_EDGE))
{
let mut e = Edge::zero(
EdgeFlags::DELETED_EDGE
| (to_edge.flag & (EdgeFlags::PARENT_EDGE | EdgeFlags::FOLDER_EDGE)),
);
e.dest = to_edge.dest;
let mut is_zombie_marker = to_edge.introduced_by != patch_id
&& (match self.iter_nodes(branch, Some((to, Some(&e)))).next() {
Some((k, v)) if k == to && v.dest == e.dest && v.flag == e.flag => {
v.introduced_by != patch_id
}
_ => false,
});
debug!(
"is_zombie_marker {:?}: {:?}",
to_edge.dest, is_zombie_marker
);
if !is_zombie_marker {
pseudo_edges.insert(*to_edge);
}
}
debug!("killing pseudo-edges from {:?}: {:?}", to, pseudo_edges);
for edge in pseudo_edges.drain() {
self.del_nodes_with_rev(branch, to, edge)?;
}
Ok(())
}
fn add_new_nodes(
&mut self,
branch: &mut Branch,
patch_id: PatchId,
up_context: &[Key<Option<Hash>>],
down_context: &[Key<Option<Hash>>],
line_num: &LineId,
flag: EdgeFlags,
nodes: &[Vec<u8>],
) -> Result<()> {
debug!("up_context {:?}", up_context);
debug!("down_context {:?}", up_context);
let mut v = Key {
patch: patch_id.clone(),
line: line_num.clone(),
};
let mut e = Edge {
flag: flag ^ EdgeFlags::PARENT_EDGE,
dest: ROOT_KEY,
introduced_by: patch_id,
};
for c in up_context {
e.dest = self.internal_key(c, patch_id);
debug!("up_context: put_nodes {:?} {:?}", v, e);
self.put_nodes_with_rev(branch, v, e)?;
}
debug!("up context done");
e.flag = flag;
e.dest.patch = patch_id;
let mut nodes = nodes.iter();
if let Some(first_line) = nodes.next() {
debug!("first_line = {:?}", first_line);
let value = self.alloc_value(&first_line)?;
debug!("put_contents {:?} {:?}", v, value);
self.put_contents(v, value)?;
}
for content in nodes {
e.dest.line = v.line + 1;
self.put_nodes_with_rev(branch, v, e)?;
v.line = e.dest.line;
if !content.is_empty() {
let value = self.alloc_value(&content)?;
debug!("put_contents {:?} {:?}", v, value);
self.put_contents(v, value)?;
}
}
debug!("newnodes core done");
for c in down_context {
e.dest = self.internal_key(c, patch_id);
self.put_nodes_with_rev(branch, v, e)?;
}
debug!("down context done");
Ok(())
}
}