Skip to content

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

:bug: Fix block ref candidate list sorted by most recently referenced #16468

Danielce386ae2b413
diff --git a/kernel/model/search.go b/kernel/model/search.go
index 3347e37b96..df316c4e78 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -336,7 +336,7 @@ func SearchRefBlockInBox(id, rootID, keyword string, beforeLen int, isSquareBrac
}
if "" == keyword {
- // 查询为空时默认的块引排序规则按最近使用优先 https://github.com/siyuan-note/siyuan/issues/3218
+ // 查询为空时默认的块引排序规则按最近引用优先 https://github.com/siyuan-note/siyuan/issues/3218
typeFilter := Conf.Search.TypeFilter()
ignoreLines := getRefSearchIgnoreLines()
diff --git a/kernel/sql/block_ref.go b/kernel/sql/block_ref.go
index 6672648552..356e712d24 100644
--- a/kernel/sql/block_ref.go
+++ b/kernel/sql/block_ref.go
@@ -20,6 +20,7 @@ import (
"database/sql"
"github.com/88250/lute/parse"
+ "github.com/siyuan-note/logging"
)
type Ref struct {
@@ -38,13 +39,15 @@ type Ref struct {
}
func upsertRefs(tx *sql.Tx, tree *parse.Tree) (err error) {
+ // 删除前先保留旧 refs 的 id 映射,重建时命中业务键则复用,避免每次重建刷新 id 而打乱“最近引用”排序
+ refIDs := queryRefIDsByPath(tx, tree.Box, tree.Path)
if err = deleteRefsByPath(tx, tree.Box, tree.Path); err != nil {
return
}
if err = deleteFileAnnotationRefsByPath(tx, tree.Box, tree.Path); err != nil {
return
}
- err = insertRefs(tx, tree)
+ err = insertRefs(tx, tree, refIDs)
return
}
@@ -58,8 +61,8 @@ func deleteRefs(tx *sql.Tx, tree *parse.Tree) (err error) {
return
}
-func insertRefs(tx *sql.Tx, tree *parse.Tree) (err error) {
- refs, fileAnnotationRefs := refsFromTree(tree)
+func insertRefs(tx *sql.Tx, tree *parse.Tree, refIDs map[string]string) (err error) {
+ refs, fileAnnotationRefs := refsFromTree(tree, refIDs)
if err = insertBlockRefs(tx, refs); err != nil {
return
}
@@ -68,3 +71,23 @@ func insertRefs(tx *sql.Tx, tree *parse.Tree) (err error) {
}
return err
}
+
+// queryRefIDsByPath 返回某篇文档现有 refs 的业务键→id 映射,用于重建时复用旧 id 以稳定“最近引用”排序。
+func queryRefIDsByPath(tx *sql.Tx, box, path string) (ret map[string]string) {
+ rows, err := queryTx(tx, "SELECT id, block_id, def_block_id FROM refs WHERE box = ? AND path = ?", box, path)
+ if err != nil {
+ logging.LogErrorf("sql query failed: %s", err)
+ return
+ }
+ defer rows.Close()
+ ret = map[string]string{}
+ for rows.Next() {
+ var id, blockID, defBlockID string
+ if err = rows.Scan(&id, &blockID, &defBlockID); err != nil {
+ logging.LogErrorf("query scan field failed: %s", err)
+ return
+ }
+ ret[blockID+"\x00"+defBlockID] = id
+ }
+ return
+}
diff --git a/kernel/sql/cache.go b/kernel/sql/cache.go
index 5d3f870cd7..66799cd70e 100644
--- a/kernel/sql/cache.go
+++ b/kernel/sql/cache.go
@@ -178,7 +178,7 @@ func GetRefsCacheByDefIDInBox(defID, boxID string) (ret []*Ref) {
}
func CacheRef(tree *parse.Tree, refNode *ast.Node) {
- ref := buildRef(tree, refNode)
+ ref := buildRef(tree, refNode, nil)
putRefCache(tree.Box, ref)
}
diff --git a/kernel/sql/database.go b/kernel/sql/database.go
index d7ce0f50ce..eb43bedf66 100644
--- a/kernel/sql/database.go
+++ b/kernel/sql/database.go
@@ -497,14 +497,15 @@ func SetIndexAssetPath(b bool) {
indexAssetPath = b
}
-func refsFromTree(tree *parse.Tree) (refs []*Ref, fileAnnotationRefs []*FileAnnotationRef) {
+func refsFromTree(tree *parse.Tree, refIDs map[string]string) (refs []*Ref, fileAnnotationRefs []*FileAnnotationRef) {
+ // refIDs 为某篇文档现有 refs 的业务键→id 映射,命中时复用旧 id 以稳定“最近引用”排序,传 nil 表示不复用。
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if entering {
return ast.WalkContinue
}
if treenode.IsBlockRef(n) {
- ref := buildRef(tree, n)
+ ref := buildRef(tree, n, refIDs)
if !isRepeatedRef(refs, ref) {
refs = append(refs, ref)
}
@@ -537,7 +538,7 @@ func refsFromTree(tree *parse.Tree) (refs []*Ref, fileAnnotationRefs []*FileAnno
}
fileAnnotationRefs = append(fileAnnotationRefs, ref)
} else if treenode.IsEmbedBlockRef(n) {
- ref := buildEmbedRef(tree, n)
+ ref := buildEmbedRef(tree, n, refIDs)
if !isRepeatedRef(refs, ref) {
refs = append(refs, ref)
}
@@ -557,7 +558,7 @@ func isRepeatedRef(refs []*Ref, ref *Ref) bool {
return false
}
-func buildRef(tree *parse.Tree, refNode *ast.Node) *Ref {
+func buildRef(tree *parse.Tree, refNode *ast.Node, refIDs map[string]string) *Ref {
// 多个类型可能会导致渲染的 Markdown 不正确,所以这里只保留 block-ref 类型
tmpTyp := refNode.TextMarkType
refNode.TextMarkType = "block-ref"
@@ -573,8 +574,15 @@ func buildRef(tree *parse.Tree, refNode *ast.Node) *Ref {
defBlockPath = defBlock.Path
}
parentBlock := treenode.ParentBlock(refNode)
+ // 命中已有业务键时复用旧 id,避免重建索引时刷新 id 而打乱“最近引用”排序
+ id := ast.NewNodeID()
+ if nil != refIDs {
+ if oldID := refIDs[parentBlock.ID+"\x00"+defBlockID]; "" != oldID {
+ id = oldID
+ }
+ }
return &Ref{
- ID: ast.NewNodeID(),
+ ID: id,
DefBlockID: defBlockID,
DefBlockParentID: defBlockParentID,
DefBlockRootID: defBlockRootID,
@@ -589,7 +597,7 @@ func buildRef(tree *parse.Tree, refNode *ast.Node) *Ref {
}
}
-func buildEmbedRef(tree *parse.Tree, embedNode *ast.Node) *Ref {
+func buildEmbedRef(tree *parse.Tree, embedNode *ast.Node, refIDs map[string]string) *Ref {
defBlockID := getEmbedRef(embedNode)
var defBlockParentID, defBlockRootID, defBlockPath string
defBlock := treenode.GetBlockTreeInBox(defBlockID, tree.Box)
@@ -599,8 +607,15 @@ func buildEmbedRef(tree *parse.Tree, embedNode *ast.Node) *Ref {
defBlockPath = defBlock.Path
}
+ // 命中已有业务键时复用旧 id,避免重建索引时刷新 id 而打乱“最近引用”排序
+ id := ast.NewNodeID()
+ if nil != refIDs {
+ if oldID := refIDs[embedNode.ID+"\x00"+defBlockID]; "" != oldID {
+ id = oldID
+ }
+ }
return &Ref{
- ID: ast.NewNodeID(),
+ ID: id,
DefBlockID: defBlockID,
DefBlockParentID: defBlockParentID,
DefBlockRootID: defBlockRootID,
diff --git a/kernel/sql/upsert.go b/kernel/sql/upsert.go
index ee37dfeebb..f3fb7c2e26 100644
--- a/kernel/sql/upsert.go
+++ b/kernel/sql/upsert.go
@@ -428,7 +428,14 @@ func insertFileAnnotationRefs0(tx *sql.Tx, bulk []*FileAnnotationRef) (err error
func indexTree(tx *sql.Tx, tree *parse.Tree, context map[string]any) (err error) {
blocks, spans, assets, attributes := fromTree(tree.Root, tree)
- refs, fileAnnotationRefs := refsFromTree(tree)
+ // 对已存在文档的修复性重建也会走到这里,先按篇删除避免产生重复 refs 行
+ if err = deleteRefsByPathTx(tx, tree.Box, tree.Path); err != nil {
+ return
+ }
+ if err = deleteFileAnnotationRefsByPathTx(tx, tree.Box, tree.Path); err != nil {
+ return
+ }
+ refs, fileAnnotationRefs := refsFromTree(tree, nil)
err = insertTree0(tx, tree, context, blocks, spans, assets, attributes, refs, fileAnnotationRefs)
return
}
@@ -475,6 +482,8 @@ func upsertTree(tx *sql.Tx, tree *parse.Tree, context map[string]any) (err error
if err = deleteAttributesByRootID(tx, tree.ID); err != nil {
return
}
+ // 删除前先保留旧 refs 的 id 映射,重建时命中业务键则复用,避免每次重建刷新 id 而打乱“最近引用”排序
+ refIDs := queryRefIDsByPath(tx, tree.Box, tree.Path)
if err = deleteRefsByPathTx(tx, tree.Box, tree.Path); err != nil {
return
}
@@ -482,7 +491,7 @@ func upsertTree(tx *sql.Tx, tree *parse.Tree, context map[string]any) (err error
return
}
- refs, fileAnnotationRefs := refsFromTree(tree)
+ refs, fileAnnotationRefs := refsFromTree(tree, refIDs)
if err = insertTree0(tx, tree, context, blocks, spans, assets, attributes, refs, fileAnnotationRefs); err != nil {
return
}