From daaa9e67fd25c5514abaac5c813fd3d7495efd04 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Tue, 7 Feb 2023 09:58:13 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=95=B0=E6=8D=AE=E8=AE=A2=E6=AD=A3?= =?UTF-8?q?=E5=88=86=E6=AD=A5=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/index_fix.go | 41 ++++++++++++++++++++++++--------------- kernel/task/queue.go | 3 +-- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/kernel/model/index_fix.go b/kernel/model/index_fix.go index a63cd8d53..65b2fc920 100644 --- a/kernel/model/index_fix.go +++ b/kernel/model/index_fix.go @@ -39,20 +39,27 @@ import ( // FixIndexJob 自动校验数据库索引 https://github.com/siyuan-note/siyuan/issues/7016 func FixIndexJob() { - task.AppendTask(task.DatabaseIndexFix, autoFixIndex) + task.AppendTask(task.DatabaseIndexFix, removeDuplicateDatabaseIndex) + sql.WaitForWritingDatabase() + task.AppendTask(task.DatabaseIndexFix, fixBlockTreeByFileSys) + sql.WaitForWritingDatabase() + task.AppendTask(task.DatabaseIndexFix, fixDatabaseIndexByBlockTree) + + sql.WaitForWritingDatabase() + util.PushStatusBar(Conf.Language(185)) + debug.FreeOSMemory() } var autoFixLock = sync.Mutex{} -func autoFixIndex() { +// removeDuplicateDatabaseIndex 删除重复的数据库索引。 +func removeDuplicateDatabaseIndex() { defer logging.Recover() autoFixLock.Lock() defer autoFixLock.Unlock() util.PushStatusBar(Conf.Language(58)) - - // 去除重复的数据库块记录 duplicatedRootIDs := sql.GetDuplicatedRootIDs("blocks") if 1 > len(duplicatedRootIDs) { duplicatedRootIDs = sql.GetDuplicatedRootIDs("blocks_fts") @@ -87,11 +94,16 @@ func autoFixIndex() { if 0 < deletes { logging.LogWarnf("exist more than one tree duplicated [%d], reindex it", deletes) } +} + +// fixBlockTreeByFileSys 通过文件系统订正块树。 +func fixBlockTreeByFileSys() { + defer logging.Recover() + + autoFixLock.Lock() + defer autoFixLock.Unlock() util.PushStatusBar(Conf.Language(58)) - sql.WaitForWritingDatabase() - util.PushStatusBar(Conf.Language(58)) - // 根据文件系统补全块树 boxes := Conf.GetOpenedBoxes() for _, box := range boxes { boxPath := filepath.Join(util.DataDir, box.ID) @@ -130,26 +142,23 @@ func autoFixIndex() { } } - util.PushStatusBar(Conf.Language(58)) - sql.WaitForWritingDatabase() - util.PushStatusBar(Conf.Language(58)) // 清理已关闭的笔记本块树 boxes = Conf.GetClosedBoxes() for _, box := range boxes { treenode.RemoveBlockTreesByBoxID(box.ID) } +} - // 对比块树和数据库并订正数据库 +// fixDatabaseIndexByBlockTree 通过块树订正数据库索引。 +func fixDatabaseIndexByBlockTree() { + defer logging.Recover() + + util.PushStatusBar(Conf.Language(58)) rootUpdatedMap := treenode.GetRootUpdated() dbRootUpdatedMap, err := sql.GetRootUpdated() if nil == err { reindexTreeByUpdated(rootUpdatedMap, dbRootUpdatedMap) } - - util.PushStatusBar(Conf.Language(58)) - sql.WaitForWritingDatabase() - util.PushStatusBar(Conf.Language(185)) - debug.FreeOSMemory() } func reindexTreeByUpdated(rootUpdatedMap, dbRootUpdatedMap map[string]string) { diff --git a/kernel/task/queue.go b/kernel/task/queue.go index 99c3324a3..d17391d05 100644 --- a/kernel/task/queue.go +++ b/kernel/task/queue.go @@ -98,7 +98,6 @@ var uniqueActions = []string{ RepoCheckout, DatabaseIndexFull, DatabaseIndexCommit, - DatabaseIndexFix, OCRImage, HistoryGenerateDoc, DatabaseIndexEmbedBlock, @@ -197,7 +196,7 @@ func execTask(task *Task) { currentTaskAction = task.Action - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute) + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() ch := make(chan bool, 1) go func() {