Skip to content

Commit

Permalink
🎨 重建历史索引遮罩 #7386
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Feb 17, 2023
1 parent 9167439 commit fa1ee8f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/appearance/langs/zh_CHT.json
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,6 @@
"188": "鎖定雲端同步目錄失敗,請稍後再試",
"189": "雲端同步目錄還在被其他設備鎖定,請稍後再試",
"190": "校驗索引時發現一個問題,已經自動修復",
"191": "[%d/%d] 已经建立条历史数据索引"
"191": "[%d/%d] 已經建立條歷史數據索引"
}
}
11 changes: 9 additions & 2 deletions kernel/model/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package model
import (
"encoding/json"
"fmt"
"github.com/siyuan-note/eventbus"
"io/fs"
"math"
"os"
Expand Down Expand Up @@ -575,13 +576,15 @@ func ReindexHistory() (err error) {

sql.InitHistoryDatabase(true)
lutEngine := util.NewLute()

context := map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBarAndProgress}
for _, historyDir := range historyDirs {
if !historyDir.IsDir() {
continue
}

name := historyDir.Name()
indexHistoryDir(name, lutEngine)
indexHistoryDirWithContext(name, lutEngine, context)
}

sql.WaitForWritingHistoryDatabase()
Expand All @@ -597,6 +600,10 @@ const (
)

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()

op := name[strings.LastIndex(name, "-")+1:]
Expand Down Expand Up @@ -660,7 +667,7 @@ func indexHistoryDir(name string, luteEngine *lute.Lute) {
})
}

sql.IndexHistoriesQueue(histories)
sql.IndexHistoriesQueue(histories, context)
return
}

Expand Down
26 changes: 14 additions & 12 deletions kernel/sql/queue_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"database/sql"
"errors"
"fmt"
"github.com/siyuan-note/eventbus"
"runtime/debug"
"sync"
"time"

"github.com/siyuan-note/eventbus"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/task"
"github.com/siyuan-note/siyuan/kernel/util"
Expand All @@ -39,7 +39,8 @@ var (

type historyDBQueueOperation struct {
inQueueTime time.Time
action string // index/deletePathPrefix
action string // index/deletePathPrefix
context map[string]interface{} // 消息推送上下文

histories []*History // index
pathPrefix string // deletePathPrefix
Expand All @@ -59,7 +60,6 @@ func FlushHistoryQueue() {
defer txLock.Unlock()
start := time.Now()

context := map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBarAndProgress}
total := len(ops)
for i, op := range ops {
if util.IsExiting {
Expand All @@ -71,9 +71,9 @@ func FlushHistoryQueue() {
return
}

context["current"] = i
context["total"] = total
if err = execHistoryOp(op, tx, context); nil != err {
op.context["current"] = i
op.context["total"] = total
if err = execHistoryOp(op, tx); nil != err {
tx.Rollback()
logging.LogErrorf("queue operation failed: %s", err)
continue
Expand All @@ -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 {
case "index":
err = insertHistories(tx, op.histories, context)
err = insertHistories(tx, op.histories, op.context)
case "deletePathPrefix":
err = deleteHistoriesByPathPrefix(tx, op.pathPrefix, context)
err = deleteHistoriesByPathPrefix(tx, op.pathPrefix, op.context)
default:
msg := fmt.Sprintf("unknown history operation [%s]", op.action)
logging.LogErrorf(msg)
Expand All @@ -117,15 +117,17 @@ func DeleteHistoriesByPathPrefixQueue(pathPrefix string) {
historyDBQueueLock.Lock()
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)
}

func IndexHistoriesQueue(histories []*History) {
func IndexHistoriesQueue(histories []*History, context map[string]interface{}) {
historyDBQueueLock.Lock()
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)
}

Expand Down

0 comments on commit fa1ee8f

Please sign in to comment.