refactor: remove unused "keySet" in browserStore

This commit is contained in:
Lykin 2023-12-19 20:19:41 +08:00
parent f98229b9fa
commit 7fa1ecfa0a
1 changed files with 39 additions and 69 deletions

View File

@ -102,9 +102,11 @@ const useBrowserStore = defineStore('browser', {
* @typedef {Object} BrowserState * @typedef {Object} BrowserState
* @property {Object} serverStats * @property {Object} serverStats
* @property {Object.<string, FilterItem>} filter * @property {Object.<string, FilterItem>} filter
* @property {Object.<string, LoadingState>} loadingState
* @property {Object.<string, KeyViewType>} viewType * @property {Object.<string, KeyViewType>} viewType
* @property {Object.<string, DatabaseItem[]>} databases * @property {Object.<string, DatabaseItem[]>} databases
* @property {Object.<string, Map<string, DatabaseItem>>} nodeMap key format likes 'server#db', children key format likes 'key#type' * @property {Object.<string, Map<string, DatabaseItem>>} nodeMap key format likes 'server#db', children key format likes 'key#type'
* @property {Object.<string, string>} openedDB
*/ */
/** /**
@ -118,7 +120,6 @@ const useBrowserStore = defineStore('browser', {
viewType: {}, // view type selection for all opened connections group by 'server' viewType: {}, // view type selection for all opened connections group by 'server'
databases: {}, // all database lists in opened connections group by 'server name' databases: {}, // all database lists in opened connections group by 'server name'
nodeMap: {}, // all nodes in opened connections group by 'server#db' and 'type/key' nodeMap: {}, // all nodes in opened connections group by 'server#db' and 'type/key'
keySet: {}, // all keys set in opened connections group by 'server#db
openedDB: {}, // opened database map by server and database index openedDB: {}, // opened database map by server and database index
}), }),
getters: { getters: {
@ -151,7 +152,6 @@ const useBrowserStore = defineStore('browser', {
this.databases = {} this.databases = {}
this.nodeMap.clear() this.nodeMap.clear()
this.keySet.clear()
this.serverStats = {} this.serverStats = {}
const tabStore = useTabStore() const tabStore = useTabStore()
tabStore.removeAllTab() tabStore.removeAllTab()
@ -209,32 +209,31 @@ const useBrowserStore = defineStore('browser', {
* @param {string} connName * @param {string} connName
* @param {number} viewType * @param {number} viewType
*/ */
async switchKeyView(connName, viewType) { // async switchKeyView(connName, viewType) {
if (viewType !== KeyViewType.Tree && viewType !== KeyViewType.List) { // if (viewType !== KeyViewType.Tree && viewType !== KeyViewType.List) {
return // return
} // }
//
const t = get(this.viewType, connName, KeyViewType.Tree) // const t = get(this.viewType, connName, KeyViewType.Tree)
if (t === viewType) { // if (t === viewType) {
return // return
} // }
//
this.viewType[connName] = viewType // this.viewType[connName] = viewType
const dbs = get(this.databases, connName, []) // const dbs = get(this.databases, connName, [])
for (const dbItem of dbs) { // for (const dbItem of dbs) {
if (!dbItem.opened) { // if (!dbItem.opened) {
continue // continue
} // }
//
dbItem.children = undefined // dbItem.children = undefined
dbItem.keys = 0 // dbItem.keys = 0
const { db = 0 } = dbItem // const { db = 0 } = dbItem
this._getNodeMap(connName, db).clear() // this._getNodeMap(connName, db).clear()
const keys = this._getKeySet(connName, db) // this._addKeyNodes(connName, db, keys)
this._addKeyNodes(connName, db, keys) // this._tidyNode(connName, db, '')
this._tidyNode(connName, db, '') // }
} // },
},
/** /**
* open connection * open connection
@ -269,7 +268,6 @@ const useBrowserStore = defineStore('browser', {
let containLastDB = false let containLastDB = false
for (let i = 0; i < db.length; i++) { for (let i = 0; i < db.length; i++) {
this._getNodeMap(name, i).clear() this._getNodeMap(name, i).clear()
this._getKeySet(name, i).clear()
dbs.push({ dbs.push({
key: `${name}/${db[i].name}`, key: `${name}/${db[i].name}`,
label: db[i].name, label: db[i].name,
@ -311,7 +309,6 @@ const useBrowserStore = defineStore('browser', {
if (!isEmpty(dbs)) { if (!isEmpty(dbs)) {
for (const db of dbs) { for (const db of dbs) {
this._getNodeMap(name, db.db).clear() this._getNodeMap(name, db.db).clear()
this._getKeySet(name, db.db).clear()
} }
} }
delete this.filter[name] delete this.filter[name]
@ -369,7 +366,6 @@ const useBrowserStore = defineStore('browser', {
selDB.isLeaf = false selDB.isLeaf = false
this._getNodeMap(connName, db).clear() this._getNodeMap(connName, db).clear()
this._getKeySet(connName, db).clear()
delete this.filter[connName] delete this.filter[connName]
}, },
@ -389,7 +385,6 @@ const useBrowserStore = defineStore('browser', {
selDB.keys = 0 selDB.keys = 0
this._getNodeMap(server, db).clear() this._getNodeMap(server, db).clear()
this._getKeySet(server, db).clear()
delete this.filter[server] delete this.filter[server]
}, },
@ -667,34 +662,34 @@ const useBrowserStore = defineStore('browser', {
/** /**
* reload keys under layer * reload keys under layer
* @param {string} connName * @param {string} server
* @param {number} db * @param {number} db
* @param {string} prefix * @param {string} prefix
* @return {Promise<void>} * @return {Promise<void>}
*/ */
async reloadLayer(connName, db, prefix) { async reloadLayer(server, db, prefix) {
if (isEmpty(prefix)) { if (isEmpty(prefix)) {
return return
} }
let match = prefix let match = prefix
const separator = this._getSeparator(connName) const separator = this._getSeparator(server)
if (!endsWith(match, separator)) { if (!endsWith(match, separator)) {
match += separator + '*' match += separator + '*'
} else { } else {
match += '*' match += '*'
} }
// FIXME: ignore original match pattern due to redis not support combination matching // FIXME: ignore original match pattern due to redis not support combination matching
const { match: originMatch, type: keyType } = this.getKeyFilter(connName, db) const { match: originMatch, type: keyType } = this.getKeyFilter(server)
const { keys, maxKeys, success } = await this._loadKeys(connName, db, match, keyType, true) const { keys, maxKeys, success } = await this._loadKeys(server, db, match, keyType, true)
if (!success) { if (!success) {
return return
} }
this._setDBMaxKeys(connName, db, maxKeys) this._setDBMaxKeys(server, db, maxKeys)
// remove current keys below prefix // remove current keys below prefix
this._deleteKeyNode(connName, db, prefix, true) this._deleteKeyNode(server, db, prefix, true)
this._addKeyNodes(connName, db, keys) this._addKeyNodes(server, db, keys)
this._tidyNode(connName, db, prefix) this._tidyNode(server, db, prefix)
}, },
/** /**
@ -727,21 +722,6 @@ const useBrowserStore = defineStore('browser', {
return this.nodeMap[`${connName}#${db}`] return this.nodeMap[`${connName}#${db}`]
}, },
/**
* get all keys in a database
* @param {string} connName
* @param {number} db
* @return {Set<string|number[]>}
* @private
*/
_getKeySet(connName, db) {
if (!this.keySet.hasOwnProperty(`${connName}#${db}`)) {
this.keySet[`${connName}#${db}`] = new Set()
}
// construct a key set
return this.keySet[`${connName}#${db}`]
},
/** /**
* remove keys in db * remove keys in db
* @param {string} connName * @param {string} connName
@ -771,7 +751,6 @@ const useBrowserStore = defineStore('browser', {
selDB.children = [] selDB.children = []
} }
const nodeMap = this._getNodeMap(connName, db) const nodeMap = this._getNodeMap(connName, db)
const keySet = this._getKeySet(connName, db)
const rootChildren = selDB.children const rootChildren = selDB.children
const viewType = get(this.viewType, connName, KeyViewType.Tree) const viewType = get(this.viewType, connName, KeyViewType.Tree)
if (viewType === KeyViewType.List) { if (viewType === KeyViewType.List) {
@ -793,7 +772,6 @@ const useBrowserStore = defineStore('browser', {
isLeaf: true, isLeaf: true,
} }
nodeMap.set(nodeKey, selectedNode) nodeMap.set(nodeKey, selectedNode)
keySet.add(key)
if (!replaceKey) { if (!replaceKey) {
if (sortInsert) { if (sortInsert) {
const index = sortedIndexBy(rootChildren, selectedNode, 'key') const index = sortedIndexBy(rootChildren, selectedNode, 'key')
@ -860,7 +838,6 @@ const useBrowserStore = defineStore('browser', {
isLeaf: true, isLeaf: true,
} }
nodeMap.set(nodeKey, selectedNode) nodeMap.set(nodeKey, selectedNode)
keySet.add(key)
if (!replaceKey) { if (!replaceKey) {
if (sortInsert) { if (sortInsert) {
const index = sortedIndexBy(children, selectedNode, 'key') const index = sortedIndexBy(children, selectedNode, 'key')
@ -1844,9 +1821,8 @@ const useBrowserStore = defineStore('browser', {
} }
const nodeMap = this._getNodeMap(connName, db) const nodeMap = this._getNodeMap(connName, db)
const keySet = this._getKeySet(connName, db)
if (isLayer === true) { if (isLayer === true) {
this._deleteChildrenKeyNodes(nodeMap, keySet, key) this._deleteChildrenKeyNodes(nodeMap, key)
} }
if (isEmpty(key)) { if (isEmpty(key)) {
// clear all key nodes // clear all key nodes
@ -1884,7 +1860,6 @@ const useBrowserStore = defineStore('browser', {
if (isEmpty(anceNode.children)) { if (isEmpty(anceNode.children)) {
nodeMap.delete(`${ConnectionType.RedisKey}/${anceKey}`) nodeMap.delete(`${ConnectionType.RedisKey}/${anceKey}`)
keySet.delete(anceNode.redisKeyCode || anceNode.redisKey)
} else { } else {
break break
} }
@ -1894,7 +1869,6 @@ const useBrowserStore = defineStore('browser', {
const node = nodeMap.get(`${ConnectionType.RedisValue}/${keyParts[0]}`) const node = nodeMap.get(`${ConnectionType.RedisValue}/${keyParts[0]}`)
if (node != null) { if (node != null) {
nodeMap.delete(`${ConnectionType.RedisValue}/${keyParts[0]}`) nodeMap.delete(`${ConnectionType.RedisValue}/${keyParts[0]}`)
keySet.delete(node.redisKeyCode || node.redisKey)
} }
} }
} }
@ -1906,14 +1880,12 @@ const useBrowserStore = defineStore('browser', {
/** /**
* delete node and all it's children from nodeMap * delete node and all it's children from nodeMap
* @param {Map<string, DatabaseItem>} nodeMap * @param {Map<string, DatabaseItem>} nodeMap
* @param {Set<string|number[]>} keySet
* @param {string} [key] clean nodeMap if key is empty * @param {string} [key] clean nodeMap if key is empty
* @private * @private
*/ */
_deleteChildrenKeyNodes(nodeMap, keySet, key) { _deleteChildrenKeyNodes(nodeMap, key) {
if (isEmpty(key)) { if (isEmpty(key)) {
nodeMap.clear() nodeMap.clear()
keySet.clear()
} else { } else {
const mapKey = `${ConnectionType.RedisKey}/${key}` const mapKey = `${ConnectionType.RedisKey}/${key}`
const node = nodeMap.get(mapKey) const node = nodeMap.get(mapKey)
@ -1922,15 +1894,13 @@ const useBrowserStore = defineStore('browser', {
if (!nodeMap.delete(`${ConnectionType.RedisValue}/${child.redisKey}`)) { if (!nodeMap.delete(`${ConnectionType.RedisValue}/${child.redisKey}`)) {
console.warn('delete:', `${ConnectionType.RedisValue}/${child.redisKey}`) console.warn('delete:', `${ConnectionType.RedisValue}/${child.redisKey}`)
} }
keySet.delete(child.redisKeyCode || child.redisKey)
} else if (child.type === ConnectionType.RedisKey) { } else if (child.type === ConnectionType.RedisKey) {
this._deleteChildrenKeyNodes(nodeMap, keySet, child.redisKey) this._deleteChildrenKeyNodes(nodeMap, child.redisKey)
} }
} }
if (!nodeMap.delete(mapKey)) { if (!nodeMap.delete(mapKey)) {
console.warn('delete map key', mapKey) console.warn('delete map key', mapKey)
} }
keySet.delete(node.redisKeyCode || node.redisKey)
} }
}, },