fix: database index confused after setting database filter

This commit is contained in:
tiny-craft 2023-10-08 18:25:52 +08:00
parent b5dfe377fa
commit 30e7016aa3
5 changed files with 79 additions and 28 deletions

View File

@ -9,15 +9,16 @@ import (
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
"net" "net"
"os" "os"
"slices"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
. "tinyrdm/backend/storage" . "tinyrdm/backend/storage"
"tinyrdm/backend/types" "tinyrdm/backend/types"
"tinyrdm/backend/utils/coll"
maputil "tinyrdm/backend/utils/map" maputil "tinyrdm/backend/utils/map"
redis2 "tinyrdm/backend/utils/redis" redis2 "tinyrdm/backend/utils/redis"
sliceutil "tinyrdm/backend/utils/slice"
strutil "tinyrdm/backend/utils/string" strutil "tinyrdm/backend/utils/string"
) )
@ -352,24 +353,28 @@ func (c *connectionService) OpenConnection(name string) (resp types.JSResp) {
dbInfo := c.parseDBItemInfo(dbInfoStr) dbInfo := c.parseDBItemInfo(dbInfoStr)
return types.ConnectionDB{ return types.ConnectionDB{
Name: dbName, Name: dbName,
Index: idx,
Keys: dbInfo["keys"], Keys: dbInfo["keys"],
Expires: dbInfo["expires"], Expires: dbInfo["expires"],
AvgTTL: dbInfo["avg_ttl"], AvgTTL: dbInfo["avg_ttl"],
} }
} else { } else {
return types.ConnectionDB{ return types.ConnectionDB{
Name: dbName, Name: dbName,
Index: idx,
} }
} }
} }
switch selConn.DBFilterType { switch selConn.DBFilterType {
case "show": case "show":
for _, idx := range selConn.DBFilterList { filterList := sliceutil.Unique(selConn.DBFilterList)
for _, idx := range filterList {
dbs = append(dbs, queryDB(idx)) dbs = append(dbs, queryDB(idx))
} }
case "hide": case "hide":
hiddenList := coll.NewSet(selConn.DBFilterList...)
for idx := 0; idx < totaldb; idx++ { for idx := 0; idx < totaldb; idx++ {
if !slices.Contains(selConn.DBFilterList, idx) { if !hiddenList.Contains(idx) {
dbs = append(dbs, queryDB(idx)) dbs = append(dbs, queryDB(idx))
} }
} }

View File

@ -35,6 +35,7 @@ type ConnectionGroup struct {
type ConnectionDB struct { type ConnectionDB struct {
Name string `json:"name"` Name string `json:"name"`
Index int `json:"index"`
Keys int `json:"keys"` Keys int `json:"keys"`
Expires int `json:"expires,omitempty"` Expires int `json:"expires,omitempty"`
AvgTTL int `json:"avgTtl,omitempty"` AvgTTL int `json:"avgTtl,omitempty"`

View File

@ -178,11 +178,11 @@ func decodeBinary(str string) (string, bool) {
} }
func decodeHex(str string) (string, bool) { func decodeHex(str string) (string, bool) {
encodeStr := hex.EncodeToString([]byte(str)) decodeStr := hex.EncodeToString([]byte(str))
var resultStr strings.Builder var resultStr strings.Builder
for i := 0; i < len(encodeStr); i += 2 { for i := 0; i < len(decodeStr); i += 2 {
resultStr.WriteString("\\x") resultStr.WriteString("\\x")
resultStr.WriteString(encodeStr[i : i+2]) resultStr.WriteString(decodeStr[i : i+2])
} }
return resultStr.String(), true return resultStr.String(), true
} }

View File

@ -460,7 +460,7 @@ const onClose = () => {
:disabled="!generalForm.sentinel.enable" :disabled="!generalForm.sentinel.enable"
label-placement="top"> label-placement="top">
<n-form-item :label="$t('dialogue.connection.sentinel.master')"> <n-form-item :label="$t('dialogue.connection.sentinel.master')">
<n-space> <n-input-group>
<n-select <n-select
v-model:value="generalForm.sentinel.master" v-model:value="generalForm.sentinel.master"
filterable filterable
@ -469,7 +469,7 @@ const onClose = () => {
<n-button :loading="loadingSentinelMaster" @click="onLoadSentinelMasters"> <n-button :loading="loadingSentinelMaster" @click="onLoadSentinelMasters">
{{ $t('dialogue.connection.sentinel.auto_discover') }} {{ $t('dialogue.connection.sentinel.auto_discover') }}
</n-button> </n-button>
</n-space> </n-input-group>
</n-form-item> </n-form-item>
<n-form-item :label="$t('dialogue.connection.sentinel.password')"> <n-form-item :label="$t('dialogue.connection.sentinel.password')">
<n-input <n-input

View File

@ -1,5 +1,19 @@
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { endsWith, get, isEmpty, join, remove, size, slice, sortedIndexBy, split, sumBy, toUpper, uniq } from 'lodash' import {
endsWith,
find,
get,
isEmpty,
join,
remove,
size,
slice,
sortedIndexBy,
split,
sumBy,
toUpper,
uniq,
} from 'lodash'
import { import {
AddHashField, AddHashField,
AddListItem, AddListItem,
@ -271,6 +285,23 @@ const useConnectionStore = defineStore('connections', {
return null return null
}, },
/**
* get database by server name and index
* @param {string} connName
* @param {number} db
* @return {{}|null}
*/
getDatabase(connName, db) {
const dbs = this.databases[connName]
if (dbs != null) {
const selDB = find(dbs, (item) => item.db === db)
if (selDB != null) {
return selDB
}
}
return null
},
/** /**
* create a new connection or update current connection profile * create a new connection or update current connection profile
* @param {string} name set null if create a new connection * @param {string} name set null if create a new connection
@ -362,7 +393,7 @@ const useConnectionStore = defineStore('connections', {
label: db[i].name, label: db[i].name,
name: name, name: name,
keys: db[i].keys, keys: db[i].keys,
db: i, db: db[i].index,
type: ConnectionType.RedisDB, type: ConnectionType.RedisDB,
isLeaf: false, isLeaf: false,
children: undefined, children: undefined,
@ -490,10 +521,14 @@ const useConnectionStore = defineStore('connections', {
throw new Error(msg) throw new Error(msg)
} }
const { keys = [] } = data const { keys = [] } = data
const dbs = this.databases[connName] const selDB = this.getDatabase(connName, db)
dbs[db].opened = true if (selDB == null) {
return
}
selDB.opened = true
if (isEmpty(keys)) { if (isEmpty(keys)) {
dbs[db].children = [] selDB.children = []
return return
} }
@ -509,9 +544,12 @@ const useConnectionStore = defineStore('connections', {
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async reopenDatabase(connName, db) { async reopenDatabase(connName, db) {
const dbs = this.databases[connName] const selDB = this.getDatabase(connName, db)
dbs[db].children = undefined if (selDB == null) {
dbs[db].isLeaf = false return
}
selDB.children = undefined
selDB.isLeaf = false
this._getNodeMap(connName, db).clear() this._getNodeMap(connName, db).clear()
}, },
@ -522,10 +560,13 @@ const useConnectionStore = defineStore('connections', {
* @param db * @param db
*/ */
closeDatabase(connName, db) { closeDatabase(connName, db) {
const dbs = this.databases[connName] const selDB = this.getDatabase(connName, db)
delete dbs[db].children if (selDB == null) {
dbs[db].isLeaf = false return
dbs[db].opened = false }
delete selDB.children
selDB.isLeaf = false
selDB.opened = false
this._getNodeMap(connName, db).clear() this._getNodeMap(connName, db).clear()
}, },
@ -683,12 +724,16 @@ const useConnectionStore = defineStore('connections', {
return result return result
} }
const separator = this._getSeparator(connName) const separator = this._getSeparator(connName)
const dbs = this.databases[connName] const selDB = this.getDatabase(connName, db)
if (dbs[db].children == null) { if (selDB == null) {
dbs[db].children = [] return result
}
if (selDB.children == null) {
selDB.children = []
} }
const nodeMap = this._getNodeMap(connName, db) const nodeMap = this._getNodeMap(connName, db)
const rootChildren = dbs[db].children const rootChildren = selDB.children
for (const key of keys) { for (const key of keys) {
const keyParts = split(key, separator) const keyParts = split(key, separator)
const len = size(keyParts) const len = size(keyParts)
@ -782,7 +827,7 @@ 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 dbNode = this.getDatabase(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)
@ -873,7 +918,7 @@ const useConnectionStore = defineStore('connections', {
const nodeMap = this._getNodeMap(server, db) const nodeMap = this._getNodeMap(server, db)
return nodeMap.get(keyPart) return nodeMap.get(keyPart)
} else { } else {
return this.databases[server][db] return this.getDatabase(server, db)
} }
}, },
@ -1291,7 +1336,7 @@ const useConnectionStore = defineStore('connections', {
* @private * @private
*/ */
_deleteKeyNode(connName, db, key, isLayer) { _deleteKeyNode(connName, db, key, isLayer) {
const dbRoot = get(this.databases, [connName, db], {}) const dbRoot = this.getDatabase(connName, db) || {}
const separator = this._getSeparator(connName) const separator = this._getSeparator(connName)
if (dbRoot == null) { if (dbRoot == null) {