How to get all positions of the set bits in bitmaps of Redis?

好久不见. 提交于 2021-01-28 10:51:00

问题


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.

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.

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.


回答1:


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

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.



来源:https://stackoverflow.com/questions/52639300/how-to-get-all-positions-of-the-set-bits-in-bitmaps-of-redis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!