Skip to content

Commit

Permalink
RBitSet.set should return boolean #1410
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Apr 26, 2018
1 parent 49b26f5 commit 4e2e95d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 21 deletions.
21 changes: 9 additions & 12 deletions redisson/src/main/java/org/redisson/RedissonBitSet.java
Expand Up @@ -24,7 +24,8 @@
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;
Expand Down Expand Up @@ -57,12 +58,12 @@ public boolean get(long bitIndex) {

@Override
public RFuture<Boolean> 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
Expand All @@ -82,11 +83,7 @@ public void set(long bitIndex, boolean value) {

@Override
public RFuture<Boolean> setAsync(long bitIndex, boolean value) {
RedisCommand<Boolean> 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
Expand Down Expand Up @@ -204,7 +201,7 @@ public RFuture<Long> lengthAsync() {
public RFuture<Void> 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();
}
Expand All @@ -231,7 +228,7 @@ public RFuture<Void> setAsync(long fromIndex, long toIndex) {

@Override
public RFuture<Long> sizeAsync() {
return commandExecutor.readAsync(getName(), codec, RedisCommands.BITS_SIZE, getName());
return commandExecutor.readAsync(getName(), LongCodec.INSTANCE, RedisCommands.BITS_SIZE, getName());
}

@Override
Expand All @@ -241,7 +238,7 @@ public RFuture<Boolean> setAsync(long bitIndex) {

@Override
public RFuture<Long> cardinalityAsync() {
return commandExecutor.readAsync(getName(), codec, RedisCommands.BITCOUNT, getName());
return commandExecutor.readAsync(getName(), LongCodec.INSTANCE, RedisCommands.BITCOUNT, getName());
}

@Override
Expand Down
31 changes: 29 additions & 2 deletions redisson/src/main/java/org/redisson/api/RBitSet.java
Expand Up @@ -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();

/**
Expand Down Expand Up @@ -84,9 +92,10 @@ public interface RBitSet extends RExpirable, RBitSetAsync {
* Set bit to one at specified bitIndex
*
* @param bitIndex - index of bit
*
* @return <code>true</code> - if previous value was true,
* <code>false</code> - if previous value was false
*/
void set(long bitIndex);
boolean set(long bitIndex);

/**
* Set bit to <code>value</code> at specified <code>bitIndex</code>
Expand Down Expand Up @@ -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);

}
42 changes: 37 additions & 5 deletions redisson/src/main/java/org/redisson/api/RBitSetAsync.java
Expand Up @@ -57,8 +57,19 @@ public interface RBitSetAsync extends RExpirableAsync {
*/
RFuture<Void> clearAsync(long fromIndex, long toIndex);

/**
* Copy bits state of source BitSet object to this object
*
* @param bs - BitSet source
* @return void
*/
RFuture<Void> setAsync(BitSet bs);

/**
* Executes NOT operation over all bits
*
* @return void
*/
RFuture<Void> notAsync();

/**
Expand All @@ -67,7 +78,6 @@ public interface RBitSetAsync extends RExpirableAsync {
* @param fromIndex inclusive
* @param toIndex exclusive
* @return void
*
*/
RFuture<Void> setAsync(long fromIndex, long toIndex);

Expand All @@ -90,7 +100,8 @@ public interface RBitSetAsync extends RExpirableAsync {
* Set bit to one at specified bitIndex
*
* @param bitIndex - index of bit
* @return void
* @return <code>true</code> - if previous value was true,
* <code>false</code> - if previous value was false
*/
RFuture<Boolean> setAsync(long bitIndex);

Expand All @@ -99,9 +110,9 @@ public interface RBitSetAsync extends RExpirableAsync {
*
* @param bitIndex - index of bit
* @param value true = 1, false = 0
* @return previous value
*
*/
* @return <code>true</code> - if previous value was true,
* <code>false</code> - if previous value was false
*/
RFuture<Boolean> setAsync(long bitIndex, boolean value);

/**
Expand All @@ -127,10 +138,31 @@ public interface RBitSetAsync extends RExpirableAsync {
*/
RFuture<Void> clearAsync();

/**
* Executes OR operation over this object and specified bitsets.
* Stores result into this object.
*
* @param bitSetNames - name of stored bitsets
* @return void
*/
RFuture<Void> 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<Void> 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<Void> xorAsync(String... bitSetNames);

}
Expand Up @@ -92,6 +92,7 @@ public interface RedisCommands {
RedisStrictCommand<Long> BITCOUNT = new RedisStrictCommand<Long>("BITCOUNT");
RedisStrictCommand<Integer> BITPOS = new RedisStrictCommand<Integer>("BITPOS", new IntegerReplayConvertor());
RedisStrictCommand<Void> SETBIT_VOID = new RedisStrictCommand<Void>("SETBIT", new VoidReplayConvertor());
RedisStrictCommand<Boolean> SETBIT = new RedisStrictCommand<Boolean>("SETBIT", new BooleanReplayConvertor());
RedisStrictCommand<Boolean> SETBIT_TRUE = new RedisStrictCommand<Boolean>("SETBIT", new BitSetReplayConvertor(0));
RedisStrictCommand<Boolean> SETBIT_FALSE = new RedisStrictCommand<Boolean>("SETBIT", new BitSetReplayConvertor(1));
RedisStrictCommand<Void> BITOP = new RedisStrictCommand<Void>("BITOP", new VoidReplayConvertor());
Expand Down
5 changes: 3 additions & 2 deletions redisson/src/test/java/org/redisson/RedissonBitSetTest.java
Expand Up @@ -65,8 +65,9 @@ public void testNot() {
@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();
Expand Down

0 comments on commit 4e2e95d

Please sign in to comment.