Hash: HSTRLEN (was HVSTRLEN) improved.

1. HVSTRLEN -> HSTRLEN. It's unlikely one needs the length of the key,
   not clear how the API would work (by value does not make sense) and
   there will be better names anyway.
2. Default is to return 0 when field is missing.
3. Default is to return 0 when key is missing.
4. The implementation was slower than needed, and produced unnecessary COW.

Related issue #2415.
This commit is contained in:
antirez
2015-02-27 15:30:15 +01:00
parent 8855b8161f
commit 4e54b85a19
4 changed files with 17 additions and 24 deletions

View File

@ -202,7 +202,7 @@ struct redisCommand redisCommandTable[] = {
{"hincrbyfloat",hincrbyfloatCommand,4,"wmF",0,NULL,1,1,1,0,0},
{"hdel",hdelCommand,-3,"wF",0,NULL,1,1,1,0,0},
{"hlen",hlenCommand,2,"rF",0,NULL,1,1,1,0,0},
{"hvstrlen",hvstrlenCommand,3,"rF",0,NULL,1,1,1,0,0},
{"hstrlen",hstrlenCommand,3,"rF",0,NULL,1,1,1,0,0},
{"hkeys",hkeysCommand,2,"rS",0,NULL,1,1,1,0,0},
{"hvals",hvalsCommand,2,"rS",0,NULL,1,1,1,0,0},
{"hgetall",hgetallCommand,2,"r",0,NULL,1,1,1,0,0},

View File

@ -1516,7 +1516,7 @@ void hmsetCommand(redisClient *c);
void hmgetCommand(redisClient *c);
void hdelCommand(redisClient *c);
void hlenCommand(redisClient *c);
void hvstrlenCommand(redisClient *c);
void hstrlenCommand(redisClient *c);
void zremrangebyrankCommand(redisClient *c);
void zunionstoreCommand(redisClient *c);
void zinterstoreCommand(redisClient *c);

View File

@ -701,24 +701,19 @@ void hdelCommand(redisClient *c) {
void hlenCommand(redisClient *c) {
robj *o;
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
checkType(c,o,REDIS_HASH)) return;
addReplyLongLong(c,hashTypeLength(o));
}
void hvstrlenCommand(redisClient *c) {
void hstrlenCommand(redisClient *c) {
robj *o;
robj *value;
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
checkType(c,o,REDIS_HASH)) return;
if ((value = hashTypeGetObject(o,c->argv[2])) == NULL) {
addReply(c, shared.nullbulk);
} else {
addReplyLongLong(c,stringObjectLen(value));
decrRefCount(value);
}
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
checkType(c,o,REDIS_HASH)) return;
addReplyLongLong(c,hashTypeGetValueLength(o,c->argv[2]));
}
static void addHashIteratorCursorToReply(redisClient *c, hashTypeIterator *hi, int what) {