Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: 改进块引搜索性能 https://github.com/siyuan-note/siyuan/issues/7262

Liang Ding6f701b6295d9
diff --git a/kernel/api/block.go b/kernel/api/block.go
index 71d0a54e6c..36ff236d10 100644
--- a/kernel/api/block.go
+++ b/kernel/api/block.go
@@ -261,6 +261,7 @@ func getRefText(c *gin.Context) {
}
id := arg["id"].(string)
+ model.WaitForWritingFiles()
ret.Data = model.GetBlockRefText(id)
}
diff --git a/kernel/model/blockinfo.go b/kernel/model/blockinfo.go
index 11aa9627e1..cffbb2673b 100644
--- a/kernel/model/blockinfo.go
+++ b/kernel/model/blockinfo.go
@@ -74,8 +74,6 @@ func GetDocInfo(rootID string) (ret *BlockInfo) {
}
func GetBlockRefText(id string) string {
- WaitForWritingFiles()
-
bt := treenode.GetBlockTree(id)
if nil == bt {
return ErrBlockNotFound.Error()
@@ -93,6 +91,22 @@ func GetBlockRefText(id string) string {
return getNodeRefText(node)
}
+func getBlockRefText(id string, tree *parse.Tree) (ret string) {
+ node := treenode.GetNodeInTree(tree, id)
+ if nil == node {
+ return
+ }
+
+ sqlBlock := sql.BuildBlockFromNode(node, tree)
+ if nil == sqlBlock {
+ return
+ }
+
+ ret = getNodeRefText(node)
+ ret = maxContent(ret, Conf.Editor.BlockRefDynamicAnchorTextMaxLen)
+ return
+}
+
func getNodeRefText(node *ast.Node) string {
if ret := node.IALAttr("name"); "" != ret {
ret = strings.TrimSpace(ret)
diff --git a/kernel/model/search.go b/kernel/model/search.go
index c0c7adb93d..e72c0f30d7 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -118,16 +118,33 @@ func searchEmbedBlock(embedBlockID, stmt string, excludeIDs []string, headingMod
}
func SearchRefBlock(id, rootID, keyword string, beforeLen int) (ret []*Block, newDoc bool) {
+ cachedTrees := map[string]*parse.Tree{}
+
if "" == keyword {
// 查询为空时默认的块引排序规则按最近使用优先 https://github.com/siyuan-note/siyuan/issues/3218
refs := sql.QueryRefsRecent()
for _, ref := range refs {
- sqlBlock := sql.GetBlock(ref.DefBlockID)
- block := fromSQLBlock(sqlBlock, "", beforeLen)
- if nil == block {
+ tree := cachedTrees[ref.RootID]
+ if nil == tree {
+ tree, _ = loadTreeByBlockID(ref.RootID)
+ }
+ if nil == tree {
continue
}
- block.RefText = sql.GetRefText(block.ID)
+ cachedTrees[ref.RootID] = tree
+
+ node := treenode.GetNodeInTree(tree, ref.DefBlockID)
+ if nil == node {
+ continue
+ }
+
+ sqlBlock := sql.BuildBlockFromNode(node, tree)
+ if nil == sqlBlock {
+ return
+ }
+
+ block := fromSQLBlock(sqlBlock, "", 0)
+ block.RefText = getNodeRefText(node)
block.RefText = maxContent(block.RefText, Conf.Editor.BlockRefDynamicAnchorTextMaxLen)
ret = append(ret, block)
}
@@ -139,18 +156,24 @@ func SearchRefBlock(id, rootID, keyword string, beforeLen int) (ret []*Block, ne
ret = fullTextSearchRefBlock(keyword, beforeLen)
tmp := ret[:0]
- trees := map[string]*parse.Tree{}
for _, b := range ret {
+ tree := cachedTrees[b.RootID]
+ if nil == tree {
+ tree, _ = loadTreeByBlockID(b.RootID)
+ }
+ if nil == tree {
+ continue
+ }
+ cachedTrees[b.RootID] = tree
+ b.RefText = getBlockRefText(id, tree)
+
hitFirstChildID := false
- b.RefText = sql.GetRefText(b.ID)
- b.RefText = maxContent(b.RefText, Conf.Editor.BlockRefDynamicAnchorTextMaxLen)
if b.IsContainerBlock() {
-
// `((` 引用候选中排除当前块的父块 https://github.com/siyuan-note/siyuan/issues/4538
- tree := trees[b.RootID]
+ tree := cachedTrees[b.RootID]
if nil == tree {
tree, _ = loadTreeByBlockID(b.RootID)
- trees[b.RootID] = tree
+ cachedTrees[b.RootID] = tree
}
if nil != tree {
bNode := treenode.GetNodeInTree(tree, b.ID)