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
This commit is contained in:
Christoph Strobl
2014-03-26 09:03:43 +01:00
committed by Thomas Darimont
parent 2679405e96
commit 2b280e57e8
10 changed files with 101 additions and 32 deletions

View File

@@ -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) {

View File

@@ -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}.

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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<List<ScoredValue<byte[]>>, Set<Tuple>> SCORED_VALUES_TO_TUPLE_SET;
private static final Converter<ScoredValue<byte[]>, Tuple> SCORED_VALUE_TO_TUPLE;
private static final Converter<Exception, DataAccessException> EXCEPTION_CONVERTER = new LettuceExceptionConverter();
private static final Converter<Long, Boolean> LONG_TO_BOOLEAN = new LongToBooleanConverter();
private static final Converter<String[], List<RedisClientInfo>> STRING_TO_LIST_OF_CLIENT_INFO = new StringToRedisClientInfoConverter();
@@ -119,6 +123,7 @@ abstract public class LettuceConverters extends Converters {
}
};
}
public static Converter<String, List<RedisClientInfo>> stringToRedisClientListConverter() {
@@ -167,6 +172,14 @@ abstract public class LettuceConverters extends Converters {
return EXCEPTION_CONVERTER;
}
/**
* @return
* @sice 1.3
*/
public static Converter<Long, Boolean> longToBooleanConverter() {
return LONG_TO_BOOLEAN;
}
public static Long toLong(Date source) {
return DATE_TO_LONG.convert(source);
}

View File

@@ -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);
}

View File

@@ -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, List<RedisClientInfo>> REPLY_T0_LIST_OF_CLIENT_INFO;
private static final Converter<String[], List<RedisClientInfo>> STRING_TO_LIST_OF_CLIENT_INFO = new StringToRedisClientInfoConverter();
private static final Converter<byte[], List<RedisClientInfo>> BYTEARRAY_T0_LIST_OF_CLIENT_INFO;
private static final Converter<IntegerReply, Boolean> INTEGER_REPLY_TO_BOOLEAN;
private static final Converter<Long, Boolean> 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<IntegerReply, Boolean>() {
@Override
public Boolean convert(IntegerReply source) {
if (source == null || source.data() == null) {
return false;
}
return source.data() == 1;
}
};
}
public static Converter<Reply[], List<byte[]>> repliesToBytesList() {
@@ -269,10 +283,22 @@ abstract public class SrpConverters extends Converters {
return REPLIES_TO_TIME_AS_LONG;
}
/**
* @return
* @since 1.3
*/
public static List<RedisClientInfo> toListOfRedisClientInformation(Reply reply) {
return REPLY_T0_LIST_OF_CLIENT_INFO.convert(reply);
}
/**
* @return
* @since 1.3
*/
public static Converter<Long, Boolean> longToBooleanConverter() {
return LONG_TO_BOOLEAN;
}
public static List<byte[]> 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<byte[], List<RedisClientInfo>> 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);
}
}