Redis --- Bitmap

Table of contents

background

structure

Access method

Statistics and searches


background

During the development process, there will be Boolean type storage, such as recording a user's check-in status 365 days a year. If there is a Boolean variable every day, multiple users, or kv format is used, the storage amount will be if there are hundreds of millions of users. Astonishing. So Redis bitmaps came into being.

A value occupies one bit, 365 days means 365 bits, 46 characters, which is a longer string, which greatly saves memory space.

structure

It is not a special data structure similar to a queue. The content is actually an ordinary string. In terms of binary digits, it is also a byte array. You can use get and set indexes to directly obtain or set the entire bitmap. Content.

Redis's array is automatically expanded. If the offset position exceeds the nausea, it will be automatically expanded.

Directly set the string to "hello",

ASCII code of hello:
>>> bin(ord(' h '))
'0b1101000'
# High bit -> Low bit
>>> bin(ord(' e '))
'0b1100101'
>>> bin(ord(' l '))
'0b1101100'
>>> bin(ord(' l '))
'0b1101100'
>>> bin(ord(' o '))
'0b1101111'

 Note that they are all eight digits.

To set the bits of a string, you only need to set the bit with a value of 1.

127.0.0.1:6379> setbit s 1 1
(integer) 0
127.0.0.1:6379> setbit s 2 1
(integer) 0
127.0.0.1:6379> setbit s 4 1
(integer) 0
127.0.0.1:6379> setbit s 9 1
(integer) 0
127.0.0.1:6379> setbit s 10 1
(integer) 0
127.0.0.1:6379> setbit s 13 1
(integer) 0
127.0.0.1:6379> setbit s 15 1
(integer) 0
127.0.0.1:6379> get s
"he"

 h has only 1, 2, 4 bits, and e is 9, 10, 13, and 15 bits (two characters, 16 bits). The direct result is a string of "he"

Access method

The above example can be understood as zero deposit and integer pickup, zero deposit and zero pickup, and zero deposit and zero pickup. Zero deposit is to set the position one by one as above. The entire storage is to fill all the bit arrays with the string at once, overwriting the old values.

After looking at the structure and storage of bitmaps, we save them in order to obtain statistical quantities and complete what we need.

Statistics and searches

Redis provides bitmap instructions: bitcount and bitpos,

Bitcount is used to count the number of 1's within a specified position range.

bitpos is used to find the first 0 or 1 that appears in the specified range 

The following parameters [start, end], but these two parameters are in 8-byte units, that is to say, 0 and 1 refer to the first starting and ending with the second byte, and the range is actually the first sixteen bits. .

127.0.0.1:6379> set w hello
OK
127.0.0.1:6379> bitcount w
(integer) 21
127.0.0.1:6379> bitcount w 0 0 # Number of bits of 1 in the first character
(integer) 3
127.0.0.1:6379> bitcount w 0 1 # Number of 1’s in the first two characters
(integer) 7
   
127.0.0.1:6379> bitpos w 0 # The first 0 bit
(integer) 0
127.0.0.1:6379> bitpos w 1 #The first 1 bit
(integer) 1
127.0.0.1:6379> bitpos w 1 1 1 # Starting from the second character, the first 1 bit
(integer) 9
127.0.0.1:6379> bitpos w 1 2 2 # Starting from the third character, the first 1 bit
(integer) 17

Guess you like

Origin blog.csdn.net/Yoke______/article/details/132665094