美文网首页
redis rehash

redis rehash

作者: wncbbnk | 来源:发表于2019-08-16 11:59 被阅读0次

Add

如果不在rehash,加在0表
如果在rehash,加到1表上

    ht = dictIsRehashing(d) ? &d->ht[1] : &d->ht[0];

Delete

先删0表。如果在rehash,继续删1表

    for (table = 0; table <= 1; table++) {

Update

如果key不存在,那就是Add(如果在rehash,加到1表上)
如果key已经存在,借用Search的逻辑。(在0表就改0表,在1表就改1表)

Search

如果查找的key已经存在了,那么:
不在rehash,查0表;在rehash,查01表
如果查找的key不存在,那么:
不在rehash,返回0表索引;在rehash,返回1表索引(为增做准备)

/* Returns the index of a free slot that can be populated with
 * a hash entry for the given 'key'.
 * If the key already exists, -1 is returned
 * and the optional output parameter may be filled.
 *
 * Note that if we are in the process of rehashing the hash table, the
 * index is always returned in the context of the second (new) hash table. */
static long _dictKeyIndex(dict *d, const void *key, uint64_t hash, dictEntry **existing)
{
    unsigned long idx, table;
    dictEntry *he;
    if (existing) *existing = NULL;

    /* Expand the hash table if needed */
    if (_dictExpandIfNeeded(d) == DICT_ERR)
        return -1;
    for (table = 0; table <= 1; table++) {
        idx = hash & d->ht[table].sizemask;
        /* Search if this slot does not already contain the given key */
        he = d->ht[table].table[idx];
        while(he) {
            if (key==he->key || dictCompareKeys(d, key, he->key)) {
                if (existing) *existing = he;
                return -1;
            }
            he = he->next;
        }
        if (!dictIsRehashing(d)) break;
    }
    return idx;
}

Summary

增: 在rehash,一定加在1表
删: 可以简单认为01两表都会删
改(key已经存在,做修改): 在0表就改0表,在1表就改1表
改(key不存在): 那就是增
查: 可以简单认为01两表都会查(一定最多只在一个表中出现)

相关文章

  • 思考题

    1.java hashMap和redis map的rehash有什么区别? Java hashMap rehash...

  • 从零手写缓存框架(14)redis渐进式rehash详解

    redis 的 rehash 设计 本文思维导图如下: HashMap 的 rehash 回顾 读过 HashMa...

  • Redis:rehash

    Redis解决键冲突:使用的是链地址法 随着操作的不断执行, 哈希表保存的键值对会逐渐地增多或者减少, 为了让哈希...

  • redis rehash

    Add 如果不在rehash,加在0表如果在rehash,加到1表上 Delete 先删0表。如果在rehash,...

  • Redis rehash

    rehash分扩容和缩容,两个过程互斥 交替使用h0和h1,来回搬迁,类似jvm的s1和s2 dict结构中的re...

  • Redis笔记

    Redis核心技术与实战 rehash·装载因子(entry个数 除以 hash桶个数) 渐进式rehash·每处...

  • redis笔记

    数据结构 SDS 字典 index确定 渐进式rehash 为了缓解一次性rehash带来的性能问题,redis提...

  • redis rehash机制

    redis触发rehash条件 服务器目前没有在执行BGSAVE命令或者BGREWRITEAOF命令,并且哈希表的...

  • redis中的rehash?

    Redis中所有数据都有key-value,这是通过哈希表实现的,redis的字典数据结构保存了两张哈希表,采取了...

  • 转载:美团针对Redis Rehash机制的探索和实践

    转载:美团针对Redis Rehash机制的探索和实践 原链接:https://tech.meituan.com/...

网友评论

      本文标题:redis rehash

      本文链接:https://www.haomeiwen.com/subject/kusfsctx.html