Skip to content

Commit df8b07d

Browse files
committed
Merge branch 'java/integ_acarbo_api_getsetinfoping' into java/dev_acarbo_api_getsetinfoping
2 parents 285665c + a43e31f commit df8b07d

File tree

11 files changed

+218
-229
lines changed

11 files changed

+218
-229
lines changed

java/client/src/main/java/glide/api/BaseClient.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package glide.api;
22

3+
import glide.api.models.exceptions.RedisException;
4+
import glide.ffi.resolvers.RedisValueResolver;
5+
import glide.managers.BaseCommandResponseResolver;
36
import glide.managers.CommandManager;
47
import glide.managers.ConnectionManager;
8+
import java.util.HashMap;
59
import java.util.concurrent.ExecutionException;
610
import lombok.AllArgsConstructor;
11+
import response.ResponseOuterClass.Response;
712

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

20+
/**
21+
* Extracts the response from the Protobuf response and either throws an exception or returns the
22+
* appropriate response as an Object
23+
*
24+
* @param response Redis protobuf message
25+
* @return Response Object
26+
*/
27+
protected static Object handleObjectResponse(Response response) {
28+
// return function to convert protobuf.Response into the response object by
29+
// calling valueFromPointer
30+
return (new BaseCommandResponseResolver(RedisValueResolver::valueFromPointer)).apply(response);
31+
}
32+
33+
/**
34+
* Check for errors in the Response and return null Throws an error if an unexpected value is
35+
* returned
36+
*
37+
* @return null if the response is empty
38+
*/
39+
protected static Void handleVoidResponse(Response response) {
40+
Object value = handleObjectResponse(response);
41+
if (value == null) {
42+
return null;
43+
}
44+
throw new RedisException(
45+
"Unexpected return type from Redis: got "
46+
+ value.getClass().getSimpleName()
47+
+ " expected null");
48+
}
49+
50+
/**
51+
* Extracts the response value from the Redis response and either throws an exception or returns
52+
* the value as a String.
53+
*
54+
* @param response Redis protobuf message
55+
* @return Response as a String
56+
*/
57+
protected static String handleStringResponse(Response response) {
58+
Object value = handleObjectResponse(response);
59+
if (value instanceof String) {
60+
return (String) value;
61+
}
62+
throw new RedisException(
63+
"Unexpected return type from Redis: got "
64+
+ value.getClass().getSimpleName()
65+
+ " expected String");
66+
}
67+
68+
/**
69+
* Extracts the response value from the Redis response and either throws an exception or returns
70+
* the * value as a HashMap
71+
*
72+
* @param response Redis protobuf message
73+
* @return Response as a String
74+
*/
75+
protected static HashMap<String, Object> handleMapResponse(Response response) {
76+
Object value = handleObjectResponse(response);
77+
if (value instanceof HashMap) {
78+
return (HashMap<String, Object>) value;
79+
}
80+
throw new RedisException(
81+
"Unexpected return type from Redis: got "
82+
+ value.getClass().getSimpleName()
83+
+ " expected HashMap");
84+
}
85+
1586
/**
1687
* Closes this resource, relinquishing any underlying resources. This method is invoked
1788
* automatically on objects managed by the try-with-resources statement.

java/client/src/main/java/glide/api/RedisClient.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55
import glide.api.commands.BaseCommands;
66
import glide.api.commands.ConnectionCommands;
77
import glide.api.commands.GenericCommands;
8-
import glide.api.commands.ResponseHandlers;
98
import glide.api.commands.ServerCommands;
109
import glide.api.commands.StringCommands;
11-
import glide.api.models.Command;
1210
import glide.api.models.commands.InfoOptions;
1311
import glide.api.models.commands.SetOptions;
1412
import glide.api.models.configuration.RedisClientConfiguration;
1513
import glide.connectors.handlers.CallbackDispatcher;
1614
import glide.connectors.handlers.ChannelHandler;
1715
import glide.managers.CommandManager;
1816
import glide.managers.ConnectionManager;
17+
import glide.managers.models.Command;
1918
import java.util.List;
2019
import java.util.Map;
2120
import java.util.concurrent.CompletableFuture;
@@ -79,7 +78,7 @@ protected RedisClient(ConnectionManager connectionManager, CommandManager comman
7978
public CompletableFuture<Object> customCommand(String[] args) {
8079
Command command =
8180
Command.builder().requestType(Command.RequestType.CUSTOM_COMMAND).arguments(args).build();
82-
return commandManager.submitNewCommand(command, ResponseHandlers::handleObjectResponse);
81+
return commandManager.submitNewCommand(command, BaseClient::handleObjectResponse);
8382
}
8483

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

9796
/**
@@ -108,7 +107,7 @@ public CompletableFuture<String> ping(String msg) {
108107
.requestType(Command.RequestType.PING)
109108
.arguments(new String[] {msg})
110109
.build();
111-
return commandManager.submitNewCommand(command, ResponseHandlers::handleStringResponse);
110+
return commandManager.submitNewCommand(command, BaseClient::handleStringResponse);
112111
}
113112

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

126125
/**
@@ -138,7 +137,7 @@ public CompletableFuture<Map> info(InfoOptions options) {
138137
.requestType(Command.RequestType.INFO)
139138
.arguments(options.toInfoOptions())
140139
.build();
141-
return commandManager.submitNewCommand(command, ResponseHandlers::handleMapResponse);
140+
return commandManager.submitNewCommand(command, BaseClient::handleMapResponse);
142141
}
143142

144143
/**
@@ -155,7 +154,7 @@ public CompletableFuture<String> get(String key) {
155154
.requestType(Command.RequestType.GET_STRING)
156155
.arguments(new String[] {key})
157156
.build();
158-
return commandManager.submitNewCommand(command, ResponseHandlers::handleStringResponse);
157+
return commandManager.submitNewCommand(command, BaseClient::handleStringResponse);
159158
}
160159

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

179178
/**
@@ -193,6 +192,6 @@ public CompletableFuture<String> set(String key, String value, SetOptions option
193192
.requestType(Command.RequestType.SET_STRING)
194193
.arguments(options.toSetOptions(List.of(key, value)))
195194
.build();
196-
return commandManager.submitNewCommand(command, ResponseHandlers::handleStringResponse);
195+
return commandManager.submitNewCommand(command, BaseClient::handleStringResponse);
197196
}
198197
}

java/client/src/main/java/glide/api/commands/BaseCommands.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@
55
/** Base Commands interface to handle generic command and transaction requests. */
66
public interface BaseCommands {
77

8+
/**
9+
* Execute a @see{Command} by sending command via socket manager
10+
*
11+
* @param args arguments for the custom command
12+
* @return a CompletableFuture with response result from Redis
13+
*/
814
CompletableFuture<Object> customCommand(String[] args);
915
}

java/client/src/main/java/glide/api/commands/ResponseHandlers.java

Lines changed: 0 additions & 78 deletions
This file was deleted.

java/client/src/main/java/glide/api/models/commands/SetOptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public enum TimeToLiveType {
9191

9292
/**
9393
* Converts SetOptions into a String[] to add to a {@link glide.api.models.Command}
94+
*
9495
* @param arguments
9596
* @return
9697
*/

java/client/src/main/java/glide/connectors/handlers/ChannelHandler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,15 @@ public ChannelHandler(
7070
* @param flush True to flush immediately
7171
* @return A response promise
7272
*/
73-
public CompletableFuture<Response> write(RedisRequest.Builder request, boolean flush) {
73+
public CompletableFuture<Response> write(RedisRequest request, boolean flush) {
7474
var commandId = callbackDispatcher.registerRequest();
75-
request.setCallbackIdx(commandId.getKey());
75+
RedisRequest requestWithCallbackIdx =
76+
RedisRequest.newBuilder(request).setCallbackIdx(commandId.getKey()).build();
7677

7778
if (flush) {
78-
channel.writeAndFlush(request.build());
79+
channel.writeAndFlush(requestWithCallbackIdx);
7980
} else {
80-
channel.write(request.build());
81+
channel.write(requestWithCallbackIdx);
8182
}
8283
return commandId.getValue();
8384
}

java/client/src/main/java/glide/managers/BaseCommandResponseResolver.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public class BaseCommandResponseResolver
2626
* @return A generic Object with the Response | null if the response is empty
2727
*/
2828
public Object apply(Response response) throws RedisException {
29-
// TODO: handle object if the object is small
30-
// TODO: handle RESP2 object if configuration is set
3129
if (response.hasRequestError()) {
3230
RequestError error = response.getRequestError();
3331
String msg = error.getMessage();

java/client/src/main/java/glide/managers/CommandManager.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package glide.managers;
22

3-
import glide.api.models.Command;
43
import glide.connectors.handlers.ChannelHandler;
4+
import glide.managers.models.Command;
55
import java.util.concurrent.CompletableFuture;
66
import lombok.RequiredArgsConstructor;
77
import redis_request.RedisRequestOuterClass;
8+
import redis_request.RedisRequestOuterClass.RequestType;
89
import response.ResponseOuterClass.Response;
910

1011
/**
@@ -42,7 +43,7 @@ public <T> CompletableFuture<T> submitNewCommand(
4243
* @return An uncompleted request. CallbackDispatcher is responsible to complete it by adding a
4344
* callback id.
4445
*/
45-
private RedisRequestOuterClass.RedisRequest.Builder prepareRedisRequest(
46+
private RedisRequestOuterClass.RedisRequest prepareRedisRequest(
4647
Command.RequestType command, String[] args) {
4748
RedisRequestOuterClass.Command.ArgsArray.Builder commandArgs =
4849
RedisRequestOuterClass.Command.ArgsArray.newBuilder();
@@ -59,13 +60,22 @@ private RedisRequestOuterClass.RedisRequest.Builder prepareRedisRequest(
5960
.setRoute(
6061
RedisRequestOuterClass.Routes.newBuilder()
6162
.setSimpleRoutes(RedisRequestOuterClass.SimpleRoutes.AllNodes)
62-
.build());
63+
.build())
64+
.build();
6365
}
6466

65-
private RedisRequestOuterClass.RequestType mapRequestTypes(Command.RequestType inType) {
67+
private RequestType mapRequestTypes(Command.RequestType inType) {
6668
switch (inType) {
6769
case CUSTOM_COMMAND:
68-
return RedisRequestOuterClass.RequestType.CustomCommand;
70+
return RequestType.CustomCommand;
71+
case PING:
72+
return RequestType.Ping;
73+
case INFO:
74+
return RequestType.Info;
75+
case GET_STRING:
76+
return RequestType.GetString;
77+
case SET_STRING:
78+
return RequestType.SetString;
6979
}
7080
throw new RuntimeException("Unsupported request type");
7181
}

0 commit comments

Comments
 (0)