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 Dingdc112fd56c29
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 6e5e64278f..36608549ca 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -1238,42 +1238,43 @@ func autoFixIndex() {
defer autoFixLock.Unlock()
rootUpdatedMap := treenode.GetRootUpdated()
- i := -1
- size := len(rootUpdatedMap)
- for rootID, updated := range rootUpdatedMap {
- if isFullReindexing {
- break
- }
+ dbRootUpdatedMap, err := sql.GetRootUpdated()
+ if nil == err {
+ i := -1
+ size := len(rootUpdatedMap)
+ for rootID, updated := range rootUpdatedMap {
+ if isFullReindexing {
+ break
+ }
- i++
+ i++
- rootUpdated, err := sql.GetRootUpdated(rootID)
- if nil != err {
- continue
- }
- if "" == rootUpdated {
- logging.LogWarnf("not found tree [%s] in database, reindex it", rootID)
- reindexTree(rootID, i, size)
- continue
- }
+ rootUpdated := dbRootUpdatedMap[rootID]
+ if "" == rootUpdated {
+ logging.LogWarnf("not found tree [%s] in database, reindex it", rootID)
+ reindexTree(rootID, i, size)
+ continue
+ }
- if "" == updated {
- // BlockTree 迁移,v2.6.3 之前没有 updated 字段
- reindexTree(rootID, i, size)
- continue
- }
+ if "" == updated {
+ // BlockTree 迁移,v2.6.3 之前没有 updated 字段
+ reindexTree(rootID, i, size)
+ continue
+ }
- btUpdated, _ := time.Parse("20060102150405", updated)
- dbUpdated, _ := time.Parse("20060102150405", rootUpdated)
- if dbUpdated.Before(btUpdated.Add(-1 * time.Minute)) {
- logging.LogWarnf("tree [%s] is not up to date, reindex it", rootID)
- reindexTree(rootID, i, size)
- continue
+ btUpdated, _ := time.Parse("20060102150405", updated)
+ dbUpdated, _ := time.Parse("20060102150405", rootUpdated)
+ if dbUpdated.Before(btUpdated.Add(-1 * time.Minute)) {
+ logging.LogWarnf("tree [%s] is not up to date, reindex it", rootID)
+ reindexTree(rootID, i, size)
+ continue
+ }
}
}
duplicatedRootIDs := sql.GetDuplicatedRootIDs()
- for _, rootID := range duplicatedRootIDs {
+ size := len(duplicatedRootIDs)
+ for i, rootID := range duplicatedRootIDs {
root := sql.GetBlock(rootID)
if nil == root {
continue
diff --git a/kernel/sql/block_query.go b/kernel/sql/block_query.go
index 693bdd8c1e..71b5bd8ac9 100644
--- a/kernel/sql/block_query.go
+++ b/kernel/sql/block_query.go
@@ -581,15 +581,19 @@ func GetBlock(id string) (ret *Block) {
return
}
-func GetRootUpdated(rootID string) (ret string, err error) {
- rows, err := query("SELECT updated FROM blocks WHERE root_id = ? AND type = 'd'", rootID)
+func GetRootUpdated() (ret map[string]string, err error) {
+ rows, err := query("SELECT root_id, updated FROM blocks WHERE type = 'd'")
if nil != err {
logging.LogErrorf("sql query failed: %s", err)
return
}
defer rows.Close()
+
+ ret = map[string]string{}
for rows.Next() {
- rows.Scan(&ret)
+ var rootID, updated string
+ rows.Scan(&rootID, &updated)
+ ret[rootID] = updated
}
return
}