Skip to content

Commit

Permalink
Clean up for rebase
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Carbonetto <[email protected]>
  • Loading branch information
acarbonetto committed Jan 25, 2024
1 parent d8655f3 commit 926d04e
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 144 deletions.
55 changes: 55 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,11 @@
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;
Expand All @@ -28,6 +30,59 @@ protected static Object handleObjectResponse(Response response) {
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
25 changes: 17 additions & 8 deletions java/client/src/main/java/glide/api/RedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@
import static glide.ffi.resolvers.SocketListenerResolver.getSocket;

import glide.api.commands.BaseCommands;
import glide.api.commands.ConnectionCommands;
import glide.api.commands.GenericCommands;
import glide.api.commands.ServerCommands;
import glide.api.commands.StringCommands;
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;

/**
* Async (non-blocking) client for Redis in Standalone mode. Use {@link
* #CreateClient(RedisClientConfiguration)} to request a client to Redis.
*/
public class RedisClient extends BaseClient implements BaseCommands {
public class RedisClient extends BaseClient
implements BaseCommands, GenericCommands, ConnectionCommands, ServerCommands, StringCommands {

/**
* Request an async (non-blocking) Redis client in Standalone mode.
Expand Down Expand Up @@ -88,7 +97,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 @@ -105,7 +114,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 @@ -117,7 +126,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 @@ -135,7 +144,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 @@ -152,7 +161,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 @@ -170,7 +179,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 @@ -190,6 +199,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);
}
}
78 changes: 0 additions & 78 deletions java/client/src/main/java/glide/api/commands/ResponseHandlers.java

This file was deleted.

57 changes: 0 additions & 57 deletions java/client/src/main/java/glide/api/models/Command.java

This file was deleted.

8 changes: 8 additions & 0 deletions java/client/src/main/java/glide/managers/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ private RequestType mapRequestTypes(Command.RequestType inType) {
switch (inType) {
case CUSTOM_COMMAND:
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
24 changes: 24 additions & 0 deletions java/client/src/main/java/glide/managers/models/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,29 @@ public class Command {
public enum RequestType {
/** Call a custom command with list of string arguments */
CUSTOM_COMMAND,
/**
* Ping the Redis server.
*
* @see <hred=https://redis.io/commands/ping/>command reference</a>
*/
PING,
/**
* Get information and statistics about the Redis server.
*
* @see <hred=https://redis.io/commands/info/>command reference</a>
*/
INFO,
/**
* Get the value of key.
*
* @see: <href=https://redis.io/commands/get/>command reference</a>
*/
GET_STRING,
/**
* Set key to hold the string value.
*
* @see: <href=https://redis.io/commands/set/>command reference</a>
*/
SET_STRING,
}
}
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 @@ -14,11 +14,11 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import glide.api.models.Command;
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.SetOptions;
import glide.managers.CommandManager;
import glide.managers.ConnectionManager;
import glide.managers.models.Command;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand Down

0 comments on commit 926d04e

Please sign in to comment.