当前位置:网站首页>面试官:Redis中集合数据类型的内部实现方式是什么?
面试官:Redis中集合数据类型的内部实现方式是什么?
2022-07-05 19:03:00 【51CTO】
虽然已经是阳春三月,但骑着共享单车骑了这么远,还有有点冷的。我搓了搓的被冻的麻木的手,对着前台的小姐姐说:“您好,我是来面试的。”
小姐姐问:“您好,您叫什么名字?”我回答:“我叫万猫学社。”小姐姐笑出了声,说到:“这名字好怪,谁给你起的啊。”我面无表情地回答:“俺爹。”小姐姐收起了笑容,说到:“跟我来吧。”我被带到了面试间等候,片刻后一个着干净满脸清秀的青年走了进来,一股男士香水的淡香扑面而来。
面试官:Redis中基本的数据类型有哪些?
我:Redis的基本数据类型有:字符串(string)、哈希(hash)、列表(list)、集合(set)、有序集合(zset)。
面试官:集合数据类型的内部实现方式是什么?
我还沉浸在上一个问题的沾沾自喜中,顿时表情凝固了,手心开始冒出冷汗。“这个。。没有太深入了解”,我支支吾吾的说到。
面试官:回去等消息吧。
这句话说的干净利落,然后就没有然后了。失败是成功的妈妈,我不气馁,决定马上恶补一下。
类型和编码
首先,整明白什么是类型?什么是编码?在Redis中使用对象来表示内存中的键和值。每个对象由一个叫做redisObject
结构体表示,其中有三个属性:类型(type)、编码(encoding)、指向具体数据的指针(ptr)。
我们通常说的字符串、哈希、列表、集合、有序集合都是redisObject
中的类型,实际上针对每一个数据结构在Redis内部都有自己底层的多种内部编码实现,这样是为了在合适的场景选择合适的内部编码,以达到内存空间和处理效率的平衡,这可能就是中庸之道吧。
在面试中,经常被问到的内部实现方式、内部构造、内部原理,一般指的就是redisObject
中的编码。
集合的编码
集合的编码有两种,分别是:整数集合(intset)和哈希表(hashtable)。
当集合中的所有元素都是整数,并且元素的个数小于set-max-intset-entries
(默认为512个)时,使用整数集合作为集合的编码,集合的所有元素都保存在整数集合里面。比如:
当集合中的所有元素不都是整数,或者元素的个数大于等于set-max-intset-entries
(默认为512个)时,使用哈希表作为集合的编码,哈希表的每一个键都是字符串对象,每一个字符串包含一个集合的元素,哈希表的值全部为NULL
。
比如,集合中的所有元素不是整数:
当然,了解以上细节还没能完全“征服”面试官,我们需要更深入一些:)
集合的编码转换
当一个集合是以整数集合为编码时,再向这个集合添加非整数的元素,或向这个集合添加整数的元素使元素个数过多时,就会执行集合的编码转换。
把原来保存在整数集合中的所有元素转移到哈希表中,并且把集合的编码用整数集合修改为哈希表。不过,把非整数的元素从集合中移除,或者减少整数元素的个数,以哈希表为编码的集合也不会转化为整数集合。
举个例子,我们先创建一个以整数集合为编码的集合:
然后,再向它添加两个字符串元素,它就是转换为以哈希表为编码:
然后,再把那两个字符串元素从集合中移除,集合的编码依然是哈希表:
总结
在Redis中,集合的内部实现有整数集合(intset)和哈希表(hashtable)两种,当集合中的所有元素都是整数并元素个数较少时,使用整数集合作为内部实现,否则使用哈希表作为内部实现。当条件不满足时,整数集合可以转换为哈希表,但哈希表不能转换为整数集合。
竟然已经看到这里了,你我定是有缘人,留下你的点赞和关注,他日必成大器。
边栏推荐
- [today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
- 基于FPGA的超声波测距
- JS solution force deduction daily question (12) - 556 Next larger element III (2022-7-3)
- Low code practice of xtransfer, a cross-border payment platform: how to integrate with other medium-sized platforms is the core
- Startup and shutdown of CDB instances
- Why can't Bi software do correlation analysis? Take you to analyze
- 5. Data access - entityframework integration
- shell编程基础(第9篇:循环)
- 2022 the latest big company Android interview real problem analysis, Android development will be able to technology
- R language Visual scatter plot graph, add labels to some data points in the graph, and always display all labels, even if they have too much overlap. Ggrep package helps
猜你喜欢
1亿单身男女撑起一个IPO,估值130亿
手把手教你处理 JS 逆向之图片伪装
Oracle Chinese sorting Oracle Chinese field sorting
ELK分布式日志分析系统部署(华为云)
Postman核心功能解析 —— 参数化和测试报告
企业级数据安全,天翼云是这样理解的
Common interview questions in Android, 2022 golden nine silver ten Android factory interview questions hit
[detailed explanation of AUTOSAR 14 startup process]
Ultrasonic ranging based on FPGA
The basic grammatical structure of C language
随机推荐
After the company went bankrupt, the blackstones came
R language Visual scatter plot graph, add labels to some data points in the graph, and always display all labels, even if they have too much overlap. Ggrep package helps
Startup and shutdown of CDB instances
C# 语言的高级应用
golang通过指针for...range实现切片中元素的值的更改
Analysis of postman core functions - parameterization and test report
手机开户选择哪家券商公司比较好哪家平台更安全
cf:B. Almost Ternary Matrix【对称 + 找规律 + 构造 + 我是构造垃圾】
R语言可视化散点图(scatter plot)图、为图中的部分数据点添加标签、始终显示所有标签,即使它们有太多重叠、ggrepel包来帮忙
Go语言学习教程(十五)
Benefits of automated testing
How to quickly advance automated testing? Listen to the personal feelings of the three bat test engineers
EasyCVR电子地图中设备播放器loading样式的居中对齐优化
The era of Web3.0 is coming. See how Tianyi cloud storage resources revitalize the system to enable new infrastructure (Part 2)
Android面试,android音视频开发
JAD installation, configuration and integration idea
android中常见的面试题,2022金九银十Android大厂面试题来袭
2022 the most complete Tencent background automation testing and continuous deployment practice in the whole network [10000 words]
Why can't Bi software do correlation analysis? Take you to analyze
Debezium系列之:postgresql从偏移量加载正确的最后一次提交 LSN