Skip to content

Commit

Permalink
Merge branch 'java/integ_acarbo_api_getsetinfoping' into java/dev_aca…
Browse files Browse the repository at this point in the history
…rbo_api_getsetinfoping
  • Loading branch information
acarbonetto committed Jan 23, 2024
2 parents 285665c + a43e31f commit df8b07d
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 229 deletions.
71 changes: 71 additions & 0 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package glide.api;

import glide.api.models.exceptions.RedisException;
import glide.ffi.resolvers.RedisValueResolver;
import glide.managers.BaseCommandResponseResolver;
import glide.managers.CommandManager;
import glide.managers.ConnectionManager;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;
import lombok.AllArgsConstructor;
import response.ResponseOuterClass.Response;

/** Base Client class for Redis */
@AllArgsConstructor
Expand All @@ -12,6 +17,72 @@ public abstract class BaseClient implements AutoCloseable {
protected ConnectionManager connectionManager;
protected CommandManager commandManager;

/**
* Extracts the response from the Protobuf response and either throws an exception or returns the
* appropriate response as an Object
*
* @param response Redis protobuf message
* @return Response Object
*/
protected static Object handleObjectResponse(Response response) {
// return function to convert protobuf.Response into the response object by
// calling valueFromPointer
return (new BaseCommandResponseResolver(RedisValueResolver::valueFromPointer)).apply(response);
}

/**
* Check for errors in the Response and return null Throws an error if an unexpected value is
* returned
*
* @return null if the response is empty
*/
protected static Void handleVoidResponse(Response response) {
Object value = handleObjectResponse(response);
if (value == null) {
return null;
}
throw new RedisException(
"Unexpected return type from Redis: got "
+ value.getClass().getSimpleName()
+ " expected null");
}

/**
* Extracts the response value from the Redis response and either throws an exception or returns
* the value as a String.
*
* @param response Redis protobuf message
* @return Response as a String
*/
protected static String handleStringResponse(Response response) {
Object value = handleObjectResponse(response);
if (value instanceof String) {
return (String) value;
}
throw new RedisException(
"Unexpected return type from Redis: got "
+ value.getClass().getSimpleName()
+ " expected String");
}

/**
* Extracts the response value from the Redis response and either throws an exception or returns
* the * value as a HashMap
*
* @param response Redis protobuf message
* @return Response as a String
*/
protected static HashMap<String, Object> handleMapResponse(Response response) {
Object value = handleObjectResponse(response);
if (value instanceof HashMap) {
return (HashMap<String, Object>) value;
}
throw new RedisException(
"Unexpected return type from Redis: got "
+ value.getClass().getSimpleName()
+ " expected HashMap");
}

/**
* Closes this resource, relinquishing any underlying resources. This method is invoked
* automatically on objects managed by the try-with-resources statement.
Expand Down
19 changes: 9 additions & 10 deletions java/client/src/main/java/glide/api/RedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
import glide.api.commands.BaseCommands;
import glide.api.commands.ConnectionCommands;
import glide.api.commands.GenericCommands;
import glide.api.commands.ResponseHandlers;
import glide.api.commands.ServerCommands;
import glide.api.commands.StringCommands;
import glide.api.models.Command;
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.SetOptions;
import glide.api.models.configuration.RedisClientConfiguration;
import glide.connectors.handlers.CallbackDispatcher;
import glide.connectors.handlers.ChannelHandler;
import glide.managers.CommandManager;
import glide.managers.ConnectionManager;
import glide.managers.models.Command;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -79,7 +78,7 @@ protected RedisClient(ConnectionManager connectionManager, CommandManager comman
public CompletableFuture<Object> customCommand(String[] args) {
Command command =
Command.builder().requestType(Command.RequestType.CUSTOM_COMMAND).arguments(args).build();
return commandManager.submitNewCommand(command, ResponseHandlers::handleObjectResponse);
return commandManager.submitNewCommand(command, BaseClient::handleObjectResponse);
}

/**
Expand All @@ -91,7 +90,7 @@ public CompletableFuture<Object> customCommand(String[] args) {
@Override
public CompletableFuture<String> ping() {
Command command = Command.builder().requestType(Command.RequestType.PING).build();
return commandManager.submitNewCommand(command, ResponseHandlers::handleStringResponse);
return commandManager.submitNewCommand(command, BaseClient::handleStringResponse);
}

/**
Expand All @@ -108,7 +107,7 @@ public CompletableFuture<String> ping(String msg) {
.requestType(Command.RequestType.PING)
.arguments(new String[] {msg})
.build();
return commandManager.submitNewCommand(command, ResponseHandlers::handleStringResponse);
return commandManager.submitNewCommand(command, BaseClient::handleStringResponse);
}

/**
Expand All @@ -120,7 +119,7 @@ public CompletableFuture<String> ping(String msg) {
@Override
public CompletableFuture<Map> info() {
Command command = Command.builder().requestType(Command.RequestType.INFO).build();
return commandManager.submitNewCommand(command, ResponseHandlers::handleMapResponse);
return commandManager.submitNewCommand(command, BaseClient::handleMapResponse);
}

/**
Expand All @@ -138,7 +137,7 @@ public CompletableFuture<Map> info(InfoOptions options) {
.requestType(Command.RequestType.INFO)
.arguments(options.toInfoOptions())
.build();
return commandManager.submitNewCommand(command, ResponseHandlers::handleMapResponse);
return commandManager.submitNewCommand(command, BaseClient::handleMapResponse);
}

/**
Expand All @@ -155,7 +154,7 @@ public CompletableFuture<String> get(String key) {
.requestType(Command.RequestType.GET_STRING)
.arguments(new String[] {key})
.build();
return commandManager.submitNewCommand(command, ResponseHandlers::handleStringResponse);
return commandManager.submitNewCommand(command, BaseClient::handleStringResponse);
}

/**
Expand All @@ -173,7 +172,7 @@ public CompletableFuture<Void> set(String key, String value) {
.requestType(Command.RequestType.SET_STRING)
.arguments(new String[] {key, value})
.build();
return commandManager.submitNewCommand(command, ResponseHandlers::handleVoidResponse);
return commandManager.submitNewCommand(command, BaseClient::handleVoidResponse);
}

/**
Expand All @@ -193,6 +192,6 @@ public CompletableFuture<String> set(String key, String value, SetOptions option
.requestType(Command.RequestType.SET_STRING)
.arguments(options.toSetOptions(List.of(key, value)))
.build();
return commandManager.submitNewCommand(command, ResponseHandlers::handleStringResponse);
return commandManager.submitNewCommand(command, BaseClient::handleStringResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@
/** Base Commands interface to handle generic command and transaction requests. */
public interface BaseCommands {

/**
* Execute a @see{Command} by sending command via socket manager
*
* @param args arguments for the custom command
* @return a CompletableFuture with response result from Redis
*/
CompletableFuture<Object> customCommand(String[] args);
}
78 changes: 0 additions & 78 deletions java/client/src/main/java/glide/api/commands/ResponseHandlers.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public enum TimeToLiveType {

/**
* Converts SetOptions into a String[] to add to a {@link glide.api.models.Command}
*
* @param arguments
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ public ChannelHandler(
* @param flush True to flush immediately
* @return A response promise
*/
public CompletableFuture<Response> write(RedisRequest.Builder request, boolean flush) {
public CompletableFuture<Response> write(RedisRequest request, boolean flush) {
var commandId = callbackDispatcher.registerRequest();
request.setCallbackIdx(commandId.getKey());
RedisRequest requestWithCallbackIdx =
RedisRequest.newBuilder(request).setCallbackIdx(commandId.getKey()).build();

if (flush) {
channel.writeAndFlush(request.build());
channel.writeAndFlush(requestWithCallbackIdx);
} else {
channel.write(request.build());
channel.write(requestWithCallbackIdx);
}
return commandId.getValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class BaseCommandResponseResolver
* @return A generic Object with the Response | null if the response is empty
*/
public Object apply(Response response) throws RedisException {
// TODO: handle object if the object is small
// TODO: handle RESP2 object if configuration is set
if (response.hasRequestError()) {
RequestError error = response.getRequestError();
String msg = error.getMessage();
Expand Down
20 changes: 15 additions & 5 deletions java/client/src/main/java/glide/managers/CommandManager.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package glide.managers;

import glide.api.models.Command;
import glide.connectors.handlers.ChannelHandler;
import glide.managers.models.Command;
import java.util.concurrent.CompletableFuture;
import lombok.RequiredArgsConstructor;
import redis_request.RedisRequestOuterClass;
import redis_request.RedisRequestOuterClass.RequestType;
import response.ResponseOuterClass.Response;

/**
Expand Down Expand Up @@ -42,7 +43,7 @@ public <T> CompletableFuture<T> submitNewCommand(
* @return An uncompleted request. CallbackDispatcher is responsible to complete it by adding a
* callback id.
*/
private RedisRequestOuterClass.RedisRequest.Builder prepareRedisRequest(
private RedisRequestOuterClass.RedisRequest prepareRedisRequest(
Command.RequestType command, String[] args) {
RedisRequestOuterClass.Command.ArgsArray.Builder commandArgs =
RedisRequestOuterClass.Command.ArgsArray.newBuilder();
Expand All @@ -59,13 +60,22 @@ private RedisRequestOuterClass.RedisRequest.Builder prepareRedisRequest(
.setRoute(
RedisRequestOuterClass.Routes.newBuilder()
.setSimpleRoutes(RedisRequestOuterClass.SimpleRoutes.AllNodes)
.build());
.build())
.build();
}

private RedisRequestOuterClass.RequestType mapRequestTypes(Command.RequestType inType) {
private RequestType mapRequestTypes(Command.RequestType inType) {
switch (inType) {
case CUSTOM_COMMAND:
return RedisRequestOuterClass.RequestType.CustomCommand;
return RequestType.CustomCommand;
case PING:
return RequestType.Ping;
case INFO:
return RequestType.Info;
case GET_STRING:
return RequestType.GetString;
case SET_STRING:
return RequestType.SetString;
}
throw new RuntimeException("Unsupported request type");
}
Expand Down
Loading

0 comments on commit df8b07d

Please sign in to comment.