redisコードがサポートするデータ構造
2826 ワード
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, <=size
unsigned 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の
rdb....
rdbSaveBackground forkサブプロセスは、サブプロセス内でrdbSaveを呼び出してすべてを巡り、ファイルを書きます.
aof....優先度はrdbより高い.
mysqlに似たbinlog