Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: 自动校验数据库索引 https://github.com/siyuan-note/siyuan/issues/7016

Liang Ding5a09669d3a48
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 4992a1b412..8b3f42be52 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)