5. [series] Redis Redis of advanced applications - bitmap

Original: 5. [series] Redis Redis of advanced applications - bitmap

Assume a scenario: we need to record the user check-ins a year, a sign is 1, did not sign is 0, record 365 days, when the user million storage space is amazing.
To solve this problem, redis provides a bitmap data structure. Such daily attendance record occupies only one bit, 365 days is 365 bits, 46 bytes can accommodate.

The bitmap is not a special data structure, its content is an ordinary string, i.e. a byte array, we can set / get methods to set and get the contents of the bitmap, bitmap operations can be used to byte array, and setbit getbit as the number of bits set to deal with.

Basic use

Redis bit array is automatically extended, if set an offset position beyond the existing content range, it will automatically set the number of bits for zero expansion.

Next, we use the bit string to the hello operation (instruction set is not used directly), we first need to get hello ASCII codes, can be easily obtained binary value of the ASCII code for each character in Python command line.

>>> bin(ord('h'))
'0b1101000'   # 高位 -> 低位
>>> bin(ord('e'))
'0b1100101'
>>> bin(ord('l'))
'0b1101100'
>>> bin(ord('l'))
'0b1101100'
>>> bin(ord('o'))
'0b1101111'
image.png

Next we use redis-cli first character set, i.e. the first eight bits, we only need to set the number of bits of a bit value of the group, as shown above, h 1/2/4 bits need only character set, e 9/10/13/15 bit character only needs to be set. It is noteworthy that the order bit and bit array of characters is reversed.

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"

The above example can be understood as "installment savings" Similarly, we can also "zero-sum withdrawal", "the whole sum withdrawal." "Zero deposit" is the use of by-bit values ​​setbit set, "Lump" is filled with all groups using the median disposable string, overwriting the old value.

Zero-sum withdrawal
127.0.0.1:6379> setbit w 1 1
(integer) 0
127.0.0.1:6379> setbit w 2 1
(integer) 0
127.0.0.1:6379> setbit w 4 1
(integer) 0
127.0.0.1:6379> getbit w 1  # 获取某个具体位置的值 0/1
(integer) 1
127.0.0.1:6379> getbit w 2
(integer) 1
127.0.0.1:6379> getbit w 4
(integer) 1
127.0.0.1:6379> getbit w 5
(integer) 0
Zero-sum withdrawal
127.0.0.1:6379> set w h  # 整存
(integer) 0
127.0.0.1:6379> getbit w 1
(integer) 1
127.0.0.1:6379> getbit w 2
(integer) 1
127.0.0.1:6379> getbit w 4
(integer) 1
127.0.0.1:6379> getbit w 5
(integer) 0

If the corresponding bit byte is non-printable characters, redis-cli displays the hexadecimal representation of the character.

127.0.0.1:6379> setbit x 0 1
(integer) 0
127.0.0.1:6379> setbit x 1 1
(integer) 0
127.0.0.1:6379> get x
"\xc0"

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12515047.html