Merge branch 'etreworgy-impl-incrbyfloat'
This commit is contained in:
@@ -199,6 +199,10 @@ public class BinaryClient extends Connection {
|
|||||||
sendCommand(INCRBY, key, toByteArray(integer));
|
sendCommand(INCRBY, key, toByteArray(integer));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void incrByFloat(final byte[] key, final double value) {
|
||||||
|
sendCommand(INCRBYFLOAT, key, toByteArray(value));
|
||||||
|
}
|
||||||
|
|
||||||
public void incr(final byte[] key) {
|
public void incr(final byte[] key) {
|
||||||
sendCommand(INCR, key);
|
sendCommand(INCR, key);
|
||||||
}
|
}
|
||||||
@@ -1112,10 +1116,6 @@ public class BinaryClient extends Connection {
|
|||||||
sendCommand(PTTL, key);
|
sendCommand(PTTL, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void incrByFloat(final byte[] key, final double increment) {
|
|
||||||
sendCommand(INCRBYFLOAT, key, toByteArray(increment));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void psetex(final byte[] key, final int milliseconds,
|
public void psetex(final byte[] key, final int milliseconds,
|
||||||
final byte[] value) {
|
final byte[] value) {
|
||||||
sendCommand(PSETEX, key, toByteArray(milliseconds), value);
|
sendCommand(PSETEX, key, toByteArray(milliseconds), value);
|
||||||
|
|||||||
@@ -634,6 +634,36 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
|
|||||||
return client.getIntegerReply();
|
return client.getIntegerReply();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* INCRBYFLOAT work just like {@link #incrBy(byte[]) INCRBY} but increments
|
||||||
|
* by floats instead of integers.
|
||||||
|
* <p>
|
||||||
|
* INCRBYFLOAT commands are limited to double precision floating point values.
|
||||||
|
* <p>
|
||||||
|
* Note: this is actually a string operation, that is, in Redis there are
|
||||||
|
* not "double" types. Simply the string stored at the key is parsed as a
|
||||||
|
* base double precision floating point value, incremented, and then converted
|
||||||
|
* back as a string. There is no DECRYBYFLOAT but providing a negative
|
||||||
|
* value will work as expected.
|
||||||
|
* <p>
|
||||||
|
* Time complexity: O(1)
|
||||||
|
*
|
||||||
|
* @see #incr(byte[])
|
||||||
|
* @see #decr(byte[])
|
||||||
|
* @see #decrBy(byte[], long)
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* @param integer
|
||||||
|
* @return Integer reply, this commands will reply with the new value of key
|
||||||
|
* after the increment.
|
||||||
|
*/
|
||||||
|
public Double incrByFloat(final byte[] key, final double integer) {
|
||||||
|
checkIsInMulti();
|
||||||
|
client.incrByFloat(key, integer);
|
||||||
|
String dval = client.getBulkReply();
|
||||||
|
return (dval != null ? new Double(dval) : null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Increment the number stored at key by one. If the key does not exist or
|
* Increment the number stored at key by one. If the key does not exist or
|
||||||
* contains a value of a wrong type, set the key to the value of "0" before
|
* contains a value of a wrong type, set the key to the value of "0" before
|
||||||
@@ -826,6 +856,32 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
|
|||||||
return client.getIntegerReply();
|
return client.getIntegerReply();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Increment the number stored at field in the hash at key by a double
|
||||||
|
* precision floating point value. If key does not exist,
|
||||||
|
* a new key holding a hash is created. If field does not
|
||||||
|
* exist or holds a string, the value is set to 0 before applying the
|
||||||
|
* operation. Since the value argument is signed you can use this command to
|
||||||
|
* perform both increments and decrements.
|
||||||
|
* <p>
|
||||||
|
* The range of values supported by HINCRBYFLOAT is limited to
|
||||||
|
* double precision floating point values.
|
||||||
|
* <p>
|
||||||
|
* <b>Time complexity:</b> O(1)
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* @param field
|
||||||
|
* @param value
|
||||||
|
* @return Double precision floating point reply The new value at field after the increment
|
||||||
|
* operation.
|
||||||
|
*/
|
||||||
|
public Double hincrByFloat(final byte[] key, final byte[] field, final double value) {
|
||||||
|
checkIsInMulti();
|
||||||
|
client.hincrByFloat(key, field, value);
|
||||||
|
final String dval = client.getBulkReply();
|
||||||
|
return (dval != null ? new Double(dval) : null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for existence of a specified field in a hash.
|
* Test for existence of a specified field in a hash.
|
||||||
*
|
*
|
||||||
@@ -3347,13 +3403,6 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
|
|||||||
return client.getIntegerReply();
|
return client.getIntegerReply();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Double incrByFloat(final byte[] key, final double increment) {
|
|
||||||
checkIsInMulti();
|
|
||||||
client.incrByFloat(key, increment);
|
|
||||||
String relpy = client.getBulkReply();
|
|
||||||
return (relpy != null ? new Double(relpy) : null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String psetex(final byte[] key, final int milliseconds,
|
public String psetex(final byte[] key, final int milliseconds,
|
||||||
final byte[] value) {
|
final byte[] value) {
|
||||||
checkIsInMulti();
|
checkIsInMulti();
|
||||||
@@ -3411,14 +3460,6 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
|
|||||||
return client.getStatusCodeReply();
|
return client.getStatusCodeReply();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Double hincrByFloat(final byte[] key, final byte[] field,
|
|
||||||
double increment) {
|
|
||||||
checkIsInMulti();
|
|
||||||
client.hincrByFloat(key, field, increment);
|
|
||||||
String relpy = client.getBulkReply();
|
|
||||||
return (relpy != null ? new Double(relpy) : null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Syncrhonous replication of Redis as described here:
|
* Syncrhonous replication of Redis as described here:
|
||||||
* http://antirez.com/news/66
|
* http://antirez.com/news/66
|
||||||
|
|||||||
@@ -47,6 +47,8 @@ public interface BinaryJedisCommands {
|
|||||||
|
|
||||||
Long incrBy(byte[] key, long integer);
|
Long incrBy(byte[] key, long integer);
|
||||||
|
|
||||||
|
Double incrByFloat(byte[] key, double value);
|
||||||
|
|
||||||
Long incr(byte[] key);
|
Long incr(byte[] key);
|
||||||
|
|
||||||
Long append(byte[] key, byte[] value);
|
Long append(byte[] key, byte[] value);
|
||||||
@@ -65,6 +67,8 @@ public interface BinaryJedisCommands {
|
|||||||
|
|
||||||
Long hincrBy(byte[] key, byte[] field, long value);
|
Long hincrBy(byte[] key, byte[] field, long value);
|
||||||
|
|
||||||
|
Double hincrByFloat(byte[] key, byte[] field, double value);
|
||||||
|
|
||||||
Boolean hexists(byte[] key, byte[] field);
|
Boolean hexists(byte[] key, byte[] field);
|
||||||
|
|
||||||
Long hdel(byte[] key, byte[]... field);
|
Long hdel(byte[] key, byte[]... field);
|
||||||
|
|||||||
@@ -110,6 +110,11 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
|
|||||||
return j.incrBy(key, integer);
|
return j.incrBy(key, integer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Double incrByFloat(byte[] key, double integer) {
|
||||||
|
Jedis j = getShard(key);
|
||||||
|
return j.incrByFloat(key, integer);
|
||||||
|
}
|
||||||
|
|
||||||
public Long incr(byte[] key) {
|
public Long incr(byte[] key) {
|
||||||
Jedis j = getShard(key);
|
Jedis j = getShard(key);
|
||||||
return j.incr(key);
|
return j.incr(key);
|
||||||
@@ -155,6 +160,11 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
|
|||||||
return j.hincrBy(key, field, value);
|
return j.hincrBy(key, field, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Double hincrByFloat(byte[] key, byte[] field, double value) {
|
||||||
|
Jedis j = getShard(key);
|
||||||
|
return j.hincrByFloat(key, field, value);
|
||||||
|
}
|
||||||
|
|
||||||
public Boolean hexists(byte[] key, byte[] field) {
|
public Boolean hexists(byte[] key, byte[] field) {
|
||||||
Jedis j = getShard(key);
|
Jedis j = getShard(key);
|
||||||
return j.hexists(key, field);
|
return j.hexists(key, field);
|
||||||
|
|||||||
@@ -173,6 +173,10 @@ public class Client extends BinaryClient implements Commands {
|
|||||||
hincrBy(SafeEncoder.encode(key), SafeEncoder.encode(field), value);
|
hincrBy(SafeEncoder.encode(key), SafeEncoder.encode(field), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void hincrByFloat(final String key, final String field, final double value) {
|
||||||
|
hincrByFloat(SafeEncoder.encode(key), SafeEncoder.encode(field), value);
|
||||||
|
}
|
||||||
|
|
||||||
public void hexists(final String key, final String field) {
|
public void hexists(final String key, final String field) {
|
||||||
hexists(SafeEncoder.encode(key), SafeEncoder.encode(field));
|
hexists(SafeEncoder.encode(key), SafeEncoder.encode(field));
|
||||||
}
|
}
|
||||||
@@ -836,12 +840,6 @@ public class Client extends BinaryClient implements Commands {
|
|||||||
destinationDb, timeout);
|
destinationDb, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void hincrByFloat(final String key, final String field,
|
|
||||||
double increment) {
|
|
||||||
hincrByFloat(SafeEncoder.encode(key), SafeEncoder.encode(field),
|
|
||||||
increment);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
/**
|
/**
|
||||||
* This method is deprecated due to bug (scan cursor should be unsigned long)
|
* This method is deprecated due to bug (scan cursor should be unsigned long)
|
||||||
|
|||||||
@@ -61,6 +61,8 @@ public interface Commands {
|
|||||||
|
|
||||||
public void incrBy(final String key, final long integer);
|
public void incrBy(final String key, final long integer);
|
||||||
|
|
||||||
|
public void incrByFloat(final String key, final double value);
|
||||||
|
|
||||||
public void incr(final String key);
|
public void incr(final String key);
|
||||||
|
|
||||||
public void append(final String key, final String value);
|
public void append(final String key, final String value);
|
||||||
@@ -79,6 +81,8 @@ public interface Commands {
|
|||||||
|
|
||||||
public void hincrBy(final String key, final String field, final long value);
|
public void hincrBy(final String key, final String field, final long value);
|
||||||
|
|
||||||
|
public void hincrByFloat(final String key, final String field, final double value);
|
||||||
|
|
||||||
public void hexists(final String key, final String field);
|
public void hexists(final String key, final String field);
|
||||||
|
|
||||||
public void hdel(final String key, final String... fields);
|
public void hdel(final String key, final String... fields);
|
||||||
|
|||||||
@@ -540,6 +540,31 @@ public class Jedis extends BinaryJedis implements JedisCommands,
|
|||||||
return client.getIntegerReply();
|
return client.getIntegerReply();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* INCRBYFLOAT
|
||||||
|
* <p>
|
||||||
|
* INCRBYFLOAT commands are limited to double precision floating point values.
|
||||||
|
* <p>
|
||||||
|
* Note: this is actually a string operation, that is, in Redis there are
|
||||||
|
* not "double" types. Simply the string stored at the key is parsed as a
|
||||||
|
* base double precision floating point value, incremented, and then
|
||||||
|
* converted back as a string. There is no DECRYBYFLOAT but providing a
|
||||||
|
* negative value will work as expected.
|
||||||
|
* <p>
|
||||||
|
* Time complexity: O(1)
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* @param value
|
||||||
|
* @return Double reply, this commands will reply with the new value of key
|
||||||
|
* after the increment.
|
||||||
|
*/
|
||||||
|
public Double incrByFloat(final String key, final double value) {
|
||||||
|
checkIsInMulti();
|
||||||
|
client.incrByFloat(key, value);
|
||||||
|
String dval = client.getBulkReply();
|
||||||
|
return (dval != null ? new Double(dval) : null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Increment the number stored at key by one. If the key does not exist or
|
* Increment the number stored at key by one. If the key does not exist or
|
||||||
* contains a value of a wrong type, set the key to the value of "0" before
|
* contains a value of a wrong type, set the key to the value of "0" before
|
||||||
@@ -732,6 +757,32 @@ public class Jedis extends BinaryJedis implements JedisCommands,
|
|||||||
return client.getIntegerReply();
|
return client.getIntegerReply();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Increment the number stored at field in the hash at key by a double
|
||||||
|
* precision floating point value. If key does not exist,
|
||||||
|
* a new key holding a hash is created. If field does not
|
||||||
|
* exist or holds a string, the value is set to 0 before applying the
|
||||||
|
* operation. Since the value argument is signed you can use this command to
|
||||||
|
* perform both increments and decrements.
|
||||||
|
* <p>
|
||||||
|
* The range of values supported by HINCRBYFLOAT is limited to
|
||||||
|
* double precision floating point values.
|
||||||
|
* <p>
|
||||||
|
* <b>Time complexity:</b> O(1)
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* @param field
|
||||||
|
* @param value
|
||||||
|
* @return Double precision floating point reply The new value at field after the increment
|
||||||
|
* operation.
|
||||||
|
*/
|
||||||
|
public Double hincrByFloat(final String key, final String field, final double value) {
|
||||||
|
checkIsInMulti();
|
||||||
|
client.hincrByFloat(key, field, value);
|
||||||
|
final String dval = client.getBulkReply();
|
||||||
|
return (dval != null ? new Double(dval) : null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for existence of a specified field in a hash.
|
* Test for existence of a specified field in a hash.
|
||||||
*
|
*
|
||||||
@@ -3085,13 +3136,6 @@ public class Jedis extends BinaryJedis implements JedisCommands,
|
|||||||
return client.getIntegerReply();
|
return client.getIntegerReply();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Double incrByFloat(final String key, final double increment) {
|
|
||||||
checkIsInMulti();
|
|
||||||
client.incrByFloat(key, increment);
|
|
||||||
String relpy = client.getBulkReply();
|
|
||||||
return (relpy != null ? new Double(relpy) : null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String psetex(final String key, final int milliseconds,
|
public String psetex(final String key, final int milliseconds,
|
||||||
final String value) {
|
final String value) {
|
||||||
checkIsInMulti();
|
checkIsInMulti();
|
||||||
@@ -3131,14 +3175,6 @@ public class Jedis extends BinaryJedis implements JedisCommands,
|
|||||||
return client.getStatusCodeReply();
|
return client.getStatusCodeReply();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Double hincrByFloat(final String key, final String field,
|
|
||||||
double increment) {
|
|
||||||
checkIsInMulti();
|
|
||||||
client.hincrByFloat(key, field, increment);
|
|
||||||
String relpy = client.getBulkReply();
|
|
||||||
return (relpy != null ? new Double(relpy) : null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
/**
|
/**
|
||||||
* This method is deprecated due to bug (scan cursor should be unsigned long)
|
* This method is deprecated due to bug (scan cursor should be unsigned long)
|
||||||
|
|||||||
@@ -139,6 +139,11 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands {
|
|||||||
return j.incrBy(key, integer);
|
return j.incrBy(key, integer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Double incrByFloat(String key, double integer) {
|
||||||
|
Jedis j = getShard(key);
|
||||||
|
return j.incrByFloat(key, integer);
|
||||||
|
}
|
||||||
|
|
||||||
public Long incr(String key) {
|
public Long incr(String key) {
|
||||||
Jedis j = getShard(key);
|
Jedis j = getShard(key);
|
||||||
return j.incr(key);
|
return j.incr(key);
|
||||||
@@ -184,6 +189,11 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands {
|
|||||||
return j.hincrBy(key, field, value);
|
return j.hincrBy(key, field, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Double hincrByFloat(String key, String field, double value) {
|
||||||
|
Jedis j = getShard(key);
|
||||||
|
return j.hincrByFloat(key, field, value);
|
||||||
|
}
|
||||||
|
|
||||||
public Boolean hexists(String key, String field) {
|
public Boolean hexists(String key, String field) {
|
||||||
Jedis j = getShard(key);
|
Jedis j = getShard(key);
|
||||||
return j.hexists(key, field);
|
return j.hexists(key, field);
|
||||||
|
|||||||
@@ -153,6 +153,25 @@ public class HashesCommandsTest extends JedisCommandTestBase {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void hincrByFloat() {
|
||||||
|
Double value = jedis.hincrByFloat("foo", "bar", 1.5d);
|
||||||
|
assertEquals((Double) 1.5d, value);
|
||||||
|
value = jedis.hincrByFloat("foo", "bar", -1.5d);
|
||||||
|
assertEquals((Double) 0d, value);
|
||||||
|
value = jedis.hincrByFloat("foo", "bar", -10.7d);
|
||||||
|
assertEquals(Double.compare(-10.7d, value), 0);
|
||||||
|
|
||||||
|
// Binary
|
||||||
|
double bvalue = jedis.hincrByFloat(bfoo, bbar, 1.5d);
|
||||||
|
assertEquals(Double.compare(1.5d, bvalue), 0);
|
||||||
|
bvalue = jedis.hincrByFloat(bfoo, bbar, -1.5d);
|
||||||
|
assertEquals(Double.compare(0d, bvalue), 0);
|
||||||
|
bvalue = jedis.hincrByFloat(bfoo, bbar, -10.7d);
|
||||||
|
assertEquals(Double.compare(-10.7d, value), 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void hexists() {
|
public void hexists() {
|
||||||
Map<String, String> hash = new HashMap<String, String>();
|
Map<String, String> hash = new HashMap<String, String>();
|
||||||
|
|||||||
@@ -123,6 +123,12 @@ public class StringValuesCommandsTest extends JedisCommandTestBase {
|
|||||||
assertEquals(4, value);
|
assertEquals(4, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = JedisDataException.class)
|
||||||
|
public void incrByFloatWrongValue() {
|
||||||
|
jedis.set("foo", "bar");
|
||||||
|
jedis.incrByFloat("foo", 2d);
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expected = JedisDataException.class)
|
@Test(expected = JedisDataException.class)
|
||||||
public void decrWrongValue() {
|
public void decrWrongValue() {
|
||||||
jedis.set("foo", "bar");
|
jedis.set("foo", "bar");
|
||||||
|
|||||||
Reference in New Issue
Block a user