知识星球:写代码那些事
----
如果你有收获|欢迎|点赞|关注|转发
----
这里会定期更新|大厂的开发|架构|方案设计
这里也会更新|如何摸鱼|抓虾
欢迎来到写代码那些事 !Redis作为一种高性能的键值存储数据库,拥有丰富的数据结构,其中之一就是BitMap。BitMap是一种位图数据结构,它可以在Redis中高效地存储和操作大量的布尔类型数据。本文将深度解析Redis BitMap的原理、用途和常见操作,帮助你更好地理解和应用这一功能。
Bitmaps are not an actual data type, but a set of bit-oriented operations defined on the String type which is treated like a bit vector. Since strings are binary safe blobs and their maximum length is 512 MB, they are suitable to set up to 2^32 different bits.
Bitmaps不是实际的数据类型,而是字符串类型上定义的一组面向位的操作,该操作被视为位矢量。由于字符串是binary safe blobs,其最大长度为512 MB,因此它们适合设置为2^32个不同的bits。
一个"binary safe string" 是指可以包含任何字符(字节)的字符串。举个例子,很多编程语言使用0x00字符作为字符串的结束标记,所以从这个角度来看,"binary safe string"就是可以包含这些字符的字符串。这意味着它不会在处理字符串时将这些特殊字符视为结束或者分隔符,而是会将它们作为字符串的一部分来处理。
这段介绍虽然简洁,但是包含的信息量很大
- 2^32 different bits:这就意味着Bitmaps可以轻松表示大数量级的信息
- 我们可以在一个或多个字符串上执行位操作
- 如果我们有一个集合包含0到100之间的整数,Bitmaps使用每一位来表示集合中对应整数的存在与否,从而可以用极少的存储空间来表示整个集合。这种方法可以在很小的开销下实现集合的判断、并集、交集等操作
Basic commands
- SETBIT sets a bit at the provided offset to 0 or 1.
- GETBIT returns the value of a bit at a given offset.
- BITOP lets you perform bitwise operations against one or more strings.
- BITPOS Return the position of the first bit set to 1 or 0 in a string
- BITCOUNT Count the number of set bits (population counting) in a string
- BITFIELD The command treats a Redis string as an array of bits, and is capable of addressing specific integer fields of varying bit widths and arbitrary non (necessary) aligned offset
- BITFIELD_RO Read-only variant of the BITFIELD command. It is like the original BITFIELD but only accepts GET subcommand and can safely be used in read-only replicas
SETBIT | GETBIT
语法 Time complexity:O(1)
SETBIT key offset value
GETBIT key offset
Examples
redis> setbit key 10 1
(integer) 0
redis> getbit key 10
(integer) 1
redis> getbit key 11
(integer) 0
Bitmaps are trivial to split into multiple keys, for example for the sake of sharding the data set and because in general it is better to avoid working with huge keys. To split a bitmap across different keys instead of setting all the bits into a key, a trivial strategy is just to store M bits per key and obtain the key name with bit-number/M and the Nth bit to address inside the key with bit-number MOD M.
怎么理解这句话呢?
其实就是每个键存储M位,然后使用"bit-number/m"的结果来获取键名称,并将第n个位的信息存储在对应的键内,以便在键内解决"bit-number mod M"的问题。
这种策略的目的是将大的位图分解成多个小的部分,以避免单个键变得过于庞大。通过将位图分布在多个键上,可以更有效地管理和查询位图数据。这对于需要在分布式系统中存储大量位图数据时尤其有用,可以避免单个键的存储负担过重。
案例分析:用户在线状态
假设我们需要记录用户的在线状态,可以使用一个Bitmaps来实现。以下是一个示例代码:
# 用户ID为1001的用户在线
user_id = 1001
time_slot = 10 # 时间段为第10个
# 设置用户ID为1001的用户在时间段10内在线
redis_conn.setbit(f"user_online:{user_id}", time_slot, 1)
# 获取用户ID为1001的用户在时间段10内是否在线
is_online = redis_conn.getbit(f"user_online:{user_id}", time_slot)
if is_online:
print("用户在线")
else:
print("用户不在线")
# 统计用户ID为1001的用户近10个时间段内的在线次数
online_count = redis_conn.bitcount(f"user_online:{user_id}", 0, 9)
print(f"用户近10个时间段内在线次数:{online_count}")
- 用户在线状态的记录:可以使用一个Bitmaps来记录用户的在线状态,每一位表示一个时间段,0表示不在线,1表示在线。
Bitmaps的优势主要体现在其高效的存储和操作性能,尤其在处理大量布尔类型数据时,Bitmaps能够节省大量内存和计算资源。
BITOP
"BITOP" 命令用于对多个位图进行按位逻辑操作,并将结果存储在指定的目标位图中。它的语法如下:
语法 Time complexity:O(N)
BITOP destkey key [key ...]
Examples
redis> SET key1 "foobar"
"OK"
redis> SET key2 "abcdef"
"OK"
redis> BITOP AND dest key1 key2
(integer) 6
redis> GET dest
"`bc`ab"
其中:
- operation 可以是 "AND"、"OR"、"XOR" 或 "NOT",分别表示按位与、按位或、按位异或和按位取反操作。
- destkey 是存储操作结果的目标位图的键名。
- key 是参与操作的源位图的键名,可以指定多个源位图。
示例
假设我们有两个位图,分别表示用户1和用户2的在线状态,我们可以使用 "BITOP" 命令来对这两个位图进行按位与操作,并将结果存储到新的位图中。以下是一个示例:
# 假设用户1在线状态的位图键为 "user:1:online",用户2在线状态的位图键为 "user:2:online"
# 我们将结果存储到 "online_both_users" 位图键中
# 对用户1和用户2的在线状态位图进行按位与操作,结果存储到 "online_both_users" 位图键中
127.0.0.1:6379> BITOP AND online_both_users user:1:online user:2:online
(integer) 7 # 操作成功,共有7位被设置为 1
# 获取 "online_both_users" 位图的第0个位的值
127.0.0.1:6379> GETBIT online_both_users 0
(integer) 1 # 由于按位与操作的结果在第0个位上被设置为 1,所以返回 1
请注意,"BITOP" 命令对参与操作的位图进行位数对齐,位数不足的位图会自动在高位补0。
通过 "BITOP" 命令,您可以进行位图的复杂逻辑操作,如按位与、按位或、按位异或等,从而实现更高级的位图处理需求。