问题
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