1, the introduction of
Bitmaps are called Bitmaps and are not a data type. Many video tutorials online refer to Bitmaps as data types, which is probably incorrect. Bitmaps are “data types” that Redis provides to users to manipulate bits. It mainly has the following basic characteristics:
- Bitmaps are not data types. They are basically key-values and byte arrays. We can get and set the contents of the bitmap directly using the ordinary GET /set bitmap, or we can treat byte arrays as “bit arrays” through the bitmap operations provided by Redis, such as getbit/setbit
- Bitmaps’ “bitarray” can only store zeros and ones per cell, and the subscripts of the arrays are called offsets in Bitmaps
- Setting Bitmaps without a key automatically generates a new string, and if the offset is set beyond the range of existing content, the bitarray is automatically expanded to zero
2. Basic operations
2.1 SETBIT key offset value
For strings stored in key, sets or clears bits at the specified offset, depending on the value argument, 0/1. When the key does not exist, a new string is automatically generated. The string is stretched to ensure that the value is stored at the specified offset. When the string is stretched, the whitespace is filled with zeros. Time complexity:
O(1)
Offset range:
0 ~ 2 ^ 32
The return value:
Specifies the bits that the offset originally stored
Example: Using Bitmaps to store whether the user clocked in, clocked in as 1, not clocked in as 0, user ID as offset suppose there are 10 users, users 1, 3, 5, 9, 10 clocked in, others did not clocked in, Bitmaps initialization result is as follows: \
Clock :20210806 represents the clock record \ of 2021/08/06
Matters needing attention: In a formal system, the id will not be 0, 1, or 2, but will start with an array, such as 1000000000000001, 1000000000000002, which is very easy to waste offsets, so we can consider calculating and subtracting an appropriate value before setting offsets. If the Bitmaps offset is too large, it will take too long to allocate memory and the Redis server will block.
2.2 GETBIT key offset
image.png
Gets the bit at the specified offset. If offset is greater than the length of the string, or if key does not exist, 0 is returned. Time complexity:
O(1)
Return value: string value Specifies the bit at the offset. Example: clock:20210806 represents the clocking record \ of 2021/08/06
2.3 BITCOUNT key [start] [end]
Counts the number of bits in the given string that are set to 1. The start and end arguments specify the scope of the query and can use negative values. -1 represents the last byte and -2 represents the second byte. Note: Start and end are byte indexes, so each increment of 1 represents an increment of one character, that is, 8 bits, so the query range of bits must be a multiple of 8. Time complexity:
O(N)
Return value: the number of bits set to 1 Example: clock:20210806 represents the clock record of 2021/08/06. At this time, there are 11 bits in total, the first 8 bits have 3 ones, and the last 3 bits have 2 ones \
Bitcount clock:20210806 0 0 Indicates the number of 1s in the first character. bitcount clock:20210806 1 1 Indicates the number of 1s in the second character. bitcount clock:20210806 0 1 Denotes the number of 1 \ in the first and second characters
2.4 BITPOS key bit [start] [end]
By default, the entire Bitmaps can be detected. You can also specify the range of Bitmaps by using the start and end arguments. Note: Start and end are byte indexes, so each increment of 1 represents an increment of 8 bits, so the range of Bitmaps must be a multiple of 8. Time complexity:
O(N)
Return value: integer Bitpos clock:20210806 0 indicates the position of the first 0. Bitpos clock:20210806 1 indicates the position of the first 1. 1 1 1 indicates the position of the first 1 in the second character. Bitpos clock:20210806 10 1 indicates the position of the first 1 in the first and second characters
2.5 BITOP operation destKey Key [key…]
Redis Bitmaps provides BITOP instructions to operate on one OR more binary keys (except for the NOT operation). The result of the operation is saved to destkey. Operation is the operation type, AND there are four types: AND, OR, NOT, XOR
- BITOP AND destkey Key [key… , obtains a logical union for one or more keys, and saves the result to destkey
- BITOP OR destkey key [key… To obtain logical or for one or more keys and save the result to destkey
- BITOP XOR destKey Key [key…] To obtain logical XOR for one or more keys and save the result to the Destkey
- BITOP NOT destKey Key Specifies the logical no of the given key and saves the result to the destkey
When the length of the string is inconsistent, the missing part of the shorter string is treated as a 0, and the empty key is treated as the time complexity of the string sequence containing 0:
O(N)
Return value: result of bit operation (the length of the string saved to destkey is equal to the length of the longest string in the input key) Example: here use key1 1001 and key2 1011 for the above four operations \
BITOP AND destkey Key [key… Operation rules: 0&0=0; 1 = 0 0 &; 1 & 0 = 0; 1 &1 = 1;
That is, if both of them are “1”, the result is “1”; otherwise, 0\
BITOP OR destkey key [key… Algorithm: 0 | 0 = 0; 0 | 1 = 1; 1 | 0 = 1; 1 | 1 = 1; That is, if either of the two objects participating in the operation is 1, the value is 1\
BITOP XOR destKey Key [key…] Operation rule: 0^0=0; 0 ^ 1 = 1; 1 ^ 0 = 1; 1 ^ 1 = 0; That is, if the two corresponding bits of the two objects participating in the operation are “different” (different values), the result of that bit is 1, otherwise, 0\
BITOP NOT destKey Key operation rule: Set the value invert \
2.6 BITFIELD key [GET type offset] [the SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP | | SAT FAIL]
Both setbit and getbit in 2.1 and 2.2 operate on a single bit of the specified key. If multiple bits need to be operated simultaneously, the bitfield instruction can be used. Bitfield has three sub-instructions, namely GET, set and incrby, which can read and write the specified fragment. However, a maximum of 64 consecutive bits are processed. More than 64 consecutive bits require the use of multiple subinstructions, and bitfield can execute multiple subinstructions at the same time (unsigned integers can only return 63 bits).
Note:
- Use the GET subcommand to access bits outside the current range of the string (including if the key does not exist). The value of the bits outside the range is treated as 0.
- Using the SET subcommand or the INCRBY subcommand to access bits beyond the current range of the string causes the string to be enlarged, and the enlarged bits are padded with bits with a value of 0. When extending a string, the command calculates the minimum length required to perform the operation based on the farthest bits of the string that are currently available.
Value operation subinstruction:
- GET – Returns the specified range of bits
- SET – Sets the specified range of bits and returns its old value
- INCRBY – Performs addition on the specified range of bits and returns its old value. The user can implement subtraction by passing a negative value to the increment parameter
Overflow policy subdirective:
- WRAP: WRAP around – The default overflow strategy. For unsigned integers, wrapping is like performing a modulo calculation using the value itself and the largest unsigned integer that can be stored. This is also standard behavior in C. For signed integers, an overflow causes the number to be reevaluated from the smallest negative number, while an underflow causes the number to be reevaluated from the largest positive number.
- SAT: saturation calculation can also be understood as saturation truncation. In this mode, the result of underflow calculation is the smallest integer value, while the result of overflow calculation is the largest integer value
- FAIL: This mode rejects computations that cause overflows or underflows and returns nil to indicate that the computations were not performed.
Note that the OVERFLOW subcommand will only have an effect on the INCRBY command executed immediately after it until the next OVERFLOW command is executed with it. By default, the INCRBY command uses WRAP to handle overflow calculations. I and u: I indicates a signed integer, and u indicates an unsigned integer. U4 represents a 4-bit unsigned integer and I8 represents an 8-bit signed integer.
Example: Test number is 10100111\
Bitfield key get u4 0 Take 4 bits from the first bit and get the unsigned number 1010=10\
Bitfield key set u8 0 128 replaces the next 8 bits with an unsigned integer 128, i.e. 10000000\
Bitfield key incrby u4 2 1 The next 4 unsigned digits +1\ from the second digit
Bitfield key set u8 0 128 GET U4 0 incrby u4 2 1