Add char* typeNameCanonicalize(robj*) to remove duplicate code between SCAN and TYPE commands,
and to keep OBJ_* enum to string canonicalization in one place. Former-commit-id: 3cdc6e8d846e88cf4e250b2643662bde2a9317c5
This commit is contained in:
parent
f84ee30d7d
commit
7b615e7274
37
src/db.cpp
37
src/db.cpp
@ -818,26 +818,8 @@ void scanGenericCommand(client *c, robj_roptr o, unsigned long cursor) {
|
|||||||
|
|
||||||
/* Filter an element if it isn't the type we want. */
|
/* Filter an element if it isn't the type we want. */
|
||||||
if (!filter && o == NULL && typename){
|
if (!filter && o == NULL && typename){
|
||||||
robj* typecheck;
|
robj* typecheck = lookupKeyReadWithFlags(c->db, kobj, LOOKUP_NOTOUCH);
|
||||||
char *type;
|
char* type = typeNameCanonicalize(typecheck);
|
||||||
typecheck = lookupKeyReadWithFlags(c->db, kobj, LOOKUP_NOTOUCH);
|
|
||||||
if (typecheck == NULL) {
|
|
||||||
type = "none";
|
|
||||||
} else {
|
|
||||||
switch(typecheck->type) {
|
|
||||||
case OBJ_STRING: type = "string"; break;
|
|
||||||
case OBJ_LIST: type = "list"; break;
|
|
||||||
case OBJ_SET: type = "set"; break;
|
|
||||||
case OBJ_ZSET: type = "zset"; break;
|
|
||||||
case OBJ_HASH: type = "hash"; break;
|
|
||||||
case OBJ_STREAM: type = "stream"; break;
|
|
||||||
case OBJ_MODULE: {
|
|
||||||
moduleValue *mv = typecheck->ptr;
|
|
||||||
type = mv->type->name;
|
|
||||||
}; break;
|
|
||||||
default: type = "unknown"; break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (strcasecmp((char*) typename, type)) filter = 1;
|
if (strcasecmp((char*) typename, type)) filter = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -897,11 +879,9 @@ void lastsaveCommand(client *c) {
|
|||||||
addReplyLongLong(c,g_pserver->lastsave);
|
addReplyLongLong(c,g_pserver->lastsave);
|
||||||
}
|
}
|
||||||
|
|
||||||
void typeCommand(client *c) {
|
const char* typeNameCanonicalize(robj *o) {
|
||||||
const char *type;
|
const char* type;
|
||||||
|
if (o == NULL) {
|
||||||
robj_roptr o = lookupKeyReadWithFlags(c->db,c->argv[1],LOOKUP_NOTOUCH);
|
|
||||||
if (o == nullptr) {
|
|
||||||
type = "none";
|
type = "none";
|
||||||
} else {
|
} else {
|
||||||
switch(o->type) {
|
switch(o->type) {
|
||||||
@ -918,7 +898,12 @@ void typeCommand(client *c) {
|
|||||||
default: type = "unknown"; break;
|
default: type = "unknown"; break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
addReplyStatus(c,type);
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
void typeCommand(client *c) {
|
||||||
|
robj_roptr o = lookupKeyReadWithFlags(c->db,c->argv[1],LOOKUP_NOTOUCH);
|
||||||
|
addReplyStatus(c, typeNameCanonicalize(o));
|
||||||
}
|
}
|
||||||
|
|
||||||
void shutdownCommand(client *c) {
|
void shutdownCommand(client *c) {
|
||||||
|
@ -721,7 +721,6 @@ typedef struct redisObject {
|
|||||||
void *m_ptr;
|
void *m_ptr;
|
||||||
} robj;
|
} robj;
|
||||||
|
|
||||||
|
|
||||||
__attribute__((always_inline)) inline const void *ptrFromObj(robj_roptr &o)
|
__attribute__((always_inline)) inline const void *ptrFromObj(robj_roptr &o)
|
||||||
{
|
{
|
||||||
if (o->encoding == OBJ_ENCODING_EMBSTR)
|
if (o->encoding == OBJ_ENCODING_EMBSTR)
|
||||||
@ -746,6 +745,13 @@ __attribute__((always_inline)) inline char *szFromObj(const robj *o)
|
|||||||
return (char*)ptrFromObj(o);
|
return (char*)ptrFromObj(o);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* The 'cannonical' name for a type as enumerated above is given by the
|
||||||
|
* below function. Native types are checked against the OBJ_STRING,
|
||||||
|
* OBJ_LIST, OBJ_* defines, and Module types have their registered name
|
||||||
|
* returned.*/
|
||||||
|
char* typeNameCanonicalize(robj*);
|
||||||
|
|
||||||
/* Macro used to initialize a Redis object allocated on the stack.
|
/* Macro used to initialize a Redis object allocated on the stack.
|
||||||
* Note that this macro is taken near the structure definition to make sure
|
* Note that this macro is taken near the structure definition to make sure
|
||||||
* we'll update it when the structure is changed, to avoid bugs like
|
* we'll update it when the structure is changed, to avoid bugs like
|
||||||
|
Loading…
x
Reference in New Issue
Block a user