chrislu
3 years ago
2 changed files with 0 additions and 305 deletions
@ -1,179 +0,0 @@ |
|||||
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 |
|
||||
} |
|
||||
components := p.Split() |
|
||||
// fmt.Printf("components %v %d\n", components, len(components))
|
|
||||
canDelete, err := t.ensureVisited(t.root, util.FullPath("/"), 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)
|
|
||||
|
|
||||
children, err := visitFn(currentPath) |
|
||||
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) |
|
||||
|
|
||||
} |
|
@ -1,126 +0,0 @@ |
|||||
package bounded_tree |
|
||||
|
|
||||
import ( |
|
||||
"fmt" |
|
||||
"testing" |
|
||||
|
|
||||
"github.com/stretchr/testify/assert" |
|
||||
|
|
||||
"github.com/chrislusf/seaweedfs/weed/util" |
|
||||
) |
|
||||
|
|
||||
var ( |
|
||||
visitFn = func(path util.FullPath) (childDirectories []string, err error) { |
|
||||
fmt.Printf(" visit %v ...\n", path) |
|
||||
switch path { |
|
||||
case "/": |
|
||||
return []string{"a", "g", "h"}, nil |
|
||||
case "/a": |
|
||||
return []string{"b", "f"}, nil |
|
||||
case "/a/b": |
|
||||
return []string{"c", "e"}, nil |
|
||||
case "/a/b/c": |
|
||||
return []string{"d"}, nil |
|
||||
case "/a/b/c/d": |
|
||||
return []string{"i", "j"}, nil |
|
||||
case "/a/b/c/d/i": |
|
||||
return []string{}, nil |
|
||||
case "/a/b/c/d/j": |
|
||||
return []string{}, nil |
|
||||
case "/a/b/e": |
|
||||
return []string{}, nil |
|
||||
case "/a/f": |
|
||||
return []string{}, nil |
|
||||
} |
|
||||
return nil, nil |
|
||||
} |
|
||||
|
|
||||
printMap = func(m map[string]*Node) { |
|
||||
for k := range m { |
|
||||
println(" >", k) |
|
||||
} |
|
||||
} |
|
||||
) |
|
||||
|
|
||||
func TestBoundedTree(t *testing.T) { |
|
||||
|
|
||||
// a/b/c/d/i
|
|
||||
// a/b/c/d/j
|
|
||||
// a/b/c/d
|
|
||||
// a/b/e
|
|
||||
// a/f
|
|
||||
// g
|
|
||||
// h
|
|
||||
|
|
||||
tree := NewBoundedTree(util.FullPath("/")) |
|
||||
|
|
||||
tree.EnsureVisited(util.FullPath("/a/b/c"), visitFn) |
|
||||
|
|
||||
assert.Equal(t, true, tree.HasVisited(util.FullPath("/a/b"))) |
|
||||
assert.Equal(t, true, tree.HasVisited(util.FullPath("/a/b/c"))) |
|
||||
assert.Equal(t, false, tree.HasVisited(util.FullPath("/a/b/c/d"))) |
|
||||
assert.Equal(t, false, tree.HasVisited(util.FullPath("/a/b/e"))) |
|
||||
assert.Equal(t, false, tree.HasVisited(util.FullPath("/a/f"))) |
|
||||
assert.Equal(t, false, tree.HasVisited(util.FullPath("/g"))) |
|
||||
assert.Equal(t, false, tree.HasVisited(util.FullPath("/h"))) |
|
||||
assert.Equal(t, true, tree.HasVisited(util.FullPath("/"))) |
|
||||
assert.Equal(t, true, tree.HasVisited(util.FullPath("/x"))) |
|
||||
assert.Equal(t, false, tree.HasVisited(util.FullPath("/a/b/e/x"))) |
|
||||
|
|
||||
printMap(tree.root.Children) |
|
||||
|
|
||||
a := tree.root.getChild("a") |
|
||||
|
|
||||
b := a.getChild("b") |
|
||||
if !b.isVisited() { |
|
||||
t.Errorf("expect visited /a/b") |
|
||||
} |
|
||||
c := b.getChild("c") |
|
||||
if !c.isVisited() { |
|
||||
t.Errorf("expect visited /a/b/c") |
|
||||
} |
|
||||
|
|
||||
d := c.getChild("d") |
|
||||
if d.isVisited() { |
|
||||
t.Errorf("expect unvisited /a/b/c/d") |
|
||||
} |
|
||||
|
|
||||
tree.EnsureVisited(util.FullPath("/a/b/c/d"), visitFn) |
|
||||
tree.EnsureVisited(util.FullPath("/a/b/c/d/i"), visitFn) |
|
||||
tree.EnsureVisited(util.FullPath("/a/b/c/d/j"), visitFn) |
|
||||
tree.EnsureVisited(util.FullPath("/a/b/e"), visitFn) |
|
||||
tree.EnsureVisited(util.FullPath("/a/f"), visitFn) |
|
||||
|
|
||||
printMap(tree.root.Children) |
|
||||
|
|
||||
} |
|
||||
|
|
||||
func TestEmptyBoundedTree(t *testing.T) { |
|
||||
|
|
||||
// g
|
|
||||
// h
|
|
||||
|
|
||||
tree := NewBoundedTree(util.FullPath("/")) |
|
||||
|
|
||||
visitFn := func(path util.FullPath) (childDirectories []string, err error) { |
|
||||
fmt.Printf(" visit %v ...\n", path) |
|
||||
switch path { |
|
||||
case "/": |
|
||||
return []string{"g", "h"}, nil |
|
||||
} |
|
||||
t.Fatalf("expected visit %s", path) |
|
||||
return nil, nil |
|
||||
} |
|
||||
|
|
||||
tree.EnsureVisited(util.FullPath("/a/b"), visitFn) |
|
||||
|
|
||||
tree.EnsureVisited(util.FullPath("/a/b"), visitFn) |
|
||||
|
|
||||
printMap(tree.root.Children) |
|
||||
|
|
||||
assert.Equal(t, true, tree.HasVisited(util.FullPath("/a/b"))) |
|
||||
assert.Equal(t, true, tree.HasVisited(util.FullPath("/a"))) |
|
||||
assert.Equal(t, false, tree.HasVisited(util.FullPath("/g"))) |
|
||||
assert.Equal(t, false, tree.HasVisited(util.FullPath("/g/x"))) |
|
||||
|
|
||||
} |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue