Each bit in the bitmap is located using an offset value. The offset value of a given bit is calculated based on the x and y coordinates. The following mathematical formula can be applied to calculate the offset of a given bit.
Let’s consider the following bitmap and what the offset value looks like for different bits.
The values on each bit can be set using the offset value. Redis provides the SETBIT command to set a bit value to 0 or 1. In this Guide, we will be discussing the syntax and use cases of the SETBIT command.
The SETBIT Command
The SETBIT command is used to manipulate bits stored at a given offset in a Redis string. Specifically, this command sets a given bit’s value to 1 or 0. Redis bitmap can hold up to 2^32-1 offsets which limits the maximum size of a bitmap to 512MB. Furthermore, the bitmap offsets start from the 0th index.
The following is the syntax of the SETBIT command.
key: The name of the Redis key, which stores a string value.
offset: The offset value of the bit.
value: The value of the bit. This can be 1 or 0.
If the key doesn’t exist, the SETBIT command will create a new string value and set the bit value at a specified offset. Furthermore, the SETBIT command operates on O(1) time complexity which is a considerably fast process. This might slightly vary when we are going to set the 2^31-1 bit in a bitmap when the specified key doesn’t exist or stores a very small string value because the memory for all the intermediate bits should be allocated. It would take some time.
The SETBIT command returns the original bit value stored at the offset in the string stored at a specified key.
Use Case – File System Permissions Using Redis Bitmap
Let’s assume that a UNIX-like operating system maintains file permissions using bitmaps. The READ, WRITE and EXECUTE permissions for a given file/folder are stored in a bitmap as follows.
Let’s set the Read-only permissions to the file named “a.txt”. To make it clear, Read permission is denoted by the bit located at the 0th offset. So, we will be setting the 0th offset to 1 by calling the SETBIT command as follows.
Let’s verify whether the 0th offset has been properly set using the GETBIT command, as shown in the following.
We do not need to set the other two offsets explicitly since the default bit value of a bit is 0. Hence, the 1st and 2nd offset bits should be implicitly set to 0, as shown in the following.
Similarly, we can set all three read, write, and execute permissions on the folder abc as follows. Multiple bits can be set by calling the SETBIT command multiple times.
setbit folder:id:3:name:abc 1 1
setbit folder:id:3:name:abc 2 1
Let’s check the values of 3 bits using the GETBIT command.
getbit folder:id:3:name:abc 1
getbit folder:id:3:name:abc 2
Furthermore, we can use the Redis GET command to retrieve the string representation of a given bitmap. As you can see in the following output, the hex value has been returned.
The equal decimal value would be 224.
Overall, the SETBIT command can be effectively used to manipulate the bit of bitmap.
In summary, the SETBIT command is used to clear or set the bit value at a given offset in a Redis string stored at a given key. As mentioned, a Redis string can be treated as an array of bits that can represent a bitmap data structure. Also, the SETBIT command operates on bitmaps in O(1) time complexity. As you have seen in the use cases, multiple bits can be set by calling the SETBIT command multiple times. With the help of the Redis GET command, the string value of the bitmap can be retrieved.