diff --git a/redisson/src/main/java/org/redisson/RedissonBitSet.java b/redisson/src/main/java/org/redisson/RedissonBitSet.java index 6f8ccfe45..a58fcce46 100644 --- a/redisson/src/main/java/org/redisson/RedissonBitSet.java +++ b/redisson/src/main/java/org/redisson/RedissonBitSet.java @@ -24,7 +24,8 @@ import java.util.List; import org.redisson.api.RBitSet; import org.redisson.api.RFuture; import org.redisson.client.codec.ByteArrayCodec; -import org.redisson.client.protocol.RedisCommand; +import org.redisson.client.codec.LongCodec; +import org.redisson.client.codec.StringCodec; import org.redisson.client.protocol.RedisCommands; import org.redisson.command.CommandAsyncExecutor; import org.redisson.command.CommandBatchService; @@ -57,12 +58,12 @@ public class RedissonBitSet extends RedissonExpirable implements RBitSet { @Override public RFuture getAsync(long bitIndex) { - return commandExecutor.readAsync(getName(), codec, RedisCommands.GETBIT, getName(), bitIndex); + return commandExecutor.readAsync(getName(), LongCodec.INSTANCE, RedisCommands.GETBIT, getName(), bitIndex); } @Override - public void set(long bitIndex) { - get(setAsync(bitIndex, true)); + public boolean set(long bitIndex) { + return get(setAsync(bitIndex, true)); } @Override @@ -82,11 +83,7 @@ public class RedissonBitSet extends RedissonExpirable implements RBitSet { @Override public RFuture setAsync(long bitIndex, boolean value) { - RedisCommand command = RedisCommands.SETBIT_TRUE; - if (!value) { - command = RedisCommands.SETBIT_FALSE; - } - return commandExecutor.writeAsync(getName(), codec, command, getName(), bitIndex, value ? 1 : 0); + return commandExecutor.writeAsync(getName(), LongCodec.INSTANCE, RedisCommands.SETBIT, getName(), bitIndex, value ? 1 : 0); } @Override @@ -204,7 +201,7 @@ public class RedissonBitSet extends RedissonExpirable implements RBitSet { public RFuture setAsync(long fromIndex, long toIndex, boolean value) { CommandBatchService executorService = new CommandBatchService(commandExecutor.getConnectionManager()); for (long i = fromIndex; i < toIndex; i++) { - executorService.writeAsync(getName(), codec, RedisCommands.SETBIT_VOID, getName(), i, value ? 1 : 0); + executorService.writeAsync(getName(), LongCodec.INSTANCE, RedisCommands.SETBIT_VOID, getName(), i, value ? 1 : 0); } return executorService.executeAsyncVoid(); } @@ -231,7 +228,7 @@ public class RedissonBitSet extends RedissonExpirable implements RBitSet { @Override public RFuture sizeAsync() { - return commandExecutor.readAsync(getName(), codec, RedisCommands.BITS_SIZE, getName()); + return commandExecutor.readAsync(getName(), LongCodec.INSTANCE, RedisCommands.BITS_SIZE, getName()); } @Override @@ -241,7 +238,7 @@ public class RedissonBitSet extends RedissonExpirable implements RBitSet { @Override public RFuture cardinalityAsync() { - return commandExecutor.readAsync(getName(), codec, RedisCommands.BITCOUNT, getName()); + return commandExecutor.readAsync(getName(), LongCodec.INSTANCE, RedisCommands.BITCOUNT, getName()); } @Override diff --git a/redisson/src/main/java/org/redisson/api/RBitSet.java b/redisson/src/main/java/org/redisson/api/RBitSet.java index 033df0058..e9684d5d2 100644 --- a/redisson/src/main/java/org/redisson/api/RBitSet.java +++ b/redisson/src/main/java/org/redisson/api/RBitSet.java @@ -52,8 +52,16 @@ public interface RBitSet extends RExpirable, RBitSetAsync { */ void clear(long fromIndex, long toIndex); + /** + * Copy bits state of source BitSet object to this object + * + * @param bs - BitSet source + */ void set(BitSet bs); + /** + * Executes NOT operation over all bits + */ void not(); /** @@ -84,9 +92,10 @@ public interface RBitSet extends RExpirable, RBitSetAsync { * Set bit to one at specified bitIndex * * @param bitIndex - index of bit - * + * @return true - if previous value was true, + * false - if previous value was false */ - void set(long bitIndex); + boolean set(long bitIndex); /** * Set bit to value at specified bitIndex @@ -122,10 +131,28 @@ public interface RBitSet extends RExpirable, RBitSetAsync { BitSet asBitSet(); + /** + * Executes OR operation over this object and specified bitsets. + * Stores result into this object. + * + * @param bitSetNames - name of stored bitsets + */ void or(String... bitSetNames); + /** + * Executes AND operation over this object and specified bitsets. + * Stores result into this object. + * + * @param bitSetNames - name of stored bitsets + */ void and(String... bitSetNames); + /** + * Executes XOR operation over this object and specified bitsets. + * Stores result into this object. + * + * @param bitSetNames - name of stored bitsets + */ void xor(String... bitSetNames); } diff --git a/redisson/src/main/java/org/redisson/api/RBitSetAsync.java b/redisson/src/main/java/org/redisson/api/RBitSetAsync.java index 473601a00..6af40ffd6 100644 --- a/redisson/src/main/java/org/redisson/api/RBitSetAsync.java +++ b/redisson/src/main/java/org/redisson/api/RBitSetAsync.java @@ -57,8 +57,19 @@ public interface RBitSetAsync extends RExpirableAsync { */ RFuture clearAsync(long fromIndex, long toIndex); + /** + * Copy bits state of source BitSet object to this object + * + * @param bs - BitSet source + * @return void + */ RFuture setAsync(BitSet bs); + /** + * Executes NOT operation over all bits + * + * @return void + */ RFuture notAsync(); /** @@ -67,7 +78,6 @@ public interface RBitSetAsync extends RExpirableAsync { * @param fromIndex inclusive * @param toIndex exclusive * @return void - * */ RFuture setAsync(long fromIndex, long toIndex); @@ -90,7 +100,8 @@ public interface RBitSetAsync extends RExpirableAsync { * Set bit to one at specified bitIndex * * @param bitIndex - index of bit - * @return void + * @return true - if previous value was true, + * false - if previous value was false */ RFuture setAsync(long bitIndex); @@ -99,9 +110,9 @@ public interface RBitSetAsync extends RExpirableAsync { * * @param bitIndex - index of bit * @param value true = 1, false = 0 - * @return previous value - * - */ + * @return true - if previous value was true, + * false - if previous value was false + */ RFuture setAsync(long bitIndex, boolean value); /** @@ -127,10 +138,31 @@ public interface RBitSetAsync extends RExpirableAsync { */ RFuture clearAsync(); + /** + * Executes OR operation over this object and specified bitsets. + * Stores result into this object. + * + * @param bitSetNames - name of stored bitsets + * @return void + */ RFuture orAsync(String... bitSetNames); + /** + * Executes AND operation over this object and specified bitsets. + * Stores result into this object. + * + * @param bitSetNames - name of stored bitsets + * @return void + */ RFuture andAsync(String... bitSetNames); + /** + * Executes XOR operation over this object and specified bitsets. + * Stores result into this object. + * + * @param bitSetNames - name of stored bitsets + * @return void + */ RFuture xorAsync(String... bitSetNames); } diff --git a/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java b/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java index 2d3a63aa3..1dddbf781 100644 --- a/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java +++ b/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java @@ -92,6 +92,7 @@ public interface RedisCommands { RedisStrictCommand BITCOUNT = new RedisStrictCommand("BITCOUNT"); RedisStrictCommand BITPOS = new RedisStrictCommand("BITPOS", new IntegerReplayConvertor()); RedisStrictCommand SETBIT_VOID = new RedisStrictCommand("SETBIT", new VoidReplayConvertor()); + RedisStrictCommand SETBIT = new RedisStrictCommand("SETBIT", new BooleanReplayConvertor()); RedisStrictCommand SETBIT_TRUE = new RedisStrictCommand("SETBIT", new BitSetReplayConvertor(0)); RedisStrictCommand SETBIT_FALSE = new RedisStrictCommand("SETBIT", new BitSetReplayConvertor(1)); RedisStrictCommand BITOP = new RedisStrictCommand("BITOP", new VoidReplayConvertor()); diff --git a/redisson/src/test/java/org/redisson/RedissonBitSetTest.java b/redisson/src/test/java/org/redisson/RedissonBitSetTest.java index 9e852cd0b..3ede613eb 100644 --- a/redisson/src/test/java/org/redisson/RedissonBitSetTest.java +++ b/redisson/src/test/java/org/redisson/RedissonBitSetTest.java @@ -65,8 +65,9 @@ public class RedissonBitSetTest extends BaseTest { @Test public void testSet() { RBitSet bs = redisson.getBitSet("testbitset"); - bs.set(3); - bs.set(5); + assertThat(bs.set(3)).isFalse(); + assertThat(bs.set(5)).isFalse(); + assertThat(bs.set(5)).isTrue(); assertThat(bs.toString()).isEqualTo("{3, 5}"); BitSet bs1 = new BitSet();