Redis bitmap: from introduction to mastery

Time:2020-8-1

1. Introduction to bitmap


Bitmap is not a real data type. It is defined in a string type. A string type value can store up to 512M bytes of content

Bit upper limit: 2 ^ (9 (512) + 10 (1024) + 10 (1024) + 3 (8B = 1b)) = 2 ^ 32B

2. Setbit sets the value of a bit


Syntax: setbit key offset value (offset bit offset, starting from 0)

geekmooc:6379> flushall
OK
geekmooc:6379> setbit k1 1 1
0
geekmooc:6379> get k1
@
geekmooc:6379> setbit k1 7 1
0
geekmooc:6379> get k1
A
geekmooc:6379> setbit k1 7 2
ERR bit is not an integer or out of range

Redis bitmap: from introduction to mastery

geekmooc:6379> setbit k1 9 1
0

geekmooc:6379> get k1
[email protected]

Redis bitmap: from introduction to mastery

Redis bitmap: from introduction to mastery

3. Getbit gets the value of a bit


Syntax: getbit key offset

geekmooc:6379> getbit k1 7
1
geekmooc:6379> getbit k1 8
0
geekmooc:6379> getbit k1 1
1

Redis bitmap: from introduction to mastery

4. Bitpos returns the first subscript of the specified value 0 or 1 in the specified interval


Syntax: bitpos key bit [start] [End] (byte index, 0 means the first byte)

summary: Find first bit set or clear in a string since: 2.8.7 group: string

If you do not specify the search range, it means to search from all contents: bitpos key bit

geekmooc:6379> keys *
k1
geekmooc:6379> bitpos k1 1
1
geekmooc:6379> setbit k1 1 0
1
geekmooc:6379> bitpos k1 1
7
geekmooc:6379> setbit k1 7 0
1
geekmooc:6379> bitpos k1 1
9

Redis bitmap: from introduction to mastery

Specify the search range:

Bitpos key bit start: search from start + 1 byte to tail bitpos key bit start end: search from start + 1 byte to end + 1 byte

Then restore the data:

geekmooc:6379> setbit k1 1 1
0
geekmooc:6379> setbit k1 7 1
0

Find Demo:

geekmooc:6379> bitpos k1 1 0 0
1 ා finds the first occurrence of the subscript of 1 in the first byte
geekmooc:6379> bitpos k1 1 0
1 ා finds the first occurrence of the subscript of 1 from the first byte to the last byte of the value
geekmooc:6379> setbit k1 1 0
1 ා changes the value of the specified subscript to 0
geekmooc:6379> bitpos k1 1 0 0
7 #
geekmooc:6379> bitpos k1 1 0
7
geekmooc:6379> setbit k1 7 0
1
geekmooc:6379> bitpos k1 1 0 0
-If 1 is not found in the first byte, – 1 is returned
geekmooc:6379> bitpos k1 1 0
9 ා look up from the first byte to the last byte of the value
geekmooc:6379> bitpos k1 1 0 1
9 ා always find the first occurrence of 1 in the first and second bytes
geekmooc:6379> bitpos k1 1 0 2
9 ා find the first occurrence position of 1 in the first to third bytes, but the total data is 2 (less than the 3 corresponding to end), and no error will be thrown.

5. Bitop bit operation


Syntax: bitop operation destkey key [key…]

summary: Perform bitwise operations between strings since: 2.6.0 group: string

Perform bit operation on one or more string keys that hold binary bits, and save the results to destkey. Operation can be any one of the four operations: and, or, not, XOR

Bitop and destkey key [key…], computes the logical sum of one or more keys, and saves the result to destkey

Bitop or destkey key [key…], calculates the logical or of one or more keys, and saves the result to destkey

Bitop XOR destkey key [key…], computes logical XOR for one or more keys, and saves the result to destkey

Bitop not destkey key key, find the logical non of the given key, and save the result to destkey

Except for the not operation, other operations can accept one or more keys as input. When bitop processes strings of different lengths, the missing part of the shorter string is considered as 0, and the empty key is also regarded as a string sequence containing 0

Bitop and destkey key [key…] Demo:

geekmooc:6379> flushall
OK
geekmooc:6379> keys *
(empty list or set)
geekmooc:6379> setbit k1 1 1
(integer) 0
geekmooc:6379> setbit k2 7 1
(integer) 0
geekmooc:6379> bitop and k3 k1 k2
(integer) 1
geekmooc:6379> get k3
“\x00”

Redis bitmap: from introduction to mastery

All 1 is 1, incomplete 1 is 0

Bitop or destkey key [key…] demo

geekmooc:6379> bitop or k4 k1 k2
(integer) 1
geekmooc:6379> get k4
“A”

Redis bitmap: from introduction to mastery

All 0 is 0, incomplete 0 is 1

BITOP XOR destkey key [key …]

geekmooc:6379> bitop xor k5 k1 k2
(integer) 1
geekmooc:6379> get k5
“A”

Redis bitmap: from introduction to mastery

The difference is 1, the same is 0

geekmooc:6379> bitop not k6 k1
(integer) 1
geekmooc:6379> get k6
“\xbf”

Redis bitmap: from introduction to mastery

Bitwise inversion

6.bitcount


Statistics refers to the number of 1 in the location interval

  1. Bitcount key [start] [End] the positive index direction of the byte

Start from 0 from left to right. Note that the official start and end are bits, and bytes are after testing

geekmooc:6379> get k1
“@”
geekmooc:6379> bitcount k1
(integer) 1
geekmooc:6379> setbit k1 7 1
(integer) 0
geekmooc:6379> bitcount k1
(integer) 2
geekmooc:6379> setbit k1 9 1
(integer) 0
geekmooc:6379> bitcount k1
(integer) 3 ා count the total number of all 1
geekmooc:6379> bitcount k1 0 0
(integer) 2 ා counts the total number of occurrences of 1 in the first byte
geekmooc:6379> bitcount k1 0 1
(integer) 3 ා counts the total number of occurrences of 1 in bytes 0 + 1 to 1 + 1

  1. Bitcount key [start] [End] start end byte index negative direction

Start from – 1 from right to left. Note that the official start and end are bits, and bytes are after testing

geekmooc:6379 >Bitcount K1 0 – 1 ා is equivalent to bitcount K1
(integer) 3

The most commonly used one is bitcount K1

7. Bits of redis


geekmooc:6379> set k7 ab
OK
geekmooc:6379> get k7
“ab”
geekmooc:6379> bitcount k7
(integer) 6
geekmooc:6379> bitcount k7 0 0
(integer) 3
geekmooc:6379> bitcount k7 1 1
(integer) 3

Redis bitmap: from introduction to mastery

geekmooc:6379 >In set K8
OK
geekmooc:6379> bitcount k8
(integer) 13
geekmooc:6379> get k8
“\xe4\xb8\xad”

Redis bitmap: from introduction to mastery

Redis bitmap: from introduction to mastery

If it is under GBK environment, it is shown in the following figure

8. Bitmap application scenarios


Statistics of the number of days users sign in

The user ID is key, the day is taken as the offset, and the online setting is 1 366 > 000000000000000

366 / 8 = 46byte ID 18 users sign in on day 1 and day 30 of this year

geekmooc:6379[2]> setbit u18 1 1
(integer) 0
geekmooc:6379[2]> setbit u18 30 1
(integer) 0
geekmooc:6379 [2] > bitcount U18 ා statistics the total number of sign in times of users with ID 18
(integer) 2
geekmooc:6379[2]> keys u*
1) “u18”

Statistics of active users by day

Day is the key, the user ID is offset, and the online setting is 1

The number of active users in a period of time is 50 million / 8 * 366 = 6.3mb = * 366 (50 million active users only generate 2GB of data in one year)

geekmooc:6379>SETBIT 20190601 5 1 #0000 0100
geekmooc:6379>SETBIT 20190602 7 1 #0000 0001
geekmooc:6379>SETBIT 20190603 7 1 #0000 0001
Find the number of active users from June 1 to June 10
geekmooc:6379>BITOP OR users 20190601 20190602 20190603 … 20190610
geekmooc:6379 >Bitcount users ා the target key is users
The result is 2

Statistics of online status and number of users


geekmooc:6379 >Setbit online 5 1 ා 0000 0100 online is 1
(integer) 0
geekmooc:6379> SETBIT online 7 1 #0000 0101
(integer) 0
geekmooc:6379 >Bitcount Online
(integer) 2
geekmooc:6379> SETBIT online 7 0
(integer) 1
geekmooc:6379 >Bitcount Online
(integer) 1

Redis bitmap: from introduction to mastery www.geekmooc.cnRedis bitmap: from introduction to mastery