An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Maintain relationships between database blocks and database storage Fix https://github.com/siyuan-note/siyuan/issues/9496
Daniel903b046ce1c0
diff --git a/kernel/av/av.go b/kernel/av/av.goindex 0b87967aec..2a6b38c0d4 100644--- a/kernel/av/av.go+++ b/kernel/av/av.go@@ -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) {diff --git a/kernel/model/transaction.go b/kernel/model/transaction.goindex 71c2056320..ef5318a1be 100644--- a/kernel/model/transaction.go+++ b/kernel/model/transaction.go@@ -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.IDoperation.ParentID = insertedNode.Parent.IDreturn@@ -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}