From 2b280e57e801d742d05ba324d98f076978550e1c Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 26 Mar 2014 09:03:43 +0100 Subject: [PATCH] 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 --- .../DefaultStringRedisConnection.java | 8 ++-- .../redis/connection/RedisStringCommands.java | 3 +- .../connection/StringRedisConnection.java | 10 ++++- .../connection/jedis/JedisConnection.java | 13 +++--- .../connection/jredis/JredisConnection.java | 4 +- .../connection/lettuce/LettuceConnection.java | 15 ++++--- .../connection/lettuce/LettuceConverters.java | 15 ++++++- .../redis/connection/srp/SrpConnection.java | 9 ++-- .../redis/connection/srp/SrpConverters.java | 41 +++++++++++++++++++ .../AbstractConnectionIntegrationTests.java | 15 +++---- 10 files changed, 101 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java b/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java index e779d006a..1cb592853 100644 --- a/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java @@ -724,8 +724,8 @@ public class DefaultStringRedisConnection implements StringRedisConnection { delegate.set(key, value); } - public void setBit(byte[] key, long offset, boolean value) { - delegate.setBit(key, offset, value); + public Boolean setBit(byte[] key, long offset, boolean value) { + return delegate.setBit(key, offset, value); } public void setConfig(String param, String value) { @@ -1706,8 +1706,8 @@ public class DefaultStringRedisConnection implements StringRedisConnection { delegate.set(serialize(key), serialize(value)); } - public void setBit(String key, long offset, boolean value) { - delegate.setBit(serialize(key), offset, value); + public Boolean setBit(String key, long offset, boolean value) { + return delegate.setBit(serialize(key), offset, value); } public void setEx(String key, long seconds, String value) { diff --git a/src/main/java/org/springframework/data/redis/connection/RedisStringCommands.java b/src/main/java/org/springframework/data/redis/connection/RedisStringCommands.java index f362be751..9929adf6d 100644 --- a/src/main/java/org/springframework/data/redis/connection/RedisStringCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/RedisStringCommands.java @@ -209,8 +209,9 @@ public interface RedisStringCommands { * @param key * @param offset * @param value + * @return the original bit value stored at {@code offset}. */ - void setBit(byte[] key, long offset, boolean value); + Boolean setBit(byte[] key, long offset, boolean value); /** * Count the number of set bits (population counting) in value stored at {@code key}. diff --git a/src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java b/src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java index 4a25597f1..9be9f35b3 100644 --- a/src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java @@ -128,7 +128,15 @@ public interface StringRedisConnection extends RedisConnection { Boolean getBit(String key, long offset); - void setBit(String key, long offset, boolean value); + /** + * Sets the bit at {@code offset} in value stored at {@code key}. + * + * @param key + * @param offset + * @param value + * @return the original bit value stored at {@code offset}. + */ + Boolean setBit(String key, long offset, boolean value); Long bitCount(String key); diff --git a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java index 7671f965f..116db8e13 100644 --- a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java @@ -1349,17 +1349,18 @@ public class JedisConnection implements RedisConnection { } } - public void setBit(byte[] key, long offset, boolean value) { + public Boolean setBit(byte[] key, long offset, boolean value) { try { if (isPipelined()) { - pipeline(new JedisStatusResult(pipeline.setbit(key, offset, JedisConverters.toBit(value)))); - return; + + pipeline(new JedisResult(pipeline.setbit(key, offset, JedisConverters.toBit(value)))); + return null; } if (isQueueing()) { - transaction(new JedisStatusResult(transaction.setbit(key, offset, JedisConverters.toBit(value)))); - return; + transaction(new JedisResult(transaction.setbit(key, offset, JedisConverters.toBit(value)))); + return null; } - jedis.setbit(key, offset, JedisConverters.toBit(value)); + return jedis.setbit(key, offset, JedisConverters.toBit(value)); } catch (Exception ex) { throw convertJedisAccessException(ex); } diff --git a/src/main/java/org/springframework/data/redis/connection/jredis/JredisConnection.java b/src/main/java/org/springframework/data/redis/connection/jredis/JredisConnection.java index 1f2b538a4..8bc46345a 100644 --- a/src/main/java/org/springframework/data/redis/connection/jredis/JredisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/jredis/JredisConnection.java @@ -574,9 +574,9 @@ public class JredisConnection implements RedisConnection { } } - public void setBit(byte[] key, long offset, boolean value) { + public Boolean setBit(byte[] key, long offset, boolean value) { try { - jredis.setbit(key, (int) offset, value); + return jredis.setbit(key, (int) offset, value); } catch (Exception ex) { throw convertJredisAccessException(ex); } diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java index 4483b6c5a..6815aeeee 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java @@ -1403,17 +1403,20 @@ public class LettuceConnection implements RedisConnection { } } - public void setBit(byte[] key, long offset, boolean value) { + public Boolean setBit(byte[] key, long offset, boolean value) { try { if (isPipelined()) { - pipeline(new LettuceStatusResult(getAsyncConnection().setbit(key, offset, LettuceConverters.toInt(value)))); - return; + pipeline(new LettuceResult(getAsyncConnection().setbit(key, offset, LettuceConverters.toInt(value)), + LettuceConverters.longToBooleanConverter())); + return null; } if (isQueueing()) { - transaction(new LettuceTxStatusResult(getConnection().setbit(key, offset, LettuceConverters.toInt(value)))); - return; + transaction(new LettuceTxResult(getConnection().setbit(key, offset, LettuceConverters.toInt(value)), + LettuceConverters.longToBooleanConverter())); + return null; } - getConnection().setbit(key, offset, LettuceConverters.toInt(value)); + return LettuceConverters.longToBooleanConverter().convert( + getConnection().setbit(key, offset, LettuceConverters.toInt(value))); } catch (Exception ex) { throw convertLettuceAccessException(ex); } diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java index 6e5604c18..720ea95e2 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,6 +32,7 @@ import org.springframework.data.redis.connection.ReturnType; import org.springframework.data.redis.connection.SortParameters; import org.springframework.data.redis.connection.SortParameters.Order; import org.springframework.data.redis.connection.convert.Converters; +import org.springframework.data.redis.connection.convert.LongToBooleanConverter; import org.springframework.data.redis.connection.convert.StringToRedisClientInfoConverter; import org.springframework.data.redis.core.types.RedisClientInfo; import org.springframework.util.Assert; @@ -47,6 +48,8 @@ import com.lambdaworks.redis.protocol.Charsets; * Lettuce type converters * * @author Jennifer Hickey + * @author Christoph Strobl + * @author Thomas Darimont */ abstract public class LettuceConverters extends Converters { @@ -58,6 +61,7 @@ abstract public class LettuceConverters extends Converters { private static final Converter>, Set> SCORED_VALUES_TO_TUPLE_SET; private static final Converter, Tuple> SCORED_VALUE_TO_TUPLE; private static final Converter EXCEPTION_CONVERTER = new LettuceExceptionConverter(); + private static final Converter LONG_TO_BOOLEAN = new LongToBooleanConverter(); private static final Converter> STRING_TO_LIST_OF_CLIENT_INFO = new StringToRedisClientInfoConverter(); @@ -119,6 +123,7 @@ abstract public class LettuceConverters extends Converters { } }; + } public static Converter> stringToRedisClientListConverter() { @@ -167,6 +172,14 @@ abstract public class LettuceConverters extends Converters { return EXCEPTION_CONVERTER; } + /** + * @return + * @sice 1.3 + */ + public static Converter longToBooleanConverter() { + return LONG_TO_BOOLEAN; + } + public static Long toLong(Date source) { return DATE_TO_LONG.convert(source); } diff --git a/src/main/java/org/springframework/data/redis/connection/srp/SrpConnection.java b/src/main/java/org/springframework/data/redis/connection/srp/SrpConnection.java index 65afaed30..9a93da1f7 100644 --- a/src/main/java/org/springframework/data/redis/connection/srp/SrpConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/srp/SrpConnection.java @@ -1048,13 +1048,14 @@ public class SrpConnection implements RedisConnection { } } - public void setBit(byte[] key, long offset, boolean value) { + public Boolean setBit(byte[] key, long offset, boolean value) { try { if (isPipelined()) { - pipeline(new SrpStatusResult(pipeline.setbit(key, offset, SrpConverters.toBit(value)))); - return; + pipeline(new SrpGenericResult(pipeline.setbit(key, offset, SrpConverters.toBit(value)), + SrpConverters.longToBooleanConverter())); + return null; } - client.setbit(key, offset, SrpConverters.toBit(value)); + return SrpConverters.toBoolean(client.setbit(key, offset, SrpConverters.toBit(value))); } catch (Exception ex) { throw convertSrpAccessException(ex); } diff --git a/src/main/java/org/springframework/data/redis/connection/srp/SrpConverters.java b/src/main/java/org/springframework/data/redis/connection/srp/SrpConverters.java index d9975aac3..68276a1f9 100644 --- a/src/main/java/org/springframework/data/redis/connection/srp/SrpConverters.java +++ b/src/main/java/org/springframework/data/redis/connection/srp/SrpConverters.java @@ -34,6 +34,7 @@ import org.springframework.data.redis.connection.RedisListCommands.Position; import org.springframework.data.redis.connection.RedisStringCommands.BitOperation; import org.springframework.data.redis.connection.RedisZSetCommands.Tuple; import org.springframework.data.redis.connection.convert.Converters; +import org.springframework.data.redis.connection.convert.LongToBooleanConverter; import org.springframework.data.redis.connection.convert.StringToRedisClientInfoConverter; import org.springframework.data.redis.core.types.RedisClientInfo; import org.springframework.util.Assert; @@ -70,6 +71,8 @@ abstract public class SrpConverters extends Converters { private static final Converter> REPLY_T0_LIST_OF_CLIENT_INFO; private static final Converter> STRING_TO_LIST_OF_CLIENT_INFO = new StringToRedisClientInfoConverter(); private static final Converter> BYTEARRAY_T0_LIST_OF_CLIENT_INFO; + private static final Converter INTEGER_REPLY_TO_BOOLEAN; + private static final Converter LONG_TO_BOOLEAN = new LongToBooleanConverter(); static { @@ -223,6 +226,17 @@ abstract public class SrpConverters extends Converters { return STRING_TO_LIST_OF_CLIENT_INFO.convert(s.split("\\r?\\n")); } }; + + INTEGER_REPLY_TO_BOOLEAN = new Converter() { + + @Override + public Boolean convert(IntegerReply source) { + if (source == null || source.data() == null) { + return false; + } + return source.data() == 1; + } + }; } public static Converter> repliesToBytesList() { @@ -269,10 +283,22 @@ abstract public class SrpConverters extends Converters { return REPLIES_TO_TIME_AS_LONG; } + /** + * @return + * @since 1.3 + */ public static List toListOfRedisClientInformation(Reply reply) { return REPLY_T0_LIST_OF_CLIENT_INFO.convert(reply); } + /** + * @return + * @since 1.3 + */ + public static Converter longToBooleanConverter() { + return LONG_TO_BOOLEAN; + } + public static List toBytesList(Reply[] source) { return REPLIES_TO_BYTES_LIST.convert(source); } @@ -358,7 +384,22 @@ abstract public class SrpConverters extends Converters { return Collections.singletonList(source); } + /** + * @since 1.3 + * @return + */ public static Converter> replyToListOfRedisClientInfo() { return BYTEARRAY_T0_LIST_OF_CLIENT_INFO; } + + /** + * Convert an {@link IntegerReply} to a {@link Boolean} by inspecting {@link IntegerReply#data()}. + * + * @since 1.3 + * @param reply + * @return + */ + public static Boolean toBoolean(IntegerReply reply) { + return INTEGER_REPLY_TO_BOOLEAN.convert(reply); + } } diff --git a/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java index ccb3c8f06..37e1a19e3 100644 --- a/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java @@ -417,22 +417,23 @@ public abstract class AbstractConnectionIntegrationTests { @Test public void testBitSet() throws Exception { String key = "bitset-test"; - connection.setBit(key, 0, false); - connection.setBit(key, 1, true); + actual.add(connection.setBit(key, 0, true)); + actual.add(connection.setBit(key, 0, false)); + actual.add(connection.setBit(key, 1, true)); actual.add(connection.getBit(key, 0)); actual.add(connection.getBit(key, 1)); - verifyResults(Arrays.asList(new Object[] { false, true })); + verifyResults(Arrays.asList(new Object[] { false, true, false, false, true })); } @Test @IfProfileValue(name = "redisVersion", value = "2.6") public void testBitCount() { String key = "bitset-test"; - connection.setBit(key, 0, false); - connection.setBit(key, 1, true); - connection.setBit(key, 2, true); + actual.add(connection.setBit(key, 0, false)); + actual.add(connection.setBit(key, 1, true)); + actual.add(connection.setBit(key, 2, true)); actual.add(connection.bitCount(key)); - verifyResults(new ArrayList(Collections.singletonList(2l))); + verifyResults(Arrays.asList(new Object[] { false, false, false, 2L })); } @Test