/
children_traverser.go
153 lines (118 loc) · 4.05 KB
/
children_traverser.go
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
package dag
import (
"container/list"
"context"
"sync"
"github.com/pkg/errors"
"github.com/iotaledger/hornet/pkg/common"
"github.com/iotaledger/hornet/pkg/model/hornet"
"github.com/iotaledger/hornet/pkg/utils"
)
// ChildrenTraverser can be used to walk the dag in direction of the tips (future cone).
type ChildrenTraverser struct {
// interface to the used storage.
childrenTraverserStorage ChildrenTraverserStorage
// stack holding the ordered msg to process.
stack *list.List
// discovers map with already found messages.
discovered map[string]struct{}
ctx context.Context
condition Predicate
consumer Consumer
walkAlreadyDiscovered bool
traverserLock sync.Mutex
}
// NewChildrenTraverser create a new traverser to traverse the children (future cone)
func NewChildrenTraverser(childrenTraverserStorage ChildrenTraverserStorage) *ChildrenTraverser {
t := &ChildrenTraverser{
childrenTraverserStorage: childrenTraverserStorage,
stack: list.New(),
discovered: make(map[string]struct{}),
}
return t
}
// reset the traverser for the next walk.
func (t *ChildrenTraverser) reset() {
t.discovered = make(map[string]struct{})
t.stack = list.New()
}
// Traverse starts to traverse the children (future cone) of the given start message until
// the traversal stops due to no more messages passing the given condition.
// It is unsorted BFS because the children are not ordered in the database.
func (t *ChildrenTraverser) Traverse(ctx context.Context, startMessageID hornet.MessageID, condition Predicate, consumer Consumer, walkAlreadyDiscovered bool) error {
// make sure only one traversal is running
t.traverserLock.Lock()
// release lock so the traverser can be reused
defer t.traverserLock.Unlock()
t.ctx = ctx
t.condition = condition
t.consumer = consumer
t.walkAlreadyDiscovered = walkAlreadyDiscovered
// Prepare for a new traversal
t.reset()
t.stack.PushFront(startMessageID)
if !t.walkAlreadyDiscovered {
t.discovered[startMessageID.ToMapKey()] = struct{}{}
}
for t.stack.Len() > 0 {
if err := t.processStackChildren(); err != nil {
return err
}
}
return nil
}
// processStackChildren checks if the current element in the stack must be processed and traversed.
// current element gets consumed first, afterwards it's children get traversed in random order.
func (t *ChildrenTraverser) processStackChildren() error {
if err := utils.ReturnErrIfCtxDone(t.ctx, common.ErrOperationAborted); err != nil {
return err
}
// load candidate msg
ele := t.stack.Front()
currentMessageID := ele.Value.(hornet.MessageID)
// remove the message from the stack
t.stack.Remove(ele)
cachedMsgMeta, err := t.childrenTraverserStorage.CachedMessageMetadata(currentMessageID) // meta +1
if err != nil {
return err
}
if cachedMsgMeta == nil {
// there was an error, stop processing the stack
return errors.Wrapf(common.ErrMessageNotFound, "message ID: %s", currentMessageID.ToHex())
}
defer cachedMsgMeta.Release(true) // meta -1
// check condition to decide if msg should be consumed and traversed
traverse, err := t.condition(cachedMsgMeta.Retain()) // meta pass +1
if err != nil {
// there was an error, stop processing the stack
return err
}
if !traverse {
// message will not get consumed and children are not traversed
return nil
}
if t.consumer != nil {
// consume the message
if err := t.consumer(cachedMsgMeta.Retain()); err != nil { // meta pass +1
// there was an error, stop processing the stack
return err
}
}
childrenMessageIDs, err := t.childrenTraverserStorage.ChildrenMessageIDs(currentMessageID)
if err != nil {
return err
}
for _, childMessageID := range childrenMessageIDs {
if !t.walkAlreadyDiscovered {
childMessageIDMapKey := childMessageID.ToMapKey()
if _, childDiscovered := t.discovered[childMessageIDMapKey]; childDiscovered {
// child was already discovered
continue
}
t.discovered[childMessageIDMapKey] = struct{}{}
}
// traverse the child
t.stack.PushBack(childMessageID)
}
return nil
}