From 68b0f4e8b3100d1192c58b06c5c1e5da62f86104 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Mon, 9 Jan 2023 20:27:10 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E8=87=AA=E5=8A=A8=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E7=B4=A2=E5=BC=95=20https://github.?= =?UTF-8?q?com/siyuan-note/siyuan/issues/7016?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/transaction.go | 6 ++++++ kernel/treenode/blocktree.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go index 60866ef1c..f30a69786 100644 --- a/kernel/model/transaction.go +++ b/kernel/model/transaction.go @@ -1255,6 +1255,12 @@ func autoFixIndex() { }) size := len(paths) + + redundantPaths := treenode.GetRedundantPaths(box.ID, paths) + for _, p := range redundantPaths { + treenode.RemoveBlockTreesByPathPrefix(p) + } + missingPaths := treenode.GetNotExistPaths(box.ID, paths) for i, p := range missingPaths { reindexTreeByPath(box.ID, p, i, size) diff --git a/kernel/treenode/blocktree.go b/kernel/treenode/blocktree.go index d1a26f2f5..c1afce2ac 100644 --- a/kernel/treenode/blocktree.go +++ b/kernel/treenode/blocktree.go @@ -116,6 +116,31 @@ func CeilBlockCount(count int) int { return 10000*100 + 1 } +func GetRedundantPaths(boxID string, paths []string) (ret []string) { + pathsMap := map[string]bool{} + for _, path := range paths { + pathsMap[path] = true + } + + tmp := blockTrees + btPathsMap := map[string]bool{} + for _, blockTree := range tmp { + if blockTree.BoxID != boxID { + continue + } + + btPathsMap[blockTree.Path] = true + } + + for p, _ := range btPathsMap { + if !pathsMap[p] { + ret = append(ret, p) + } + } + ret = gulu.Str.RemoveDuplicatedElem(ret) + return +} + func GetNotExistPaths(boxID string, paths []string) (ret []string) { pathsMap := map[string]bool{} for _, path := range paths { @@ -132,6 +157,7 @@ func GetNotExistPaths(boxID string, paths []string) (ret []string) { ret = append(ret, blockTree.Path) } } + ret = gulu.Str.RemoveDuplicatedElem(ret) return } @@ -191,6 +217,7 @@ func RemoveBlockTreesByRootID(rootID string) { ids = append(ids, b.RootID) } } + ids = gulu.Str.RemoveDuplicatedElem(ids) for _, id := range ids { delete(blockTrees, id) } @@ -207,6 +234,7 @@ func RemoveBlockTreesByPathPrefix(pathPrefix string) { ids = append(ids, b.ID) } } + ids = gulu.Str.RemoveDuplicatedElem(ids) for _, id := range ids { delete(blockTrees, id) } @@ -222,6 +250,7 @@ func RemoveBlockTreesByBoxID(boxID string) (ids []string) { ids = append(ids, b.ID) } } + ids = gulu.Str.RemoveDuplicatedElem(ids) for _, id := range ids { delete(blockTrees, id) } @@ -247,6 +276,7 @@ func ReindexBlockTree(tree *parse.Tree) { ids = append(ids, b.ID) } } + ids = gulu.Str.RemoveDuplicatedElem(ids) for _, id := range ids { delete(blockTrees, id) }