当前位置:网站首页>Interviewer: what is the internal implementation of set data types in redis?
Interviewer: what is the internal implementation of set data types in redis?
2022-07-05 19:45:00 【51CTO】
Although it is already March in spring , But riding a shared bike so far , And a little cold . I rubbed my frozen numb hands , Say to the lady at the front desk :“ Hello! , I'm here for an interview .”
Miss asked :“ Hello! , What's your name ?” I reply :“ My name is wanmao society .” Miss, sister laughed , Speaking of :“ That's a strange name , Who gave it to you .” I replied without expression :“ My father .” The little sister put away her smile , Speaking of :“ Come with me .” I was taken to the interview room to wait , A moment later, a young man with a clean and beautiful face came in , A faint fragrance of men's perfume comes to us. .
interviewer :Redis What are the basic data types in ?
I :Redis The basic data types of are : character string (string)、 Hash (hash)、 list (list)、 aggregate (set)、 Ordered set (zset).
interviewer : What is the internal implementation of the collection data type ?
I was also immersed in the complacency of the last question , Suddenly, his expression solidified , The palms began to sweat .“ This .. Not much in-depth understanding ”, I hesitated .
interviewer : Go back and wait for the news .
This sentence is clear , And then there's no then . Failure is the mother of success , I'm not discouraged , Decided to mend it right away .
Type and code
First , Understand what is type ? What is? code ? stay Redis Objects are used in to represent keys and values in memory . Each object consists of a redisObject
The structure represents , There are three properties : type (type)、 code (encoding)、 Pointer to specific data (ptr).
We usually say string 、 Hash 、 list 、 aggregate 、 Ordered sets are redisObject
Medium type , In fact, for each data structure Redis There are various internal coding implementations at the bottom of the system , This is to select the appropriate internal code in the appropriate scene , To achieve a balance between memory space and processing efficiency , This may be the golden mean .
In the interview , Often asked about the internal implementation 、 Internal construction 、 internals , Generally speaking, it means redisObject
Medium code .
The encoding of the set
There are two kinds of codes for sets , Namely : Set of integers (intset) Hash table (hashtable).
When all elements in a set are integers , And the number of elements is less than set-max-intset-entries
( The default is 512 individual ) when , Use an integer set as the encoding of the set , All integers are stored in the set of elements . such as :
When all elements in the set are not integers , Or the number of elements is greater than or equal to set-max-intset-entries
( The default is 512 individual ) when , Use the hash table as the encoding of the set , Each key in the hash table is a string object , Each string contains a collection of elements , The values of the hash table are all NULL
.
such as , All elements in the collection are not integers :
Of course , I haven't fully understood the above details yet “ conquer ” interviewer , We need to go deeper :)
Encoding conversion of sets
When a set is encoded as an integer set , Then add non integer elements to this set , Or add integer elements to this set, so that the number of elements is too large , Will perform the encoding conversion of the set .
Transfer all elements originally saved in the integer set to the hash table , And the encoding of the set is changed from an integer set to a hash table . however , Remove non integer elements from the set , Or reduce the number of integer elements , A set encoded by a hash table will not be converted to an integer set .
for instance , Let's first create a set encoded by an integer set :
then , Add two more string elements to it , It is converted to hash table encoding :
then , Then remove the two string elements from the collection , The encoding of the set is still a hash table :
summary
stay Redis in , The internal implementation of the set has an integer set (intset) Hash table (hashtable) Two kinds of , When all elements in the set are integers and the number of elements is small , Use the integer set as the internal implementation , Otherwise, use the hash table as the internal implementation . When conditions are not met , An integer set can be converted to a hash table , But the hash table cannot be converted to an integer set .
I've seen it here , You and I must be predestined friends , Leave your give the thumbs-up and Focus on , It will become a great thing in the future .
边栏推荐
- 四万字长文说operator new & operator delete
- 什么是面上项目
- Is it safe for Guohai Securities to open an account online?
- -v parameter of GST launch
- 国海证券在网上开户安全吗?
- ACM getting started Day1
- Summer Challenge database Xueba notes, quick review of exams / interviews~
- 通配符选择器
- The problem of returning the longtext field in MySQL and its solution
- Django uses mysqlclient service to connect and write to the database
猜你喜欢
Do you know several assertion methods commonly used by JMeter?
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
通过POI追加数据到excel中小案例
【obs】QString的UTF-8中文转换到blog打印 UTF-8 char*
再忙不能忘安全
Summer Challenge database Xueba notes, quick review of exams / interviews~
Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
Debezium系列之:记录mariadb数据库删除多张临时表debezium解析到的消息以及解决方法
40000 word Wenshuo operator new & operator delete
Android interview, Android audio and video development
随机推荐
使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)
Mysql如何对json数据进行查询及修改
【obs】libobs-winrt :CreateDispatcherQueueController
通过POI追加数据到excel中小案例
Concept and syntax of function
redis集群模拟消息队列
【无标题】
Reptile exercises (II)
全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
That's awesome. It's enough to read this article
Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
Django uses mysqlclient service to connect and write to the database
Apprentissage du projet MMO I: préchauffage
Is it safe to open a mobile stock account? Is it reliable?
Vagrant2.2.6 supports virtualbox6.1
如何安全快速地从 Centos迁移到openEuler
国海证券在网上开户安全吗?
C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
HiEngine:可媲美本地的云原生内存数据库引擎