RBitSet.set should return boolean #1410

pull/1423/head
Nikita 7 years ago
parent 49b26f5545
commit 4e2e95d4a7

@ -24,7 +24,8 @@ import java.util.List;
import org.redisson.api.RBitSet; import org.redisson.api.RBitSet;
import org.redisson.api.RFuture; import org.redisson.api.RFuture;
import org.redisson.client.codec.ByteArrayCodec; 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.client.protocol.RedisCommands;
import org.redisson.command.CommandAsyncExecutor; import org.redisson.command.CommandAsyncExecutor;
import org.redisson.command.CommandBatchService; import org.redisson.command.CommandBatchService;
@ -57,12 +58,12 @@ public class RedissonBitSet extends RedissonExpirable implements RBitSet {
@Override @Override
public RFuture<Boolean> getAsync(long bitIndex) { 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 @Override
public void set(long bitIndex) { public boolean set(long bitIndex) {
get(setAsync(bitIndex, true)); return get(setAsync(bitIndex, true));
} }
@Override @Override
@ -82,11 +83,7 @@ public class RedissonBitSet extends RedissonExpirable implements RBitSet {
@Override @Override
public RFuture<Boolean> setAsync(long bitIndex, boolean value) { public RFuture<Boolean> setAsync(long bitIndex, boolean value) {
RedisCommand<Boolean> command = RedisCommands.SETBIT_TRUE; return commandExecutor.writeAsync(getName(), LongCodec.INSTANCE, RedisCommands.SETBIT, getName(), bitIndex, value ? 1 : 0);
if (!value) {
command = RedisCommands.SETBIT_FALSE;
}
return commandExecutor.writeAsync(getName(), codec, command, getName(), bitIndex, value ? 1 : 0);
} }
@Override @Override
@ -204,7 +201,7 @@ public class RedissonBitSet extends RedissonExpirable implements RBitSet {
public RFuture<Void> setAsync(long fromIndex, long toIndex, boolean value) { public RFuture<Void> setAsync(long fromIndex, long toIndex, boolean value) {
CommandBatchService executorService = new CommandBatchService(commandExecutor.getConnectionManager()); CommandBatchService executorService = new CommandBatchService(commandExecutor.getConnectionManager());
for (long i = fromIndex; i < toIndex; i++) { 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(); return executorService.executeAsyncVoid();
} }
@ -231,7 +228,7 @@ public class RedissonBitSet extends RedissonExpirable implements RBitSet {
@Override @Override
public RFuture<Long> sizeAsync() { public RFuture<Long> sizeAsync() {
return commandExecutor.readAsync(getName(), codec, RedisCommands.BITS_SIZE, getName()); return commandExecutor.readAsync(getName(), LongCodec.INSTANCE, RedisCommands.BITS_SIZE, getName());
} }
@Override @Override
@ -241,7 +238,7 @@ public class RedissonBitSet extends RedissonExpirable implements RBitSet {
@Override @Override
public RFuture<Long> cardinalityAsync() { public RFuture<Long> cardinalityAsync() {
return commandExecutor.readAsync(getName(), codec, RedisCommands.BITCOUNT, getName()); return commandExecutor.readAsync(getName(), LongCodec.INSTANCE, RedisCommands.BITCOUNT, getName());
} }
@Override @Override

@ -52,8 +52,16 @@ public interface RBitSet extends RExpirable, RBitSetAsync {
*/ */
void clear(long fromIndex, long toIndex); void clear(long fromIndex, long toIndex);
/**
* Copy bits state of source BitSet object to this object
*
* @param bs - BitSet source
*/
void set(BitSet bs); void set(BitSet bs);
/**
* Executes NOT operation over all bits
*/
void not(); void not();
/** /**
@ -84,9 +92,10 @@ public interface RBitSet extends RExpirable, RBitSetAsync {
* Set bit to one at specified bitIndex * Set bit to one at specified bitIndex
* *
* @param bitIndex - index of bit * @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> * Set bit to <code>value</code> at specified <code>bitIndex</code>
@ -122,10 +131,28 @@ public interface RBitSet extends RExpirable, RBitSetAsync {
BitSet asBitSet(); 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); 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); 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); void xor(String... bitSetNames);
} }

@ -57,8 +57,19 @@ public interface RBitSetAsync extends RExpirableAsync {
*/ */
RFuture<Void> clearAsync(long fromIndex, long toIndex); 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); RFuture<Void> setAsync(BitSet bs);
/**
* Executes NOT operation over all bits
*
* @return void
*/
RFuture<Void> notAsync(); RFuture<Void> notAsync();
/** /**
@ -67,7 +78,6 @@ public interface RBitSetAsync extends RExpirableAsync {
* @param fromIndex inclusive * @param fromIndex inclusive
* @param toIndex exclusive * @param toIndex exclusive
* @return void * @return void
*
*/ */
RFuture<Void> setAsync(long fromIndex, long toIndex); RFuture<Void> setAsync(long fromIndex, long toIndex);
@ -90,7 +100,8 @@ public interface RBitSetAsync extends RExpirableAsync {
* Set bit to one at specified bitIndex * Set bit to one at specified bitIndex
* *
* @param bitIndex - index of bit * @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); RFuture<Boolean> setAsync(long bitIndex);
@ -99,9 +110,9 @@ public interface RBitSetAsync extends RExpirableAsync {
* *
* @param bitIndex - index of bit * @param bitIndex - index of bit
* @param value true = 1, false = 0 * @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); RFuture<Boolean> setAsync(long bitIndex, boolean value);
/** /**
@ -127,10 +138,31 @@ public interface RBitSetAsync extends RExpirableAsync {
*/ */
RFuture<Void> clearAsync(); 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); 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); 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); RFuture<Void> xorAsync(String... bitSetNames);
} }

@ -92,6 +92,7 @@ public interface RedisCommands {
RedisStrictCommand<Long> BITCOUNT = new RedisStrictCommand<Long>("BITCOUNT"); RedisStrictCommand<Long> BITCOUNT = new RedisStrictCommand<Long>("BITCOUNT");
RedisStrictCommand<Integer> BITPOS = new RedisStrictCommand<Integer>("BITPOS", new IntegerReplayConvertor()); RedisStrictCommand<Integer> BITPOS = new RedisStrictCommand<Integer>("BITPOS", new IntegerReplayConvertor());
RedisStrictCommand<Void> SETBIT_VOID = new RedisStrictCommand<Void>("SETBIT", new VoidReplayConvertor()); 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_TRUE = new RedisStrictCommand<Boolean>("SETBIT", new BitSetReplayConvertor(0));
RedisStrictCommand<Boolean> SETBIT_FALSE = new RedisStrictCommand<Boolean>("SETBIT", new BitSetReplayConvertor(1)); RedisStrictCommand<Boolean> SETBIT_FALSE = new RedisStrictCommand<Boolean>("SETBIT", new BitSetReplayConvertor(1));
RedisStrictCommand<Void> BITOP = new RedisStrictCommand<Void>("BITOP", new VoidReplayConvertor()); RedisStrictCommand<Void> BITOP = new RedisStrictCommand<Void>("BITOP", new VoidReplayConvertor());

@ -65,8 +65,9 @@ public class RedissonBitSetTest extends BaseTest {
@Test @Test
public void testSet() { public void testSet() {
RBitSet bs = redisson.getBitSet("testbitset"); RBitSet bs = redisson.getBitSet("testbitset");
bs.set(3); assertThat(bs.set(3)).isFalse();
bs.set(5); assertThat(bs.set(5)).isFalse();
assertThat(bs.set(5)).isTrue();
assertThat(bs.toString()).isEqualTo("{3, 5}"); assertThat(bs.toString()).isEqualTo("{3, 5}");
BitSet bs1 = new BitSet(); BitSet bs1 = new BitSet();

Loading…
Cancel
Save