The data structure illustrated Redis articles - Packing List

Foreword

    Like compression list with a set of integers is not the underlying data structure, a data storage structure but Redis own design. It is a bit similar to the array by a contiguous memory space to store data. However, it is different with arrays is that it allows the storage of different data size.

A compression list

    Hear "compressed" word, intuitive reaction is to save memory. The reason why this structure saves storage memory, is compared to the storage array in terms of ideas. We know that require the same array size of each element, if we want to store strings of different lengths, then we need to use the maximum string size as the size of the element length (assumed to be 20 bytes). When storing 20 bytes less than the string length, part of the storage space will be wasted.

    The advantages of a continuous array of occupied space can be a good use of CPU cache access data. If we want to keep this advantage, we want to save storage space can be compressed array.

     But there is such a problem, we do not know because the size is how much of each element, and therefore will not be able to calculate the exact location of the next node when traversing it. This time we can add properties to a lenght of each node.

    in this way. We know that node after traversing the length of each node (memory size), it is easy to calculate the location of the next node memory again. This structure is like a simple list of the compression.

Two, Redis Packing List

    Packing List (zip1ist) is one of the backing list and hash.

    When the list contains only a small amount of a string list items, and each is a small list item or integer values, or is relatively short length, then it will use the compression list Redis do list underlying implementation.

    When a string containing only a small amount of a hash key-value pairs, and each of the keys and key values ​​than either of the values ​​is a small integer, or is relatively short length, then it will use the underlying compression Redis hash list do achieve.

2.1 Redis compression constitute the list

    Compression Redis list is developed in order to save memory, a sequential type (Sequential) Jin data structure from a continuous block of memory specially encoded series thereof. A list of compression may comprise any number of nodes (entry), each node may store a byte array or an integer value, as shown below.

Example:

    如上图,展示了一个总长为80字节,包含3个节点的压缩列表。如果我们有一个指向压缩列表起始地址的指针p,那么表为节点的地址就是P+60。

2.2 Redis压缩列表节点的构成

    每个压缩列表节点可以保存一个字节数组或者一个整数值。其中,字节数组可以是以下三种长度中的一种。

  • 长度小于等于63(2^6-1)字节的字节数组;
  • 长度小于等于16383(2^14-1)字节的字节数组
  • 长度小于等于4294967295(2^32-1)字节的字节数组

整数值可以是以下6种长度中的一种

  • 4位长,介于0至12之间的无符号整数
  • 1字节长的有符号整数
  • 3字节长的有符号整数
  • int16_t类型整数
  • int32_t类型整数
  • int64_t类型整数

    节点的 previous_entry_length属性以字节为单位,记录了压缩列表中前一个节点的长度。 previous_entry_length属性的长度可以是1字节或者5字节。

  • 如果前一节点的长度小于254字节,那么 previous_entry_length属性的长度为1字节,前一节点的长度就保存在这一个字节里面。
  • 如果前一节点的长度大于等于254字节,那么 previous_entry_length属性的长度为5字节:其中属性的第一字节会被设置为0xFE(十进制值254),而之后的四个字节则用于保存前一节点的长度.

    节点的encoding属性记录了节点的content属性所保存数据的类型以及长度。

  • 一字节、两字节或者五字节长,值的最高位为00、01或者10的是字节数组编码这种编码表示节点的 content属性保存着字节数组,数组的长度由编码除去最高两位之后的其他位记录。
  • 一字节长,值的最高位以11开头的是整数编码:这种编码表示节点的content属性保存着整数值,整数值的类型和长度由编码除去最高两位之后的其他位记录。

    节点的content属性负责保存节点的值,节点值可以是一个字节数组或者整数,值的类型和长度由节点的encoding属性决定。

  • 编码的最高两位00表示节点保存的是一个字节数组。
  • 编码的后六位001011记录了字节数组的长度11。
  • content属性保存着节点的值"hello world"。
  • 编码11000000表示节点保存的是一个int16_t类型的整数值;
  • content属性保存着节点的值10086

2.3 常用操作的时间复杂度

操作 时间复杂度
创建一个新的压缩列表 O(1)
创建一个包含给定值的新节点,并将这个新节点添加到压缩列表的表头或者表尾 平均O(N),最坏O(N^2)(可能发生连锁更新)
将包含给定值的新节点插人到给定节点之后 平均O(N),最坏O(N^2)(可能发生连锁更新)
返回压缩列表给定索引上的节点 O(N)
在压缩列表中査找并返回包含了给定值的节点 因为节点的值可能是一个字节数组,所以检查节点值和给定值是否相同的复杂度为O(N),而查找整个列表的复杂度则为(N^2)
返回给定节点的下一个节点 O(1)
返回给定节点的前一个节点 O(1)
获取给定节点所保存的值 O(1)
从压缩列表中删除给定的节点 平均O(N),最坏O(N^2)(可能发生连锁更新)
删除压缩列表在给定索引上的连续多个 平均O(N),最坏O(N^2)(可能发生连锁更新)
返回压缩列表目前占用的内存字节数 O(1)
返回压缩列表目前包含的节点数量 点数量小于65535时为O(1),大于65535时为O(N)

本文重点

  • 压缩列表是Redis为节约内存自己设计的一种顺序型数据结构。

  • 压缩列表被用作列表键和哈希键的底层实现之一。
  • 压缩列表可以包含多个节点,每个节点可以保存一个字节数组或者整数值。
  • 添加新节点到压缩列表,或者从压缩列表中删除节点,可能会引发连锁更新操作,但这种操作出现的几率并不高。

参考

《Redis设计与实现》

《Redis开发与运维》

《Redis官方文档》

-----END-----

Guess you like

Origin www.cnblogs.com/hunternet/p/11306690.html