redis列表键的底层实现之链表

来源:互联网 发布:java获取list泛型类型 编辑:程序博客网 时间:2024/06/10 19:15

       最近在啃redis的源码,看到列表键的底层实现之一就是链表。当一个链表键包含了数据比较多的元素,又或者列表中包含的元素都是比较长的字符串,redis就会使用链表作为列表键的底层实现。除了链表键之外,发布与订阅、慢查询、监视器等功能也用到了链表,redis服务器本身也使用了链表来保存多个客户端的状态信息,以及使用链表来构建客户端输出缓冲区,在后续都会和大家解析源码,接下来就给大家解析一下redis源码中是如何实现双端链表的:

     声明一下我看的源码是redis-3.0.7版本的,在官网都可以下载的

      我们首先来看双端链表的头文件:

          [root@localhost src]# vim adlist.h

/* adlist.h - A generic doubly linked list implementation * * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * *   * Redistributions of source code must retain the above copyright notice, *     this list of conditions and the following disclaimer. *   * Redistributions in binary form must reproduce the above copyright *     notice, this list of conditions and the following disclaimer in the *     documentation and/or other materials provided with the distribution. *   * Neither the name of Redis nor the names of its contributors may be used *     to endorse or promote products derived from this software without *     specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */#ifndef __ADLIST_H__#define __ADLIST_H__/* Node, List, and Iterator are the only data structures used currently. *///链表节点信息typedef struct listNode {    //前置节点    struct listNode *prev;    //后置节点    struct listNode *next;    //节点的值    void *value;} listNode;//双端链表迭代器typedef struct listIter {    //当前迭代到的节点    listNode *next;    //迭代方向    int direction;} listIter;//双端链表结构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;/* Functions implemented as macros *///返回给定链表所包含的节点数量#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)//将链表l的复制函数设置为m#define listSetDupMethod(l,m) ((l)->dup = (m))//将链表l的释放函数设置为m#define listSetFreeMethod(l,m) ((l)->free = (m))//将链表l的对比函数设置为m#define listSetMatchMethod(l,m) ((l)->match = (m))//返回给定链表l的复制函数#define listGetDupMethod(l) ((l)->dup)//返回给定链表l的释放函数#define listGetFree(l) ((l)->free)//返回给定链表l的对比函数#define listGetMatchMethod(l) ((l)->match)/* Prototypes *///创建链表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);//listNode *listNext(listIter *iter);void listReleaseIterator(listIter *iter);//复制一个给定链表的副本list *listDup(list *orig);//查找并返回链表中包含给定值的节点listNode *listSearchKey(list *list, void *key);//返回链表在给定索引上的节点listNode *listIndex(list *list, long index);//让迭代器指向头部void listRewind(list *list, listIter *li);//让迭代器指向尾部void listRewindTail(list *list, listIter *li);//将链表从表尾节点删除,然后将被弹出的节点插入到链表的表头,//成为新的表头节点void listRotate(list *list);/* Directions for iterators *///从表头迭代到表尾#define AL_START_HEAD 0//从表尾迭代到表头  #define AL_START_TAIL 1#endif /* __ADLIST_H__ */

 接下来我们看具体的实现:

  [root@localhost src]# vim adlist.c

/* adlist.c - A generic doubly linked list implementation * * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * *   * Redistributions of source code must retain the above copyright notice, *     this list of conditions and the following disclaimer. *   * Redistributions in binary form must reproduce the above copyright *     notice, this list of conditions and the following disclaimer in the *     documentation and/or other materials provided with the distribution. *   * Neither the name of Redis nor the names of its contributors may be used *     to endorse or promote products derived from this software without *     specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */#include <stdlib.h>#include "adlist.h"#include "zmalloc.h"/* Create a new list. The created list can be freed with * AlFreeList(), but private value of every node need to be freed * by the user before to call AlFreeList(). * * On error, NULL is returned. Otherwise the pointer to the new list. *//* * 链表的创建(初始化链表结构信息) *  * * */list *listCreate(void){   //定义链表结构体的指针    struct list *list;    //使用zmalloc进行内存的分配,分配失败则返回NULL    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指向链表的头节点    current = list->head;    //len记录链表的节点个数    len = list->len;    //释放策略:    //    先销毁链表的节点信息    //    再销毁链表的结构信息    while(len--) {        //首先记录当前节点的后继节点        next = current->next;        //先判断结构信息里的释放函数是否为真        //如果为真,就要先释放链表节点的数据信息        if (list->free) list->free(current->value);        //释放节点        zfree(current);        //指针后移        current = next;    }//直到len为0,代表所有的链表节点都已释放完    //最后释放链表结构信息的结构体    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;    //申请链表节点的内存,如果申请失败返回NULL    if ((node = zmalloc(sizeof(*node))) == NULL)        return NULL;    //给链表节点的数据域赋值    node->value = value;    if (list->len == 0) {        //(1)如果当前链表节点个数为0(也就说明当前链表为NULL)        //将链表的表头、表尾都赋为node        list->head = list->tail = node;        //node节点的前驱、后继都为NULL        node->prev = node->next = NULL;    } else {        //(2)链表不为空时,        //使node的前驱为NULL        node->prev = NULL;        //node的next域赋值为表头结点,(从而使得给定节点插入到了表头节点之前)        node->next = list->head;        //由于是双端链表,所以要把之前的头节点的前驱置为node        list->head->prev = node;        //更新新的表头节点        list->head = node;    }    //链表节点个数加1    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;    //分配链表节点的内存,如果分配失败,返回NULL    if ((node = zmalloc(sizeof(*node))) == NULL)        return NULL;    //给链表节点的数据域赋值    node->value = value;    if (list->len == 0) {        //(1)链表为空的情况        list->head = list->tail = node;        node->prev = node->next = NULL;    } else {        //(2)链表不为空时:        //将node的前驱设为表尾节点        node->prev = list->tail;        //将node的next域置为NULL        node->next = NULL;        //将之前的表尾节点的next域赋值为node        list->tail->next = node;        //更新新的表尾节点        list->tail = node;    }    //链表节点个数加1    list->len++;    return list;}/* * 将一个给定值的节点插入到指定节点的前边或后边 * * * */list *listInsertNode(list *list, listNode *old_node, void *value, int after) {    //定义链表节点    listNode *node;    //为链表节点分配内存,如果分配失败,则返回NULL    if ((node = zmalloc(sizeof(*node))) == NULL)        return NULL;    //为链表节点的数据域赋值    node->value = value;    if (after) {        //(1)如果after > 0,插入到指定节点的后面        //将node的前驱置为指定节点old_node        node->prev = old_node;        //将node的后继置为指定节点old_node的后继        node->next = old_node->next;        //如果指定节点为表尾节点,则需更新表尾节点        if (list->tail == old_node) {            list->tail = node;        }    } else {        //(2)插入到指定节点的前面        //将node的next域指定为old_node        node->next = old_node;        //将node的前驱置为指定节点old_node的前驱        node->prev = old_node->prev;        //如果指定节点为表头节点时,则需更新表头节点        if (list->head == old_node) {            list->head = node;        }    }    //如果node的前驱不为空时,则给node的前驱节点的后继置为node    if (node->prev != NULL) {        node->prev->next = node;    }    //如果node节点的后继不为空,则给node的后继节点的前驱置为node    if (node->next != NULL) {        node->next->prev = node;    }    //链表的节点个数加1    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的前驱的后继置为node的后继节点        node->prev->next = node->next;    else        //当指定节点为表头节点时,        //只用修改表头节点        list->head = node->next;    //如果指定节点node的后继为真时    if (node->next)        //将指定节点node的后继节点的前驱设置为node节点的前驱节点        node->next->prev = node->prev;    else        //node为表尾节点时,        //更新表尾节点        list->tail = node->prev;    //判断链表结构的释放函数    if (list->free) list->free(node->value);    //释放节点node    zfree(node);    //链表节点的个数减1    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;    //为迭代器分配内存空间,如果分配失败,则返回NULL    if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL;    //判断迭代方向,AL_START_HEAD为定义的宏,其值为0(从表头迭代到表尾)    if (direction == AL_START_HEAD)        //如果direction为0时,设置迭代器的当前迭代节点为表头节点        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 *//* * 将迭代器的方向设置为AL_START_HEAD * 并将迭代器指针重新指向表头节点 * * */void listRewind(list *list, listIter *li) {    li->next = list->head;    li->direction = AL_START_HEAD;}/* * * 将迭代器的方向设置为AL_START_TAIL * 并将迭代器指针重新指向表尾节点 * * */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,<direction>); * while ((node = listNext(iter)) != NULL) { *     doSomethingWith(listNodeValue(node)); * } * * *//* * 返回迭代器当前所指向的节点 * 删除当前节点是允许的,但不能修改链表里的其他节点 * 函数要么返回一个节点,要么返回NULL * */listNode *listNext(listIter *iter){   //current指针指向迭代器当前中的当前节点    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;    //创建链表结构信息,如果copy == NULL,则创建失败,返回NULL    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;        //判断链表结构的dup成原是否为真        if (copy->dup) {            //如果dup成员为真,说明用户自定义了数据拷贝策略            //调用用户的数据拷贝策略            value = copy->dup(node->value);            if (value == NULL) {                //如果拷贝失败                //释放链表                listRelease(copy);                //释放迭代器                listReleaseIterator(iter);                //返回空                return NULL;            }        } else            //如果dup成员为假时,说明用户没有定义数据拷贝策略            //将当前节点的数据赋值给value            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. *//* * 查找链表list中值和key匹配的节点 * *  * * */listNode *listSearchKey(list *list, void *key){   //定义链表迭代器    listIter *iter;    //定义链表节点    listNode *node;    //创建链表迭代器,(从表头至表尾)    iter = listGetIterator(list, AL_START_HEAD);    //node指向迭代器的当前节点    while((node = listNext(iter)) != NULL) {        //如果链表控制信息中的成员match为真        //说明用户已指定了两个链表节点的对比策略        if (list->match) {            //调用用户指定的对比策略            if (list->match(node->value, key)) {                //如果匹配成功                //释放迭代器                listReleaseIterator(iter);                //返回node                return node;            }        } else {            //链表控制信息中match为空,说明用户没有指定对比策略            //我们就判断两节点数据域的值是否相同            if (key == node->value) {                //如果相同,释放迭代器                listReleaseIterator(iter);                //返回node                return node;            }        }    }    //以上都不满足时,说明没有找到    //释放迭代器    listReleaseIterator(iter);    //返回NULL    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. *//* * 返回链表在给定索引上的值 * * 如果索引超出范围,返回NULL * */listNode *listIndex(list *list, long index) {    listNode *n;    if (index < 0) {        //(1)如果索引为小于0,将index更改为正        index = (-index)-1;        //将n指向链表的表尾        n = list->tail;        //如果index为正并且n不为空        //n指向n的前驱        while(index-- && n) n = n->prev;    } else {        //(2)索引index为正        //n指向链表的表头        n = list->head;        //同样,如果index为正并且n不为空,        //n指向n的next域        while(index-- && n) n = n->next;    }    //返回n    return n;}/* Rotate the list removing the tail node and inserting it to the head. *//* * 取出链表的表尾节点,并将它移动表头,成为新的表头节点 * * */void listRotate(list *list) {    //定义tail节点使其指向表尾节点    listNode *tail = list->tail;    //如果链表为只有一个节点或没有节点时, 直接返回    //listlength(list)用来获取链表节点的个数    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;}
其中zmalloc.h是redis对内存的一个配置,在以后的博客中回和大家解析的!!

下面我们来看redis的链表实现的特性可以总结如下:
   《1》双端:链表节点带有prev和next指针,获取某个节点的前置节点和后置节点的时间复杂度都是o(1)。

   《2》无环:表头节点的prev指针和表尾节点的next指针都指向NULL,对链表的访问以NULL为终点。

   《3》带表头指针和表尾指针:通过list结构的head指针和tail指针,程序获取链表的表头节点和表尾节点的时间复杂度为o(1)。

   《4》带链表长度的计数器:程序使用list结构的len属性来对list持有的链表节点进行计数,程序获取链表中节点数量的复杂度                      o(1)。

   《5》多态:链表节点使用void *指针来保存节点值,并且可以通过list结构的dup、free、match三个属性为节点值设置类型特定函数,所以链表可以用于保存各种不同类型的值。

对于双端链表的实现,我基本上每一行都有注释,欢迎大家查错,互相交流!!!!!!!

0 0
原创粉丝点击