refactoring

pull/4226/head
Nikita Koksharov 3 years ago
parent acf9050965
commit b1a8ac3bf8

@ -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<Void> restoreAsync(byte[] payload) {
List<CompletableFuture<Void>> futures = commandExecutor.executeMasters(RedisCommands.FUNCTION_RESTORE, (Object) payload);
List<CompletableFuture<Void>> futures = commandExecutor.executeMasters(RedisCommands.FUNCTION_RESTORE, payload);
CompletableFuture<Void> f = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
return new CompletableFutureWrapper<>(f);
}
@ -178,7 +175,7 @@ public class RedissonFuction implements RFunction {
}
private List<Object> encode(Collection<?> values, Codec codec) {
List<Object> result = new ArrayList<Object>(values.size());
List<Object> 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> R call(String key, Mode mode, String name, ReturnType returnType, List<Object> keys, Object... values) {
public <R> R call(String key, FunctionMode mode, String name, FunctionResult returnType, List<Object> keys, Object... values) {
return commandExecutor.get(callAsync(key, mode, name, returnType, keys, values));
}
@Override
public <R> R call(Mode mode, String name, ReturnType returnType, List<Object> keys, Object... values) {
public <R> R call(FunctionMode mode, String name, FunctionResult returnType, List<Object> keys, Object... values) {
return commandExecutor.get(callAsync(mode, name, returnType, keys, values));
}
@Override
public <R> R call(Mode mode, String name, ReturnType returnType) {
public <R> 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 <R> RFuture<R> callAsync(String key, Mode mode, String name, ReturnType returnType, List<Object> keys, Object... values) {
public <R> RFuture<R> callAsync(String key, FunctionMode mode, String name, FunctionResult returnType, List<Object> keys, Object... values) {
List<Object> 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 <R> RFuture<R> callAsync(Mode mode, String name, ReturnType returnType, List<Object> keys, Object... values) {
public <R> RFuture<R> callAsync(FunctionMode mode, String name, FunctionResult returnType, List<Object> keys, Object... values) {
return callAsync(null, mode, name, returnType, keys, values);
}
@Override
public <R> RFuture<R> callAsync(Mode mode, String name, ReturnType returnType) {
public <R> RFuture<R> callAsync(FunctionMode mode, String name, FunctionResult returnType) {
return callAsync(mode, name, returnType, Collections.emptyList());
}

@ -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
}

@ -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;
}
}

@ -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> R call(String key, Mode mode, String name, ReturnType returnType, List<Object> keys, Object... values);
<R> R call(String key, FunctionMode mode, String name, FunctionResult returnType, List<Object> 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> R call(Mode mode, String name, ReturnType returnType, List<Object> keys, Object... values);
<R> R call(FunctionMode mode, String name, FunctionResult returnType, List<Object> keys, Object... values);
/**
* Executes function
@ -222,6 +156,6 @@ public interface RFunction extends RFunctionAsync {
* @param returnType - return type
* @return result object
*/
<R> R call(Mode mode, String name, ReturnType returnType);
<R> R call(FunctionMode mode, String name, FunctionResult returnType);
}

@ -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
*/
<R> RFuture<R> callAsync(String key, Mode mode, String name, ReturnType returnType, List<Object> keys, Object... values);
<R> RFuture<R> callAsync(String key, FunctionMode mode, String name, FunctionResult returnType, List<Object> keys, Object... values);
/**
* Executes function
@ -148,7 +145,7 @@ public interface RFunctionAsync {
* @param values - values available through VALUES param in script
* @return result object
*/
<R> RFuture<R> callAsync(Mode mode, String name, RFunction.ReturnType returnType, List<Object> keys, Object... values);
<R> RFuture<R> callAsync(FunctionMode mode, String name, FunctionResult returnType, List<Object> keys, Object... values);
/**
* Executes function
@ -159,6 +156,6 @@ public interface RFunctionAsync {
* @param returnType - return type
* @return result object
*/
<R> RFuture<R> callAsync(Mode mode, String name, ReturnType returnType);
<R> RFuture<R> callAsync(FunctionMode mode, String name, FunctionResult returnType);
}

@ -134,7 +134,7 @@ public interface RFunctionReactive {
* @param values - values available through VALUES param in script
* @return result object
*/
<R> Mono<R> call(String key, RFunction.Mode mode, String name, RFunction.ReturnType returnType, List<Object> keys, Object... values);
<R> Mono<R> call(String key, FunctionMode mode, String name, FunctionResult returnType, List<Object> keys, Object... values);
/**
* Executes function
@ -147,7 +147,7 @@ public interface RFunctionReactive {
* @param values - values available through VALUES param in script
* @return result object
*/
<R> Mono<R> call(RFunction.Mode mode, String name, RFunction.ReturnType returnType, List<Object> keys, Object... values);
<R> Mono<R> call(FunctionMode mode, String name, FunctionResult returnType, List<Object> keys, Object... values);
/**
* Executes function
@ -158,6 +158,6 @@ public interface RFunctionReactive {
* @param returnType - return type
* @return result object
*/
<R> Mono<R> call(RFunction.Mode mode, String name, RFunction.ReturnType returnType);
<R> Mono<R> call(FunctionMode mode, String name, FunctionResult returnType);
}

@ -136,7 +136,7 @@ public interface RFunctionRx {
* @param values - values available through VALUES param in script
* @return result object
*/
<R> Maybe<R> call(String key, RFunction.Mode mode, String name, RFunction.ReturnType returnType, List<Object> keys, Object... values);
<R> Maybe<R> call(String key, FunctionMode mode, String name, FunctionResult returnType, List<Object> keys, Object... values);
/**
* Executes function
@ -149,7 +149,7 @@ public interface RFunctionRx {
* @param values - values available through VALUES param in script
* @return result object
*/
<R> Maybe<R> call(RFunction.Mode mode, String name, RFunction.ReturnType returnType, List<Object> keys, Object... values);
<R> Maybe<R> call(FunctionMode mode, String name, FunctionResult returnType, List<Object> keys, Object... values);
/**
* Executes function
@ -160,6 +160,6 @@ public interface RFunctionRx {
* @param returnType - return type
* @return result object
*/
<R> Maybe<R> call(RFunction.Mode mode, String name, RFunction.ReturnType returnType);
<R> Maybe<R> call(FunctionMode mode, String name, FunctionResult returnType);
}

@ -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);
}

Loading…
Cancel
Save