当前位置:网站首页>redis数据结构之压缩列表
redis数据结构之压缩列表
2022-06-24 18:57:00 【华为云】
redis数据结构之压缩列表
压缩列表是列表list和hash数据结构的底层实现之一。
压缩列表是redis为了节约内存而开发的,由一系列特殊编码的连续内存块组成的顺序型数据结构。一个压缩列表可以包含任意多个节点,每个节点保存一个字节数组或者一个整数值。
创建一个空的ziplist
/* * 新创建一个空 ziplist * * 复杂度:O(1) * * 返回值:新创建的 ziplist */unsigned char *ziplistNew(void) { // 分配 2 个 32 bit,一个 16 bit,以及一个 8 bit // 分别用于 <zlbytes><zltail><zllen> 和 <zlend> unsigned int bytes = ZIPLIST_HEADER_SIZE+1; unsigned char *zl = zmalloc(bytes); // 设置长度 ZIPLIST_BYTES(zl) = intrev32ifbe(bytes); // 设置表尾偏移量 ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(ZIPLIST_HEADER_SIZE); // 设置列表项数量 ZIPLIST_LENGTH(zl) = 0; // 设置表尾标识 zl[bytes-1] = ZIP_END; return zl;}压缩列表包含任意多个压缩列表节点,每个节点可以保存一个字节数组或者一个整数值。
压缩列表组成部分:
zlbytes:记录整个压缩列表占用的内存字节数
zltail:记录压缩列表表尾节点距离压缩列表的起始地址的字节数
entryX:列表节点
zlend:用于标记压缩列表的末端
压缩列表节点的构成:
preivous_entry_length:记录压缩列表中前一个节点的长度
encoding:记录节点content属性保存数据的类型以及长度
content:负责保存节点的值,值的类型和长度由节点的encoding属性决定。
当我们知道一个指向某个节点起始地址的指针,那么通过这个指针以及这个节点的preivous_entry_length属性,我们可以一直向前一个节点回溯,最终到达压缩列表的表头节点。
连锁更新
每个节点的preivous_entry_length属性记录前一个节点的长度
如果前一个节点长度小于254字节,preivous_entry_length属性需要用1字节长的空间来保存这个长度值
如果前一个节点长度大于等于254字节,preivous_entry_length属性需要用5字节长的空间来保存这个长度值
如果前一个节点长度变大,这个节点的preivous_entry_length就要扩展,这个节点的扩展引起下一个节点的扩展,这就是连锁更新
redis将这种在特殊情况下产生的连续多次空间扩展操作称之为连锁更新
在添加新节点和删除节点都可能引发连锁更新。
连锁更新最坏情况下需要对压缩列表进行N次空间重分配操作,每次空间重分配的最坏复杂度为O(N),所以连锁更新的最坏复杂度为O(N的平方),平均复杂度为O(N)
总结
压缩列表是为了节约内存而开发的顺序型数据结构,它是列表键和哈希键的底层实现之一,压缩列表可以包含多个节点,每个节点可以保存一个字节数组或整数值,添加新节点或删除节点可能引发连锁更新操作,这种操作出现几率不高。
边栏推荐
- Huawei machine learning service speech recognition function enables applications to paint "sound" and color
- R for Data Science (note) -- data transformation (select basic use)
- Unityshader world coordinates do not change with the model
- Using dynamic time warping (DTW) to solve the similarity measurement of time series and the similarity identification analysis of pollution concentration in upstream and downstream rivers
- Does finkcdc support sqlserver2008?
- 对国产数据库厂商提几个关于SQL引擎的小需求
- 工作6年,月薪3W,1名PM的奋斗史
- What about the Golden Angel of thunder one? Golden Angel mission details
- 应用实践 | 海量数据,秒级分析!Flink+Doris 构建实时数仓方案
- What are the functions of IBPs open source form designer?
猜你喜欢

【Go语言刷题篇】Go从0到入门4:切片的高级用法、初级复习与Map入门学习

The efficiency of okcc call center data operation

1、 Downloading and installing appium

60 divine vs Code plug-ins!!

php OSS文件讀取和寫入文件,workerman生成臨時文件並輸出瀏覽器下載

技术实现 | Apache Doris 冷热数据存储(一)

全链路业务追踪落地实践方案

Northwestern Polytechnic University attacked by hackers? Two factor authentication changes the situation!

Some small requirements for SQL Engine for domestic database manufacturers

Kubernetes cluster deployment
随机推荐
Fabric ledger data block structure analysis (I): how to analyze the smart contract transaction data in the ledger
Two solutions to the problem of 0xv0000225 unable to start the computer
Real time rendering: the difference between real-time, offline, cloud rendering and hybrid rendering
How to customize cursor position in wechat applet rotation chart
Introduction: continuously update the self-study version of the learning manual for junior test development engineers
Experience of MDM master data project implementation for manufacturing projects
LCD12864 (ST7565P) Chinese character display (STM32F103)
Vs2017 setting function Chinese Notes
Huawei machine learning service speech recognition function enables applications to paint "sound" and color
Some ideas about chaos Engineering
What are the functions of IBPs open source form designer?
Error in Android connection database query statement
Intel and Microsoft give full play to the potential energy of edge cloud collaboration to promote the large-scale deployment of AI
Teach you how to cancel computer hibernation
Landcover100, planned land cover website
Application DDoS attack principle and defense method
If the programmer tells the truth during the interview
Download steps of STM32 firmware library
Volcano becomes spark default batch scheduler
A detailed explanation of the implementation principle of go Distributed Link Tracking