|
|
|
@ -26,6 +26,7 @@ import org.redisson.misc.CompletableFutureWrapper;
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -264,4 +265,79 @@ public class RedissonScript implements RScript {
|
|
|
|
|
return commandExecutor.get(evalAsync(key, mode, luaScript, returnType, keys, values));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public <R> R eval(Mode mode, String luaScript, ReturnType returnType, Function<Collection<R>, R> resultMapper, Object... values) {
|
|
|
|
|
return commandExecutor.get(evalAsync(mode, luaScript, returnType, resultMapper, values));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public <R> RFuture<R> evalAsync(Mode mode, String luaScript, ReturnType returnType, Function<Collection<R>, R> resultMapper, Object... values) {
|
|
|
|
|
List<Object> args = new ArrayList<>();
|
|
|
|
|
args.add(luaScript);
|
|
|
|
|
args.add(0);
|
|
|
|
|
for (Object object : values) {
|
|
|
|
|
args.add(commandExecutor.encode(codec, object));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<CompletableFuture<R>> futures;
|
|
|
|
|
if (mode == Mode.READ_ONLY) {
|
|
|
|
|
futures = commandExecutor.readAllAsync(codec, returnType.getCommand(), args.toArray());
|
|
|
|
|
} else {
|
|
|
|
|
futures = commandExecutor.writeAllAsync(codec, returnType.getCommand(), args.toArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CompletableFuture<Void> r = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
|
|
|
|
|
CompletableFuture<R> res = r.thenApply(v -> {
|
|
|
|
|
List<R> l = futures.stream().map(f -> f.join()).collect(Collectors.toList());
|
|
|
|
|
return resultMapper.apply(l);
|
|
|
|
|
});
|
|
|
|
|
return new CompletableFutureWrapper<>(res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public <R> R evalSha(Mode mode, String shaDigest, ReturnType returnType, Function<Collection<R>, R> resultMapper, Object... values) {
|
|
|
|
|
return commandExecutor.get(evalShaAsync(mode, shaDigest, returnType, resultMapper, values));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public <R> RFuture<R> evalShaAsync(Mode mode, String shaDigest, ReturnType returnType, Function<Collection<R>, R> resultMapper, Object... values) {
|
|
|
|
|
List<Object> args = new ArrayList<>();
|
|
|
|
|
args.add(shaDigest);
|
|
|
|
|
args.add(0);
|
|
|
|
|
for (Object object : values) {
|
|
|
|
|
args.add(commandExecutor.encode(codec, object));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mode == Mode.READ_ONLY && commandExecutor.isEvalShaROSupported()) {
|
|
|
|
|
RedisCommand cmd = new RedisCommand(returnType.getCommand(), "EVALSHA_RO");
|
|
|
|
|
List<CompletableFuture<R>> futures = commandExecutor.readAllAsync(codec, cmd, args.toArray());
|
|
|
|
|
|
|
|
|
|
CompletableFuture<Void> r = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
|
|
|
|
|
CompletableFuture<R> rr = r.handle((res, e) -> {
|
|
|
|
|
if (e != null) {
|
|
|
|
|
if (e.getMessage().startsWith("ERR unknown command")) {
|
|
|
|
|
commandExecutor.setEvalShaROSupported(false);
|
|
|
|
|
return evalShaAsync(mode, shaDigest, returnType, resultMapper, values);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CompletableFuture<R> ex = new CompletableFuture<>();
|
|
|
|
|
ex.completeExceptionally(e);
|
|
|
|
|
return ex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<R> l = futures.stream().map(f -> f.join()).collect(Collectors.toList());
|
|
|
|
|
R result = resultMapper.apply(l);
|
|
|
|
|
return CompletableFuture.completedFuture(result);
|
|
|
|
|
}).thenCompose(ff -> ff);
|
|
|
|
|
return new CompletableFutureWrapper<>(rr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<CompletableFuture<R>> futures = commandExecutor.readAllAsync(codec, returnType.getCommand(), args.toArray());
|
|
|
|
|
CompletableFuture<Void> r = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
|
|
|
|
|
CompletableFuture<R> res = r.thenApply(v -> {
|
|
|
|
|
List<R> l = futures.stream().map(f -> f.join()).collect(Collectors.toList());
|
|
|
|
|
return resultMapper.apply(l);
|
|
|
|
|
});
|
|
|
|
|
return new CompletableFutureWrapper<>(res);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|