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.

182 lines
3.9 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. if t.baseDir != "/" {
  37. p = p[len(t.baseDir):]
  38. }
  39. components := p.Split()
  40. // fmt.Printf("components %v %d\n", components, len(components))
  41. canDelete, err := t.ensureVisited(t.root, t.baseDir, components, 0, visitFn)
  42. if err != nil {
  43. return err
  44. }
  45. if canDelete {
  46. t.root = nil
  47. }
  48. return nil
  49. }
  50. func (t *BoundedTree) ensureVisited(n *Node, currentPath util.FullPath, components []string, i int, visitFn VisitNodeFunc) (canDeleteNode bool, visitErr error) {
  51. // println("ensureVisited", currentPath, i)
  52. if n == nil {
  53. // fmt.Printf("%s null\n", currentPath)
  54. return
  55. }
  56. if n.isVisited() {
  57. // fmt.Printf("%s visited %v\n", currentPath, n.Name)
  58. } else {
  59. // fmt.Printf("ensure %v\n", currentPath)
  60. children, err := visitFn(currentPath)
  61. if err != nil {
  62. glog.V(0).Infof("failed to visit %s: %v", currentPath, err)
  63. return false, err
  64. }
  65. if len(children) == 0 {
  66. // fmt.Printf(" canDelete %v without children\n", currentPath)
  67. return true, nil
  68. }
  69. n.Children = make(map[string]*Node)
  70. for _, child := range children {
  71. // fmt.Printf(" add child %v %v\n", currentPath, child)
  72. n.Children[child] = &Node{
  73. Name: child,
  74. }
  75. }
  76. }
  77. if i >= len(components) {
  78. return
  79. }
  80. // fmt.Printf(" check child %v %v\n", currentPath, components[i])
  81. toVisitNode, found := n.Children[components[i]]
  82. if !found {
  83. // fmt.Printf(" did not find child %v %v\n", currentPath, components[i])
  84. return
  85. }
  86. // fmt.Printf(" ensureVisited %v %v\n", currentPath, toVisitNode.Name)
  87. canDelete, childVisitErr := t.ensureVisited(toVisitNode, currentPath.Child(components[i]), components, i+1, visitFn)
  88. if childVisitErr != nil {
  89. return false, childVisitErr
  90. }
  91. if canDelete {
  92. // fmt.Printf(" delete %v %v\n", currentPath, components[i])
  93. delete(n.Children, components[i])
  94. if len(n.Children) == 0 {
  95. // fmt.Printf(" canDelete %v\n", currentPath)
  96. return true, nil
  97. }
  98. }
  99. return false, nil
  100. }
  101. func (n *Node) isVisited() bool {
  102. if n == nil {
  103. return true
  104. }
  105. if len(n.Children) > 0 {
  106. return true
  107. }
  108. return false
  109. }
  110. func (n *Node) getChild(childName string) *Node {
  111. if n == nil {
  112. return nil
  113. }
  114. if len(n.Children) > 0 {
  115. return n.Children[childName]
  116. }
  117. return nil
  118. }
  119. func (t *BoundedTree) HasVisited(p util.FullPath) bool {
  120. t.RLock()
  121. defer t.RUnlock()
  122. if t.root == nil {
  123. return true
  124. }
  125. components := p.Split()
  126. // fmt.Printf("components %v %d\n", components, len(components))
  127. return t.hasVisited(t.root, util.FullPath("/"), components, 0)
  128. }
  129. func (t *BoundedTree) hasVisited(n *Node, currentPath util.FullPath, components []string, i int) bool {
  130. if n == nil {
  131. return true
  132. }
  133. if !n.isVisited() {
  134. return false
  135. }
  136. // fmt.Printf(" hasVisited child %v %+v %d\n", currentPath, components, i)
  137. if i >= len(components) {
  138. return true
  139. }
  140. toVisitNode, found := n.Children[components[i]]
  141. if !found {
  142. return true
  143. }
  144. return t.hasVisited(toVisitNode, currentPath.Child(components[i]), components, i+1)
  145. }