Skip to content

Commit

Permalink
Use GlideString
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Carbonetto <[email protected]>
  • Loading branch information
acarbonetto committed Jun 20, 2024
1 parent 6db425f commit c05ceae
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 54 deletions.
6 changes: 4 additions & 2 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,9 @@ protected <V> Map<String, V> handleMapResponse(Response response) throws RedisEx
return handleRedisResponse(Map.class, EnumSet.of(ResponseFlags.ENCODING_UTF8), response);
}

/** Get a map and convert {@link Map} keys from <code>byte[]</code> to {@link String}.
/**
* Get a map and convert {@link Map} keys from <code>byte[]</code> to {@link String}.
*
* @param response A Protobuf response
* @return A map of <code>GlideString</code> to <code>V</code>.
* @param <V> Value type.
Expand Down Expand Up @@ -716,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::handleMapResponseBinary);
HGetAll, new GlideString[] {key}, this::handleBinaryMapResponse);
}

@Override
Expand Down
10 changes: 6 additions & 4 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.GlideString.gs;
import static glide.api.models.commands.function.FunctionListOptions.LIBRARY_NAME_REDIS_API;
import static glide.api.models.commands.function.FunctionListOptions.WITH_CODE_REDIS_API;
import static glide.api.models.commands.function.FunctionLoadOptions.REPLACE;
Expand Down Expand Up @@ -42,6 +43,7 @@
import glide.api.commands.ScriptingAndFunctionsCommands;
import glide.api.commands.ServerManagementCommands;
import glide.api.commands.TransactionsCommands;
import glide.api.models.GlideString;
import glide.api.models.Transaction;
import glide.api.models.commands.FlushMode;
import glide.api.models.commands.InfoOptions;
Expand All @@ -50,7 +52,6 @@
import glide.managers.CommandManager;
import glide.managers.ConnectionManager;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import lombok.NonNull;
Expand Down Expand Up @@ -278,21 +279,22 @@ public CompletableFuture<String> functionDelete(@NonNull String libName) {

@Override
public CompletableFuture<byte[]> functionDump() {
return commandManager.submitNewCommand(FunctionDump, List.of(), this::handleBytesResponse);
return commandManager.submitNewCommand(
FunctionDump, new GlideString[0], response -> handleBytesResponse(response).getBytes());
}

@Override
public CompletableFuture<String> functionRestore(byte @NonNull [] payload) {
return commandManager.submitNewCommand(
FunctionRestore, List.of(payload), this::handleStringResponse);
FunctionRestore, new GlideString[] {gs(payload)}, this::handleStringResponse);
}

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

Expand Down
21 changes: 11 additions & 10 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.GlideString.gs;
import static glide.api.models.commands.function.FunctionListOptions.LIBRARY_NAME_REDIS_API;
import static glide.api.models.commands.function.FunctionListOptions.WITH_CODE_REDIS_API;
import static glide.api.models.commands.function.FunctionLoadOptions.REPLACE;
Expand Down Expand Up @@ -45,6 +46,7 @@
import glide.api.commands.TransactionsClusterCommands;
import glide.api.models.ClusterTransaction;
import glide.api.models.ClusterValue;
import glide.api.models.GlideString;
import glide.api.models.commands.FlushMode;
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.function.FunctionRestorePolicy;
Expand All @@ -55,7 +57,6 @@
import glide.managers.ConnectionManager;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -583,49 +584,49 @@ public CompletableFuture<String> functionDelete(@NonNull String libName, @NonNul
public CompletableFuture<ClusterValue<byte[]>> functionDump() {
return commandManager.submitNewCommand(
FunctionDump,
List.of(),
response -> ClusterValue.ofMultiValue(handleBinaryMapResponse(response)));
new GlideString[] {},
response -> ClusterValue.ofMultiValueBinary(handleBinaryMapResponse(response)));
}

@Override
public CompletableFuture<ClusterValue<byte[]>> functionDump(@NonNull Route route) {
return commandManager.submitNewCommand(
FunctionDump,
List.of(),
new GlideString[] {},
route,
response ->
route instanceof SingleNodeRoute
? ClusterValue.ofSingleValue(handleBytesResponse(response))
: ClusterValue.ofMultiValue(handleBinaryMapResponse(response)));
? ClusterValue.ofSingleValue(handleBytesResponse(response).getBytes())
: ClusterValue.ofMultiValueBinary(handleBinaryMapResponse(response)));
}

@Override
public CompletableFuture<String> functionRestore(byte @NonNull [] payload) {
return commandManager.submitNewCommand(
FunctionRestore, List.of(payload), this::handleStringResponse);
FunctionRestore, new GlideString[] {gs(payload)}, this::handleStringResponse);
}

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

@Override
public CompletableFuture<String> functionRestore(byte @NonNull [] payload, @NonNull Route route) {
return commandManager.submitNewCommand(
FunctionRestore, List.of(payload), route, this::handleStringResponse);
FunctionRestore, new GlideString[] {gs(payload)}, route, this::handleStringResponse);
}

@Override
public CompletableFuture<String> functionRestore(
byte @NonNull [] payload, @NonNull FunctionRestorePolicy policy, @NonNull Route route) {
return commandManager.submitNewCommand(
FunctionRestore,
List.of(payload, policy.toString().getBytes()),
new GlideString[] {gs(payload), gs(policy.toString().getBytes())},
route,
this::handleStringResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import glide.api.models.ClusterValue;
import glide.api.models.commands.FlushMode;
import glide.api.models.configuration.ReadFrom;
import glide.api.models.commands.function.FunctionRestorePolicy;
import glide.api.models.configuration.ReadFrom;
import glide.api.models.configuration.RequestRoutingConfiguration.Route;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
package glide.api.commands;

import glide.api.models.commands.FlushMode;
import glide.api.models.configuration.ReadFrom;
import glide.api.models.commands.function.FunctionRestorePolicy;
import glide.api.models.configuration.ReadFrom;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

Expand Down
12 changes: 12 additions & 0 deletions java/client/src/main/java/glide/api/models/ClusterValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import glide.api.models.configuration.RequestRoutingConfiguration.Route;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Represents a returned value object from a Redis server with cluster-mode enabled. The response
Expand Down Expand Up @@ -68,6 +69,17 @@ public static <T> ClusterValue<T> ofMultiValue(Map<String, T> data) {
return res;
}

/** A constructor for the value. */
public static <T> ClusterValue<T> ofMultiValueBinary(Map<GlideString, T> data) {
var res = new ClusterValue<T>();
// the map node address can be converted to a string
Map<String, T> multiValue =
data.entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey().getString(), Map.Entry::getValue));
res.multiValue = multiValue;
return res;
}

/**
* Check that multi-value is stored in this object. Should be called prior to {@link
* #getMultiValue()}.
Expand Down
13 changes: 7 additions & 6 deletions java/client/src/test/java/glide/api/RedisClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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.commands.StringBaseCommands.LEN_REDIS_API;
import static glide.api.models.GlideString.gs;
import static glide.api.models.commands.FlushMode.ASYNC;
import static glide.api.models.commands.FlushMode.SYNC;
import static glide.api.models.commands.LInsertOptions.InsertPosition.BEFORE;
Expand Down Expand Up @@ -229,6 +230,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.ZUnion;
import static redis_request.RedisRequestOuterClass.RequestType.ZUnionStore;

import glide.api.models.GlideString;
import glide.api.models.Script;
import glide.api.models.Transaction;
import glide.api.models.commands.ConditionalChange;
Expand Down Expand Up @@ -5731,7 +5733,7 @@ public void functionDump_returns_success() {
testResponse.complete(value);

// match on protobuf request
when(commandManager.<byte[]>submitNewCommand(eq(FunctionDump), eq(List.of()), any()))
when(commandManager.<byte[]>submitNewCommand(eq(FunctionDump), eq(new GlideString[0]), any()))
.thenReturn(testResponse);

// exercise
Expand All @@ -5748,13 +5750,12 @@ public void functionDump_returns_success() {
public void functionRestore_returns_success() {
// setup
byte[] data = new byte[] {42};
List<byte[]> args = List.of(data);
CompletableFuture<String> testResponse = new CompletableFuture<>();
testResponse.complete(OK);

// match on protobuf request
// TODO maybe use ByteArrayArgumentMatcher
when(commandManager.<String>submitNewCommand(eq(FunctionRestore), any(List.class), any()))
when(commandManager.<String>submitNewCommand(
eq(FunctionRestore), eq(new GlideString[] {gs(data)}), any()))
.thenReturn(testResponse);

// exercise
Expand All @@ -5771,12 +5772,12 @@ public void functionRestore_returns_success() {
public void functionRestore_with_policy_returns_success() {
// setup
byte[] data = new byte[] {42};
List<byte[]> args = List.of(data, FunctionRestorePolicy.FLUSH.toString().getBytes());
GlideString[] args = {gs(data), gs(FunctionRestorePolicy.FLUSH.toString().getBytes())};
CompletableFuture<String> testResponse = new CompletableFuture<>();
testResponse.complete(OK);

// match on protobuf request
when(commandManager.<String>submitNewCommand(eq(FunctionRestore), any(List.class), any()))
when(commandManager.<String>submitNewCommand(eq(FunctionRestore), eq(args), any()))
.thenReturn(testResponse);

// exercise
Expand Down
39 changes: 19 additions & 20 deletions java/client/src/test/java/glide/api/RedisClusterClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import static glide.api.BaseClient.OK;
import static glide.api.commands.ServerManagementCommands.VERSION_REDIS_API;
import static glide.api.models.GlideString.gs;
import static glide.api.models.commands.FlushMode.ASYNC;
import static glide.api.models.commands.FlushMode.SYNC;
import static glide.api.models.commands.function.FunctionListOptions.LIBRARY_NAME_REDIS_API;
Expand Down Expand Up @@ -48,6 +49,7 @@

import glide.api.models.ClusterTransaction;
import glide.api.models.ClusterValue;
import glide.api.models.GlideString;
import glide.api.models.commands.FlushMode;
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.function.FunctionLoadOptions;
Expand All @@ -58,7 +60,6 @@
import glide.managers.ConnectionManager;
import glide.managers.RedisExceptionCheckedFunction;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -1877,8 +1878,8 @@ public void functionDump_returns_success() {

// match on protobuf request
when(commandManager.<ClusterValue<byte[]>>submitNewCommand(
eq(FunctionDump), eq(List.of()), any()))
.thenReturn(testResponse);
eq(FunctionDump), eq(new GlideString[0]), any()))
.thenReturn(testResponse);

// exercise
CompletableFuture<ClusterValue<byte[]>> response = service.functionDump();
Expand All @@ -1899,8 +1900,8 @@ public void functionDump_with_route_returns_success() {

// match on protobuf request
when(commandManager.<ClusterValue<byte[]>>submitNewCommand(
eq(FunctionDump), eq(List.of()), eq(RANDOM), any()))
.thenReturn(testResponse);
eq(FunctionDump), eq(new GlideString[0]), eq(RANDOM), any()))
.thenReturn(testResponse);

// exercise
CompletableFuture<ClusterValue<byte[]>> response = service.functionDump(RANDOM);
Expand All @@ -1916,14 +1917,14 @@ public void functionDump_with_route_returns_success() {
public void functionRestore_returns_success() {
// setup
byte[] data = new byte[] {42};
List<byte[]> args = List.of(data);
GlideString[] args = {gs(data)};
CompletableFuture<String> testResponse = new CompletableFuture<>();
testResponse.complete(OK);

// match on protobuf request
// TODO maybe use ByteArrayArgumentMatcher
when(commandManager.<String>submitNewCommand(eq(FunctionRestore), any(List.class), any()))
.thenReturn(testResponse);
when(commandManager.<String>submitNewCommand(eq(FunctionRestore), eq(args), any()))
.thenReturn(testResponse);

// exercise
CompletableFuture<String> response = service.functionRestore(data);
Expand All @@ -1939,13 +1940,13 @@ public void functionRestore_returns_success() {
public void functionRestore_with_policy_returns_success() {
// setup
byte[] data = new byte[] {42};
List<byte[]> args = List.of(data, FunctionRestorePolicy.FLUSH.toString().getBytes());
GlideString[] args = {gs(data), gs(FunctionRestorePolicy.FLUSH.toString().getBytes())};
CompletableFuture<String> testResponse = new CompletableFuture<>();
testResponse.complete(OK);

// match on protobuf request
when(commandManager.<String>submitNewCommand(eq(FunctionRestore), any(List.class), any()))
.thenReturn(testResponse);
when(commandManager.<String>submitNewCommand(eq(FunctionRestore), eq(args), any()))
.thenReturn(testResponse);

// exercise
CompletableFuture<String> response = service.functionRestore(data, FunctionRestorePolicy.FLUSH);
Expand All @@ -1961,15 +1962,14 @@ public void functionRestore_with_policy_returns_success() {
public void functionRestore_with_route_returns_success() {
// setup
byte[] data = new byte[] {42};
List<byte[]> args = List.of(data);
GlideString[] args = {gs(data)};
CompletableFuture<String> testResponse = new CompletableFuture<>();
testResponse.complete(OK);

// match on protobuf request
// TODO maybe use ByteArrayArgumentMatcher
when(commandManager.<String>submitNewCommand(
eq(FunctionRestore), any(List.class), eq(RANDOM), any()))
.thenReturn(testResponse);
when(commandManager.<String>submitNewCommand(eq(FunctionRestore), eq(args), eq(RANDOM), any()))
.thenReturn(testResponse);

// exercise
CompletableFuture<String> response = service.functionRestore(data, RANDOM);
Expand All @@ -1985,18 +1985,17 @@ public void functionRestore_with_route_returns_success() {
public void functionRestore_with_policy_and_route_returns_success() {
// setup
byte[] data = new byte[] {42};
List<byte[]> args = List.of(data, FunctionRestorePolicy.FLUSH.toString().getBytes());
GlideString[] args = {gs(data), gs(FunctionRestorePolicy.FLUSH.toString().getBytes())};
CompletableFuture<String> testResponse = new CompletableFuture<>();
testResponse.complete(OK);

// match on protobuf request
when(commandManager.<String>submitNewCommand(
eq(FunctionRestore), any(List.class), eq(RANDOM), any()))
.thenReturn(testResponse);
when(commandManager.<String>submitNewCommand(eq(FunctionRestore), eq(args), eq(RANDOM), any()))
.thenReturn(testResponse);

// exercise
CompletableFuture<String> response =
service.functionRestore(data, FunctionRestorePolicy.FLUSH, RANDOM);
service.functionRestore(data, FunctionRestorePolicy.FLUSH, RANDOM);
String payload = response.get();

// verify
Expand Down
Loading

0 comments on commit c05ceae

Please sign in to comment.