From 352e7b714d97c06256a5919fe128b50c7721ab9c Mon Sep 17 00:00:00 2001 From: tiny-craft <137850705+tiny-craft@users.noreply.github.com> Date: Thu, 9 Nov 2023 00:30:07 +0800 Subject: [PATCH] refactor: optimize the key renaming logic --- frontend/src/stores/browser.js | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/frontend/src/stores/browser.js b/frontend/src/stores/browser.js index c022f99..0dbd685 100644 --- a/frontend/src/stores/browser.js +++ b/frontend/src/stores/browser.js @@ -5,6 +5,7 @@ import { get, isEmpty, join, + last, remove, set, size, @@ -1367,6 +1368,34 @@ const useBrowserStore = defineStore('browser', { } }, + /** + * + * @param {string} connName + * @param {number} db + * @param {string} key + * @param {string} newKey + * @private + */ + _renameKeyNode(connName, db, key, newKey) { + const nodeMap = this._getNodeMap(connName, db) + const nodeKey = `${ConnectionType.RedisValue}/${key}` + const newNodeKey = `${ConnectionType.RedisValue}/${newKey}` + const node = nodeMap.get(nodeKey) + if (node != null) { + // replace node map item + const separator = this._getSeparator(connName) + node.label = last(split(newKey, separator)) + node.key = `${connName}/db${db}#${newNodeKey}` + node.redisKey = newKey + nodeMap[newNodeKey] = node + nodeMap.delete(nodeKey) + // replace key set item + const keySet = this._getKeySet(connName, db) + keySet.delete(key) + keySet.add(newKey) + } + }, + /** * * @param {string} connName @@ -1582,8 +1611,7 @@ const useBrowserStore = defineStore('browser', { const { success = false, msg } = await RenameKey(connName, db, key, newKey) if (success) { // delete old key and add new key struct - this._deleteKeyNode(connName, db, key) - this._addKeyNodes(connName, db, [newKey]) + this._renameKeyNode(connName, db, key, newKey) return { success: true, nodeKey: `${connName}/db${db}#${ConnectionType.RedisValue}/${newKey}` } } else { return { success: false, msg }