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.

179 lines
3.8 KiB

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