Add char* typeNameCanonicalize(robj*) to remove duplicate code between SCAN and TYPE commands,

and to keep OBJ_* enum to string canonicalization in one place.
This commit is contained in:
Angus Pearson 2019-06-10 17:41:44 +01:00
parent bf963253ec
commit e2adea2188
2 changed files with 17 additions and 26 deletions

View File

@ -766,26 +766,8 @@ void scanGenericCommand(client *c, robj *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;
}
@ -845,11 +827,8 @@ void lastsaveCommand(client *c) {
addReplyLongLong(c,server.lastsave);
}
void typeCommand(client *c) {
robj *o;
char* typeNameCanonicalize(robj *o) {
char* type;
o = lookupKeyReadWithFlags(c->db,c->argv[1],LOOKUP_NOTOUCH);
if (o == NULL) {
type = "none";
} else {
@ -867,7 +846,13 @@ void typeCommand(client *c) {
default: type = "unknown"; break;
}
}
addReplyStatus(c,type);
return type;
}
void typeCommand(client *c) {
robj *o;
o = lookupKeyReadWithFlags(c->db,c->argv[1],LOOKUP_NOTOUCH);
addReplyStatus(c, typeNameCanonicalize(o));
}
void shutdownCommand(client *c) {

View File

@ -646,6 +646,12 @@ typedef struct redisObject {
void *ptr;
} robj;
/* 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