Skip to content

Commit 2b280e5

Browse files
christophstroblThomas Darimont
authored and
Thomas Darimont
committed
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 2679405 commit 2b280e5

File tree

10 files changed

+101
-32
lines changed

10 files changed

+101
-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
@@ -724,8 +724,8 @@ public void set(byte[] key, byte[] value) {
724724
delegate.set(key, value);
725725
}
726726

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

731731
public void setConfig(String param, String value) {
@@ -1706,8 +1706,8 @@ public void set(String key, String value) {
17061706
delegate.set(serialize(key), serialize(value));
17071707
}
17081708

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

17131713
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
@@ -128,7 +128,15 @@ public interface StringTuple extends Tuple {
128128

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

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

133141
Long bitCount(String key);
134142

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
@@ -1349,17 +1349,18 @@ public Boolean getBit(byte[] key, long offset) {
13491349
}
13501350
}
13511351

1352-
public void setBit(byte[] key, long offset, boolean value) {
1352+
public Boolean setBit(byte[] key, long offset, boolean value) {
13531353
try {
13541354
if (isPipelined()) {
1355-
pipeline(new JedisStatusResult(pipeline.setbit(key, offset, JedisConverters.toBit(value))));
1356-
return;
1355+
1356+
pipeline(new JedisResult(pipeline.setbit(key, offset, JedisConverters.toBit(value))));
1357+
return null;
13571358
}
13581359
if (isQueueing()) {
1359-
transaction(new JedisStatusResult(transaction.setbit(key, offset, JedisConverters.toBit(value))));
1360-
return;
1360+
transaction(new JedisResult(transaction.setbit(key, offset, JedisConverters.toBit(value))));
1361+
return null;
13611362
}
1362-
jedis.setbit(key, offset, JedisConverters.toBit(value));
1363+
return jedis.setbit(key, offset, JedisConverters.toBit(value));
13631364
} catch (Exception ex) {
13641365
throw convertJedisAccessException(ex);
13651366
}

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
@@ -574,9 +574,9 @@ public Boolean getBit(byte[] key, long offset) {
574574
}
575575
}
576576

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

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
@@ -1403,17 +1403,20 @@ public Boolean getBit(byte[] key, long offset) {
14031403
}
14041404
}
14051405

1406-
public void setBit(byte[] key, long offset, boolean value) {
1406+
public Boolean setBit(byte[] key, long offset, boolean value) {
14071407
try {
14081408
if (isPipelined()) {
1409-
pipeline(new LettuceStatusResult(getAsyncConnection().setbit(key, offset, LettuceConverters.toInt(value))));
1410-
return;
1409+
pipeline(new LettuceResult(getAsyncConnection().setbit(key, offset, LettuceConverters.toInt(value)),
1410+
LettuceConverters.longToBooleanConverter()));
1411+
return null;
14111412
}
14121413
if (isQueueing()) {
1413-
transaction(new LettuceTxStatusResult(getConnection().setbit(key, offset, LettuceConverters.toInt(value))));
1414-
return;
1414+
transaction(new LettuceTxResult(getConnection().setbit(key, offset, LettuceConverters.toInt(value)),
1415+
LettuceConverters.longToBooleanConverter()));
1416+
return null;
14151417
}
1416-
getConnection().setbit(key, offset, LettuceConverters.toInt(value));
1418+
return LettuceConverters.longToBooleanConverter().convert(
1419+
getConnection().setbit(key, offset, LettuceConverters.toInt(value)));
14171420
} catch (Exception ex) {
14181421
throw convertLettuceAccessException(ex);
14191422
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import org.springframework.data.redis.connection.SortParameters;
3333
import org.springframework.data.redis.connection.SortParameters.Order;
3434
import org.springframework.data.redis.connection.convert.Converters;
35+
import org.springframework.data.redis.connection.convert.LongToBooleanConverter;
3536
import org.springframework.data.redis.connection.convert.StringToRedisClientInfoConverter;
3637
import org.springframework.data.redis.core.types.RedisClientInfo;
3738
import org.springframework.util.Assert;
@@ -47,6 +48,8 @@
4748
* Lettuce type converters
4849
*
4950
* @author Jennifer Hickey
51+
* @author Christoph Strobl
52+
* @author Thomas Darimont
5053
*/
5154
abstract public class LettuceConverters extends Converters {
5255

@@ -58,6 +61,7 @@ abstract public class LettuceConverters extends Converters {
5861
private static final Converter<List<ScoredValue<byte[]>>, Set<Tuple>> SCORED_VALUES_TO_TUPLE_SET;
5962
private static final Converter<ScoredValue<byte[]>, Tuple> SCORED_VALUE_TO_TUPLE;
6063
private static final Converter<Exception, DataAccessException> EXCEPTION_CONVERTER = new LettuceExceptionConverter();
64+
private static final Converter<Long, Boolean> LONG_TO_BOOLEAN = new LongToBooleanConverter();
6165

6266
private static final Converter<String[], List<RedisClientInfo>> STRING_TO_LIST_OF_CLIENT_INFO = new StringToRedisClientInfoConverter();
6367

@@ -119,6 +123,7 @@ public Tuple convert(ScoredValue<byte[]> source) {
119123
}
120124

121125
};
126+
122127
}
123128

124129
public static Converter<String, List<RedisClientInfo>> stringToRedisClientListConverter() {
@@ -167,6 +172,14 @@ public static Converter<Exception, DataAccessException> exceptionConverter() {
167172
return EXCEPTION_CONVERTER;
168173
}
169174

175+
/**
176+
* @return
177+
* @sice 1.3
178+
*/
179+
public static Converter<Long, Boolean> longToBooleanConverter() {
180+
return LONG_TO_BOOLEAN;
181+
}
182+
170183
public static Long toLong(Date source) {
171184
return DATE_TO_LONG.convert(source);
172185
}

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
@@ -1048,13 +1048,14 @@ public Boolean getBit(byte[] key, long offset) {
10481048
}
10491049
}
10501050

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

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
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.data.redis.connection.convert.StringToRedisClientInfoConverter;
3839
import org.springframework.data.redis.core.types.RedisClientInfo;
3940
import org.springframework.util.Assert;
@@ -70,6 +71,8 @@ abstract public class SrpConverters extends Converters {
7071
private static final Converter<Reply, List<RedisClientInfo>> REPLY_T0_LIST_OF_CLIENT_INFO;
7172
private static final Converter<String[], List<RedisClientInfo>> STRING_TO_LIST_OF_CLIENT_INFO = new StringToRedisClientInfoConverter();
7273
private static final Converter<byte[], List<RedisClientInfo>> BYTEARRAY_T0_LIST_OF_CLIENT_INFO;
74+
private static final Converter<IntegerReply, Boolean> INTEGER_REPLY_TO_BOOLEAN;
75+
private static final Converter<Long, Boolean> LONG_TO_BOOLEAN = new LongToBooleanConverter();
7376

7477
static {
7578

@@ -223,6 +226,17 @@ public List<RedisClientInfo> convert(byte[] source) {
223226
return STRING_TO_LIST_OF_CLIENT_INFO.convert(s.split("\\r?\\n"));
224227
}
225228
};
229+
230+
INTEGER_REPLY_TO_BOOLEAN = new Converter<IntegerReply, Boolean>() {
231+
232+
@Override
233+
public Boolean convert(IntegerReply source) {
234+
if (source == null || source.data() == null) {
235+
return false;
236+
}
237+
return source.data() == 1;
238+
}
239+
};
226240
}
227241

228242
public static Converter<Reply[], List<byte[]>> repliesToBytesList() {
@@ -269,10 +283,22 @@ public static Converter<Reply[], Long> repliesToTimeAsLong() {
269283
return REPLIES_TO_TIME_AS_LONG;
270284
}
271285

286+
/**
287+
* @return
288+
* @since 1.3
289+
*/
272290
public static List<RedisClientInfo> toListOfRedisClientInformation(Reply reply) {
273291
return REPLY_T0_LIST_OF_CLIENT_INFO.convert(reply);
274292
}
275293

294+
/**
295+
* @return
296+
* @since 1.3
297+
*/
298+
public static Converter<Long, Boolean> longToBooleanConverter() {
299+
return LONG_TO_BOOLEAN;
300+
}
301+
276302
public static List<byte[]> toBytesList(Reply[] source) {
277303
return REPLIES_TO_BYTES_LIST.convert(source);
278304
}
@@ -358,7 +384,22 @@ public static List<String> toStringList(String source) {
358384
return Collections.singletonList(source);
359385
}
360386

387+
/**
388+
* @since 1.3
389+
* @return
390+
*/
361391
public static Converter<byte[], List<RedisClientInfo>> replyToListOfRedisClientInfo() {
362392
return BYTEARRAY_T0_LIST_OF_CLIENT_INFO;
363393
}
394+
395+
/**
396+
* Convert an {@link IntegerReply} to a {@link Boolean} by inspecting {@link IntegerReply#data()}.
397+
*
398+
* @since 1.3
399+
* @param reply
400+
* @return
401+
*/
402+
public static Boolean toBoolean(IntegerReply reply) {
403+
return INTEGER_REPLY_TO_BOOLEAN.convert(reply);
404+
}
364405
}

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

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

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

438439
@Test

0 commit comments

Comments
 (0)