redis代码 支持的数据结构
生活随笔
收集整理的這篇文章主要介紹了
redis代码 支持的数据结构
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
String....................................
typedef char *sds;struct sdshdr {int len;int free;char buf[];
};//buf[]不占結構體shshdr的空間。 都是通過buf獲取對應的sdshdr的指針,來獲取其他成員len/free; 內存的申請和釋放也是以sdshdr為申請單位。 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更前面是個頭部, 包含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;//一維數組, 數組元素是dictEntry* 類型。unsigned long size;//table數組的長度、桶個數unsigned long sizemask;//size - 1用于 與hash(key) ^ sizemask, 得到的值將永遠<=sizeunsigned long used;//總元素個數
} dictht;typedef struct dict {dictType *type;//hash計算中的一組函數指針。void *privdata;dictht ht[2];//用于expand/rehash時的切換;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? 類似于數據庫
redisServer::db[] 類似于數據庫的db
redisServer::db->dict類似于table; dict中的<key, value>類似于表記錄。? db只有1個dict。
rdb。。。。
rdbSaveBackground?? fork子進程, 在子進程內調用rdbSave遍歷所有并 進行寫文件。
aof。。。。優先級比rdb高。
類似于mysql的binlog
總結
以上是生活随笔為你收集整理的redis代码 支持的数据结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redis代码 发布订阅
- 下一篇: protobuf中 repeated[P