diff --git a/redisson/src/main/java/org/redisson/RedissonFuction.java b/redisson/src/main/java/org/redisson/RedissonFuction.java index 08682cf02..ee5e89efc 100644 --- a/redisson/src/main/java/org/redisson/RedissonFuction.java +++ b/redisson/src/main/java/org/redisson/RedissonFuction.java @@ -15,10 +15,7 @@ */ package org.redisson; -import org.redisson.api.FunctionLibrary; -import org.redisson.api.FunctionStats; -import org.redisson.api.RFunction; -import org.redisson.api.RFuture; +import org.redisson.api.*; import org.redisson.client.codec.ByteArrayCodec; import org.redisson.client.codec.Codec; import org.redisson.client.codec.StringCodec; @@ -148,7 +145,7 @@ public class RedissonFuction implements RFunction { @Override public RFuture restoreAsync(byte[] payload) { - List> futures = commandExecutor.executeMasters(RedisCommands.FUNCTION_RESTORE, (Object) payload); + List> futures = commandExecutor.executeMasters(RedisCommands.FUNCTION_RESTORE, payload); CompletableFuture f = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])); return new CompletableFutureWrapper<>(f); } @@ -178,7 +175,7 @@ public class RedissonFuction implements RFunction { } private List encode(Collection values, Codec codec) { - List result = new ArrayList(values.size()); + List result = new ArrayList<>(values.size()); for (Object object : values) { result.add(commandExecutor.encode(codec, object)); } @@ -186,17 +183,17 @@ public class RedissonFuction implements RFunction { } @Override - public R call(String key, Mode mode, String name, ReturnType returnType, List keys, Object... values) { + public R call(String key, FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values) { return commandExecutor.get(callAsync(key, mode, name, returnType, keys, values)); } @Override - public R call(Mode mode, String name, ReturnType returnType, List keys, Object... values) { + public R call(FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values) { return commandExecutor.get(callAsync(mode, name, returnType, keys, values)); } @Override - public R call(Mode mode, String name, ReturnType returnType) { + public R call(FunctionMode mode, String name, FunctionResult returnType) { return commandExecutor.get(callAsync(mode, name, returnType)); } @@ -211,7 +208,7 @@ public class RedissonFuction implements RFunction { } @Override - public RFuture callAsync(String key, Mode mode, String name, ReturnType returnType, List keys, Object... values) { + public RFuture callAsync(String key, FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values) { List args = new ArrayList<>(); args.add(name); args.add(keys.size()); @@ -219,19 +216,19 @@ public class RedissonFuction implements RFunction { args.add(keys); } args.addAll(encode(Arrays.asList(values), codec)); - if (mode == Mode.READ) { + if (mode == FunctionMode.READ) { return commandExecutor.readAsync(key, codec, returnType.getCommand(), args.toArray()); } return commandExecutor.writeAsync(key, codec, returnType.getCommand(), args.toArray()); } @Override - public RFuture callAsync(Mode mode, String name, ReturnType returnType, List keys, Object... values) { + public RFuture callAsync(FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values) { return callAsync(null, mode, name, returnType, keys, values); } @Override - public RFuture callAsync(Mode mode, String name, ReturnType returnType) { + public RFuture callAsync(FunctionMode mode, String name, FunctionResult returnType) { return callAsync(mode, name, returnType, Collections.emptyList()); } diff --git a/redisson/src/main/java/org/redisson/api/FunctionMode.java b/redisson/src/main/java/org/redisson/api/FunctionMode.java new file mode 100644 index 000000000..2cc35be5e --- /dev/null +++ b/redisson/src/main/java/org/redisson/api/FunctionMode.java @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2013-2021 Nikita Koksharov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.redisson.api; + +/** + * Function execution mode. + * + * @author Nikita Koksharov + * + */ +public enum FunctionMode { + + /** + * Execute function as read operation + */ + READ, + + /** + * Execute function as read operation + */ + WRITE + +} diff --git a/redisson/src/main/java/org/redisson/api/FunctionResult.java b/redisson/src/main/java/org/redisson/api/FunctionResult.java new file mode 100644 index 000000000..60ead5da6 --- /dev/null +++ b/redisson/src/main/java/org/redisson/api/FunctionResult.java @@ -0,0 +1,77 @@ +/** + * Copyright (c) 2013-2021 Nikita Koksharov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.redisson.api; + +import org.redisson.client.protocol.RedisCommand; +import org.redisson.client.protocol.RedisCommands; + +/** + * Function result type. + * + * @author Nikita Koksharov + * + */ +public enum FunctionResult { + + /** + * Result is a value of Boolean type + */ + BOOLEAN(RedisCommands.FCALL_BOOLEAN_SAFE), + + /** + * Result is a value of Long type + */ + LONG(RedisCommands.FCALL_LONG), + + /** + * Result is a value of List type + */ + LIST(RedisCommands.FCALL_LIST), + + /** + * Result is a value of plain String type + */ + STRING(RedisCommands.FCALL_STRING), + + /** + * Result is a value of user defined type + */ + VALUE(RedisCommands.FCALL_OBJECT), + + /** + * Result is a value of Map Value type. Codec.getMapValueDecoder() and Codec.getMapValueEncoder() + * methods are used for data deserialization or serialization. + */ + MAPVALUE(RedisCommands.FCALL_MAP_VALUE), + + /** + * Result is a value of List type, which consists of objects of Map Value type. + * Codec.getMapValueDecoder() and Codec.getMapValueEncoder() + * methods are used for data deserialization or serialization. + */ + MAPVALUELIST(RedisCommands.FCALL_MAP_VALUE_LIST); + + private final RedisCommand command; + + FunctionResult(RedisCommand command) { + this.command = command; + } + + public RedisCommand getCommand() { + return command; + } + +} diff --git a/redisson/src/main/java/org/redisson/api/RFunction.java b/redisson/src/main/java/org/redisson/api/RFunction.java index c965fd55c..e46a69017 100644 --- a/redisson/src/main/java/org/redisson/api/RFunction.java +++ b/redisson/src/main/java/org/redisson/api/RFunction.java @@ -15,9 +15,6 @@ */ package org.redisson.api; -import org.redisson.client.protocol.RedisCommand; -import org.redisson.client.protocol.RedisCommands; - import java.util.List; /** @@ -28,69 +25,6 @@ import java.util.List; */ public interface RFunction extends RFunctionAsync { - enum Mode { - /** - * Execute function as read operation - */ - READ, - - /** - * Execute function as read operation - */ - WRITE - } - - enum ReturnType { - /** - * Result is a value of Boolean type - */ - BOOLEAN(RedisCommands.FCALL_BOOLEAN_SAFE), - - /** - * Result is a value of Long type - */ - LONG(RedisCommands.FCALL_LONG), - - /** - * Result is a value of List type - */ - LIST(RedisCommands.FCALL_LIST), - - /** - * Result is a value of plain String type - */ - STRING(RedisCommands.FCALL_STRING), - - /** - * Result is a value of user defined type - */ - VALUE(RedisCommands.FCALL_OBJECT), - - /** - * Result is a value of Map Value type. Codec.getMapValueDecoder() and Codec.getMapValueEncoder() - * methods are used for data deserialization or serialization. - */ - MAPVALUE(RedisCommands.FCALL_MAP_VALUE), - - /** - * Result is a value of List type, which consists of objects of Map Value type. - * Codec.getMapValueDecoder() and Codec.getMapValueEncoder() - * methods are used for data deserialization or serialization. - */ - MAPVALUELIST(RedisCommands.FCALL_MAP_VALUE_LIST); - - private final RedisCommand command; - - ReturnType(RedisCommand command) { - this.command = command; - } - - public RedisCommand getCommand() { - return command; - } - - }; - /** * Deletes library. Error is thrown if library doesn't exist. * @@ -198,7 +132,7 @@ public interface RFunction extends RFunctionAsync { * @param values - values available through VALUES param in script * @return result object */ - R call(String key, Mode mode, String name, ReturnType returnType, List keys, Object... values); + R call(String key, FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values); /** * Executes function @@ -211,7 +145,7 @@ public interface RFunction extends RFunctionAsync { * @param values - values available through VALUES param in script * @return result object */ - R call(Mode mode, String name, ReturnType returnType, List keys, Object... values); + R call(FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values); /** * Executes function @@ -222,6 +156,6 @@ public interface RFunction extends RFunctionAsync { * @param returnType - return type * @return result object */ - R call(Mode mode, String name, ReturnType returnType); + R call(FunctionMode mode, String name, FunctionResult returnType); } diff --git a/redisson/src/main/java/org/redisson/api/RFunctionAsync.java b/redisson/src/main/java/org/redisson/api/RFunctionAsync.java index 1c18b2a9c..75b498e43 100644 --- a/redisson/src/main/java/org/redisson/api/RFunctionAsync.java +++ b/redisson/src/main/java/org/redisson/api/RFunctionAsync.java @@ -15,9 +15,6 @@ */ package org.redisson.api; -import org.redisson.api.RFunction.Mode; -import org.redisson.api.RFunction.ReturnType; - import java.util.List; /** @@ -135,7 +132,7 @@ public interface RFunctionAsync { * @param values - values available through VALUES param in script * @return result object */ - RFuture callAsync(String key, Mode mode, String name, ReturnType returnType, List keys, Object... values); + RFuture callAsync(String key, FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values); /** * Executes function @@ -148,7 +145,7 @@ public interface RFunctionAsync { * @param values - values available through VALUES param in script * @return result object */ - RFuture callAsync(Mode mode, String name, RFunction.ReturnType returnType, List keys, Object... values); + RFuture callAsync(FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values); /** * Executes function @@ -159,6 +156,6 @@ public interface RFunctionAsync { * @param returnType - return type * @return result object */ - RFuture callAsync(Mode mode, String name, ReturnType returnType); + RFuture callAsync(FunctionMode mode, String name, FunctionResult returnType); } diff --git a/redisson/src/main/java/org/redisson/api/RFunctionReactive.java b/redisson/src/main/java/org/redisson/api/RFunctionReactive.java index 1810563c3..fbe6911aa 100644 --- a/redisson/src/main/java/org/redisson/api/RFunctionReactive.java +++ b/redisson/src/main/java/org/redisson/api/RFunctionReactive.java @@ -134,7 +134,7 @@ public interface RFunctionReactive { * @param values - values available through VALUES param in script * @return result object */ - Mono call(String key, RFunction.Mode mode, String name, RFunction.ReturnType returnType, List keys, Object... values); + Mono call(String key, FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values); /** * Executes function @@ -147,7 +147,7 @@ public interface RFunctionReactive { * @param values - values available through VALUES param in script * @return result object */ - Mono call(RFunction.Mode mode, String name, RFunction.ReturnType returnType, List keys, Object... values); + Mono call(FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values); /** * Executes function @@ -158,6 +158,6 @@ public interface RFunctionReactive { * @param returnType - return type * @return result object */ - Mono call(RFunction.Mode mode, String name, RFunction.ReturnType returnType); + Mono call(FunctionMode mode, String name, FunctionResult returnType); } diff --git a/redisson/src/main/java/org/redisson/api/RFunctionRx.java b/redisson/src/main/java/org/redisson/api/RFunctionRx.java index 1ed5c4493..82a92ffbe 100644 --- a/redisson/src/main/java/org/redisson/api/RFunctionRx.java +++ b/redisson/src/main/java/org/redisson/api/RFunctionRx.java @@ -136,7 +136,7 @@ public interface RFunctionRx { * @param values - values available through VALUES param in script * @return result object */ - Maybe call(String key, RFunction.Mode mode, String name, RFunction.ReturnType returnType, List keys, Object... values); + Maybe call(String key, FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values); /** * Executes function @@ -149,7 +149,7 @@ public interface RFunctionRx { * @param values - values available through VALUES param in script * @return result object */ - Maybe call(RFunction.Mode mode, String name, RFunction.ReturnType returnType, List keys, Object... values); + Maybe call(FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values); /** * Executes function @@ -160,6 +160,6 @@ public interface RFunctionRx { * @param returnType - return type * @return result object */ - Maybe call(RFunction.Mode mode, String name, RFunction.ReturnType returnType); + Maybe call(FunctionMode mode, String name, FunctionResult returnType); } diff --git a/redisson/src/test/java/org/redisson/RedissonFunctionTest.java b/redisson/src/test/java/org/redisson/RedissonFunctionTest.java index 693f726c2..8a591bf91 100644 --- a/redisson/src/test/java/org/redisson/RedissonFunctionTest.java +++ b/redisson/src/test/java/org/redisson/RedissonFunctionTest.java @@ -1,9 +1,7 @@ package org.redisson; import org.junit.jupiter.api.Test; -import org.redisson.api.FunctionLibrary; -import org.redisson.api.FunctionStats; -import org.redisson.api.RFunction; +import org.redisson.api.*; import org.redisson.client.codec.LongCodec; import org.redisson.client.codec.StringCodec; @@ -30,7 +28,7 @@ public class RedissonFunctionTest extends BaseTest { f.load("lib", "redis.register_function('myfun', function(keys, args) for i = 1, 8829381983, 1 do end return args[1] end)" + "redis.register_function('myfun2', function(keys, args) return 'test' end)" + "redis.register_function('myfun3', function(keys, args) return 123 end)"); - f.callAsync(RFunction.Mode.READ, "myfun", RFunction.ReturnType.VALUE, Collections.emptyList(), "test"); + f.callAsync(FunctionMode.READ, "myfun", FunctionResult.VALUE, Collections.emptyList(), "test"); FunctionStats stats = f.stats(); FunctionStats.RunningFunction func = stats.getRunningFunction(); assertThat(func.getName()).isEqualTo("myfun"); @@ -50,15 +48,15 @@ public class RedissonFunctionTest extends BaseTest { f.load("lib", "redis.register_function('myfun', function(keys, args) return args[1] end)" + "redis.register_function('myfun2', function(keys, args) return 'test' end)" + "redis.register_function('myfun3', function(keys, args) return 123 end)"); - String s = f.call(RFunction.Mode.READ, "myfun", RFunction.ReturnType.VALUE, Collections.emptyList(), "test"); + String s = f.call(FunctionMode.READ, "myfun", FunctionResult.VALUE, Collections.emptyList(), "test"); assertThat(s).isEqualTo("test"); RFunction f2 = redisson.getFunction(StringCodec.INSTANCE); - String s2 = f2.call(RFunction.Mode.READ, "myfun2", RFunction.ReturnType.STRING, Collections.emptyList()); + String s2 = f2.call(FunctionMode.READ, "myfun2", FunctionResult.STRING, Collections.emptyList()); assertThat(s2).isEqualTo("test"); RFunction f3 = redisson.getFunction(LongCodec.INSTANCE); - Long s3 = f3.call(RFunction.Mode.READ, "myfun3", RFunction.ReturnType.LONG, Collections.emptyList()); + Long s3 = f3.call(FunctionMode.READ, "myfun3", FunctionResult.LONG, Collections.emptyList()); assertThat(s3).isEqualTo(123L); }