Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

补充 dict.c 的注释以便阅读理解 #119

Draft
wants to merge 1 commit into
base: 7.0-cn-annotated
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ dict *dictCreate(dictType *type)
}

/* Initialize the hash table */
/* 字典实现使用了两个哈希表,一个作为主表,另一个作为副表
* 所以调用2次 _dictReset */
int _dictInit(dict *d, dictType *type)
{
_dictReset(d, 0);
Expand Down Expand Up @@ -235,6 +237,7 @@ int dictTryExpand(dict *d, unsigned long size) {
* rehashidx 记录上一次重哈希结束时遍历到哈希表元素的下标
* */
int dictRehash(dict *d, int n) {
/* 防止死循环的常见做法 设置循环次数上限 empty_visits */
int empty_visits = n*10; /* Max number of empty buckets to visit. */
if (!dictIsRehashing(d)) return 0;

Expand All @@ -244,13 +247,15 @@ int dictRehash(dict *d, int n) {
/* Note that rehashidx can't overflow as we are sure there are more
* elements because ht[0].used != 0 */
/* 当 rehashidx 等于哈希表大小时,表明重哈希完成 */
/* 找出一个非空槽位,遇到空槽时 rehashidx 直接加1 */
assert(DICTHT_SIZE(d->ht_size_exp[0]) > (unsigned long)d->rehashidx);
while(d->ht_table[0][d->rehashidx] == NULL) {
d->rehashidx++;
if (--empty_visits == 0) return 1;
}
de = d->ht_table[0][d->rehashidx];
/* Move all the keys in this bucket from the old to the new hash HT */
/* 因用拉链法解决哈希冲突,需转移整个链表上的节点 */
while(de) {
uint64_t h;

Expand Down