Skip to content

Commit

Permalink
Clean up FUNCTION DUMP & RESTORE
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Carbonetto <[email protected]>
  • Loading branch information
acarbonetto committed Jun 21, 2024
1 parent c05ceae commit 5453a09
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 21 deletions.
14 changes: 7 additions & 7 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ protected static CommandManager buildCommandManager(ChannelHandler channelHandle
*/
@SuppressWarnings("unchecked")
protected <T> T handleRedisResponse(
Class<T> classType, Set<ResponseFlags> flags, Response response) throws RedisException {
Class<T> classType, EnumSet<ResponseFlags> flags, Response response) throws RedisException {
boolean encodingUtf8 = flags.contains(ResponseFlags.ENCODING_UTF8);
boolean isNullable = flags.contains(ResponseFlags.IS_NULLABLE);
Object value =
Expand Down Expand Up @@ -373,11 +373,11 @@ protected String handleStringOrNullResponse(Response response) throws RedisExcep
String.class, EnumSet.of(ResponseFlags.IS_NULLABLE, ResponseFlags.ENCODING_UTF8), response);
}

protected GlideString handleBytesResponse(Response response) throws RedisException {
protected GlideString handleGlideStringResponse(Response response) throws RedisException {
return handleRedisResponse(GlideString.class, EnumSet.noneOf(ResponseFlags.class), response);
}

protected GlideString handleBytesOrNullResponse(Response response) throws RedisException {
protected GlideString handleGlideStringOrNullResponse(Response response) throws RedisException {
return handleRedisResponse(GlideString.class, EnumSet.of(ResponseFlags.IS_NULLABLE), response);
}

Expand Down Expand Up @@ -434,7 +434,7 @@ protected <V> Map<String, V> handleMapResponse(Response response) throws RedisEx
* @param <V> Value type.
*/
@SuppressWarnings("unchecked") // raw Map cast to Map<GlideString, V>
protected <V> Map<GlideString, V> handleBinaryMapResponse(Response response)
protected <V> Map<GlideString, V> handleBinaryStringMapResponse(Response response)
throws RedisException {
return handleRedisResponse(Map.class, EnumSet.noneOf(ResponseFlags.class), response);
}
Expand Down Expand Up @@ -509,7 +509,7 @@ public CompletableFuture<String> get(@NonNull String key) {
@Override
public CompletableFuture<GlideString> get(@NonNull GlideString key) {
return commandManager.submitNewCommand(
Get, new GlideString[] {key}, this::handleBytesOrNullResponse);
Get, new GlideString[] {key}, this::handleGlideStringOrNullResponse);
}

@Override
Expand All @@ -521,7 +521,7 @@ public CompletableFuture<String> getdel(@NonNull String key) {
@Override
public CompletableFuture<GlideString> getdel(@NonNull GlideString key) {
return commandManager.submitNewCommand(
GetDel, new GlideString[] {key}, this::handleBytesOrNullResponse);
GetDel, new GlideString[] {key}, this::handleGlideStringOrNullResponse);
}

@Override
Expand Down Expand Up @@ -718,7 +718,7 @@ public CompletableFuture<Map<String, String>> hgetall(@NonNull String key) {
@Override
public CompletableFuture<Map<GlideString, GlideString>> hgetall(@NonNull GlideString key) {
return commandManager.submitNewCommand(
HGetAll, new GlideString[] {key}, this::handleBinaryMapResponse);
HGetAll, new GlideString[] {key}, this::handleBinaryStringMapResponse);
}

@Override
Expand Down
6 changes: 4 additions & 2 deletions java/client/src/main/java/glide/api/RedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ public CompletableFuture<String> functionDelete(@NonNull String libName) {
@Override
public CompletableFuture<byte[]> functionDump() {
return commandManager.submitNewCommand(
FunctionDump, new GlideString[0], response -> handleBytesResponse(response).getBytes());
FunctionDump,
new GlideString[0],
response -> handleGlideStringResponse(response).getBytes());
}

@Override
Expand All @@ -294,7 +296,7 @@ public CompletableFuture<String> functionRestore(
byte @NonNull [] payload, @NonNull FunctionRestorePolicy policy) {
return commandManager.submitNewCommand(
FunctionRestore,
new GlideString[] {gs(payload), gs(policy.toString().getBytes())},
new GlideString[] {gs(payload), gs(policy.toString())},
this::handleStringResponse);
}

Expand Down
10 changes: 5 additions & 5 deletions java/client/src/main/java/glide/api/RedisClusterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ public CompletableFuture<ClusterValue<byte[]>> functionDump() {
return commandManager.submitNewCommand(
FunctionDump,
new GlideString[] {},
response -> ClusterValue.ofMultiValueBinary(handleBinaryMapResponse(response)));
response -> ClusterValue.ofMultiValueBinary(handleBinaryStringMapResponse(response)));
}

@Override
Expand All @@ -596,8 +596,8 @@ public CompletableFuture<ClusterValue<byte[]>> functionDump(@NonNull Route route
route,
response ->
route instanceof SingleNodeRoute
? ClusterValue.ofSingleValue(handleBytesResponse(response).getBytes())
: ClusterValue.ofMultiValueBinary(handleBinaryMapResponse(response)));
? ClusterValue.ofSingleValue(handleGlideStringResponse(response).getBytes())
: ClusterValue.ofMultiValueBinary(handleBinaryStringMapResponse(response)));
}

@Override
Expand All @@ -611,7 +611,7 @@ public CompletableFuture<String> functionRestore(
byte @NonNull [] payload, @NonNull FunctionRestorePolicy policy) {
return commandManager.submitNewCommand(
FunctionRestore,
new GlideString[] {gs(payload), gs(policy.toString().getBytes())},
new GlideString[] {gs(payload), gs(policy.toString())},
this::handleStringResponse);
}

Expand All @@ -626,7 +626,7 @@ public CompletableFuture<String> functionRestore(
byte @NonNull [] payload, @NonNull FunctionRestorePolicy policy, @NonNull Route route) {
return commandManager.submitNewCommand(
FunctionRestore,
new GlideString[] {gs(payload), gs(policy.toString().getBytes())},
new GlideString[] {gs(payload), gs(policy.toString())},
route,
this::handleStringResponse);
}
Expand Down
2 changes: 1 addition & 1 deletion java/client/src/test/java/glide/api/RedisClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5772,7 +5772,7 @@ public void functionRestore_returns_success() {
public void functionRestore_with_policy_returns_success() {
// setup
byte[] data = new byte[] {42};
GlideString[] args = {gs(data), gs(FunctionRestorePolicy.FLUSH.toString().getBytes())};
GlideString[] args = {gs(data), gs(FunctionRestorePolicy.FLUSH.toString())};
CompletableFuture<String> testResponse = new CompletableFuture<>();
testResponse.complete(OK);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@
import glide.managers.CommandManager;
import glide.managers.ConnectionManager;
import glide.managers.RedisExceptionCheckedFunction;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import lombok.SneakyThrows;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -160,7 +160,7 @@ public TestClient(CommandManager commandManager, Object objectToReturn) {

@Override
protected <T> T handleRedisResponse(
Class<T> classType, Set<ResponseFlags> flags, Response response) {
Class<T> classType, EnumSet<ResponseFlags> flags, Response response) {
@SuppressWarnings("unchecked")
T returnValue = (T) object;
return returnValue;
Expand Down Expand Up @@ -1940,7 +1940,7 @@ public void functionRestore_returns_success() {
public void functionRestore_with_policy_returns_success() {
// setup
byte[] data = new byte[] {42};
GlideString[] args = {gs(data), gs(FunctionRestorePolicy.FLUSH.toString().getBytes())};
GlideString[] args = {gs(data), gs(FunctionRestorePolicy.FLUSH.toString())};
CompletableFuture<String> testResponse = new CompletableFuture<>();
testResponse.complete(OK);

Expand Down Expand Up @@ -1985,7 +1985,7 @@ public void functionRestore_with_route_returns_success() {
public void functionRestore_with_policy_and_route_returns_success() {
// setup
byte[] data = new byte[] {42};
GlideString[] args = {gs(data), gs(FunctionRestorePolicy.FLUSH.toString().getBytes())};
GlideString[] args = {gs(data), gs(FunctionRestorePolicy.FLUSH.toString())};
CompletableFuture<String> testResponse = new CompletableFuture<>();
testResponse.complete(OK);

Expand Down
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.models;

import static glide.api.models.GlideString.gs;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -70,6 +71,24 @@ public void multi_value_ctor() {
assertAll(
() -> assertTrue(value.hasMultiData()),
() -> assertFalse(value.hasSingleData()),
() -> assertNotNull(value.getMultiValue()));
() -> assertNotNull(value.getMultiValue()),
() -> assertTrue(value.getMultiValue().containsKey("config1")),
() -> assertTrue(value.getMultiValue().containsKey("config2")));
}

@Test
public void multi_value_binary_ctor() {
var value =
ClusterValue.ofMultiValueBinary(
Map.of(gs("config1"), gs("param1"), gs("config2"), gs("param2")));
assertAll(
() -> assertTrue(value.hasMultiData()),
() -> assertFalse(value.hasSingleData()),
() -> assertNotNull(value.getMultiValue()),
// ofMultiValueBinary converts the key to a String, but the values are not converted
() -> assertTrue(value.getMultiValue().containsKey("config1")),
() -> assertTrue(value.getMultiValue().get("config1").equals(gs("param1"))),
() -> assertTrue(value.getMultiValue().containsKey("config2")),
() -> assertTrue(value.getMultiValue().get("config2").equals(gs("param2"))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ public void function_dump_and_restore() {
code = generateLuaLibCode(name2, Map.of(name1, "return args[1]", name2, "return #args"), false);
assertEquals(name2, regularClient.functionLoad(code, true).get());

// REPLACE policy now fails due name collision
// REPLACE policy now fails due to a name collision
executionException =
assertThrows(
ExecutionException.class, () -> regularClient.functionRestore(dump, REPLACE).get());
Expand Down

0 comments on commit 5453a09

Please sign in to comment.