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:
Angus Pearson 2019-06-10 17:41:44 +01:00 committed by John Sully
parent f84ee30d7d
commit 7b615e7274
2 changed files with 18 additions and 27 deletions

View File

@ -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. */
if (!filter && o == NULL && typename){
robj* typecheck;
char *type;
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;
}
}
robj* typecheck = lookupKeyReadWithFlags(c->db, kobj, LOOKUP_NOTOUCH);
char* type = typeNameCanonicalize(typecheck);
if (strcasecmp((char*) typename, type)) filter = 1;
}
@ -897,11 +879,9 @@ void lastsaveCommand(client *c) {
addReplyLongLong(c,g_pserver->lastsave);
}
void typeCommand(client *c) {
const char *type;
robj_roptr o = lookupKeyReadWithFlags(c->db,c->argv[1],LOOKUP_NOTOUCH);
if (o == nullptr) {
const char* typeNameCanonicalize(robj *o) {
const char* type;
if (o == NULL) {
type = "none";
} else {
switch(o->type) {
@ -918,7 +898,12 @@ void typeCommand(client *c) {
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) {

View File

@ -721,7 +721,6 @@ typedef struct redisObject {
void *m_ptr;
} robj;
__attribute__((always_inline)) inline const void *ptrFromObj(robj_roptr &o)
{
if (o->encoding == OBJ_ENCODING_EMBSTR)
@ -746,6 +745,13 @@ __attribute__((always_inline)) inline char *szFromObj(const robj *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.
* 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