***********************************************************************************************
/*-----------------------------------------------------------------------------
* Sorted set commands 排序集命令
*----------------------------------------------------------------------------*/
/* This generic command implements both ZADD and ZINCRBY. */
这个通用命令同时实现ZADD和ZINCRBY
void zaddGenericCommand(client *c, int flags) {
static char *nanerr = "resulting score is not a number (NaN)";
robj *key = c->argv[1];
robj *zobj;
sds ele;
double score = 0, *scores = NULL;
int j, elements;
int scoreidx = 0;
/* The following vars are used in order to track what the command actually
* did during the execution, to reply to the client and to trigger the
* notification of keyspace change. */
下面的变量被用来追踪执行过程中什么命令被真正执行,回复客户端并触发键空间变化的通知
int added = 0; /* Number of new elements added. */ 添加新元素的个数
int updated = 0; /* Number of elements with updated score. */ 更新元素的个数
int processed = 0; /* Number of elements processed, may remain zero with
options like XX. */ 处理过的元素,通过选项像XX可以保持处理过的元素个数为0
XX: 仅仅更新存在的成员,不添加新成员。
NX: 不更新存在的成员。只添加新成员
/* Parse options. At the end 'scoreidx' is set to the argument position
* of the score of the first score-element pair. */
解析选项。最后,将“scoreidx”设置为第一个score参数后的参数位置
XX: 仅更新存在的成员,不添加新成员。
NX: 不更新存在的成员。只添加新成员。
CH: 返回变更成员的数量。变更的成员是指 新增成员 和 score值更新的成员,命令指明的和之前score值相同的成员不计在内。
注意: 在通常情况下,ZADD返回值只计算新添加成员的数量。
INCR: ZADD 使用该参数与 ZINCRBY 功能一样。一次只能操作一个score-element对。
scoreidx = 2;
while(scoreidx < c->argc) {
char *opt = c->argv[scoreidx]->ptr;
if (!strcasecmp(opt,"nx")) flags |= ZADD_NX;
else if (!strcasecmp(opt,"xx")) flags |= ZADD_XX;
else if (!strcasecmp(opt,"ch")) flags |= ZADD_CH;
else if (!strcasecmp(opt,"incr")) flags |= ZADD_INCR;
else break;
scoreidx++;
}
/* Turn options into simple to check vars. */ 转化选择项 变成容易检查的变量
int incr = (flags & ZADD_INCR) != 0;
int nx = (flags & ZADD_NX) != 0;
int xx = (flags & ZADD_XX) != 0;
int ch = (flags & ZADD_CH) != 0;
/* After the options, we expect to have an even number of args, since
* we expect any number of score-element pairs. */
在选择项后, 我们期望偶数个的参数,因为预期是数字和字符串对,所以是偶数个
elements = c->argc-scoreidx;
if (elements % 2 || !elements) { 如果不是成对出现 或者 参数为空
addReply(c,shared.syntaxerr);
return;
}
elements /= 2; /* Now this holds the number of score-element pairs. */现在保存分数元素对的数量
/* Check for incompatible options. */ 检查不兼容的选项, nx 和 xx 不能同时存在
if (nx && xx) {
addReplyError(c,
"XX and NX options at the same time are not compatible");
return;
}
if (incr && elements > 1) { incr只支持一个元素对
addReplyError(c,
"INCR option supports a single increment-element pair");
return;
}
/* Start parsing all the scores, we need to emit any syntax error
* before executing additions to the sorted set, as the command should
* either execute fully or nothing at all. */
开始解析所有的分数对,我们需要在对排序集执行加法之前提示任何格式错误,因为命令要么全部执行,要么什么都不执行
scores = zmalloc(sizeof(double)*elements); 获取所有传入的参数对
for (j = 0; j < elements; j++) {
if (getDoubleFromObjectOrReply(c,c->argv[scoreidx+j*2],&scores[j],NULL)
!= C_OK) goto cleanup;
}
/* Lookup the key and create the sorted set if does not exist. */
库中检查键,如果不存在就创建有序集合
zobj = lookupKeyWrite(c->db,key);
if (zobj == NULL) {
if (xx) goto reply_to_client; /* No key + XX option: nothing to do. */
库中没有对象 并且是 XX 选择项: 不用做任何事情
createSizeTConfig("zset-max-ziplist-entries", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX,
server.zset_max_ziplist_entries, 128, INTEGER_CONFIG, NULL, NULL),
createSizeTConfig("zset-max-ziplist-value", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX,
server.zset_max_ziplist_value, 64, MEMORY_CONFIG, NULL, NULL),
if (server.zset_max_ziplist_entries == 0 ||
server.zset_max_ziplist_value < sdslen(c->argv[scoreidx+1]->ptr))
{
zobj = createZsetObject(); 跳表格式的有序集合
} else {
zobj = createZsetZiplistObject(); 压表形式的有序集合
}
dbAdd(c->db,key,zobj);
} else {
if (zobj->type != OBJ_ZSET) {
addReply(c,shared.wrongtypeerr);
goto cleanup;
}
}
for (j = 0; j < elements; j++) {
double newscore;
score = scores[j];
int retflags = flags;
ele = c->argv[scoreidx+1+j*2]->ptr;获取元素
int retval = zsetAdd(zobj, score, ele, &retflags, &newscore);
if (retval == 0) {
addReplyError(c,nanerr);
goto cleanup;
}
if (retflags & ZADD_ADDED) added++; 新增
if (retflags & ZADD_UPDATED) updated++; 更新
if (!(retflags & ZADD_NOP)) processed++; 表示处理过的元素数量
score = newscore; 新的键对应的值
}
server.dirty += (added+updated); 有变化的
reply_to_client:
if (incr) { /* ZINCRBY or INCR option. */ 单个元素的行为
if (processed)
addReplyDouble(c,score);
else
addReplyNull(c);
} else { /* ZADD. */ 多个元素的行为
addReplyLongLong(c,ch ? added+updated : added);
}
cleanup:
zfree(scores); 释放申请的数组空间
if (added || updated) { 有变化,需要通知客户端具体事件
signalModifiedKey(c,c->db,key);
notifyKeyspaceEvent(NOTIFY_ZSET,
incr ? "zincr" : "zadd", key, c->db->id);
}
}
***********************************************************************************************
命令用于将一个或多个成员元素及其分数值加入到有序集当中
void zaddCommand(client *c) {
zaddGenericCommand(c,ZADD_NONE);
}
***********************************************************************************************
命令对有序集合中指定成员的分数加上增量 increment
void zincrbyCommand(client *c) {
zaddGenericCommand(c,ZADD_INCR);
}
***********************************************************************************************
命令用于移除有序集中的一个或多个成员,不存在的成员将被忽略
void zremCommand(client *c) {
robj *key = c->argv[1];
robj *zobj;
int deleted = 0, keyremoved = 0, j;
if ((zobj = lookupKeyWriteOrReply(c,key,shared.czero)) == NULL || 库中是否存在对象 类型是不是有序集
checkType(c,zobj,OBJ_ZSET)) return;
for (j = 2; j < c->argc; j++) {
if (zsetDel(zobj,c->argv[j]->ptr)) deleted++; 成功删除 计数加1
if (zsetLength(zobj) == 0) { 如果对象没有元素了,删除库中的对象
dbDelete(c->db,key);
keyremoved = 1;
break;
}
}
if (deleted) { 有删除元素
notifyKeyspaceEvent(NOTIFY_ZSET,"zrem",key,c->db->id);
if (keyremoved) 库中的对象也被删除的情况
notifyKeyspaceEvent(NOTIFY_GENERIC,"del",key,c->db->id);
signalModifiedKey(c,c->db,key);
server.dirty += deleted;
}
addReplyLongLong(c,deleted);
}
***********************************************************************************************
/* Implements ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZREMRANGEBYLEX commands. */
实现 通过排名删除元素 通过分数删除元素 通过长度删除元素
#define ZRANGE_RANK 0
#define ZRANGE_SCORE 1
#define ZRANGE_LEX 2
void zremrangeGenericCommand(client *c, int rangetype) {
robj *key = c->argv[1];
robj *zobj;
int keyremoved = 0;
unsigned long deleted = 0;
zrangespec range;
zlexrangespec lexrange;
long start, end, llen;
/* Step 1: Parse the range. */ 步骤1 分式区间范围
if (rangetype == ZRANGE_RANK) {
if ((getLongFromObjectOrReply(c,c->argv[2],&start,NULL) != C_OK) || 获取区间的头和尾
(getLongFromObjectOrReply(c,c->argv[3],&end,NULL) != C_OK))
return;
} else if (rangetype == ZRANGE_SCORE) { 获取分数的最大最小值
if (zslParseRange(c->argv[2],c->argv[3],&range) != C_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) != C_OK) {
addReplyError(c,"min or max not valid string range item");
return;
}
}
/* Step 2: Lookup & range sanity checks if needed. */ 步骤2: 查找范围合法性检查(如果需要)
if ((zobj = lookupKeyWriteOrReply(c,key,shared.czero)) == NULL ||
checkType(c,zobj,OBJ_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.
变量start大于等于0,所以当end小于0时,测试总是成功
* The range is empty when start > end or start >= length. */
当start大于end或者start大于长度时,范围为空
if (start > end || start >= llen) {
addReply(c,shared.czero);
goto cleanup;
}
if (end >= llen) end = llen-1; 结尾好过了长度,将长度最后一个位置赋值给end
}
/* Step 3: Perform the range deletion operation. */ 步骤3,执行范围删除的操作
if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {
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) { 库中对象是否为0
dbDelete(c->db,key);
keyremoved = 1;
}
} else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {
zset *zs = zobj->ptr;
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);
keyremoved = 1;
}
} else {
serverPanic("Unknown sorted set encoding");
}
/* Step 4: Notifications and reply. */ 步骤4: 通知和回复
if (deleted) {
char *event[3] = {"zremrangebyrank","zremrangebyscore","zremrangebylex"};
signalModifiedKey(c,c->db,key);
notifyKeyspaceEvent(NOTIFY_ZSET,event[rangetype],key,c->db->id);
if (keyremoved)
notifyKeyspaceEvent(NOTIFY_GENERIC,"del",key,c->db->id);
}
server.dirty += deleted;
addReplyLongLong(c,deleted);
cleanup: 清理
if (rangetype == ZRANGE_LEX) zslFreeLexRange(&lexrange);
}
***********************************************************************************************
void zremrangebyrankCommand(client *c) {
zremrangeGenericCommand(c,ZRANGE_RANK);
}
***********************************************************************************************
void zremrangebyscoreCommand(client *c) {
zremrangeGenericCommand(c,ZRANGE_SCORE);
}
***********************************************************************************************
void zremrangebylexCommand(client *c) {
zremrangeGenericCommand(c,ZRANGE_LEX);
}
***********************************************************************************************
typedef struct {
robj *subject; 对象
int type; /* Set, sorted set */ 对象类型
int encoding; 编码
double weight;
用联合来定义不同类型的迭代器,4取1
union {
/* Set iterators. */ 集合迭代器
union _iterset {
struct {
intset *is; 整型迭代器
int ii;
} is;
struct { 字典迭代器
dict *dict;
dictIterator *di;
dictEntry *de;
} ht;
} set;
/* Sorted set iterators. */ 有序集合迭代器
union _iterzset {
struct { 压表迭代器
unsigned char *zl;
unsigned char *eptr, *sptr;
} zl;
struct { 跳表迭代器
zset *zs;
zskiplistNode *node;
} sl;
} zset;
} iter;
} zsetopsrc;
***********************************************************************************************
/* Use dirty flags for pointers that need to be cleaned up in the next
* iteration over the zsetopval. The dirty flag for the long long value is
* special, since long long values don't need cleanup. Instead, it means that
* we already checked that "ell" holds a long long, or tried to convert another
* representation into a long long value. When this was successful,
* OPVAL_VALID_LL is set as well. */
对需要在zsetopval上的下一次迭代中清除的指针使用脏标志。长整型值的脏标志是特殊的,因为长整型值不需要清除。
相反,这意味着我们已经检查了“ell”是否包含长整型值,或者尝试将另一个表示形式转换为长整型值。
当此操作成功时,OPVAL_VALID_LL也会被设置
#define OPVAL_DIRTY_SDS 1
#define OPVAL_DIRTY_LL 2
#define OPVAL_VALID_LL 4
/* Store value retrieved from the iterator. */ 保存从迭代器获取的值
typedef struct {
int flags; 标志
unsigned char _buf[32]; /* Private buffer. */ 私下缓存
sds ele; 元素
unsigned char *estr; 元素指向字符串
unsigned int elen; 元素长度
long long ell; 整型值
double score; 分数
} zsetopval;
typedef union _iterset iterset;
typedef union _iterzset iterzset;
***********************************************************************************************
初始化迭代器
void zuiInitIterator(zsetopsrc *op) {
if (op->subject == NULL)
return;
if (op->type == OBJ_SET) { 集合
iterset *it = &op->iter.set;
if (op->encoding == OBJ_ENCODING_INTSET) { 整型
it->is.is = op->subject->ptr;
it->is.ii = 0;
} else if (op->encoding == OBJ_ENCODING_HT) { hash表
it->ht.dict = op->subject->ptr;
it->ht.di = dictGetIterator(op->subject->ptr);
it->ht.de = dictNext(it->ht.di);
} else {
serverPanic("Unknown set encoding");
}
} else if (op->type == OBJ_ZSET) { 有序集合
iterzset *it = &op->iter.zset;
if (op->encoding == OBJ_ENCODING_ZIPLIST) { 压表
it->zl.zl = op->subject->ptr;
it->zl.eptr = ziplistIndex(it->zl.zl,0);
if (it->zl.eptr != NULL) {
it->zl.sptr = ziplistNext(it->zl.zl,it->zl.eptr);
serverAssert(it->zl.sptr != NULL);
}
} else if (op->encoding == OBJ_ENCODING_SKIPLIST) { 跳表
it->sl.zs = op->subject->ptr;
it->sl.node = it->sl.zs->zsl->header->level[0].forward;
} else {
serverPanic("Unknown sorted set encoding");
}
} else {
serverPanic("Unsupported type");
}
}
***********************************************************************************************
清理迭代器
void zuiClearIterator(zsetopsrc *op) {
if (op->subject == NULL) 没有对象
return;
if (op->type == OBJ_SET) { 类型为集合
iterset *it = &op->iter.set;
if (op->encoding == OBJ_ENCODING_INTSET) {
UNUSED(it); /* skip */ 整型无需处理,没有分配空间
} else if (op->encoding == OBJ_ENCODING_HT) { hash表
dictReleaseIterator(it->ht.di);
} else {
serverPanic("Unknown set encoding");
}
} else if (op->type == OBJ_ZSET) { 有序集合
iterzset *it = &op->iter.zset;
if (op->encoding == OBJ_ENCODING_ZIPLIST) {
UNUSED(it); /* skip */ 无需处理,没有分配空间
} else if (op->encoding == OBJ_ENCODING_SKIPLIST) {
UNUSED(it); /* skip */无需处理,没有分配空间
} else {
serverPanic("Unknown sorted set encoding");
}
} else {
serverPanic("Unsupported type");
}
}
***********************************************************************************************
求集合元素个数
unsigned long zuiLength(zsetopsrc *op) {
if (op->subject == NULL)
return 0;
if (op->type == OBJ_SET) {
if (op->encoding == OBJ_ENCODING_INTSET) { 整型
return intsetLen(op->subject->ptr);
} else if (op->encoding == OBJ_ENCODING_HT) { hash表
dict *ht = op->subject->ptr;
return dictSize(ht);
} else {
serverPanic("Unknown set encoding");
}
} else if (op->type == OBJ_ZSET) {
if (op->encoding == OBJ_ENCODING_ZIPLIST) { 压表
return zzlLength(op->subject->ptr);
} else if (op->encoding == OBJ_ENCODING_SKIPLIST) { 跳表
zset *zs = op->subject->ptr;
return zs->zsl->length;
} else {
serverPanic("Unknown sorted set encoding");
}
} else {
serverPanic("Unsupported type");
}
}
***********************************************************************************************
/* Check if the current value is valid. If so, store it in the passed structure
* and move to the next element. If not valid, this means we have reached the
* end of the structure and can abort. */
检查当前值是否有效。如果有效,则将其存储在传递的结构中并移动到下一个元素。
如果无效,这意味着我们已经到达结构的末尾,可以中止
获取下一个元素
int zuiNext(zsetopsrc *op, zsetopval *val) {
if (op->subject == NULL)
return 0;
if (val->flags & OPVAL_DIRTY_SDS)
sdsfree(val->ele);
memset(val,0,sizeof(zsetopval));
if (op->type == OBJ_SET) {
iterset *it = &op->iter.set;
if (op->encoding == OBJ_ENCODING_INTSET) {
int64_t ell;
if (!intsetGet(it->is.is,it->is.ii,&ell))
return 0;
val->ell = ell;
val->score = 1.0;
/* Move to next element. */移动到下一个元素
it->is.ii++;
} else if (op->encoding == OBJ_ENCODING_HT) {
if (it->ht.de == NULL)
return 0;
val->ele = dictGetKey(it->ht.de);
val->score = 1.0;
/* Move to next element. */
it->ht.de = dictNext(it->ht.di);
} else {
serverPanic("Unknown set encoding");
}
} else if (op->type == OBJ_ZSET) {
iterzset *it = &op->iter.zset;
if (op->encoding == OBJ_ENCODING_ZIPLIST) {
/* No need to check both, but better be explicit. */ 不需要检查两个,但是越早明确越好
if (it->zl.eptr == NULL || it->zl.sptr == NULL)
return 0;
serverAssert(ziplistGet(it->zl.eptr,&val->estr,&val->elen,&val->ell)); 获取值
val->score = zzlGetScore(it->zl.sptr);
/* Move to next element. */
zzlNext(it->zl.zl,&it->zl.eptr,&it->zl.sptr);
} else if (op->encoding == OBJ_ENCODING_SKIPLIST) {
if (it->sl.node == NULL)
return 0;
val->ele = it->sl.node->ele;
val->score = it->sl.node->score;
/* Move to next element. */
it->sl.node = it->sl.node->level[0].forward;
} else {
serverPanic("Unknown sorted set encoding");
}
} else {
serverPanic("Unsupported type");
}
return 1;
}
***********************************************************************************************
获取长整型值
int zuiLongLongFromValue(zsetopval *val) {
if (!(val->flags & OPVAL_DIRTY_LL)) {
val->flags |= OPVAL_DIRTY_LL;
if (val->ele != NULL) {
if (string2ll(val->ele,sdslen(val->ele),&val->ell))
val->flags |= OPVAL_VALID_LL;
} else if (val->estr != NULL) {
if (string2ll((char*)val->estr,val->elen,&val->ell))
val->flags |= OPVAL_VALID_LL;
} else {
/* The long long was already set, flag as valid. */ long long已设置,标志为有效
val->flags |= OPVAL_VALID_LL;
}
}
return val->flags & OPVAL_VALID_LL;
}
***********************************************************************************************
获取字符串值
sds zuiSdsFromValue(zsetopval *val) {
if (val->ele == NULL) {
if (val->estr != NULL) {
val->ele = sdsnewlen((char*)val->estr,val->elen);
} else {
val->ele = sdsfromlonglong(val->ell);
}
val->flags |= OPVAL_DIRTY_SDS;
}
return val->ele;
}
***********************************************************************************************
/* This is different from zuiSdsFromValue since returns a new SDS string
* which is up to the caller to free. */
这与zuiSdsFromValue不同,因为它返回一个新的SDS字符串,由调用者释放
返回一个新创建的字符串
sds zuiNewSdsFromValue(zsetopval *val) {
if (val->flags & OPVAL_DIRTY_SDS) {
/* We have already one to return! */ 我们已经返回了一个字符串
sds ele = val->ele;
val->flags &= ~OPVAL_DIRTY_SDS;
val->ele = NULL;
return ele;
} else if (val->ele) {
return sdsdup(val->ele); 新建字符串
} else if (val->estr) {
return sdsnewlen((char*)val->estr,val->elen);
} else {
return sdsfromlonglong(val->ell);
}
}
***********************************************************************************************
将字符串放到缓存中
int zuiBufferFromValue(zsetopval *val) {
if (val->estr == NULL) {
if (val->ele != NULL) {
val->elen = sdslen(val->ele);
val->estr = (unsigned char*)val->ele;
} else {
val->elen = ll2string((char*)val->_buf,sizeof(val->_buf),val->ell);
val->estr = val->_buf;
}
}
return 1;
}
***********************************************************************************************
/* Find value pointed to by val in the source pointer to by op. When found,
* return 1 and store its score in target. Return 0 otherwise. */
在由op指向的源指针中查找由val指向的值。找到后,返回1并将其分数存储在目标中。否则返回0
int zuiFind(zsetopsrc *op, zsetopval *val, double *score) {
if (op->subject == NULL)
return 0;
if (op->type == OBJ_SET) {
if (op->encoding == OBJ_ENCODING_INTSET) {
if (zuiLongLongFromValue(val) &&
intsetFind(op->subject->ptr,val->ell))
{
*score = 1.0;
return 1;
} else {
return 0;
}
} else if (op->encoding == OBJ_ENCODING_HT) {
dict *ht = op->subject->ptr;
zuiSdsFromValue(val);
if (dictFind(ht,val->ele) != NULL) { 找到
*score = 1.0;
return 1;
} else {
return 0;
}
} else {
serverPanic("Unknown set encoding");
}
} else if (op->type == OBJ_ZSET) {
zuiSdsFromValue(val);
if (op->encoding == OBJ_ENCODING_ZIPLIST) {
if (zzlFind(op->subject->ptr,val->ele,score) != NULL) {
/* Score is already set by zzlFind. */ 分值已经由函数zzlFind获取设置
return 1;
} else {
return 0;
}
} else if (op->encoding == OBJ_ENCODING_SKIPLIST) {
zset *zs = op->subject->ptr;
dictEntry *de;
if ((de = dictFind(zs->dict,val->ele)) != NULL) {
*score = *(double*)dictGetVal(de);
return 1;
} else {
return 0;
}
} else {
serverPanic("Unknown sorted set encoding");
}
} else {
serverPanic("Unsupported type");
}
}
***********************************************************************************************
通过元素个数比较集合,大返回1,小返回-1 相等返回0
int zuiCompareByCardinality(const void *s1, const void *s2) {
unsigned long first = zuiLength((zsetopsrc*)s1);
unsigned long second = zuiLength((zsetopsrc*)s2);
if (first > second) return 1;
if (first < second) return -1;
return 0;
}
***********************************************************************************************
#define REDIS_AGGR_SUM 1
#define REDIS_AGGR_MIN 2
#define REDIS_AGGR_MAX 3
#define zunionInterDictValue(_e) (dictGetVal(_e) == NULL ? 1.0 : *(double*)dictGetVal(_e))
为空分值默认是1.0,否则就是值本身
并集交集 集合聚合函数
inline static void zunionInterAggregate(double *target, double val, int aggregate) {
if (aggregate == REDIS_AGGR_SUM) { 求和
*target = *target + val;
/* The result of adding two doubles is NaN when one variable
* is +inf and the other is -inf. When these numbers are added,
* we maintain the convention of the result being 0.0. */
当一个变量为无穷大,另一个是无穷小时,这两个数相加的结果是没有定义。
当这样的数字相加时,我们保存结果的约定为0.0。
if (isnan(*target)) *target = 0.0;
} else if (aggregate == REDIS_AGGR_MIN) { 求最小
*target = val < *target ? val : *target;
} else if (aggregate == REDIS_AGGR_MAX) { 求最大
*target = val > *target ? val : *target;
} else {
/* safety net */ 安全分支,其它剩余不知道的情况
serverPanic("Unknown ZUNION/INTER aggregate type");
}
}
***********************************************************************************************
uint64_t dictSdsHash(const void *key);
int dictSdsKeyCompare(void *privdata, const void *key1, const void *key2);
dictType setAccumulatorDictType = {
dictSdsHash, /* hash function */
NULL, /* key dup */
NULL, /* val dup */
dictSdsKeyCompare, /* key compare */
NULL, /* key destructor */
NULL /* val destructor */
};
并集交集基础命令
void zunionInterGenericCommand(client *c, robj *dstkey, int op) {
int i, j;
long setnum;
int aggregate = REDIS_AGGR_SUM;
zsetopsrc *src;
zsetopval zval;
sds tmp;
size_t maxelelen = 0;
robj *dstobj;
zset *dstzset;
zskiplistNode *znode;
int touched = 0;
/* expect setnum input keys to be given */ 期望给定键的集合数量输入参数
if ((getLongFromObjectOrReply(c, c->argv[2], &setnum, NULL) != C_OK))
return;
if (setnum < 1) { 求集合的并集 和 交集 至少需要一个键
addReplyError(c,
"at least 1 input key is needed for ZUNIONSTORE/ZINTERSTORE");
return;
}
/* test if the expected number of keys would overflow */
检车期望的键是否超过了实际输入的键个数
if (setnum > c->argc-3) {
addReply(c,shared.syntaxerr);
return;
}
/* read keys to be used for input */ 读取输入的键
src = zcalloc(sizeof(zsetopsrc) * setnum); 分配输入键保存所需空间
for (i = 0, j = 3; i < setnum; i++, j++) {
robj *obj = lookupKeyWrite(c->db,c->argv[j]); 是否存在库中
if (obj != NULL) {
if (obj->type != OBJ_ZSET && obj->type != OBJ_SET) { 是否为集合 或 有序集合
zfree(src);
addReply(c,shared.wrongtypeerr);
return;
}
src[i].subject = obj;
src[i].type = obj->type;
src[i].encoding = obj->encoding;
} else {
src[i].subject = NULL;
}
/* Default all weights to 1. */ 默认所有的权重为1.0
src[i].weight = 1.0;
}
/* parse optional extra arguments */ 分析 可选项的额外参数
if (j < c->argc) {
int remaining = c->argc - j;
while (remaining) { 存在额外的参数
if (remaining >= (setnum + 1) &&
!strcasecmp(c->argv[j]->ptr,"weights")) 是不是权重参数
{
j++; remaining--;
for (i = 0; i < setnum; i++, j++, remaining--) {
if (getDoubleFromObjectOrReply(c,c->argv[j],&src[i].weight,
"weight value is not a float") != C_OK)
{
zfree(src);
return;
}
}
} else if (remaining >= 2 &&
!strcasecmp(c->argv[j]->ptr,"aggregate")) 聚合函数
{
j++; remaining--;
if (!strcasecmp(c->argv[j]->ptr,"sum")) {
aggregate = REDIS_AGGR_SUM;
} else if (!strcasecmp(c->argv[j]->ptr,"min")) {
aggregate = REDIS_AGGR_MIN;
} else if (!strcasecmp(c->argv[j]->ptr,"max")) {
aggregate = REDIS_AGGR_MAX;
} else {
zfree(src);
addReply(c,shared.syntaxerr);
return;
}
j++; remaining--;
} else {
zfree(src);
addReply(c,shared.syntaxerr);
return;
}
}
}
/* sort sets from the smallest to largest, this will improve our
* algorithm's performance */
将集合从小到大排序,这样做会改善我们算法的性能
qsort(src,setnum,sizeof(zsetopsrc),zuiCompareByCardinality);
dstobj = createZsetObject();
dstzset = dstobj->ptr;
memset(&zval, 0, sizeof(zval));
if (op == SET_OP_INTER) { 求交集
/* Skip everything if the smallest input is empty. */ 如果最小的集合是空集,跳过所有集合
if (zuiLength(&src[0]) > 0) {
/* Precondition: as src[0] is non-empty and the inputs are ordered
* by size, all src[i > 0] are non-empty too. */
前提条件: 如果src[0]是非空的,因为输入是按照大小排序的,所以所有后面的节后都非空
zuiInitIterator(&src[0]);
while (zuiNext(&src[0],&zval)) { 遍历最小集合的所有元素
double score, value;
score = src[0].weight * zval.score;
if (isnan(score)) score = 0;
for (j = 1; j < setnum; j++) {
/* It is not safe to access the zset we are
* iterating, so explicitly check for equal object. */
访问我们正在迭代访问的的有序集合是不安全的(有变动),所以显式检查对象是否相等
if (src[j].subject == src[0].subject) {
value = zval.score*src[j].weight;
zunionInterAggregate(&score,value,aggregate);
} else if (zuiFind(&src[j],&zval,&value)) {
value *= src[j].weight;
zunionInterAggregate(&score,value,aggregate);
} else {
break;
}
}
/* Only continue when present in every input. */ 只有在每个集合中出现过,才能达到这里
if (j == setnum) {
tmp = zuiNewSdsFromValue(&zval); 拷贝值
znode = zslInsert(dstzset->zsl,score,tmp); 插入一个元素节点
dictAdd(dstzset->dict,tmp,&znode->score); 添加到字典中
if (sdslen(tmp) > maxelelen) maxelelen = sdslen(tmp); 更新最长元素长度
}
}
zuiClearIterator(&src[0]); 清空迭代器
}
} else if (op == SET_OP_UNION) { 求并集
dict *accumulator = dictCreate(&setAccumulatorDictType,NULL);
dictIterator *di;
dictEntry *de, *existing;
double score;
if (setnum) { 我们的并集至少和最大的集合一样大,为了避免迁移尽快重新分配字典的空间
/* Our union is at least as large as the largest set.
* Resize the dictionary ASAP to avoid useless rehashing. */
dictExpand(accumulator,zuiLength(&src[setnum-1]));
}
/* Step 1: Create a dictionary of elements -> aggregated-scores
* by iterating one sorted set after the other. */
步骤1:创建一个字典保存 元素-聚合数值 ,通过挨个迭代排序集合
for (i = 0; i < setnum; i++) {
if (zuiLength(&src[i]) == 0) continue;集合为空,继续下一个集合
zuiInitIterator(&src[i]);
while (zuiNext(&src[i],&zval)) { 对非空集合进行遍历
/* Initialize value */ 初始化
score = src[i].weight * zval.score;
if (isnan(score)) score = 0;
/* Search for this element in the accumulating dictionary. */
在累积的字典中查找当前的元素
de = dictAddRaw(accumulator,zuiSdsFromValue(&zval),&existing);
/* If we don't have it, we need to create a new entry. */
如果找不到,那么需要创建一个新的实体元素(添加到累积字典)
if (!existing) {
tmp = zuiNewSdsFromValue(&zval);
/* Remember the longest single element encountered,
* to understand if it's possible to convert to ziplist
* at the end. */
记住遇到的最长的单个元素,以了解是否有可能在最后转换为ziplist
if (sdslen(tmp) > maxelelen) maxelelen = sdslen(tmp);
/* Update the element with its initial score. */用初始化的数值更新元素
dictSetKey(accumulator, de, tmp); 设置键
dictSetDoubleVal(de,score); 设置分值
} else { 找到实体元素的情况
/* Update the score with the score of the new instance
* of the element found in the current sorted set.
使用新的值的数值(通过指定的聚合算法)更新 在当前排序集中找到的元素的数值
* Here we access directly the dictEntry double
* value inside the union as it is a big speedup
* compared to using the getDouble/setDouble API. */
在这里,我们直接访问并集中的实体的数值,因为与使用getDouble/setDouble API相比,它速度更快
zunionInterAggregate(&existing->v.d,score,aggregate);
}
}
zuiClearIterator(&src[i]);
}
/* Step 2: convert the dictionary into the final sorted set. */
步骤2: 转化字典到最终的排序集合
di = dictGetIterator(accumulator); 初始化字典迭代器
/* We now are aware of the final size of the resulting sorted set,
* let's resize the dictionary embedded inside the sorted set to the
* right size, in order to save rehashing time. */
现在我们已经知道了结果排序集的最终大小,让我们将嵌入排序集的字典调整到合适的大小,以节省重新迁移的时间
dictExpand(dstzset->dict,dictSize(accumulator));
while((de = dictNext(di)) != NULL) { 遍历累积的集合
sds ele = dictGetKey(de);
score = dictGetDoubleVal(de);
znode = zslInsert(dstzset->zsl,score,ele); 插入到最终的排序集
dictAdd(dstzset->dict,ele,&znode->score);
}
dictReleaseIterator(di); 释放迭代器
dictRelease(accumulator); 释放累积字典
} else {
serverPanic("Unknown operator");
}
if (dbDelete(c->db,dstkey)) 删除原目标集
touched = 1;
if (dstzset->zsl->length) { 结果集不为0
zsetConvertToZiplistIfNeeded(dstobj,maxelelen); 是否需要转化为跳表
dbAdd(c->db,dstkey,dstobj); 加入新值
addReplyLongLong(c,zsetLength(dstobj));
signalModifiedKey(c,c->db,dstkey);
notifyKeyspaceEvent(NOTIFY_ZSET,
(op == SET_OP_UNION) ? "zunionstore" : "zinterstore",
dstkey,c->db->id);
server.dirty++;
} else { 长度为0
decrRefCount(dstobj); 减少引用,供释放使用
addReply(c,shared.czero);
if (touched) { 原目标存在
signalModifiedKey(c,c->db,dstkey);
notifyKeyspaceEvent(NOTIFY_GENERIC,"del",dstkey,c->db->id);
server.dirty++;
}
}
zfree(src);
}
***********************************************************************************************
求并集
void zunionstoreCommand(client *c) {
zunionInterGenericCommand(c,c->argv[1], SET_OP_UNION);
}
求交集
void zinterstoreCommand(client *c) {
zunionInterGenericCommand(c,c->argv[1], SET_OP_INTER);
}
***********************************************************************************************
返回区域元素
void zrangeGenericCommand(client *c, int reverse) {
robj *key = c->argv[1];
robj *zobj;
int withscores = 0;
long start;
long end;
long llen;
long rangelen;
if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != C_OK) || 获取起始和结束位置
(getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != C_OK)) return;
if (c->argc == 5 && !strcasecmp(c->argv[4]->ptr,"withscores")) { 是否带数值
withscores = 1;
} else if (c->argc >= 5) {
addReply(c,shared.syntaxerr);
return;
}
if ((zobj = lookupKeyReadOrReply(c,key,shared.emptyarray)) == NULL
|| checkType(c,zobj,OBJ_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.
变量start大于等于0,所以当end小于0时,测试总是成功
* The range is empty when start > end or start >= length. */
当start大于end或者start大于长度时,范围为空
if (start > end || start >= llen) {
addReply(c,shared.emptyarray);
return;
}
if (end >= llen) end = llen-1;
rangelen = (end-start)+1; 设置最后的区间范围值
/* Return the result in form of a multi-bulk reply. RESP3 clients
* will receive sub arrays with score->element, while RESP2 returned
* a flat array. */
以多批量回复的形式返回结果。RESP3客户端将接收带有score->element的子数组,
而RESP2返回一个一维数组
if (withscores && c->resp == 2)
addReplyArrayLen(c, rangelen*2);
else
addReplyArrayLen(c, rangelen);
if (zobj->encoding == OBJ_ENCODING_ZIPLIST) { 如果编码是压表的
unsigned char *zl = zobj->ptr;
unsigned char *eptr, *sptr;
unsigned char *vstr;
unsigned int vlen;
long long vlong;
if (reverse) 反向从尾巴向头部
eptr = ziplistIndex(zl,-2-(2*start));
else
eptr = ziplistIndex(zl,2*start);
serverAssertWithInfo(c,zobj,eptr != NULL);
sptr = ziplistNext(zl,eptr); 获取迭代器
while (rangelen--) { 遍历范围内元素
serverAssertWithInfo(c,zobj,eptr != NULL && sptr != NULL); 确认非空
serverAssertWithInfo(c,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong)); 获取元素值
if (withscores && c->resp > 2) addReplyArrayLen(c,2); 需要返回数值
if (vstr == NULL)
addReplyBulkLongLong(c,vlong); 整型值
else
addReplyBulkCBuffer(c,vstr,vlen); 字符串值
if (withscores) addReplyDouble(c,zzlGetScore(sptr)); 返回实际的数值
if (reverse) 向前移动
zzlPrev(zl,&eptr,&sptr);
else 向后移动
zzlNext(zl,&eptr,&sptr);
}
} else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) { 跳表形式
zset *zs = zobj->ptr;
zskiplist *zsl = zs->zsl;
zskiplistNode *ln;
sds ele;
/* Check if starting point is trivial, before doing log(N) lookup. */
在做log(n)的查找算法前,检查起始点是否是平凡的,平凡就不需要做log(N)的查找
if (reverse) {
ln = zsl->tail;
if (start > 0) 这里做这个检查
ln = zslGetElementByRank(zsl,llen-start);
} else {
ln = zsl->header->level[0].forward;
if (start > 0)
ln = zslGetElementByRank(zsl,start+1);
}
while(rangelen--) { 在查找范围内
serverAssertWithInfo(c,zobj,ln != NULL);
ele = ln->ele;
if (withscores && c->resp > 2) addReplyArrayLen(c,2); 需要返回数值
addReplyBulkCBuffer(c,ele,sdslen(ele));
if (withscores) addReplyDouble(c,ln->score);
ln = reverse ? ln->backward : ln->level[0].forward; 根据reverse前向或者后向移动到下一个
}
} else {
serverPanic("Unknown sorted set encoding");
}
}
***********************************************************************************************
正向返回范围内的元素
void zrangeCommand(client *c) {
zrangeGenericCommand(c,0);
}
反向返回范围内的元素
void zrevrangeCommand(client *c) {
zrangeGenericCommand(c,1);
}
***********************************************************************************************
/* This command implements ZRANGEBYSCORE, ZREVRANGEBYSCORE. */
这个命令实现 通过数值返回区间元素 通过数值反向返回区间元素
void genericZrangebyscoreCommand(client *c, int reverse) {
zrangespec range;
robj *key = c->argv[1];
robj *zobj;
long offset = 0, limit = -1;
int withscores = 0;
unsigned long rangelen = 0;
void *replylen = NULL;
int minidx, maxidx;
/* Parse the range arguments. */ 分析区间参数
if (reverse) { 反向区间 从大到小
/* Range is given as [max,min] */
maxidx = 2; minidx = 3;
} else { 正向区间 从小到大
/* Range is given as [min,max] */
minidx = 2; maxidx = 3;
}
if (zslParseRange(c->argv[minidx],c->argv[maxidx],&range) != C_OK) { 解析输入的范围参数是否合适
addReplyError(c,"min or max is not a float");
return;
}
/* Parse optional extra arguments. Note that ZCOUNT will exactly have
* 4 arguments, so we'll never enter the following code path. */
分析可选的额外参数。请注意,ZCOUNT正好有4个参数,因此我们永远不会进入下面这段代码
if (c->argc > 4) {
int remaining = c->argc - 4;
int pos = 4;
while (remaining) { 还有大于1个参数
if (remaining >= 1 && !strcasecmp(c->argv[pos]->ptr,"withscores")) { 带数值
pos++; remaining--;
withscores = 1;
} else if (remaining >= 3 && !strcasecmp(c->argv[pos]->ptr,"limit")) { 还有大于3个参数 限制数量
if ((getLongFromObjectOrReply(c, c->argv[pos+1], &offset, NULL)
!= C_OK) ||
(getLongFromObjectOrReply(c, c->argv[pos+2], &limit, NULL)
!= C_OK))
{
return;
}
pos += 3; remaining -= 3;
} else {
addReply(c,shared.syntaxerr);
return;
}
}
}
/* Ok, lookup the key and get the range */ 查找键 获取范围
if ((zobj = lookupKeyReadOrReply(c,key,shared.emptyarray)) == NULL ||
checkType(c,zobj,OBJ_ZSET)) return;
if (zobj->encoding == OBJ_ENCODING_ZIPLIST) { 压表
unsigned char *zl = zobj->ptr;
unsigned char *eptr, *sptr;
unsigned char *vstr;
unsigned int vlen;
long long vlong;
double score;
/* If reversed, get the last node in range as starting point. */
反向,获取最后一个范围内的节点当做起始节点
if (reverse) {
eptr = zzlLastInRange(zl,&range);
} else {
eptr = zzlFirstInRange(zl,&range);
}
/* No "first" element in the specified interval. */
在指定区间没有第一个元素(范围为空)
if (eptr == NULL) {
addReply(c,shared.emptyarray);
return;
}
/* Get score pointer for the first element. */
获取第一个元素的数值指针
serverAssertWithInfo(c,zobj,eptr != NULL);
sptr = ziplistNext(zl,eptr);
/* We don't know in advance how many matching elements there are in the
* list, so we push this object that will represent the multi-bulk
* length in the output buffer, and will "fix" it later */
我们事先不知道列表中有多少匹配的元素,所以我们现用输出缓冲区中多块数据长度的对象再输出缓冲区中占位,
稍后再给它填充实际的值
replylen = addReplyDeferredLen(c); 占位
/* If there is an offset, just traverse the number of elements without
* checking the score because that is done in the next loop. */
如果有偏移量,只需遍历元素数而不检查分数,因为这是在下一个循环中完成的
while (eptr && offset--) { 先遍历掉偏移量
if (reverse) {
zzlPrev(zl,&eptr,&sptr);
} else {
zzlNext(zl,&eptr,&sptr);
}
}
while (eptr && limit--) { 再返回指定的元素个数
score = zzlGetScore(sptr); 获取数值
/* Abort when the node is no longer in range. */ 如果迭代出范围,终止循环
if (reverse) {
if (!zslValueGteMin(score,&range)) break;
} else {
if (!zslValueLteMax(score,&range)) break;
}
/* We know the element exists, so ziplistGet should always
* succeed */
这里我确定符合要求的元素存在,所以函数ziplistGet总是成功
serverAssertWithInfo(c,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong));
rangelen++; 范围内元素加1
if (withscores && c->resp > 2) addReplyArrayLen(c,2); 需要返回数值
if (vstr == NULL) {
addReplyBulkLongLong(c,vlong);
} else {
addReplyBulkCBuffer(c,vstr,vlen);
}
if (withscores) addReplyDouble(c,score);
/* Move to next node */ 根据方向移动到下个节点
if (reverse) {
zzlPrev(zl,&eptr,&sptr);
} else {
zzlNext(zl,&eptr,&sptr);
}
}
} else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) { 跳表
zset *zs = zobj->ptr;
zskiplist *zsl = zs->zsl;
zskiplistNode *ln;
/* If reversed, get the last node in range as starting point. */
如果反向,获取范围内最后一个节点当做起始节点
if (reverse) {
ln = zslLastInRange(zsl,&range);
} else {
ln = zslFirstInRange(zsl,&range);
}
/* No "first" element in the specified interval. */
在范围内没有首节点元素(范围包含元素为空)
if (ln == NULL) {
addReply(c,shared.emptyarray);
return;
}
/* We don't know in advance how many matching elements there are in the
* list, so we push this object that will represent the multi-bulk
* length in the output buffer, and will "fix" it later */
我们事先不知道列表中有多少匹配的元素,所以我们现用输出缓冲区中多块数据长度的对象再输出缓冲区中占位,
稍后再给它填充实际的值
replylen = addReplyDeferredLen(c);
/* If there is an offset, just traverse the number of elements without
* checking the score because that is done in the next loop. */
如果有偏移量,只需遍历元素数而不检查分数,因为这是在下一个循环中完成的
while (ln && offset--) {
if (reverse) {
ln = ln->backward;
} else {
ln = ln->level[0].forward;
}
}
while (ln && limit--) {
/* Abort when the node is no longer in range. */
if (reverse) {
if (!zslValueGteMin(ln->score,&range)) break;
} else {
if (!zslValueLteMax(ln->score,&range)) break;
}
rangelen++;
if (withscores && c->resp > 2) addReplyArrayLen(c,2);
addReplyBulkCBuffer(c,ln->ele,sdslen(ln->ele));
if (withscores) addReplyDouble(c,ln->score);
/* Move to next node */
if (reverse) {
ln = ln->backward;
} else {
ln = ln->level[0].forward;
}
}
} else {
serverPanic("Unknown sorted set encoding");
}
if (withscores && c->resp == 2) rangelen *= 2;
setDeferredArrayLen(c, replylen, rangelen);
}
***********************************************************************************************
正向通过数值获取集合元素
void zrangebyscoreCommand(client *c) {
genericZrangebyscoreCommand(c,0);
}
反向通过数值获取集合元素
void zrevrangebyscoreCommand(client *c) {
genericZrangebyscoreCommand(c,1);
}
***********************************************************************************************
求范围内集合元素个数
void zcountCommand(client *c) {
robj *key = c->argv[1];
robj *zobj;
zrangespec range;
unsigned long count = 0;
/* Parse the range arguments */ 分析传入的区间参数
if (zslParseRange(c->argv[2],c->argv[3],&range) != C_OK) {
addReplyError(c,"min or max is not a float");
return;
}
/* Lookup the sorted set */ 查找排序集合
if ((zobj = lookupKeyReadOrReply(c, key, shared.czero)) == NULL ||
checkType(c, zobj, OBJ_ZSET)) return;
if (zobj->encoding == OBJ_ENCODING_ZIPLIST) { 压表
unsigned char *zl = zobj->ptr;
unsigned char *eptr, *sptr;
double score;
/* Use the first element in range as the starting point */ 使用范围内的第一个元素当做起始元素
eptr = zzlFirstInRange(zl,&range);
/* No "first" element */ 第一个元素为空(范围为空集)
if (eptr == NULL) {
addReply(c, shared.czero);
return;
}
/* First element is in range */ 不空的情况获取第一个元素
sptr = ziplistNext(zl,eptr);
score = zzlGetScore(sptr);
serverAssertWithInfo(c,zobj,zslValueLteMax(score,&range));
/* Iterate over elements in range */ 迭代范围内的每个元素
while (eptr) {
score = zzlGetScore(sptr);
/* Abort when the node is no longer in range. */ 当节点不在范围内时候退出
if (!zslValueLteMax(score,&range)) {
break;
} else {
count++; 计数加1
zzlNext(zl,&eptr,&sptr); 获取下一个节点元素
}
}
} else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) { 跳表
zset *zs = zobj->ptr;
zskiplist *zsl = zs->zsl;
zskiplistNode *zn;
unsigned long rank;
/* Find first element in range */ 范围内第一个元素
zn = zslFirstInRange(zsl, &range);
/* Use rank of first element, if any, to determine preliminary count */
使用第一个元素的秩(如果有的话)来确定初步计数
if (zn != NULL) {
rank = zslGetRank(zsl, zn->score, zn->ele);
count = (zsl->length - (rank - 1));
/* Find last element in range */ 查找范围内的最后一个元素
zn = zslLastInRange(zsl, &range);
/* Use rank of last element, if any, to determine the actual count */
使用最后一个元素的秩(如果有的话)来确定实际数量
if (zn != NULL) {
rank = zslGetRank(zsl, zn->score, zn->ele);
count -= (zsl->length - rank);
}
}
} else {
serverPanic("Unknown sorted set encoding");
}
addReplyLongLong(c, count);
}
***********************************************************************************************
根据参数获取范围内的元素
void zlexcountCommand(client *c) {
robj *key = c->argv[1];
robj *zobj;
zlexrangespec range;
unsigned long count = 0;
/* Parse the range arguments */
if (zslParseLexRange(c->argv[2],c->argv[3],&range) != C_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, OBJ_ZSET))
{
zslFreeLexRange(&range);
return;
}
if (zobj->encoding == OBJ_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);
serverAssertWithInfo(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 == OBJ_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->ele);
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->ele);
count -= (zsl->length - rank);
}
}
} else {
serverPanic("Unknown sorted set encoding");
}
zslFreeLexRange(&range);
addReplyLongLong(c, count);
}
***********************************************************************************************
/* This command implements ZRANGEBYLEX, ZREVRANGEBYLEX. */
这个命令实现函数 ZRANGEBYLEX, ZREVRANGEBYLEX
void genericZrangebylexCommand(client *c, int reverse) {
zlexrangespec range;
robj *key = c->argv[1];
robj *zobj;
long offset = 0, limit = -1;
unsigned long rangelen = 0;
void *replylen = NULL;
int minidx, maxidx;
/* Parse the range arguments. */
if (reverse) {
/* Range is given as [max,min] */
maxidx = 2; minidx = 3;
} else {
/* Range is given as [min,max] */
minidx = 2; maxidx = 3;
}
if (zslParseLexRange(c->argv[minidx],c->argv[maxidx],&range) != C_OK) {
addReplyError(c,"min or max not valid string range item");
return;
}
/* Parse optional extra arguments. Note that ZCOUNT will exactly have
* 4 arguments, so we'll never enter the following code path. */
if (c->argc > 4) {
int remaining = c->argc - 4;
int pos = 4;
while (remaining) {
if (remaining >= 3 && !strcasecmp(c->argv[pos]->ptr,"limit")) {
if ((getLongFromObjectOrReply(c, c->argv[pos+1], &offset, NULL) != C_OK) ||
(getLongFromObjectOrReply(c, c->argv[pos+2], &limit, NULL) != C_OK)) {
zslFreeLexRange(&range);
return;
}
pos += 3; remaining -= 3;
} else {
zslFreeLexRange(&range);
addReply(c,shared.syntaxerr);
return;
}
}
}
/* Ok, lookup the key and get the range */
if ((zobj = lookupKeyReadOrReply(c,key,shared.emptyarray)) == NULL ||
checkType(c,zobj,OBJ_ZSET))
{
zslFreeLexRange(&range);
return;
}
if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {
unsigned char *zl = zobj->ptr;
unsigned char *eptr, *sptr;
unsigned char *vstr;
unsigned int vlen;
long long vlong;
/* If reversed, get the last node in range as starting point. */
if (reverse) {
eptr = zzlLastInLexRange(zl,&range);
} else {
eptr = zzlFirstInLexRange(zl,&range);
}
/* No "first" element in the specified interval. */
if (eptr == NULL) {
addReply(c,shared.emptyarray);
zslFreeLexRange(&range);
return;
}
/* Get score pointer for the first element. */
serverAssertWithInfo(c,zobj,eptr != NULL);
sptr = ziplistNext(zl,eptr);
/* We don't know in advance how many matching elements there are in the
* list, so we push this object that will represent the multi-bulk
* length in the output buffer, and will "fix" it later */
replylen = addReplyDeferredLen(c);
/* If there is an offset, just traverse the number of elements without
* checking the score because that is done in the next loop. */
while (eptr && offset--) {
if (reverse) {
zzlPrev(zl,&eptr,&sptr);
} else {
zzlNext(zl,&eptr,&sptr);
}
}
while (eptr && limit--) {
/* Abort when the node is no longer in range. */
if (reverse) {
if (!zzlLexValueGteMin(eptr,&range)) break;
} else {
if (!zzlLexValueLteMax(eptr,&range)) break;
}
/* We know the element exists, so ziplistGet should always
* succeed. */
serverAssertWithInfo(c,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong));
rangelen++;
if (vstr == NULL) {
addReplyBulkLongLong(c,vlong);
} else {
addReplyBulkCBuffer(c,vstr,vlen);
}
/* Move to next node */
if (reverse) {
zzlPrev(zl,&eptr,&sptr);
} else {
zzlNext(zl,&eptr,&sptr);
}
}
} else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {
zset *zs = zobj->ptr;
zskiplist *zsl = zs->zsl;
zskiplistNode *ln;
/* If reversed, get the last node in range as starting point. */
if (reverse) {
ln = zslLastInLexRange(zsl,&range);
} else {
ln = zslFirstInLexRange(zsl,&range);
}
/* No "first" element in the specified interval. */
if (ln == NULL) {
addReply(c,shared.emptyarray);
zslFreeLexRange(&range);
return;
}
/* We don't know in advance how many matching elements there are in the
* list, so we push this object that will represent the multi-bulk
* length in the output buffer, and will "fix" it later */
replylen = addReplyDeferredLen(c);
/* If there is an offset, just traverse the number of elements without
* checking the score because that is done in the next loop. */
while (ln && offset--) {
if (reverse) {
ln = ln->backward;
} else {
ln = ln->level[0].forward;
}
}
while (ln && limit--) {
/* Abort when the node is no longer in range. */
if (reverse) {
if (!zslLexValueGteMin(ln->ele,&range)) break;
} else {
if (!zslLexValueLteMax(ln->ele,&range)) break;
}
rangelen++;
addReplyBulkCBuffer(c,ln->ele,sdslen(ln->ele));
/* Move to next node */
if (reverse) {
ln = ln->backward;
} else {
ln = ln->level[0].forward;
}
}
} else {
serverPanic("Unknown sorted set encoding");
}
zslFreeLexRange(&range);
setDeferredArrayLen(c, replylen, rangelen);
}
***********************************************************************************************
void zrangebylexCommand(client *c) {
genericZrangebylexCommand(c,0);
}
void zrevrangebylexCommand(client *c) {
genericZrangebylexCommand(c,1);
}
***********************************************************************************************
返回集合元素个数
void zcardCommand(client *c) {
robj *key = c->argv[1];
robj *zobj;
if ((zobj = lookupKeyReadOrReply(c,key,shared.czero)) == NULL ||
checkType(c,zobj,OBJ_ZSET)) return;
addReplyLongLong(c,zsetLength(zobj));
}
***********************************************************************************************
返回特定元素的数值
void zscoreCommand(client *c) {
robj *key = c->argv[1];
robj *zobj;
double score;
if ((zobj = lookupKeyReadOrReply(c,key,shared.null[c->resp])) == NULL ||
checkType(c,zobj,OBJ_ZSET)) return;
if (zsetScore(zobj,c->argv[2]->ptr,&score) == C_ERR) {
addReplyNull(c);
} else {
addReplyDouble(c,score);
}
}
***********************************************************************************************
返回特定元素的秩
void zrankGenericCommand(client *c, int reverse) {
robj *key = c->argv[1];
robj *ele = c->argv[2];
robj *zobj;
long rank;
if ((zobj = lookupKeyReadOrReply(c,key,shared.null[c->resp])) == NULL ||
checkType(c,zobj,OBJ_ZSET)) return;
serverAssertWithInfo(c,ele,sdsEncodedObject(ele));
rank = zsetRank(zobj,ele->ptr,reverse);
if (rank >= 0) {
addReplyLongLong(c,rank);
} else {
addReplyNull(c);
}
}
***********************************************************************************************
void zrankCommand(client *c) {
zrankGenericCommand(c, 0);
}
void zrevrankCommand(client *c) {
zrankGenericCommand(c, 1);
}
***********************************************************************************************
迭代返回有序集合中的元素
void zscanCommand(client *c) {
robj *o;
unsigned long cursor;
if (parseScanCursorOrReply(c,c->argv[2],&cursor) == C_ERR) return;
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptyscan)) == NULL ||
checkType(c,o,OBJ_ZSET)) return;
scanGenericCommand(c,o,cursor);
}
***********************************************************************************************
/* This command implements the generic zpop operation, used by:
* ZPOPMIN, ZPOPMAX, BZPOPMIN and BZPOPMAX. This function is also used
* inside blocked.c in the unblocking stage of BZPOPMIN and BZPOPMAX.
此命令实现一般的zpop操作,由:ZPOPMIN、ZPOPMAX、BZPOPMIN和BZPOPMAX使用。
此函数也用于BZPOPMIN和BZPOPMAX的非阻塞阶段的blocked.c中
* If 'emitkey' is true also the key name is emitted, useful for the blocking
* behavior of BZPOP[MIN|MAX], since we can block into multiple keys.
如果'emitkey'为真,那么也会返回键,这对于BZPOP[MIN | MAX]的阻塞行为很有用,因为我们可以阻塞多个键
* The synchronous version instead does not need to emit the key, but may
* use the 'count' argument to return multiple items if available. */
同步版本不需要返回键,但可以使用'count'参数返回多个项(如果可用)
void genericZpopCommand(client *c, robj **keyv, int keyc, int where, int emitkey, robj *countarg) {
int idx;
robj *key = NULL;
robj *zobj = NULL;
sds ele;
double score;
long count = 1;
/* If a count argument as passed, parse it or return an error. */
如果一个计数参数被传入,分析它 错误就返回
if (countarg) {
if (getLongFromObjectOrReply(c,countarg,&count,NULL) != C_OK)
return;
if (count <= 0) {
addReply(c,shared.emptyarray);
return;
}
}
/* Check type and break on the first error, otherwise identify candidate. */
首先检查输入参数的类型,如果有错就返回,否则确定候选项
idx = 0;
while (idx < keyc) { 遍历每个参数
key = keyv[idx++];
zobj = lookupKeyWrite(c->db,key);
if (!zobj) continue;
if (checkType(c,zobj,OBJ_ZSET)) return;
break;
}
/* No candidate for zpopping, return empty. */ 没有为zpop的候选参数,返回空
if (!zobj) {
addReply(c,shared.emptyarray);
return;
}
void *arraylen_ptr = addReplyDeferredLen(c); 占位
long arraylen = 0;
/* We emit the key only for the blocking variant. */ 我们只针对阻塞情况下返回键
if (emitkey) addReplyBulk(c,key);
/* Remove the element. */ 移除元素(弹出了)
do {
if (zobj->encoding == OBJ_ENCODING_ZIPLIST) { 压表
unsigned char *zl = zobj->ptr;
unsigned char *eptr, *sptr;
unsigned char *vstr;
unsigned int vlen;
long long vlong;
/* Get the first or last element in the sorted set. */ 获取排序集合中的第一个或最后一个元素
eptr = ziplistIndex(zl,where == ZSET_MAX ? -2 : 0);
serverAssertWithInfo(c,zobj,eptr != NULL);
serverAssertWithInfo(c,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong));
if (vstr == NULL)
ele = sdsfromlonglong(vlong);
else
ele = sdsnewlen(vstr,vlen);
/* Get the score. */ 获取元素的数值
sptr = ziplistNext(zl,eptr);
serverAssertWithInfo(c,zobj,sptr != NULL);
score = zzlGetScore(sptr);
} else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) { 跳表
zset *zs = zobj->ptr;
zskiplist *zsl = zs->zsl;
zskiplistNode *zln;
/* Get the first or last element in the sorted set. */
zln = (where == ZSET_MAX ? zsl->tail :
zsl->header->level[0].forward);
/* There must be an element in the sorted set. */ 在排序集合中必定存在一个元素(集合非空)
serverAssertWithInfo(c,zobj,zln != NULL);
ele = sdsdup(zln->ele);
score = zln->score;
} else {
serverPanic("Unknown sorted set encoding");
}
serverAssertWithInfo(c,zobj,zsetDel(zobj,ele));
server.dirty++;
if (arraylen == 0) { /* Do this only for the first iteration. */ 只针对第一次的情况
char *events[2] = {"zpopmin","zpopmax"};
notifyKeyspaceEvent(NOTIFY_ZSET,events[where],key,c->db->id);
signalModifiedKey(c,c->db,key);
}
addReplyBulkCBuffer(c,ele,sdslen(ele)); 返回字符串和长度
addReplyDouble(c,score); 返回数值
sdsfree(ele); 释放字符串空间
arraylen += 2; 长度加2
/* Remove the key, if indeed needed. */ 如果有需要,移除库中的对象
if (zsetLength(zobj) == 0) {
dbDelete(c->db,key);
notifyKeyspaceEvent(NOTIFY_GENERIC,"del",key,c->db->id);
break;
}
} while(--count); 还没有到指定弹出数目,继续
setDeferredArrayLen(c,arraylen_ptr,arraylen + (emitkey != 0));
}
***********************************************************************************************
/* ZPOPMIN key [<count>] */
从前向后弹出元素
void zpopminCommand(client *c) {
if (c->argc > 3) {
addReply(c,shared.syntaxerr);
return;
}
genericZpopCommand(c,&c->argv[1],1,ZSET_MIN,0,
c->argc == 3 ? c->argv[2] : NULL);
}
***********************************************************************************************
/* ZMAXPOP key [<count>] */
从后向前弹出元素
void zpopmaxCommand(client *c) {
if (c->argc > 3) {
addReply(c,shared.syntaxerr);
return;
}
genericZpopCommand(c,&c->argv[1],1,ZSET_MAX,0,
c->argc == 3 ? c->argv[2] : NULL);
}
***********************************************************************************************
/* BZPOPMIN / BZPOPMAX actual implementation. */
命令 BZPOPMIN / BZPOPMAX 实际实现
void blockingGenericZpopCommand(client *c, int where) {
robj *o;
mstime_t timeout;
int j;
if (getTimeoutFromObjectOrReply(c,c->argv[c->argc-1],&timeout,UNIT_SECONDS) 获取返回元素的时间
!= C_OK) return;
for (j = 1; j < c->argc-1; j++) {
o = lookupKeyWrite(c->db,c->argv[j]);
if (o != NULL) {
if (o->type != OBJ_ZSET) {
addReply(c,shared.wrongtypeerr);
return;
} else {
if (zsetLength(o) != 0) {
/* Non empty zset, this is like a normal ZPOP[MIN|MAX]. */
非空集合,如正常ZPOP[MIN|MAX]般操作
genericZpopCommand(c,&c->argv[j],1,where,1,NULL);
/* Replicate it as an ZPOP[MIN|MAX] instead of BZPOP[MIN|MAX]. */
将其复制为ZPOP[MIN | MAX],而不是BZPOP[MIN | MAX]
rewriteClientCommandVector(c,2,
where == ZSET_MAX ? shared.zpopmax : shared.zpopmin,
c->argv[j]);
return;
}
}
}
}
/* If we are inside a MULTI/EXEC and the zset is empty the only thing
* we can do is treating it as a timeout (even with timeout 0). */
如果我们在MULTI/EXEC中,zset是空的,那么我们唯一能做的就是将其视为超时(即使超时设置为0)
if (c->flags & CLIENT_MULTI) {
addReplyNullArray(c);
return;
}
/* If the keys do not exist we must block */ 如果键不存在,我们必须先阻塞
blockForKeys(c,BLOCKED_ZSET,c->argv + 1,c->argc - 2,timeout,NULL,NULL);
}
***********************************************************************************************
// BZPOPMIN key [key ...] timeout
void bzpopminCommand(client *c) {
blockingGenericZpopCommand(c,ZSET_MIN);
}
// BZPOPMAX key [key ...] timeout
void bzpopmaxCommand(client *c) {
blockingGenericZpopCommand(c,ZSET_MAX);
}
***********************************************************************************************