bounded_tree.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, util.FullPath("/"), 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. }