This commit is contained in:
Liang Ding 2023-01-19 01:12:31 +08:00
parent 3a93650d9d
commit 2cb6e82330
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
6 changed files with 79 additions and 10 deletions

View file

@ -36,6 +36,7 @@ type Search struct {
SuperBlock bool `json:"superBlock"`
Paragraph bool `json:"paragraph"`
HTMLBlock bool `json:"htmlBlock"`
EmbedBlock bool `json:"embedBlock"`
Limit int `json:"limit"`
CaseSensitive bool `json:"caseSensitive"`
@ -71,6 +72,7 @@ func NewSearch() *Search {
SuperBlock: true,
Paragraph: true,
HTMLBlock: true,
EmbedBlock: false,
Limit: 64,
CaseSensitive: true,
@ -180,6 +182,12 @@ func (s *Search) TypeFilter() string {
buf.WriteByte('\'')
buf.WriteString(",")
}
if s.EmbedBlock {
buf.WriteByte('\'')
buf.WriteString(treenode.TypeAbbr(ast.NodeBlockQueryEmbed.String()))
buf.WriteByte('\'')
buf.WriteString(",")
}
// 无法搜索到 iframe 块、视频块和音频块 https://github.com/siyuan-note/siyuan/issues/3604
buf.WriteString("'iframe','query_embed','video','audio',")
// 挂件块支持内置属性搜索 https://github.com/siyuan-note/siyuan/issues/4497

View file

@ -56,6 +56,7 @@ func main() {
go treenode.AutoFlushBlockTree()
go cache.LoadAssets()
go model.AutoFixIndex()
go model.AutoIndexEmbedBlock()
go model.AutoOCRAssets()
go model.AutoFlushAssetsTexts()
go model.HookDesktopUIProc()

View file

@ -70,6 +70,7 @@ func StartKernel(container, appDir, workspaceBaseDir, timezoneID, localIPs, lang
go treenode.AutoFlushBlockTree()
go cache.LoadAssets()
go model.AutoFixIndex()
go model.AutoIndexEmbedBlock()
go model.AutoOCRAssets()
go model.AutoFlushAssetsTexts()
}()

View file

@ -1216,6 +1216,27 @@ func updateRefText(refNode *ast.Node, changedDefNodes map[string]*ast.Node) (cha
return
}
// AutoIndexEmbedBlock 嵌入块支持搜索 https://github.com/siyuan-note/siyuan/issues/7112
func AutoIndexEmbedBlock() {
for {
task.AppendTask(task.DatabaseIndexEmbedBlock, autoIndexEmbedBlock)
time.Sleep(30 * time.Second)
}
}
func autoIndexEmbedBlock() {
embedBlocks := sql.QueryEmbedBlocks()
for _, embedBlock := range embedBlocks {
stmt := strings.TrimPrefix(embedBlock.Markdown, "{{")
stmt = strings.TrimSuffix(stmt, "}}")
blocks := sql.SelectBlocksRawStmtNoParse(stmt, 102400)
for _, block := range blocks {
embedBlock.Content = block.Content
sql.UpdateBlockContent(embedBlock)
}
}
}
// AutoFixIndex 自动校验数据库索引 https://github.com/siyuan-note/siyuan/issues/7016
func AutoFixIndex() {
for {
@ -1229,13 +1250,6 @@ var autoFixLock = sync.Mutex{}
func autoFixIndex() {
defer logging.Recover()
if util.IsMutexLocked(&autoFixLock) {
return
}
autoFixLock.Lock()
defer autoFixLock.Unlock()
// 根据文件系统补全块树
boxes := Conf.GetOpenedBoxes()
for _, box := range boxes {

View file

@ -55,9 +55,11 @@ func updateRootContent(tx *sql.Tx, content, updated, id string) {
if err := execStmtTx(tx, stmt, content, content, updated, id); nil != err {
return
}
stmt = "UPDATE blocks_fts_case_insensitive SET content = ?, fcontent = ?, updated = ? WHERE id = ?"
if err := execStmtTx(tx, stmt, content, content, updated, id); nil != err {
return
if !caseSensitive {
stmt = "UPDATE blocks_fts_case_insensitive SET content = ?, fcontent = ?, updated = ? WHERE id = ?"
if err := execStmtTx(tx, stmt, content, content, updated, id); nil != err {
return
}
}
removeBlockCache(id)
cache.RemoveBlockIAL(id)
@ -66,3 +68,30 @@ func updateRootContent(tx *sql.Tx, content, updated, id string) {
func InsertBlock(tx *sql.Tx, block *Block, context map[string]interface{}) (err error) {
return insertBlocks(tx, []*Block{block}, context)
}
func UpdateBlockContent(block *Block) {
tx, err := BeginTx()
if nil != err {
return
}
stmt := "UPDATE blocks SET content = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, block.Content, block.ID); nil != err {
tx.Rollback()
return
}
stmt = "UPDATE blocks_fts SET content = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, block.Content, block.ID); nil != err {
tx.Rollback()
return
}
if !caseSensitive {
stmt = "UPDATE blocks_fts_case_insensitive SET content = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, block.Content, block.ID); nil != err {
tx.Rollback()
return
}
}
tx.Commit()
putBlockCache(block)
}

View file

@ -31,6 +31,22 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
func QueryEmbedBlocks() (ret []*Block) {
stmt := "SELECT * FROM blocks WHERE type = 'query_embed' AND content = ''"
rows, err := query(stmt)
if nil != err {
logging.LogErrorf("sql query [%s] failed: %s", stmt, err)
return
}
defer rows.Close()
for rows.Next() {
if block := scanBlockRows(rows); nil != block {
ret = append(ret, block)
}
}
return
}
func queryBlockHashes(rootID string) (ret map[string]string) {
stmt := "SELECT id, hash FROM blocks WHERE root_id = ?"
rows, err := query(stmt, rootID)