Skip to content

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

:art: Improve mirror database blocks reload

Daniel7f982138794d
diff --git a/kernel/api/av.go b/kernel/api/av.go
index 08fcc4415e..2cb0cc4520 100644
--- a/kernel/api/av.go
+++ b/kernel/api/av.go
@@ -135,7 +135,7 @@ func addAttributeViewValues(c *gin.Context) {
return
}
- pushRefreshAttrView(avID)
+ util.PushReloadAttrView(avID)
}
func removeAttributeViewValues(c *gin.Context) {
@@ -160,7 +160,7 @@ func removeAttributeViewValues(c *gin.Context) {
return
}
- pushRefreshAttrView(avID)
+ util.PushReloadAttrView(avID)
}
func addAttributeViewKey(c *gin.Context) {
@@ -186,7 +186,7 @@ func addAttributeViewKey(c *gin.Context) {
return
}
- pushRefreshAttrView(avID)
+ util.PushReloadAttrView(avID)
}
func removeAttributeViewKey(c *gin.Context) {
@@ -208,7 +208,7 @@ func removeAttributeViewKey(c *gin.Context) {
return
}
- pushRefreshAttrView(avID)
+ util.PushReloadAttrView(avID)
}
func sortAttributeViewKey(c *gin.Context) {
@@ -235,7 +235,7 @@ func sortAttributeViewKey(c *gin.Context) {
return
}
- pushRefreshAttrView(avID)
+ util.PushReloadAttrView(avID)
}
func getAttributeViewFilterSort(c *gin.Context) {
@@ -509,9 +509,5 @@ func setAttributeViewBlockAttr(c *gin.Context) {
blockAttributeViewKeys := model.UpdateAttributeViewCell(nil, avID, keyID, rowID, cellID, value)
ret.Data = blockAttributeViewKeys
- pushRefreshAttrView(avID)
-}
-
-func pushRefreshAttrView(avID string) {
- util.BroadcastByType("protyle", "refreshAttributeView", 0, "", map[string]interface{}{"id": avID})
+ util.PushReloadAttrView(avID)
}
diff --git a/kernel/av/mirror.go b/kernel/av/mirror.go
index 8814d8f566..bd9db29e0a 100644
--- a/kernel/av/mirror.go
+++ b/kernel/av/mirror.go
@@ -66,7 +66,7 @@ func IsMirror(avID string) bool {
return nil != blockIDs && 1 < len(blockIDs)
}
-func RemoveBlockRel(avID, blockID string) {
+func RemoveBlockRel(avID, blockID string, existBlockTree func(string) bool) (ret bool) {
AttributeViewBlocksLock.Lock()
defer AttributeViewBlocksLock.Unlock()
@@ -95,10 +95,13 @@ func RemoveBlockRel(avID, blockID string) {
var newBlockIDs []string
for _, v := range blockIDs {
if v != blockID {
- newBlockIDs = append(newBlockIDs, v)
+ if existBlockTree(v) {
+ newBlockIDs = append(newBlockIDs, v)
+ }
}
}
avBlocks[avID] = newBlockIDs
+ ret = len(newBlockIDs) != len(blockIDs)
data, err = msgpack.Marshal(avBlocks)
if nil != err {
@@ -109,6 +112,7 @@ func RemoveBlockRel(avID, blockID string) {
logging.LogErrorf("write attribute view blocks failed: %s", err)
return
}
+ return
}
func BatchUpsertBlockRel(nodes []*ast.Node) {
@@ -161,7 +165,7 @@ func BatchUpsertBlockRel(nodes []*ast.Node) {
}
}
-func UpsertBlockRel(avID, blockID string) {
+func UpsertBlockRel(avID, blockID string) (ret bool) {
AttributeViewBlocksLock.Lock()
defer AttributeViewBlocksLock.Unlock()
@@ -186,9 +190,11 @@ func UpsertBlockRel(avID, blockID string) {
}
blockIDs := avBlocks[avID]
+ oldLen := len(blockIDs)
blockIDs = append(blockIDs, blockID)
blockIDs = gulu.Str.RemoveDuplicatedElem(blockIDs)
avBlocks[avID] = blockIDs
+ ret = oldLen != len(blockIDs)
data, err := msgpack.Marshal(avBlocks)
if nil != err {
@@ -199,4 +205,5 @@ func UpsertBlockRel(avID, blockID string) {
logging.LogErrorf("write attribute view blocks failed: %s", err)
return
}
+ return
}
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index eefa76e4a1..9d8f37c3e3 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -1603,7 +1603,7 @@ func updateAttributeViewColRelation(operation *Operation) (err error) {
}
if !isSameAv {
err = av.SaveAttributeView(destAv)
- util.BroadcastByType("protyle", "refreshAttributeView", 0, "", map[string]interface{}{"id": destAv.ID})
+ util.PushReloadAttrView(destAv.ID)
}
av.UpsertAvBackRel(srcAv.ID, destAv.ID)
@@ -2420,7 +2420,7 @@ func removeAttributeViewBlock(srcIDs []string, avID string, tx *Transaction) (er
relatedAvIDs := av.GetSrcAvIDs(avID)
for _, relatedAvID := range relatedAvIDs {
- util.BroadcastByType("protyle", "refreshAttributeView", 0, "", map[string]interface{}{"id": relatedAvID})
+ util.PushReloadAttrView(relatedAvID)
}
err = av.SaveAttributeView(attrView)
@@ -2935,7 +2935,7 @@ func RemoveAttributeViewKey(avID, keyID string) (err error) {
}
av.SaveAttributeView(destAv)
- util.BroadcastByType("protyle", "refreshAttributeView", 0, "", map[string]interface{}{"id": destAv.ID})
+ util.PushReloadAttrView(destAv.ID)
if !destAvRelSrcAv {
av.RemoveAvRel(destAv.ID, attrView.ID)
@@ -3251,7 +3251,7 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string,
relatedAvIDs := av.GetSrcAvIDs(avID)
for _, relatedAvID := range relatedAvIDs {
- util.BroadcastByType("protyle", "refreshAttributeView", 0, "", map[string]interface{}{"id": relatedAvID})
+ util.PushReloadAttrView(relatedAvID)
}
if err = av.SaveAttributeView(attrView); nil != err {
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 3f3e1ff1df..6747c7c628 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -799,6 +799,7 @@ func (tx *Transaction) doDelete(operation *Operation) (ret *TxErr) {
}
func removeAvBlockRel(node *ast.Node) {
+ var avIDs []string
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
return ast.WalkContinue
@@ -806,10 +807,16 @@ func removeAvBlockRel(node *ast.Node) {
if ast.NodeAttributeView == n.Type {
avID := n.AttributeViewID
- av.RemoveBlockRel(avID, n.ID)
+ if changed := av.RemoveBlockRel(avID, n.ID, treenode.ExistBlockTree); changed {
+ avIDs = append(avIDs, avID)
+ }
}
return ast.WalkContinue
})
+ avIDs = gulu.Str.RemoveDuplicatedElem(avIDs)
+ for _, avID := range avIDs {
+ util.PushReloadAttrView(avID)
+ }
}
func syncDelete2AttributeView(node *ast.Node) {
@@ -854,7 +861,7 @@ func syncDelete2AttributeView(node *ast.Node) {
})
for _, avID := range changedAvIDs.Values() {
- util.BroadcastByType("protyle", "refreshAttributeView", 0, "", map[string]interface{}{"id": avID})
+ util.PushReloadAttrView(avID.(string))
}
}
@@ -1127,6 +1134,7 @@ func refreshHeadingChildrenUpdated(heading *ast.Node, updated string) {
}
func upsertAvBlockRel(node *ast.Node) {
+ var avIDs []string
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
return ast.WalkContinue
@@ -1134,10 +1142,16 @@ func upsertAvBlockRel(node *ast.Node) {
if ast.NodeAttributeView == n.Type {
avID := n.AttributeViewID
- av.UpsertBlockRel(avID, n.ID)
+ if changed := av.UpsertBlockRel(avID, n.ID); changed {
+ avIDs = append(avIDs, avID)
+ }
}
return ast.WalkContinue
})
+ avIDs = gulu.Str.RemoveDuplicatedElem(avIDs)
+ for _, avID := range avIDs {
+ util.PushReloadAttrView(avID)
+ }
}
func (tx *Transaction) doUpdateUpdated(operation *Operation) (ret *TxErr) {
@@ -1471,7 +1485,7 @@ func refreshDynamicRefTexts(updatedDefNodes map[string]*ast.Node, updatedTrees m
}
if changedAv {
av.SaveAttributeView(attrView)
- util.BroadcastByType("protyle", "refreshAttributeView", 0, "", map[string]interface{}{"id": avID})
+ util.PushReloadAttrView(avID)
}
}
}
diff --git a/kernel/treenode/blocktree.go b/kernel/treenode/blocktree.go
index 823036acc8..03c3ddafd2 100644
--- a/kernel/treenode/blocktree.go
+++ b/kernel/treenode/blocktree.go
@@ -194,6 +194,19 @@ func GetBlockTreeRootByHPathPreferredParentID(boxID, hPath, preferredParentID st
return
}
+func ExistBlockTree(id string) bool {
+ hash := btHash(id)
+ val, ok := blockTrees.Load(hash)
+ if !ok {
+ return false
+ }
+ slice := val.(*btSlice)
+ slice.m.Lock()
+ _, ok = slice.data[id]
+ slice.m.Unlock()
+ return ok
+}
+
func GetBlockTree(id string) (ret *BlockTree) {
if "" == id {
return
diff --git a/kernel/util/websocket.go b/kernel/util/websocket.go
index 8683157393..2c8bcbf10e 100644
--- a/kernel/util/websocket.go
+++ b/kernel/util/websocket.go
@@ -216,6 +216,10 @@ func PushClearProgress() {
BroadcastByType("main", "cprogress", 0, "", nil)
}
+func PushReloadAttrView(avID string) {
+ BroadcastByType("protyle", "refreshAttributeView", 0, "", map[string]interface{}{"id": avID})
+}
+
func PushReloadDoc(rootID string) {
BroadcastByType("main", "reloaddoc", 0, "", rootID)
}