From 5a09669d3a4815274f5f2079f9df106fb882851e Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Mon, 9 Jan 2023 17:30:19 +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 | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go index 4992a1b41..8b3f42be5 100644 --- a/kernel/model/transaction.go +++ b/kernel/model/transaction.go @@ -1238,6 +1238,29 @@ func autoFixIndex() { autoFixLock.Lock() defer autoFixLock.Unlock() + // 根据文件系统补全块树 + boxes := Conf.GetOpenedBoxes() + for _, box := range boxes { + boxPath := filepath.Join(util.DataDir, box.ID) + var paths []string + filepath.Walk(boxPath, func(path string, info os.FileInfo, err error) error { + if !info.IsDir() && filepath.Ext(path) == ".sy" { + p := path[len(boxPath):] + p = filepath.ToSlash(p) + paths = append(paths, p) + } + return nil + }) + + size := len(paths) + for i, p := range paths { + if nil == treenode.GetBlockTreeRootByPath(box.ID, p) { + reindexTreeByPath(box.ID, p, i, size) + } + } + } + + // 对比块树和数据库并订正数据库 rootUpdatedMap := treenode.GetRootUpdated() dbRootUpdatedMap, err := sql.GetRootUpdated() if nil == err { @@ -1273,6 +1296,7 @@ func autoFixIndex() { } } + // 去除重复的数据库块记录 duplicatedRootIDs := sql.GetDuplicatedRootIDs() size := len(duplicatedRootIDs) for i, rootID := range duplicatedRootIDs { @@ -1287,6 +1311,15 @@ func autoFixIndex() { } } +func reindexTreeByPath(box, p string, i, size int) { + tree, err := LoadTree(box, p) + if nil != err { + return + } + + reindexTree0(tree, i, size) +} + func reindexTree(rootID string, i, size int) { root := treenode.GetBlockTree(rootID) if nil == root { @@ -1303,6 +1336,10 @@ func reindexTree(rootID string, i, size int) { return } + reindexTree0(tree, i, size) +} + +func reindexTree0(tree *parse.Tree, i, size int) { updated := tree.Root.IALAttr("updated") if "" == updated { updated = util.TimeFromID(tree.Root.ID)