This commit is contained in:
Liang Ding 2023-02-17 22:13:48 +08:00
parent 91674399d8
commit fa1ee8f4a1
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
3 changed files with 24 additions and 15 deletions

View file

@ -1091,6 +1091,6 @@
"188": "鎖定雲端同步目錄失敗,請稍後再試", "188": "鎖定雲端同步目錄失敗,請稍後再試",
"189": "雲端同步目錄還在被其他設備鎖定,請稍後再試", "189": "雲端同步目錄還在被其他設備鎖定,請稍後再試",
"190": "校驗索引時發現一個問題,已經自動修復", "190": "校驗索引時發現一個問題,已經自動修復",
"191": "[%d/%d] 已经建立条历史数据索引" "191": "[%d/%d] 已經建立條歷史數據索引"
} }
} }

View file

@ -19,6 +19,7 @@ package model
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/siyuan-note/eventbus"
"io/fs" "io/fs"
"math" "math"
"os" "os"
@ -575,13 +576,15 @@ func ReindexHistory() (err error) {
sql.InitHistoryDatabase(true) sql.InitHistoryDatabase(true)
lutEngine := util.NewLute() lutEngine := util.NewLute()
context := map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBarAndProgress}
for _, historyDir := range historyDirs { for _, historyDir := range historyDirs {
if !historyDir.IsDir() { if !historyDir.IsDir() {
continue continue
} }
name := historyDir.Name() name := historyDir.Name()
indexHistoryDir(name, lutEngine) indexHistoryDirWithContext(name, lutEngine, context)
} }
sql.WaitForWritingHistoryDatabase() sql.WaitForWritingHistoryDatabase()
@ -597,6 +600,10 @@ const (
) )
func indexHistoryDir(name string, luteEngine *lute.Lute) { func indexHistoryDir(name string, luteEngine *lute.Lute) {
indexHistoryDirWithContext(name, luteEngine, map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBar})
}
func indexHistoryDirWithContext(name string, luteEngine *lute.Lute, context map[string]interface{}) {
defer logging.Recover() defer logging.Recover()
op := name[strings.LastIndex(name, "-")+1:] op := name[strings.LastIndex(name, "-")+1:]
@ -660,7 +667,7 @@ func indexHistoryDir(name string, luteEngine *lute.Lute) {
}) })
} }
sql.IndexHistoriesQueue(histories) sql.IndexHistoriesQueue(histories, context)
return return
} }

View file

@ -20,11 +20,11 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"github.com/siyuan-note/eventbus"
"runtime/debug" "runtime/debug"
"sync" "sync"
"time" "time"
"github.com/siyuan-note/eventbus"
"github.com/siyuan-note/logging" "github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/task" "github.com/siyuan-note/siyuan/kernel/task"
"github.com/siyuan-note/siyuan/kernel/util" "github.com/siyuan-note/siyuan/kernel/util"
@ -40,6 +40,7 @@ var (
type historyDBQueueOperation struct { type historyDBQueueOperation struct {
inQueueTime time.Time inQueueTime time.Time
action string // index/deletePathPrefix action string // index/deletePathPrefix
context map[string]interface{} // 消息推送上下文
histories []*History // index histories []*History // index
pathPrefix string // deletePathPrefix pathPrefix string // deletePathPrefix
@ -59,7 +60,6 @@ func FlushHistoryQueue() {
defer txLock.Unlock() defer txLock.Unlock()
start := time.Now() start := time.Now()
context := map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBarAndProgress}
total := len(ops) total := len(ops)
for i, op := range ops { for i, op := range ops {
if util.IsExiting { if util.IsExiting {
@ -71,9 +71,9 @@ func FlushHistoryQueue() {
return return
} }
context["current"] = i op.context["current"] = i
context["total"] = total op.context["total"] = total
if err = execHistoryOp(op, tx, context); nil != err { if err = execHistoryOp(op, tx); nil != err {
tx.Rollback() tx.Rollback()
logging.LogErrorf("queue operation failed: %s", err) logging.LogErrorf("queue operation failed: %s", err)
continue continue
@ -99,12 +99,12 @@ func FlushHistoryQueue() {
} }
} }
func execHistoryOp(op *historyDBQueueOperation, tx *sql.Tx, context map[string]interface{}) (err error) { func execHistoryOp(op *historyDBQueueOperation, tx *sql.Tx) (err error) {
switch op.action { switch op.action {
case "index": case "index":
err = insertHistories(tx, op.histories, context) err = insertHistories(tx, op.histories, op.context)
case "deletePathPrefix": case "deletePathPrefix":
err = deleteHistoriesByPathPrefix(tx, op.pathPrefix, context) err = deleteHistoriesByPathPrefix(tx, op.pathPrefix, op.context)
default: default:
msg := fmt.Sprintf("unknown history operation [%s]", op.action) msg := fmt.Sprintf("unknown history operation [%s]", op.action)
logging.LogErrorf(msg) logging.LogErrorf(msg)
@ -117,15 +117,17 @@ func DeleteHistoriesByPathPrefixQueue(pathPrefix string) {
historyDBQueueLock.Lock() historyDBQueueLock.Lock()
defer historyDBQueueLock.Unlock() defer historyDBQueueLock.Unlock()
newOp := &historyDBQueueOperation{inQueueTime: time.Now(), action: "deletePathPrefix", pathPrefix: pathPrefix} newOp := &historyDBQueueOperation{inQueueTime: time.Now(), action: "deletePathPrefix", pathPrefix: pathPrefix,
context: map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBar}}
historyOperationQueue = append(historyOperationQueue, newOp) historyOperationQueue = append(historyOperationQueue, newOp)
} }
func IndexHistoriesQueue(histories []*History) { func IndexHistoriesQueue(histories []*History, context map[string]interface{}) {
historyDBQueueLock.Lock() historyDBQueueLock.Lock()
defer historyDBQueueLock.Unlock() defer historyDBQueueLock.Unlock()
newOp := &historyDBQueueOperation{inQueueTime: time.Now(), action: "index", histories: histories} newOp := &historyDBQueueOperation{inQueueTime: time.Now(), action: "index", histories: histories,
context: context}
historyOperationQueue = append(historyOperationQueue, newOp) historyOperationQueue = append(historyOperationQueue, newOp)
} }