🎨 Maintain relationships between database blocks and database storage Fix https://github.com/siyuan-note/siyuan/issues/9496
This commit is contained in:
parent
af6036eefe
commit
903b046ce1
2 changed files with 105 additions and 0 deletions
|
@ -25,6 +25,7 @@ import (
|
|||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/88250/gulu"
|
||||
|
@ -32,6 +33,7 @@ import (
|
|||
"github.com/siyuan-note/filelock"
|
||||
"github.com/siyuan-note/logging"
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
"golang.org/x/text/language"
|
||||
"golang.org/x/text/message"
|
||||
)
|
||||
|
@ -584,6 +586,95 @@ func NewAttributeView(id string) (ret *AttributeView) {
|
|||
return
|
||||
}
|
||||
|
||||
var (
|
||||
attributeViewBlocksLock = sync.Mutex{}
|
||||
)
|
||||
|
||||
func RemoveBlockRel(avID, blockID string) {
|
||||
attributeViewBlocksLock.Lock()
|
||||
defer attributeViewBlocksLock.Unlock()
|
||||
|
||||
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
|
||||
if !gulu.File.IsExist(blocks) {
|
||||
return
|
||||
}
|
||||
|
||||
data, err := filelock.ReadFile(blocks)
|
||||
if nil != err {
|
||||
logging.LogErrorf("read attribute view blocks failed: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
avBlocks := map[string][]string{}
|
||||
if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
|
||||
logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
blockIDs := avBlocks[avID]
|
||||
if nil == blockIDs {
|
||||
return
|
||||
}
|
||||
|
||||
var newBlockIDs []string
|
||||
for _, v := range blockIDs {
|
||||
if v != blockID {
|
||||
newBlockIDs = append(newBlockIDs, v)
|
||||
}
|
||||
}
|
||||
avBlocks[avID] = newBlockIDs
|
||||
|
||||
data, err = msgpack.Marshal(avBlocks)
|
||||
if nil != err {
|
||||
logging.LogErrorf("marshal attribute view blocks failed: %s", err)
|
||||
return
|
||||
}
|
||||
if err = filelock.WriteFile(blocks, data); nil != err {
|
||||
logging.LogErrorf("write attribute view blocks failed: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func UpsertBlockRel(avID, blockID string) {
|
||||
attributeViewBlocksLock.Lock()
|
||||
defer attributeViewBlocksLock.Unlock()
|
||||
|
||||
avBlocks := map[string][]string{}
|
||||
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
|
||||
if !gulu.File.IsExist(blocks) {
|
||||
if err := os.MkdirAll(filepath.Dir(blocks), 0755); nil != err {
|
||||
logging.LogErrorf("create attribute view dir failed: %s", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
data, err := filelock.ReadFile(blocks)
|
||||
if nil != err {
|
||||
logging.LogErrorf("read attribute view blocks failed: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
|
||||
logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
blockIDs := avBlocks[avID]
|
||||
blockIDs = append(blockIDs, blockID)
|
||||
blockIDs = gulu.Str.RemoveDuplicatedElem(blockIDs)
|
||||
avBlocks[avID] = blockIDs
|
||||
|
||||
data, err := msgpack.Marshal(avBlocks)
|
||||
if nil != err {
|
||||
logging.LogErrorf("marshal attribute view blocks failed: %s", err)
|
||||
return
|
||||
}
|
||||
if err = filelock.WriteFile(blocks, data); nil != err {
|
||||
logging.LogErrorf("write attribute view blocks failed: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func ParseAttributeView(avID string) (ret *AttributeView, err error) {
|
||||
avJSONPath := GetAttributeViewDataPath(avID)
|
||||
if !gulu.File.IsExist(avJSONPath) {
|
||||
|
|
|
@ -712,6 +712,10 @@ func (tx *Transaction) doDelete(operation *Operation) (ret *TxErr) {
|
|||
}
|
||||
|
||||
syncDelete2AttributeView(node)
|
||||
if ast.NodeAttributeView == node.Type {
|
||||
avID := node.AttributeViewID
|
||||
av.RemoveBlockRel(avID, node.ID)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -898,6 +902,11 @@ func (tx *Transaction) doInsert(operation *Operation) (ret *TxErr) {
|
|||
return &TxErr{code: TxErrCodeWriteTree, msg: err.Error(), id: block.ID}
|
||||
}
|
||||
|
||||
if ast.NodeAttributeView == insertedNode.Type {
|
||||
avID := insertedNode.AttributeViewID
|
||||
av.UpsertBlockRel(avID, insertedNode.ID)
|
||||
}
|
||||
|
||||
operation.ID = insertedNode.ID
|
||||
operation.ParentID = insertedNode.Parent.ID
|
||||
return
|
||||
|
@ -986,6 +995,11 @@ func (tx *Transaction) doUpdate(operation *Operation) (ret *TxErr) {
|
|||
if err = tx.writeTree(tree); nil != err {
|
||||
return &TxErr{code: TxErrCodeWriteTree, msg: err.Error(), id: id}
|
||||
}
|
||||
|
||||
if ast.NodeAttributeView == updatedNode.Type {
|
||||
avID := updatedNode.AttributeViewID
|
||||
av.UpsertBlockRel(avID, updatedNode.ID)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue