1. 程式人生 > 實用技巧 >Redis原始碼系列(二)

Redis原始碼系列(二)

Redis原始碼系列——雙鏈表

redis底層的資料結構使用了雙鏈表,其實現很簡潔,值得閱讀。

原型 src/adlist.h

/*list node*/
typedef struct listNode{
        struct listNode *prev;
        struct listNode *next;
        /*generic value*/
        void *value;
}listNode;

提供了迭代器

/*list iterator*/
typedef struct listIter{
        listNode *next;
        int direction;
}listIter;

一個連結串列結構的定義如下:

typedef struct list{
        /*head and tail node*/
        listNode *head;
        listNode *tail;
        /*copy a node*/
        void * (*dup)(void *ptr);
        /*free a node*/
        void (*free)(void *ptr);
        /*matching function*/
        int (*match)(void *ptr,void *key);
        /*node number*/
        unsigned long len;
}list;

所以根據這幾個結構體,連結串列應該是這個樣子

下面是幾個巨集定義,提供了一些方便的功能

#define listLength(l) ((l)->len)
#define listFirst(l) ((l)->head)
#define listLast(l) ((l)->last)

#define listPrevNode(n) ((n)->prev)
#define listNextNode(n) ((n)->next)
#define listNodeValue(n)((n)->value)

/*set the dup function pointer*/
#define listSetDupMethod(l,m) ((l)->dup=(m))
/*set the free function pointer*/
#define listSetFreeMethod(l,m) ((l)->free=(m))
/*set the matching function pointer*/
#define listSetMatchMethod(l,m) ((l)->match=(m))

/*return function pointer method of list*/
#define listGetDupMethod(l) ((l)->dup)
#define listGetFreeMethod(l) ((l)->free)
#define listGetMatchMethod(l) ((l)->match)

下面是一些API介面

list *listCreate(void);
void listRelease(list *list);
list *listAddNodeHead(list *list, void *value);
list *listAddNodLeTail(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);

因為有迭代器,還有兩個巨集定義來對迭代器的方向進行控制

/*from head to tail*/
#define AL_START_HEAD 0
/*from tail to head*/
#define AL_START_TAIL 1

實現 src/adlist.c

1.listCreate

建立一個空的雙鏈表

/*
 * 建立一個新的連結串列 
 * 建立成功返回連結串列,失敗返回 NULL 。
 */
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;
}

2.listRelease

釋放整個連結串列

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);
}

3.listAddNodeHead

在表頭插入一個節點,該節點成為新的表頭.在各種操作中,由於headtail這兩個屬性,所以要考慮邊界條件,即會改變這倆屬性的地方.

/*
 * 將一個包含有給定值指標 value 的新節點新增到連結串列的表頭
 * 如果為新節點分配記憶體出錯,那麼返回 NULL
 * 如果執行成功,返回傳入的連結串列指標
 */
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;
}

4.listAddNodeTail

在尾部插入節點,新的節點變成尾節點

/*
 * 將一個包含有給定值指標 value 的新節點新增到連結串列的表尾
 * 如果為新節點分配記憶體出錯,那麼返回 NULL
 * 如果執行成功,返回傳入的連結串列指標
 */
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;
}

5.listInsertNode

在特定的節點前後插入節點

/*
 * 建立一個包含值 value 的新節點,並將它插入到 old_node 的之前或之後
 * after 為 0 ,插入到 old_node 之前。
 * after 為 1 ,插入到 old_node 之後。
 */
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;
}

6. listDelNode

從連結串列中刪除節點

/*
 * 從連結串列 list 中刪除給定節點 node 
 */
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--;
}

7.listGetIterator

/*
 * 為給定連結串列建立一個迭代器,
 * 之後每次對這個迭代器呼叫 listNext 都返回被迭代到的連結串列節點
 * direction 引數決定了迭代器的迭代方向:
 *  AL_START_HEAD :從表頭向表尾迭代
 *  AL_START_TAIL :從表尾想表頭迭代
 */
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;
}

8.listReleaseIterator

void listReleaseIterator(listIter *iter) {
    zfree(iter);
}

9.listRewind

/*
 * 將迭代器的方向設定為 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;
}

10.listNext

迭代器的使用,返回當前迭代到的元素

/*
 * 返回迭代器當前所指向的節點。
 * !刪除當前節點是允許的,但不能修改連結串列裡的其他節點。
 * 函式要麼返回一個節點,要麼返回 NULL ,常見的用法是:
 */
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;
}

11.listDup

複製整個連結串列,然後返回一個副本

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;
}

12.listSearchKey

匹配list中的值與傳入的key,對比由match或者直接對比指標實現

/* 
 * 查詢連結串列 list 中值和 key 匹配的節點。
 * 對比操作由連結串列的 match 函式負責進行,
 * 如果沒有match 函式,
 * 那麼直接通過對比值的指標來決定是否匹配。
 * 如果匹配成功,那麼第一個匹配的節點會被返回。
 * 否則返回 NULL 。
 */
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;
}

13.listIndex

返回指定索引上的值,可以為負數,表示從尾部開始

/*
 * 返回連結串列在給定索引上的值。
 * 索引以 0 為起始,也可以是負數, -1 表示連結串列最後一個節點
 * 如果索引超出範圍,返回 NULL 。
 */
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;
}

14.listRotate

將表尾變成表頭

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;
}