You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
3.6 KiB

  1. package bounded_tree
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/util"
  6. )
  7. type Node struct {
  8. Parent *Node
  9. Name string
  10. Children map[string]*Node
  11. }
  12. type BoundedTree struct {
  13. root *Node
  14. }
  15. func NewBoundedTree() *BoundedTree {
  16. return &BoundedTree{
  17. root: &Node{
  18. Name: "/",
  19. },
  20. }
  21. }
  22. type VisitNodeFunc func(path util.FullPath) (childDirectories []string, err error)
  23. // If the path is not visited, call the visitFn for each level of directory
  24. // No action if the directory has been visited before or does not exist.
  25. // A leaf node, which has no children, represents a directory not visited.
  26. // A non-leaf node or a non-existing node represents a directory already visited, or does not need to visit.
  27. func (t *BoundedTree) EnsureVisited(p util.FullPath, visitFn VisitNodeFunc) {
  28. // println()
  29. // println("EnsureVisited", p)
  30. if t.root == nil {
  31. return
  32. }
  33. components := p.Split()
  34. // fmt.Printf("components %v %d\n", components, len(components))
  35. if canDelete := t.ensureVisited(t.root, util.FullPath("/"), components, 0, visitFn); canDelete {
  36. t.root = nil
  37. }
  38. }
  39. func (t *BoundedTree) ensureVisited(n *Node, currentPath util.FullPath, components []string, i int, visitFn VisitNodeFunc) (canDeleteNode bool) {
  40. // println("ensureVisited", currentPath, i)
  41. if n == nil {
  42. // fmt.Printf("%s null\n", currentPath)
  43. return
  44. }
  45. if n.isVisited() {
  46. // fmt.Printf("%s visited %v\n", currentPath, n.Name)
  47. } else {
  48. // fmt.Printf("ensure %v\n", currentPath)
  49. children, err := visitFn(currentPath)
  50. if err != nil {
  51. glog.V(0).Infof("failed to visit %s: %v", currentPath, err)
  52. return
  53. }
  54. if len(children) == 0 {
  55. // fmt.Printf(" canDelete %v without children\n", currentPath)
  56. return true
  57. }
  58. n.Children = make(map[string]*Node)
  59. for _, child := range children {
  60. // fmt.Printf(" add child %v %v\n", currentPath, child)
  61. n.Children[child] = &Node{
  62. Name: child,
  63. }
  64. }
  65. }
  66. if i >= len(components) {
  67. return
  68. }
  69. // fmt.Printf(" check child %v %v\n", currentPath, components[i])
  70. toVisitNode, found := n.Children[components[i]]
  71. if !found {
  72. // fmt.Printf(" did not find child %v %v\n", currentPath, components[i])
  73. return
  74. }
  75. // fmt.Printf(" ensureVisited %v %v\n", currentPath, toVisitNode.Name)
  76. if canDelete := t.ensureVisited(toVisitNode, currentPath.Child(components[i]), components, i+1, visitFn); canDelete {
  77. // fmt.Printf(" delete %v %v\n", currentPath, components[i])
  78. delete(n.Children, components[i])
  79. if len(n.Children) == 0 {
  80. // fmt.Printf(" canDelete %v\n", currentPath)
  81. return true
  82. }
  83. }
  84. return false
  85. }
  86. func (n *Node) isVisited() bool {
  87. if n == nil {
  88. return true
  89. }
  90. if len(n.Children) > 0 {
  91. return true
  92. }
  93. return false
  94. }
  95. func (n *Node) getChild(childName string) *Node {
  96. if n == nil {
  97. return nil
  98. }
  99. if len(n.Children) > 0 {
  100. return n.Children[childName]
  101. }
  102. return nil
  103. }
  104. func (t *BoundedTree) HasVisited(p util.FullPath) bool {
  105. if t.root == nil {
  106. return true
  107. }
  108. components := p.Split()
  109. fmt.Printf("components %v %d\n", components, len(components))
  110. return t.hasVisited(t.root, util.FullPath("/"), components, 0)
  111. }
  112. func (t *BoundedTree) hasVisited(n *Node, currentPath util.FullPath, components []string, i int) bool {
  113. if n == nil {
  114. return true
  115. }
  116. if !n.isVisited() {
  117. return false
  118. }
  119. fmt.Printf(" hasVisited child %v %+v %d\n", currentPath, components, i)
  120. if i >= len(components) {
  121. return true
  122. }
  123. toVisitNode, found := n.Children[components[i]]
  124. if !found {
  125. return true
  126. }
  127. return t.hasVisited(toVisitNode, currentPath.Child(components[i]), components, i+1)
  128. }