如何在Redis的位图中获取设置位的所有位置? [英] How to get all positions of the set bits in bitmaps of Redis?

查看:66
本文介绍了如何在Redis的位图中获取设置位的所有位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,我们可以获取/设置单个位并对其进行计数,但没有命令可以检索设置位的所有位置.

As I know we can get/set an individual bit and count it but there is not command to retrieve all positions of the set bits.

我的解决方案是遍历所有可能的索引并询问它是否已设置.这种方式的缺点是如果密钥包含大量位,例如:1,000,000 位,则会导致大量的 getbits 请求.而且我需要事先知道我必须获得哪些索引.

My solution was iterate over all the possible index and ask if it is or not set. Disadvantage of this way is that it lead to a huge amount of getbits requests if the key contain large amount of bits, ex: 1,000,000 bits. And I need to know beforehand which indexes I have to get.

其他解决方案是使用get"命令获取密钥的值,然后在客户端扫描它.但我正在寻找一个更好的解决方案,它可以利用 redis 处理位图

Other solution is using 'get' command to get value of the key then scan it on client side. But I am looking for a better solution it can take advantage of redis in handle bitmaps

有人知道更好的解决方案吗?我真的很感激任何评论.

Anyone know about better solution? I would really appreciate any comment.

推荐答案

你可以迭代调用BITPOS命令找到1位的偏移量,每次改变起始位置.

You can iteratively call the BITPOS command to find the offsets of 1-bits, changing the start location each time.

如果您的应用程序对密集字段敏感,您可以通过执行 BITCOUNT 来采取分而治之的方法 在开始和结束段上查找位图中的高影响区域.

If your application is sensitive to dense fields, you can take a divide-and-conquer approach by doing a BITCOUNT on start-end segements to find the high-impact regions in your bitmap.

这篇关于如何在Redis的位图中获取设置位的所有位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆