【源码-5】rehash

本文深入探讨了Redis中的rehash过程,它用于在内存中迁移数据,确保高效的数据存储。rehash操作按桶单位进行,逐步将数据从旧哈希表移动到新哈希表,以解决冲突并优化性能。通过dictIsRehashing函数检查迁移状态,当所有键迁移完成后返回0。这个过程涉及到Redis内部的两个哈希表,确保在迁移期间服务的连续性。
摘要由CSDN通过智能技术生成

rehash是为了迁移redis的数据,redis在内存中是类似于java的hashmap形式存储的,相当于一个链表数组,数据是存放在dict里面的,也就是下面rehash的入口参数,dict里面存放两张hash表,rehash就是实现数据在这两张表之间进行迁移的。
rehash的源码在此,入口参数为dict和int
dictIsRehashing返回1代表仍然有key需要从老表迁移到新表,0代表迁移完了。
rehash是以桶(bucket)为单位进行迁移的,n是代表步数,每步完成一个bucket的迁移。一个bucket对应的是hash数组中的一个链表。

/* Performs N steps of incremental rehashing. Returns 1 if there are still
 * keys to move from the old to the new hash table, otherwise 0 is returned.
 *
 * Note that a rehashing step consists in moving a bucket (that may have more
 * than one key as we use chaining) from the old to the new hash table, however
 * since part of the hash table may be composed of empty spaces, it is not
 * guaranteed that this function will rehash even a single bucket, since it
 * will visit at max N*10 empty buckets in total, otherwise the amount of
 * work it does would be unbound and the function may block for a long time. */

int dictRehash(dict *d, int n) {
   
    int empty_visits = n*10; /* Max number of empty buckets to visit. */
    if (!dictIsRehashing(d)) return 0;
    // 分n步执行,used参数表示节点数量,如果还没执行完n步且旧表还有数据,就执行循环。
    while(n-- && d->ht[0].used != 0) {
   
        dictEntry *de, *nextde;

        /* Note that rehashidx can't overflow as we are sur
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值