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.

166 lines
3.6 KiB

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