Implementation of the chain table for Redis data types
The underlying implementation of the Redis list is a doubly linked list, with the source code under SRC adlist.h and adlist.c
Node data structure of linked list
/* * 双端链表节点 */ typedefstruct listNode { // 前置节点 struct listNode *prev; // 后置节点 struct listNode *next; // 节点的值 void *value; } listNode;
Linked list data structure
/ * Double-ended linked list structure */typedef struct List{//Table header nodeListNode *head;//Footer nodeListNode *tail;//Node value copy function void* (*dup) (void*PTR);//Node value deallocation function void(* Free)(void*PTR);//Node value comparison function int(*match) (void*ptr,void*key);the number of nodes included in the// list unsigned LongLen;}List;
The specific features of the linked list Redis
- List key
- Publish and subscribe
- Slow query
- Monitor
Time complexity of some functions
//Returns the number of nodes included in the given list//T = O (1)#define LISTLENGTH (L) ((l)->len)//Returns the table header node for the given list//T = O (1)#define LISTFIRST (L) ((l)->head)//Returns the footer node of the given list//T = O (1)#define LISTLAST (L) ((l)->tail)//Returns the predecessor node of a given node//T = O (1)#define LISTPREVNODE (n) ((n)->prev)//Returns the backend node of a given node//T = O (1)#define LISTNEXTNODE (n) ((n)->next)//Returns the value of the given node//T = O (1)#define LISTNODEVALUE (n) ((n)->value)//Set the value copy function of the linked list L to M//T = O (1)#define LISTSETDUPMETHOD (L,m) ((l)->dup = (m))//Set the value release function of the linked list L to M//T = O (1)#define LISTSETFREEMETHOD (L,m) ((l)->free = (m))//Set the comparison function of the list to M//T = O (1)#define LISTSETMATCHMETHOD (L,m) ((l)->match = (m))//Returns the value of the given list copy function//T = O (1)#define LISTGETDUPMETHOD (L) ((l)->dup)//Returns the value deallocation function for a given list//T = O (1)#define LISTGETFREE (L) ((l)->free)//Returns the value comparison function for a given list//T = O (1)#define LISTGETMATCHMETHOD (L) ((l)->match)
/ * * Create a new linked list * * Create a successful return list, Failure returns NULL. * * T = O (1) */List *Listcreate (void) {structList *List;//Allocate memory if((List =Zmalloc (sizeof (*List)))== NULL)return NULL;//Initialize Properties List -Head= List -Tail= NULL;List -Len= 0;List -Dup= NULL;List -Free= NULL;List -Match = NULL;return List;}
Note that the allocation of the malloc function here is self-fulfilling zmalloc
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linked list of Redis data types