diff --git a/src/main/java/redis/clients/jedis/BinaryClient.java b/src/main/java/redis/clients/jedis/BinaryClient.java index f8f20c5a89..692e18debf 100644 --- a/src/main/java/redis/clients/jedis/BinaryClient.java +++ b/src/main/java/redis/clients/jedis/BinaryClient.java @@ -24,6 +24,7 @@ import javax.net.ssl.SSLSocketFactory; import redis.clients.jedis.Protocol.Keyword; +import redis.clients.jedis.args.FlushMode; import redis.clients.jedis.args.UnblockType; import redis.clients.jedis.params.*; import redis.clients.jedis.util.SafeEncoder; @@ -217,6 +218,10 @@ public void flushDB() { sendCommand(FLUSHDB); } + public void flushDB(FlushMode flushMode) { + sendCommand(FLUSHDB, flushMode.getRaw()); + } + public void keys(final byte[] pattern) { sendCommand(KEYS, pattern); } @@ -277,6 +282,10 @@ public void flushAll() { sendCommand(FLUSHALL); } + public void flushAll(FlushMode flushMode) { + sendCommand(FLUSHALL, flushMode.getRaw()); + } + public void getSet(final byte[] key, final byte[] value) { sendCommand(GETSET, key, value); } @@ -1098,6 +1107,10 @@ public void scriptFlush() { sendCommand(SCRIPT, Keyword.FLUSH.getRaw()); } + public void scriptFlush(FlushMode flushMode) { + sendCommand(SCRIPT, Keyword.FLUSH.getRaw(), flushMode.getRaw()); + } + public void scriptExists(final byte[]... sha1) { sendCommand(SCRIPT, joinParameters(Keyword.EXISTS.getRaw(), sha1)); } diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index f5e4a93140..2940bb9bfe 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -21,6 +21,7 @@ import javax.net.ssl.SSLParameters; import javax.net.ssl.SSLSocketFactory; +import redis.clients.jedis.args.FlushMode; import redis.clients.jedis.args.UnblockType; import redis.clients.jedis.commands.AdvancedBinaryJedisCommands; import redis.clients.jedis.commands.BasicCommands; @@ -529,6 +530,18 @@ public String flushDB() { return client.getStatusCodeReply(); } + /** + * Delete all the keys of the currently selected DB. This command never fails. + * @param flushMode + * @return Status code reply + */ + @Override + public String flushDB(FlushMode flushMode) { + checkIsInMultiOrPipeline(); + client.flushDB(flushMode); + return client.getStatusCodeReply(); + } + /** * Returns all the keys matching the glob-style pattern as space separated strings. For example if * you have in the database the keys "foo" and "foobar" the command "KEYS foo*" will return @@ -766,6 +779,19 @@ public String flushAll() { return client.getStatusCodeReply(); } + /** + * Delete all the keys of all the existing databases, not just the currently selected one. This + * command never fails. + * @param flushMode + * @return Status code reply + */ + @Override + public String flushAll(FlushMode flushMode) { + checkIsInMultiOrPipeline(); + client.flushAll(flushMode); + return client.getStatusCodeReply(); + } + /** * GETSET is an atomic set this value and return the old value command. Set key to the string * value and return the old value stored at key. The string can't be longer than 1073741824 bytes @@ -3747,6 +3773,12 @@ public String scriptFlush() { return client.getStatusCodeReply(); } + @Override + public String scriptFlush(final FlushMode flushMode) { + client.scriptFlush(flushMode); + return client.getStatusCodeReply(); + } + public Long scriptExists(final byte[] sha1) { byte[][] a = new byte[1][]; a[0] = sha1; diff --git a/src/main/java/redis/clients/jedis/BinaryJedisCluster.java b/src/main/java/redis/clients/jedis/BinaryJedisCluster.java index b33ea9abcf..df86f83c35 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedisCluster.java +++ b/src/main/java/redis/clients/jedis/BinaryJedisCluster.java @@ -1,5 +1,6 @@ package redis.clients.jedis; +import redis.clients.jedis.args.FlushMode; import redis.clients.jedis.commands.BinaryJedisClusterCommands; import redis.clients.jedis.commands.JedisClusterBinaryScriptingCommands; import redis.clients.jedis.commands.MultiKeyBinaryJedisClusterCommands; @@ -1710,6 +1711,16 @@ public String execute(Jedis connection) { }.runBinary(sampleKey); } + @Override + public String scriptFlush(final byte[] sampleKey, final FlushMode flushMode) { + return new JedisClusterCommand(connectionHandler, maxAttempts) { + @Override + public String execute(Jedis connection) { + return connection.scriptFlush(flushMode); + } + }.runBinary(sampleKey); + } + @Override public String scriptKill(final byte[] sampleKey) { return new JedisClusterCommand(connectionHandler, maxAttempts) { diff --git a/src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java b/src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java index da762f6dbd..8e1b1b9811 100644 --- a/src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java +++ b/src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java @@ -1,5 +1,6 @@ package redis.clients.jedis; +import redis.clients.jedis.args.FlushMode; import redis.clients.jedis.commands.*; import redis.clients.jedis.params.*; @@ -499,6 +500,18 @@ public Response flushAll() { return getResponse(BuilderFactory.STRING); } + @Override + public Response flushDB(FlushMode flushMode) { + client.flushDB(flushMode); + return getResponse(BuilderFactory.STRING); + } + + @Override + public Response flushAll(FlushMode flushMode) { + client.flushAll(flushMode); + return getResponse(BuilderFactory.STRING); + } + @Override public Response info() { client.info(); diff --git a/src/main/java/redis/clients/jedis/args/FlushMode.java b/src/main/java/redis/clients/jedis/args/FlushMode.java new file mode 100644 index 0000000000..4be513066f --- /dev/null +++ b/src/main/java/redis/clients/jedis/args/FlushMode.java @@ -0,0 +1,30 @@ +package redis.clients.jedis.args; + +import redis.clients.jedis.util.SafeEncoder; + +/** + * Enum object describing flushing mode. + */ +public enum FlushMode implements Rawable { + + /** + * flushes synchronously + */ + SYNC, + + /** + * flushes asynchronously + */ + ASYNC; + + private final byte[] raw; + + FlushMode() { + raw = SafeEncoder.encode(this.name()); + } + + @Override + public byte[] getRaw() { + return raw; + } +} diff --git a/src/main/java/redis/clients/jedis/commands/BasicCommands.java b/src/main/java/redis/clients/jedis/commands/BasicCommands.java index eb87ce92fc..81deb1669c 100644 --- a/src/main/java/redis/clients/jedis/commands/BasicCommands.java +++ b/src/main/java/redis/clients/jedis/commands/BasicCommands.java @@ -1,6 +1,7 @@ package redis.clients.jedis.commands; import redis.clients.jedis.DebugParams; +import redis.clients.jedis.args.FlushMode; public interface BasicCommands { @@ -24,6 +25,14 @@ public interface BasicCommands { */ String flushDB(); + /** + * Delete all the keys of the currently selected DB. This command never fails. The time-complexity + * for this operation is O(N), N being the number of keys in the database. + * @param flushMode + * @return OK + */ + String flushDB(FlushMode flushMode); + /** * Return the number of keys in the currently-selected database. * @return the number of key in the currently-selected database. @@ -52,6 +61,13 @@ public interface BasicCommands { */ String flushAll(); + /** + * Delete all the keys of all the existing databases, not just the currently selected one. + * @param flushMode + * @return a simple string reply (OK) + */ + String flushAll(FlushMode flushMode); + /** * Request for authentication in a password-protected Redis server. Redis can be instructed to * require a password before allowing clients to execute commands. This is done using the diff --git a/src/main/java/redis/clients/jedis/commands/BasicRedisPipeline.java b/src/main/java/redis/clients/jedis/commands/BasicRedisPipeline.java index d030c655e6..ea397949fd 100644 --- a/src/main/java/redis/clients/jedis/commands/BasicRedisPipeline.java +++ b/src/main/java/redis/clients/jedis/commands/BasicRedisPipeline.java @@ -2,6 +2,7 @@ import redis.clients.jedis.Module; import redis.clients.jedis.Response; +import redis.clients.jedis.args.FlushMode; import java.util.List; @@ -26,8 +27,12 @@ public interface BasicRedisPipeline { Response flushDB(); + Response flushDB(FlushMode flushMode); + Response flushAll(); + Response flushAll(FlushMode flushMode); + Response info(); Response> time(); diff --git a/src/main/java/redis/clients/jedis/commands/BinaryScriptingCommands.java b/src/main/java/redis/clients/jedis/commands/BinaryScriptingCommands.java index 51af0aaba2..bfa7e6637a 100644 --- a/src/main/java/redis/clients/jedis/commands/BinaryScriptingCommands.java +++ b/src/main/java/redis/clients/jedis/commands/BinaryScriptingCommands.java @@ -1,5 +1,7 @@ package redis.clients.jedis.commands; +import redis.clients.jedis.args.FlushMode; + import java.util.List; public interface BinaryScriptingCommands { @@ -25,5 +27,7 @@ public interface BinaryScriptingCommands { String scriptFlush(); + String scriptFlush(FlushMode flushMode); + String scriptKill(); } diff --git a/src/main/java/redis/clients/jedis/commands/JedisClusterBinaryScriptingCommands.java b/src/main/java/redis/clients/jedis/commands/JedisClusterBinaryScriptingCommands.java index d9aa6b9694..e93c46f1a5 100644 --- a/src/main/java/redis/clients/jedis/commands/JedisClusterBinaryScriptingCommands.java +++ b/src/main/java/redis/clients/jedis/commands/JedisClusterBinaryScriptingCommands.java @@ -1,5 +1,7 @@ package redis.clients.jedis.commands; +import redis.clients.jedis.args.FlushMode; + import java.util.List; public interface JedisClusterBinaryScriptingCommands { @@ -52,6 +54,14 @@ public interface JedisClusterBinaryScriptingCommands { */ String scriptFlush(byte[] sampleKey); + /** + * @param sampleKey Command will be executed in the node where the hash slot of this key is + * assigned to + * @param flushMode + * @return + */ + String scriptFlush(byte[] sampleKey, FlushMode flushMode); + /** * @param sampleKey Command will be executed in the node where the hash slot of this key is * assigned to diff --git a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java index 4060893ccc..f082d4cb70 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java @@ -35,6 +35,7 @@ import redis.clients.jedis.ScanParams; import redis.clients.jedis.ScanResult; import redis.clients.jedis.StreamEntryID; +import redis.clients.jedis.args.FlushMode; import redis.clients.jedis.util.SafeEncoder; import redis.clients.jedis.exceptions.JedisDataException; @@ -555,7 +556,8 @@ public void flushDB() { assertEquals(0, jedis.dbSize().intValue()); jedis.select(1); assertEquals(1, jedis.dbSize().intValue()); - jedis.del("bar"); + assertEquals("OK", jedis.flushDB(FlushMode.SYNC)); + assertEquals(0, jedis.dbSize().intValue()); // Binary jedis.select(0); @@ -568,7 +570,8 @@ public void flushDB() { assertEquals(0, jedis.dbSize().intValue()); jedis.select(1); assertEquals(1, jedis.dbSize().intValue()); - + assertEquals("OK", jedis.flushDB(FlushMode.ASYNC)); + assertEquals(0, jedis.dbSize().intValue()); } @Test @@ -582,6 +585,10 @@ public void flushAll() { assertEquals(0, jedis.dbSize().intValue()); jedis.select(1); assertEquals(0, jedis.dbSize().intValue()); + jedis.set("foo", "bar"); + assertEquals(1, jedis.dbSize().intValue()); + assertEquals("OK", jedis.flushAll(FlushMode.SYNC)); + assertEquals(0, jedis.dbSize().intValue()); // Binary jedis.select(0); @@ -594,7 +601,10 @@ public void flushAll() { assertEquals(0, jedis.dbSize().intValue()); jedis.select(1); assertEquals(0, jedis.dbSize().intValue()); - + jedis.set(bfoo, bbar); + assertEquals(1, jedis.dbSize().intValue()); + assertEquals("OK", jedis.flushAll(FlushMode.ASYNC)); + assertEquals(0, jedis.dbSize().intValue()); } @Test diff --git a/src/test/java/redis/clients/jedis/tests/commands/ClusterScriptingCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ClusterScriptingCommandsTest.java index 1b7653c8cf..47c293e34a 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ClusterScriptingCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ClusterScriptingCommandsTest.java @@ -7,6 +7,7 @@ import java.util.List; import org.junit.Test; +import redis.clients.jedis.args.FlushMode; import redis.clients.jedis.exceptions.JedisClusterOperationException; import redis.clients.jedis.exceptions.JedisDataException; @@ -73,6 +74,7 @@ public void testBinaryScriptFlush() { byte[] byteKey = "key1".getBytes(); jedisCluster.scriptLoad("return redis.call('get','foo')".getBytes(), byteKey); assertEquals("OK", jedisCluster.scriptFlush(byteKey)); + assertEquals("OK", jedisCluster.scriptFlush(byteKey, FlushMode.SYNC)); } @Test(expected = JedisDataException.class) diff --git a/src/test/java/redis/clients/jedis/tests/commands/ScriptingCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ScriptingCommandsTest.java index 5bf28431c9..cba8cb9da7 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ScriptingCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ScriptingCommandsTest.java @@ -17,6 +17,7 @@ import redis.clients.jedis.BinaryJedis; import redis.clients.jedis.Jedis; +import redis.clients.jedis.args.FlushMode; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.exceptions.JedisNoScriptException; @@ -152,6 +153,16 @@ public void scriptFlush() { assertFalse(jedis.scriptExists("6b1bf486c81ceb7edf3c093f4c48582e38c0e791")); } + @Test + public void scriptFlushMode() { + jedis.set("foo", "bar"); + jedis.eval("return redis.call('get','foo')"); + String sha1 = "6b1bf486c81ceb7edf3c093f4c48582e38c0e791"; + assertTrue(jedis.scriptExists(sha1)); + jedis.scriptFlush(FlushMode.SYNC); + assertFalse(jedis.scriptExists(sha1)); + } + @Test public void scriptExists() { jedis.scriptLoad("return redis.call('get','foo')");