Skip to content

Commit c466b0e

Browse files
DATAREDIS-287 - 'setBit' should return boolean value.
SetBit now returns the original bit value stored at the offset. The result will also be added in pipeline and transaction mode which means that the final result collection may now contain more entries than in prior versions. Original Pull Request: #58
1 parent cd1fac2 commit c466b0e

File tree

10 files changed

+89
-32
lines changed

10 files changed

+89
-32
lines changed

src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,8 @@ public void set(byte[] key, byte[] value) {
723723
delegate.set(key, value);
724724
}
725725

726-
public void setBit(byte[] key, long offset, boolean value) {
727-
delegate.setBit(key, offset, value);
726+
public Boolean setBit(byte[] key, long offset, boolean value) {
727+
return delegate.setBit(key, offset, value);
728728
}
729729

730730
public void setConfig(String param, String value) {
@@ -1705,8 +1705,8 @@ public void set(String key, String value) {
17051705
delegate.set(serialize(key), serialize(value));
17061706
}
17071707

1708-
public void setBit(String key, long offset, boolean value) {
1709-
delegate.setBit(serialize(key), offset, value);
1708+
public Boolean setBit(String key, long offset, boolean value) {
1709+
return delegate.setBit(serialize(key), offset, value);
17101710
}
17111711

17121712
public void setEx(String key, long seconds, String value) {

src/main/java/org/springframework/data/redis/connection/RedisStringCommands.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,9 @@ public enum BitOperation {
209209
* @param key
210210
* @param offset
211211
* @param value
212+
* @return the original bit value stored at {@code offset}.
212213
*/
213-
void setBit(byte[] key, long offset, boolean value);
214+
Boolean setBit(byte[] key, long offset, boolean value);
214215

215216
/**
216217
* Count the number of set bits (population counting) in value stored at {@code key}.

src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,15 @@ public interface StringTuple extends Tuple {
127127

128128
Boolean getBit(String key, long offset);
129129

130-
void setBit(String key, long offset, boolean value);
130+
/**
131+
* Sets the bit at {@code offset} in value stored at {@code key}.
132+
*
133+
* @param key
134+
* @param offset
135+
* @param value
136+
* @return the original bit value stored at {@code offset}.
137+
*/
138+
Boolean setBit(String key, long offset, boolean value);
131139

132140
Long bitCount(String key);
133141

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,17 +1348,18 @@ public Boolean getBit(byte[] key, long offset) {
13481348
}
13491349
}
13501350

1351-
public void setBit(byte[] key, long offset, boolean value) {
1351+
public Boolean setBit(byte[] key, long offset, boolean value) {
13521352
try {
13531353
if (isPipelined()) {
1354-
pipeline(new JedisStatusResult(pipeline.setbit(key, offset, JedisConverters.toBit(value))));
1355-
return;
1354+
1355+
pipeline(new JedisResult(pipeline.setbit(key, offset, JedisConverters.toBit(value))));
1356+
return null;
13561357
}
13571358
if (isQueueing()) {
1358-
transaction(new JedisStatusResult(transaction.setbit(key, offset, JedisConverters.toBit(value))));
1359-
return;
1359+
transaction(new JedisResult(transaction.setbit(key, offset, JedisConverters.toBit(value))));
1360+
return null;
13601361
}
1361-
jedis.setbit(key, offset, JedisConverters.toBit(value));
1362+
return jedis.setbit(key, offset, JedisConverters.toBit(value));
13621363
} catch (Exception ex) {
13631364
throw convertJedisAccessException(ex);
13641365
}

src/main/java/org/springframework/data/redis/connection/jredis/JredisConnection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,9 @@ public Boolean getBit(byte[] key, long offset) {
573573
}
574574
}
575575

576-
public void setBit(byte[] key, long offset, boolean value) {
576+
public Boolean setBit(byte[] key, long offset, boolean value) {
577577
try {
578-
jredis.setbit(key, (int) offset, value);
578+
return jredis.setbit(key, (int) offset, value);
579579
} catch (Exception ex) {
580580
throw convertJredisAccessException(ex);
581581
}

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,17 +1402,20 @@ public Boolean getBit(byte[] key, long offset) {
14021402
}
14031403
}
14041404

1405-
public void setBit(byte[] key, long offset, boolean value) {
1405+
public Boolean setBit(byte[] key, long offset, boolean value) {
14061406
try {
14071407
if (isPipelined()) {
1408-
pipeline(new LettuceStatusResult(getAsyncConnection().setbit(key, offset, LettuceConverters.toInt(value))));
1409-
return;
1408+
pipeline(new LettuceResult(getAsyncConnection().setbit(key, offset, LettuceConverters.toInt(value)),
1409+
LettuceConverters.longToBooleanConverter()));
1410+
return null;
14101411
}
14111412
if (isQueueing()) {
1412-
transaction(new LettuceTxStatusResult(getConnection().setbit(key, offset, LettuceConverters.toInt(value))));
1413-
return;
1413+
transaction(new LettuceTxResult(getConnection().setbit(key, offset, LettuceConverters.toInt(value)),
1414+
LettuceConverters.longToBooleanConverter()));
1415+
return null;
14141416
}
1415-
getConnection().setbit(key, offset, LettuceConverters.toInt(value));
1417+
return LettuceConverters.longToBooleanConverter().convert(
1418+
getConnection().setbit(key, offset, LettuceConverters.toInt(value)));
14161419
} catch (Exception ex) {
14171420
throw convertLettuceAccessException(ex);
14181421
}

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.data.redis.connection.SortParameters;
3131
import org.springframework.data.redis.connection.SortParameters.Order;
3232
import org.springframework.data.redis.connection.convert.Converters;
33+
import org.springframework.data.redis.connection.convert.LongToBooleanConverter;
3334
import org.springframework.util.Assert;
3435

3536
import com.lambdaworks.redis.KeyValue;
@@ -52,6 +53,7 @@ abstract public class LettuceConverters extends Converters {
5253
private static final Converter<List<ScoredValue<byte[]>>, Set<Tuple>> SCORED_VALUES_TO_TUPLE_SET;
5354
private static final Converter<ScoredValue<byte[]>, Tuple> SCORED_VALUE_TO_TUPLE;
5455
private static final Converter<Exception, DataAccessException> EXCEPTION_CONVERTER = new LettuceExceptionConverter();
56+
private static final Converter<Long, Boolean> LONG_TO_BOOLEAN = new LongToBooleanConverter();
5557

5658
static {
5759
DATE_TO_LONG = new Converter<Date, Long>() {
@@ -100,6 +102,7 @@ public Tuple convert(ScoredValue<byte[]> source) {
100102
}
101103

102104
};
105+
103106
}
104107

105108
public static Converter<Date, Long> dateToLong() {
@@ -130,6 +133,14 @@ public static Converter<Exception, DataAccessException> exceptionConverter() {
130133
return EXCEPTION_CONVERTER;
131134
}
132135

136+
/**
137+
* @return
138+
* @sice 1.3
139+
*/
140+
public static Converter<Long, Boolean> longToBooleanConverter() {
141+
return LONG_TO_BOOLEAN;
142+
}
143+
133144
public static Long toLong(Date source) {
134145
return DATE_TO_LONG.convert(source);
135146
}

src/main/java/org/springframework/data/redis/connection/srp/SrpConnection.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,13 +1047,14 @@ public Boolean getBit(byte[] key, long offset) {
10471047
}
10481048
}
10491049

1050-
public void setBit(byte[] key, long offset, boolean value) {
1050+
public Boolean setBit(byte[] key, long offset, boolean value) {
10511051
try {
10521052
if (isPipelined()) {
1053-
pipeline(new SrpStatusResult(pipeline.setbit(key, offset, SrpConverters.toBit(value))));
1054-
return;
1053+
pipeline(new SrpGenericResult(pipeline.setbit(key, offset, SrpConverters.toBit(value)),
1054+
SrpConverters.longToBooleanConverter()));
1055+
return null;
10551056
}
1056-
client.setbit(key, offset, SrpConverters.toBit(value));
1057+
return SrpConverters.toBoolean(client.setbit(key, offset, SrpConverters.toBit(value)));
10571058
} catch (Exception ex) {
10581059
throw convertSrpAccessException(ex);
10591060
}

src/main/java/org/springframework/data/redis/connection/srp/SrpConverters.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
import org.springframework.data.redis.connection.RedisStringCommands.BitOperation;
3535
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
3636
import org.springframework.data.redis.connection.convert.Converters;
37+
import org.springframework.data.redis.connection.convert.LongToBooleanConverter;
3738
import org.springframework.util.Assert;
38-
import org.springframework.util.NumberUtils;
3939

4040
import redis.client.RedisException;
4141
import redis.reply.IntegerReply;
@@ -65,6 +65,8 @@ abstract public class SrpConverters extends Converters {
6565
private static final Converter<byte[], String> BYTES_TO_STRING;
6666
private static final Converter<byte[], Double> BYTES_TO_DOUBLE;
6767
private static final Converter<Reply[], Long> REPLIES_TO_TIME_AS_LONG;
68+
private static final Converter<IntegerReply, Boolean> INTEGER_REPLY_TO_BOOLEAN;
69+
private static final Converter<Long, Boolean> LONG_TO_BOOLEAN = new LongToBooleanConverter();
6870

6971
static {
7072
REPLIES_TO_BYTES_LIST = new Converter<Reply[], List<byte[]>>() {
@@ -171,6 +173,16 @@ public List<String> convert(Reply[] source) {
171173
return results;
172174
}
173175
};
176+
INTEGER_REPLY_TO_BOOLEAN = new Converter<IntegerReply, Boolean>() {
177+
178+
@Override
179+
public Boolean convert(IntegerReply source) {
180+
if (source == null || source.data() == null) {
181+
return false;
182+
}
183+
return source.data() == 1;
184+
}
185+
};
174186
}
175187

176188
public static Converter<Reply[], List<byte[]>> repliesToBytesList() {
@@ -213,6 +225,14 @@ public static Converter<Reply[], Long> repliesToTimeAsLong() {
213225
return REPLIES_TO_TIME_AS_LONG;
214226
}
215227

228+
/**
229+
* @return
230+
* @since 1.3
231+
*/
232+
public static Converter<Long, Boolean> longToBooleanConverter() {
233+
return LONG_TO_BOOLEAN;
234+
}
235+
216236
public static List<byte[]> toBytesList(Reply[] source) {
217237
return REPLIES_TO_BYTES_LIST.convert(source);
218238
}
@@ -293,4 +313,15 @@ public static byte[] toBytes(Position source) {
293313
public static List<String> toStringList(String source) {
294314
return Collections.singletonList(source);
295315
}
316+
317+
/**
318+
* Convert an {@link IntegerReply} to a {@link Boolean} by inspecting {@link IntegerReply#data()}.
319+
*
320+
* @since 1.3
321+
* @param reply
322+
* @return
323+
*/
324+
public static Boolean toBoolean(IntegerReply reply) {
325+
return INTEGER_REPLY_TO_BOOLEAN.convert(reply);
326+
}
296327
}

src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -416,22 +416,23 @@ public void testPingPong() throws Exception {
416416
@Test
417417
public void testBitSet() throws Exception {
418418
String key = "bitset-test";
419-
connection.setBit(key, 0, false);
420-
connection.setBit(key, 1, true);
419+
actual.add(connection.setBit(key, 0, true));
420+
actual.add(connection.setBit(key, 0, false));
421+
actual.add(connection.setBit(key, 1, true));
421422
actual.add(connection.getBit(key, 0));
422423
actual.add(connection.getBit(key, 1));
423-
verifyResults(Arrays.asList(new Object[] { false, true }));
424+
verifyResults(Arrays.asList(new Object[] { false, true, false, false, true }));
424425
}
425426

426427
@Test
427428
@IfProfileValue(name = "redisVersion", value = "2.6")
428429
public void testBitCount() {
429430
String key = "bitset-test";
430-
connection.setBit(key, 0, false);
431-
connection.setBit(key, 1, true);
432-
connection.setBit(key, 2, true);
431+
actual.add(connection.setBit(key, 0, false));
432+
actual.add(connection.setBit(key, 1, true));
433+
actual.add(connection.setBit(key, 2, true));
433434
actual.add(connection.bitCount(key));
434-
verifyResults(new ArrayList<Object>(Collections.singletonList(2l)));
435+
verifyResults(Arrays.asList(new Object[] { false, false, false, 2L }));
435436
}
436437

437438
@Test

0 commit comments

Comments
 (0)