Redis in the dictionary (Dictionary) to achieve

Dictionary
dictionary, also known as the symbol table (Symbol table), association table (associative array) or map (Map), is an abstract data structure for storing key-on (key-value pair) is.

In the dictionary, a key (key) can be associated with a value (value) (mapped to the key value)

Dictionary of each key is unique, the program can be found in a dictionary based on key values ​​associated with it, or to delete the entire key-value pairs based on the updated value of the key by key, or.

Redis the C language used is not built such a data structure, Redis build your own dictionary to achieve.

Redis widely used in the dictionary, such a database is to use the Redis dictionary as the underlying implementation, additions and deletions to the database search operation are also constructed to change in the operation of the dictionary.

A few examples, when we execute the command

Created in the database is a key msg, hello world is the key for this key pair is stored in the database dictionary representatives inside.

When used to represent databases outside the addition, the underlying hash key dictionary is one implementation, a comparison hash key when the key contains more, or key elements of the string is relatively long, it will use the Redis Dictionary implemented as the underlying hash keys.

Dictionary achieve
Redis dictionary implemented as the underlying hash table, a hash table which nodes can have multiple hash tables, each hash table of the node is saved in a dictionary key pair.

1.1 hash table
hash table Redis dictionary definitions used by the dict.h / dictht structure

struct {dictht typedef
    // hash table array
    dictEntry ** Table;
    // hash table size
    unsigned Long size;
    unsigned Long sizemask;
    // number of nodes in the hash table has
    unsigned Long Used;
}
Table property is an array, the array each element is a pointer pointing to dict.h / dictEntry structure, each structure dictEntry holds a key-value pair. size attribute records the size of the hash table, that is, the size of the array table, and records the number of used property hash table there are nodes. sizemask attribute value is always equal size-1, and a hash value of this attribute determined with a key that should be placed in an array indexed table above.

1.2 hash table node
hash table using node dictEntry structure, said structures each dictEntry holds a key-value pair:

struct {dictEntry typedef
   void * Key;
   Union {
      void * Val;
      uint_64_tu64;
      int64_ts64;
     } v
     struct * Next dictEntry;
} dictEntry;
Key attribute holds the key to the key, and v is the attribute holds the key to the value, wherein the key-value pair may be a pointer or an integer.
next property is a hash table points to the next node, the pointers can be multiple hash values are the same key-value pairs are linked together in order to solve the key problem of the conflict.

1.3 dictionary
Redis is represented by a dictionary dict.h / dict structure:

struct {dict typedef
   // Type-specific function
   dictType * type;
   // private data
   void * privdata;
   // hash table
   dictht HT [2];
   // index rehash
   // rehash when not in progress, a value of -1
   int trehashidex
}
of the type property and privdata properties are for different types of key-value pairs, in order to create a multi-state set of dictionaries.

type attribute is a pointer dictType structure, each of the storage cluster configuration dictType operation function for a particular type of key-value pairs, the Redis particular function will be provided for the different types of dictionaries of different uses.
Private attributes are stored and the need for those types of optional parameter passed to a particular function
typedef struct dictType {
       unsigned int * (HashFunction) (const void * Key);
 
       void * (* keyDup) (privdata void *, const void * Key) ;
 
       void * (* valDup) (privdata void *, const void * obj);
 
       ....
 
 
 
}
HT property is an array comprising two terms, the array is a dictht each hash table entry, while the case next, except that only the dictionary ht [0] hash table, ht [1] only in the hash table for ht [0] is to use a hash table rehash.

In addition ht [1] outside, and another rehash of the relevant properties are rehashidx, it recorded a rehash current rate, if not currently performing rehash, then his value should be -1.

Hashing algorithm
when you want a new key is added to the dictionary, the program calculates according to the key value of the key index values and the hash value, then the more the index value, the hash table comprising key-value pairs new node Specifies the index into the hash table array above.

hash = dict->type->hashFunction(key);

index= hash&dict->ht[x].sizemask;

Resolve key conflicts
   when there are two or more the number of keys are assigned to the same array index hash table above, we call these keys clashed zipper method to solve the conflict. Specific solutions can reference implementation of the principle of hashmap. Here is not the specific details. That focused on the following Rehash and progressive hash.


Rehash
    With the increasing operation, save the hash table keys will be gradually increased or decreased in order to allow loading of the hash table stored within a reasonable range, when you save a hash table of key number too when multiple or too small for the size of the hash table of the program dynamically expanded or contracted.

    Expansion and contraction of the hash table work can be done by performing Rehash operation, the Dictionary of the Redis Rehash process is as follows:

 1) is a dictionary HT [1] distribution space hash table, the hash table space depending on the operation to be performed, and ht [0] contains the number of the current key (that is, ht [0] .used value of the property)

    2) holding at ht [0] to all key-value pairs rehash ht [1] above, rehash refers to recalculate the hash value and the index value, then the key-value pair is placed ht [1] specified location hash table on.

    3) When ht [0] contain all key-value pairs are migrated to ht [1] After the release of ht [0], when ht [1] is set to ht [0], and ht [1] Create a new blank Kazakhstan Greek table, in preparation for the next hash.

The following example, to set up the program of FIGS. 4-8 dictionary ht [0] to expand the operation, the program will perform the following steps:

Guess you like

Origin blog.csdn.net/belalds/article/details/92806223