mirror of
https://github.com/fluencelabs/redis
synced 2025-03-18 16:40:50 +00:00
Merge remote-tracking branch 'upstream/unstable' into unstable
This commit is contained in:
commit
fdaab02347
14
redis.conf
14
redis.conf
@ -682,6 +682,20 @@ set-max-intset-entries 512
|
||||
zset-max-ziplist-entries 128
|
||||
zset-max-ziplist-value 64
|
||||
|
||||
# HyperLogLog sparse representation bytes limit. The limit includes the
|
||||
# 16 bytes header. When an HyperLogLog using the sparse representation crosses
|
||||
# this limit, it is convereted into the dense representation.
|
||||
#
|
||||
# A value greater than 16000 is totally useless, since at that point the
|
||||
# dense representation is more memory efficient.
|
||||
#
|
||||
# The suggested value is ~ 3000 in order to have the benefits of
|
||||
# the space efficient encoding without slowing down too much PFADD,
|
||||
# which is O(N) with the sparse encoding. Thev value can be raised to
|
||||
# ~ 10000 when CPU is not a concern, but space is, and the data set is
|
||||
# composed of many HyperLogLogs with cardinality in the 0 - 15000 range.
|
||||
hll-sparse-max-bytes 3000
|
||||
|
||||
# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in
|
||||
# order to help rehashing the main Redis hash table (the one mapping top-level
|
||||
# keys to values). The hash table implementation Redis uses (see dict.c)
|
||||
|
@ -1178,8 +1178,9 @@ void clusterUpdateSlotsConfigWith(clusterNode *sender, uint64_t senderConfigEpoc
|
||||
"I've still keys about this slot! "
|
||||
"Putting the slot in IMPORTING state. "
|
||||
"Please run the 'redis-trib fix' command.",
|
||||
j, sender->name, senderConfigEpoch,
|
||||
myself->configEpoch);
|
||||
j, sender->name,
|
||||
(unsigned long long) senderConfigEpoch,
|
||||
(unsigned long long) myself->configEpoch);
|
||||
server.cluster->importing_slots_from[j] = sender;
|
||||
}
|
||||
|
||||
|
@ -391,6 +391,8 @@ void loadServerConfigFromString(char *config) {
|
||||
server.zset_max_ziplist_entries = memtoll(argv[1], NULL);
|
||||
} else if (!strcasecmp(argv[0],"zset-max-ziplist-value") && argc == 2) {
|
||||
server.zset_max_ziplist_value = memtoll(argv[1], NULL);
|
||||
} else if (!strcasecmp(argv[0],"hll-sparse-max-bytes") && argc == 2) {
|
||||
server.hll_sparse_max_bytes = memtoll(argv[1], NULL);
|
||||
} else if (!strcasecmp(argv[0],"rename-command") && argc == 3) {
|
||||
struct redisCommand *cmd = lookupCommand(argv[1]);
|
||||
int retval;
|
||||
@ -765,6 +767,9 @@ void configSetCommand(redisClient *c) {
|
||||
} else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-value")) {
|
||||
if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
|
||||
server.zset_max_ziplist_value = ll;
|
||||
} else if (!strcasecmp(c->argv[2]->ptr,"hll-sparse-max-bytes")) {
|
||||
if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
|
||||
server.hll_sparse_max_bytes = ll;
|
||||
} else if (!strcasecmp(c->argv[2]->ptr,"lua-time-limit")) {
|
||||
if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
|
||||
server.lua_time_limit = ll;
|
||||
@ -974,6 +979,8 @@ void configGetCommand(redisClient *c) {
|
||||
server.zset_max_ziplist_entries);
|
||||
config_get_numerical_field("zset-max-ziplist-value",
|
||||
server.zset_max_ziplist_value);
|
||||
config_get_numerical_field("hll-sparse-max-bytes",
|
||||
server.hll_sparse_max_bytes);
|
||||
config_get_numerical_field("lua-time-limit",server.lua_time_limit);
|
||||
config_get_numerical_field("slowlog-log-slower-than",
|
||||
server.slowlog_log_slower_than);
|
||||
@ -1773,6 +1780,7 @@ int rewriteConfig(char *path) {
|
||||
rewriteConfigNumericalOption(state,"set-max-intset-entries",server.set_max_intset_entries,REDIS_SET_MAX_INTSET_ENTRIES);
|
||||
rewriteConfigNumericalOption(state,"zset-max-ziplist-entries",server.zset_max_ziplist_entries,REDIS_ZSET_MAX_ZIPLIST_ENTRIES);
|
||||
rewriteConfigNumericalOption(state,"zset-max-ziplist-value",server.zset_max_ziplist_value,REDIS_ZSET_MAX_ZIPLIST_VALUE);
|
||||
rewriteConfigNumericalOption(state,"hll-sparse-max-bytes",server.hll_sparse_max_bytes,REDIS_DEFAULT_HLL_SPARSE_MAX_BYTES);
|
||||
rewriteConfigYesNoOption(state,"activerehashing",server.activerehashing,REDIS_DEFAULT_ACTIVE_REHASHING);
|
||||
rewriteConfigClientoutputbufferlimitOption(state);
|
||||
rewriteConfigNumericalOption(state,"hz",server.hz,REDIS_DEFAULT_HZ);
|
||||
|
6
src/db.c
6
src/db.c
@ -1143,7 +1143,7 @@ unsigned int getKeysInSlot(unsigned int hashslot, robj **keys, unsigned int coun
|
||||
range.min = range.max = hashslot;
|
||||
range.minex = range.maxex = 0;
|
||||
|
||||
n = zslFirstInRange(server.cluster->slots_to_keys, range);
|
||||
n = zslFirstInRange(server.cluster->slots_to_keys, &range);
|
||||
while(n && n->score == hashslot && count--) {
|
||||
keys[j++] = n->obj;
|
||||
n = n->level[0].forward;
|
||||
@ -1161,7 +1161,7 @@ unsigned int countKeysInSlot(unsigned int hashslot) {
|
||||
range.minex = range.maxex = 0;
|
||||
|
||||
/* Find first element in range */
|
||||
zn = zslFirstInRange(zsl, range);
|
||||
zn = zslFirstInRange(zsl, &range);
|
||||
|
||||
/* Use rank of first element, if any, to determine preliminary count */
|
||||
if (zn != NULL) {
|
||||
@ -1169,7 +1169,7 @@ unsigned int countKeysInSlot(unsigned int hashslot) {
|
||||
count = (zsl->length - (rank - 1));
|
||||
|
||||
/* Find last element in range */
|
||||
zn = zslLastInRange(zsl, range);
|
||||
zn = zslLastInRange(zsl, &range);
|
||||
|
||||
/* Use rank of last element, if any, to determine the actual count */
|
||||
if (zn != NULL) {
|
||||
|
1170
src/hyperloglog.c
1170
src/hyperloglog.c
File diff suppressed because it is too large
Load Diff
21
src/redis.c
21
src/redis.c
@ -171,6 +171,7 @@ struct redisCommand redisCommandTable[] = {
|
||||
{"zrem",zremCommand,-3,"w",0,NULL,1,1,1,0,0},
|
||||
{"zremrangebyscore",zremrangebyscoreCommand,4,"w",0,NULL,1,1,1,0,0},
|
||||
{"zremrangebyrank",zremrangebyrankCommand,4,"w",0,NULL,1,1,1,0,0},
|
||||
{"zremrangebylex",zremrangebylexCommand,4,"w",0,NULL,1,1,1,0,0},
|
||||
{"zunionstore",zunionstoreCommand,-4,"wm",0,zunionInterGetKeys,0,0,0,0,0},
|
||||
{"zinterstore",zinterstoreCommand,-4,"wm",0,zunionInterGetKeys,0,0,0,0,0},
|
||||
{"zrange",zrangeCommand,-4,"r",0,NULL,1,1,1,0,0},
|
||||
@ -179,6 +180,7 @@ struct redisCommand redisCommandTable[] = {
|
||||
{"zrangebylex",zrangebylexCommand,-4,"r",0,NULL,1,1,1,0,0},
|
||||
{"zrevrangebylex",zrevrangebylexCommand,-4,"r",0,NULL,1,1,1,0,0},
|
||||
{"zcount",zcountCommand,4,"r",0,NULL,1,1,1,0,0},
|
||||
{"zlexcount",zlexcountCommand,4,"r",0,NULL,1,1,1,0,0},
|
||||
{"zrevrange",zrevrangeCommand,-4,"r",0,NULL,1,1,1,0,0},
|
||||
{"zcard",zcardCommand,2,"r",0,NULL,1,1,1,0,0},
|
||||
{"zscore",zscoreCommand,3,"r",0,NULL,1,1,1,0,0},
|
||||
@ -272,9 +274,9 @@ struct redisCommand redisCommandTable[] = {
|
||||
{"wait",waitCommand,3,"rs",0,NULL,0,0,0,0,0},
|
||||
{"pfselftest",pfselftestCommand,1,"r",0,NULL,0,0,0,0,0},
|
||||
{"pfadd",pfaddCommand,-2,"wm",0,NULL,1,1,1,0,0},
|
||||
{"pfcount",pfcountCommand,2,"w",0,NULL,1,1,1,0,0},
|
||||
{"pfcount",pfcountCommand,-2,"w",0,NULL,1,1,1,0,0},
|
||||
{"pfmerge",pfmergeCommand,-2,"wm",0,NULL,1,-1,1,0,0},
|
||||
{"pfgetreg",pfgetregCommand,2,"r",0,NULL,0,0,0,0,0}
|
||||
{"pfdebug",pfdebugCommand,-3,"w",0,NULL,0,0,0,0,0}
|
||||
};
|
||||
|
||||
struct evictionPoolEntry *evictionPoolAlloc(void);
|
||||
@ -1421,6 +1423,7 @@ void initServerConfig() {
|
||||
server.set_max_intset_entries = REDIS_SET_MAX_INTSET_ENTRIES;
|
||||
server.zset_max_ziplist_entries = REDIS_ZSET_MAX_ZIPLIST_ENTRIES;
|
||||
server.zset_max_ziplist_value = REDIS_ZSET_MAX_ZIPLIST_VALUE;
|
||||
server.hll_sparse_max_bytes = REDIS_DEFAULT_HLL_SPARSE_MAX_BYTES;
|
||||
server.shutdown_asap = 0;
|
||||
server.repl_ping_slave_period = REDIS_REPL_PING_SLAVE_PERIOD;
|
||||
server.repl_timeout = REDIS_REPL_TIMEOUT;
|
||||
@ -1555,24 +1558,28 @@ void adjustOpenFilesLimit(void) {
|
||||
redisLog(REDIS_WARNING,"Your current 'ulimit -n' "
|
||||
"of %llu is not enough for Redis to start. "
|
||||
"Please increase your open file limit to at least "
|
||||
"%llu. Exiting.", oldlimit, maxfiles);
|
||||
"%llu. Exiting.",
|
||||
(unsigned long long) oldlimit,
|
||||
(unsigned long long) maxfiles);
|
||||
exit(1);
|
||||
}
|
||||
redisLog(REDIS_WARNING,"You requested maxclients of %d "
|
||||
"requiring at least %llu max file descriptors.",
|
||||
old_maxclients, maxfiles);
|
||||
old_maxclients,
|
||||
(unsigned long long) maxfiles);
|
||||
redisLog(REDIS_WARNING,"Redis can't set maximum open files "
|
||||
"to %llu because of OS error: %s.",
|
||||
maxfiles, strerror(setrlimit_error));
|
||||
(unsigned long long) maxfiles, strerror(setrlimit_error));
|
||||
redisLog(REDIS_WARNING,"Current maximum open files is %llu. "
|
||||
"maxclients has been reduced to %d to compensate for "
|
||||
"low ulimit. "
|
||||
"If you need higher maxclients increase 'ulimit -n'.",
|
||||
oldlimit, server.maxclients);
|
||||
(unsigned long long) oldlimit, server.maxclients);
|
||||
} else {
|
||||
redisLog(REDIS_NOTICE,"Increased maximum number of open files "
|
||||
"to %llu (it was originally set to %llu).",
|
||||
maxfiles, oldlimit);
|
||||
(unsigned long long) maxfiles,
|
||||
(unsigned long long) oldlimit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
12
src/redis.h
12
src/redis.h
@ -312,6 +312,9 @@
|
||||
#define REDIS_ZSET_MAX_ZIPLIST_ENTRIES 128
|
||||
#define REDIS_ZSET_MAX_ZIPLIST_VALUE 64
|
||||
|
||||
/* HyperLogLog defines */
|
||||
#define REDIS_DEFAULT_HLL_SPARSE_MAX_BYTES 3000
|
||||
|
||||
/* Sets operations codes */
|
||||
#define REDIS_OP_UNION 0
|
||||
#define REDIS_OP_DIFF 1
|
||||
@ -809,6 +812,7 @@ struct redisServer {
|
||||
size_t set_max_intset_entries;
|
||||
size_t zset_max_ziplist_entries;
|
||||
size_t zset_max_ziplist_value;
|
||||
size_t hll_sparse_max_bytes;
|
||||
time_t unixtime; /* Unix time sampled every cron cycle. */
|
||||
long long mstime; /* Like 'unixtime' but with milliseconds resolution. */
|
||||
/* Pubsub */
|
||||
@ -1147,8 +1151,8 @@ void zslFree(zskiplist *zsl);
|
||||
zskiplistNode *zslInsert(zskiplist *zsl, double score, robj *obj);
|
||||
unsigned char *zzlInsert(unsigned char *zl, robj *ele, double score);
|
||||
int zslDelete(zskiplist *zsl, double score, robj *obj);
|
||||
zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec range);
|
||||
zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec range);
|
||||
zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec *range);
|
||||
zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec *range);
|
||||
double zzlGetScore(unsigned char *sptr);
|
||||
void zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
|
||||
void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
|
||||
@ -1396,11 +1400,13 @@ void zrevrangebyscoreCommand(redisClient *c);
|
||||
void zrangebylexCommand(redisClient *c);
|
||||
void zrevrangebylexCommand(redisClient *c);
|
||||
void zcountCommand(redisClient *c);
|
||||
void zlexcountCommand(redisClient *c);
|
||||
void zrevrangeCommand(redisClient *c);
|
||||
void zcardCommand(redisClient *c);
|
||||
void zremCommand(redisClient *c);
|
||||
void zscoreCommand(redisClient *c);
|
||||
void zremrangebyscoreCommand(redisClient *c);
|
||||
void zremrangebylexCommand(redisClient *c);
|
||||
void multiCommand(redisClient *c);
|
||||
void execCommand(redisClient *c);
|
||||
void discardCommand(redisClient *c);
|
||||
@ -1460,7 +1466,7 @@ void pfselftestCommand(redisClient *c);
|
||||
void pfaddCommand(redisClient *c);
|
||||
void pfcountCommand(redisClient *c);
|
||||
void pfmergeCommand(redisClient *c);
|
||||
void pfgetregCommand(redisClient *c);
|
||||
void pfdebugCommand(redisClient *c);
|
||||
|
||||
#if defined(__GNUC__)
|
||||
void *calloc(size_t count, size_t size) __attribute__ ((deprecated));
|
||||
|
409
src/t_zset.c
409
src/t_zset.c
@ -52,6 +52,9 @@
|
||||
#include "redis.h"
|
||||
#include <math.h>
|
||||
|
||||
static int zslLexValueGteMin(robj *value, zlexrangespec *spec);
|
||||
static int zslLexValueLteMax(robj *value, zlexrangespec *spec);
|
||||
|
||||
zskiplistNode *zslCreateNode(int level, double score, robj *obj) {
|
||||
zskiplistNode *zn = zmalloc(sizeof(*zn)+level*sizeof(struct zskiplistLevel));
|
||||
zn->score = score;
|
||||
@ -235,18 +238,18 @@ int zslIsInRange(zskiplist *zsl, zrangespec *range) {
|
||||
|
||||
/* Find the first node that is contained in the specified range.
|
||||
* Returns NULL when no element is contained in the range. */
|
||||
zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec range) {
|
||||
zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec *range) {
|
||||
zskiplistNode *x;
|
||||
int i;
|
||||
|
||||
/* If everything is out of range, return early. */
|
||||
if (!zslIsInRange(zsl,&range)) return NULL;
|
||||
if (!zslIsInRange(zsl,range)) return NULL;
|
||||
|
||||
x = zsl->header;
|
||||
for (i = zsl->level-1; i >= 0; i--) {
|
||||
/* Go forward while *OUT* of range. */
|
||||
while (x->level[i].forward &&
|
||||
!zslValueGteMin(x->level[i].forward->score,&range))
|
||||
!zslValueGteMin(x->level[i].forward->score,range))
|
||||
x = x->level[i].forward;
|
||||
}
|
||||
|
||||
@ -255,24 +258,24 @@ zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec range) {
|
||||
redisAssert(x != NULL);
|
||||
|
||||
/* Check if score <= max. */
|
||||
if (!zslValueLteMax(x->score,&range)) return NULL;
|
||||
if (!zslValueLteMax(x->score,range)) return NULL;
|
||||
return x;
|
||||
}
|
||||
|
||||
/* Find the last node that is contained in the specified range.
|
||||
* Returns NULL when no element is contained in the range. */
|
||||
zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec range) {
|
||||
zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec *range) {
|
||||
zskiplistNode *x;
|
||||
int i;
|
||||
|
||||
/* If everything is out of range, return early. */
|
||||
if (!zslIsInRange(zsl,&range)) return NULL;
|
||||
if (!zslIsInRange(zsl,range)) return NULL;
|
||||
|
||||
x = zsl->header;
|
||||
for (i = zsl->level-1; i >= 0; i--) {
|
||||
/* Go forward while *IN* range. */
|
||||
while (x->level[i].forward &&
|
||||
zslValueLteMax(x->level[i].forward->score,&range))
|
||||
zslValueLteMax(x->level[i].forward->score,range))
|
||||
x = x->level[i].forward;
|
||||
}
|
||||
|
||||
@ -280,7 +283,7 @@ zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec range) {
|
||||
redisAssert(x != NULL);
|
||||
|
||||
/* Check if score >= min. */
|
||||
if (!zslValueGteMin(x->score,&range)) return NULL;
|
||||
if (!zslValueGteMin(x->score,range)) return NULL;
|
||||
return x;
|
||||
}
|
||||
|
||||
@ -288,16 +291,16 @@ zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec range) {
|
||||
* Min and max are inclusive, so a score >= min || score <= max is deleted.
|
||||
* Note that this function takes the reference to the hash table view of the
|
||||
* sorted set, in order to remove the elements from the hash table too. */
|
||||
unsigned long zslDeleteRangeByScore(zskiplist *zsl, zrangespec range, dict *dict) {
|
||||
unsigned long zslDeleteRangeByScore(zskiplist *zsl, zrangespec *range, dict *dict) {
|
||||
zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
|
||||
unsigned long removed = 0;
|
||||
int i;
|
||||
|
||||
x = zsl->header;
|
||||
for (i = zsl->level-1; i >= 0; i--) {
|
||||
while (x->level[i].forward && (range.minex ?
|
||||
x->level[i].forward->score <= range.min :
|
||||
x->level[i].forward->score < range.min))
|
||||
while (x->level[i].forward && (range->minex ?
|
||||
x->level[i].forward->score <= range->min :
|
||||
x->level[i].forward->score < range->min))
|
||||
x = x->level[i].forward;
|
||||
update[i] = x;
|
||||
}
|
||||
@ -306,7 +309,38 @@ unsigned long zslDeleteRangeByScore(zskiplist *zsl, zrangespec range, dict *dict
|
||||
x = x->level[0].forward;
|
||||
|
||||
/* Delete nodes while in range. */
|
||||
while (x && (range.maxex ? x->score < range.max : x->score <= range.max)) {
|
||||
while (x &&
|
||||
(range->maxex ? x->score < range->max : x->score <= range->max))
|
||||
{
|
||||
zskiplistNode *next = x->level[0].forward;
|
||||
zslDeleteNode(zsl,x,update);
|
||||
dictDelete(dict,x->obj);
|
||||
zslFreeNode(x);
|
||||
removed++;
|
||||
x = next;
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
unsigned long zslDeleteRangeByLex(zskiplist *zsl, zlexrangespec *range, dict *dict) {
|
||||
zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
|
||||
unsigned long removed = 0;
|
||||
int i;
|
||||
|
||||
|
||||
x = zsl->header;
|
||||
for (i = zsl->level-1; i >= 0; i--) {
|
||||
while (x->level[i].forward &&
|
||||
!zslLexValueGteMin(x->level[i].forward->obj,range))
|
||||
x = x->level[i].forward;
|
||||
update[i] = x;
|
||||
}
|
||||
|
||||
/* Current node is the last with score < or <= min. */
|
||||
x = x->level[0].forward;
|
||||
|
||||
/* Delete nodes while in range. */
|
||||
while (x && zslLexValueLteMax(x->obj,range)) {
|
||||
zskiplistNode *next = x->level[0].forward;
|
||||
zslDeleteNode(zsl,x,update);
|
||||
dictDelete(dict,x->obj);
|
||||
@ -444,7 +478,7 @@ static int zslParseRange(robj *min, robj *max, zrangespec *spec) {
|
||||
* respectively if the item is exclusive or inclusive. REDIS_OK will be
|
||||
* returned.
|
||||
*
|
||||
* If the stirng is not a valid range REDIS_ERR is returned, and the value
|
||||
* If the string is not a valid range REDIS_ERR is returned, and the value
|
||||
* of *dest and *ex is undefined. */
|
||||
int zslParseLexRangeItem(robj *item, robj **dest, int *ex) {
|
||||
char *c = item->ptr;
|
||||
@ -475,8 +509,14 @@ int zslParseLexRangeItem(robj *item, robj **dest, int *ex) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Populate the rangespec according to the objects min and max. */
|
||||
/* Populate the rangespec according to the objects min and max.
|
||||
*
|
||||
* Return REDIS_OK on success. On error REDIS_ERR is returned.
|
||||
* When OK is returned the structure must be freed with zslFreeLexRange(),
|
||||
* otherwise no release is needed. */
|
||||
static int zslParseLexRange(robj *min, robj *max, zlexrangespec *spec) {
|
||||
/* The range can't be valid if objects are integer encoded.
|
||||
* Every item must start with ( or [. */
|
||||
if (min->encoding == REDIS_ENCODING_INT ||
|
||||
max->encoding == REDIS_ENCODING_INT) return REDIS_ERR;
|
||||
|
||||
@ -491,6 +531,13 @@ static int zslParseLexRange(robj *min, robj *max, zlexrangespec *spec) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Free a lex range structure, must be called only after zelParseLexRange()
|
||||
* populated the structure with success (REDIS_OK returned). */
|
||||
void zslFreeLexRange(zlexrangespec *spec) {
|
||||
decrRefCount(spec->min);
|
||||
decrRefCount(spec->max);
|
||||
}
|
||||
|
||||
/* This is just a wrapper to compareStringObjects() that is able to
|
||||
* handle shared.minstring and shared.maxstring as the equivalent of
|
||||
* -inf and +inf for strings */
|
||||
@ -534,18 +581,18 @@ int zslIsInLexRange(zskiplist *zsl, zlexrangespec *range) {
|
||||
|
||||
/* Find the first node that is contained in the specified lex range.
|
||||
* Returns NULL when no element is contained in the range. */
|
||||
zskiplistNode *zslFirstInLexRange(zskiplist *zsl, zlexrangespec range) {
|
||||
zskiplistNode *zslFirstInLexRange(zskiplist *zsl, zlexrangespec *range) {
|
||||
zskiplistNode *x;
|
||||
int i;
|
||||
|
||||
/* If everything is out of range, return early. */
|
||||
if (!zslIsInLexRange(zsl,&range)) return NULL;
|
||||
if (!zslIsInLexRange(zsl,range)) return NULL;
|
||||
|
||||
x = zsl->header;
|
||||
for (i = zsl->level-1; i >= 0; i--) {
|
||||
/* Go forward while *OUT* of range. */
|
||||
while (x->level[i].forward &&
|
||||
!zslLexValueGteMin(x->level[i].forward->obj,&range))
|
||||
!zslLexValueGteMin(x->level[i].forward->obj,range))
|
||||
x = x->level[i].forward;
|
||||
}
|
||||
|
||||
@ -554,24 +601,24 @@ zskiplistNode *zslFirstInLexRange(zskiplist *zsl, zlexrangespec range) {
|
||||
redisAssert(x != NULL);
|
||||
|
||||
/* Check if score <= max. */
|
||||
if (!zslLexValueLteMax(x->obj,&range)) return NULL;
|
||||
if (!zslLexValueLteMax(x->obj,range)) return NULL;
|
||||
return x;
|
||||
}
|
||||
|
||||
/* Find the last node that is contained in the specified range.
|
||||
* Returns NULL when no element is contained in the range. */
|
||||
zskiplistNode *zslLastInLexRange(zskiplist *zsl, zlexrangespec range) {
|
||||
zskiplistNode *zslLastInLexRange(zskiplist *zsl, zlexrangespec *range) {
|
||||
zskiplistNode *x;
|
||||
int i;
|
||||
|
||||
/* If everything is out of range, return early. */
|
||||
if (!zslIsInLexRange(zsl,&range)) return NULL;
|
||||
if (!zslIsInLexRange(zsl,range)) return NULL;
|
||||
|
||||
x = zsl->header;
|
||||
for (i = zsl->level-1; i >= 0; i--) {
|
||||
/* Go forward while *IN* range. */
|
||||
while (x->level[i].forward &&
|
||||
zslLexValueLteMax(x->level[i].forward->obj,&range))
|
||||
zslLexValueLteMax(x->level[i].forward->obj,range))
|
||||
x = x->level[i].forward;
|
||||
}
|
||||
|
||||
@ -579,7 +626,7 @@ zskiplistNode *zslLastInLexRange(zskiplist *zsl, zlexrangespec range) {
|
||||
redisAssert(x != NULL);
|
||||
|
||||
/* Check if score >= min. */
|
||||
if (!zslLexValueGteMin(x->obj,&range)) return NULL;
|
||||
if (!zslLexValueGteMin(x->obj,range)) return NULL;
|
||||
return x;
|
||||
}
|
||||
|
||||
@ -717,21 +764,21 @@ int zzlIsInRange(unsigned char *zl, zrangespec *range) {
|
||||
|
||||
/* Find pointer to the first element contained in the specified range.
|
||||
* Returns NULL when no element is contained in the range. */
|
||||
unsigned char *zzlFirstInRange(unsigned char *zl, zrangespec range) {
|
||||
unsigned char *zzlFirstInRange(unsigned char *zl, zrangespec *range) {
|
||||
unsigned char *eptr = ziplistIndex(zl,0), *sptr;
|
||||
double score;
|
||||
|
||||
/* If everything is out of range, return early. */
|
||||
if (!zzlIsInRange(zl,&range)) return NULL;
|
||||
if (!zzlIsInRange(zl,range)) return NULL;
|
||||
|
||||
while (eptr != NULL) {
|
||||
sptr = ziplistNext(zl,eptr);
|
||||
redisAssert(sptr != NULL);
|
||||
|
||||
score = zzlGetScore(sptr);
|
||||
if (zslValueGteMin(score,&range)) {
|
||||
if (zslValueGteMin(score,range)) {
|
||||
/* Check if score <= max. */
|
||||
if (zslValueLteMax(score,&range))
|
||||
if (zslValueLteMax(score,range))
|
||||
return eptr;
|
||||
return NULL;
|
||||
}
|
||||
@ -745,21 +792,21 @@ unsigned char *zzlFirstInRange(unsigned char *zl, zrangespec range) {
|
||||
|
||||
/* Find pointer to the last element contained in the specified range.
|
||||
* Returns NULL when no element is contained in the range. */
|
||||
unsigned char *zzlLastInRange(unsigned char *zl, zrangespec range) {
|
||||
unsigned char *zzlLastInRange(unsigned char *zl, zrangespec *range) {
|
||||
unsigned char *eptr = ziplistIndex(zl,-2), *sptr;
|
||||
double score;
|
||||
|
||||
/* If everything is out of range, return early. */
|
||||
if (!zzlIsInRange(zl,&range)) return NULL;
|
||||
if (!zzlIsInRange(zl,range)) return NULL;
|
||||
|
||||
while (eptr != NULL) {
|
||||
sptr = ziplistNext(zl,eptr);
|
||||
redisAssert(sptr != NULL);
|
||||
|
||||
score = zzlGetScore(sptr);
|
||||
if (zslValueLteMax(score,&range)) {
|
||||
if (zslValueLteMax(score,range)) {
|
||||
/* Check if score >= min. */
|
||||
if (zslValueGteMin(score,&range))
|
||||
if (zslValueGteMin(score,range))
|
||||
return eptr;
|
||||
return NULL;
|
||||
}
|
||||
@ -816,16 +863,16 @@ int zzlIsInLexRange(unsigned char *zl, zlexrangespec *range) {
|
||||
|
||||
/* Find pointer to the first element contained in the specified lex range.
|
||||
* Returns NULL when no element is contained in the range. */
|
||||
unsigned char *zzlFirstInLexRange(unsigned char *zl, zlexrangespec range) {
|
||||
unsigned char *zzlFirstInLexRange(unsigned char *zl, zlexrangespec *range) {
|
||||
unsigned char *eptr = ziplistIndex(zl,0), *sptr;
|
||||
|
||||
/* If everything is out of range, return early. */
|
||||
if (!zzlIsInLexRange(zl,&range)) return NULL;
|
||||
if (!zzlIsInLexRange(zl,range)) return NULL;
|
||||
|
||||
while (eptr != NULL) {
|
||||
if (zzlLexValueGteMin(eptr,&range)) {
|
||||
if (zzlLexValueGteMin(eptr,range)) {
|
||||
/* Check if score <= max. */
|
||||
if (zzlLexValueLteMax(eptr,&range))
|
||||
if (zzlLexValueLteMax(eptr,range))
|
||||
return eptr;
|
||||
return NULL;
|
||||
}
|
||||
@ -841,16 +888,16 @@ unsigned char *zzlFirstInLexRange(unsigned char *zl, zlexrangespec range) {
|
||||
|
||||
/* Find pointer to the last element contained in the specified lex range.
|
||||
* Returns NULL when no element is contained in the range. */
|
||||
unsigned char *zzlLastInLexRange(unsigned char *zl, zlexrangespec range) {
|
||||
unsigned char *zzlLastInLexRange(unsigned char *zl, zlexrangespec *range) {
|
||||
unsigned char *eptr = ziplistIndex(zl,-2), *sptr;
|
||||
|
||||
/* If everything is out of range, return early. */
|
||||
if (!zzlIsInLexRange(zl,&range)) return NULL;
|
||||
if (!zzlIsInLexRange(zl,range)) return NULL;
|
||||
|
||||
while (eptr != NULL) {
|
||||
if (zzlLexValueLteMax(eptr,&range)) {
|
||||
if (zzlLexValueLteMax(eptr,range)) {
|
||||
/* Check if score >= min. */
|
||||
if (zzlLexValueGteMin(eptr,&range))
|
||||
if (zzlLexValueGteMin(eptr,range))
|
||||
return eptr;
|
||||
return NULL;
|
||||
}
|
||||
@ -964,7 +1011,7 @@ unsigned char *zzlInsert(unsigned char *zl, robj *ele, double score) {
|
||||
return zl;
|
||||
}
|
||||
|
||||
unsigned char *zzlDeleteRangeByScore(unsigned char *zl, zrangespec range, unsigned long *deleted) {
|
||||
unsigned char *zzlDeleteRangeByScore(unsigned char *zl, zrangespec *range, unsigned long *deleted) {
|
||||
unsigned char *eptr, *sptr;
|
||||
double score;
|
||||
unsigned long num = 0;
|
||||
@ -978,7 +1025,34 @@ unsigned char *zzlDeleteRangeByScore(unsigned char *zl, zrangespec range, unsign
|
||||
* byte and ziplistNext will return NULL. */
|
||||
while ((sptr = ziplistNext(zl,eptr)) != NULL) {
|
||||
score = zzlGetScore(sptr);
|
||||
if (zslValueLteMax(score,&range)) {
|
||||
if (zslValueLteMax(score,range)) {
|
||||
/* Delete both the element and the score. */
|
||||
zl = ziplistDelete(zl,&eptr);
|
||||
zl = ziplistDelete(zl,&eptr);
|
||||
num++;
|
||||
} else {
|
||||
/* No longer in range. */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (deleted != NULL) *deleted = num;
|
||||
return zl;
|
||||
}
|
||||
|
||||
unsigned char *zzlDeleteRangeByLex(unsigned char *zl, zlexrangespec *range, unsigned long *deleted) {
|
||||
unsigned char *eptr, *sptr;
|
||||
unsigned long num = 0;
|
||||
|
||||
if (deleted != NULL) *deleted = 0;
|
||||
|
||||
eptr = zzlFirstInLexRange(zl,range);
|
||||
if (eptr == NULL) return zl;
|
||||
|
||||
/* When the tail of the ziplist is deleted, eptr will point to the sentinel
|
||||
* byte and ziplistNext will return NULL. */
|
||||
while ((sptr = ziplistNext(zl,eptr)) != NULL) {
|
||||
if (zzlLexValueLteMax(eptr,range)) {
|
||||
/* Delete both the element and the score. */
|
||||
zl = ziplistDelete(zl,&eptr);
|
||||
zl = ziplistDelete(zl,&eptr);
|
||||
@ -1300,31 +1374,86 @@ void zremCommand(redisClient *c) {
|
||||
addReplyLongLong(c,deleted);
|
||||
}
|
||||
|
||||
void zremrangebyscoreCommand(redisClient *c) {
|
||||
/* Implements ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZREMRANGEBYLEX commands. */
|
||||
#define ZRANGE_RANK 0
|
||||
#define ZRANGE_SCORE 1
|
||||
#define ZRANGE_LEX 2
|
||||
void zremrangeGenericCommand(redisClient *c, int rangetype) {
|
||||
robj *key = c->argv[1];
|
||||
robj *zobj;
|
||||
zrangespec range;
|
||||
int keyremoved = 0;
|
||||
unsigned long deleted;
|
||||
zrangespec range;
|
||||
zlexrangespec lexrange;
|
||||
long start, end, llen;
|
||||
|
||||
/* Parse the range arguments. */
|
||||
if (zslParseRange(c->argv[2],c->argv[3],&range) != REDIS_OK) {
|
||||
addReplyError(c,"min or max is not a float");
|
||||
return;
|
||||
/* Step 1: Parse the range. */
|
||||
if (rangetype == ZRANGE_RANK) {
|
||||
if ((getLongFromObjectOrReply(c,c->argv[2],&start,NULL) != REDIS_OK) ||
|
||||
(getLongFromObjectOrReply(c,c->argv[3],&end,NULL) != REDIS_OK))
|
||||
return;
|
||||
} else if (rangetype == ZRANGE_SCORE) {
|
||||
if (zslParseRange(c->argv[2],c->argv[3],&range) != REDIS_OK) {
|
||||
addReplyError(c,"min or max is not a float");
|
||||
return;
|
||||
}
|
||||
} else if (rangetype == ZRANGE_LEX) {
|
||||
if (zslParseLexRange(c->argv[2],c->argv[3],&lexrange) != REDIS_OK) {
|
||||
addReplyError(c,"min or max not valid string range item");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* Step 2: Lookup & range sanity checks if needed. */
|
||||
if ((zobj = lookupKeyWriteOrReply(c,key,shared.czero)) == NULL ||
|
||||
checkType(c,zobj,REDIS_ZSET)) return;
|
||||
checkType(c,zobj,REDIS_ZSET)) goto cleanup;
|
||||
|
||||
if (rangetype == ZRANGE_RANK) {
|
||||
/* Sanitize indexes. */
|
||||
llen = zsetLength(zobj);
|
||||
if (start < 0) start = llen+start;
|
||||
if (end < 0) end = llen+end;
|
||||
if (start < 0) start = 0;
|
||||
|
||||
/* Invariant: start >= 0, so this test will be true when end < 0.
|
||||
* The range is empty when start > end or start >= length. */
|
||||
if (start > end || start >= llen) {
|
||||
addReply(c,shared.czero);
|
||||
goto cleanup;
|
||||
}
|
||||
if (end >= llen) end = llen-1;
|
||||
}
|
||||
|
||||
/* Step 3: Perform the range deletion operation. */
|
||||
if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
|
||||
zobj->ptr = zzlDeleteRangeByScore(zobj->ptr,range,&deleted);
|
||||
switch(rangetype) {
|
||||
case ZRANGE_RANK:
|
||||
zobj->ptr = zzlDeleteRangeByRank(zobj->ptr,start+1,end+1,&deleted);
|
||||
break;
|
||||
case ZRANGE_SCORE:
|
||||
zobj->ptr = zzlDeleteRangeByScore(zobj->ptr,&range,&deleted);
|
||||
break;
|
||||
case ZRANGE_LEX:
|
||||
zobj->ptr = zzlDeleteRangeByLex(zobj->ptr,&lexrange,&deleted);
|
||||
break;
|
||||
}
|
||||
if (zzlLength(zobj->ptr) == 0) {
|
||||
dbDelete(c->db,key);
|
||||
keyremoved = 1;
|
||||
}
|
||||
} else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {
|
||||
zset *zs = zobj->ptr;
|
||||
deleted = zslDeleteRangeByScore(zs->zsl,range,zs->dict);
|
||||
switch(rangetype) {
|
||||
case ZRANGE_RANK:
|
||||
deleted = zslDeleteRangeByRank(zs->zsl,start+1,end+1,zs->dict);
|
||||
break;
|
||||
case ZRANGE_SCORE:
|
||||
deleted = zslDeleteRangeByScore(zs->zsl,&range,zs->dict);
|
||||
break;
|
||||
case ZRANGE_LEX:
|
||||
deleted = zslDeleteRangeByLex(zs->zsl,&lexrange,zs->dict);
|
||||
break;
|
||||
}
|
||||
if (htNeedsResize(zs->dict)) dictResize(zs->dict);
|
||||
if (dictSize(zs->dict) == 0) {
|
||||
dbDelete(c->db,key);
|
||||
@ -1334,74 +1463,31 @@ void zremrangebyscoreCommand(redisClient *c) {
|
||||
redisPanic("Unknown sorted set encoding");
|
||||
}
|
||||
|
||||
/* Step 4: Notifications and reply. */
|
||||
if (deleted) {
|
||||
char *event[3] = {"zremrangebyrank","zremrangebyscore","zremrangebylex"};
|
||||
signalModifiedKey(c->db,key);
|
||||
notifyKeyspaceEvent(REDIS_NOTIFY_ZSET,"zrembyscore",key,c->db->id);
|
||||
notifyKeyspaceEvent(REDIS_NOTIFY_ZSET,event[rangetype],key,c->db->id);
|
||||
if (keyremoved)
|
||||
notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",key,c->db->id);
|
||||
}
|
||||
server.dirty += deleted;
|
||||
addReplyLongLong(c,deleted);
|
||||
|
||||
cleanup:
|
||||
if (rangetype == ZRANGE_LEX) zslFreeLexRange(&lexrange);
|
||||
}
|
||||
|
||||
void zremrangebyrankCommand(redisClient *c) {
|
||||
robj *key = c->argv[1];
|
||||
robj *zobj;
|
||||
long start;
|
||||
long end;
|
||||
int llen;
|
||||
unsigned long deleted;
|
||||
int keyremoved = 0;
|
||||
zremrangeGenericCommand(c,ZRANGE_RANK);
|
||||
}
|
||||
|
||||
if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != REDIS_OK) ||
|
||||
(getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != REDIS_OK)) return;
|
||||
void zremrangebyscoreCommand(redisClient *c) {
|
||||
zremrangeGenericCommand(c,ZRANGE_SCORE);
|
||||
}
|
||||
|
||||
if ((zobj = lookupKeyWriteOrReply(c,key,shared.czero)) == NULL ||
|
||||
checkType(c,zobj,REDIS_ZSET)) return;
|
||||
|
||||
/* Sanitize indexes. */
|
||||
llen = zsetLength(zobj);
|
||||
if (start < 0) start = llen+start;
|
||||
if (end < 0) end = llen+end;
|
||||
if (start < 0) start = 0;
|
||||
|
||||
/* Invariant: start >= 0, so this test will be true when end < 0.
|
||||
* The range is empty when start > end or start >= length. */
|
||||
if (start > end || start >= llen) {
|
||||
addReply(c,shared.czero);
|
||||
return;
|
||||
}
|
||||
if (end >= llen) end = llen-1;
|
||||
|
||||
if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
|
||||
/* Correct for 1-based rank. */
|
||||
zobj->ptr = zzlDeleteRangeByRank(zobj->ptr,start+1,end+1,&deleted);
|
||||
if (zzlLength(zobj->ptr) == 0) {
|
||||
dbDelete(c->db,key);
|
||||
keyremoved = 1;
|
||||
}
|
||||
} else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {
|
||||
zset *zs = zobj->ptr;
|
||||
|
||||
/* Correct for 1-based rank. */
|
||||
deleted = zslDeleteRangeByRank(zs->zsl,start+1,end+1,zs->dict);
|
||||
if (htNeedsResize(zs->dict)) dictResize(zs->dict);
|
||||
if (dictSize(zs->dict) == 0) {
|
||||
dbDelete(c->db,key);
|
||||
keyremoved = 1;
|
||||
}
|
||||
} else {
|
||||
redisPanic("Unknown sorted set encoding");
|
||||
}
|
||||
|
||||
if (deleted) {
|
||||
signalModifiedKey(c->db,key);
|
||||
notifyKeyspaceEvent(REDIS_NOTIFY_ZSET,"zrembyrank",key,c->db->id);
|
||||
if (keyremoved)
|
||||
notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",key,c->db->id);
|
||||
}
|
||||
server.dirty += deleted;
|
||||
addReplyLongLong(c,deleted);
|
||||
void zremrangebylexCommand(redisClient *c) {
|
||||
zremrangeGenericCommand(c,ZRANGE_LEX);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
@ -2147,9 +2233,9 @@ void genericZrangebyscoreCommand(redisClient *c, int reverse) {
|
||||
|
||||
/* If reversed, get the last node in range as starting point. */
|
||||
if (reverse) {
|
||||
eptr = zzlLastInRange(zl,range);
|
||||
eptr = zzlLastInRange(zl,&range);
|
||||
} else {
|
||||
eptr = zzlFirstInRange(zl,range);
|
||||
eptr = zzlFirstInRange(zl,&range);
|
||||
}
|
||||
|
||||
/* No "first" element in the specified interval. */
|
||||
@ -2215,9 +2301,9 @@ void genericZrangebyscoreCommand(redisClient *c, int reverse) {
|
||||
|
||||
/* If reversed, get the last node in range as starting point. */
|
||||
if (reverse) {
|
||||
ln = zslLastInRange(zsl,range);
|
||||
ln = zslLastInRange(zsl,&range);
|
||||
} else {
|
||||
ln = zslFirstInRange(zsl,range);
|
||||
ln = zslFirstInRange(zsl,&range);
|
||||
}
|
||||
|
||||
/* No "first" element in the specified interval. */
|
||||
@ -2304,7 +2390,7 @@ void zcountCommand(redisClient *c) {
|
||||
double score;
|
||||
|
||||
/* Use the first element in range as the starting point */
|
||||
eptr = zzlFirstInRange(zl,range);
|
||||
eptr = zzlFirstInRange(zl,&range);
|
||||
|
||||
/* No "first" element */
|
||||
if (eptr == NULL) {
|
||||
@ -2336,7 +2422,7 @@ void zcountCommand(redisClient *c) {
|
||||
unsigned long rank;
|
||||
|
||||
/* Find first element in range */
|
||||
zn = zslFirstInRange(zsl, range);
|
||||
zn = zslFirstInRange(zsl, &range);
|
||||
|
||||
/* Use rank of first element, if any, to determine preliminary count */
|
||||
if (zn != NULL) {
|
||||
@ -2344,7 +2430,7 @@ void zcountCommand(redisClient *c) {
|
||||
count = (zsl->length - (rank - 1));
|
||||
|
||||
/* Find last element in range */
|
||||
zn = zslLastInRange(zsl, range);
|
||||
zn = zslLastInRange(zsl, &range);
|
||||
|
||||
/* Use rank of last element, if any, to determine the actual count */
|
||||
if (zn != NULL) {
|
||||
@ -2359,6 +2445,85 @@ void zcountCommand(redisClient *c) {
|
||||
addReplyLongLong(c, count);
|
||||
}
|
||||
|
||||
void zlexcountCommand(redisClient *c) {
|
||||
robj *key = c->argv[1];
|
||||
robj *zobj;
|
||||
zlexrangespec range;
|
||||
int count = 0;
|
||||
|
||||
/* Parse the range arguments */
|
||||
if (zslParseLexRange(c->argv[2],c->argv[3],&range) != REDIS_OK) {
|
||||
addReplyError(c,"min or max not valid string range item");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Lookup the sorted set */
|
||||
if ((zobj = lookupKeyReadOrReply(c, key, shared.czero)) == NULL ||
|
||||
checkType(c, zobj, REDIS_ZSET))
|
||||
{
|
||||
zslFreeLexRange(&range);
|
||||
return;
|
||||
}
|
||||
|
||||
if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
|
||||
unsigned char *zl = zobj->ptr;
|
||||
unsigned char *eptr, *sptr;
|
||||
|
||||
/* Use the first element in range as the starting point */
|
||||
eptr = zzlFirstInLexRange(zl,&range);
|
||||
|
||||
/* No "first" element */
|
||||
if (eptr == NULL) {
|
||||
zslFreeLexRange(&range);
|
||||
addReply(c, shared.czero);
|
||||
return;
|
||||
}
|
||||
|
||||
/* First element is in range */
|
||||
sptr = ziplistNext(zl,eptr);
|
||||
redisAssertWithInfo(c,zobj,zzlLexValueLteMax(eptr,&range));
|
||||
|
||||
/* Iterate over elements in range */
|
||||
while (eptr) {
|
||||
/* Abort when the node is no longer in range. */
|
||||
if (!zzlLexValueLteMax(eptr,&range)) {
|
||||
break;
|
||||
} else {
|
||||
count++;
|
||||
zzlNext(zl,&eptr,&sptr);
|
||||
}
|
||||
}
|
||||
} else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {
|
||||
zset *zs = zobj->ptr;
|
||||
zskiplist *zsl = zs->zsl;
|
||||
zskiplistNode *zn;
|
||||
unsigned long rank;
|
||||
|
||||
/* Find first element in range */
|
||||
zn = zslFirstInLexRange(zsl, &range);
|
||||
|
||||
/* Use rank of first element, if any, to determine preliminary count */
|
||||
if (zn != NULL) {
|
||||
rank = zslGetRank(zsl, zn->score, zn->obj);
|
||||
count = (zsl->length - (rank - 1));
|
||||
|
||||
/* Find last element in range */
|
||||
zn = zslLastInLexRange(zsl, &range);
|
||||
|
||||
/* Use rank of last element, if any, to determine the actual count */
|
||||
if (zn != NULL) {
|
||||
rank = zslGetRank(zsl, zn->score, zn->obj);
|
||||
count -= (zsl->length - rank);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
redisPanic("Unknown sorted set encoding");
|
||||
}
|
||||
|
||||
zslFreeLexRange(&range);
|
||||
addReplyLongLong(c, count);
|
||||
}
|
||||
|
||||
/* This command implements ZRANGEBYLEX, ZREVRANGEBYLEX. */
|
||||
void genericZrangebylexCommand(redisClient *c, int reverse) {
|
||||
zlexrangespec range;
|
||||
@ -2395,6 +2560,7 @@ void genericZrangebylexCommand(redisClient *c, int reverse) {
|
||||
(getLongFromObjectOrReply(c, c->argv[pos+2], &limit, NULL) != REDIS_OK)) return;
|
||||
pos += 3; remaining -= 3;
|
||||
} else {
|
||||
zslFreeLexRange(&range);
|
||||
addReply(c,shared.syntaxerr);
|
||||
return;
|
||||
}
|
||||
@ -2403,7 +2569,11 @@ void genericZrangebylexCommand(redisClient *c, int reverse) {
|
||||
|
||||
/* Ok, lookup the key and get the range */
|
||||
if ((zobj = lookupKeyReadOrReply(c,key,shared.emptymultibulk)) == NULL ||
|
||||
checkType(c,zobj,REDIS_ZSET)) return;
|
||||
checkType(c,zobj,REDIS_ZSET))
|
||||
{
|
||||
zslFreeLexRange(&range);
|
||||
return;
|
||||
}
|
||||
|
||||
if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
|
||||
unsigned char *zl = zobj->ptr;
|
||||
@ -2414,14 +2584,15 @@ void genericZrangebylexCommand(redisClient *c, int reverse) {
|
||||
|
||||
/* If reversed, get the last node in range as starting point. */
|
||||
if (reverse) {
|
||||
eptr = zzlLastInLexRange(zl,range);
|
||||
eptr = zzlLastInLexRange(zl,&range);
|
||||
} else {
|
||||
eptr = zzlFirstInLexRange(zl,range);
|
||||
eptr = zzlFirstInLexRange(zl,&range);
|
||||
}
|
||||
|
||||
/* No "first" element in the specified interval. */
|
||||
if (eptr == NULL) {
|
||||
addReply(c, shared.emptymultibulk);
|
||||
zslFreeLexRange(&range);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -2477,14 +2648,15 @@ void genericZrangebylexCommand(redisClient *c, int reverse) {
|
||||
|
||||
/* If reversed, get the last node in range as starting point. */
|
||||
if (reverse) {
|
||||
ln = zslLastInLexRange(zsl,range);
|
||||
ln = zslLastInLexRange(zsl,&range);
|
||||
} else {
|
||||
ln = zslFirstInLexRange(zsl,range);
|
||||
ln = zslFirstInLexRange(zsl,&range);
|
||||
}
|
||||
|
||||
/* No "first" element in the specified interval. */
|
||||
if (ln == NULL) {
|
||||
addReply(c, shared.emptymultibulk);
|
||||
zslFreeLexRange(&range);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -2525,6 +2697,7 @@ void genericZrangebylexCommand(redisClient *c, int reverse) {
|
||||
redisPanic("Unknown sorted set encoding");
|
||||
}
|
||||
|
||||
zslFreeLexRange(&range);
|
||||
setDeferredMultiBulkLength(c, replylen, rangelen);
|
||||
}
|
||||
|
||||
|
@ -39,6 +39,82 @@ start_server {tags {"hll"}} {
|
||||
set res
|
||||
} {5 10}
|
||||
|
||||
test {HyperLogLogs are promote from sparse to dense} {
|
||||
r del hll
|
||||
r config set hll-sparse-max-bytes 3000
|
||||
set n 0
|
||||
while {$n < 100000} {
|
||||
set elements {}
|
||||
for {set j 0} {$j < 100} {incr j} {lappend elements [expr rand()]}
|
||||
incr n 100
|
||||
r pfadd hll {*}$elements
|
||||
set card [r pfcount hll]
|
||||
set err [expr {abs($card-$n)}]
|
||||
assert {$err < (double($card)/100)*5}
|
||||
if {$n < 1000} {
|
||||
assert {[r pfdebug encoding hll] eq {sparse}}
|
||||
} elseif {$n > 10000} {
|
||||
assert {[r pfdebug encoding hll] eq {dense}}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test {HyperLogLog sparse encoding stress test} {
|
||||
for {set x 0} {$x < 1000} {incr x} {
|
||||
r del hll1 hll2
|
||||
set numele [randomInt 100]
|
||||
set elements {}
|
||||
for {set j 0} {$j < $numele} {incr j} {
|
||||
lappend elements [expr rand()]
|
||||
}
|
||||
# Force dense representation of hll2
|
||||
r pfadd hll2
|
||||
r pfdebug todense hll2
|
||||
r pfadd hll1 {*}$elements
|
||||
r pfadd hll2 {*}$elements
|
||||
assert {[r pfdebug encoding hll1] eq {sparse}}
|
||||
assert {[r pfdebug encoding hll2] eq {dense}}
|
||||
# Cardinality estimated should match exactly.
|
||||
assert {[r pfcount hll1] eq [r pfcount hll2]}
|
||||
}
|
||||
}
|
||||
|
||||
test {Corrupted sparse HyperLogLogs are detected: Additionl at tail} {
|
||||
r del hll
|
||||
r pfadd hll a b c
|
||||
r append hll "hello"
|
||||
set e {}
|
||||
catch {r pfcount hll} e
|
||||
set e
|
||||
} {*INVALIDOBJ*}
|
||||
|
||||
test {Corrupted sparse HyperLogLogs are detected: Broken magic} {
|
||||
r del hll
|
||||
r pfadd hll a b c
|
||||
r setrange hll 0 "0123"
|
||||
set e {}
|
||||
catch {r pfcount hll} e
|
||||
set e
|
||||
} {*WRONGTYPE*}
|
||||
|
||||
test {Corrupted sparse HyperLogLogs are detected: Invalid encoding} {
|
||||
r del hll
|
||||
r pfadd hll a b c
|
||||
r setrange hll 4 "x"
|
||||
set e {}
|
||||
catch {r pfcount hll} e
|
||||
set e
|
||||
} {*WRONGTYPE*}
|
||||
|
||||
test {Corrupted dense HyperLogLogs are detected: Wrong length} {
|
||||
r del hll
|
||||
r pfadd hll a b c
|
||||
r setrange hll 4 "\x00"
|
||||
set e {}
|
||||
catch {r pfcount hll} e
|
||||
set e
|
||||
} {*WRONGTYPE*}
|
||||
|
||||
test {PFADD, PFCOUNT, PFMERGE type checking works} {
|
||||
r set foo bar
|
||||
catch {r pfadd foo 1} e
|
||||
@ -60,9 +136,24 @@ start_server {tags {"hll"}} {
|
||||
r pfcount hll
|
||||
} {5}
|
||||
|
||||
test {PFGETREG returns the HyperLogLog raw registers} {
|
||||
test {PFCOUNT multiple-keys merge returns cardinality of union} {
|
||||
r del hll1 hll2 hll3
|
||||
for {set x 1} {$x < 10000} {incr x} {
|
||||
# Force dense representation of hll2
|
||||
r pfadd hll1 "foo-$x"
|
||||
r pfadd hll2 "bar-$x"
|
||||
r pfadd hll3 "zap-$x"
|
||||
|
||||
set card [r pfcount hll1 hll2 hll3]
|
||||
set realcard [expr {$x*3}]
|
||||
set err [expr {abs($card-$realcard)}]
|
||||
assert {$err < (double($card)/100)*5}
|
||||
}
|
||||
}
|
||||
|
||||
test {PFDEBUG GETREG returns the HyperLogLog raw registers} {
|
||||
r del hll
|
||||
r pfadd hll 1 2 3
|
||||
llength [r pfgetreg hll]
|
||||
llength [r pfdebug getreg hll]
|
||||
} {16384}
|
||||
}
|
||||
|
@ -296,6 +296,62 @@ start_server {tags {"zset"}} {
|
||||
assert_error "*not*float*" {r zrangebyscore fooz 1 NaN}
|
||||
}
|
||||
|
||||
proc create_default_lex_zset {} {
|
||||
create_zset zset {0 alpha 0 bar 0 cool 0 down
|
||||
0 elephant 0 foo 0 great 0 hill
|
||||
0 omega}
|
||||
}
|
||||
|
||||
test "ZRANGEBYLEX/ZREVRANGEBYLEX/ZCOUNT basics" {
|
||||
create_default_lex_zset
|
||||
|
||||
# inclusive range
|
||||
assert_equal {alpha bar cool} [r zrangebylex zset - \[cool]
|
||||
assert_equal {bar cool down} [r zrangebylex zset \[bar \[down]
|
||||
assert_equal {great hill omega} [r zrangebylex zset \[g +]
|
||||
assert_equal {cool bar alpha} [r zrevrangebylex zset \[cool -]
|
||||
assert_equal {down cool bar} [r zrevrangebylex zset \[down \[bar]
|
||||
assert_equal {omega hill great foo elephant down} [r zrevrangebylex zset + \[d]
|
||||
assert_equal 3 [r zlexcount zset \[ele \[h]
|
||||
|
||||
# exclusive range
|
||||
assert_equal {alpha bar} [r zrangebylex zset - (cool]
|
||||
assert_equal {cool} [r zrangebylex zset (bar (down]
|
||||
assert_equal {hill omega} [r zrangebylex zset (great +]
|
||||
assert_equal {bar alpha} [r zrevrangebylex zset (cool -]
|
||||
assert_equal {cool} [r zrevrangebylex zset (down (bar]
|
||||
assert_equal {omega hill} [r zrevrangebylex zset + (great]
|
||||
assert_equal 2 [r zlexcount zset (ele (great]
|
||||
|
||||
# inclusive and exclusive
|
||||
assert_equal {} [r zrangebylex zset (az (b]
|
||||
assert_equal {} [r zrangebylex zset (z +]
|
||||
assert_equal {} [r zrangebylex zset - \[aaaa]
|
||||
assert_equal {} [r zrevrangebylex zset \[elez \[elex]
|
||||
assert_equal {} [r zrevrangebylex zset (hill (omega]
|
||||
}
|
||||
|
||||
test "ZRANGEBYSLEX with LIMIT" {
|
||||
create_default_lex_zset
|
||||
assert_equal {alpha bar} [r zrangebylex zset - \[cool LIMIT 0 2]
|
||||
assert_equal {bar cool} [r zrangebylex zset - \[cool LIMIT 1 2]
|
||||
assert_equal {} [r zrangebylex zset \[bar \[down LIMIT 0 0]
|
||||
assert_equal {} [r zrangebylex zset \[bar \[down LIMIT 2 0]
|
||||
assert_equal {bar} [r zrangebylex zset \[bar \[down LIMIT 0 1]
|
||||
assert_equal {cool} [r zrangebylex zset \[bar \[down LIMIT 1 1]
|
||||
assert_equal {bar cool down} [r zrangebylex zset \[bar \[down LIMIT 0 100]
|
||||
assert_equal {omega hill great foo elephant} [r zrevrangebylex zset + \[d LIMIT 0 5]
|
||||
assert_equal {omega hill great foo} [r zrevrangebylex zset + \[d LIMIT 0 4]
|
||||
}
|
||||
|
||||
test "ZRANGEBYLEX with invalid lex range specifiers" {
|
||||
assert_error "*not*string*" {r zrangebylex fooz foo bar}
|
||||
assert_error "*not*string*" {r zrangebylex fooz \[foo bar}
|
||||
assert_error "*not*string*" {r zrangebylex fooz foo \[bar}
|
||||
assert_error "*not*string*" {r zrangebylex fooz +x \[bar}
|
||||
assert_error "*not*string*" {r zrangebylex fooz -x \[bar}
|
||||
}
|
||||
|
||||
test "ZREMRANGEBYSCORE basics" {
|
||||
proc remrangebyscore {min max} {
|
||||
create_zset zset {1 a 2 b 3 c 4 d 5 e}
|
||||
@ -708,6 +764,111 @@ start_server {tags {"zset"}} {
|
||||
assert_equal {} $err
|
||||
}
|
||||
|
||||
test "ZRANGEBYLEX fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
|
||||
set lexset {}
|
||||
r del zset
|
||||
for {set j 0} {$j < $elements} {incr j} {
|
||||
set e [randstring 0 30 alpha]
|
||||
lappend lexset $e
|
||||
r zadd zset 0 $e
|
||||
}
|
||||
set lexset [lsort -unique $lexset]
|
||||
for {set j 0} {$j < 100} {incr j} {
|
||||
set min [randstring 0 30 alpha]
|
||||
set max [randstring 0 30 alpha]
|
||||
set mininc [randomInt 2]
|
||||
set maxinc [randomInt 2]
|
||||
if {$mininc} {set cmin "\[$min"} else {set cmin "($min"}
|
||||
if {$maxinc} {set cmax "\[$max"} else {set cmax "($max"}
|
||||
set rev [randomInt 2]
|
||||
if {$rev} {
|
||||
set cmd zrevrangebylex
|
||||
} else {
|
||||
set cmd zrangebylex
|
||||
}
|
||||
|
||||
# Make sure data is the same in both sides
|
||||
assert {[r zrange zset 0 -1] eq $lexset}
|
||||
|
||||
# Get the Redis output
|
||||
set output [r $cmd zset $cmin $cmax]
|
||||
if {$rev} {
|
||||
set outlen [r zlexcount zset $cmax $cmin]
|
||||
} else {
|
||||
set outlen [r zlexcount zset $cmin $cmax]
|
||||
}
|
||||
|
||||
# Compute the same output via Tcl
|
||||
set o {}
|
||||
set copy $lexset
|
||||
if {(!$rev && [string compare $min $max] > 0) ||
|
||||
($rev && [string compare $max $min] > 0)} {
|
||||
# Empty output when ranges are inverted.
|
||||
} else {
|
||||
if {$rev} {
|
||||
# Invert the Tcl array using Redis itself.
|
||||
set copy [r zrevrange zset 0 -1]
|
||||
# Invert min / max as well
|
||||
lassign [list $min $max $mininc $maxinc] \
|
||||
max min maxinc mininc
|
||||
}
|
||||
foreach e $copy {
|
||||
set mincmp [string compare $e $min]
|
||||
set maxcmp [string compare $e $max]
|
||||
if {
|
||||
($mininc && $mincmp >= 0 || !$mininc && $mincmp > 0)
|
||||
&&
|
||||
($maxinc && $maxcmp <= 0 || !$maxinc && $maxcmp < 0)
|
||||
} {
|
||||
lappend o $e
|
||||
}
|
||||
}
|
||||
}
|
||||
assert {$o eq $output}
|
||||
assert {$outlen eq [llength $output]}
|
||||
}
|
||||
}
|
||||
|
||||
test "ZREMRANGEBYLEX fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
|
||||
set lexset {}
|
||||
r del zset zsetcopy
|
||||
for {set j 0} {$j < $elements} {incr j} {
|
||||
set e [randstring 0 30 alpha]
|
||||
lappend lexset $e
|
||||
r zadd zset 0 $e
|
||||
}
|
||||
set lexset [lsort -unique $lexset]
|
||||
for {set j 0} {$j < 100} {incr j} {
|
||||
# Copy...
|
||||
r zunionstore zsetcopy 1 zset
|
||||
set lexsetcopy $lexset
|
||||
|
||||
set min [randstring 0 30 alpha]
|
||||
set max [randstring 0 30 alpha]
|
||||
set mininc [randomInt 2]
|
||||
set maxinc [randomInt 2]
|
||||
if {$mininc} {set cmin "\[$min"} else {set cmin "($min"}
|
||||
if {$maxinc} {set cmax "\[$max"} else {set cmax "($max"}
|
||||
|
||||
# Make sure data is the same in both sides
|
||||
assert {[r zrange zset 0 -1] eq $lexset}
|
||||
|
||||
# Get the range we are going to remove
|
||||
set torem [r zrangebylex zset $cmin $cmax]
|
||||
set toremlen [r zlexcount zset $cmin $cmax]
|
||||
r zremrangebylex zsetcopy $cmin $cmax
|
||||
set output [r zrange zsetcopy 0 -1]
|
||||
|
||||
# Remove the range with Tcl from the original list
|
||||
if {$toremlen} {
|
||||
set first [lsearch -exact $lexsetcopy [lindex $torem 0]]
|
||||
set last [expr {$first+$toremlen-1}]
|
||||
set lexsetcopy [lreplace $lexsetcopy $first $last]
|
||||
}
|
||||
assert {$lexsetcopy eq $output}
|
||||
}
|
||||
}
|
||||
|
||||
test "ZSETs skiplist implementation backlink consistency test - $encoding" {
|
||||
set diff 0
|
||||
for {set j 0} {$j < $elements} {incr j} {
|
||||
|
Loading…
x
Reference in New Issue
Block a user