From 72ca4943627bced9fafc316d0487e3f939a02009 Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Fri, 23 Dec 2011 12:31:32 -0300 Subject: [PATCH] add missing command to Transaction, BinaryTransaction and Pipeline --- .../redis/clients/jedis/BinaryClient.java | 8 +- .../java/redis/clients/jedis/BinaryJedis.java | 18 ++-- .../clients/jedis/BinaryTransaction.java | 82 +++++++++++++++- src/main/java/redis/clients/jedis/Client.java | 8 ++ src/main/java/redis/clients/jedis/Jedis.java | 98 ++++++++++++++++--- .../redis/clients/jedis/JedisCommands.java | 6 +- .../java/redis/clients/jedis/Pipeline.java | 47 +++++++-- .../redis/clients/jedis/ShardedJedis.java | 6 +- .../jedis/tests/commands/BitCommandsTest.java | 6 +- 9 files changed, 235 insertions(+), 44 deletions(-) diff --git a/src/main/java/redis/clients/jedis/BinaryClient.java b/src/main/java/redis/clients/jedis/BinaryClient.java index 4a76a63..812defe 100644 --- a/src/main/java/redis/clients/jedis/BinaryClient.java +++ b/src/main/java/redis/clients/jedis/BinaryClient.java @@ -643,12 +643,12 @@ public class BinaryClient extends Connection { sendCommand(SLAVEOF, NO.raw, ONE.raw); } - public void configGet(final String pattern) { - sendCommand(CONFIG, Keyword.GET.name(), pattern); + public void configGet(final byte[] pattern) { + sendCommand(CONFIG, Keyword.GET.raw, pattern); } - public void configSet(final String parameter, final String value) { - sendCommand(CONFIG, Keyword.SET.name(), parameter, value); + public void configSet(final byte[] parameter, final byte[] value) { + sendCommand(CONFIG, Keyword.SET.raw, parameter, value); } public void strlen(final byte[] key) { diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index 8632497..7b4cd30 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -2822,9 +2822,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @param pattern * @return Bulk reply. */ - public List configGet(final String pattern) { + public List configGet(final byte[] pattern) { client.configGet(pattern); - return client.getMultiBulkReply(); + return client.getBinaryMultiBulkReply(); } /** @@ -2871,9 +2871,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @param value * @return Status code reply */ - public String configSet(final String parameter, final String value) { + public byte[] configSet(final byte[] parameter, final byte[] value) { client.configSet(parameter, value); - return client.getStatusCodeReply(); + return client.getBinaryBulkReply(); } public boolean isConnected() { @@ -2959,9 +2959,9 @@ public class BinaryJedis implements BinaryJedisCommands { * @param value * @return */ - public Long setbit(byte[] key, long offset, byte[] value) { + public Boolean setbit(byte[] key, long offset, byte[] value) { client.setbit(key, offset, value); - return client.getIntegerReply(); + return client.getIntegerReply() == 1; } /** @@ -2971,12 +2971,12 @@ public class BinaryJedis implements BinaryJedisCommands { * @param offset * @return */ - public Long getbit(byte[] key, long offset) { + public Boolean getbit(byte[] key, long offset) { client.getbit(key, offset); - return client.getIntegerReply(); + return client.getIntegerReply() == 1; } - public long setrange(byte[] key, long offset, byte[] value) { + public Long setrange(byte[] key, long offset, byte[] value) { client.setrange(key, offset, value); return client.getIntegerReply(); } diff --git a/src/main/java/redis/clients/jedis/BinaryTransaction.java b/src/main/java/redis/clients/jedis/BinaryTransaction.java index 54f6483..3a6893d 100644 --- a/src/main/java/redis/clients/jedis/BinaryTransaction.java +++ b/src/main/java/redis/clients/jedis/BinaryTransaction.java @@ -568,4 +568,84 @@ public class BinaryTransaction extends Queable { client.select(index); return getResponse(BuilderFactory.STRING); } -} + + public Response flushDB() { + client.flushDB(); + return getResponse(BuilderFactory.STRING); + } + + public Response flushAll() { + client.flushAll(); + return getResponse(BuilderFactory.STRING); + } + + public Response save() { + client.save(); + return getResponse(BuilderFactory.STRING); + } + + public Response info() { + client.info(); + return getResponse(BuilderFactory.STRING); + } + + public Response lastsave() { + client.lastsave(); + return getResponse(BuilderFactory.LONG); + } + + public Response dbSize() { + client.dbSize(); + return getResponse(BuilderFactory.LONG); + } + + public Response> configGet(final byte[] pattern) { + client.configGet(pattern); + return getResponse(BuilderFactory.BYTE_ARRAY_LIST); + } + + public Response configSet(final byte[] parameter, final byte[] value) { + client.configSet(parameter, value); + return getResponse(BuilderFactory.BYTE_ARRAY); + } + + public Response configResetStat() { + client.configResetStat(); + return getResponse(BuilderFactory.STRING); + } + + public Response shutdown() { + client.shutdown(); + return getResponse(BuilderFactory.STRING); + } + + public Response getbit(final byte[] key, final long offset) { + client.getbit(key, offset); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response setbit(final byte[] key, final long offset, final byte[] value) { + client.setbit(key, offset, value); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response ping() { + client.ping(); + return getResponse(BuilderFactory.STRING); + } + + public Response setrange(byte[] key, long offset, byte[] value) { + client.setrange(key, offset, value); + return getResponse(BuilderFactory.LONG); + } + + public Response randomKey() { + client.randomKey(); + return getResponse(BuilderFactory.STRING); + } + + public Response publish(byte[] channel, byte[] message) { + client.publish(channel, message); + return getResponse(BuilderFactory.LONG); + } +} \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index fb35c10..1f493e0 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -587,4 +587,12 @@ public class Client extends BinaryClient implements Commands { } subscribe(cs); } + + public void configSet(String parameter, String value) { + configSet(SafeEncoder.encode(parameter), SafeEncoder.encode(value)); + } + + public void configGet(String pattern) { + configGet(SafeEncoder.encode(pattern)); + } } diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index b43b00a..c71d9aa 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -226,18 +226,6 @@ public class Jedis extends BinaryJedis implements JedisCommands { return client.getIntegerReply(); } - /** - * Return the number of keys in the currently selected database. - * - * @return Integer reply - */ - - public Long dbSize() { - checkIsInMulti(); - client.dbSize(); - return client.getIntegerReply(); - } - /** * Set a timeout on the specified key. After the timeout the key will be * automatically deleted by the server. A key with an associated timeout is @@ -2595,7 +2583,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @param value * @return */ - public boolean setbit(String key, long offset, boolean value) { + public Boolean setbit(String key, long offset, boolean value) { client.setbit(key, offset, value); return client.getIntegerReply() == 1; } @@ -2607,12 +2595,12 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @param offset * @return */ - public boolean getbit(String key, long offset) { + public Boolean getbit(String key, long offset) { client.getbit(key, offset); return client.getIntegerReply() == 1; } - public long setrange(String key, long offset, String value) { + public Long setrange(String key, long offset, String value) { client.setrange(key, offset, value); return client.getIntegerReply(); } @@ -2621,4 +2609,84 @@ public class Jedis extends BinaryJedis implements JedisCommands { client.getrange(key, startOffset, endOffset); return client.getBulkReply(); } + + /** + * Retrieve the configuration of a running Redis server. Not all the + * configuration parameters are supported. + *

+ * CONFIG GET returns the current configuration parameters. This sub command + * only accepts a single argument, that is glob style pattern. All the + * configuration parameters matching this parameter are reported as a list + * of key-value pairs. + *

+ * Example: + * + *

+     * $ redis-cli config get '*'
+     * 1. "dbfilename"
+     * 2. "dump.rdb"
+     * 3. "requirepass"
+     * 4. (nil)
+     * 5. "masterauth"
+     * 6. (nil)
+     * 7. "maxmemory"
+     * 8. "0\n"
+     * 9. "appendfsync"
+     * 10. "everysec"
+     * 11. "save"
+     * 12. "3600 1 300 100 60 10000"
+     * 
+     * $ redis-cli config get 'm*'
+     * 1. "masterauth"
+     * 2. (nil)
+     * 3. "maxmemory"
+     * 4. "0\n"
+     * 
+ * + * @param pattern + * @return Bulk reply. + */ + public List configGet(final String pattern) { + client.configGet(pattern); + return client.getMultiBulkReply(); + } + + /** + * Alter the configuration of a running Redis server. Not all the + * configuration parameters are supported. + *

+ * The list of configuration parameters supported by CONFIG SET can be + * obtained issuing a {@link #configGet(String) CONFIG GET *} command. + *

+ * The configuration set using CONFIG SET is immediately loaded by the Redis + * server that will start acting as specified starting from the next + * command. + *

+ * + * Parameters value format + *

+ * The value of the configuration parameter is the same as the one of the + * same parameter in the Redis configuration file, with the following + * exceptions: + *

+ *

    + *
  • The save paramter is a list of space-separated integers. Every pair + * of integers specify the time and number of changes limit to trigger a + * save. For instance the command CONFIG SET save "3600 10 60 10000" will + * configure the server to issue a background saving of the RDB file every + * 3600 seconds if there are at least 10 changes in the dataset, and every + * 60 seconds if there are at least 10000 changes. To completely disable + * automatic snapshots just set the parameter as an empty string. + *
  • All the integer parameters representing memory are returned and + * accepted only using bytes as unit. + *
+ * + * @param parameter + * @param value + * @return Status code reply + */ + public String configSet(final String parameter, final String value) { + client.configSet(parameter, value); + return client.getStatusCodeReply(); + } } diff --git a/src/main/java/redis/clients/jedis/JedisCommands.java b/src/main/java/redis/clients/jedis/JedisCommands.java index 806bfbb..603b60c 100644 --- a/src/main/java/redis/clients/jedis/JedisCommands.java +++ b/src/main/java/redis/clients/jedis/JedisCommands.java @@ -22,11 +22,11 @@ public interface JedisCommands { Long ttl(String key); - boolean setbit(String key, long offset, boolean value); + Boolean setbit(String key, long offset, boolean value); - boolean getbit(String key, long offset); + Boolean getbit(String key, long offset); - long setrange(String key, long offset, String value); + Long setrange(String key, long offset, String value); String getrange(String key, long startOffset, long endOffset); diff --git a/src/main/java/redis/clients/jedis/Pipeline.java b/src/main/java/redis/clients/jedis/Pipeline.java index 692ab72..95bc773 100644 --- a/src/main/java/redis/clients/jedis/Pipeline.java +++ b/src/main/java/redis/clients/jedis/Pipeline.java @@ -40,11 +40,11 @@ public class Pipeline extends Queable { List unformatted = client.getAll(); List formatted = new ArrayList(); for (Object o : unformatted) { - try{ - formatted.add(generateResponse(o).get()); - }catch(JedisDataException e){ - formatted.add(e); - } + try { + formatted.add(generateResponse(o).get()); + } catch (JedisDataException e) { + formatted.add(e); + } } return formatted; } @@ -1191,4 +1191,39 @@ public class Pipeline extends Queable { client.publish(channel, message); return getResponse(BuilderFactory.LONG); } -} + + public Response flushDB() { + client.flushDB(); + return getResponse(BuilderFactory.STRING); + } + + public Response flushAll() { + client.flushAll(); + return getResponse(BuilderFactory.STRING); + } + + public Response info() { + client.info(); + return getResponse(BuilderFactory.STRING); + } + + public Response dbSize() { + client.dbSize(); + return getResponse(BuilderFactory.LONG); + } + + public Response shutdown() { + client.shutdown(); + return getResponse(BuilderFactory.STRING); + } + + public Response ping() { + client.ping(); + return getResponse(BuilderFactory.STRING); + } + + public Response randomKey() { + client.randomKey(); + return getResponse(BuilderFactory.STRING); + } +} \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/ShardedJedis.java b/src/main/java/redis/clients/jedis/ShardedJedis.java index b8c00e7..b294149 100644 --- a/src/main/java/redis/clients/jedis/ShardedJedis.java +++ b/src/main/java/redis/clients/jedis/ShardedJedis.java @@ -68,17 +68,17 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands { return j.ttl(key); } - public boolean setbit(String key, long offset, boolean value) { + public Boolean setbit(String key, long offset, boolean value) { Jedis j = getShard(key); return j.setbit(key, offset, value); } - public boolean getbit(String key, long offset) { + public Boolean getbit(String key, long offset) { Jedis j = getShard(key); return j.getbit(key, offset); } - public long setrange(String key, long offset, String value) { + public Long setrange(String key, long offset, String value) { Jedis j = getShard(key); return j.setrange(key, offset, value); } diff --git a/src/test/java/redis/clients/jedis/tests/commands/BitCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/BitCommandsTest.java index 204caa8..047ca20 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/BitCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/BitCommandsTest.java @@ -11,11 +11,11 @@ public class BitCommandsTest extends JedisCommandTestBase { bit = jedis.getbit("foo", 0); assertEquals(true, bit); - long bbit = jedis.setbit("bfoo".getBytes(), 0, "1".getBytes()); - assertEquals(0, bbit); + boolean bbit = jedis.setbit("bfoo".getBytes(), 0, "1".getBytes()); + assertFalse(bbit); bbit = jedis.getbit("bfoo".getBytes(), 0); - assertEquals(1, bbit); + assertTrue(bbit); } @Test