fix: incorrect key count after clear the database #36

This commit is contained in:
tiny-craft 2023-09-28 17:05:54 +08:00
parent 8e8f62073e
commit 3de2a47d12
1 changed files with 60 additions and 50 deletions

View File

@ -749,10 +749,10 @@ const useConnectionStore = defineStore('connections', {
*/ */
_tidyNode(connName, db, key, skipResort) { _tidyNode(connName, db, key, skipResort) {
const nodeMap = this._getNodeMap(connName, db) const nodeMap = this._getNodeMap(connName, db)
const dbNode = get(this.databases, [connName, db], {})
const separator = this._getSeparator(connName) const separator = this._getSeparator(connName)
const keyParts = split(key, separator) const keyParts = split(key, separator)
const totalParts = size(keyParts) const totalParts = size(keyParts)
const dbNode = get(this.databases, [connName, db], {})
let node let node
// find last exists ancestor key // find last exists ancestor key
let i = totalParts - 1 let i = totalParts - 1
@ -1252,7 +1252,7 @@ const useConnectionStore = defineStore('connections', {
* *
* @param {string} connName * @param {string} connName
* @param {number} db * @param {number} db
* @param {string} key * @param {string} [key]
* @param {boolean} [isLayer] * @param {boolean} [isLayer]
* @private * @private
*/ */
@ -1265,11 +1265,16 @@ const useConnectionStore = defineStore('connections', {
} }
const nodeMap = this._getNodeMap(connName, db) const nodeMap = this._getNodeMap(connName, db)
const keyParts = split(key, separator)
const totalParts = size(keyParts)
if (isLayer === true) { if (isLayer === true) {
this._deleteChildrenKeyNodes(nodeMap, key) this._deleteChildrenKeyNodes(nodeMap, key)
} }
if (isEmpty(key)) {
// clear all key nodes
dbRoot.children = []
dbRoot.keys = 0
} else {
const keyParts = split(key, separator)
const totalParts = size(keyParts)
// remove from parent in tree node // remove from parent in tree node
const parentKey = slice(keyParts, 0, totalParts - 1) const parentKey = slice(keyParts, 0, totalParts - 1)
let parentNode let parentNode
@ -1307,17 +1312,21 @@ const useConnectionStore = defineStore('connections', {
remove(dbRoot.children, { type: ConnectionType.RedisKey, redisKey: keyParts[0] }) remove(dbRoot.children, { type: ConnectionType.RedisKey, redisKey: keyParts[0] })
} }
} }
}
return true return true
}, },
/** /**
* delete node and all it's children from nodeMap * delete node and all it's children from nodeMap
* @param nodeMap * @param {Map<string, DatabaseItem>} nodeMap
* @param key * @param {string} [key] clean nodeMap if key is empty
* @private * @private
*/ */
_deleteChildrenKeyNodes(nodeMap, key) { _deleteChildrenKeyNodes(nodeMap, key) {
if (isEmpty(key)) {
nodeMap.clear()
} else {
const mapKey = `${ConnectionType.RedisKey}/${key}` const mapKey = `${ConnectionType.RedisKey}/${key}`
const node = nodeMap.get(mapKey) const node = nodeMap.get(mapKey)
for (const child of node.children || []) { for (const child of node.children || []) {
@ -1332,6 +1341,7 @@ const useConnectionStore = defineStore('connections', {
if (!nodeMap.delete(mapKey)) { if (!nodeMap.delete(mapKey)) {
console.warn('delete map key', mapKey) console.warn('delete map key', mapKey)
} }
}
}, },
/** /**