summaryrefslogtreecommitdiff
path: root/src/t_zset.c
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2015-06-22 17:26:36 +0200
committerantirez <antirez@gmail.com>2015-06-22 17:26:36 +0200
commit9fc47ddf0b8174e5e652ba11bc3d368f6536ba40 (patch)
tree35c14faab6d266ec14349cebe5e1b03768aa63d7 /src/t_zset.c
parent575e247a0e91d662163fd92032b47e7a3b1f2b6b (diff)
downloadredis-9fc47ddf0b8174e5e652ba11bc3d368f6536ba40.tar.gz
Geo: zsetScore refactoring
Now used both in geo.c and t_zset to provide ZSCORE.
Diffstat (limited to 'src/t_zset.c')
-rw-r--r--src/t_zset.c41
1 files changed, 23 insertions, 18 deletions
diff --git a/src/t_zset.c b/src/t_zset.c
index b900a9ccb..93808a871 100644
--- a/src/t_zset.c
+++ b/src/t_zset.c
@@ -1166,6 +1166,26 @@ void zsetConvert(robj *zobj, int encoding) {
}
}
+/* Return (by reference) the score of the specified member of the sorted set
+ * storing it into *score. If the element does not exist REDIS_ERR is returned
+ * otherwise REDIS_OK is returned and *score is correctly populated.
+ * If 'zobj' or 'member' is NULL, REDIS_ERR is returned. */
+int zsetScore(robj *zobj, robj *member, double *score) {
+ if (!zobj || !member) return REDIS_ERR;
+
+ if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
+ if (zzlFind(zobj->ptr, member, score) == NULL) return REDIS_ERR;
+ } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {
+ zset *zs = zobj->ptr;
+ dictEntry *de = dictFind(zs->dict, member);
+ if (de == NULL) return REDIS_ERR;
+ *score = *(double*)dictGetVal(de);
+ } else {
+ redisPanic("Unknown sorted set encoding");
+ }
+ return REDIS_OK;
+}
+
/*-----------------------------------------------------------------------------
* Sorted set commands
*----------------------------------------------------------------------------*/
@@ -2815,25 +2835,10 @@ void zscoreCommand(redisClient *c) {
if ((zobj = lookupKeyReadOrReply(c,key,shared.nullbulk)) == NULL ||
checkType(c,zobj,REDIS_ZSET)) return;
- if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
- if (zzlFind(zobj->ptr,c->argv[2],&score) != NULL)
- addReplyDouble(c,score);
- else
- addReply(c,shared.nullbulk);
- } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {
- zset *zs = zobj->ptr;
- dictEntry *de;
-
- c->argv[2] = tryObjectEncoding(c->argv[2]);
- de = dictFind(zs->dict,c->argv[2]);
- if (de != NULL) {
- score = *(double*)dictGetVal(de);
- addReplyDouble(c,score);
- } else {
- addReply(c,shared.nullbulk);
- }
+ if (zsetScore(zobj,c->argv[2],&score) == REDIS_ERR) {
+ addReply(c,shared.nullbulk);
} else {
- redisPanic("Unknown sorted set encoding");
+ addReplyDouble(c,score);
}
}