Redis dict and_rehash

11
Redis Dict Data Structure [email protected]

description

 

Transcript of Redis dict and_rehash

Page 1: Redis dict and_rehash

Redis Dict Data Structure

[email protected]

Page 2: Redis dict and_rehash

Redis Insert Flow

Page 3: Redis dict and_rehash

dictAdd

dictSetVal

1. Redis uses two tables: table[0], table[1] 2. Tables have buckets for dictEntry

dictAddRaw

dictAddRaw

_dictKeyIndex

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. 4. Used++

_dictSetKey

_dictRehashStep

Page 4: Redis dict and_rehash

dictRehashStep 1. dictRehash’s parameter n is the variable that how

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;

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

dictRehash

Page 5: Redis dict and_rehash

Redis Dict is Hash

Page 6: Redis dict and_rehash

Dynamic Hash Expand twice

Linear probing

Page 7: Redis dict and_rehash

Bucket #0

Bucket #1

Bucket #2

Bucket #3

0

1

2

3

Add 0,1,2,3

Page 8: Redis dict and_rehash

Bucket #0

Bucket #1

Bucket #2

Bucket #3

4

1

2

3

Add 4 with line Probing

0

Page 9: Redis dict and_rehash

Bucket #0

Bucket #1

Bucket #2

Bucket #3

4

1

2

3

Rehashing with HT[1]

0

Bucket #0

Bucket #1

Bucket #2

Bucket #3

Bucket #4

Bucket #5

Bucket #6

Bucket #7

Page 10: Redis dict and_rehash

Bucket #0

Bucket #1

Bucket #2

Bucket #3

1

2

3

Rehashing one step #1

Bucket #0

Bucket #1

Bucket #2

Bucket #3

Bucket #4

Bucket #5

Bucket #6

Bucket #7

4

0

Used--; Used--;

Page 11: Redis dict and_rehash

After all rehashing

ht[0] = ht[1] reset ht[1] For next rehashing