forked from ethereumproject/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree_nonrecursive.go
292 lines (275 loc) · 7.06 KB
/
tree_nonrecursive.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
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
// Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
package notify
import "sync"
// nonrecursiveTree TODO(rjeczalik)
type nonrecursiveTree struct {
rw sync.RWMutex // protects root
root root
w watcher
c chan EventInfo
rec chan EventInfo
}
// newNonrecursiveTree TODO(rjeczalik)
func newNonrecursiveTree(w watcher, c, rec chan EventInfo) *nonrecursiveTree {
if rec == nil {
rec = make(chan EventInfo, buffer)
}
t := &nonrecursiveTree{
root: root{nd: newnode("")},
w: w,
c: c,
rec: rec,
}
go t.dispatch(c)
go t.internal(rec)
return t
}
// dispatch TODO(rjeczalik)
func (t *nonrecursiveTree) dispatch(c <-chan EventInfo) {
for ei := range c {
dbgprintf("dispatching %v on %q", ei.Event(), ei.Path())
go func(ei EventInfo) {
var nd node
var isrec bool
dir, base := split(ei.Path())
fn := func(it node, isbase bool) error {
isrec = isrec || it.Watch.IsRecursive()
if isbase {
nd = it
} else {
it.Watch.Dispatch(ei, recursive)
}
return nil
}
t.rw.RLock()
// Notify recursive watchpoints found on the path.
if err := t.root.WalkPath(dir, fn); err != nil {
dbgprint("dispatch did not reach leaf:", err)
t.rw.RUnlock()
return
}
// Notify parent watchpoint.
nd.Watch.Dispatch(ei, 0)
isrec = isrec || nd.Watch.IsRecursive()
// If leaf watchpoint exists, notify it.
if nd, ok := nd.Child[base]; ok {
isrec = isrec || nd.Watch.IsRecursive()
nd.Watch.Dispatch(ei, 0)
}
t.rw.RUnlock()
// If the event describes newly leaf directory created within
if !isrec || ei.Event() != Create {
return
}
if ok, err := ei.(isDirer).isDir(); !ok || err != nil {
return
}
t.rec <- ei
}(ei)
}
}
// internal TODO(rjeczalik)
func (t *nonrecursiveTree) internal(rec <-chan EventInfo) {
for ei := range rec {
var nd node
var eset = internal
t.rw.Lock()
t.root.WalkPath(ei.Path(), func(it node, _ bool) error {
if e := it.Watch[t.rec]; e != 0 && e > eset {
eset = e
}
nd = it
return nil
})
if eset == internal {
t.rw.Unlock()
continue
}
err := nd.Add(ei.Path()).AddDir(t.recFunc(eset))
t.rw.Unlock()
if err != nil {
dbgprintf("internal(%p) error: %v", rec, err)
}
}
}
// watchAdd TODO(rjeczalik)
func (t *nonrecursiveTree) watchAdd(nd node, c chan<- EventInfo, e Event) eventDiff {
if e&recursive != 0 {
diff := nd.Watch.Add(t.rec, e|Create|omit)
nd.Watch.Add(c, e)
return diff
}
return nd.Watch.Add(c, e)
}
// watchDelMin TODO(rjeczalik)
func (t *nonrecursiveTree) watchDelMin(min Event, nd node, c chan<- EventInfo, e Event) eventDiff {
old, ok := nd.Watch[t.rec]
if ok {
nd.Watch[t.rec] = min
}
diff := nd.Watch.Del(c, e)
if ok {
switch old &^= diff[0] &^ diff[1]; {
case old|internal == internal:
delete(nd.Watch, t.rec)
if set, ok := nd.Watch[nil]; ok && len(nd.Watch) == 1 && set == 0 {
delete(nd.Watch, nil)
}
default:
nd.Watch.Add(t.rec, old|Create)
switch {
case diff == none:
case diff[1]|Create == diff[0]:
diff = none
default:
diff[1] |= Create
}
}
}
return diff
}
// watchDel TODO(rjeczalik)
func (t *nonrecursiveTree) watchDel(nd node, c chan<- EventInfo, e Event) eventDiff {
return t.watchDelMin(0, nd, c, e)
}
// Watch TODO(rjeczalik)
func (t *nonrecursiveTree) Watch(path string, c chan<- EventInfo, events ...Event) error {
if c == nil {
panic("notify: Watch using nil channel")
}
// Expanding with empty event set is a nop.
if len(events) == 0 {
return nil
}
path, isrec, err := cleanpath(path)
if err != nil {
return err
}
eset := joinevents(events)
t.rw.Lock()
defer t.rw.Unlock()
nd := t.root.Add(path)
if isrec {
return t.watchrec(nd, c, eset|recursive)
}
return t.watch(nd, c, eset)
}
func (t *nonrecursiveTree) watch(nd node, c chan<- EventInfo, e Event) (err error) {
diff := nd.Watch.Add(c, e)
switch {
case diff == none:
return nil
case diff[1] == 0:
// TODO(rjeczalik): cleanup this panic after implementation is stable
panic("eset is empty: " + nd.Name)
case diff[0] == 0:
err = t.w.Watch(nd.Name, diff[1])
default:
err = t.w.Rewatch(nd.Name, diff[0], diff[1])
}
if err != nil {
nd.Watch.Del(c, diff.Event())
return err
}
return nil
}
func (t *nonrecursiveTree) recFunc(e Event) walkFunc {
return func(nd node) error {
switch diff := nd.Watch.Add(t.rec, e|omit|Create); {
case diff == none:
case diff[1] == 0:
// TODO(rjeczalik): cleanup this panic after implementation is stable
panic("eset is empty: " + nd.Name)
case diff[0] == 0:
t.w.Watch(nd.Name, diff[1])
default:
t.w.Rewatch(nd.Name, diff[0], diff[1])
}
return nil
}
}
func (t *nonrecursiveTree) watchrec(nd node, c chan<- EventInfo, e Event) error {
var traverse func(walkFunc) error
// Non-recursive tree listens on Create event for every recursive
// watchpoint in order to automagically set a watch for every
// created directory.
switch diff := nd.Watch.dryAdd(t.rec, e|Create); {
case diff == none:
t.watchAdd(nd, c, e)
nd.Watch.Add(t.rec, e|omit|Create)
return nil
case diff[1] == 0:
// TODO(rjeczalik): cleanup this panic after implementation is stable
panic("eset is empty: " + nd.Name)
case diff[0] == 0:
// TODO(rjeczalik): BFS into directories and skip subtree as soon as first
// recursive watchpoint is encountered.
traverse = nd.AddDir
default:
traverse = nd.Walk
}
// TODO(rjeczalik): account every path that failed to be (re)watched
// and retry.
if err := traverse(t.recFunc(e)); err != nil {
return err
}
t.watchAdd(nd, c, e)
return nil
}
type walkWatchpointFunc func(Event, node) error
func (t *nonrecursiveTree) walkWatchpoint(nd node, fn walkWatchpointFunc) error {
type minode struct {
min Event
nd node
}
mnd := minode{nd: nd}
stack := []minode{mnd}
Traverse:
for n := len(stack); n != 0; n = len(stack) {
mnd, stack = stack[n-1], stack[:n-1]
// There must be no recursive watchpoints if the node has no watchpoints
// itself (every node in subtree rooted at recursive watchpoints must
// have at least nil (total) and t.rec watchpoints).
if len(mnd.nd.Watch) != 0 {
switch err := fn(mnd.min, mnd.nd); err {
case nil:
case errSkip:
continue Traverse
default:
return err
}
}
for _, nd := range mnd.nd.Child {
stack = append(stack, minode{mnd.nd.Watch[t.rec], nd})
}
}
return nil
}
// Stop TODO(rjeczalik)
func (t *nonrecursiveTree) Stop(c chan<- EventInfo) {
fn := func(min Event, nd node) error {
// TODO(rjeczalik): aggregate watcher errors and retry; in worst case
// forward to the user.
switch diff := t.watchDelMin(min, nd, c, all); {
case diff == none:
return nil
case diff[1] == 0:
t.w.Unwatch(nd.Name)
default:
t.w.Rewatch(nd.Name, diff[0], diff[1])
}
return nil
}
t.rw.Lock()
err := t.walkWatchpoint(t.root.nd, fn) // TODO(rjeczalik): store max root per c
t.rw.Unlock()
dbgprintf("Stop(%p) error: %v\n", c, err)
}
// Close TODO(rjeczalik)
func (t *nonrecursiveTree) Close() error {
err := t.w.Close()
close(t.c)
return err
}