From 0994aee746a2397f97a262cd6ff7cf2e6223254e Mon Sep 17 00:00:00 2001 From: Nikita Date: Wed, 19 Jul 2017 11:03:20 +0300 Subject: [PATCH] unlink, flashdb and flashall async commands support. #965 --- .../main/java/org/redisson/RedissonKeys.java | 40 ++++++++++++++++++- .../java/org/redisson/RedissonObject.java | 10 +++++ .../src/main/java/org/redisson/api/RKeys.java | 29 ++++++++++++++ .../java/org/redisson/api/RKeysAsync.java | 31 ++++++++++++++ .../main/java/org/redisson/api/RObject.java | 10 +++++ .../java/org/redisson/api/RObjectAsync.java | 10 +++++ .../client/protocol/RedisCommands.java | 6 +++ 7 files changed, 134 insertions(+), 2 deletions(-) diff --git a/redisson/src/main/java/org/redisson/RedissonKeys.java b/redisson/src/main/java/org/redisson/RedissonKeys.java index 776e9dee0..3040c3273 100644 --- a/redisson/src/main/java/org/redisson/RedissonKeys.java +++ b/redisson/src/main/java/org/redisson/RedissonKeys.java @@ -36,6 +36,7 @@ import org.redisson.client.RedisException; import org.redisson.client.codec.ScanCodec; import org.redisson.client.codec.StringCodec; import org.redisson.client.protocol.RedisCommands; +import org.redisson.client.protocol.RedisStrictCommand; import org.redisson.client.protocol.decoder.ListScanResult; import org.redisson.client.protocol.decoder.ScanObjectEntry; import org.redisson.command.CommandAsyncExecutor; @@ -277,10 +278,24 @@ public class RedissonKeys implements RKeys { return deleteAsync(keys.toArray(new String[keys.size()])); } + @Override + public long unlink(String ... keys) { + return commandExecutor.get(deleteAsync(keys)); + } + + @Override + public RFuture unlinkAsync(String ... keys) { + return executeAsync(RedisCommands.UNLINK, keys); + } + @Override public RFuture deleteAsync(String ... keys) { + return executeAsync(RedisCommands.DEL, keys); + } + + private RFuture executeAsync(RedisStrictCommand command, String ... keys) { if (!commandExecutor.getConnectionManager().isClusterMode()) { - return commandExecutor.writeAsync(null, RedisCommands.DEL, keys); + return commandExecutor.writeAsync(null, command, keys); } Map> range2key = new HashMap>(); @@ -320,7 +335,7 @@ public class RedissonKeys implements RKeys { // executes in batch due to CROSSLOT error CommandBatchService executorService = new CommandBatchService(commandExecutor.getConnectionManager()); for (String key : entry.getValue()) { - executorService.writeAsync(entry.getKey(), null, RedisCommands.DEL, key); + executorService.writeAsync(entry.getKey(), null, command, key); } RFuture> future = executorService.executeAsync(); @@ -351,6 +366,27 @@ public class RedissonKeys implements RKeys { }); } + @Override + public void flushdbParallel() { + commandExecutor.get(flushdbParallelAsync()); + } + + @Override + public RFuture flushdbParallelAsync() { + return commandExecutor.writeAllAsync(RedisCommands.FLUSHDB_ASYNC); + } + + @Override + public void flushallParallel() { + commandExecutor.get(flushallParallelAsync()); + } + + @Override + public RFuture flushallParallelAsync() { + return commandExecutor.writeAllAsync(RedisCommands.FLUSHALL_ASYNC); + } + + @Override public void flushdb() { commandExecutor.get(flushdbAsync()); diff --git a/redisson/src/main/java/org/redisson/RedissonObject.java b/redisson/src/main/java/org/redisson/RedissonObject.java index a1285a43b..e7d5f4ead 100644 --- a/redisson/src/main/java/org/redisson/RedissonObject.java +++ b/redisson/src/main/java/org/redisson/RedissonObject.java @@ -136,6 +136,16 @@ public abstract class RedissonObject implements RObject { public RFuture deleteAsync() { return commandExecutor.writeAsync(getName(), RedisCommands.DEL_BOOL, getName()); } + + @Override + public boolean unlink() { + return get(unlinkAsync()); + } + + @Override + public RFuture unlinkAsync() { + return commandExecutor.writeAsync(getName(), RedisCommands.UNLINK_BOOL, getName()); + } @Override public boolean touch() { diff --git a/redisson/src/main/java/org/redisson/api/RKeys.java b/redisson/src/main/java/org/redisson/api/RKeys.java index 7e039732f..f3445184b 100644 --- a/redisson/src/main/java/org/redisson/api/RKeys.java +++ b/redisson/src/main/java/org/redisson/api/RKeys.java @@ -230,6 +230,17 @@ public interface RKeys extends RKeysAsync { */ long delete(String ... keys); + /** + * Delete multiple objects by name. + * Actual removal will happen later asynchronously. + *

+ * Requires Redis 4.0+ + * + * @param keys + * @return number of removed keys + */ + long unlink(String ... keys); + /** * Returns the number of keys in the currently-selected database * @@ -242,9 +253,27 @@ public interface RKeys extends RKeysAsync { */ void flushdb(); + /** + * Delete all keys of currently selected database + * in background without blocking server. + *

+ * Requires Redis 4.0+ + * + */ + void flushdbParallel(); + /** * Delete all keys of all existing databases */ void flushall(); + + /** + * Delete all keys of all existing databases + * in background without blocking server. + *

+ * Requires Redis 4.0+ + * + */ + void flushallParallel(); } diff --git a/redisson/src/main/java/org/redisson/api/RKeysAsync.java b/redisson/src/main/java/org/redisson/api/RKeysAsync.java index 0f2e3cb7a..65343be43 100644 --- a/redisson/src/main/java/org/redisson/api/RKeysAsync.java +++ b/redisson/src/main/java/org/redisson/api/RKeysAsync.java @@ -188,6 +188,17 @@ public interface RKeysAsync { */ RFuture deleteAsync(String ... keys); + /** + * Delete multiple objects by name. + * Actual removal will happen later asynchronously. + *

+ * Requires Redis 4.0+ + * + * @param keys + * @return number of removed keys + */ + RFuture unlinkAsync(String ... keys); + /** * Returns the number of keys in the currently-selected database in async mode * @@ -207,4 +218,24 @@ public interface RKeysAsync { */ RFuture flushallAsync(); + /** + * Delete all keys of currently selected database + * in background without blocking server. + *

+ * Requires Redis 4.0+ + * + * @return void + */ + RFuture flushdbParallelAsync(); + + /** + * Delete all keys of all existing databases + * in background without blocking server. + *

+ * Requires Redis 4.0+ + * + * @return void + */ + RFuture flushallParallelAsync(); + } diff --git a/redisson/src/main/java/org/redisson/api/RObject.java b/redisson/src/main/java/org/redisson/api/RObject.java index cc0b021ba..e06bf5575 100644 --- a/redisson/src/main/java/org/redisson/api/RObject.java +++ b/redisson/src/main/java/org/redisson/api/RObject.java @@ -63,6 +63,16 @@ public interface RObject extends RObjectAsync { */ boolean delete(); + /** + * Delete the objects. + * Actual removal will happen later asynchronously. + *

+ * Requires Redis 4.0+ + * + * @return true if it was exist and deleted else false + */ + boolean unlink(); + /** * Rename current object key to newName * diff --git a/redisson/src/main/java/org/redisson/api/RObjectAsync.java b/redisson/src/main/java/org/redisson/api/RObjectAsync.java index 829034e5f..ef97a69da 100644 --- a/redisson/src/main/java/org/redisson/api/RObjectAsync.java +++ b/redisson/src/main/java/org/redisson/api/RObjectAsync.java @@ -56,6 +56,16 @@ public interface RObjectAsync { */ RFuture deleteAsync(); + /** + * Delete the objects. + * Actual removal will happen later asynchronously. + *

+ * Requires Redis 4.0+ + * + * @return true if it was exist and deleted else false + */ + RFuture unlinkAsync(); + /** * Rename current object key to newName * in async mode diff --git a/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java b/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java index 85c569237..083fe33e5 100644 --- a/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java +++ b/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java @@ -245,6 +245,9 @@ public interface RedisCommands { RedisStrictCommand CLIENT_GETNAME = new RedisStrictCommand("CLIENT", "GETNAME", new StringDataDecoder()); RedisStrictCommand FLUSHDB = new RedisStrictCommand("FLUSHDB", new VoidReplayConvertor()); RedisStrictCommand FLUSHALL = new RedisStrictCommand("FLUSHALL", new VoidReplayConvertor()); + + RedisStrictCommand FLUSHDB_ASYNC = new RedisStrictCommand("FLUSHDB", "ASYNC", new VoidReplayConvertor()); + RedisStrictCommand FLUSHALL_ASYNC = new RedisStrictCommand("FLUSHALL", "ASYNC", new VoidReplayConvertor()); RedisStrictCommand> KEYS = new RedisStrictCommand>("KEYS", new StringListReplayDecoder()); RedisCommand> MGET = new RedisCommand>("MGET", new ObjectListReplayDecoder()); @@ -272,6 +275,9 @@ public interface RedisCommands { RedisStrictCommand DEL_BOOL = new RedisStrictCommand("DEL", new BooleanNullSafeReplayConvertor()); RedisStrictCommand DEL_OBJECTS = new RedisStrictCommand("DEL", new BooleanNullSafeReplayConvertor()); RedisStrictCommand DEL_VOID = new RedisStrictCommand("DEL", new VoidReplayConvertor()); + + RedisStrictCommand UNLINK = new RedisStrictCommand("UNLINK"); + RedisStrictCommand UNLINK_BOOL = new RedisStrictCommand("UNLINK", new BooleanNullSafeReplayConvertor()); RedisCommand GET = new RedisCommand("GET"); RedisStrictCommand GET_LONG = new RedisStrictCommand("GET", new LongReplayConvertor());