From ddc3868f22400c15593b5ea9a8d132dffb52d38c Mon Sep 17 00:00:00 2001 From: Lykin <137850705+tiny-craft@users.noreply.github.com> Date: Fri, 17 Nov 2023 18:41:15 +0800 Subject: [PATCH] refactor: optimized refresh logic after update fields for set/zset type --- backend/services/browser_service.go | 105 ++++++++++++++---- backend/types/js_resp.go | 34 +++--- .../content_value/ContentEntryEditor.vue | 2 +- .../content_value/ContentValueHash.vue | 4 +- .../content_value/ContentValueSet.vue | 15 +-- .../content_value/ContentValueStream.vue | 2 +- .../content_value/ContentValueZSet.vue | 16 +-- frontend/src/stores/browser.js | 104 ++++++++++++----- frontend/src/stores/tab.js | 24 +++- 9 files changed, 211 insertions(+), 95 deletions(-) diff --git a/backend/services/browser_service.go b/backend/services/browser_service.go index e76f49d..ded2c8a 100644 --- a/backend/services/browser_service.go +++ b/backend/services/browser_service.go @@ -1132,9 +1132,10 @@ func (b *browserService) SetHashValue(param types.SetHashParam) (resp types.JSRe resp.Msg = err.Error() return } + affect, err = client.HSet(ctx, key, param.NewField, saveStr).Result() if affect <= 0 { - // no new filed added, just replace exists item + // no new filed added, just update exists item removed = append(removed, types.HashEntryItem{ Key: param.Field, }) @@ -1404,9 +1405,15 @@ func (b *browserService) UpdateSetItem(param types.SetSetParam) (resp types.JSRe client, ctx := item.client, item.ctx key := strutil.DecodeRedisKey(param.Key) + var added, removed []types.SetEntryItem + var affect int64 // remove old value str := strutil.DecodeRedisKey(param.Value) - _, _ = client.SRem(ctx, key, str).Result() + if affect, _ = client.SRem(ctx, key, str).Result(); affect > 0 { + removed = append(removed, types.SetEntryItem{ + Value: str, + }) + } // insert new value str = strutil.DecodeRedisKey(param.NewValue) @@ -1415,15 +1422,29 @@ func (b *browserService) UpdateSetItem(param types.SetSetParam) (resp types.JSRe resp.Msg = fmt.Sprintf(`save to type "%s" fail: %s`, param.Format, err.Error()) return } - _, err = client.SAdd(ctx, key, saveStr).Result() + if affect, _ = client.SAdd(ctx, key, saveStr).Result(); affect > 0 { + // add new item + var displayStr string + if len(param.RetDecode) > 0 && len(param.RetFormat) > 0 { + displayStr, _, _ = strutil.ConvertTo(saveStr, param.RetDecode, param.RetFormat) + } + added = append(added, types.SetEntryItem{ + Value: saveStr, + DisplayValue: displayStr, + }) + } if err != nil { resp.Msg = err.Error() return } resp.Success = true - resp.Data = map[string]any{ - "added": saveStr, + resp.Data = struct { + Added []types.SetEntryItem `json:"added,omitempty"` + Removed []types.SetEntryItem `json:"removed,omitempty"` + }{ + Added: added, + Removed: removed, } return } @@ -1439,13 +1460,16 @@ func (b *browserService) UpdateZSetValue(param types.SetZSetParam) (resp types.J client, ctx := item.client, item.ctx key := strutil.DecodeRedisKey(param.Key) val, newVal := strutil.DecodeRedisKey(param.Value), strutil.DecodeRedisKey(param.NewValue) - updated := map[string]float64{} - var removed []string + var added, updated, removed []types.ZSetEntryItem + var replaced []types.ZSetReplaceItem + var affect int64 if len(newVal) <= 0 { // no new value, delete value - _, err = client.ZRem(ctx, key, val).Result() - if err == nil { - removed = append(removed, val) + if affect, err = client.ZRem(ctx, key, val).Result(); affect > 0 { + //removed = append(removed, val) + removed = append(removed, types.ZSetEntryItem{ + Value: val, + }) } } else { var saveVal string @@ -1455,27 +1479,57 @@ func (b *browserService) UpdateZSetValue(param types.SetZSetParam) (resp types.J } if saveVal == val { - // update score only - _, err = client.ZAdd(ctx, key, redis.Z{ + affect, err = client.ZAdd(ctx, key, redis.Z{ Score: param.Score, Member: saveVal, }).Result() - if err == nil { - updated[saveVal] = param.Score + displayValue, _, _ := strutil.ConvertTo(val, param.RetDecode, param.RetFormat) + if affect > 0 { + // add new item + added = append(added, types.ZSetEntryItem{ + Score: param.Score, + Value: val, + DisplayValue: displayValue, + }) + } else { + // update score only + updated = append(updated, types.ZSetEntryItem{ + Score: param.Score, + Value: val, + DisplayValue: displayValue, + }) } } else { // remove old value and add new one _, err = client.ZRem(ctx, key, val).Result() - if err == nil { - removed = append(removed, val) + if err != nil { + resp.Msg = err.Error() + return } - _, err = client.ZAdd(ctx, key, redis.Z{ + affect, err = client.ZAdd(ctx, key, redis.Z{ Score: param.Score, Member: saveVal, }).Result() - if err == nil { - updated[saveVal] = param.Score + displayValue, _, _ := strutil.ConvertTo(saveVal, param.RetDecode, param.RetFormat) + if affect <= 0 { + // no new value added, just update exists item + removed = append(removed, types.ZSetEntryItem{ + Value: val, + }) + updated = append(updated, types.ZSetEntryItem{ + Score: param.Score, + Value: saveVal, + DisplayValue: displayValue, + }) + } else { + // add new field + replaced = append(replaced, types.ZSetReplaceItem{ + Score: param.Score, + Value: val, + NewValue: saveVal, + DisplayValue: displayValue, + }) } } } @@ -1485,9 +1539,16 @@ func (b *browserService) UpdateZSetValue(param types.SetZSetParam) (resp types.J } resp.Success = true - resp.Data = map[string]any{ - "updated": updated, - "removed": removed, + resp.Data = struct { + Added []types.ZSetEntryItem `json:"added,omitempty"` + Updated []types.ZSetEntryItem `json:"updated,omitempty"` + Replaced []types.ZSetReplaceItem `json:"replaced,omitempty"` + Removed []types.ZSetEntryItem `json:"removed,omitempty"` + }{ + Added: added, + Updated: updated, + Replaced: replaced, + Removed: removed, } return } diff --git a/backend/types/js_resp.go b/backend/types/js_resp.go index ddee3bf..6e5aabf 100644 --- a/backend/types/js_resp.go +++ b/backend/types/js_resp.go @@ -75,22 +75,26 @@ type SetHashParam struct { } type SetSetParam struct { - Server string `json:"server"` - DB int `json:"db"` - Key any `json:"key"` - Value any `json:"value"` - NewValue any `json:"newValue"` - Format string `json:"format,omitempty"` - Decode string `json:"decode,omitempty"` + Server string `json:"server"` + DB int `json:"db"` + Key any `json:"key"` + Value any `json:"value"` + NewValue any `json:"newValue"` + Format string `json:"format,omitempty"` + Decode string `json:"decode,omitempty"` + RetFormat string `json:"retFormat,omitempty"` + RetDecode string `json:"retDecode,omitempty"` } type SetZSetParam struct { - Server string `json:"server"` - DB int `json:"db"` - Key any `json:"key"` - Value any `json:"value"` - NewValue any `json:"newValue"` - Score float64 `json:"score"` - Format string `json:"format,omitempty"` - Decode string `json:"decode,omitempty"` + Server string `json:"server"` + DB int `json:"db"` + Key any `json:"key"` + Value any `json:"value"` + NewValue any `json:"newValue"` + Score float64 `json:"score"` + Format string `json:"format,omitempty"` + Decode string `json:"decode,omitempty"` + RetFormat string `json:"retFormat,omitempty"` + RetDecode string `json:"retDecode,omitempty"` } diff --git a/frontend/src/components/content_value/ContentEntryEditor.vue b/frontend/src/components/content_value/ContentEntryEditor.vue index a1e29d8..789387c 100644 --- a/frontend/src/components/content_value/ContentEntryEditor.vue +++ b/frontend/src/components/content_value/ContentEntryEditor.vue @@ -160,11 +160,11 @@ const onSave = () => {