blocktree_fix.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package treenode
  17. import (
  18. "github.com/88250/gulu"
  19. "time"
  20. )
  21. func ClearRedundantBlockTrees(boxID string, paths []string) {
  22. redundantPaths := getRedundantPaths(boxID, paths)
  23. for _, p := range redundantPaths {
  24. removeBlockTreesByPath(boxID, p)
  25. }
  26. }
  27. func getRedundantPaths(boxID string, paths []string) (ret []string) {
  28. pathsMap := map[string]bool{}
  29. for _, path := range paths {
  30. pathsMap[path] = true
  31. }
  32. btPathsMap := map[string]bool{}
  33. blockTrees.Range(func(key, value interface{}) bool {
  34. slice := value.(*btSlice)
  35. slice.m.Lock()
  36. for _, b := range slice.data {
  37. if b.BoxID == boxID {
  38. btPathsMap[b.Path] = true
  39. }
  40. }
  41. slice.m.Unlock()
  42. return true
  43. })
  44. for p, _ := range btPathsMap {
  45. if !pathsMap[p] {
  46. ret = append(ret, p)
  47. }
  48. }
  49. ret = gulu.Str.RemoveDuplicatedElem(ret)
  50. return
  51. }
  52. func removeBlockTreesByPath(boxID, path string) {
  53. blockTrees.Range(func(key, value interface{}) bool {
  54. slice := value.(*btSlice)
  55. slice.m.Lock()
  56. for _, b := range slice.data {
  57. if b.Path == path && b.BoxID == boxID {
  58. delete(slice.data, b.ID)
  59. slice.changed = time.Now()
  60. }
  61. }
  62. slice.m.Unlock()
  63. return true
  64. })
  65. }
  66. func GetNotExistPaths(boxID string, paths []string) (ret []string) {
  67. pathsMap := map[string]bool{}
  68. for _, path := range paths {
  69. pathsMap[path] = true
  70. }
  71. btPathsMap := map[string]bool{}
  72. blockTrees.Range(func(key, value interface{}) bool {
  73. slice := value.(*btSlice)
  74. slice.m.Lock()
  75. for _, b := range slice.data {
  76. if b.BoxID == boxID {
  77. btPathsMap[b.Path] = true
  78. }
  79. }
  80. slice.m.Unlock()
  81. return true
  82. })
  83. for p, _ := range pathsMap {
  84. if !btPathsMap[p] {
  85. ret = append(ret, p)
  86. }
  87. }
  88. ret = gulu.Str.RemoveDuplicatedElem(ret)
  89. return
  90. }
  91. func GetRootUpdated() (ret map[string]string) {
  92. ret = map[string]string{}
  93. blockTrees.Range(func(key, value interface{}) bool {
  94. slice := value.(*btSlice)
  95. slice.m.Lock()
  96. for _, b := range slice.data {
  97. if b.RootID == b.ID {
  98. ret[b.RootID] = b.Updated
  99. }
  100. }
  101. slice.m.Unlock()
  102. return true
  103. })
  104. return
  105. }