Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Potential problem with setbit and getbit when used with BitSet.valueOf() #301
While experimenting with the code presented in this very popular blog post related to redis bitmaps, I came across a problem that I can't seem to get my head around.
It seems that when you individually write bits to redis via the Jedis setbit command, everything works properly as long as you use the getbit command to get the bit status back out. If you read the entire key however, the representation of the bitset appears to be in every way linear, and couldn't possibly work with BitSet.valueOf().
Here is a gist that inserts and retrieves the bitset in various ways, none of them work as expected.
It looks as though the get command retrieves that byte in literal order. For instance if I write all of the primes to 20 as a bit set into Redis, then read the array out, I get a byte representation of:
If I look at the binary representation of the first byte, I see 00110101. If read from left to right, like a human would, I see that bit position 2, 3, 5, and 7 are set, corresponding to the correct primes. Read, right to left, as BitSet.valueOf() would read them, the values are of course all wrong.
Additionally, when read via BitSet.valueOf() the byte order is backwards. I tried reversing the byte order, which would work just fine if the bit positions are read in reverse order.
This may be expected behavior, but the renders the code in the blog post referenced above incorrect. Given the code that I've tried reading the value of the key from Redis using Jedis and BitSet.valueOf() will never work properly without intermediary methods.
On Sun, May 20, 2012 at 1:48 PM, Grant Muller <
but wouldn't that prevent it from being cross-platform. If I
And if I do this:
This all may be expected behavior, it just implies that you cannot use BitSet.valueOf in java out of the box without reforming the order of the bits and bytes that you're reading.
How do you exactly do:
There is no overload for BitSet in Jedis. Actually Jedis doesn't know
On Mon, May 21, 2012 at 10:55 PM, Grant Muller <
It looks to me like the issue revolves around the fact that there is not a clear, documented way to get the values of a Redis bitset into a
There are at least 4 methods that I could think of to do this, most of which don't do what you want.
In order to get the right answer, I had to reverse the order of the bits in the bytes returned from
Jonathan, does it make sense to you to add a utility method to help people out with this?
Greg has it exactly right. Again, this isn't a requirement (but it would be nice to have a helper method built in). All I was trying to point out was that the built in methods aren't going to work, and to make sure that it wasn't because a bug in the way that the bytestream was being returned. It didn't look like it, its just the way that Redis stores the array (in VERY literal order).
Thanks for looking into it a little deeper guys. I'll keep a look out here for a helper method just in case. Otherwise referencing the gist Greg created will work for others doing this on their own.