redis代码 支持的数据结构
生活随笔
收集整理的這篇文章主要介紹了
redis代码 支持的数据结构
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
String....................................
typedef char *sds;struct sdshdr {int len;int free;char buf[];
};//buf[]不占結(jié)構(gòu)體shshdr的空間。 都是通過buf獲取對(duì)應(yīng)的sdshdr的指針,來獲取其他成員len/free; 內(nèi)存的申請(qǐng)和釋放也是以sdshdr為申請(qǐng)單位。 Strings支持的操作類似char *; 有cat, cpy, dup, range(sub), cmp, trim等static inline size_t sdslen(const sds s) {struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));/s更前面是個(gè)頭部, 包含len與free信息。return sh->len;
}static inline size_t sdsavail(const sds s) {struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));return sh->free;
}hash table..........................................................................
typedef struct dictEntry {void *key;union {void *val;uint64_t u64;int64_t s64;} v;struct dictEntry *next;
} dictEntry;typedef struct dictType {unsigned int (*hashFunction)(const void *key);void *(*keyDup)(void *privdata, const void *key);void *(*valDup)(void *privdata, const void *obj);int (*keyCompare)(void *privdata, const void *key1, const void *key2);void (*keyDestructor)(void *privdata, void *key);void (*valDestructor)(void *privdata, void *obj);
} dictType;/* This is our hash table structure. Every dictionary has two of this as we* implement incremental rehashing, for the old to the new table. */
typedef struct dictht {dictEntry **table;//一維數(shù)組, 數(shù)組元素是dictEntry* 類型。unsigned long size;//table數(shù)組的長度、桶個(gè)數(shù)unsigned long sizemask;//size - 1用于 與hash(key) ^ sizemask, 得到的值將永遠(yuǎn)<=sizeunsigned long used;//總元素個(gè)數(shù)
} dictht;typedef struct dict {dictType *type;//hash計(jì)算中的一組函數(shù)指針。void *privdata;dictht ht[2];//用于expand/rehash時(shí)的切換;int rehashidx; /* rehashing not in progress if rehashidx == -1 */int iterators; /* number of iterators currently running */
} dict;支持fetch/add/replace/find等操作adlist, linked list.......................................
add/insert/del/search
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;
redisServer? 類似于數(shù)據(jù)庫
redisServer::db[] 類似于數(shù)據(jù)庫的db
redisServer::db->dict類似于table; dict中的<key, value>類似于表記錄。? db只有1個(gè)dict。
rdb。。。。
rdbSaveBackground?? fork子進(jìn)程, 在子進(jìn)程內(nèi)調(diào)用rdbSave遍歷所有并 進(jìn)行寫文件。
aof。。。。優(yōu)先級(jí)比rdb高。
類似于mysql的binlog
總結(jié)
以上是生活随笔為你收集整理的redis代码 支持的数据结构的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redis代码 发布订阅
- 下一篇: protobuf中 repeated[P