Skip to content

Commit

Permalink
add ScripArgOptions for the cluster client
Browse files Browse the repository at this point in the history
Signed-off-by: TJ Zhang <[email protected]>
  • Loading branch information
TJ Zhang committed Sep 12, 2024
1 parent 80b129f commit e91a553
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 239 deletions.
45 changes: 45 additions & 0 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
import glide.api.models.ClusterValue;
import glide.api.models.GlideString;
import glide.api.models.PubSubMessage;
import glide.api.models.Script;
import glide.api.models.commands.ExpireOptions;
import glide.api.models.commands.GetExOptions;
import glide.api.models.commands.LInsertOptions.InsertPosition;
Expand All @@ -223,6 +224,8 @@
import glide.api.models.commands.RangeOptions.ScoredRangeQuery;
import glide.api.models.commands.RestoreOptions;
import glide.api.models.commands.ScoreFilter;
import glide.api.models.commands.ScriptOptions;
import glide.api.models.commands.ScriptOptionsGlideString;
import glide.api.models.commands.SetOptions;
import glide.api.models.commands.WeightAggregateOptions.Aggregate;
import glide.api.models.commands.WeightAggregateOptions.KeyArray;
Expand Down Expand Up @@ -276,6 +279,7 @@
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -5018,4 +5022,45 @@ public CompletableFuture<Long> wait(long numreplicas, long timeout) {
new String[] {Long.toString(numreplicas), Long.toString(timeout)},
this::handleLongResponse);
}

@Override
public CompletableFuture<Object> invokeScript(@NonNull Script script) {
if (script.getBinaryOutput()) {
return commandManager.submitScript(
script, List.of(), List.of(), this::handleBinaryObjectOrNullResponse);
} else {
return commandManager.submitScript(
script, List.of(), List.of(), this::handleObjectOrNullResponse);
}
}

@Override
public CompletableFuture<Object> invokeScript(
@NonNull Script script, @NonNull ScriptOptions options) {
if (script.getBinaryOutput()) {
return commandManager.submitScript(
script,
options.getKeys().stream().map(GlideString::gs).collect(Collectors.toList()),
options.getArgs().stream().map(GlideString::gs).collect(Collectors.toList()),
this::handleBinaryObjectOrNullResponse);
} else {
return commandManager.submitScript(
script,
options.getKeys().stream().map(GlideString::gs).collect(Collectors.toList()),
options.getArgs().stream().map(GlideString::gs).collect(Collectors.toList()),
this::handleObjectOrNullResponse);
}
}

@Override
public CompletableFuture<Object> invokeScript(
@NonNull Script script, @NonNull ScriptOptionsGlideString options) {
if (script.getBinaryOutput()) {
return commandManager.submitScript(
script, options.getKeys(), options.getArgs(), this::handleBinaryObjectOrNullResponse);
} else {
return commandManager.submitScript(
script, options.getKeys(), options.getArgs(), this::handleObjectOrNullResponse);
}
}
}
46 changes: 0 additions & 46 deletions java/client/src/main/java/glide/api/GlideClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,18 @@
import glide.api.commands.ServerManagementCommands;
import glide.api.commands.TransactionsCommands;
import glide.api.models.GlideString;
import glide.api.models.Script;
import glide.api.models.Transaction;
import glide.api.models.commands.FlushMode;
import glide.api.models.commands.InfoOptions.Section;
import glide.api.models.commands.ScriptOptions;
import glide.api.models.commands.ScriptOptionsGlideString;
import glide.api.models.commands.SortOptions;
import glide.api.models.commands.SortOptionsBinary;
import glide.api.models.commands.function.FunctionRestorePolicy;
import glide.api.models.commands.scan.ScanOptions;
import glide.api.models.configuration.GlideClientConfiguration;
import glide.utils.ArgsBuilder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.NonNull;
import org.apache.commons.lang3.ArrayUtils;
Expand Down Expand Up @@ -577,47 +572,6 @@ public CompletableFuture<Object[]> scan(
return commandManager.submitNewCommand(Scan, arguments, this::handleArrayResponseBinary);
}

@Override
public CompletableFuture<Object> invokeScript(@NonNull Script script) {
if (script.getBinaryOutput()) {
return commandManager.submitScript(
script, List.of(), List.of(), this::handleBinaryObjectOrNullResponse);
} else {
return commandManager.submitScript(
script, List.of(), List.of(), this::handleObjectOrNullResponse);
}
}

@Override
public CompletableFuture<Object> invokeScript(
@NonNull Script script, @NonNull ScriptOptions options) {
if (script.getBinaryOutput()) {
return commandManager.submitScript(
script,
options.getKeys().stream().map(GlideString::gs).collect(Collectors.toList()),
options.getArgs().stream().map(GlideString::gs).collect(Collectors.toList()),
this::handleBinaryObjectOrNullResponse);
} else {
return commandManager.submitScript(
script,
options.getKeys().stream().map(GlideString::gs).collect(Collectors.toList()),
options.getArgs().stream().map(GlideString::gs).collect(Collectors.toList()),
this::handleObjectOrNullResponse);
}
}

@Override
public CompletableFuture<Object> invokeScript(
@NonNull Script script, @NonNull ScriptOptionsGlideString options) {
if (script.getBinaryOutput()) {
return commandManager.submitScript(
script, options.getKeys(), options.getArgs(), this::handleBinaryObjectOrNullResponse);
} else {
return commandManager.submitScript(
script, options.getKeys(), options.getArgs(), this::handleObjectOrNullResponse);
}
}

@Override
public CompletableFuture<Boolean[]> scriptExists(@NonNull String[] sha1s) {
return commandManager.submitNewCommand(
Expand Down
84 changes: 19 additions & 65 deletions java/client/src/main/java/glide/api/GlideClusterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
import glide.api.models.Script;
import glide.api.models.commands.FlushMode;
import glide.api.models.commands.InfoOptions.Section;
import glide.api.models.commands.ScriptOptions;
import glide.api.models.commands.ScriptOptionsGlideString;
import glide.api.models.commands.ScriptArgOptions;
import glide.api.models.commands.ScriptArgOptionsGlideString;
import glide.api.models.commands.SortClusterOptions;
import glide.api.models.commands.function.FunctionRestorePolicy;
import glide.api.models.commands.scan.ClusterScanCursor;
Expand Down Expand Up @@ -957,47 +957,6 @@ public CompletableFuture<String> functionKill(@NonNull Route route) {
FunctionKill, new String[0], route, this::handleStringResponse);
}

@Override
public CompletableFuture<Object> invokeScript(@NonNull Script script) {
if (script.getBinaryOutput()) {
return commandManager.submitScript(
script, List.of(), List.of(), this::handleBinaryObjectOrNullResponse);
} else {
return commandManager.submitScript(
script, List.of(), List.of(), this::handleObjectOrNullResponse);
}
}

@Override
public CompletableFuture<Object> invokeScript(
@NonNull Script script, @NonNull ScriptOptions options) {
if (script.getBinaryOutput()) {
return commandManager.submitScript(
script,
options.getKeys().stream().map(GlideString::gs).collect(Collectors.toList()),
options.getArgs().stream().map(GlideString::gs).collect(Collectors.toList()),
this::handleBinaryObjectOrNullResponse);
} else {
return commandManager.submitScript(
script,
options.getKeys().stream().map(GlideString::gs).collect(Collectors.toList()),
options.getArgs().stream().map(GlideString::gs).collect(Collectors.toList()),
this::handleObjectOrNullResponse);
}
}

@Override
public CompletableFuture<Object> invokeScript(
@NonNull Script script, @NonNull ScriptOptionsGlideString options) {
if (script.getBinaryOutput()) {
return commandManager.submitScript(
script, options.getKeys(), options.getArgs(), this::handleBinaryObjectOrNullResponse);
} else {
return commandManager.submitScript(
script, options.getKeys(), options.getArgs(), this::handleObjectOrNullResponse);
}
}

@Override
public CompletableFuture<Object> invokeScript(@NonNull Script script, @NonNull Route route) {
if (script.getBinaryOutput()) {
Expand All @@ -1011,31 +970,26 @@ public CompletableFuture<Object> invokeScript(@NonNull Script script, @NonNull R

@Override
public CompletableFuture<Object> invokeScript(
@NonNull Script script, @NonNull List<String> args, @NonNull Route route) {
if (script.getBinaryOutput()) {
return commandManager.submitScript(
script,
args.stream().map(GlideString::gs).collect(Collectors.toList()),
route,
this::handleBinaryObjectOrNullResponse);
} else {
return commandManager.submitScript(
script,
args.stream().map(GlideString::gs).collect(Collectors.toList()),
route,
this::handleObjectOrNullResponse);
}
@NonNull Script script, @NonNull ScriptArgOptions options, @NonNull Route route) {
return commandManager.submitScript(
script,
options.getArgs().stream().map(GlideString::gs).collect(Collectors.toList()),
route,
script.getBinaryOutput()
? this::handleBinaryObjectOrNullResponse
: this::handleObjectOrNullResponse);
}

@Override
public CompletableFuture<Object> invokeScriptBinary(
@NonNull Script script, @NonNull List<GlideString> args, @NonNull Route route) {
if (script.getBinaryOutput()) {
return commandManager.submitScript(
script, args, route, this::handleBinaryObjectOrNullResponse);
} else {
return commandManager.submitScript(script, args, route, this::handleObjectOrNullResponse);
}
public CompletableFuture<Object> invokeScript(
@NonNull Script script, @NonNull ScriptArgOptionsGlideString options, @NonNull Route route) {
return commandManager.submitScript(
script,
options.getArgs(),
route,
script.getBinaryOutput()
? this::handleBinaryObjectOrNullResponse
: this::handleObjectOrNullResponse);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
package glide.api.commands;

import glide.api.models.GlideString;
import glide.api.models.Script;
import glide.api.models.commands.ScriptOptions;
import glide.api.models.commands.ScriptOptionsGlideString;
import glide.api.models.configuration.ReadFrom;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -128,4 +131,88 @@ CompletableFuture<Object> fcall(
*/
CompletableFuture<Object> fcallReadOnly(
GlideString function, GlideString[] keys, GlideString[] arguments);

/**
* Invokes a Lua script.<br>
* This method simplifies the process of invoking scripts on the server by using an object that
* represents a Lua script. The script loading and execution will all be handled internally. If
* the script has not already been loaded, it will be loaded automatically using the <code>
* SCRIPT LOAD</code> command. After that, it will be invoked using the <code>EVALSHA </code>
* command.
*
* @see <a href="https://valkey.io/commands/script-load/">SCRIPT LOAD</a> and <a
* href="https://valkey.io/commands/evalsha/">EVALSHA</a> for details.
* @param script The Lua script to execute.
* @return A value that depends on the script that was executed.
* @example
* <pre>{@code
* try(Script luaScript = new Script("return 'Hello'", false)) {
* String result = (String) client.invokeScript(luaScript).get();
* assert result.equals("Hello");
* }
* }</pre>
*/
CompletableFuture<Object> invokeScript(Script script);

/**
* Invokes a Lua script with its keys and arguments.<br>
* This method simplifies the process of invoking scripts on the server by using an object that
* represents a Lua script. The script loading, argument preparation, and execution will all be
* handled internally. If the script has not already been loaded, it will be loaded automatically
* using the <code>SCRIPT LOAD</code> command. After that, it will be invoked using the <code>
* EVALSHA</code> command.
*
* @apiNote When in cluster mode
* <ul>
* <li>all <code>keys</code> must map to the same hash slot.
* <li>if no <code>keys</code> are given, command will be routed to a random primary node.
* </ul>
*
* @see <a href="https://valkey.io/commands/script-load/">SCRIPT LOAD</a> and <a
* href="https://valkey.io/commands/evalsha/">EVALSHA</a> for details.
* @param script The Lua script to execute.
* @param options The script option that contains keys and arguments for the script.
* @return A value that depends on the script that was executed.
* @example
* <pre>{@code
* try(Script luaScript = new Script("return { KEYS[1], ARGV[1] }", false)) {
* ScriptOptions scriptOptions = ScriptOptions.builder().key("foo").arg("bar").build();
* Object[] result = (Object[]) client.invokeScript(luaScript, scriptOptions).get();
* assert result[0].equals("foo");
* assert result[1].equals("bar");
* }
* }</pre>
*/
CompletableFuture<Object> invokeScript(Script script, ScriptOptions options);

/**
* Invokes a Lua script with its keys and arguments.<br>
* This method simplifies the process of invoking scripts on the server by using an object that
* represents a Lua script. The script loading, argument preparation, and execution will all be
* handled internally. If the script has not already been loaded, it will be loaded automatically
* using the <code>SCRIPT LOAD</code> command. After that, it will be invoked using the <code>
* EVALSHA</code> command.
*
* @apiNote When in cluster mode
* <ul>
* <li>all <code>keys</code> must map to the same hash slot.
* <li>if no <code>keys</code> are given, command will be routed to a random primary node.
* </ul>
*
* @see <a href="https://valkey.io/commands/script-load/">SCRIPT LOAD</a> and <a
* href="https://valkey.io/commands/evalsha/">EVALSHA</a> for details.
* @param script The Lua script to execute.
* @param options The script option that contains keys and arguments for the script.
* @return A value that depends on the script that was executed.
* @example
* <pre>{@code
* try(Script luaScript = new Script(gs("return { KEYS[1], ARGV[1] }", true))) {
* ScriptOptionsGlideString scriptOptions = ScriptOptionsGlideString.builder().key(gs("foo")).arg(gs("bar")).build();
* Object[] result = (Object[]) client.invokeScript(luaScript, scriptOptions).get();
* assert result[0].equals(gs("foo"));
* assert result[1].equals(gs("bar"));
* }
* }</pre>
*/
CompletableFuture<Object> invokeScript(Script script, ScriptOptionsGlideString options);
}
Loading

0 comments on commit e91a553

Please sign in to comment.