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
use crate::{
tangle::{Hooks, Tangle},
MessageRef,
};
use bee_message::MessageId;
use std::{collections::HashSet, future::Future};
pub async fn visit_parents_depth_first<Fut, Metadata, Match, Apply, ElseApply, MissingApply, H: Hooks<Metadata>>(
tangle: &Tangle<Metadata, H>,
root: MessageId,
matches: Match,
mut apply: Apply,
mut else_apply: ElseApply,
mut missing_apply: MissingApply,
) where
Fut: Future<Output = bool>,
Metadata: Clone + Copy,
Match: Fn(MessageId, MessageRef, Metadata) -> Fut,
Apply: FnMut(&MessageId, &MessageRef, &Metadata),
ElseApply: FnMut(&MessageId, &MessageRef, &Metadata),
MissingApply: FnMut(&MessageId),
{
let mut parents = Vec::new();
let mut visited = HashSet::new();
parents.push(root);
while let Some(message_id) = parents.pop() {
if !visited.contains(&message_id) {
let msg_meta = tangle
.get_vertex(&message_id)
.await
.as_ref()
.and_then(|v| v.message_and_metadata().cloned());
match msg_meta {
Some((msg, meta)) => {
if matches(message_id, msg.clone(), meta).await {
apply(&message_id, &msg, &meta);
parents.extend_from_slice(&msg.parents());
} else {
else_apply(&message_id, &msg, &meta);
}
}
None => {
missing_apply(&message_id);
}
}
visited.insert(message_id);
}
}
}