Skip to content

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

:art: Improve copying database block https://github.com/siyuan-note/siyuan/issues/11460

Danielda629f39f5e5
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index 9f564200c9..eff63e7291 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -35,6 +35,7 @@ import (
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/cache"
+ "github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
@@ -78,6 +79,8 @@ func DuplicateDatabaseBlock(avID string) (newAvID, newBlockID string, err error)
logging.LogErrorf("write attribute view [%s] failed: %s", newAvID, err)
return
}
+
+ updateBoundBlockAvsAttribute([]string{newAvID})
return
}
@@ -3314,3 +3317,73 @@ func replaceRelationAvValues(avID, previousID, nextID string) {
}
}
}
+
+func updateBoundBlockAvsAttribute(avIDs []string) {
+ // 更新指定 avIDs 中绑定块的 avs 属性
+
+ cachedTrees, saveTrees := map[string]*parse.Tree{}, map[string]*parse.Tree{}
+ luteEngine := util.NewLute()
+ for _, avID := range avIDs {
+ attrView, _ := av.ParseAttributeView(avID)
+ if nil == attrView {
+ continue
+ }
+
+ blockKeyValues := attrView.GetBlockKeyValues()
+ for _, blockValue := range blockKeyValues.Values {
+ if blockValue.IsDetached {
+ continue
+ }
+ bt := treenode.GetBlockTree(blockValue.BlockID)
+ if nil == bt {
+ continue
+ }
+
+ tree := cachedTrees[bt.RootID]
+ if nil == tree {
+ tree, _ = filesys.LoadTree(bt.BoxID, bt.Path, luteEngine)
+ if nil == tree {
+ continue
+ }
+ cachedTrees[bt.RootID] = tree
+ }
+
+ node := treenode.GetNodeInTree(tree, blockValue.BlockID)
+ if nil == node {
+ continue
+ }
+
+ attrs := parse.IAL2Map(node.KramdownIAL)
+ if "" == attrs[av.NodeAttrNameAvs] {
+ attrs[av.NodeAttrNameAvs] = avID
+ } else {
+ nodeAvIDs := strings.Split(attrs[av.NodeAttrNameAvs], ",")
+ nodeAvIDs = append(nodeAvIDs, avID)
+ nodeAvIDs = gulu.Str.RemoveDuplicatedElem(nodeAvIDs)
+ attrs[av.NodeAttrNameAvs] = strings.Join(nodeAvIDs, ",")
+ saveTrees[bt.RootID] = tree
+ }
+
+ avNames := getAvNames(attrs[av.NodeAttrNameAvs])
+ if "" != avNames {
+ attrs[av.NodeAttrViewNames] = avNames
+ }
+
+ oldAttrs, setErr := setNodeAttrs0(node, attrs)
+ if nil != setErr {
+ continue
+ }
+ cache.PutBlockIAL(node.ID, parse.IAL2Map(node.KramdownIAL))
+ pushBroadcastAttrTransactions(oldAttrs, node)
+ }
+ }
+
+ for _, saveTree := range saveTrees {
+ if treeErr := indexWriteTreeUpsertQueue(saveTree); nil != treeErr {
+ logging.LogErrorf("index write tree upsert queue failed: %s", treeErr)
+ }
+
+ avNodes := saveTree.Root.ChildrenByType(ast.NodeAttributeView)
+ av.BatchUpsertBlockRel(avNodes)
+ }
+}
diff --git a/kernel/model/import.go b/kernel/model/import.go
index 4ff0c6df32..0ca9714f39 100644
--- a/kernel/model/import.go
+++ b/kernel/model/import.go
@@ -48,7 +48,6 @@ import (
"github.com/siyuan-note/logging"
"github.com/siyuan-note/riff"
"github.com/siyuan-note/siyuan/kernel/av"
- "github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
@@ -300,71 +299,12 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
av.BatchUpsertBlockRel(avNodes)
}
- // 如果数据库中绑定的块不在导入的文档中
- cachedTrees, saveTrees := map[string]*parse.Tree{}, map[string]*parse.Tree{}
+ // 如果数据库中绑定的块不在导入的文档中,则需要单独更新这些绑定块的属性
+ var attrViewIDs []string
for _, avID := range avIDs {
- attrView, _ := av.ParseAttributeView(avID)
- if nil == attrView {
- continue
- }
-
- blockKeyValues := attrView.GetBlockKeyValues()
- for _, blockValue := range blockKeyValues.Values {
- if blockValue.IsDetached {
- continue
- }
- bt := treenode.GetBlockTree(blockValue.BlockID)
- if nil == bt {
- continue
- }
-
- tree := cachedTrees[bt.RootID]
- if nil == tree {
- tree, _ = filesys.LoadTree(bt.BoxID, bt.Path, luteEngine)
- if nil == tree {
- continue
- }
- cachedTrees[bt.RootID] = tree
- }
-
- node := treenode.GetNodeInTree(tree, blockValue.BlockID)
- if nil == node {
- continue
- }
-
- attrs := parse.IAL2Map(node.KramdownIAL)
- if "" == attrs[av.NodeAttrNameAvs] {
- attrs[av.NodeAttrNameAvs] = avID
- } else {
- nodeAvIDs := strings.Split(attrs[av.NodeAttrNameAvs], ",")
- nodeAvIDs = append(nodeAvIDs, avID)
- nodeAvIDs = gulu.Str.RemoveDuplicatedElem(nodeAvIDs)
- attrs[av.NodeAttrNameAvs] = strings.Join(nodeAvIDs, ",")
- saveTrees[bt.RootID] = tree
- }
-
- avNames := getAvNames(attrs[av.NodeAttrNameAvs])
- if "" != avNames {
- attrs[av.NodeAttrViewNames] = avNames
- }
-
- oldAttrs, setErr := setNodeAttrs0(node, attrs)
- if nil != setErr {
- continue
- }
- cache.PutBlockIAL(node.ID, parse.IAL2Map(node.KramdownIAL))
- pushBroadcastAttrTransactions(oldAttrs, node)
- }
- }
-
- for _, saveTree := range saveTrees {
- if treeErr := indexWriteTreeUpsertQueue(saveTree); nil != treeErr {
- logging.LogErrorf("index write tree upsert queue failed: %s", treeErr)
- }
-
- avNodes := saveTree.Root.ChildrenByType(ast.NodeAttributeView)
- av.BatchUpsertBlockRel(avNodes)
+ attrViewIDs = append(attrViewIDs, avID)
}
+ updateBoundBlockAvsAttribute(attrViewIDs)
}
// 将关联的闪卡数据合并到默认卡包 data/storage/riff/20230218211946-2kw8jgx 中