🎨 OCR no longer blocks document loading https://github.com/siyuan-note/siyuan/issues/9230

This commit is contained in:
Daniel 2023-12-07 20:40:16 +08:00
parent b31765d0ab
commit 283917a9a8
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
8 changed files with 123 additions and 11 deletions

View file

@ -20,6 +20,9 @@ import (
"database/sql"
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
type Block struct {
@ -88,3 +91,41 @@ func updateBlockContent(tx *sql.Tx, block *Block) (err error) {
putBlockCache(block)
return
}
func indexNode(tx *sql.Tx, id string) (err error) {
bt := treenode.GetBlockTree(id)
if nil == bt {
return
}
luteEngine := util.NewLute()
tree, _ := filesys.LoadTree(bt.BoxID, bt.Path, luteEngine)
if nil == tree {
return
}
node := treenode.GetNodeInTree(tree, id)
if nil == node {
return
}
content := treenode.NodeStaticContent(node, nil, true, indexAssetPath)
stmt := "UPDATE blocks SET content = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, content, id); nil != err {
tx.Rollback()
return
}
stmt = "UPDATE blocks_fts SET content = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, content, id); nil != err {
tx.Rollback()
return
}
if !caseSensitive {
stmt = "UPDATE blocks_fts_case_insensitive SET content = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, content, id); nil != err {
tx.Rollback()
return
}
}
return
}

View file

@ -798,9 +798,18 @@ func buildBlockFromNode(n *ast.Node, tree *parse.Tree) (block *Block, attributes
length = utf8.RuneCountInString(fcontent)
} else if n.IsContainerBlock() {
markdown = treenode.ExportNodeStdMd(n, luteEngine)
if !treenode.IsNodeOCRed(n) {
IndexNodeQueue(n.ID)
}
content = treenode.NodeStaticContent(n, nil, true, indexAssetPath)
fc := treenode.FirstLeafBlock(n)
if !treenode.IsNodeOCRed(fc) {
IndexNodeQueue(fc.ID)
}
fcontent = treenode.NodeStaticContent(fc, nil, true, false)
parentID = n.Parent.ID
// 将标题块作为父节点
if h := heading(n); nil != h {
@ -809,7 +818,13 @@ func buildBlockFromNode(n *ast.Node, tree *parse.Tree) (block *Block, attributes
length = utf8.RuneCountInString(fcontent)
} else {
markdown = treenode.ExportNodeStdMd(n, luteEngine)
if !treenode.IsNodeOCRed(n) {
IndexNodeQueue(n.ID)
}
content = treenode.NodeStaticContent(n, nil, true, indexAssetPath)
parentID = n.Parent.ID
// 将标题块作为父节点
if h := heading(n); nil != h {

View file

@ -51,6 +51,7 @@ type dbQueueOperation struct {
box string // delete_box/delete_box_refs/index
renameTree *parse.Tree // rename/rename_sub_tree
block *Block // update_block_content
id string // index_node
removeAssetHashes []string // delete_assets
}
@ -191,6 +192,8 @@ func execOp(op *dbQueueOperation, tx *sql.Tx, context map[string]interface{}) (e
err = updateBlockContent(tx, op.block)
case "delete_assets":
err = deleteAssetsByHashes(tx, op.removeAssetHashes)
case "index_node":
err = indexNode(tx, op.id)
default:
msg := fmt.Sprintf("unknown operation [%s]", op.action)
logging.LogErrorf(msg)
@ -199,6 +202,20 @@ func execOp(op *dbQueueOperation, tx *sql.Tx, context map[string]interface{}) (e
return
}
func IndexNodeQueue(id string) {
dbQueueLock.Lock()
defer dbQueueLock.Unlock()
newOp := &dbQueueOperation{id: id, inQueueTime: time.Now(), action: "index_node"}
for i, op := range operationQueue {
if "index_node" == op.action && op.id == id {
operationQueue[i] = newOp
return
}
}
operationQueue = append(operationQueue, newOp)
}
func BatchRemoveAssetsQueue(hashes []string) {
if 1 > len(hashes) {
return