A dictionary of Redis data types
tags (space delimited): Redis
A Redis dictionary
The dictionary is also called the symbol table, an associative array (associative array), or a map (map). is an abstract data structure used to hold key-value pairs.
The key of the dictionary is unique, and the operation of the value pair is basically based on key. The database underlying in Redis is implemented using a dictionary, and the additions and deletions to the database are based on dictionaries. A Redis hash key is also based on a dictionary.
The specific implementation is in SRC under the dict.h and dict.c files
Data structure of the dictionary
Hash Table node
/* * 哈希表节点 */typedefstruct dictEntry { // 键 void *key; // 值 union { void *val; uint64_t u64; int64_t s64; } v; // 指向下个哈希表节点,形成链表 struct dictEntry *next;} dictEntry;
Data structure of hash table
/* * 哈希表 */typedefstruct dictht { // 哈希表数组 dictEntry **table; // 哈希表大小 unsignedlong size; // 哈希表大小掩码,用于计算索引值 // 总是等于 size - 1 unsignedlong sizemask; // 该哈希表已有节点的数量 unsignedlong used;} dictht;
Data structure of the dictionary
/* * 字典 */typedefstruct dict { // 类型特定函数 dictType *type; // 私有数据 void *privdata; // 哈希表 dictht ht[2]; // rehash 索引 // 当 rehash 不在进行时,值为 -1 int/* rehashing not in progress if rehashidx == -1 */ // 目前正在运行的安全迭代器的数量 int/* number of iterators currently running */} dict;
One of the dicttype is a struct
/ * Dictionary type specific function * /typedef structDicttype {//function to calculate the hash value unsigned int(*hashfunction) (Const void*key);//Copy key function void* (*keydup) (void*privdata,Const void*key);//Copy value of function void* (*valdup) (void*privdata,Const void*obj);//Comparison key functions int(*keycompare) (void*privdata,Const void*key1,Const void*key2);//Destroy the key function void(*keydestructor) (void*privdata,void*key);//function to destroy values void(*valdestructor) (void*privdata,void*obj);} Dicttype;
// 哈希表 dictht ht[2];
HT here is an array of two data, each of which is a dictht hash table, and in general, only use ht[0],ht[1] is used in rehash case.
Hashing algorithm
When you need to add a pair of key-value pairs to the dictionary, the program first calculates the hash value and index value based on the key of the dictionary, and then, based on the index value, places the hash table node that contains the new key-value pair at the index specified by the hash array.
Redis calculates hash values and indexes in the following ways
// 计算给定键的哈希值#define dictHashKey(d, key) (d)->type->hashFunction(key)// 计算索引值idx = h & d->ht[table].sizemask;
Hash conflict
When two or more than two keys are assigned to the same index of the hash array, they are called hash collisions.
Workarounds for Hash Conflicts:
- Open addressing method (including linear probing method, linear compensation detection method, random detection)
- Zipper method, also known as chain address method
How does Redis resolve the hash conflict?
Chain address method, each hash table node has a next pointer field, multiple hash table nodes can form a single-linked list, the key assigned to the same index can use the next pointer to connect, thus resolving the hash conflict.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
A dictionary of Redis data types