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
|
package bounded_tree
import (
"sync"
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/util"
)
type Node struct {
Parent *Node
Name string
Children map[string]*Node
}
type BoundedTree struct {
root *Node
sync.RWMutex
baseDir util.FullPath
}
func NewBoundedTree(baseDir util.FullPath) *BoundedTree {
return &BoundedTree{
root: &Node{
Name: "/",
},
baseDir: baseDir,
}
}
type VisitNodeFunc func(path util.FullPath) (childDirectories []string, err error)
// If the path is not visited, call the visitFn for each level of directory
// No action if the directory has been visited before or does not exist.
// A leaf node, which has no children, represents a directory not visited.
// A non-leaf node or a non-existing node represents a directory already visited, or does not need to visit.
func (t *BoundedTree) EnsureVisited(p util.FullPath, visitFn VisitNodeFunc) (visitErr error) {
t.Lock()
defer t.Unlock()
if t.root == nil {
return
}
if t.baseDir != "/" {
p = p[len(t.baseDir):]
}
components := p.Split()
// fmt.Printf("components %v %d\n", components, len(components))
canDelete, err := t.ensureVisited(t.root, t.baseDir, components, 0, visitFn)
if err != nil {
return err
}
if canDelete {
t.root = nil
}
return nil
}
func (t *BoundedTree) ensureVisited(n *Node, currentPath util.FullPath, components []string, i int, visitFn VisitNodeFunc) (canDeleteNode bool, visitErr error) {
// println("ensureVisited", currentPath, i)
if n == nil {
// fmt.Printf("%s null\n", currentPath)
return
}
if n.isVisited() {
// fmt.Printf("%s visited %v\n", currentPath, n.Name)
} else {
// fmt.Printf("ensure %v\n", currentPath)
filerPath := currentPath
if t.baseDir != "/" {
filerPath = t.baseDir + filerPath
}
children, err := visitFn(filerPath)
if err != nil {
glog.V(0).Infof("failed to visit %s: %v", currentPath, err)
return false, err
}
if len(children) == 0 {
// fmt.Printf(" canDelete %v without children\n", currentPath)
return true, nil
}
n.Children = make(map[string]*Node)
for _, child := range children {
// fmt.Printf(" add child %v %v\n", currentPath, child)
n.Children[child] = &Node{
Name: child,
}
}
}
if i >= len(components) {
return
}
// fmt.Printf(" check child %v %v\n", currentPath, components[i])
toVisitNode, found := n.Children[components[i]]
if !found {
// fmt.Printf(" did not find child %v %v\n", currentPath, components[i])
return
}
// fmt.Printf(" ensureVisited %v %v\n", currentPath, toVisitNode.Name)
canDelete, childVisitErr := t.ensureVisited(toVisitNode, currentPath.Child(components[i]), components, i+1, visitFn)
if childVisitErr != nil {
return false, childVisitErr
}
if canDelete {
// fmt.Printf(" delete %v %v\n", currentPath, components[i])
delete(n.Children, components[i])
if len(n.Children) == 0 {
// fmt.Printf(" canDelete %v\n", currentPath)
return true, nil
}
}
return false, nil
}
func (n *Node) isVisited() bool {
if n == nil {
return true
}
if len(n.Children) > 0 {
return true
}
return false
}
func (n *Node) getChild(childName string) *Node {
if n == nil {
return nil
}
if len(n.Children) > 0 {
return n.Children[childName]
}
return nil
}
func (t *BoundedTree) HasVisited(p util.FullPath) bool {
t.RLock()
defer t.RUnlock()
if t.root == nil {
return true
}
components := p.Split()
// fmt.Printf("components %v %d\n", components, len(components))
return t.hasVisited(t.root, util.FullPath("/"), components, 0)
}
func (t *BoundedTree) hasVisited(n *Node, currentPath util.FullPath, components []string, i int) bool {
if n == nil {
return true
}
if !n.isVisited() {
return false
}
// fmt.Printf(" hasVisited child %v %+v %d\n", currentPath, components, i)
if i >= len(components) {
return true
}
toVisitNode, found := n.Children[components[i]]
if !found {
return true
}
return t.hasVisited(toVisitNode, currentPath.Child(components[i]), components, i+1)
}
|