aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/rjeczalik/notify/tree_recursive.go
blob: 7f00dfe35a2ce6d1e5bfb3b45ce697c68afaa9e9 (plain) (blame)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// 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"

// watchAdd TODO(rjeczalik)
func watchAdd(nd node, c chan<- EventInfo, e Event) eventDiff {
    diff := nd.Watch.Add(c, e)
    if wp := nd.Child[""].Watch; len(wp) != 0 {
        e = wp.Total()
        diff[0] |= e
        diff[1] |= e
        if diff[0] == diff[1] {
            return none
        }
    }
    return diff
}

// watchAddInactive TODO(rjeczalik)
func watchAddInactive(nd node, c chan<- EventInfo, e Event) eventDiff {
    wp := nd.Child[""].Watch
    if wp == nil {
        wp = make(watchpoint)
        nd.Child[""] = node{Watch: wp}
    }
    diff := wp.Add(c, e)
    e = nd.Watch.Total()
    diff[0] |= e
    diff[1] |= e
    if diff[0] == diff[1] {
        return none
    }
    return diff
}

// watchCopy TODO(rjeczalik)
func watchCopy(src, dst node) {
    for c, e := range src.Watch {
        if c == nil {
            continue
        }
        watchAddInactive(dst, c, e)
    }
    if wpsrc := src.Child[""].Watch; len(wpsrc) != 0 {
        wpdst := dst.Child[""].Watch
        for c, e := range wpsrc {
            if c == nil {
                continue
            }
            wpdst.Add(c, e)
        }
    }
}

// watchDel TODO(rjeczalik)
func watchDel(nd node, c chan<- EventInfo, e Event) eventDiff {
    diff := nd.Watch.Del(c, e)
    if wp := nd.Child[""].Watch; len(wp) != 0 {
        diffInactive := wp.Del(c, e)
        e = wp.Total()
        // TODO(rjeczalik): add e if e != all?
        diff[0] |= diffInactive[0] | e
        diff[1] |= diffInactive[1] | e
        if diff[0] == diff[1] {
            return none
        }
    }
    return diff
}

// watchTotal TODO(rjeczalik)
func watchTotal(nd node) Event {
    e := nd.Watch.Total()
    if wp := nd.Child[""].Watch; len(wp) != 0 {
        e |= wp.Total()
    }
    return e
}

// watchIsRecursive TODO(rjeczalik)
func watchIsRecursive(nd node) bool {
    ok := nd.Watch.IsRecursive()
    // TODO(rjeczalik): add a test for len(wp) != 0 change the condition.
    if wp := nd.Child[""].Watch; len(wp) != 0 {
        // If a watchpoint holds inactive watchpoints, it means it's a parent
        // one, which is recursive by nature even though it may be not recursive
        // itself.
        ok = true
    }
    return ok
}

// recursiveTree TODO(rjeczalik)
type recursiveTree struct {
    rw   sync.RWMutex // protects root
    root root
    // TODO(rjeczalik): merge watcher + recursiveWatcher after #5 and #6
    w interface {
        watcher
        recursiveWatcher
    }
    c chan EventInfo
}

// newRecursiveTree TODO(rjeczalik)
func newRecursiveTree(w recursiveWatcher, c chan EventInfo) *recursiveTree {
    t := &recursiveTree{
        root: root{nd: newnode("")},
        w: struct {
            watcher
            recursiveWatcher
        }{w.(watcher), w},
        c: c,
    }
    go t.dispatch()
    return t
}

// dispatch TODO(rjeczalik)
func (t *recursiveTree) dispatch() {
    for ei := range t.c {
        dbgprintf("dispatching %v on %q", ei.Event(), ei.Path())
        go func(ei EventInfo) {
            nd, ok := node{}, false
            dir, base := split(ei.Path())
            fn := func(it node, isbase bool) error {
                if isbase {
                    nd = it
                } else {
                    it.Watch.Dispatch(ei, recursive)
                }
                return nil
            }
            t.rw.RLock()
            defer t.rw.RUnlock()
            // Notify recursive watchpoints found on the path.
            if err := t.root.WalkPath(dir, fn); err != nil {
                dbgprint("dispatch did not reach leaf:", err)
                return
            }
            // Notify parent watchpoint.
            nd.Watch.Dispatch(ei, 0)
            // If leaf watchpoint exists, notify it.
            if nd, ok = nd.Child[base]; ok {
                nd.Watch.Dispatch(ei, 0)
            }
        }(ei)
    }
}

// Watch TODO(rjeczalik)
func (t *recursiveTree) 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
    }
    eventset := joinevents(events)
    if isrec {
        eventset |= recursive
    }
    t.rw.Lock()
    defer t.rw.Unlock()
    // case 1: cur is a child
    //
    // Look for parent watch which already covers the given path.
    parent := node{}
    self := false
    err = t.root.WalkPath(path, func(nd node, isbase bool) error {
        if watchTotal(nd) != 0 {
            parent = nd
            self = isbase
            return errSkip
        }
        return nil
    })
    cur := t.root.Add(path) // add after the walk, so it's less to traverse
    if err == nil && parent.Watch != nil {
        // Parent watch found. Register inactive watchpoint, so we have enough
        // information to shrink the eventset on eventual Stop.
        // return t.resetwatchpoint(parent, parent, c, eventset|inactive)
        var diff eventDiff
        if self {
            diff = watchAdd(cur, c, eventset)
        } else {
            diff = watchAddInactive(parent, c, eventset)
        }
        switch {
        case diff == none:
            // the parent watchpoint already covers requested subtree with its
            // eventset
        case diff[0] == 0:
            // TODO(rjeczalik): cleanup this panic after implementation is stable
            panic("dangling watchpoint: " + parent.Name)
        default:
            if isrec || watchIsRecursive(parent) {
                err = t.w.RecursiveRewatch(parent.Name, parent.Name, diff[0], diff[1])
            } else {
                err = t.w.Rewatch(parent.Name, diff[0], diff[1])
            }
            if err != nil {
                watchDel(parent, c, diff.Event())
                return err
            }
            watchAdd(cur, c, eventset)
            // TODO(rjeczalik): account top-most path for c
            return nil
        }
        if !self {
            watchAdd(cur, c, eventset)
        }
        return nil
    }
    // case 2: cur is new parent
    //
    // Look for children nodes, unwatch n-1 of them and rewatch the last one.
    var children []node
    fn := func(nd node) error {
        if len(nd.Watch) == 0 {
            return nil
        }
        children = append(children, nd)
        return errSkip
    }
    switch must(cur.Walk(fn)); len(children) {
    case 0:
        // no child watches, cur holds a new watch
    case 1:
        watchAdd(cur, c, eventset) // TODO(rjeczalik): update cache c subtree root?
        watchCopy(children[0], cur)
        err = t.w.RecursiveRewatch(children[0].Name, cur.Name, watchTotal(children[0]),
            watchTotal(cur))
        if err != nil {
            // Clean inactive watchpoint. The c chan did not exist before.
            cur.Child[""] = node{}
            delete(cur.Watch, c)
            return err
        }
        return nil
    default:
        watchAdd(cur, c, eventset)
        // Copy children inactive watchpoints to the new parent.
        for _, nd := range children {
            watchCopy(nd, cur)
        }
        // Watch parent subtree.
        if err = t.w.RecursiveWatch(cur.Name, watchTotal(cur)); err != nil {
            // Clean inactive watchpoint. The c chan did not exist before.
            cur.Child[""] = node{}
            delete(cur.Watch, c)
            return err
        }
        // Unwatch children subtrees.
        var e error
        for _, nd := range children {
            if watchIsRecursive(nd) {
                e = t.w.RecursiveUnwatch(nd.Name)
            } else {
                e = t.w.Unwatch(nd.Name)
            }
            if e != nil {
                err = nonil(err, e)
                // TODO(rjeczalik): child is still watched, warn all its watchpoints
                // about possible duplicate events via Error event
            }
        }
        return err
    }
    // case 3: cur is new, alone node
    switch diff := watchAdd(cur, c, eventset); {
    case diff == none:
        // TODO(rjeczalik): cleanup this panic after implementation is stable
        panic("watch requested but no parent watchpoint found: " + cur.Name)
    case diff[0] == 0:
        if isrec {
            err = t.w.RecursiveWatch(cur.Name, diff[1])
        } else {
            err = t.w.Watch(cur.Name, diff[1])
        }
        if err != nil {
            watchDel(cur, c, diff.Event())
            return err
        }
    default:
        // TODO(rjeczalik): cleanup this panic after implementation is stable
        panic("watch requested but no parent watchpoint found: " + cur.Name)
    }
    return nil
}

// Stop TODO(rjeczalik)
//
// TODO(rjeczalik): Split parent watchpoint - transfer watches to children
// if parent is no longer needed. This carries a risk that underlying
// watcher calls could fail - reconsider if it's worth the effort.
func (t *recursiveTree) Stop(c chan<- EventInfo) {
    var err error
    fn := func(nd node) (e error) {
        diff := watchDel(nd, c, all)
        switch {
        case diff == none && watchTotal(nd) == 0:
            // TODO(rjeczalik): There's no watchpoints deeper in the tree,
            // probably we should remove the nodes as well.
            return nil
        case diff == none:
            // Removing c from nd does not require shrinking its eventset.
        case diff[1] == 0:
            if watchIsRecursive(nd) {
                e = t.w.RecursiveUnwatch(nd.Name)
            } else {
                e = t.w.Unwatch(nd.Name)
            }
        default:
            if watchIsRecursive(nd) {
                e = t.w.RecursiveRewatch(nd.Name, nd.Name, diff[0], diff[1])
            } else {
                e = t.w.Rewatch(nd.Name, diff[0], diff[1])
            }
        }
        fn := func(nd node) error {
            watchDel(nd, c, all)
            return nil
        }
        err = nonil(err, e, nd.Walk(fn))
        // TODO(rjeczalik): if e != nil store dummy chan in nd.Watch just to
        // retry un/rewatching next time and/or let the user handle the failure
        // vie Error event?
        return errSkip
    }
    t.rw.Lock()
    e := t.root.Walk("", fn) // TODO(rjeczalik): use max root per c
    t.rw.Unlock()
    if e != nil {
        err = nonil(err, e)
    }
    dbgprintf("Stop(%p) error: %v\n", c, err)
}

// Close TODO(rjeczalik)
func (t *recursiveTree) Close() error {
    err := t.w.Close()
    close(t.c)
    return err
}