Quickly understand what an integer set in Redis is

Introduction to Integer Sets

The integer set (intset) is one of the internal encodings of the Redis set data type. When the elements in the set data type are all integers and the number of elements is small, Redis uses the integer set as the internal encoding.

The integer set (intset) can store integers int16_tof type int32_tand int64_ttype, and the elements in the integer set are guaranteed not to be repeated.

The structure of a set of integers

The structure of an integer set (intset) contains three properties: encoding, number of elements (length), and array of elements (contents).

  • The encoding method (encoding) indicates the encoding method of the current integer set, which can be set to: INTSET_ENC_INT16, INTSET_ENC_INT32, INTSET_ENC_INT64.
  • The number of elements (length) represents the number of elements contained in the current integer collection, that is, the length of the array holding the elements.
  • The element array (contents) holds all the elements of the current integer collection, each element is an array item of the array, the elements are arranged in the array from small to large, and there are no repeated elements.

When the encoding method is INTSET_ENC_INT16, the element array is an array of int16_ttypes, and each item in the array is int16_tan integer of type (the minimum value is $-2^{15}$ = -32,768, and the maximum value is $2^{15} - 1$ = 32,767).

When the encoding method is INTSET_ENC_INT32, the element array is an array of int32_ttypes, and each item in the array is int32_tan integer of type (the minimum value is $-2^{31}$ = -2,147,483,648, and the maximum value is $2^{31} - 1$ = 2,147,483,647).

When the encoding method is INTSET_ENC_INT64, the element array is an array of int64_ttypes, and each item in the array is int64_tan integer of type (the minimum value is $-2^{63}$ = -9,223,372,036,854,775,808, and the maximum value is $2^{63} - 1$ = 9,223,372,036,854,775,807).

Integer collection upgrade

When adding elements to an integer set, if the type of the new element is longer than the encoding of the integer set, the integer set is updated first, and then the new element is added.

Upgrade operations include:

  • From INTSET_ENC_INT16upgrade to INTSET_ENC_INT32.
  • From INTSET_ENC_INT16upgrade to INTSET_ENC_INT64.
  • From INTSET_ENC_INT32upgrade to INTSET_ENC_INT64.

The upgrade process is divided into 3 steps:

  1. According to the data type of the new element, increase the memory space of the element array, and allocate space for the new element at the same time.
  2. Convert the existing elements to the data type of the new element and place them in the correct bits in ascending order.
  3. Put the new element at the last position of the element array.

In addition, integer collections do not support downgrade operations. Even after the element is removed, the integer collection conforms to the shorter encoding, and there is no downgrade operation.

Summarize

An integer set (intset) is one of the internal encodings of the Redis set data type, which can hold integers of type int16_tand int32_ttype .int64_t

The structure of an integer set (intset) contains three properties: encoding, number of elements (length), and array of elements (contents).

When the type of the newly added element is longer than the encoding method of the integer set, the integer set will have an upgrade operation. Integer collections do not support downgrade operations.

Finally, thank you for being so handsome, and for giving me likes and attention .

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324030199&siteId=291194637