Redis链表
Redis中使用的链表为双端链表,带有携带附加信息的头节点。
redis/src/adlist
源码位置,.h,.c文件不足500行
1. 链表节点
value的类型为void*类型的指针
typedef struct listNode {
struct listNode *prev;
struct listNode *next;
void *value;
} listNode;
2. 头节点
- 头节点带有head和tail指针,可以从两个方向遍历链表;
- len属性O(1)获取链表长度
- dup 为拷贝节点value的函数指针,用于链表复制
- free为释放节点value的函数指针,用于配合销毁节点
- match 比较节点value值的函数指针,用于指定节点的查找
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;
3. 迭代器
typedef struct listIter {
listNode *next;
int direction;
} listIter;
4. api
以下例举几个常用的操作链表的函数
4.1 删除链表
- 从头节点指向的节点开始删除
- 对每个节点进行两步操作, 若是free指针不为空,则释放节点的value指针指向的内容;然后释放该节点内存
- 删除头节点
void listEmpty(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;
}
list->head = list->tail = NULL;
list->len = 0;
}
/* Free the whole list.
*
* This function can't fail. */
void listRelease(list *list)
{
listEmpty(list);
zfree(list);
}
4.2 插入
- 正常向一个链表中插入/删除一个节点时,需要注意是否是首个节点或尾节点
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;
}
}
// 将插入节点的前节点的next和后节点的pre指针指向node
if (node->prev != NULL) {
node->prev->next = node;
}
if (node->next != NULL) {
node->next->prev = node;
}
list->len++;
return list;
}
4.3 迭代器遍历链表
- 首先创建一个迭代器,根据迭代方向确定指向的节点
- 调用api改变迭代器的指向方向
- 遍历结束,销毁迭代器
// 根据方向创建一个迭代器
listIter *listGetIterator(list *list, int direction)
{
listIter *iter;
if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL;
// 正向遍历,迭代器的next指向头节点
if (direction == AL_START_HEAD)
iter->next = list->head;
else
iter->next = list->tail;
iter->direction = direction;
return iter;
}
// 改变迭代器next指向节点
// 返回值为改变之前的内容
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;
}
listIter *itr = listGetIterator(target_list, AL_START_HEAD);
while(itr=listNext(itr)!=NULL){
// do itr
}
4.4 链表拷贝
以链表的拷贝来看一下迭代器的使用和几个函数指针的使用
- 首先创建一个空链表
- 迭代器指向原链表第一个节点
- 对每个节点执行拷贝后插入到新链表的尾部
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;
// 从原链表头开始遍历
listRewind(orig, &iter);
while((node = listNext(&iter)) != NULL) {
void *value;
if (copy->dup) {
// 若是存在指定的拷贝函数
value = copy->dup(node->value);
if (value == NULL) {
listRelease(copy);
return NULL;
}
} else {
// 不存在指定的拷贝函数,则直接进行浅拷贝
value = node->value;
}
// 拷贝的节点添加到链表尾部
if (listAddNodeTail(copy, value) == NULL) {
/* Free value if dup succeed but listAddNodeTail failed. */
if (copy->free) copy->free(value);
listRelease(copy);
return NULL;
}
}
return copy;
}
5.总结
- redis的双端链表的设计更贴近于自己平时碰到的链表设计, 头节点通过增加len,tail,head等属性方便获取链表长度,从正向和反向去遍历链表
- ListNode的value类型为void*类型的指针,相对来说更加灵活,配置链表的三个函数指针使用,保证内存的安全
- 链表在Redis的列表键、监视器、慢查询、发布订阅、维护客户端状态等方面广泛使用
- 实际在操作链表时,需要注意头节点,尾节点的一些特殊情况