Skip to content

Commit

Permalink
Java: Update implementation for FUNCTION LOAD. (#1484)
Browse files Browse the repository at this point in the history
Java: Update implementation for `FUNCTION LOAD`. (#315)

* Update implementation for `FUNCTION LOAD`.

Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
Co-authored-by: Aaron <69273634+aaron-congo@users.noreply.github.com>
  • Loading branch information
Yury-Fridlyand and aaron-congo authored Jun 3, 2024
1 parent e93129a commit abd1f62
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 142 deletions.
17 changes: 5 additions & 12 deletions java/client/src/main/java/glide/api/RedisClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */
package glide.api;

import static glide.api.models.commands.function.FunctionLoadOptions.REPLACE;
import static glide.utils.ArrayTransformUtils.castArray;
import static glide.utils.ArrayTransformUtils.concatenateArrays;
import static glide.utils.ArrayTransformUtils.convertMapToKeyValueStringArray;
Expand Down Expand Up @@ -28,7 +29,6 @@
import glide.api.models.Transaction;
import glide.api.models.commands.FlushMode;
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.function.FunctionLoadOptions;
import glide.api.models.configuration.RedisClientConfiguration;
import glide.managers.CommandManager;
import glide.managers.ConnectionManager;
Expand Down Expand Up @@ -191,16 +191,9 @@ public CompletableFuture<String> lolwut(int version, int @NonNull [] parameters)
}

@Override
public CompletableFuture<String> functionLoad(@NonNull String libraryCode) {
return commandManager.submitNewCommand(
FunctionLoad, new String[] {libraryCode}, this::handleStringResponse);
}

@Override
public CompletableFuture<String> functionLoadReplace(@NonNull String libraryCode) {
return commandManager.submitNewCommand(
FunctionLoad,
new String[] {FunctionLoadOptions.REPLACE.toString(), libraryCode},
this::handleStringResponse);
public CompletableFuture<String> functionLoad(@NonNull String libraryCode, boolean replace) {
String[] arguments =
replace ? new String[] {REPLACE.toString(), libraryCode} : new String[] {libraryCode};
return commandManager.submitNewCommand(FunctionLoad, arguments, this::handleStringResponse);
}
}
34 changes: 10 additions & 24 deletions java/client/src/main/java/glide/api/RedisClusterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package glide.api;

import static glide.api.commands.ServerManagementCommands.VERSION_REDIS_API;
import static glide.api.models.commands.function.FunctionLoadOptions.REPLACE;
import static glide.utils.ArrayTransformUtils.castArray;
import static glide.utils.ArrayTransformUtils.castMapOfArrays;
import static glide.utils.ArrayTransformUtils.concatenateArrays;
Expand Down Expand Up @@ -30,7 +31,6 @@
import glide.api.models.ClusterValue;
import glide.api.models.commands.FlushMode;
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.function.FunctionLoadOptions;
import glide.api.models.configuration.RedisClusterClientConfiguration;
import glide.api.models.configuration.RequestRoutingConfiguration.Route;
import glide.api.models.configuration.RequestRoutingConfiguration.SingleNodeRoute;
Expand Down Expand Up @@ -418,32 +418,18 @@ public CompletableFuture<ClusterValue<String>> lolwut(
}

@Override
public CompletableFuture<String> functionLoad(@NonNull String libraryCode) {
return commandManager.submitNewCommand(
FunctionLoad, new String[] {libraryCode}, this::handleStringResponse);
}

@Override
public CompletableFuture<String> functionLoadReplace(@NonNull String libraryCode) {
return commandManager.submitNewCommand(
FunctionLoad,
new String[] {FunctionLoadOptions.REPLACE.toString(), libraryCode},
this::handleStringResponse);
}

@Override
public CompletableFuture<String> functionLoad(@NonNull String libraryCode, @NonNull Route route) {
return commandManager.submitNewCommand(
FunctionLoad, new String[] {libraryCode}, route, this::handleStringResponse);
public CompletableFuture<String> functionLoad(@NonNull String libraryCode, boolean replace) {
String[] arguments =
replace ? new String[] {REPLACE.toString(), libraryCode} : new String[] {libraryCode};
return commandManager.submitNewCommand(FunctionLoad, arguments, this::handleStringResponse);
}

@Override
public CompletableFuture<String> functionLoadReplace(
@NonNull String libraryCode, @NonNull Route route) {
public CompletableFuture<String> functionLoad(
@NonNull String libraryCode, boolean replace, @NonNull Route route) {
String[] arguments =
replace ? new String[] {REPLACE.toString(), libraryCode} : new String[] {libraryCode};
return commandManager.submitNewCommand(
FunctionLoad,
new String[] {FunctionLoadOptions.REPLACE.toString(), libraryCode},
route,
this::handleStringResponse);
FunctionLoad, arguments, route, this::handleStringResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,75 +13,42 @@
public interface ScriptingAndFunctionsClusterCommands {

/**
* Loads a library to Redis unless a library with the same name exists. Use {@link
* #functionLoadReplace(String)} to replace existing libraries.<br>
* Loads a library to Redis.<br>
* The command will be routed to all primary nodes.
*
* @since Redis 7.0 and above.
* @see <a href="https://redis.io/docs/latest/commands/function-load/">redis.io</a> for details.
* @param libraryCode The source code that implements the library.
* @param replace Whether the given library should overwrite a library with the same name if it
* already exists.
* @return The library name that was loaded.
* @example
* <pre>{@code
* String code = "#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)";
* String response = client.functionLoad(code).get();
* String response = client.functionLoad(code, true).get();
* assert response.equals("mylib");
* }</pre>
*/
CompletableFuture<String> functionLoad(String libraryCode);
CompletableFuture<String> functionLoad(String libraryCode, boolean replace);

/**
* Loads a library to Redis and overwrites a library with the same name if it exists.<br>
* The command will be routed to all primary nodes.
*
* @since Redis 7.0 and above.
* @see <a href="https://redis.io/docs/latest/commands/function-load/">redis.io</a> for details.
* @param libraryCode The source code that implements the library.
* @return The library name that was loaded.
* @example
* <pre>{@code
* String code = "#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)";
* String response = client.functionLoadReplace(code).get();
* assert response.equals("mylib");
* }</pre>
*/
CompletableFuture<String> functionLoadReplace(String libraryCode);

/**
* Loads a library to Redis unless a library with the same name exists. Use {@link
* #functionLoadReplace(String, Route)} to replace existing libraries.<br>
* Loads a library to Redis.
*
* @since Redis 7.0 and above.
* @see <a href="https://redis.io/docs/latest/commands/function-load/">redis.io</a> for details.
* @param libraryCode The source code that implements the library.
* @param replace Whether the given library should overwrite a library with the same name if it
* already exists.
* @param route Specifies the routing configuration for the command. The client will route the
* command to the nodes defined by <code>route</code>.
* @return The library name that was loaded.
* @example
* <pre>{@code
* String code = "#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)";
* Route route = new SlotKeyRoute("key", PRIMARY);
* String response = client.functionLoad(code, route).get();
* assert response.equals("mylib");
* }</pre>
*/
CompletableFuture<String> functionLoad(String libraryCode, Route route);

/**
* Loads a library to Redis and overwrites a library with the same name if it exists.
*
* @since Redis 7.0 and above.
* @see <a href="https://redis.io/docs/latest/commands/function-load/">redis.io</a> for details.
* @param libraryCode The source code that implements the library.
* @param route Specifies the routing configuration for the command. The client will route the
* command to the nodes defined by <code>route</code>.
* @return The library name that was loaded.
* @example
* <pre>{@code
* String code = "#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)";
* String response = client.functionLoadReplace(code, ALL_NODES).get();
* String response = client.functionLoad(code, true, route).get();
* assert response.equals("mylib");
* }</pre>
*/
CompletableFuture<String> functionLoadReplace(String libraryCode, Route route);
CompletableFuture<String> functionLoad(String libraryCode, boolean replace, Route route);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,20 @@
public interface ScriptingAndFunctionsCommands {

/**
* Loads a library to Redis unless a library with the same name exists. Use {@link
* #functionLoadReplace} to replace existing libraries.
* Loads a library to Redis.
*
* @since Redis 7.0 and above.
* @see <a href="https://redis.io/docs/latest/commands/function-load/">redis.io</a> for details.
* @param libraryCode The source code that implements the library.
* @param replace Whether the given library should overwrite a library with the same name if it
* already exists.
* @return The library name that was loaded.
* @example
* <pre>{@code
* String code = "#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)";
* String response = client.functionLoad(code).get();
* String response = client.functionLoad(code, true).get();
* assert response.equals("mylib");
* }</pre>
*/
CompletableFuture<String> functionLoad(String libraryCode);

/**
* Loads a library to Redis and overwrites a library with the same name if it exists.
*
* @since Redis 7.0 and above.
* @see <a href="https://redis.io/docs/latest/commands/function-load/">redis.io</a> for details.
* @param libraryCode The source code that implements the library.
* @return The library name that was loaded.
* @example
* <pre>{@code
* String code = "#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)";
* String response = client.functionLoadReplace(code).get();
* assert response.equals("mylib");
* }</pre>
*/
CompletableFuture<String> functionLoadReplace(String libraryCode);
CompletableFuture<String> functionLoad(String libraryCode, boolean replace);
}
26 changes: 7 additions & 19 deletions java/client/src/main/java/glide/api/models/BaseTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static glide.api.commands.SortedSetBaseCommands.WITH_SCORES_REDIS_API;
import static glide.api.commands.SortedSetBaseCommands.WITH_SCORE_REDIS_API;
import static glide.api.models.commands.RangeOptions.createZRangeArgs;
import static glide.api.models.commands.function.FunctionLoadOptions.REPLACE;
import static glide.utils.ArrayTransformUtils.concatenateArrays;
import static glide.utils.ArrayTransformUtils.convertMapToKeyValueStringArray;
import static glide.utils.ArrayTransformUtils.convertMapToValueKeyStringArray;
Expand Down Expand Up @@ -186,7 +187,6 @@
import glide.api.models.commands.ZAddOptions;
import glide.api.models.commands.bitmap.BitmapIndexType;
import glide.api.models.commands.bitmap.BitwiseOperation;
import glide.api.models.commands.function.FunctionLoadOptions;
import glide.api.models.commands.geospatial.GeoAddOptions;
import glide.api.models.commands.geospatial.GeoUnit;
import glide.api.models.commands.geospatial.GeospatialData;
Expand Down Expand Up @@ -3573,30 +3573,18 @@ public T geohash(@NonNull String key, @NonNull String[] members) {
}

/**
* Loads a library to Redis unless a library with the same name exists. Use {@link
* #functionLoadReplace} to replace existing libraries.
* Loads a library to Redis.
*
* @since Redis 7.0 and above.
* @see <a href="https://redis.io/docs/latest/commands/function-load/">redis.io</a> for details.
* @param libraryCode The source code that implements the library.
* @param replace Whether the given library should overwrite a library with the same name if it
* already exists.
* @return Command Response - The library name that was loaded.
*/
public T functionLoad(@NonNull String libraryCode) {
ArgsArray commandArgs = buildArgs(libraryCode);
protobufTransaction.addCommands(buildCommand(FunctionLoad, commandArgs));
return getThis();
}

/**
* Loads a library to Redis and overwrites a library with the same name if it exists.
*
* @since Redis 7.0 and above.
* @see <a href="https://redis.io/docs/latest/commands/function-load/">redis.io</a> for details.
* @param libraryCode The source code that implements the library.
* @return Command Response - The library name that was loaded.
*/
public T functionLoadReplace(@NonNull String libraryCode) {
ArgsArray commandArgs = buildArgs(FunctionLoadOptions.REPLACE.toString(), libraryCode);
public T functionLoad(@NonNull String libraryCode, boolean replace) {
ArgsArray commandArgs =
replace ? buildArgs(REPLACE.toString(), libraryCode) : buildArgs(libraryCode);
protobufTransaction.addCommands(buildCommand(FunctionLoad, commandArgs));
return getThis();
}
Expand Down
6 changes: 3 additions & 3 deletions java/client/src/test/java/glide/api/RedisClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4867,7 +4867,7 @@ public void functionLoad_returns_success() {
.thenReturn(testResponse);

// exercise
CompletableFuture<String> response = service.functionLoad(code);
CompletableFuture<String> response = service.functionLoad(code, false);
String payload = response.get();

// verify
Expand All @@ -4877,7 +4877,7 @@ public void functionLoad_returns_success() {

@SneakyThrows
@Test
public void functionLoadReplace_returns_success() {
public void functionLoad_with_replace_returns_success() {
// setup
String code = "The best code ever";
String[] args = new String[] {FunctionLoadOptions.REPLACE.toString(), code};
Expand All @@ -4890,7 +4890,7 @@ public void functionLoadReplace_returns_success() {
.thenReturn(testResponse);

// exercise
CompletableFuture<String> response = service.functionLoadReplace(code);
CompletableFuture<String> response = service.functionLoad(code, true);
String payload = response.get();

// verify
Expand Down
14 changes: 7 additions & 7 deletions java/client/src/test/java/glide/api/RedisClusterClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ public void functionLoad_returns_success() {
.thenReturn(testResponse);

// exercise
CompletableFuture<String> response = service.functionLoad(code);
CompletableFuture<String> response = service.functionLoad(code, false);
String payload = response.get();

// verify
Expand All @@ -1103,7 +1103,7 @@ public void functionLoad_returns_success() {

@SneakyThrows
@Test
public void functionLoadReplace_returns_success() {
public void functionLoad_with_replace_returns_success() {
// setup
String code = "The best code ever";
String[] args = new String[] {FunctionLoadOptions.REPLACE.toString(), code};
Expand All @@ -1116,7 +1116,7 @@ public void functionLoadReplace_returns_success() {
.thenReturn(testResponse);

// exercise
CompletableFuture<String> response = service.functionLoadReplace(code);
CompletableFuture<String> response = service.functionLoad(code, true);
String payload = response.get();

// verify
Expand All @@ -1126,7 +1126,7 @@ public void functionLoadReplace_returns_success() {

@SneakyThrows
@Test
public void functionLoadWithRoute_returns_success() {
public void functionLoad_with_route_returns_success() {
// setup
String code = "The best code ever";
String[] args = new String[] {code};
Expand All @@ -1139,7 +1139,7 @@ public void functionLoadWithRoute_returns_success() {
.thenReturn(testResponse);

// exercise
CompletableFuture<String> response = service.functionLoad(code, RANDOM);
CompletableFuture<String> response = service.functionLoad(code, false, RANDOM);
String payload = response.get();

// verify
Expand All @@ -1149,7 +1149,7 @@ public void functionLoadWithRoute_returns_success() {

@SneakyThrows
@Test
public void functionLoadReplaceRoute_returns_success() {
public void functionLoad_with_replace_with_route_returns_success() {
// setup
String code = "The best code ever";
String[] args = new String[] {FunctionLoadOptions.REPLACE.toString(), code};
Expand All @@ -1162,7 +1162,7 @@ public void functionLoadReplaceRoute_returns_success() {
.thenReturn(testResponse);

// exercise
CompletableFuture<String> response = service.functionLoadReplace(code, RANDOM);
CompletableFuture<String> response = service.functionLoad(code, true, RANDOM);
String payload = response.get();

// verify
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ InfScoreBound.NEGATIVE_INFINITY, new ScoreBoundary(3, false), new Limit(1, 2)),
transaction.geopos("key", new String[] {"Place"});
results.add(Pair.of(GeoPos, buildArgs("key", "Place")));

transaction.functionLoad("pewpew").functionLoadReplace("ololo");
transaction.functionLoad("pewpew", false).functionLoad("ololo", true);
results.add(Pair.of(FunctionLoad, buildArgs("pewpew")));
results.add(Pair.of(FunctionLoad, buildArgs("REPLACE", "ololo")));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,11 +643,11 @@ private static Object[] scriptingAndFunctionsCommands(BaseTransaction<?> transac
"#!lua name=mylib1T \n"
+ " redis.register_function('myfunc1T', function(keys, args) return args[1] end)";

transaction.functionLoad(code).functionLoadReplace(code);
transaction.functionLoad(code, false).functionLoad(code, true);

return new Object[] {
"mylib1T", // functionLoad(code)
"mylib1T" // functionLoadReplace(code)
"mylib1T", // functionLoad(code, false)
"mylib1T", // functionLoad(code, true)
};
}

Expand Down
Loading

0 comments on commit abd1f62

Please sign in to comment.