mirror of
https://github.com/fluencelabs/redis
synced 2025-03-25 11:51:03 +00:00
Geo: zsetScore refactoring
Now used both in geo.c and t_zset to provide ZSCORE.
This commit is contained in:
parent
575e247a0e
commit
9fc47ddf0b
@ -1240,6 +1240,7 @@ void zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
|
|||||||
void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
|
void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
|
||||||
unsigned int zsetLength(robj *zobj);
|
unsigned int zsetLength(robj *zobj);
|
||||||
void zsetConvert(robj *zobj, int encoding);
|
void zsetConvert(robj *zobj, int encoding);
|
||||||
|
int zsetScore(robj *zobj, robj *member, double *score);
|
||||||
unsigned long zslGetRank(zskiplist *zsl, double score, robj *o);
|
unsigned long zslGetRank(zskiplist *zsl, double score, robj *o);
|
||||||
|
|
||||||
/* Core functions */
|
/* Core functions */
|
||||||
|
39
src/t_zset.c
39
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
|
* Sorted set commands
|
||||||
*----------------------------------------------------------------------------*/
|
*----------------------------------------------------------------------------*/
|
||||||
@ -2815,25 +2835,10 @@ void zscoreCommand(redisClient *c) {
|
|||||||
if ((zobj = lookupKeyReadOrReply(c,key,shared.nullbulk)) == NULL ||
|
if ((zobj = lookupKeyReadOrReply(c,key,shared.nullbulk)) == NULL ||
|
||||||
checkType(c,zobj,REDIS_ZSET)) return;
|
checkType(c,zobj,REDIS_ZSET)) return;
|
||||||
|
|
||||||
if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
|
if (zsetScore(zobj,c->argv[2],&score) == REDIS_ERR) {
|
||||||
if (zzlFind(zobj->ptr,c->argv[2],&score) != NULL)
|
|
||||||
addReplyDouble(c,score);
|
|
||||||
else
|
|
||||||
addReply(c,shared.nullbulk);
|
addReply(c,shared.nullbulk);
|
||||||
} else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {
|
} else {
|
||||||
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);
|
addReplyDouble(c,score);
|
||||||
} else {
|
|
||||||
addReply(c,shared.nullbulk);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
redisPanic("Unknown sorted set encoding");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
24
src/zset.c
24
src/zset.c
@ -12,30 +12,6 @@ int zslValueLteMax(double value, zrangespec *spec);
|
|||||||
* Direct Redis DB Interaction
|
* Direct Redis DB Interaction
|
||||||
* ==================================================================== */
|
* ==================================================================== */
|
||||||
|
|
||||||
/* zset access is mostly a copy/paste from zscoreCommand() */
|
|
||||||
int zsetScore(robj *zobj, robj *member, double *score) {
|
|
||||||
if (!zobj || !member)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
|
|
||||||
if (zzlFind(zobj->ptr, member, score) == NULL)
|
|
||||||
return 0;
|
|
||||||
} else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {
|
|
||||||
zset *zs = zobj->ptr;
|
|
||||||
dictEntry *de;
|
|
||||||
|
|
||||||
member = tryObjectEncoding(member);
|
|
||||||
de = dictFind(zs->dict, member);
|
|
||||||
if (de != NULL) {
|
|
||||||
*score = *(double *)dictGetVal(de);
|
|
||||||
} else
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Largely extracted from genericZrangebyscoreCommand() in t_zset.c */
|
/* Largely extracted from genericZrangebyscoreCommand() in t_zset.c */
|
||||||
/* The zrangebyscoreCommand expects to only operate on a live redisClient,
|
/* The zrangebyscoreCommand expects to only operate on a live redisClient,
|
||||||
* but we need results returned to us, not sent over an async socket. */
|
* but we need results returned to us, not sent over an async socket. */
|
||||||
|
@ -16,7 +16,6 @@ struct zipresult {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* Redis DB Access */
|
/* Redis DB Access */
|
||||||
int zsetScore(robj *zobj, robj *member, double *score);
|
|
||||||
list *geozrangebyscore(robj *zobj, double min, double max, int limit);
|
list *geozrangebyscore(robj *zobj, double min, double max, int limit);
|
||||||
|
|
||||||
/* New list operation: append one list to another */
|
/* New list operation: append one list to another */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user