Skip to content

Commit

Permalink
🎨 Improve copying database block #11460
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed May 23, 2024
1 parent 6bf910d commit da629f3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 64 deletions.
73 changes: 73 additions & 0 deletions kernel/model/attribute_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
}
68 changes: 4 additions & 64 deletions kernel/model/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 中
Expand Down

0 comments on commit da629f3

Please sign in to comment.