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:
parent
bf963253ec
commit
e2adea2188
37
src/db.c
37
src/db.c
@ -766,26 +766,8 @@ void scanGenericCommand(client *c, robj *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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -845,11 +827,8 @@ void lastsaveCommand(client *c) {
|
|||||||
addReplyLongLong(c,server.lastsave);
|
addReplyLongLong(c,server.lastsave);
|
||||||
}
|
}
|
||||||
|
|
||||||
void typeCommand(client *c) {
|
char* typeNameCanonicalize(robj *o) {
|
||||||
robj *o;
|
char* type;
|
||||||
char *type;
|
|
||||||
|
|
||||||
o = lookupKeyReadWithFlags(c->db,c->argv[1],LOOKUP_NOTOUCH);
|
|
||||||
if (o == NULL) {
|
if (o == NULL) {
|
||||||
type = "none";
|
type = "none";
|
||||||
} else {
|
} else {
|
||||||
@ -867,7 +846,13 @@ void typeCommand(client *c) {
|
|||||||
default: type = "unknown"; break;
|
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) {
|
void shutdownCommand(client *c) {
|
||||||
|
@ -646,6 +646,12 @@ typedef struct redisObject {
|
|||||||
void *ptr;
|
void *ptr;
|
||||||
} robj;
|
} 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.
|
/* 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