forked from go-zookeeper/zk
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtree_walker.go
More file actions
122 lines (104 loc) · 3.3 KB
/
tree_walker.go
File metadata and controls
122 lines (104 loc) · 3.3 KB
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
package zk
import (
"context"
"errors"
"fmt"
"iter"
gopath "path"
)
// ChildrenFunc is a function that returns the children of a node.
type ChildrenFunc func(ctx context.Context, path string) ([]string, *Stat, error)
// VisitorFunc is a function that is called for each node visited.
type VisitorFunc func(ctx context.Context, path string, stat *Stat) error
type TraversalOrder int
const (
// BreadthFirstOrder indicates that the tree should be traversed in breadth-first order.
BreadthFirstOrder TraversalOrder = iota
// DepthFirstOrder indicates that the tree should be traversed in depth-first order.
DepthFirstOrder
)
// NewTreeWalker creates a new TreeWalker with the given fetcher function and root path.
func NewTreeWalker(fetcher ChildrenFunc, path string, order TraversalOrder) *TreeWalker {
return &TreeWalker{
fetcher: fetcher,
path: path,
order: order,
}
}
// TreeWalker provides traversal of a tree of nodes rooted at a specific path.
type TreeWalker struct {
fetcher ChildrenFunc
path string
order TraversalOrder
}
// All returns an iterator over all nodes in the tree and an error function.
// The caller can stop iteration early by breaking out of the range loop.
// After iteration, call the returned error function to check if the walk
// was interrupted by an error (as opposed to completing or being broken out of).
func (w *TreeWalker) All(ctx context.Context) (iter.Seq2[string, *Stat], func() error) {
var walkErr error
seq := func(yield func(string, *Stat) bool) {
walkErr = w.Walk(ctx, func(_ context.Context, path string, stat *Stat) error {
if !yield(path, stat) {
return errBreak
}
return nil
})
if errors.Is(walkErr, errBreak) {
walkErr = nil // Break is not an error.
}
}
return seq, func() error { return walkErr }
}
var errBreak = errors.New("break")
// Walk traverses the tree and calls the visitor function for each node visited.
func (w *TreeWalker) Walk(ctx context.Context, visitor VisitorFunc) error {
switch w.order {
case BreadthFirstOrder:
return w.walkBreadthFirst(ctx, w.path, visitor)
case DepthFirstOrder:
return w.walkDepthFirst(ctx, w.path, visitor)
default:
return fmt.Errorf("unknown traversal order: %d", w.order)
}
}
// walkBreadthFirst walks the tree rooted at path in breadth-first order.
func (w *TreeWalker) walkBreadthFirst(ctx context.Context, path string, visitor VisitorFunc) error {
children, stat, err := w.fetcher(ctx, path)
if err != nil {
if errors.Is(err, ErrNoNode) {
return nil // Ignore ErrNoNode.
}
return err
}
if err = visitor(ctx, path, stat); err != nil {
return err
}
for _, child := range children {
childPath := gopath.Join(path, child)
if err = w.walkBreadthFirst(ctx, childPath, visitor); err != nil {
return err
}
}
return nil
}
// walkDepthFirst walks the tree rooted at path in depth-first order.
func (w *TreeWalker) walkDepthFirst(ctx context.Context, path string, visitor VisitorFunc) error {
children, stat, err := w.fetcher(ctx, path)
if err != nil {
if errors.Is(err, ErrNoNode) {
return nil // Ignore ErrNoNode.
}
return err
}
for _, child := range children {
childPath := gopath.Join(path, child)
if err = w.walkDepthFirst(ctx, childPath, visitor); err != nil {
return err
}
}
if err = visitor(ctx, path, stat); err != nil {
return err
}
return nil
}