Merge pull request #6295 from seakider/fix_bitset_length

Fixed - method lengthAsync of RedissonBitSet
pull/6300/head
Nikita Koksharov 3 months ago committed by GitHub
commit f486208d96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -427,14 +427,22 @@ public class RedissonBitSet extends RedissonExpirable implements RBitSet {
@Override
public RFuture<Long> lengthAsync() {
return commandExecutor.evalReadAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_LONG,
"local fromBit = redis.call('bitpos', KEYS[1], 1, -1);"
+ "local toBit = 8*(fromBit/8 + 1) - fromBit % 8;"
+ "for i = toBit, fromBit, -1 do "
+ "if redis.call('getbit', KEYS[1], i) == 1 then "
+ "return i+1;"
+ "end;"
+ "end;" +
"return fromBit+1",
"local i = redis.call('bitpos', KEYS[1], 1, -1); "
+ "local pos = i < 0 and redis.call('bitpos', KEYS[1], 0, -1) or math.floor(i / 8) * 8; "
+ "while (pos >= 0) "
+ "do "
+ "i = redis.call('bitpos', KEYS[1], 1, math.floor(pos / 8), math.floor(pos / 8)); "
+ "if i < 0 then "
+ "pos = pos - 8; "
+ "else "
+ "for j = pos + 7, pos, -1 do "
+ "if redis.call('getbit', KEYS[1], j) == 1 then "
+ "return j + 1; "
+ "end; "
+ "end; "
+ "end; "
+ "end; "
+ "return 0; ",
Collections.<Object>singletonList(getRawName()));
}

@ -91,6 +91,20 @@ public class RedissonBitSetTest extends RedisDockerTest {
bs.clear();
bs.set(0);
assertThat(bs.length()).isEqualTo(1);
// no exception happened, but return is unexpected.
bs.clear();
bs.set(0, 2);
bs.set(9);
bs.clear(9, 10);
assertThat(bs.length()).isEqualTo(2);
// cause exception
bs.clear();
bs.set(7);
bs.set(9);
bs.clear(9, 10);
assertThat(bs.length()).isEqualTo(8);
}
@Test

Loading…
Cancel
Save