Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vanessa 2023-01-19 11:49:46 +08:00
commit f6dd7722ad
8 changed files with 91 additions and 11 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

@ -57,7 +57,7 @@ func autoOCRAssets() {
waitGroup.Add(1)
p.Invoke(assetAbsPath)
if 63 <= i { // 一次最多处理 64 张图片,防止卡顿
if 63 <= i { // 一次任务中最多处理 64 张图片,防止卡顿
break
}
}

View file

@ -58,6 +58,11 @@ func searchEmbedBlock(embedBlockID, stmt string, excludeIDs []string, headingMod
sqlBlocks := sql.SelectBlocksRawStmtNoParse(stmt, Conf.Search.Limit)
var tmp []*sql.Block
for _, b := range sqlBlocks {
if "query_embed" == b.Type { // 嵌入块不再嵌入
// 嵌入块支持搜索 https://github.com/siyuan-note/siyuan/issues/7112
// 这里会导致上面的 limit 限制不准确,导致结果变少,暂时没有解决方案,只能靠用户自己调整 SQL加上 type != 'query_embed' 的条件
continue
}
if !gulu.Str.Contains(b.ID, excludeIDs) {
tmp = append(tmp, b)
}
@ -494,6 +499,7 @@ func buildTypeFilter(types map[string]bool) string {
s.SuperBlock = types["superBlock"]
s.Paragraph = types["paragraph"]
s.HTMLBlock = types["htmlBlock"]
s.EmbedBlock = types["embedBlock"]
} else {
s.Document = Conf.Search.Document
s.Heading = Conf.Search.Heading
@ -506,6 +512,7 @@ func buildTypeFilter(types map[string]bool) string {
s.SuperBlock = Conf.Search.SuperBlock
s.Paragraph = Conf.Search.Paragraph
s.HTMLBlock = Conf.Search.HTMLBlock
s.EmbedBlock = Conf.Search.EmbedBlock
}
return s.TypeFilter()
}

View file

@ -1216,6 +1216,31 @@ 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.QueryEmptyContentEmbedBlocks()
for i, 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)
}
if 63 <= i { // 一次任务中最多处理 64 个嵌入块,防止卡顿
break
}
}
}
// AutoFixIndex 自动校验数据库索引 https://github.com/siyuan-note/siyuan/issues/7016
func AutoFixIndex() {
for {
@ -1229,13 +1254,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 QueryEmptyContentEmbedBlocks() (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)