🎨 Improve mirror database blocks reload

This commit is contained in:
Daniel 2024-04-21 18:18:23 +08:00
parent 1db8269b9f
commit 7f98213879
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
6 changed files with 55 additions and 21 deletions

View file

@ -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)
}

View file

@ -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
}

View file

@ -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 {

View file

@ -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)
}
}
}

View file

@ -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

View file

@ -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)
}