您的位置:首页 > 理论基础 > 数据结构算法

redis代码 支持的数据结构

2014-05-06 18:19 351 查看
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中的<key, value>类似于表记录。 db只有1个dict。

rdb。。。。

rdbSaveBackground fork子进程, 在子进程内调用rdbSave遍历所有并 进行写文件。

aof。。。。优先级比rdb高。

类似于mysql的binlog
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: