Usage of dict in redis and redisdict
Usage of dict in redis
Redis uses dict to represent a dictionary typedef struct dict {// similar to sds, define a set of function pointers dictType * type; // Private Data void * privdata; // hash table dictht ht [2]; // when no rehash operation is performed, the value is-1 int rehashidx; /* rehashing not in progress if rehashidx =-1 * // number of iterators int iterators;/* number of iterators currently running */} dict; this dict has two important data structures: 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 dictType defines six function pointers. The meanings of these function pointers are clear. The second is: Hash table typedef struct dictht {// hash table array dictEntry ** table; unsigned long size; // always equal to size-1 unsigned long sizemask; // number of existing nodes in the hash table unsigned long used;} dictht; The table indicates the array of the hash table, the Data Types of a single node are defined as follows: typedef struct dictEntry {// key void * key; // you can see that the hash table can store three types of data types: union {void * val; uint64_t u64; int64_t s64;} v; // points to the next hash table node to form the chain table struct dictEntry * next;} dictEntry; call dictCreate to create a new dictionary, it seems that when creating a dict, six function pointers and private data dict * dictCreate (dictType * type, void * privDataPtr) must be created) {# apply for memory and clear dict * d = zmalloc (sizeof (* d); _ dictInit (d, type, privDataPtr); return d ;} int _ dictInit (dict * d, dictType * type, void * privDataPtr) {// specify the initial value _ dictReset (& d-> ht [0]) for two hash tables; _ dictReset (& d-> ht [1]); // assign the initial value or default value d-> type = type; d-> privdata = privDataPtr to the member variable based on the parameter; d-> rehashidx =-1; d-> iterators = 0; return DICT_ OK;} // assign the initial value static void _ dictReset (dictht * ht) to the two hash tables) {ht-> table = NULL; ht-> size = 0; ht-> sizemask = 0; ht-> used = 0 ;}