diff --git a/java/client/src/main/java/glide/api/RedisClient.java b/java/client/src/main/java/glide/api/RedisClient.java index 79c3ba9a45..c70586f9b9 100644 --- a/java/client/src/main/java/glide/api/RedisClient.java +++ b/java/client/src/main/java/glide/api/RedisClient.java @@ -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; @@ -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; @@ -191,16 +191,9 @@ public CompletableFuture lolwut(int version, int @NonNull [] parameters) } @Override - public CompletableFuture functionLoad(@NonNull String libraryCode) { - return commandManager.submitNewCommand( - FunctionLoad, new String[] {libraryCode}, this::handleStringResponse); - } - - @Override - public CompletableFuture functionLoadReplace(@NonNull String libraryCode) { - return commandManager.submitNewCommand( - FunctionLoad, - new String[] {FunctionLoadOptions.REPLACE.toString(), libraryCode}, - this::handleStringResponse); + public CompletableFuture functionLoad(@NonNull String libraryCode, boolean replace) { + String[] arguments = + replace ? new String[] {REPLACE.toString(), libraryCode} : new String[] {libraryCode}; + return commandManager.submitNewCommand(FunctionLoad, arguments, this::handleStringResponse); } } diff --git a/java/client/src/main/java/glide/api/RedisClusterClient.java b/java/client/src/main/java/glide/api/RedisClusterClient.java index 6a097cbfbd..f3eef93797 100644 --- a/java/client/src/main/java/glide/api/RedisClusterClient.java +++ b/java/client/src/main/java/glide/api/RedisClusterClient.java @@ -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; @@ -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; @@ -418,32 +418,18 @@ public CompletableFuture> lolwut( } @Override - public CompletableFuture functionLoad(@NonNull String libraryCode) { - return commandManager.submitNewCommand( - FunctionLoad, new String[] {libraryCode}, this::handleStringResponse); - } - - @Override - public CompletableFuture functionLoadReplace(@NonNull String libraryCode) { - return commandManager.submitNewCommand( - FunctionLoad, - new String[] {FunctionLoadOptions.REPLACE.toString(), libraryCode}, - this::handleStringResponse); - } - - @Override - public CompletableFuture functionLoad(@NonNull String libraryCode, @NonNull Route route) { - return commandManager.submitNewCommand( - FunctionLoad, new String[] {libraryCode}, route, this::handleStringResponse); + public CompletableFuture 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 functionLoadReplace( - @NonNull String libraryCode, @NonNull Route route) { + public CompletableFuture 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); } } diff --git a/java/client/src/main/java/glide/api/commands/ScriptingAndFunctionsClusterCommands.java b/java/client/src/main/java/glide/api/commands/ScriptingAndFunctionsClusterCommands.java index 1b72619e97..0df7077f4f 100644 --- a/java/client/src/main/java/glide/api/commands/ScriptingAndFunctionsClusterCommands.java +++ b/java/client/src/main/java/glide/api/commands/ScriptingAndFunctionsClusterCommands.java @@ -13,47 +13,32 @@ public interface ScriptingAndFunctionsClusterCommands { /** - * Loads a library to Redis unless a library with the same name exists. Use {@link - * #functionLoadReplace(String)} to replace existing libraries.
+ * Loads a library to Redis.
* The command will be routed to all primary nodes. * * @since Redis 7.0 and above. * @see redis.io 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 *
{@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");
      * }
*/ - CompletableFuture functionLoad(String libraryCode); + CompletableFuture functionLoad(String libraryCode, boolean replace); /** - * Loads a library to Redis and overwrites a library with the same name if it exists.
- * The command will be routed to all primary nodes. - * - * @since Redis 7.0 and above. - * @see redis.io for details. - * @param libraryCode The source code that implements the library. - * @return The library name that was loaded. - * @example - *
{@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");
-     * }
- */ - CompletableFuture 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.
+ * Loads a library to Redis. * * @since Redis 7.0 and above. * @see redis.io 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 route. * @return The library name that was loaded. @@ -61,27 +46,9 @@ public interface ScriptingAndFunctionsClusterCommands { *
{@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");
-     * }
- */ - CompletableFuture 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 redis.io 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 route. - * @return The library name that was loaded. - * @example - *
{@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");
      * }
*/ - CompletableFuture functionLoadReplace(String libraryCode, Route route); + CompletableFuture functionLoad(String libraryCode, boolean replace, Route route); } diff --git a/java/client/src/main/java/glide/api/commands/ScriptingAndFunctionsCommands.java b/java/client/src/main/java/glide/api/commands/ScriptingAndFunctionsCommands.java index 190335c4e5..3da638d998 100644 --- a/java/client/src/main/java/glide/api/commands/ScriptingAndFunctionsCommands.java +++ b/java/client/src/main/java/glide/api/commands/ScriptingAndFunctionsCommands.java @@ -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 redis.io 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 *
{@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");
      * }
*/ - CompletableFuture 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 redis.io for details. - * @param libraryCode The source code that implements the library. - * @return The library name that was loaded. - * @example - *
{@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");
-     * }
- */ - CompletableFuture functionLoadReplace(String libraryCode); + CompletableFuture functionLoad(String libraryCode, boolean replace); } diff --git a/java/client/src/main/java/glide/api/models/BaseTransaction.java b/java/client/src/main/java/glide/api/models/BaseTransaction.java index f836decaa9..adac388126 100644 --- a/java/client/src/main/java/glide/api/models/BaseTransaction.java +++ b/java/client/src/main/java/glide/api/models/BaseTransaction.java @@ -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; @@ -176,7 +177,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; @@ -3426,30 +3426,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 redis.io 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 redis.io 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(); } diff --git a/java/client/src/test/java/glide/api/RedisClientTest.java b/java/client/src/test/java/glide/api/RedisClientTest.java index c864cf4138..b0559cfd4a 100644 --- a/java/client/src/test/java/glide/api/RedisClientTest.java +++ b/java/client/src/test/java/glide/api/RedisClientTest.java @@ -4698,7 +4698,7 @@ public void functionLoad_returns_success() { .thenReturn(testResponse); // exercise - CompletableFuture response = service.functionLoad(code); + CompletableFuture response = service.functionLoad(code, false); String payload = response.get(); // verify @@ -4708,7 +4708,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}; @@ -4721,7 +4721,7 @@ public void functionLoadReplace_returns_success() { .thenReturn(testResponse); // exercise - CompletableFuture response = service.functionLoadReplace(code); + CompletableFuture response = service.functionLoad(code, true); String payload = response.get(); // verify diff --git a/java/client/src/test/java/glide/api/RedisClusterClientTest.java b/java/client/src/test/java/glide/api/RedisClusterClientTest.java index 01beb573bf..0af49287f8 100644 --- a/java/client/src/test/java/glide/api/RedisClusterClientTest.java +++ b/java/client/src/test/java/glide/api/RedisClusterClientTest.java @@ -1093,7 +1093,7 @@ public void functionLoad_returns_success() { .thenReturn(testResponse); // exercise - CompletableFuture response = service.functionLoad(code); + CompletableFuture response = service.functionLoad(code, false); String payload = response.get(); // verify @@ -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}; @@ -1116,7 +1116,7 @@ public void functionLoadReplace_returns_success() { .thenReturn(testResponse); // exercise - CompletableFuture response = service.functionLoadReplace(code); + CompletableFuture response = service.functionLoad(code, true); String payload = response.get(); // verify @@ -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}; @@ -1139,7 +1139,7 @@ public void functionLoadWithRoute_returns_success() { .thenReturn(testResponse); // exercise - CompletableFuture response = service.functionLoad(code, RANDOM); + CompletableFuture response = service.functionLoad(code, false, RANDOM); String payload = response.get(); // verify @@ -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}; @@ -1162,7 +1162,7 @@ public void functionLoadReplaceRoute_returns_success() { .thenReturn(testResponse); // exercise - CompletableFuture response = service.functionLoadReplace(code, RANDOM); + CompletableFuture response = service.functionLoad(code, true, RANDOM); String payload = response.get(); // verify diff --git a/java/client/src/test/java/glide/api/models/TransactionTests.java b/java/client/src/test/java/glide/api/models/TransactionTests.java index 53a3dca6fe..8ee6e3fc00 100644 --- a/java/client/src/test/java/glide/api/models/TransactionTests.java +++ b/java/client/src/test/java/glide/api/models/TransactionTests.java @@ -807,7 +807,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"))); diff --git a/java/integTest/src/test/java/glide/TransactionTestUtilities.java b/java/integTest/src/test/java/glide/TransactionTestUtilities.java index f7dddad6d7..54df793dfe 100644 --- a/java/integTest/src/test/java/glide/TransactionTestUtilities.java +++ b/java/integTest/src/test/java/glide/TransactionTestUtilities.java @@ -579,11 +579,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) }; } diff --git a/java/integTest/src/test/java/glide/cluster/CommandTests.java b/java/integTest/src/test/java/glide/cluster/CommandTests.java index cf68903d74..7d5986b4f8 100644 --- a/java/integTest/src/test/java/glide/cluster/CommandTests.java +++ b/java/integTest/src/test/java/glide/cluster/CommandTests.java @@ -781,14 +781,18 @@ public void functionLoad(boolean withRoute) { Route route = new SlotKeyRoute("1", PRIMARY); var promise = - withRoute ? clusterClient.functionLoad(code, route) : clusterClient.functionLoad(code); + withRoute + ? clusterClient.functionLoad(code, false, route) + : clusterClient.functionLoad(code, false); assertEquals(libName, promise.get()); // TODO test function with FCALL when fixed in redis-rs and implemented // TODO test with FUNCTION LIST // re-load library without overwriting promise = - withRoute ? clusterClient.functionLoad(code, route) : clusterClient.functionLoad(code); + withRoute + ? clusterClient.functionLoad(code, false, route) + : clusterClient.functionLoad(code, false); var executionException = assertThrows(ExecutionException.class, promise::get); assertInstanceOf(RequestException.class, executionException.getCause()); assertTrue( @@ -797,8 +801,8 @@ public void functionLoad(boolean withRoute) { // re-load library with overwriting var promise2 = withRoute - ? clusterClient.functionLoadReplace(code, route) - : clusterClient.functionLoadReplace(code); + ? clusterClient.functionLoad(code, true, route) + : clusterClient.functionLoad(code, true); assertEquals(libName, promise2.get()); String newCode = code @@ -807,8 +811,8 @@ public void functionLoad(boolean withRoute) { + "', function(keys, args) return #args end)"; promise2 = withRoute - ? clusterClient.functionLoadReplace(newCode, route) - : clusterClient.functionLoadReplace(newCode); + ? clusterClient.functionLoad(newCode, true, route) + : clusterClient.functionLoad(newCode, true); assertEquals(libName, promise2.get()); // TODO test with FCALL } diff --git a/java/integTest/src/test/java/glide/standalone/CommandTests.java b/java/integTest/src/test/java/glide/standalone/CommandTests.java index 5426796d27..079b0593fb 100644 --- a/java/integTest/src/test/java/glide/standalone/CommandTests.java +++ b/java/integTest/src/test/java/glide/standalone/CommandTests.java @@ -346,22 +346,22 @@ public void functionLoad() { "#!lua name=" + libName + " \n redis.register_function('myfunc1c', function(keys, args) return args[1] end)"; - assertEquals(libName, regularClient.functionLoad(code).get()); + assertEquals(libName, regularClient.functionLoad(code, false).get()); // TODO test function with FCALL when fixed in redis-rs and implemented // TODO test with FUNCTION LIST // re-load library without overwriting var executionException = - assertThrows(ExecutionException.class, () -> regularClient.functionLoad(code).get()); + assertThrows(ExecutionException.class, () -> regularClient.functionLoad(code, false).get()); assertInstanceOf(RequestException.class, executionException.getCause()); assertTrue( executionException.getMessage().contains("Library '" + libName + "' already exists")); // re-load library with overwriting - assertEquals(libName, regularClient.functionLoadReplace(code).get()); + assertEquals(libName, regularClient.functionLoad(code, true).get()); String newCode = code + "\n redis.register_function('myfunc2c', function(keys, args) return #args end)"; - assertEquals(libName, regularClient.functionLoadReplace(newCode).get()); + assertEquals(libName, regularClient.functionLoad(newCode, true).get()); // TODO test with FCALL } }