Redis Learning Notes (5)---integer set Intset__redis

Source: Internet
Author: User
Tags redis
Intset

When a collection contains only integers, and the number of elements in the collection is not long, Redis uses an integer set Intset as the underlying implementation of the collection. implementation of Integer set

typedef struct INTSET {
    uint32_t encoding;
    uint32_t length;
    int8_t contents[];
} Intset;
Encoding: The current integer set encoding length: The number of elements in the collection contents: the specific storage location of the elements in the collection

There are three ways to encode Redis integer collections:
1) intset_enc_int16, then the contents array is an array of int16_t types, and each item in the array is an integer of int16_t type, the range is -32768~32767
2) Intset_enc_int32, then contents is an array of int32_t types
3) Intset_enc_int64, then contents is a int64_t type of array diagram

A simple schematic is as follows:
  
Each element in the array is of type int16_t, 2 bytes, one with 5 elements, so the space occupied by the contents array is 16*5/8=10 byte escalation

Suppose the original set of integers is:
  
At this point the contents array occupies a space of 16*3=48 bit
For elements of the int16_t type, whose range is -32768~32767, the int16_t type is not sufficient to hold the element when a new element with a value of 65535 is added, so the integer collection is upgraded at this point.
This element is int32_t enough to store, so you want to upgrade the integer set encoding to Intset_enc_int32.
This requires a 32*4=128 bit of space, and each element occupies 4 bytes.
So the steps for upgrading are:
1 expands the space size of the underlying array according to the type of the new element and allocates space
2 converts the underlying elements into the same type as the new element and places the converted elements in the newly allocated space. In the process of placing elements, the ordered nature of the underlying array needs to be maintained
3 Add new elements to the underlying array
Because the newly added element causes an upgrade, the new element must be larger than all previous elements, so storing the new element in the last item of the contents array
The upgraded integer collection is:
  
The elements in the contents array are stored in the order:
  
  
The set of integers does not support degraded operations



The source code referenced in this article is all from the Redis3.0.7 version

Redis Learning Reference:
https://github.com/huangz1990/redis-3.0-annotated
Redis Design and Implementation (second edition)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.