您的当前位置:首页正文

redis源码分析[05]-字典结构及其算法-dict.c

2024-11-07 来源:个人技术集锦

 

//节点结构,自带一个指向下一个节点的指针,用来防重
typedef struct dictEntry {
    void *key;
    union {
        void *val;
        uint64_t u64;
        int64_t s64;
        double d;
    } v;
    struct dictEntry *next;
} dictEntry;
//每个hash表都需要定义hash算法,key复制,值复制,key比较,key销毁,值销毁的方法
typedef struct dictType {
    unsigned int (*hashFunction)(const void *key);
    void *(*keyDup)(void *privdata, const void *key);
    void *(*valDup)(void *privdata, const void *obj);
    int (*keyCompare)(void *privdata, const void *key1, const void *key2);
    void (*keyDestructor)(void *privdata, void *key);
    void (*valDestructor)(void *privdata, void *obj);
} dictType;

//hash表的结构,
typedef struct dictht {
    dictEntry **table;//指向第一个节点的指针的指针
    unsigned long size;//hash表的容量
    unsigned long sizemask;
    unsigned long used;//hash表的当前使用量
} dictht;
//字典
typedef struct dict {
    dictType *type;
    void *privdata;//私有数据,保存着dictType结构中函数的参数。
    dictht ht[2];//两张hash表
    long rehashidx; /* 扩容标志,-1未扩容,其它则表示正在扩容中 */
    int iterators; /* 正在迭代的迭代器数量 */
} dict;

/* 迭代器 */
typedef struct dictIterator {
    dict *d;//正在迭代的字典
    long index;
    int table, safe;
    dictEntry *entry, *nextEntry;//当前,下一个键值对
    long long fingerprint;
} dictIterator;
//字典是否可重新设置容量标志
static int dict_can_resize = 1;
static unsigned int dict_force_resize_ratio = 5;//强制扩容比率 已用键值对/hash表大小,这个值无用

/* -------------------------- private prototypes ---------------------------- */

static int _dictExpandIfNeeded(dict *ht);
static unsigned long _dictNextPower(unsigned long size);
static int _dictKeyIndex(dict *ht, const void *key);
static int _dictInit(dict *ht, dictType *type, void *privDataPtr);

/* -------------------------- hash functions -------------------------------- */

/* 针对整数的hash算法 */
unsigned int dictIntHashFunction(unsigned int key)
{
    key += ~(key << 15);
    key ^=  (key >> 10);
    key +=  (key << 3);
    key ^=  (key >> 6);
    key += ~(key << 11);
    key ^=  (key >> 16);
    return key;
}
//hash种子
static uint32_t dict_hash_function_seed = 5381;

void dictSetHashFunctionSeed(uint32_t seed) {
    dict_hash_function_seed = seed;
}

uint32_t dictGetHashFunctionSeed(void) {
    return dict_hash_function_seed;
}

/* 
针对字符串的hash算法
 */
unsigned int dictGenHashFunction(const void *key, int len) {
    /* 'm' and 'r' are mixing constants generated offline.
     They're not really 'magic', they just happen to work well.  */
    uint32_t seed = dict_hash_function_seed;
    const uint32_t m = 0x5bd1e995;
    const int r = 24;

    /* Initialize the hash to a 'random' value */
    uint32_t h = seed ^ len;

    /* Mix 4 bytes at a time into the hash */
    const unsigned char *data = (const unsigned char *)key;

    while(len >= 4) {
        uint32_t k = *(uint32_t*)data;

        k *= m;
        k ^= k >> r;
        k *= m;

        h *= m;
        h ^= k;

        data += 4;
        len -= 4;
    }

    /* Handle the last few bytes of the input array  */
    switch(len) {
    case 3: h ^= data[2] << 16;
    case 2: h ^= data[1] << 8;
    case 1: h ^= data[0]; h *= m;
    };

    /* Do a few final mixes of the hash to ensure the last few
     * bytes are well-incorporated. */
    h ^= h >> 13;
    h *= m;
    h ^= h >> 15;

    return (unsigned int)h;
}

/* 不区分大小写的字符串hash算法 */
unsigned int dictGenCaseHashFunction(const unsigned char *buf, int len) {
    unsigned int hash = (unsigned int)dict_hash_function_seed;

    while (len--)
        hash = ((hash << 5) + hash) + (tolower(*buf++)); /* hash * 33 + c */
    return hash;
}

/* ----------------------------- API implementation ------------------------- */

/* 字典重置,表为空,各项数据为0 */
static void _dictReset(dictht *ht)
{
    ht->table = NULL;
    ht->size = 0;
    ht->sizemask = 0;
    ht->used = 0;
}

/* 创建hash表,并附私有数据 */
dict *dictCreate(dictType *type,
        void *privDataPtr)
{
    dict *d = zmalloc(sizeof(*d));

    _dictInit(d,type,privDataPtr);
    return d;
}

/* 一个字典两个hash表 */
int _dictInit(dict *d, dictType *type,
        void *privDataPtr)
{
    _dictReset(&d->ht[0]);
    _dictReset(&d->ht[1]);
    d->type = type;
    d->privdata = privDataPtr;
    d->rehashidx = -1;//扩容进行标志,-1表示没有在扩容
    d->iterators = 0;
    return DICT_OK;
}

/* 扩容操作 */
int dictResize(dict *d)
{
    int minimal;
    //如果禁止扩容或者正在扩容,则报错
    if (!dict_can_resize || dictIsRehashing(d)) return DICT_ERR;
    minimal = d->ht[0].used;
    if (minimal < DICT_HT_INITIAL_SIZE)
        minimal = DICT_HT_INITIAL_SIZE;
    return dictExpand(d, minimal);
}

/* 扩容 */
int dictExpand(dict *d, unsigned long size)
{
    dictht n; /* the new hash table */
    unsigned long realsize = _dictNextPower(size);//size一定是2的整数倍

    /* 如果正在扩容或已用大小超过扩容大小,则报错,出现异常了 */
    if (dictIsRehashing(d) || d->ht[0].used > size)
        return DICT_ERR;

    /* 如果扩容后的大小和当前大小一致,报错 */
    if (realsize == d->ht[0].size) return DICT_ERR;

    /* 重置表的大小,掩码,重新申请放键值对指针的数组内存,当前使用量重置为0 */
    n.size = realsize;
    n.sizemask = realsize-1;
    n.table = zcalloc(realsize*sizeof(dictEntry*));
    n.used = 0;

    /* 如果旧表的内存不存在,说明旧表容量是0 */
    if (d->ht[0].table == NULL) {
        d->ht[0] = n;
        return DICT_OK;
    }

    /* 将n赋值给第2张表,以备扩容用 */
    d->ht[1] = n;
    d->rehashidx = 0;
    return DICT_OK;
}

/* 重新创建hash表,进行n步 */
int dictRehash(dict *d, int n) {
    int empty_visits = n*10; /* 最多访问n*10个空值 */
    if (!dictIsRehashing(d)) return 0;//如果没有标志在进行rehash则退出

    while(n-- && d->ht[0].used != 0) {
        dictEntry *de, *nextde;

        /*  */
        assert(d->ht[0].size > (unsigned long)d->rehashidx);
        while(d->ht[0].table[d->rehashidx] == NULL) {//如果是空值就继续找
            d->rehashidx++;
            if (--empty_visits == 0) return 1;
        }
        de = d->ht[0].table[d->rehashidx];//要重新hash的键值对
        /* Move all the keys in this bucket from the old to the new hash HT */
        while(de) {
            unsigned int h;

            nextde = de->next;
            /* Get the index in the new hash table */
            h = dictHashKey(d, de->key) & d->ht[1].sizemask;//重新hash
            de->next = d->ht[1].table[h];
            d->ht[1].table[h] = de;
            d->ht[0].used--;
            d->ht[1].used++;
            de = nextde;
        }
        d->ht[0].table[d->rehashidx] = NULL;
        d->rehashidx++;//下一个rehash的key的id
    }

    /* 检查是否已经全部rehash了 */
    if (d->ht[0].used == 0) {
        zfree(d->ht[0].table);//释放第一张表
        d->ht[0] = d->ht[1];//把第二张表赋值给第一张表
        _dictReset(&d->ht[1]);//重置第二张表
        d->rehashidx = -1;//重置rehashidx标志
        return 0;
    }

    /* More to rehash... */
    return 1;
}
//返回当前时间的毫秒
long long timeInMilliseconds(void) {
    struct timeval tv;

    gettimeofday(&tv,NULL);
    return (((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000);
}

/* rehash一段时间,超过ms毫秒则返回,返回rehash的键值数 */
int dictRehashMilliseconds(dict *d, int ms) {
    long long start = timeInMilliseconds();
    int rehashes = 0;

    while(dictRehash(d,100)) {
        rehashes += 100;
        if (timeInMilliseconds()-start > ms) break;
    }
    return rehashes;
}

/* 如果遍历器个数为0,则进行一步rehash */
static void _dictRehashStep(dict *d) {
    if (d->iterators == 0) dictRehash(d,1);
}

/* 在字典中添加一个键值对,key=>val */
int dictAdd(dict *d, void *key, void *val)
{
    dictEntry *entry = dictAddRaw(d,key);

    if (!entry) return DICT_ERR;
    dictSetVal(d, entry, val);
    return DICT_OK;
}

/* 在字典中添加一个键值对,键是key,并返回这个键值对的指针 */
dictEntry *dictAddRaw(dict *d, void *key)
{
    int index;
    dictEntry *entry;
    dictht *ht;

    if (dictIsRehashing(d)) _dictRehashStep(d);//如果rehash标志正在进行,则rehash一步

    /* 算出key在本字典中首个稳定的hash表中的索引值 */
    if ((index = _dictKeyIndex(d, key)) == -1)
        return NULL;

    /* 如果rehash正在进行,则用第二个表,否则用第一个表 */
    ht = dictIsRehashing(d) ? &d->ht[1] : &d->ht[0];
    entry = zmalloc(sizeof(*entry));//申请内存
    entry->next = ht->table[index];
    ht->table[index] = entry;//给hash表中的槽赋值
    ht->used++;//计数

    /* Set the hash entry fields. */
    dictSetKey(d, entry, key);//设置请申请的内存的键为key
    return entry;
}

/* 替换一个键为key的值,替换成val */
int dictReplace(dict *d, void *key, void *val)
{
    dictEntry *entry, auxentry;

    /* 如果添加成功,说明key并不存在,则替换失败 */
    if (dictAdd(d, key, val) == DICT_OK)
        return 1;
    /* 如果添加不成功,则表示原key已存在 */
    entry = dictFind(d, key);
    /* 替换值,并回收旧值 */
    auxentry = *entry;
    dictSetVal(d, entry, val);
    dictFreeVal(d, &auxentry);
    return 0;
}

/* 返回key在字典中对应的键值对的指针 */
dictEntry *dictReplaceRaw(dict *d, void *key) {
    dictEntry *entry = dictFind(d,key);

    return entry ? entry : dictAddRaw(d,key);
}

/* 回收一个键值对 */
static int dictGenericDelete(dict *d, const void *key, int nofree)
{
    unsigned int h, idx;
    dictEntry *he, *prevHe;
    int table;

    if (d->ht[0].size == 0) return DICT_ERR; /*空表 */
    if (dictIsRehashing(d)) _dictRehashStep(d);
    h = dictHashKey(d, key);

    for (table = 0; table <= 1; table++) {
        idx = h & d->ht[table].sizemask;
        he = d->ht[table].table[idx];
        prevHe = NULL;
        while(he) {
            if (key==he->key || dictCompareKeys(d, key, he->key)) {
                /* Unlink the element from the list */
                if (prevHe)
                    prevHe->next = he->next;
                else
                    d->ht[table].table[idx] = he->next;
                if (!nofree) {//是否要销毁,0销毁,1不回收
                    dictFreeKey(d, he);
                    dictFreeVal(d, he);
                }
                zfree(he);
                d->ht[table].used--;
                return DICT_OK;
            }
            prevHe = he;
            he = he->next;
        }
        if (!dictIsRehashing(d)) break;//是否要到第二张表中找
    }
    return DICT_ERR; /* 到此表示没找着 */
}
//删除并回收一个键值对
int dictDelete(dict *ht, const void *key) {
    return dictGenericDelete(ht,key,0);
}
//删除不回收一个键值对
int dictDeleteNoFree(dict *ht, const void *key) {
    return dictGenericDelete(ht,key,1);
}

/* 销毁一个hash表 */
int _dictClear(dict *d, dictht *ht, void(callback)(void *)) {
    unsigned long i;

    /* 回收所有的键值对 */
    for (i = 0; i < ht->size && ht->used > 0; i++) {
        dictEntry *he, *nextHe;
        //如果要销毁的是65535以后的数据,则需要调用回调方法+私有数据
        if (callback && (i & 65535) == 0) callback(d->privdata);

        if ((he = ht->table[i]) == NULL) continue;//找到相应的键值对
        while(he) {
            nextHe = he->next;
            dictFreeKey(d, he);//用专用的方法销毁key
            dictFreeVal(d, he);//用专用的方法销毁value
            zfree(he);//回收键值对的内存
            ht->used--;//已用数减一
            he = nextHe;//销毁同一链上的下一个键值对
        }
    }
    /* 回收hash表的槽数组 */
    zfree(ht->table);
    /* 重置hash表 */
    _dictReset(ht);
    return DICT_OK; /* 回收是不会失败的,只能成功 */
}

/* 销毁一个字典,一个字典两张表 */
void dictRelease(dict *d)
{
    _dictClear(d,&d->ht[0],NULL);
    _dictClear(d,&d->ht[1],NULL);
    zfree(d);
}

//在字典中查找键为key的键值对
dictEntry *dictFind(dict *d, const void *key)
{
    dictEntry *he;
    unsigned int h, idx, table;

    if (d->ht[0].used + d->ht[1].used == 0) return NULL; /*两个表的使用量为0,表示字典为空*/
    if (dictIsRehashing(d)) _dictRehashStep(d);//如果字典正在rehash,则rehash一步
    h = dictHashKey(d, key);//算出这个key在字典中对应的索引值
    for (table = 0; table <= 1; table++) {
        idx = h & d->ht[table].sizemask;//算出在hash表中槽的位置
        he = d->ht[table].table[idx];
        while(he) {
            if (key==he->key || dictCompareKeys(d, key, he->key))//如果key一致,则返回这个键值对的指针,否则查找链上的下一个键值对
                return he;
            he = he->next;
        }
        if (!dictIsRehashing(d)) return NULL;//如果字典不在rehash,则说明数据都在第一个表,不用查第二个表,此时还未找到,就说明不存在,否则可以继续到第二个表中找
    }
    return NULL;
}
//找到key在字典中对应的值
void *dictFetchValue(dict *d, const void *key) {
    dictEntry *he;

    he = dictFind(d,key);
    return he ? dictGetVal(he) : NULL;
}

/* 取一个字典的指纹 */
long long dictFingerprint(dict *d) {
    long long integers[6], hash = 0;
    int j;

    integers[0] = (long) d->ht[0].table;
    integers[1] = d->ht[0].size;
    integers[2] = d->ht[0].used;
    integers[3] = (long) d->ht[1].table;
    integers[4] = d->ht[1].size;
    integers[5] = d->ht[1].used;

    for (j = 0; j < 6; j++) {
        hash += integers[j];
        /* For the hashing step we use Tomas Wang's 64 bit integer hash. */
        hash = (~hash) + (hash << 21); // hash = (hash << 21) - hash - 1;
        hash = hash ^ (hash >> 24);
        hash = (hash + (hash << 3)) + (hash << 8); // hash * 265
        hash = hash ^ (hash >> 14);
        hash = (hash + (hash << 2)) + (hash << 4); // hash * 21
        hash = hash ^ (hash >> 28);
        hash = hash + (hash << 31);
    }
    return hash;
}
//创建一个字典d的迭代器
dictIterator *dictGetIterator(dict *d)
{
    dictIterator *iter = zmalloc(sizeof(*iter));

    iter->d = d;
    iter->table = 0;
    iter->index = -1;
    iter->safe = 0;
    iter->entry = NULL;
    iter->nextEntry = NULL;
    return iter;
}
//创建一个安全的字典d的迭代器,对于一个安全的迭代器来说,是不允许字典中途销毁的,除非本迭代器不存在了,则字典才可以销毁
dictIterator *dictGetSafeIterator(dict *d) {
    dictIterator *i = dictGetIterator(d);

    i->safe = 1;
    return i;
}
//根据迭代器iter返回下一个键值对
dictEntry *dictNext(dictIterator *iter)
{
    while (1) {
        if (iter->entry == NULL) {//当前的键值对为空,表示迭代器还没开始迭代,或者当前值为空
            dictht *ht = &iter->d->ht[iter->table];//找到hash表
            if (iter->index == -1 && iter->table == 0) {//首次迭代
                if (iter->safe)//安全迭代器需要给字典做标记,字典在销毁时会检查
                    iter->d->iterators++;
                else
                    iter->fingerprint = dictFingerprint(iter->d);//算指纹
            }
            iter->index++;//更新索引
            //如果迭代器的当前索引大于当前被迭代的哈希表的大小,说明当前表已迭代完
            if (iter->index >= (long) ht->size) {
                if (dictIsRehashing(iter->d) && iter->table == 0) {//如果正在rehash,则进入下一张表继续迭代,索引置0,否则退出迭代
                    iter->table++;
                    iter->index = 0;
                    ht = &iter->d->ht[1];
                } else {
                    break;
                }
            }
            iter->entry = ht->table[iter->index];
        } else {
            iter->entry = iter->nextEntry;
        }
        if (iter->entry) {//找到键值对并更新下一个键值对的指针
            iter->nextEntry = iter->entry->next;
            return iter->entry;//返回找到的不为空的键值对的指针
        }
    }
    return NULL;
}
//回收一个迭代器
void dictReleaseIterator(dictIterator *iter)
{
    if (!(iter->index == -1 && iter->table == 0)) {
        if (iter->safe)
            iter->d->iterators--;
        else
            assert(iter->fingerprint == dictFingerprint(iter->d));
    }
    zfree(iter);
}

/* 随机从字典中取出一个key */
dictEntry *dictGetRandomKey(dict *d)
{
    dictEntry *he, *orighe;
    unsigned int h;
    int listlen, listele;

    if (dictSize(d) == 0) return NULL;
    if (dictIsRehashing(d)) _dictRehashStep(d);
    if (dictIsRehashing(d)) {
        do {
            /* We are sure there are no elements in indexes from 0
             * to rehashidx-1 */
            h = d->rehashidx + (random() % (d->ht[0].size +
                                            d->ht[1].size -
                                            d->rehashidx));
            he = (h >= d->ht[0].size) ? d->ht[1].table[h - d->ht[0].size] :
                                      d->ht[0].table[h];
        } while(he == NULL);
    } else {
        do {
            h = random() & d->ht[0].sizemask;
            he = d->ht[0].table[h];
        } while(he == NULL);
    }

    /* 随机从一条链上取出一个数据 */
    listlen = 0;
    orighe = he;
    while(he) {
        he = he->next;
        listlen++;
    }
    listele = random() % listlen;
    he = orighe;
    while(listele--) he = he->next;
    return he;
}

/*   */
unsigned int dictGetSomeKeys(dict *d, dictEntry **des, unsigned int count) {
    unsigned long j; /* internal hash table id, 0 or 1. */
    unsigned long tables; /* 1 or 2 tables? */
    unsigned long stored = 0, maxsizemask;
    unsigned long maxsteps;

    if (dictSize(d) < count) count = dictSize(d);
    maxsteps = count*10;

    /* Try to do a rehashing work proportional to 'count'. */
    for (j = 0; j < count; j++) {
        if (dictIsRehashing(d))
            _dictRehashStep(d);
        else
            break;
    }

    tables = dictIsRehashing(d) ? 2 : 1;
    maxsizemask = d->ht[0].sizemask;
    if (tables > 1 && maxsizemask < d->ht[1].sizemask)
        maxsizemask = d->ht[1].sizemask;

    /* Pick a random point inside the larger table. */
    unsigned long i = random() & maxsizemask;
    unsigned long emptylen = 0; /* Continuous empty entries so far. */
    while(stored < count && maxsteps--) {
        for (j = 0; j < tables; j++) {
            /* Invariant of the dict.c rehashing: up to the indexes already
             * visited in ht[0] during the rehashing, there are no populated
             * buckets, so we can skip ht[0] for indexes between 0 and idx-1. */
            if (tables == 2 && j == 0 && i < (unsigned long) d->rehashidx) {
                /* Moreover, if we are currently out of range in the second
                 * table, there will be no elements in both tables up to
                 * the current rehashing index, so we jump if possible.
                 * (this happens when going from big to small table). */
                if (i >= d->ht[1].size) i = d->rehashidx;
                continue;
            }
            if (i >= d->ht[j].size) continue; /* Out of range for this table. */
            dictEntry *he = d->ht[j].table[i];

            /* Count contiguous empty buckets, and jump to other
             * locations if they reach 'count' (with a minimum of 5). */
            if (he == NULL) {
                emptylen++;
                if (emptylen >= 5 && emptylen > count) {
                    i = random() & maxsizemask;
                    emptylen = 0;
                }
            } else {
                emptylen = 0;
                while (he) {
                    /* Collect all the elements of the buckets found non
                     * empty while iterating. */
                    *des = he;
                    des++;
                    he = he->next;
                    stored++;
                    if (stored == count) return stored;
                }
            }
        }
        i = (i+1) & maxsizemask;
    }
    return stored;
}

/* 字典遍历相关方法 */
static unsigned long rev(unsigned long v) {
    unsigned long s = 8 * sizeof(v); // bit size; must be power of 2
    unsigned long mask = ~0;
    while ((s >>= 1) > 0) {
        mask ^= (mask << s);
        v = ((v >> s) & mask) | ((v << s) & ~mask);
    }
    return v;
}

/* 字典遍历  https:///gqtcgq/article/details/50533336 */
unsigned long dictScan(dict *d,
                       unsigned long v,//要遍历的索引位置
                       dictScanFunction *fn,//自定义扫描回调方法
                       void *privdata)
{
    dictht *t0, *t1;
    const dictEntry *de;
    unsigned long m0, m1;

    if (dictSize(d) == 0) return 0;

    if (!dictIsRehashing(d)) {//如果没有进行rehash,则只操作第一个表
        t0 = &(d->ht[0]);
        m0 = t0->sizemask;

        /* Emit entries at cursor */
        de = t0->table[v & m0];//找到当前这个槽位,然后处理数据
        while (de) {
            fn(privdata, de);
            de = de->next;//只遍历同一链上的
        }

    } else {//即使在进行rehash,则两个表都要操作
        t0 = &d->ht[0];
        t1 = &d->ht[1];

        /* 保证t0是最大的表 */
        if (t0->size > t1->size) {
            t0 = &d->ht[1];
            t1 = &d->ht[0];
        }

        m0 = t0->sizemask;
        m1 = t1->sizemask;

        /* Emit entries at cursor */
        de = t0->table[v & m0];
        while (de) {
            fn(privdata, de);
            de = de->next;
        }

        /* Iterate over indices in larger table that are the expansion
         * of the index pointed to by the cursor in the smaller table */
        do {
            /* 遍历小表 */
            de = t1->table[v & m1];
            while (de) {
                fn(privdata, de);
                de = de->next;
            }

            /* Increment bits not covered by the smaller mask */
            v = (((v | m0) + 1) & ~m0) | (v & m0);

            /* Continue while bits covered by mask difference is non-zero */
        } while (v & (m0 ^ m1));
    }

    /* Set unmasked bits so incrementing the reversed cursor
     * operates on the masked bits of the smaller table */
    v |= ~m0;

    /* Increment the reverse cursor */
    v = rev(v);
    v++;
    v = rev(v);

    return v;
}

/* ------------------------- private functions ------------------------------ */

/* 如果有必要就扩容 */
static int _dictExpandIfNeeded(dict *d)
{
    /* 如果正在rehash则不需要扩容 */
    if (dictIsRehashing(d)) return DICT_OK;

    /* 如果容量为0,则扩容到初始值 */
    if (d->ht[0].size == 0) return dictExpand(d, DICT_HT_INITIAL_SIZE);

    /* 如果容量已用尽,则扩容至原来的2倍 */
    if (d->ht[0].used >= d->ht[0].size &&
        (dict_can_resize ||
         d->ht[0].used/d->ht[0].size > dict_force_resize_ratio))
    {
        return dictExpand(d, d->ht[0].used*2);
    }
    return DICT_OK;
}

/* 比size大的hash表的可用容量值 */
static unsigned long _dictNextPower(unsigned long size)
{
    unsigned long i = DICT_HT_INITIAL_SIZE;

    if (size >= LONG_MAX) return LONG_MAX;
    while(1) {
        if (i >= size)
            return i;
        i *= 2;
    }
}

/* 在一个字典中的首个稳定的hash表中找出key的索引 */
static int _dictKeyIndex(dict *d, const void *key)
{
    unsigned int h, idx, table;
    dictEntry *he;

    /* 如果在是否扩容这一块发生错误,表示扩容失败,则下一步无法进行 */
    if (_dictExpandIfNeeded(d) == DICT_ERR)
        return -1;
    /* 用专用方法计算key的hash值 */
    h = dictHashKey(d, key);
    for (table = 0; table <= 1; table++) {
        idx = h & d->ht[table].sizemask;//根据掩码算出槽的位置
        
        he = d->ht[table].table[idx];
        while(he) {//如果槽被占用,则he指向链的下一个
            if (key==he->key || dictCompareKeys(d, key, he->key))
                return -1;
            he = he->next;
        }
        if (!dictIsRehashing(d)) break;//如果此表不在rehash则表示此表稳定,则不再去第二个表中找槽
    }
    return idx;//返回key在其中一个稳定的hash表中的槽的位置
}
//清空字典,重置各项状态数据
void dictEmpty(dict *d, void(callback)(void*)) {
    _dictClear(d,&d->ht[0],callback);
    _dictClear(d,&d->ht[1],callback);
    d->rehashidx = -1;
    d->iterators = 0;
}
//设置字典为可扩容
void dictEnableResize(void) {
    dict_can_resize = 1;
}
//设置字典为不可扩容
void dictDisableResize(void) {
    dict_can_resize = 0;
}

/* ------------------------------- Debugging ---------------------------------*/

#define DICT_STATS_VECTLEN 50
//用来记录hash表中空槽的数量,链表的不同长度的数量的统计,输出文字描述
size_t _dictGetStatsHt(char *buf, size_t bufsize, dictht *ht, int tableid) {
    unsigned long i, slots = 0, chainlen, maxchainlen = 0;
    unsigned long totchainlen = 0;
    unsigned long clvector[DICT_STATS_VECTLEN];
    size_t l = 0;

    if (ht->used == 0) {
        return snprintf(buf,bufsize,
            "No stats available for empty dictionaries\n");
    }

    /* Compute stats. */
    for (i = 0; i < DICT_STATS_VECTLEN; i++) clvector[i] = 0;
    for (i = 0; i < ht->size; i++) {
        dictEntry *he;

        if (ht->table[i] == NULL) {
            clvector[0]++;
            continue;
        }
        slots++;
        /* For each hash entry on this slot... */
        chainlen = 0;
        he = ht->table[i];
        while(he) {
            chainlen++;
            he = he->next;
        }
        clvector[(chainlen < DICT_STATS_VECTLEN) ? chainlen : (DICT_STATS_VECTLEN-1)]++;
        if (chainlen > maxchainlen) maxchainlen = chainlen;
        totchainlen += chainlen;
    }

    /* Generate human readable stats. */
    l += snprintf(buf+l,bufsize-l,
        "Hash table %d stats (%s):\n"
        " table size: %ld\n"
        " number of elements: %ld\n"
        " different slots: %ld\n"
        " max chain length: %ld\n"
        " avg chain length (counted): %.02f\n"
        " avg chain length (computed): %.02f\n"
        " Chain length distribution:\n",
        tableid, (tableid == 0) ? "main hash table" : "rehashing target",
        ht->size, ht->used, slots, maxchainlen,
        (float)totchainlen/slots, (float)ht->used/slots);

    for (i = 0; i < DICT_STATS_VECTLEN-1; i++) {
        if (clvector[i] == 0) continue;
        if (l >= bufsize) break;
        l += snprintf(buf+l,bufsize-l,
            "   %s%ld: %ld (%.02f%%)\n",
            (i == DICT_STATS_VECTLEN-1)?">= ":"",
            i, clvector[i], ((float)clvector[i]/ht->size)*100);
    }

    /* Unlike snprintf(), teturn the number of characters actually written. */
    if (bufsize) buf[bufsize-1] = '\0';
    return strlen(buf);
}
//获取字典的状态
void dictGetStats(char *buf, size_t bufsize, dict *d) {
    size_t l;
    char *orig_buf = buf;
    size_t orig_bufsize = bufsize;

    l = _dictGetStatsHt(buf,bufsize,&d->ht[0],0);//先输出第一个表的信息
    buf += l;
    bufsize -= l;
    if (dictIsRehashing(d) && bufsize > 0) {//如果正在rehash则输出第二个hash表的信息
        _dictGetStatsHt(buf,bufsize,&d->ht[1],1);
    }
    /* Make sure there is a NULL term at the end. */
    if (orig_bufsize) orig_buf[orig_bufsize-1] = '\0';
}

 

Top