Skip to content

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

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

Daniele81e2b0f60c3
diff --git a/kernel/model/search.go b/kernel/model/search.go
index df316c4e78..3347e37b96 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 356e712d24..6672648552 100644
--- a/kernel/sql/block_ref.go
+++ b/kernel/sql/block_ref.go
@@ -20,7 +20,6 @@ import (
"database/sql"
"github.com/88250/lute/parse"
- "github.com/siyuan-note/logging"
)
type Ref struct {
@@ -39,15 +38,13 @@ 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, refIDs)
+ err = insertRefs(tx, tree)
return
}
@@ -61,8 +58,8 @@ func deleteRefs(tx *sql.Tx, tree *parse.Tree) (err error) {
return
}
-func insertRefs(tx *sql.Tx, tree *parse.Tree, refIDs map[string]string) (err error) {
- refs, fileAnnotationRefs := refsFromTree(tree, refIDs)
+func insertRefs(tx *sql.Tx, tree *parse.Tree) (err error) {
+ refs, fileAnnotationRefs := refsFromTree(tree)
if err = insertBlockRefs(tx, refs); err != nil {
return
}
@@ -71,23 +68,3 @@ func insertRefs(tx *sql.Tx, tree *parse.Tree, refIDs map[string]string) (err err
}
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 66799cd70e..5d3f870cd7 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, nil)
+ ref := buildRef(tree, refNode)
putRefCache(tree.Box, ref)
}
diff --git a/kernel/sql/database.go b/kernel/sql/database.go
index eb43bedf66..d7ce0f50ce 100644
--- a/kernel/sql/database.go
+++ b/kernel/sql/database.go
@@ -497,15 +497,14 @@ func SetIndexAssetPath(b bool) {
indexAssetPath = b
}
-func refsFromTree(tree *parse.Tree, refIDs map[string]string) (refs []*Ref, fileAnnotationRefs []*FileAnnotationRef) {
- // refIDs 为某篇文档现有 refs 的业务键→id 映射,命中时复用旧 id 以稳定“最近引用”排序,传 nil 表示不复用。
+func refsFromTree(tree *parse.Tree) (refs []*Ref, fileAnnotationRefs []*FileAnnotationRef) {
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, refIDs)
+ ref := buildRef(tree, n)
if !isRepeatedRef(refs, ref) {
refs = append(refs, ref)
}
@@ -538,7 +537,7 @@ func refsFromTree(tree *parse.Tree, refIDs map[string]string) (refs []*Ref, file
}
fileAnnotationRefs = append(fileAnnotationRefs, ref)
} else if treenode.IsEmbedBlockRef(n) {
- ref := buildEmbedRef(tree, n, refIDs)
+ ref := buildEmbedRef(tree, n)
if !isRepeatedRef(refs, ref) {
refs = append(refs, ref)
}
@@ -558,7 +557,7 @@ func isRepeatedRef(refs []*Ref, ref *Ref) bool {
return false
}
-func buildRef(tree *parse.Tree, refNode *ast.Node, refIDs map[string]string) *Ref {
+func buildRef(tree *parse.Tree, refNode *ast.Node) *Ref {
// 多个类型可能会导致渲染的 Markdown 不正确,所以这里只保留 block-ref 类型
tmpTyp := refNode.TextMarkType
refNode.TextMarkType = "block-ref"
@@ -574,15 +573,8 @@ func buildRef(tree *parse.Tree, refNode *ast.Node, refIDs map[string]string) *Re
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: id,
+ ID: ast.NewNodeID(),
DefBlockID: defBlockID,
DefBlockParentID: defBlockParentID,
DefBlockRootID: defBlockRootID,
@@ -597,7 +589,7 @@ func buildRef(tree *parse.Tree, refNode *ast.Node, refIDs map[string]string) *Re
}
}
-func buildEmbedRef(tree *parse.Tree, embedNode *ast.Node, refIDs map[string]string) *Ref {
+func buildEmbedRef(tree *parse.Tree, embedNode *ast.Node) *Ref {
defBlockID := getEmbedRef(embedNode)
var defBlockParentID, defBlockRootID, defBlockPath string
defBlock := treenode.GetBlockTreeInBox(defBlockID, tree.Box)
@@ -607,15 +599,8 @@ func buildEmbedRef(tree *parse.Tree, embedNode *ast.Node, refIDs map[string]stri
defBlockPath = defBlock.Path
}
- // 命中已有业务键时复用旧 id,避免重建索引时刷新 id 而打乱“最近引用”排序
- id := ast.NewNodeID()
- if nil != refIDs {
- if oldID := refIDs[embedNode.ID+"\x00"+defBlockID]; "" != oldID {
- id = oldID
- }
- }
return &Ref{
- ID: id,
+ ID: ast.NewNodeID(),
DefBlockID: defBlockID,
DefBlockParentID: defBlockParentID,
DefBlockRootID: defBlockRootID,
diff --git a/kernel/sql/upsert.go b/kernel/sql/upsert.go
index f3fb7c2e26..ee37dfeebb 100644
--- a/kernel/sql/upsert.go
+++ b/kernel/sql/upsert.go
@@ -428,14 +428,7 @@ 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 行
- 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)
+ refs, fileAnnotationRefs := refsFromTree(tree)
err = insertTree0(tx, tree, context, blocks, spans, assets, attributes, refs, fileAnnotationRefs)
return
}
@@ -482,8 +475,6 @@ 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
}
@@ -491,7 +482,7 @@ func upsertTree(tx *sql.Tx, tree *parse.Tree, context map[string]any) (err error
return
}
- refs, fileAnnotationRefs := refsFromTree(tree, refIDs)
+ refs, fileAnnotationRefs := refsFromTree(tree)
if err = insertTree0(tx, tree, context, blocks, spans, assets, attributes, refs, fileAnnotationRefs); err != nil {
return
}