From da629f39f5e532d5d0c9645a4255042888ce5be1 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Thu, 23 May 2024 23:57:10 +0800 Subject: [PATCH 1/2] :art: Improve copying database block https://github.com/siyuan-note/siyuan/issues/11460 --- kernel/model/attribute_view.go | 73 ++++++++++++++++++++++++++++++++++ kernel/model/import.go | 68 ++----------------------------- 2 files changed, 77 insertions(+), 64 deletions(-) diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index 9f564200c..eff63e729 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 4ff0c6df3..0ca9714f3 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 中 From 50ef0d017da0f711c157f7cfd19961ed572ec556 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Fri, 24 May 2024 00:01:30 +0800 Subject: [PATCH 2/2] :memo: Update FAQs --- README.md | 1 - README_zh_CN.md | 1 - 2 files changed, 2 deletions(-) diff --git a/README.md b/README.md index 3d9d09442..d1fbc60a0 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,6 @@ * [Does it support data synchronization through a third-party sync disk?](#does-it-support-data-synchronization-through-a-third-party-sync-disk) * [Is SiYuan open source?](#is-siyuan-open-source) * [How to upgrade to a new version?](#how-to-upgrade-to-a-new-version) - * [Is there any note for deleting docs?](#is-there-any-note-for-deleting-docs) * [How can I just wrap and not start a new paragraph?](#how-can-i-just-wrap-and-not-start-a-new-paragraph) * [What if some blocks (such as paragraph blocks in list items) cannot find the block icon?](#what-if-some-blocks-such-as-paragraph-blocks-in-list-items-cannot-find-the-block-icon) * [How to share notes?](#how-to-share-notes) diff --git a/README_zh_CN.md b/README_zh_CN.md index a76203385..29e4619d2 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -50,7 +50,6 @@ * [支持通过第三方同步盘进行数据同步吗?](#支持通过第三方同步盘进行数据同步吗) * [思源是开源的吗?](#思源是开源的吗) * [如何升级到新版本?](#如何升级到新版本) - * [删除文档有什么注意事项吗?](#删除文档有什么注意事项吗) * [如何才能只换行不新起段落?](#如何才能只换行不新起段落) * [有的块(比如在列表项中的段落块)找不到块标怎么办?](#有的块比如在列表项中的段落块找不到块标怎么办) * [如何分享笔记?](#如何分享笔记)