当前位置:网站首页>Redis source code analysis 3 implementation of discontinuous traversal
Redis source code analysis 3 implementation of discontinuous traversal
2022-07-19 05:22:00 【HjasnJH】
Total ergodic problem , Will lead to the whole redis Unavailable , So discontinuous traversal is introduced
dictscan Implement discontinuous traversal
dictscan The implementation of the
unsigned long dictScan(dict *d,
unsigned long v,
dictScanFunction *fn,
dictScanBucketFunction* bucketfn,
void *privdata)
{
dictht *t0, *t1;
const dictEntry *de, *next;
unsigned long m0, m1;
if (dictSize(d) == 0) return 0;
if (!dictIsRehashing(d)) {
t0 = &(d->ht[0]);
m0 = t0->sizemask;
/* Emit entries at cursor */
if (bucketfn) bucketfn(privdata, &t0->table[v & m0]);
de = t0->table[v & m0];
while (de) {
next = de->next;
fn(privdata, de);
de = next;
}
/* Set unmasked bits so incrementing the reversed cursor
* operates on the masked bits */
v |= ~m0;
/* Increment the reverse cursor */
v = rev(v);
v++;
v = rev(v);
} else {
t0 = &d->ht[0];
t1 = &d->ht[1];
/* Make sure t0 is the smaller and t1 is the bigger table */
if (t0->size > t1->size) {
t0 = &d->ht[1];
t1 = &d->ht[0];
}
m0 = t0->sizemask;
m1 = t1->sizemask;
/* Emit entries at cursor */
if (bucketfn) bucketfn(privdata, &t0->table[v & m0]);
de = t0->table[v & m0];
while (de) {
next = de->next;
fn(privdata, 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 {
/* Emit entries at cursor */
if (bucketfn) bucketfn(privdata, &t1->table[v & m1]);
de = t1->table[v & m1];
while (de) {
next = de->next;
fn(privdata, de);
de = next;
}
/* Increment the reverse cursor not covered by the smaller mask.*/
v |= ~m1;
v = rev(v);
v++;
v = rev(v);
/* Continue while bits covered by mask difference is non-zero */
} while (v & (m0 ^ m1));
}
return v;
}
There are three cases of discontinuous traversal :
1) From the beginning to the end of the iteration , Hash table is not rehash operation .
2) From the beginning to the end of the iteration , The hash table has been expanded or reduced , And just between the two iterations rehash operation .
3) From the beginning to the end of the iteration , Hash table is in progress during one or more iterations rehash operation
The principle of discontinuous traversal
One of the most important things to master , After the expansion and reduction of the dictionary , Where will the original data be in the new dictionary ?
For example, before capacity expansion , Suppose the length is from 4 Extended to 8, Originally in 0 Of the elements of location , In the new hash table , Only in 0 perhaps 4 The location of , The algorithm makes use of this feature , To achieve no repeat traversal and no leakage traversal . The principle of volume reduction is the same .

among , I will list one of them , Let's see how the algorithm can avoid this repeated traversal phenomenon
Suppose the current capacity bit 4, In the third iteration , The hash table has been expanded to 8,
/* Increment the reverse cursor not covered by the smaller mask.*/
v |= ~m1;
v = rev(v);
v++;
v = rev(v); The mask of the array is m0=0x11, ~m0=0x100
Then the first two iterations are as follows :

It's time to start again rehash
The mask of the array m0=0x111,~m0=0x1000

At this point, the traversal is over , Let's see what we have traversed in total
old hash surface :0 2
new hash surface :1 5 3 7
At this time, do you think it's missing ?
Actually , We have a new hash From the perspective of table , In fact, traverse the old hash Tabular 0 2, In fact, it traverses the new hash Tabular 0 4 2 6, because 0-> 0 4 , 2-> 2 6, So in fact, it is realized 0 4 2 6 1 5 3 7 Full traversal of .
Others are similar . This is this reverse binary iteration( Inverse binary iterative algorithm ).
边栏推荐
- 用户态协议栈-基于netmap的UDP实现
- 循环赛制日程表问题
- [ES6] explain in detail the common objects and other methods of set and array (full version)
- Easypoi之excel模板导出
- (elaborate) ES6 remaining parameters, ES6 built-in objects, template string content (detailed example Dictionary) and practical cases of flexible use of the project
- Single arm routing configuration
- Cesium bind mouse events and remove mouse events
- Wechat applet obtains the week, morning, noon and evening of month, year and day
- 2020-11-10
- [AI] action recognition using simple neural network -- Based on coco key points
猜你喜欢

Internship project 1 - personalized homepage configuration

Addition and removal of cesium geojson data

【全网首发】一个月后,我们又从 MySQL 双主切换成了主-从

分布式存储-fastdfs

Ucharts chart, pie chart, bar chart and line chart are used in uniapp

How to deal with the mismatch between subtitle files and video files

Excel导入长数据末尾变000

单臂路由配置

Nacos configuration management

Internship project 2 - Homepage configuration - my data module
随机推荐
markdown笔记以及Typora相关快捷键
Uni app conditional compilation ifdef ENDIF compatible with multiple terminals
es6新增-字符串部分
2.6.2 memory leakage
第一个智能合约程序Faucet.sol
vlookup函数的使用方法及实例
新手学习渗透测试的入门指南
ES6 real case deconstruction (multidimensional array object) new case:
【Es6】详细解说Set ,Array的常用对象及其他方法(完整版)
ECS deployment web project
ArcMap 创建常量栅格并镶嵌至新栅格
Beginner's Guide to learning penetration testing
基于PaddleOCR解决文本检测训练模型与inference模型预测效果不一致的问题
Cesium BIND Mouse Events and remove Mouse Events
百度地图 实现 热力图
使用Echars实现水滴状、环形图、分割图、堆叠、组织架构图、地图轮廓等图表
轮播图的两种方法及自动轮播
Two or three things to know about operation and maintenance safety
mysql的锁
Internship project 1 - personalized homepage configuration