/* 这是一个很关键的结构体,将 ziplist 节点信息填充成一个 zlentry 结构体,方便后面进行函数操作 * 需要注意这并不是一个 ziplist 节点在内存中实际的编码布局,只是为了方便我们使用 * */ typedefstructzlentry { /* 存储下面 prevrawlen 所需要的字节数 */ unsignedint prevrawlensize; /* Bytes used to encode the previous entry len*/ /* 存储前一个节点的字节长度 */ unsignedint prevrawlen; /* Previous entry len. */ /* 存储下面 len 所需要的字节数 */ unsignedint lensize; /* Bytes used to encode this entry type/len. For example strings have a 1, 2 or 5 bytes header. Integers always use a single byte.*/ /* 存储当前节点的字节长度 */ unsignedint len; /* Bytes used to represent the actual entry. For strings this is just the string length while for integers it is 1, 2, 3, 4, 8 or 0 (for 4 bit immediate) depending on the number range. */ /* prevrawlensize + lensize 当前节点的头部字节, * 其实是 prevlen + encoding 两项占用的字节数 */ unsignedint headersize; /* prevrawlensize + lensize. */ /* 存储当前节点的数据编码格式 */ unsignedchar encoding; /* Set to ZIP_STR_* or ZIP_INT_* depending on the entry encoding. However for 4 bits immediate integers this can assume a range of values and must be range-checked. */ /* 指向当前节点开头第一个字节的指针 */ unsignedchar *p; /* Pointer to the very start of the entry, that is, this points to prev-entry-len field. */ } zlentry;
/* The actual Redis Object */ #define OBJ_STRING 0 /* String object. */ #define OBJ_LIST 1 /* List object. */ #define OBJ_SET 2 /* Set object. */ #define OBJ_ZSET 3 /* Sorted set object. */ #define OBJ_HASH 4 /* Hash object. */
/* Objects encoding. Some kind of objects like Strings and Hashes can be * internally represented in multiple ways. The 'encoding' field of the object * is set to one of this fields for this object. */ #define OBJ_ENCODING_RAW 0 /* Raw representation */ #define OBJ_ENCODING_INT 1 /* Encoded as integer */ #define OBJ_ENCODING_HT 2 /* Encoded as hash table */ #define OBJ_ENCODING_ZIPMAP 3 /* No longer used: old hash encoding. */ #define OBJ_ENCODING_LINKEDLIST 4 /* No longer used: old list encoding. */ #define OBJ_ENCODING_ZIPLIST 5 /* No longer used: old list/hash/zset encoding. */ #define OBJ_ENCODING_INTSET 6 /* Encoded as intset */ #define OBJ_ENCODING_SKIPLIST 7 /* Encoded as skiplist */ #define OBJ_ENCODING_EMBSTR 8 /* Embedded sds string encoding */ #define OBJ_ENCODING_QUICKLIST 9 /* Encoded as linked list of listpacks */ #define OBJ_ENCODING_STREAM 10 /* Encoded as a radix tree of listpacks */ #define OBJ_ENCODING_LISTPACK 11 /* Encoded as a listpack */
typedefstructredisObject { unsigned type:4; unsigned encoding:4; unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or * LFU data (least significant 8 bits frequency * and most significant 16 bits access time). */ int refcount; void *ptr; } robj;
/* Response buffer */ size_t buf_peak; /* Peak used size of buffer in last 5 sec interval. */ mstime_t buf_peak_last_reset_time; /* keeps the last time the buffer peak value was reset */ int bufpos; size_t buf_usable_size; /* Usable size of buffer. */ char *buf; ...... } client;
服务端数据结构,位于 server.h 中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
structredisServer{ char *configfile; /* 配置文件绝对路径 */ int dbnum; /* 数据库的数目 */ redisDb *db; /* 数据库数组 */ dict *commands; /* 命令字典,放置命令名称到命令对象的映射 */ aeEventLoop *el; /* 事件循环 */ int port; /* 服务器监听端口号,可通过参数 port 配置,默认为 6379 */ char * bindaddr[CONFIG_BINDADDR_MAX]; /* 绑定的所有 IP */ int bindaddr_count; /* 用户配置的 IP 地址数目 */ socketFds ipfd; /* 存储所有 IP 地址创建的 socket 文件描述符 */ list *clients; /* 当前连接的所有客户端 */ int maxidletime; /* 最大空闲时间 */ ...... }