redis6.0.5之t_list.c阅读笔记--列表

2021/7/15 19:06:17

本文主要是介绍redis6.0.5之t_list.c阅读笔记--列表,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

*********************************************************************************************************************
/*-----------------------------------------------------------------------------
 * List API  列表API
 *----------------------------------------------------------------------------*/

/* The function pushes an element to the specified list object 'subject',
 * at head or tail position as specified by 'where'.
 *
 * There is no need for the caller to increment the refcount of 'value' as
 * the function takes care of it if needed. */
本函数把一个元素压入到特定的列表对象subject,根据where指定的方向压入到头部或者尾部
这里不需要调用者去增加值的引用,如果有需要函数自身会处理它
void listTypePush(robj *subject, robj *value, int where) {
    if (subject->encoding == OBJ_ENCODING_QUICKLIST) {  如果编码是快速列表
        int pos = (where == LIST_HEAD) ? QUICKLIST_HEAD : QUICKLIST_TAIL; 根据where确定压入位置
        value = getDecodedObject(value); 获取字符串对象
        size_t len = sdslen(value->ptr);  字符串对象中字符串的长度
        quicklistPush(subject->ptr, value->ptr, len, pos); 压入到快速列表
        decrRefCount(value); 将获取的对象引用减少1,因为这个对象后面不会再用了,减少不必要的浪费
    } else {
        serverPanic("Unknown list encoding");
    }
}
*********************************************************************************************************************
保存弹出的数据
void *listPopSaver(unsigned char *data, unsigned int sz) {
    return createStringObject((char*)data,sz);  根据传入的参数创建新的字符串对象
}
*********************************************************************************************************************
弹出快速列表的一个元素
robj *listTypePop(robj *subject, int where) {
    long long vlong;
    robj *value = NULL;

    int ql_where = where == LIST_HEAD ? QUICKLIST_HEAD : QUICKLIST_TAIL;
    if (subject->encoding == OBJ_ENCODING_QUICKLIST) { 快速列表
        if (quicklistPopCustom(subject->ptr, ql_where, (unsigned char **)&value,
                               NULL, &vlong, listPopSaver)) {
            if (!value) 非字符串类型,需要转化为字符串
                value = createStringObjectFromLongLong(vlong);
        }
    } else {
        serverPanic("Unknown list encoding");
    }
    return value;
}
*********************************************************************************************************************
返回列表的元素个数
unsigned long listTypeLength(const robj *subject) {
    if (subject->encoding == OBJ_ENCODING_QUICKLIST) {
        return quicklistCount(subject->ptr); 这里返回列表的元素个数
    } else {
        serverPanic("Unknown list encoding");
    }
}
*********************************************************************************************************************
/* Initialize an iterator at the specified index. */ 根据特定的额索引初始化一个迭代器

/* Structure to hold list iteration abstraction. */ 保持列表迭代器的数据结构
typedef struct {
    robj *subject;
    unsigned char encoding;
    unsigned char direction; /* Iteration direction */
    quicklistIter *iter;
} listTypeIterator;

listTypeIterator *listTypeInitIterator(robj *subject, long index,
                                       unsigned char direction) {
    listTypeIterator *li = zmalloc(sizeof(listTypeIterator));
    li->subject = subject;
    li->encoding = subject->encoding; 
    li->direction = direction;
    li->iter = NULL;
    /* LIST_HEAD means start at TAIL and move *towards* head.
     * LIST_TAIL means start at HEAD and move *towards tail. */
     LIST_HEAD 意思为 从尾部开始,向头部移动
     LIST_TAIL 意思为 从头部开始,向尾部移动
    int iter_direction =
        direction == LIST_HEAD ? AL_START_TAIL : AL_START_HEAD;
    if (li->encoding == OBJ_ENCODING_QUICKLIST) {  根据索引所在的位置初始化迭代器
        li->iter = quicklistGetIteratorAtIdx(li->subject->ptr,
                                             iter_direction, index);
    } else {
        serverPanic("Unknown list encoding");
    }
    return li;
}
*********************************************************************************************************************
/* Clean up the iterator. */ 清除迭代器
void listTypeReleaseIterator(listTypeIterator *li) {
    zfree(li->iter); 释放迭代器
    zfree(li); 释放保存迭代器的结构
}
*********************************************************************************************************************
/* Stores pointer to current the entry in the provided entry structure
 * and advances the position of the iterator. Returns 1 when the current
 * entry is in fact an entry, 0 otherwise. */
存储指针指向的当前实体提供了 迭代器的实体结构和位置。
返回1如果当前实体是有效的,否则返回0(返回实体无效的情况下)

int listTypeNext(listTypeIterator *li, listTypeEntry *entry) {
    /* Protect from converting when iterating */ 转变编码格式时的保护确认
    serverAssert(li->subject->encoding == li->encoding);

    entry->li = li;
    if (li->encoding == OBJ_ENCODING_QUICKLIST) {
        return quicklistNext(li->iter, &entry->entry); 返回找到的内容
    } else {
        serverPanic("Unknown list encoding");
    }
    return 0;
}
*********************************************************************************************************************
/* Return entry or NULL at the current position of the iterator. */
返回迭代器当前位置的实体或者空
robj *listTypeGet(listTypeEntry *entry) {
    robj *value = NULL;
    if (entry->li->encoding == OBJ_ENCODING_QUICKLIST) {
        if (entry->entry.value) {  是字符串类型的值
            value = createStringObject((char *)entry->entry.value,
                                       entry->entry.sz);  根据长度和字符串创建字符串对象
        } else {
            value = createStringObjectFromLongLong(entry->entry.longval); 将整型转化为字符串
        }
    } else {
        serverPanic("Unknown list encoding");
    }
    return value;
}
*********************************************************************************************************************
在列表插入一个实体元素
void listTypeInsert(listTypeEntry *entry, robj *value, int where) {
    if (entry->li->encoding == OBJ_ENCODING_QUICKLIST) {
        value = getDecodedObject(value); 获取字符串对象
        sds str = value->ptr; sds字符串
        size_t len = sdslen(str);
        if (where == LIST_TAIL) {
            quicklistInsertAfter((quicklist *)entry->entry.quicklist,
                                 &entry->entry, str, len); 在尾部插入
        } else if (where == LIST_HEAD) {
            quicklistInsertBefore((quicklist *)entry->entry.quicklist,
                                  &entry->entry, str, len); 在头部插入
        }
        decrRefCount(value); 减少引用计数
    } else {
        serverPanic("Unknown list encoding");
    }
}
*********************************************************************************************************************
/* Compare the given object with the entry at the current position. */
比较给定对象和在当前位置的实体对象
int listTypeEqual(listTypeEntry *entry, robj *o) {
    if (entry->li->encoding == OBJ_ENCODING_QUICKLIST) { 当前位置实体的编码
        serverAssertWithInfo(NULL,o,sdsEncodedObject(o));
        return quicklistCompare(entry->entry.zi,o->ptr,sdslen(o->ptr));  比较两个字符串
    } else {
        serverPanic("Unknown list encoding");
    }
}
*********************************************************************************************************************
/* Delete the element pointed to. */
删除迭代器指向的元素(entry就是iter指向的元素)
void listTypeDelete(listTypeIterator *iter, listTypeEntry *entry) {
    if (entry->li->encoding == OBJ_ENCODING_QUICKLIST) {
        quicklistDelEntry(iter->iter, &entry->entry); 删除迭代器指向的元素,同时更新迭代器
    } else {
        serverPanic("Unknown list encoding");
    }
}
*********************************************************************************************************************
/* Create a quicklist from a single ziplist */
createIntConfig("list-max-ziplist-size", NULL, MODIFIABLE_CONFIG, INT_MIN, INT_MAX, 
server.list_max_ziplist_size, -2, INTEGER_CONFIG, NULL, NULL),
默认配置为-2,但是实际取值会经过转化
size_t offset = (-fill) - 1;
static const size_t optimization_level[] = {4096, 8192, 16384, 32768, 65536};
所以-2对应的位置是8192个字节

createIntConfig("list-compress-depth", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, 
server.list_compress_depth, 0, INTEGER_CONFIG, NULL, NULL),  
默认设置为0

从一个压缩列表创建快速列表
void listTypeConvert(robj *subject, int enc) {
    serverAssertWithInfo(NULL,subject,subject->type==OBJ_LIST); 确认是列表对象
    serverAssertWithInfo(NULL,subject,subject->encoding==OBJ_ENCODING_ZIPLIST); 确认是压缩列表编码

    if (enc == OBJ_ENCODING_QUICKLIST) {  确认目标编码为快速列表
        size_t zlen = server.list_max_ziplist_size;  设置为默认的初始化值-2
        int depth = server.list_compress_depth; 设置默认值为0, 不压缩列表
        subject->ptr = quicklistCreateFromZiplist(zlen, depth, subject->ptr); 从压缩列表转化为快速列表
        subject->encoding = OBJ_ENCODING_QUICKLIST;
    } else {
        serverPanic("Unsupported list conversion");
    }
}
*********************************************************************************************************************
/*-----------------------------------------------------------------------------
 * List Commands  列表命令
 *----------------------------------------------------------------------------*/
一般化的压入命令
void pushGenericCommand(client *c, int where) {
    int j, pushed = 0;
    robj *lobj = lookupKeyWrite(c->db,c->argv[1]); 库中查找对象是否存在

    if (lobj && lobj->type != OBJ_LIST) { 存在对象但是不是列表对象
        addReply(c,shared.wrongtypeerr);
        return;
    }

    for (j = 2; j < c->argc; j++) {
        if (!lobj) { 对象不存在,那需要创建新对象,设置初始化的默认值
            lobj = createQuicklistObject();
            quicklistSetOptions(lobj->ptr, server.list_max_ziplist_size,
                                server.list_compress_depth);
            dbAdd(c->db,c->argv[1],lobj); 在数据库中添加键值
        }
        listTypePush(lobj,c->argv[j],where); 压入一个元素
        pushed++; 压入元素计数加1
    }
    addReplyLongLong(c, (lobj ? listTypeLength(lobj) : 0)); 回复客户端
    if (pushed) {
        char *event = (where == LIST_HEAD) ? "lpush" : "rpush"; 如果有压入数据,看看是什么事件

        signalModifiedKey(c,c->db,c->argv[1]);  把修改键的信息发送给客户端
        notifyKeyspaceEvent(NOTIFY_LIST,event,c->argv[1],c->db->id); 通知订阅的客户端
    }
    server.dirty += pushed; 变动的键计数修改
}
*********************************************************************************************************************
左边压入
void lpushCommand(client *c) {
    pushGenericCommand(c,LIST_HEAD);
}
*********************************************************************************************************************
右边压入
void rpushCommand(client *c) {
    pushGenericCommand(c,LIST_TAIL);
}
*********************************************************************************************************************
将一个值插入到已经存在的列表中,如果列表不存在就不操作
void pushxGenericCommand(client *c, int where) {
    int j, pushed = 0;
    robj *subject;

    if ((subject = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||  如果库中找不到 或者类型不是列表 
        checkType(c,subject,OBJ_LIST)) return;

    for (j = 2; j < c->argc; j++) { 正常情况,一个一个元素的压入
        listTypePush(subject,c->argv[j],where);
        pushed++; 压入元素计数
    }

    addReplyLongLong(c,listTypeLength(subject));

    if (pushed) { 如果有压入新的元素,需要通知相关各方
        char *event = (where == LIST_HEAD) ? "lpush" : "rpush";
        signalModifiedKey(c,c->db,c->argv[1]);
        notifyKeyspaceEvent(NOTIFY_LIST,event,c->argv[1],c->db->id);
    }
    server.dirty += pushed;
}
*********************************************************************************************************************
左压入
void lpushxCommand(client *c) {
    pushxGenericCommand(c,LIST_HEAD);
}
*********************************************************************************************************************
右压入
void rpushxCommand(client *c) {
    pushxGenericCommand(c,LIST_TAIL);
}
*********************************************************************************************************************
列表元素插入命令
void linsertCommand(client *c) {
    int where;
    robj *subject;
    listTypeIterator *iter;
    listTypeEntry entry;
    int inserted = 0;

    if (strcasecmp(c->argv[2]->ptr,"after") == 0) {  后面插入
        where = LIST_TAIL;
    } else if (strcasecmp(c->argv[2]->ptr,"before") == 0) { 前面插入
        where = LIST_HEAD;
    } else {
        addReply(c,shared.syntaxerr);
        return;
    }

    if ((subject = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL || 如果指定的键库中不存在 
        checkType(c,subject,OBJ_LIST)) return;

    /* Seek pivot from head to tail */ 在列表中从头到尾查找指定的元素
    iter = listTypeInitIterator(subject,0,LIST_TAIL);  从头到尾进行遍历,初始的偏移量设置为0
    while (listTypeNext(iter,&entry)) {
        if (listTypeEqual(&entry,c->argv[3])) { 如果找了指定的元素
            listTypeInsert(&entry,c->argv[4],where); 在该元素前后或者后面插入新元素
            inserted = 1;
            break;
        }
    }
    listTypeReleaseIterator(iter);  释放迭代器

    if (inserted) { 如果有插入新的元素
        signalModifiedKey(c,c->db,c->argv[1]);
        notifyKeyspaceEvent(NOTIFY_LIST,"linsert",
                            c->argv[1],c->db->id);
        server.dirty++;
    } else {
        /* Notify client of a failed insert */ 通知客户端一次失败的插入操作
        addReplyLongLong(c,-1);
        return;
    }

    addReplyLongLong(c,listTypeLength(subject));
}
*********************************************************************************************************************
返回列表长度
void llenCommand(client *c) {
    robj *o = lookupKeyReadOrReply(c,c->argv[1],shared.czero);
    if (o == NULL || checkType(c,o,OBJ_LIST)) return;
    addReplyLongLong(c,listTypeLength(o));
}
*********************************************************************************************************************
返回index所在位置的元素
void lindexCommand(client *c) {
    robj *o = lookupKeyReadOrReply(c,c->argv[1],shared.null[c->resp]); 查找列表
    if (o == NULL || checkType(c,o,OBJ_LIST)) return; 如果库中不存在对应的键 或者 不是列表
    long index;
    robj *value = NULL;

    if ((getLongFromObjectOrReply(c, c->argv[2], &index, NULL) != C_OK)) 获取索引的值
        return;

    if (o->encoding == OBJ_ENCODING_QUICKLIST) {
        quicklistEntry entry;
        if (quicklistIndex(o->ptr, index, &entry)) {  根据索引位置获取值
            if (entry.value) {字符串
                value = createStringObject((char*)entry.value,entry.sz);
            } else {整型
                value = createStringObjectFromLongLong(entry.longval);
            }
            addReplyBulk(c,value);
            decrRefCount(value);
        } else {
            addReplyNull(c);
        }
    } else {
        serverPanic("Unknown list encoding");
    }
}
*********************************************************************************************************************
通过索引设置元素的值
void lsetCommand(client *c) {
    robj *o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr); 库中查找元素
    if (o == NULL || checkType(c,o,OBJ_LIST)) return;  
    long index;
    robj *value = c->argv[3];

    if ((getLongFromObjectOrReply(c, c->argv[2], &index, NULL) != C_OK)) 获取传入索引
        return;

    if (o->encoding == OBJ_ENCODING_QUICKLIST) {
        quicklist *ql = o->ptr;
        int replaced = quicklistReplaceAtIndex(ql, index,
                                               value->ptr, sdslen(value->ptr)); 取代索引所在位置的元素值
        if (!replaced) { 没有发生取代,找不到索引所在元素的位置
            addReply(c,shared.outofrangeerr);
        } else { 发生了取代,将消息通知相关各方
            addReply(c,shared.ok);
            signalModifiedKey(c,c->db,c->argv[1]);
            notifyKeyspaceEvent(NOTIFY_LIST,"lset",c->argv[1],c->db->id);
            server.dirty++;
        }
    } else {
        serverPanic("Unknown list encoding");
    }
}
*********************************************************************************************************************
弹出元素的一般命令
void popGenericCommand(client *c, int where) {
确保库中存在对应键的值,而且值是列表对象
    robj *o = lookupKeyWriteOrReply(c,c->argv[1],shared.null[c->resp]);
    if (o == NULL || checkType(c,o,OBJ_LIST)) return;

    robj *value = listTypePop(o,where); 获取弹出值
    if (value == NULL) {
        addReplyNull(c);
    } else {
        char *event = (where == LIST_HEAD) ? "lpop" : "rpop"; 分析弹出事件

        addReplyBulk(c,value);
        decrRefCount(value); 减少引用计数
        notifyKeyspaceEvent(NOTIFY_LIST,event,c->argv[1],c->db->id);
        if (listTypeLength(o) == 0) { 如果对象长度为0
            notifyKeyspaceEvent(NOTIFY_GENERIC,"del",
                                c->argv[1],c->db->id);
            dbDelete(c->db,c->argv[1]); 将库中的键删除
        }
        signalModifiedKey(c,c->db,c->argv[1]);
        server.dirty++;
    }
}
*********************************************************************************************************************
左弹出
void lpopCommand(client *c) {
    popGenericCommand(c,LIST_HEAD);
}
*********************************************************************************************************************
右弹出
void rpopCommand(client *c) {
    popGenericCommand(c,LIST_TAIL);
}
*********************************************************************************************************************
返回列表区间元素
void lrangeCommand(client *c) {
    robj *o;
    long start, end, llen, rangelen;

    if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != C_OK) ||  获取开始值位置
        (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != C_OK)) return; 获取结束位置

    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptyarray)) == NULL
         || checkType(c,o,OBJ_LIST)) return;
    llen = listTypeLength(o); 列表对象元素个数

    /* convert negative indexes */ 转化负数的索引
    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. */
     变量start大于等于0,所以如果end小于0,那么这个测试就是真的。
     区间是空的,如果开始 比结束大 或者 开始 大于列表元素个数
    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 */
    通过多组回复的格式返回结果
    addReplyArrayLen(c,rangelen); 返回总的个数
    if (o->encoding == OBJ_ENCODING_QUICKLIST) {
        listTypeIterator *iter = listTypeInitIterator(o, start, LIST_TAIL);从头到尾

        while(rangelen--) { 遍历范围区间
            listTypeEntry entry;
            listTypeNext(iter, &entry);获取值
            quicklistEntry *qe = &entry.entry; 
            if (qe->value) {
                addReplyBulkCBuffer(c,qe->value,qe->sz);
            } else {
                addReplyBulkLongLong(c,qe->longval);
            }
        }
        listTypeReleaseIterator(iter); 释放迭代器
    } else {
        serverPanic("List encoding is not QUICKLIST!");
    }
}
*********************************************************************************************************************
对列表进行修剪,只保留传入参数中间部分
void ltrimCommand(client *c) {
    robj *o;
    long start, end, llen, ltrim, rtrim;

    if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != C_OK) || 获取开始位置
        (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != C_OK)) return; 获取结束位置

    if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.ok)) == NULL ||
        checkType(c,o,OBJ_LIST)) return;
    llen = listTypeLength(o);

    /* convert negative indexes */
    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) {
        /* Out of range start or start > end result in empty list */
        start超过范围 或者 start 大于 end  那么结果就是空列表
        ltrim = llen; 左边要删除的元素个数,整个列表的元素全部是
        rtrim = 0; 右边要删除的元素个数
    } else {
        if (end >= llen) end = llen-1; 如果结束位置大于列表元素个数,结束位置设置为最后一个元素
        ltrim = start;  左边要删除的元素个数,从0到start-1 刚好start个
        rtrim = llen-end-1; 右边要删除的元素个数 end后面的数据全部删除,刚好 llen-end-1个
    }

    /* Remove list elements to perform the trim */  移除列表元素  实现命令trim
    if (o->encoding == OBJ_ENCODING_QUICKLIST) {
        quicklistDelRange(o->ptr,0,ltrim); 删除左边
        quicklistDelRange(o->ptr,-rtrim,rtrim);  删除右边
    } else {
        serverPanic("Unknown list encoding");
    }

    notifyKeyspaceEvent(NOTIFY_LIST,"ltrim",c->argv[1],c->db->id);
    if (listTypeLength(o) == 0) {  列表为空
        dbDelete(c->db,c->argv[1]); 删除库中键值
        notifyKeyspaceEvent(NOTIFY_GENERIC,"del",c->argv[1],c->db->id);
    }
    signalModifiedKey(c,c->db,c->argv[1]);
    server.dirty++;
    addReply(c,shared.ok);
}
*********************************************************************************************************************
移除列表中的元素 同传入值一样的元素  删除个数也由传入值决定
void lremCommand(client *c) {
    robj *subject, *obj;
    obj = c->argv[3];
    long toremove;
    long removed = 0;

    if ((getLongFromObjectOrReply(c, c->argv[2], &toremove, NULL) != C_OK))
        return;

    subject = lookupKeyWriteOrReply(c,c->argv[1],shared.czero);
    if (subject == NULL || checkType(c,subject,OBJ_LIST)) return;

    listTypeIterator *li;
    if (toremove < 0) {  如果传入个数的值为负数,那么需要取绝对值
        toremove = -toremove;
        li = listTypeInitIterator(subject,-1,LIST_HEAD);从尾到头,偏移量为-1
    } else {
        li = listTypeInitIterator(subject,0,LIST_TAIL); 从头到尾,偏移量为0
    }

    listTypeEntry entry;
    while (listTypeNext(li,&entry)) { 遍历列表,查找同样值的元素
        if (listTypeEqual(&entry,obj)) { 找到就删除
            listTypeDelete(li, &entry);
            server.dirty++;
            removed++;
            if (toremove && removed == toremove) break;
        }
    }
    listTypeReleaseIterator(li); 释放迭代列表

    if (removed) {
        signalModifiedKey(c,c->db,c->argv[1]);
        notifyKeyspaceEvent(NOTIFY_LIST,"lrem",c->argv[1],c->db->id);
    }

    if (listTypeLength(subject) == 0) {
        dbDelete(c->db,c->argv[1]); 删除数据库键
        notifyKeyspaceEvent(NOTIFY_GENERIC,"del",c->argv[1],c->db->id);
    }

    addReplyLongLong(c,removed);
}
*********************************************************************************************************************
/* This is the semantic of this command: 下面是这个命令的语义
 *  RPOPLPUSH srclist dstlist: 源列表 右边弹出   目标列表 左边压入 
 *    IF LLEN(srclist) > 0 源列表还有元素
 *      element = RPOP srclist 从源列表右边弹出
 *      LPUSH dstlist element 在目标列表左边压入
 *      RETURN element 返回弹出元素
 *    ELSE
 *      RETURN nil
 *    END
 *  END
 *
 * The idea is to be able to get an element from a list in a reliable way
 * since the element is not just returned but pushed against another list
 * as well. This command was originally proposed by Ezra Zygmuntowicz.
 */
这样做的目的是能够以可靠的方式从一个列表中获取一个元素,
因为该元素不仅被返回,而且还被推到另一个列表中。这一命令最初是由以Ezra Zygmuntowicz提出的
void rpoplpushHandlePush(client *c, robj *dstkey, robj *dstobj, robj *value) {
    /* Create the list if the key does not exist */  如果目标键值库中不存在
    if (!dstobj) {
        dstobj = createQuicklistObject(); 创建 
        quicklistSetOptions(dstobj->ptr, server.list_max_ziplist_size,
                            server.list_compress_depth); 设置默认参数
        dbAdd(c->db,dstkey,dstobj); 新增
    }
    signalModifiedKey(c,c->db,dstkey);
    listTypePush(dstobj,value,LIST_HEAD); 压入目标列表的头部
    notifyKeyspaceEvent(NOTIFY_LIST,"lpush",dstkey,c->db->id);
    /* Always send the pushed value to the client. */ 发送压入的参数值到客户端
    addReplyBulk(c,value);
}
*********************************************************************************************************************
void rpoplpushCommand(client *c) {
    robj *sobj, *value;
    if ((sobj = lookupKeyWriteOrReply(c,c->argv[1],shared.null[c->resp])) 检查源列表是否存在库中 以及 类型是否是列表
        == NULL || checkType(c,sobj,OBJ_LIST)) return;

    if (listTypeLength(sobj) == 0) { 如果源列表没有元素了
        /* This may only happen after loading very old RDB files. Recent
         * versions of Redis delete keys of empty lists. */
         发生这种情况只可能是加载旧版本的RDB文件了,因为新版本的redis会删除空的列表
        addReplyNull(c);
    } else {
        robj *dobj = lookupKeyWrite(c->db,c->argv[2]);
        robj *touchedkey = c->argv[1];

        if (dobj && checkType(c,dobj,OBJ_LIST)) return; 如果目标键存在而且不是列表
        value = listTypePop(sobj,LIST_TAIL);弹出尾部元素
        /* We saved touched key, and protect it, since rpoplpushHandlePush
         * may change the client command argument vector (it does not
         * currently). */
我们保存了该键,并对其进行了保护,因为rpoplpushHandlePush函数可能会更改客户机命令参数向量(当前不会更改)
        incrRefCount(touchedkey); 增加键的引用,保护起来
        rpoplpushHandlePush(c,c->argv[2],dobj,value);压入新值

        /* listTypePop returns an object with its refcount incremented */
        函数listTypePop返回了一个引用计数加1的对象,这里需要减去
        decrRefCount(value);

        /* Delete the source list when it is empty */
        删除源列表,如果列表已空
        notifyKeyspaceEvent(NOTIFY_LIST,"rpop",touchedkey,c->db->id);
        if (listTypeLength(sobj) == 0) {
            dbDelete(c->db,touchedkey);
            notifyKeyspaceEvent(NOTIFY_GENERIC,"del",
                                touchedkey,c->db->id);
        }
        signalModifiedKey(c,c->db,touchedkey);
        decrRefCount(touchedkey); 释放引用
        server.dirty++;
        if (c->cmd->proc == brpoplpushCommand) {
            rewriteClientCommandVector(c,3,shared.rpoplpush,c->argv[1],c->argv[2]);
        }
    }
}
*********************************************************************************************************************
/*-----------------------------------------------------------------------------
 * Blocking POP operations  阻止POP操作
 *----------------------------------------------------------------------------*/

/* This is a helper function for handleClientsBlockedOnKeys(). It's work
 * is to serve a specific client (receiver) that is blocked on 'key'
 * in the context of the specified 'db', doing the following:
这是一个handleClientsBlockedOnKeys的帮助函数。 它的工作是服务一个特定的客户端(接受者),
该客户端在被指定数据库的上下文中的键所阻塞。做下面的事情
 * 1) Provide the client with the 'value' element.
提供给客户端值为value的元素
 * 2) If the dstkey is not NULL (we are serving a BRPOPLPUSH) also push the
 *    'value' element on the destination list (the LPUSH side of the command).
如果目标键非空(我们在服务一个BRPOPLPUSH操作),同时把值为value的元素压入到目标列表(命令的LPUSH端)
 * 3) Propagate the resulting BRPOP, BLPOP and additional LPUSH if any into
 *    the AOF and replication channel.
将生成的BRPOP、BLPOP和其他LPUSH(如果有)传播到AOF和复制通道
 * The argument 'where' is LIST_TAIL or LIST_HEAD, and indicates if the
 * 'value' element was popped from the head (BLPOP) or tail (BRPOP) so that
 * we can propagate the command properly.
参数where是LIST_TAIL或LIST_HEAD,表明“value”元素是从head(BLPOP)还是tail(BRPOP)弹出的,这样我们可以正确地传播命令
 * The function returns C_OK if we are able to serve the client, otherwise
 * C_ERR is returned to signal the caller that the list POP operation
 * should be undone as the client was not served: This only happens for
 * BRPOPLPUSH that fails to push the value to the destination key as it is
 * of the wrong type. */
这个函数返回C_OK,如果我们能够服务客户端,否则返回C_ERR,给调用者发出信号,弹出操作应该被取消因为客户端不在服务中:
这个只会在函数BRPOPLPUSH中发生,发生在压入值到错误类型的目标键失败。
int serveClientBlockedOnList(client *receiver, robj *key, robj *dstkey, redisDb *db, robj *value, int where)
{
    robj *argv[3];

    if (dstkey == NULL) {
        /* Propagate the [LR]POP operation. */  根据传入参数where 传播左边或右边操作
        argv[0] = (where == LIST_HEAD) ? shared.lpop : 左边弹出
                                          shared.rpop; 右边弹出
        argv[1] = key;
        propagate((where == LIST_HEAD) ? 头部操作,即左边弹出命令
            server.lpopCommand : server.rpopCommand,
            db->id,argv,2,PROPAGATE_AOF|PROPAGATE_REPL);

        /* BRPOP/BLPOP */ 左边或者右边弹出
        addReplyArrayLen(receiver,2);
        addReplyBulk(receiver,key);
        addReplyBulk(receiver,value);

        /* Notify event. */ 通知事件
        char *event = (where == LIST_HEAD) ? "lpop" : "rpop";
        notifyKeyspaceEvent(NOTIFY_LIST,event,key,receiver->db->id);
    } else {
        /* BRPOPLPUSH */
        robj *dstobj =
            lookupKeyWrite(receiver->db,dstkey); 查找数据库
        if (!(dstobj &&
             checkType(receiver,dstobj,OBJ_LIST)))  
        {
        如果  dstobj为空  或者  类型是列表对象
            rpoplpushHandlePush(receiver,dstkey,dstobj,
                value); 将值压入到目标列表
            /* Propagate the RPOPLPUSH operation. */ 传播RPOPLPUSH操作
            argv[0] = shared.rpoplpush;
            argv[1] = key;
            argv[2] = dstkey;
            propagate(server.rpoplpushCommand,
                db->id,argv,3,
                PROPAGATE_AOF|
                PROPAGATE_REPL);

            /* Notify event ("lpush" was notified by rpoplpushHandlePush). */ 通知事件,lpush事件由函数rpoplpushHandlePush通知
            notifyKeyspaceEvent(NOTIFY_LIST,"rpop",key,receiver->db->id);
        } else {
            /* BRPOPLPUSH failed because of wrong
             * destination type. */ BRPOPLPUSH操作失败 因为错误的目标类型
            return C_ERR;
        }
    }
    return C_OK;
}
*********************************************************************************************************************
/* Blocking RPOP/LPOP */ 阻塞右边弹出或左边弹出
void blockingPopGenericCommand(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]); 根据传入的键查找数据库 (gdb调试用的打印键 p *(char *)(c->argv[1]).ptr@10)
        if (o != NULL) { 在库中
            if (o->type != OBJ_LIST) { 非列表类型
                addReply(c,shared.wrongtypeerr);
                return;
            } else {
                if (listTypeLength(o) != 0) { 列表长度不为0
                    /* Non empty list, this is like a non normal [LR]POP. */
                    非空列表,就如一个非一般的左或右弹出
                    char *event = (where == LIST_HEAD) ? "lpop" : "rpop";
                    robj *value = listTypePop(o,where); 弹出元素
                    serverAssert(value != NULL);

                    addReplyArrayLen(c,2);
                    addReplyBulk(c,c->argv[j]);
                    addReplyBulk(c,value);
                    decrRefCount(value); 减少值的引用
                    notifyKeyspaceEvent(NOTIFY_LIST,event,
                                        c->argv[j],c->db->id);
                    if (listTypeLength(o) == 0) {
                        dbDelete(c->db,c->argv[j]);
                        notifyKeyspaceEvent(NOTIFY_GENERIC,"del",
                                            c->argv[j],c->db->id);
                    }
                    signalModifiedKey(c,c->db,c->argv[j]);
                    server.dirty++;

                    /* Replicate it as an [LR]POP instead of B[LR]POP. */
                    将其复制为[LR]POP而不是B[LR]POP事件
                    rewriteClientCommandVector(c,2,
                        (where == LIST_HEAD) ? shared.lpop : shared.rpop,
                        c->argv[j]);
                    return;
                }
            }
        }
    }

    /* If we are inside a MULTI/EXEC and the list is empty the only thing
     * we can do is treating it as a timeout (even with timeout 0). */
如果我们在MULTI/EXEC中,并且列表为空,那么我们唯一能做的就是将其视为超时(即使超时为0,即永不超时)
    if (c->flags & CLIENT_MULTI) {
        addReplyNullArray(c);
        return;
    }

    /* If the list is empty or the key does not exists we must block */
如果列表是空的或者键不存在,我们必须为这个键阻塞
    blockForKeys(c,BLOCKED_LIST,c->argv + 1,c->argc - 2,timeout,NULL,NULL);
}
*********************************************************************************************************************
阻塞左弹出命令
void blpopCommand(client *c) {
    blockingPopGenericCommand(c,LIST_HEAD);
}
*********************************************************************************************************************
阻塞右弹出命令
void brpopCommand(client *c) {
    blockingPopGenericCommand(c,LIST_TAIL);
}
*********************************************************************************************************************
阻塞右弹出 左压入命令
void brpoplpushCommand(client *c) {
    mstime_t timeout;

    if (getTimeoutFromObjectOrReply(c,c->argv[3],&timeout,UNIT_SECONDS)
        != C_OK) return;  获取对象超时时间

    robj *key = lookupKeyWrite(c->db, c->argv[1]); 根据键值查找数据库

    if (key == NULL) { 数据中不存在
        if (c->flags & CLIENT_MULTI) {
            /* Blocking against an empty list in a multi state
             * returns immediately. */
             阻塞对于一个在多状态客户端中的空列表来说,必须立刻返回
            addReplyNull(c);
        } else {  单上下文,可以阻塞
            /* The list is empty and the client blocks. */ 列表是空的,阻塞客户端(等待服务端返回)
            blockForKeys(c,BLOCKED_LIST,c->argv + 1,1,timeout,c->argv[2],NULL);
        }
    } else { 键非空
        if (key->type != OBJ_LIST) { 不是列表类型
            addReply(c, shared.wrongtypeerr);
        } else {
            /* The list exists and has elements, so
             * the regular rpoplpushCommand is executed. */
             列表存在并且有元素,因此执行常规rpoplpushCommand命令
            serverAssertWithInfo(c,key,listTypeLength(key) > 0);
            rpoplpushCommand(c);
        }
    }
}
*********************************************************************************************************************

 



这篇关于redis6.0.5之t_list.c阅读笔记--列表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程