1. Redis uses a hashtable with two tables (table[0] and table[1]) to store dictionary entries, with buckets for storing entries.
2. When adding an entry, it first checks if the key already exists using linear probing, and if not, adds the new entry. If the load factor is exceeded, it will rehash entries between the two tables.
3. The rehashing process moves entries one by one from the old table to the new table by doubling the number of buckets and changing the hash index of keys, to improve performance and reduce collisions.
3. dictAdd
dictAddRaw 1. Redis uses two tables: table[0], table[1]
2. Tables have buckets for dictEntry
dictSetVal
dictAddRaw
_dictRehashStep 1. dictAddRaw actually makes new dirEntry.
2. If rehash is possbile, conduct rehashing.
3. _dictKeyIndex check the item is existed. If it is existed,
dictAddRaw will fail.
_dictKeyIndex 4. Used++
_dictSetKey
4. 1. dictRehash’s parameter n is the variable that how
dictRehashStep many buckets will be rehashed. For performance, it is
always 1.
2. If Ht[0]used is 0, rehashng is finished. Ht[0] = ht[1]
and return;
dictRehash
3. while(d->ht[0].table[d->rehashidx] == NULL) d-
>rehashidx++; if current bucket is NULL, go next
bucket.
4. Bucket size is always increased as twice.
5. And divide dictEntry with hashmask. For example, at
the first time. mask is 3 and hash value 2, 5 were in
same bucket . After doubling bucket size, mask will
be 7, so hash value 2 will be in bucket #2, and 5 will
be in bucket #5