Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Impl incrbyfloat #400

Merged
merged 2 commits into from

6 participants

@etreworgy

Implemented incrByFloat and hincrByFloat commands. Included sharded and pipelined implementations and some tests.

@christophsturm

could need that, hope it will be merged soon

@xetorthio xetorthio was assigned
@xetorthio
Owner

I will merge it tomorrow! Thanks!

@allenma

not merged yet?

@xetorthio xetorthio added this to the Next minor milestone
@xetorthio
Owner

Sorry. It got lost with all other pull request. It is now scheduled for next release. Which should be in a couple of weeks.
Sorry again about that.

@marcosnils marcosnils merged commit 6af41cd into from
@marcosnils marcosnils referenced this pull request from a commit
@marcosnils marcosnils Manually merge #400 50052e9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 7, 2013
  1. @etreworgy

    Added incrByFloat and hincrByFloat commands (binary and standard) + s…

    etreworgy authored
    …upport for pipelining and sharding
Commits on Feb 20, 2013
  1. @etreworgy
This page is out of date. Refresh to see the latest.
View
8 src/main/java/redis/clients/jedis/BinaryClient.java
@@ -192,6 +192,10 @@ public void incrBy(final byte[] key, final long 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) {
sendCommand(INCR, key);
}
@@ -238,6 +242,10 @@ public void hincrBy(final byte[] key, final byte[] field, final long value) {
sendCommand(HINCRBY, key, field, toByteArray(value));
}
+ public void hincrByFloat(final byte[] key, final byte[] field, final double value) {
+ sendCommand(HINCRBYFLOAT, key, field, toByteArray(value));
+ }
+
public void hexists(final byte[] key, final byte[] field) {
sendCommand(HEXISTS, key, field);
}
View
56 src/main/java/redis/clients/jedis/BinaryJedis.java
@@ -609,6 +609,36 @@ public Long incrBy(final byte[] key, final long integer) {
}
/**
+ * 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
* contains a value of a wrong type, set the key to the value of "0" before
* to perform the increment operation.
@@ -801,6 +831,32 @@ public Long hincrBy(final byte[] key, final byte[] field, final long value) {
}
/**
+ * 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.
*
* <b>Time complexity:</b> O(1)
View
4 src/main/java/redis/clients/jedis/BinaryJedisCommands.java
@@ -49,6 +49,8 @@
Long incrBy(byte[] key, long integer);
+ Double incrByFloat(byte[] key, double value);
+
Long incr(byte[] key);
Long append(byte[] key, byte[] value);
@@ -67,6 +69,8 @@
Long hincrBy(byte[] key, byte[] field, long value);
+ Double hincrByFloat(byte[] key, byte[] field, double value);
+
Boolean hexists(byte[] key, byte[] field);
Long hdel(byte[] key, byte[]... field);
View
10 src/main/java/redis/clients/jedis/BinaryShardedJedis.java
@@ -111,6 +111,11 @@ public Long incrBy(byte[] key, long 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) {
Jedis j = getShard(key);
return j.incr(key);
@@ -156,6 +161,11 @@ public Long hincrBy(byte[] key, byte[] field, long 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) {
Jedis j = getShard(key);
return j.hexists(key, field);
View
8 src/main/java/redis/clients/jedis/Client.java
@@ -117,6 +117,10 @@ public void incrBy(final String key, final long integer) {
incrBy(SafeEncoder.encode(key), integer);
}
+ public void incrByFloat(final String key, final double value) {
+ incrByFloat(SafeEncoder.encode(key), value);
+ }
+
public void incr(final String key) {
incr(SafeEncoder.encode(key));
}
@@ -165,6 +169,10 @@ public void hincrBy(final String key, final String field, final long 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) {
hexists(SafeEncoder.encode(key), SafeEncoder.encode(field));
}
View
4 src/main/java/redis/clients/jedis/Commands.java
@@ -58,6 +58,8 @@
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 append(final String key, final String value);
@@ -76,6 +78,8 @@
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 hdel(final String key, final String... fields);
View
51 src/main/java/redis/clients/jedis/Jedis.java
@@ -523,6 +523,31 @@ public Long incrBy(final String key, final long integer) {
}
/**
+ * 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
* contains a value of a wrong type, set the key to the value of "0" before
* to perform the increment operation.
@@ -715,6 +740,32 @@ public Long hincrBy(final String key, final String field, final long value) {
}
/**
+ * 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.
*
* <b>Time complexity:</b> O(1)
View
20 src/main/java/redis/clients/jedis/PipelineBase.java
@@ -215,6 +215,16 @@
return getResponse(BuilderFactory.LONG);
}
+ public Response<Double> hincrByFloat(String key, String field, double value) {
+ getClient(key).hincrByFloat(key, field, value);
+ return getResponse(BuilderFactory.DOUBLE);
+ }
+
+ public Response<Double> hincrByFloat(byte[] key, byte[] field, double value) {
+ getClient(key).hincrByFloat(key, field, value);
+ return getResponse(BuilderFactory.DOUBLE);
+ }
+
public Response<Set<String>> hkeys(String key) {
getClient(key).hkeys(key);
return getResponse(BuilderFactory.STRING_SET);
@@ -305,6 +315,16 @@
return getResponse(BuilderFactory.LONG);
}
+ public Response<Double> incrByFloat(String key, double value) {
+ getClient(key).incrByFloat(key, value);
+ return getResponse(BuilderFactory.DOUBLE);
+ }
+
+ public Response<Double> incrByFloat(byte[] key, double value) {
+ getClient(key).incrByFloat(key, value);
+ return getResponse(BuilderFactory.DOUBLE);
+ }
+
public Response<String> lindex(String key, int index) {
getClient(key).lindex(key, index);
return getResponse(BuilderFactory.STRING);
View
2  src/main/java/redis/clients/jedis/Protocol.java
@@ -154,7 +154,7 @@ public static Object read(final RedisInputStream is) {
}
public static enum Command {
- PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREVRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT, SETRANGE, GETRANGE, EVAL, EVALSHA, SCRIPT, SLOWLOG, OBJECT, BITCOUNT, BITOP, SENTINEL;
+ PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCRBYFLOAT, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HINCRBYFLOAT, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREVRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT, SETRANGE, GETRANGE, EVAL, EVALSHA, SCRIPT, SLOWLOG, OBJECT, BITCOUNT, BITOP, SENTINEL;
public final byte[] raw;
View
10 src/main/java/redis/clients/jedis/ShardedJedis.java
@@ -131,6 +131,11 @@ public Long incrBy(String key, long 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) {
Jedis j = getShard(key);
return j.incr(key);
@@ -176,6 +181,11 @@ public Long hincrBy(String key, String field, long 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) {
Jedis j = getShard(key);
return j.hexists(key, field);
View
19 src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java
@@ -144,6 +144,25 @@ public void hincrBy() {
}
@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
public void hexists() {
Map<String, String> hash = new HashMap<String, String>();
hash.put("bar", "car");
View
16 src/test/java/redis/clients/jedis/tests/commands/StringValuesCommandsTest.java
@@ -124,6 +124,22 @@ public void incrBy() {
}
@Test(expected = JedisDataException.class)
+ public void incrByFloatWrongValue() {
+ jedis.set("foo", "bar");
+ jedis.incrByFloat("foo", 2d);
+ }
+
+ @Test
+ public void incrByFloat() {
+ Double value = jedis.incrByFloat("foo", 2d);
+ assertEquals((Double)2d, value);
+ value = jedis.incrByFloat("foo", 2.5d);
+ assertEquals((Double)4.5d, value);
+ value = jedis.incrByFloat("foo", -6.5d);
+ assertEquals(Double.compare(-2d, value), 0);
+ }
+
+ @Test(expected = JedisDataException.class)
public void decrWrongValue() {
jedis.set("foo", "bar");
jedis.decr("foo");
Something went wrong with that request. Please try again.