diff --git a/backend/services/connection_service.go b/backend/services/connection_service.go index 217d728..2b07e5e 100644 --- a/backend/services/connection_service.go +++ b/backend/services/connection_service.go @@ -9,15 +9,16 @@ import ( "golang.org/x/crypto/ssh" "net" "os" - "slices" "strconv" "strings" "sync" "time" . "tinyrdm/backend/storage" "tinyrdm/backend/types" + "tinyrdm/backend/utils/coll" maputil "tinyrdm/backend/utils/map" redis2 "tinyrdm/backend/utils/redis" + sliceutil "tinyrdm/backend/utils/slice" strutil "tinyrdm/backend/utils/string" ) @@ -352,24 +353,28 @@ func (c *connectionService) OpenConnection(name string) (resp types.JSResp) { dbInfo := c.parseDBItemInfo(dbInfoStr) return types.ConnectionDB{ Name: dbName, + Index: idx, Keys: dbInfo["keys"], Expires: dbInfo["expires"], AvgTTL: dbInfo["avg_ttl"], } } else { return types.ConnectionDB{ - Name: dbName, + Name: dbName, + Index: idx, } } } switch selConn.DBFilterType { case "show": - for _, idx := range selConn.DBFilterList { + filterList := sliceutil.Unique(selConn.DBFilterList) + for _, idx := range filterList { dbs = append(dbs, queryDB(idx)) } case "hide": + hiddenList := coll.NewSet(selConn.DBFilterList...) for idx := 0; idx < totaldb; idx++ { - if !slices.Contains(selConn.DBFilterList, idx) { + if !hiddenList.Contains(idx) { dbs = append(dbs, queryDB(idx)) } } diff --git a/backend/types/connection.go b/backend/types/connection.go index 8333f0b..2535bd0 100644 --- a/backend/types/connection.go +++ b/backend/types/connection.go @@ -35,6 +35,7 @@ type ConnectionGroup struct { type ConnectionDB struct { Name string `json:"name"` + Index int `json:"index"` Keys int `json:"keys"` Expires int `json:"expires,omitempty"` AvgTTL int `json:"avgTtl,omitempty"` diff --git a/backend/utils/string/convert.go b/backend/utils/string/convert.go index a3bf361..aafa176 100644 --- a/backend/utils/string/convert.go +++ b/backend/utils/string/convert.go @@ -178,11 +178,11 @@ func decodeBinary(str string) (string, bool) { } func decodeHex(str string) (string, bool) { - encodeStr := hex.EncodeToString([]byte(str)) + decodeStr := hex.EncodeToString([]byte(str)) 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(encodeStr[i : i+2]) + resultStr.WriteString(decodeStr[i : i+2]) } return resultStr.String(), true } diff --git a/frontend/src/components/dialogs/ConnectionDialog.vue b/frontend/src/components/dialogs/ConnectionDialog.vue index 7f4ccac..165a4bd 100644 --- a/frontend/src/components/dialogs/ConnectionDialog.vue +++ b/frontend/src/components/dialogs/ConnectionDialog.vue @@ -460,7 +460,7 @@ const onClose = () => { :disabled="!generalForm.sentinel.enable" label-placement="top"> - + { {{ $t('dialogue.connection.sentinel.auto_discover') }} - + item.db === db) + if (selDB != null) { + return selDB + } + } + return null + }, + /** * create a new connection or update current connection profile * @param {string} name set null if create a new connection @@ -362,7 +393,7 @@ const useConnectionStore = defineStore('connections', { label: db[i].name, name: name, keys: db[i].keys, - db: i, + db: db[i].index, type: ConnectionType.RedisDB, isLeaf: false, children: undefined, @@ -490,10 +521,14 @@ const useConnectionStore = defineStore('connections', { throw new Error(msg) } const { keys = [] } = data - const dbs = this.databases[connName] - dbs[db].opened = true + const selDB = this.getDatabase(connName, db) + if (selDB == null) { + return + } + + selDB.opened = true if (isEmpty(keys)) { - dbs[db].children = [] + selDB.children = [] return } @@ -509,9 +544,12 @@ const useConnectionStore = defineStore('connections', { * @returns {Promise} */ async reopenDatabase(connName, db) { - const dbs = this.databases[connName] - dbs[db].children = undefined - dbs[db].isLeaf = false + const selDB = this.getDatabase(connName, db) + if (selDB == null) { + return + } + selDB.children = undefined + selDB.isLeaf = false this._getNodeMap(connName, db).clear() }, @@ -522,10 +560,13 @@ const useConnectionStore = defineStore('connections', { * @param db */ closeDatabase(connName, db) { - const dbs = this.databases[connName] - delete dbs[db].children - dbs[db].isLeaf = false - dbs[db].opened = false + const selDB = this.getDatabase(connName, db) + if (selDB == null) { + return + } + delete selDB.children + selDB.isLeaf = false + selDB.opened = false this._getNodeMap(connName, db).clear() }, @@ -683,12 +724,16 @@ const useConnectionStore = defineStore('connections', { return result } const separator = this._getSeparator(connName) - const dbs = this.databases[connName] - if (dbs[db].children == null) { - dbs[db].children = [] + const selDB = this.getDatabase(connName, db) + if (selDB == null) { + return result + } + + if (selDB.children == null) { + selDB.children = [] } const nodeMap = this._getNodeMap(connName, db) - const rootChildren = dbs[db].children + const rootChildren = selDB.children for (const key of keys) { const keyParts = split(key, separator) const len = size(keyParts) @@ -782,7 +827,7 @@ const useConnectionStore = defineStore('connections', { */ _tidyNode(connName, db, key, skipResort) { 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 keyParts = split(key, separator) const totalParts = size(keyParts) @@ -873,7 +918,7 @@ const useConnectionStore = defineStore('connections', { const nodeMap = this._getNodeMap(server, db) return nodeMap.get(keyPart) } else { - return this.databases[server][db] + return this.getDatabase(server, db) } }, @@ -1291,7 +1336,7 @@ const useConnectionStore = defineStore('connections', { * @private */ _deleteKeyNode(connName, db, key, isLayer) { - const dbRoot = get(this.databases, [connName, db], {}) + const dbRoot = this.getDatabase(connName, db) || {} const separator = this._getSeparator(connName) if (dbRoot == null) {