2.redis学习笔记:redis List底层数据实现(通用双端链表)

redis List底层数据实现


redis列表使用两种数据结构左为底层实现:
1.双端链表
2.压缩列表

今天我们来介绍redis中的双端链表,在前边的数据结构章节中已经介绍了通用双端链表的实现,在redis数据库中,双端链表还被很多内部模块所应用:
1.事务模块使用双端链表依序保存输入的命令;
2.服务器模块使用双端链表来保存多个客户端;
3.订阅/发送模块使用双端链表来保存订阅模式的多个客户端;
4.事件模块使用双端链表来保存时间事件(time event);

在redis的双端链表中,数据结构可以分为两个部分,一个是控制信息,一个是链表节点信息

关于redis双端链表的定义和实现在其根目录src/adlist.csrc/adlist.h上进行定义和实现。

//链表控制信息
typedef struct list {
    listNode *head;    //双端链表的头部节点
    listNode *tail;    //双端链表的尾部节点
    void *(*dup)(void *ptr);    //链表节点数据域拷贝
    void (*free)(void *ptr);    //链表节点数据域释放
    int (*match)(void *ptr, void *key);    //链表节点数据域匹配
    unsigned long len;    //双端链表长度
} list;
//链表节点信息
typedef struct listNode {
    struct listNode *prev;    //前驱节点
    struct listNode *next;    //后继节点
    void *value;              //数据域
} listNode;

//迭代器的迭代方向
#define AL_START_HEAD 0
#define AL_START_TAIL 1

值的一提的是链表的遍历操作采用的是迭代器操作,关于迭代器的定义如下所示:

//迭代器定义
typedef struct listIter {
    listNode *next;
    int direction;
} listIter;

不过比较其他开源库的迭代器设计,redis的迭代器设计并不算优雅,在博客<5.数据结构之通用动态数组>章节中,我们介绍了acl库中迭代器的操作,有兴趣的同学可以参考一下。

redis把指针的操作封装成了宏的类型,方便了程序员的使用,不过如果你习惯指针写法也可以采用指针的方式:

//宏定义操作
#define listLength(l) ((l)->len)
#define listFirst(l) ((l)->head)
#define listLast(l) ((l)->tail)
#define listPrevNode(n) ((n)->prev)
#define listNextNode(n) ((n)->next)
#define listNodeValue(n) ((n)->value)

#define listSetDupMethod(l,m) ((l)->dup = (m))
#define listSetFreeMethod(l,m) ((l)->free = (m))
#define listSetMatchMethod(l,m) ((l)->match = (m))

#define listGetDupMethod(l) ((l)->dup)
#define listGetFree(l) ((l)->free)
#define listGetMatchMethod(l) ((l)->match)

redis通用双端链表接口

//接口声明
list *listCreate(void)                        ;    //链表的创建
void listRelease(list *list)                  ;    //链表的释放
list *listAddNodeHead(list *list, void *value);    //链表的头部添加
list *listAddNodeTail(list *list, void *value);    //链表的尾部添加
list *listInsertNode(list *list, listNode *old_node, void *value, int after);    //链表的节点插入
void     listDelNode(list *list, listNode *node)        ;    //链表节点删除
listIter *listGetIterator(list *list, int direction)    ;    //初始化链表迭代器(可以通过direction调整初始化方向,头部或者尾部)
listNode *listNext(listIter *iter)                      ;    //链表的下一个节点(有direction确定是向前遍历还是向后遍历)
void     listReleaseIterator(listIter *iter)            ;    //链表迭代器的释放
list     *listDup(list *orig)                           ;    //链表的拷贝
listNode *listSearchKey(list *list, void *key)          ;   //链表的查找
listNode *listIndex(list *list, long index)             ;    //找到下标为index的链表节点
void     listRewind(list *list, listIter *li)           ;   //让迭代器指向头部
void     listRewindTail(list *list, listIter *li)       ;    //让迭代器指向尾部
void     listRotate(list *list)                         ;    //取出链表的表尾节点,并且插入到头部

redis双端链表的接口实现

//adlist.c包含的头部文件
#include 
#include "adlist.h"
#include "zmalloc.h"   //这个内存配置器需要好好了解,其根据系统的内存配置器进行动态的选取,可以是tcmalloc(google)或者是jemalloc(freeBSD)

接下来我们介绍redis双端链表接口的具体实现:

//adlist.c
list *listCreate(void)
{
    struct list *list;

    if ((list = zmalloc(sizeof(*list))) == NULL)
        return NULL;
    //各个变量的初始化
    list->head = list->tail = NULL;
    list->len = 0;
    list->dup = NULL;
    list->free = NULL;
    list->match = NULL;
    return list;
}

/* Free the whole list.
 *
 * This function can't fail. */
void listRelease(list *list)
{
    unsigned long len;
    listNode *current, *next;

    current = list->head;
    len = list->len;
    while(len--) {
        next = current->next;
        if (list->free){
            list->free(current->value);
        }
        zfree(current);
        current = next;
    }
    zfree(list);
}

/* Add a new node to the list, to head, containing the specified 'value'
 * pointer as value.
 *
 * On error, NULL is returned and no operation is performed (i.e. the
 * list remains unaltered).
 * On success the 'list' pointer you pass to the function is returned. */
list *listAddNodeHead(list *list, void *value)
{
    listNode *node;

    if ((node = zmalloc(sizeof(*node))) == NULL)
        return NULL;
    node->value = value;
    if (list->len == 0) {
        list->head = list->tail = node;
        node->prev = node->next = NULL;
    } else {
        node->prev = NULL;
        node->next = list->head;
        list->head->prev = node;
        list->head = node;
    }
    list->len++;
    return list;
}

/* Add a new node to the list, to tail, containing the specified 'value'
 * pointer as value.
 *
 * On error, NULL is returned and no operation is performed (i.e. the
 * list remains unaltered).
 * On success the 'list' pointer you pass to the function is returned. */
list *listAddNodeTail(list *list, void *value)
{
    listNode *node;

    if ((node = zmalloc(sizeof(*node))) == NULL)
        return NULL;
    node->value = value;
    if (list->len == 0) {
        list->head = list->tail = node;
        node->prev = node->next = NULL;
    } else {
        node->prev = list->tail;
        node->next = NULL;
        list->tail->next = node;
        list->tail = node;
    }
    list->len++;
    return list;
}

list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
    listNode *node;

    if ((node = zmalloc(sizeof(*node))) == NULL)
        return NULL;
    node->value = value;
    if (after) {
        node->prev = old_node;
        node->next = old_node->next;
        if (list->tail == old_node) {
            list->tail = node;
        }
    } else {
        node->next = old_node;
        node->prev = old_node->prev;
        if (list->head == old_node) {
            list->head = node;
        }
    }
    if (node->prev != NULL) {
        node->prev->next = node;
    }
    if (node->next != NULL) {
        node->next->prev = node;
    }
    list->len++;
    return list;
}

/* Remove the specified node from the specified list.
 * It's up to the caller to free the private value of the node.
 *
 * This function can't fail. */
void listDelNode(list *list, listNode *node)
{
    if (node->prev)
        node->prev->next = node->next;
    else
        list->head = node->next;
    if (node->next)
        node->next->prev = node->prev;
    else
        list->tail = node->prev;
    if (list->free){
        list->free(node->value);
    }
    zfree(node);
    list->len--;
}

/* Returns a list iterator 'iter'. After the initialization every
 * call to listNext() will return the next element of the list.
 *
 * This function can't fail. */
listIter *listGetIterator(list *list, int direction)
{
    listIter *iter;

    if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL;
    if (direction == AL_START_HEAD){
        iter->next = list->head;
    }
    else{
        iter->next = list->tail;
    }
    iter->direction = direction;
    return iter;
}

/* Release the iterator memory */
void listReleaseIterator(listIter *iter) {
    zfree(iter);
}

/* Create an iterator in the list private iterator structure */
void listRewind(list *list, listIter *li) {
    li->next = list->head;
    li->direction = AL_START_HEAD;
}

void listRewindTail(list *list, listIter *li) {
    li->next = list->tail;
    li->direction = AL_START_TAIL;
}

/* Return the next element of an iterator.
 * It's valid to remove the currently returned element using
 * listDelNode(), but not to remove other elements.
 *
 * The function returns a pointer to the next element of the list,
 * or NULL if there are no more elements, so the classical usage patter
 * is:
 *
 * iter = listGetIterator(list,);
 * while ((node = listNext(iter)) != NULL) {
 *     doSomethingWith(listNodeValue(node));
 * }
 *
 * */
listNode *listNext(listIter *iter)
{
    listNode *current = iter->next;

    if (current != NULL) {
        if (iter->direction == AL_START_HEAD)
            iter->next = current->next;
        else
            iter->next = current->prev;
    }
    return current;
}

/* Duplicate the whole list. On out of memory NULL is returned.
 * On success a copy of the original list is returned.
 *
 * The 'Dup' method set with listSetDupMethod() function is used
 * to copy the node value. Otherwise the same pointer value of
 * the original node is used as value of the copied node.
 *
 * The original list both on success or error is never modified. */
list *listDup(list *orig)
{
    list *copy;
    listIter *iter;
    listNode *node;

    if ((copy = listCreate()) == NULL)
        return NULL;
    copy->dup = orig->dup;
    copy->free = orig->free;
    copy->match = orig->match;
    iter = listGetIterator(orig, AL_START_HEAD);
    while((node = listNext(iter)) != NULL) {
        void *value;

        if (copy->dup) {
            value = copy->dup(node->value);
            if (value == NULL) {
                listRelease(copy);
                listReleaseIterator(iter);
                return NULL;
            }
        } else
            value = node->value;
        if (listAddNodeTail(copy, value) == NULL) {
            listRelease(copy);
            listReleaseIterator(iter);
            return NULL;
        }
    }
    listReleaseIterator(iter);
    return copy;
}

/* Search the list for a node matching a given key.
 * The match is performed using the 'match' method
 * set with listSetMatchMethod(). If no 'match' method
 * is set, the 'value' pointer of every node is directly
 * compared with the 'key' pointer.
 *
 * On success the first matching node pointer is returned
 * (search starts from head). If no matching node exists
 * NULL is returned. */
listNode *listSearchKey(list *list, void *key)
{
    listIter *iter;
    listNode *node;

    iter = listGetIterator(list, AL_START_HEAD);
    while((node = listNext(iter)) != NULL) {
        if (list->match) {
            if (list->match(node->value, key)) {
                listReleaseIterator(iter);
                return node;
            }
        } else {
            if (key == node->value) {
                listReleaseIterator(iter);
                return node;
            }
        }
    }
    listReleaseIterator(iter);
    return NULL;
}

/* Return the element at the specified zero-based index
 * where 0 is the head, 1 is the element next to head
 * and so on. Negative integers are used in order to count
 * from the tail, -1 is the last element, -2 the penultimate
 * and so on. If the index is out of range NULL is returned. */
listNode *listIndex(list *list, long index) {
    listNode *n;

    if (index < 0) {
        index = (-index)-1;
        n = list->tail;
        while(index-- && n) n = n->prev;
    } else {
        n = list->head;
        while(index-- && n) n = n->next;
    }
    return n;
}

/* Rotate the list removing the tail node and inserting it to the head. */
void listRotate(list *list) {
    listNode *tail = list->tail;

    if (listLength(list) <= 1) return;

    /* Detach current tail */
    list->tail = tail->prev;
    list->tail->next = NULL;
    /* Move it as head */
    list->head->prev = tail;
    tail->prev = NULL;
    tail->next = list->head;
    list->head = tail;
}

小结:
可以看到redis的通用双端链表实现非常的简洁明了,其难点在于要把数据类型从结构的设计中剥离出来,也就是采用void *这样的指针。在接下来的章节中我们将会继续介绍redis的使用,敬请期待。

你可能感兴趣的:(redis,数据结构)