🎨 重建索引细节进度推送展现 https://github.com/siyuan-note/siyuan/issues/5890

This commit is contained in:
Liang Ding 2022-09-16 10:47:54 +08:00
parent fbe44a0aa0
commit 9a7f61f715
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
10 changed files with 103 additions and 27 deletions

View file

@ -63,6 +63,6 @@ func updateRootContent(tx *sql.Tx, content, updated, id string) {
cache.RemoveBlockIAL(id)
}
func InsertBlock(tx *sql.Tx, block *Block) (err error) {
return insertBlocks(tx, []*Block{block})
func InsertBlock(tx *sql.Tx, block *Block, context map[string]interface{}) (err error) {
return insertBlocks(tx, []*Block{block}, context)
}

46
kernel/sql/event.go Normal file
View file

@ -0,0 +1,46 @@
// SiYuan - Build Your Eternal Digital Garden
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package sql
import (
"github.com/siyuan-note/siyuan/kernel/util"
)
const (
EvtSQLInsertBlocks = "sql.insert.blocks"
EvtSQLInsertBlocksFTS = "sql.insert.blocks_fts"
)
const (
CtxPushMsg = "pushMsg"
CtxPushMsgToProgress = iota
CtxPushMsgToStatusBar
CtxPushMsgToStatusBarAndProgress
)
func contextPushMsg(context map[string]interface{}, msg string) {
switch context[CtxPushMsg].(int) {
case CtxPushMsgToProgress:
util.PushEndlessProgress(msg)
case CtxPushMsgToStatusBar:
util.PushStatusBar(msg)
case CtxPushMsgToStatusBarAndProgress:
util.PushStatusBar(msg)
util.PushEndlessProgress(msg)
}
}

View file

@ -98,12 +98,13 @@ func flushTreeQueue() {
return
}
context := map[string]interface{}{CtxPushMsg: CtxPushMsgToStatusBar}
boxes := hashset.New()
for _, op := range ops {
switch op.action {
case "upsert":
tree := op.upsertTree
if err = upsertTree(tx, tree); nil != err {
if err = upsertTree(tx, tree, context); nil != err {
logging.LogErrorf("upsert tree [%s] into database failed: %s", tree.Box+tree.Path, err)
}
boxes.Add(op.upsertTree.Box)

View file

@ -17,12 +17,15 @@
package sql
import (
"bytes"
"crypto/sha256"
"database/sql"
"fmt"
"strings"
"github.com/88250/lute/parse"
"github.com/emirpasic/gods/sets/hashset"
"github.com/siyuan-note/eventbus"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
@ -34,8 +37,8 @@ func init() {
luteEngine.RenderOptions.KramdownBlockIAL = false // 数据库 markdown 字段为标准 md但是要保留 span block ial
}
func InsertBlocksSpans(tx *sql.Tx, tree *parse.Tree) (err error) {
if err = insertBlocksSpans(tx, tree); nil != err {
func InsertBlocksSpans(tx *sql.Tx, tree *parse.Tree, context map[string]interface{}) (err error) {
if err = insertBlocksSpans(tx, tree, context); nil != err {
logging.LogErrorf("insert tree [%s] into database failed: %s", tree.Box+tree.Path, err)
}
return
@ -62,7 +65,7 @@ const (
FileAnnotationRefsPlaceholder = "(?, ?, ?, ?, ?, ?, ?, ?, ?)"
)
func insertBlocks(tx *sql.Tx, blocks []*Block) (err error) {
func insertBlocks(tx *sql.Tx, blocks []*Block, context map[string]interface{}) (err error) {
if 1 > len(blocks) {
return
}
@ -74,22 +77,23 @@ func insertBlocks(tx *sql.Tx, blocks []*Block) (err error) {
continue
}
if err = insertBlocks0(tx, bulk); nil != err {
if err = insertBlocks0(tx, bulk, context); nil != err {
return
}
bulk = []*Block{}
}
if 0 < len(bulk) {
if err = insertBlocks0(tx, bulk); nil != err {
if err = insertBlocks0(tx, bulk, context); nil != err {
return
}
}
return
}
func insertBlocks0(tx *sql.Tx, bulk []*Block) (err error) {
func insertBlocks0(tx *sql.Tx, bulk []*Block, context map[string]interface{}) (err error) {
valueStrings := make([]string, 0, len(bulk))
valueArgs := make([]interface{}, 0, len(bulk)*strings.Count(BlocksPlaceholder, "?"))
hashBuf := bytes.Buffer{}
for _, b := range bulk {
valueStrings = append(valueStrings, BlocksPlaceholder)
valueArgs = append(valueArgs, b.ID)
@ -113,13 +117,19 @@ func insertBlocks0(tx *sql.Tx, bulk []*Block) (err error) {
valueArgs = append(valueArgs, b.Sort)
valueArgs = append(valueArgs, b.Created)
valueArgs = append(valueArgs, b.Updated)
putBlockCache(b)
hashBuf.WriteString(b.Hash)
}
stmt := fmt.Sprintf(BlocksInsert, strings.Join(valueStrings, ","))
if err = prepareExecInsertTx(tx, stmt, valueArgs); nil != err {
return
}
hashBuf.WriteString("blocks")
evtHash := fmt.Sprintf("%x", sha256.Sum256(hashBuf.Bytes()))[:7]
eventbus.Publish(EvtSQLInsertBlocks, context, len(bulk), evtHash)
stmt = fmt.Sprintf(BlocksFTSInsert, strings.Join(valueStrings, ","))
if err = prepareExecInsertTx(tx, stmt, valueArgs); nil != err {
return
@ -131,6 +141,9 @@ func insertBlocks0(tx *sql.Tx, bulk []*Block) (err error) {
return
}
}
hashBuf.WriteString("fts")
evtHash = fmt.Sprintf("%x", sha256.Sum256(hashBuf.Bytes()))[:7]
eventbus.Publish(EvtSQLInsertBlocksFTS, context, len(bulk), evtHash)
return
}
@ -383,9 +396,9 @@ func insertFileAnnotationRefs0(tx *sql.Tx, bulk []*FileAnnotationRef) (err error
return
}
func insertBlocksSpans(tx *sql.Tx, tree *parse.Tree) (err error) {
func insertBlocksSpans(tx *sql.Tx, tree *parse.Tree, context map[string]interface{}) (err error) {
blocks, spans, assets, attributes := fromTree(tree.Root, tree)
if err = insertBlocks(tx, blocks); nil != err {
if err = insertBlocks(tx, blocks, context); nil != err {
return
}
if err = insertSpans(tx, spans); nil != err {
@ -411,7 +424,7 @@ func insertRef(tx *sql.Tx, tree *parse.Tree) (err error) {
return err
}
func upsertTree(tx *sql.Tx, tree *parse.Tree) (err error) {
func upsertTree(tx *sql.Tx, tree *parse.Tree, context map[string]interface{}) (err error) {
oldBlockHashes := queryBlockHashes(tree.ID)
blocks, spans, assets, attributes := fromTree(tree.Root, tree)
newBlockHashes := map[string]string{}
@ -457,7 +470,7 @@ func upsertTree(tx *sql.Tx, tree *parse.Tree) (err error) {
return
}
if err = insertBlocks(tx, blocks); nil != err {
if err = insertBlocks(tx, blocks, context); nil != err {
return
}
anchors := map[string]string{}
@ -483,7 +496,7 @@ func upsertTree(tx *sql.Tx, tree *parse.Tree) (err error) {
refBlock.Content = blockContent
}
deleteBlocksByIDs(tx, refIDs)
insertBlocks(tx, refBlocks)
insertBlocks(tx, refBlocks, context)
refs, fileAnnotationRefs := refsFromTree(tree)
if err = insertRefs(tx, refs); nil != err {