Add Redis 2.6 incrBy/hincrBy float ops to Connections

DATAREDIS-181
This commit is contained in:
Jennifer Hickey
2013-06-17 15:06:55 -07:00
parent 45627760aa
commit ced5a755e9
13 changed files with 153 additions and 3 deletions

View File

@@ -188,6 +188,10 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
return delegate.hIncrBy(key, field, delta);
}
public Double hIncrBy(byte[] key, byte[] field, double delta) {
return delegate.hIncrBy(key, field, delta);
}
public Set<byte[]> hKeys(byte[] key) {
return delegate.hKeys(key);
}
@@ -224,6 +228,10 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
return delegate.incrBy(key, value);
}
public Double incrBy(byte[] key, double value) {
return delegate.incrBy(key, value);
}
public Properties info() {
return delegate.info();
}
@@ -772,6 +780,9 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
return delegate.hIncrBy(serialize(key), serialize(field), delta);
}
public Double hIncrBy(String key, String field, double delta) {
return delegate.hIncrBy(serialize(key), serialize(field), delta);
}
public Set<String> hKeys(String key) {
return deserialize(delegate.hKeys(serialize(key)));
@@ -816,7 +827,10 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
return delegate.incrBy(serialize(key), value);
}
public Double incrBy(String key, double value) {
return delegate.incrBy(serialize(key), value);
}
public Collection<String> keys(String pattern) {
return deserialize(delegate.keys(serialize(pattern)));
}

View File

@@ -39,6 +39,8 @@ public interface RedisHashCommands {
Long hIncrBy(byte[] key, byte[] field, long delta);
Double hIncrBy(byte[] key, byte[] field, double delta);
Boolean hExists(byte[] key, byte[] field);
Boolean hDel(byte[] key, byte[] field);

View File

@@ -50,6 +50,8 @@ public interface RedisStringCommands {
Long incrBy(byte[] key, long value);
Double incrBy(byte[] key, double value);
Long decr(byte[] key);
Long decrBy(byte[] key, long value);

View File

@@ -101,6 +101,8 @@ public interface StringRedisConnection extends RedisConnection {
Long incrBy(String key, long value);
Double incrBy(String key, double value);
Long decr(String key);
Long decrBy(String key, long value);
@@ -241,6 +243,8 @@ public interface StringRedisConnection extends RedisConnection {
Long hIncrBy(String key, String field, long delta);
Double hIncrBy(String key, String field, double delta);
Boolean hExists(String key, String field);
Boolean hDel(String key, String field);

View File

@@ -1119,6 +1119,9 @@ public class JedisConnection implements RedisConnection {
}
}
public Double incrBy(byte[] key, double value) {
throw new UnsupportedOperationException();
}
public Boolean getBit(byte[] key, long offset) {
try {
@@ -2306,6 +2309,9 @@ public class JedisConnection implements RedisConnection {
}
}
public Double hIncrBy(byte[] key, byte[] field, double delta) {
throw new UnsupportedOperationException();
}
public Set<byte[]> hKeys(byte[] key) {
try {

View File

@@ -582,6 +582,11 @@ public class JredisConnection implements RedisConnection {
}
public Double incrBy(byte[] key, double value) {
throw new UnsupportedOperationException();
}
public Boolean getBit(byte[] key, long offset) {
try {
return jredis.getbit(key, (int)offset);
@@ -1139,6 +1144,9 @@ public class JredisConnection implements RedisConnection {
throw new UnsupportedOperationException();
}
public Double hIncrBy(byte[] key, byte[] field, double delta) {
throw new UnsupportedOperationException();
}
public Set<byte[]> hKeys(byte[] key) {
try {

View File

@@ -949,6 +949,18 @@ public class LettuceConnection implements RedisConnection {
}
}
public Double incrBy(byte[] key, double value) {
try {
if (isPipelined()) {
pipeline(getAsyncConnection().incrbyfloat(key, value));
return null;
}
return getConnection().incrbyfloat(key, value);
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
public Boolean getBit(byte[] key, long offset) {
try {
if (isPipelined()) {
@@ -1874,6 +1886,18 @@ public class LettuceConnection implements RedisConnection {
}
}
public Double hIncrBy(byte[] key, byte[] field, double delta) {
try {
if (isPipelined()) {
pipeline(getAsyncConnection().hincrbyfloat(key, field, delta));
return null;
}
return getConnection().hincrbyfloat(key, field, delta);
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
public Set<byte[]> hKeys(byte[] key) {
try {
if (isPipelined()) {

View File

@@ -904,6 +904,19 @@ public class SrpConnection implements RedisConnection {
}
public Double incrBy(byte[] key, double value) {
try {
if (isPipelined()) {
pipeline(pipeline.incrbyfloat(key, value));
return null;
}
return SrpUtils.toDouble(client.incrbyfloat(key, value).data());
} catch (Exception ex) {
throw convertSrpAccessException(ex);
}
}
public Boolean getBit(byte[] key, long offset) {
try {
if (isPipelined()) {
@@ -1832,6 +1845,17 @@ public class SrpConnection implements RedisConnection {
}
}
public Double hIncrBy(byte[] key, byte[] field, double delta) {
try {
if (isPipelined()) {
pipeline(pipeline.hincrbyfloat(key, field, delta));
return null;
}
return SrpUtils.toDouble(client.hincrbyfloat(key, field, delta).data());
} catch (Exception ex) {
throw convertSrpAccessException(ex);
}
}
public Set<byte[]> hKeys(byte[] key) {
try {

View File

@@ -747,6 +747,15 @@ public abstract class AbstractConnectionIntegrationTests {
verifyResults(Arrays.asList(new Object[] { 1l, 8l }), actual);
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testIncrByDouble() {
connection.set("tdb", "4.5");
actual.add(connection.incrBy("tdb", 7.2));
actual.add(connection.get("tdb"));
verifyResults(Arrays.asList(new Object[] { 11.7d, "11.7" }), actual);
}
@Test
public void testIncDecr() {
connection.set("incrtest", "0");
@@ -1338,6 +1347,15 @@ public abstract class AbstractConnectionIntegrationTests {
verifyResults(Arrays.asList(new Object[] { true, 5l, "5" }), actual);
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testHIncrByDouble() {
actual.add(connection.hSet("test", "key", "2.9"));
actual.add(connection.hIncrBy("test", "key", 3.5));
actual.add(connection.hGet("test", "key"));
verifyResults(Arrays.asList(new Object[] { true, 6.4d, "6.4" }), actual);
}
@Test
public void testHKeys() {
connection.hSet("test", "key", "2");

View File

@@ -145,6 +145,16 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
super.testBitOpNotMultipleSources();
}
@Test(expected=UnsupportedOperationException.class)
public void testHIncrByDouble() {
super.testHIncrByDouble();
}
@Test(expected=UnsupportedOperationException.class)
public void testIncrByDouble() {
super.testIncrByDouble();
}
@Test
public void testIncrDecrByLong() {
String key = "test.count";

View File

@@ -232,6 +232,16 @@ public class JedisConnectionPipelineIntegrationTests extends
super.testZRevRangeByScoreWithScoresOffsetCount();
}
@Test(expected=UnsupportedOperationException.class)
public void testHIncrByDouble() {
super.testHIncrByDouble();
}
@Test(expected=UnsupportedOperationException.class)
public void testIncrByDouble() {
super.testIncrByDouble();
}
// Overrides, usually due to return values being Long vs Boolean or Set vs
// List

View File

@@ -383,6 +383,16 @@ public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrat
super.testBitOpNotMultipleSources();
}
@Test(expected=UnsupportedOperationException.class)
public void testHIncrByDouble() {
super.testHIncrByDouble();
}
@Test(expected=UnsupportedOperationException.class)
public void testIncrByDouble() {
super.testIncrByDouble();
}
// Jredis returns null for rPush
@Test
public void testSort() {

View File

@@ -20,7 +20,6 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import java.util.ArrayList;
import java.util.Arrays;
@@ -31,7 +30,6 @@ import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.RedisVersionUtils;
import org.springframework.data.redis.connection.AbstractConnectionPipelineIntegrationTests;
import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.DefaultStringTuple;
@@ -172,6 +170,26 @@ public class SrpConnectionPipelineIntegrationTests extends
super.testGetRangeSetRange();
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testIncrByDouble() {
connection.set("tdb", "4.5");
actual.add(connection.incrBy("tdb", 7.2));
actual.add(connection.get("tdb"));
// pipelined incrBy returns value as a byte[] instead of Double
verifyResults(Arrays.asList(new Object[] { "11.7" , "11.7" }), actual);
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testHIncrByDouble() {
actual.add(connection.hSet("test", "key", "2.9"));
actual.add(connection.hIncrBy("test", "key", 3.5));
actual.add(connection.hGet("test", "key"));
// pipelined hIncrBy returns value as a byte[] instead of Double
verifyResults(Arrays.asList(new Object[] { 1l, "6.4", "6.4" }), actual);
}
protected Object convertResult(Object result) {
Object convertedResult = super.convertResult(result);
if (convertedResult instanceof Reply[]) {