Skip to content

Commit

Permalink
Rework
Browse files Browse the repository at this point in the history
Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand committed Jan 22, 2024
1 parent 6cbc27f commit 2b05632
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
15 changes: 4 additions & 11 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@
import glide.managers.CommandManager;
import glide.managers.ConnectionManager;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import lombok.AllArgsConstructor;
import response.ResponseOuterClass.Response;

/**
* Base Client class for Redis
*
* @param <T> The data return type.
*/
/** Base Client class for Redis */
@AllArgsConstructor
public abstract class BaseClient<T> implements AutoCloseable {
public abstract class BaseClient implements AutoCloseable {

protected final ConnectionManager connectionManager;
protected final CommandManager commandManager;
protected final Function<Object, T> dataConverter;

/**
* Extracts the response from the Protobuf response and either throws an exception or returns the
Expand All @@ -28,10 +22,9 @@ public abstract class BaseClient<T> implements AutoCloseable {
* @param response Redis protobuf message
* @return Response Object
*/
protected T handleObjectResponse(Response response) {
protected Object handleObjectResponse(Response response) {
// convert protobuf response into Object and then Object into T
return dataConverter.apply(
new BaseCommandResponseResolver(RedisValueResolver::valueFromPointer).apply(response));
return new BaseCommandResponseResolver(RedisValueResolver::valueFromPointer).apply(response);
}

/**
Expand Down
15 changes: 8 additions & 7 deletions java/client/src/main/java/glide/api/ClusterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
* Async (non-blocking) client for Redis in Cluster mode. Use {@link #CreateClient} to request a
* client to Redis.
*/
public class ClusterClient extends BaseClient<RedisValue>
implements ClusterBaseCommands<RedisValue> {
public class ClusterClient extends BaseClient implements ClusterBaseCommands<RedisValue<Object>> {

protected ClusterClient(ConnectionManager connectionManager, CommandManager commandManager) {
super(connectionManager, commandManager, RedisValue::of);
super(connectionManager, commandManager);
}

/**
Expand All @@ -43,16 +42,18 @@ public static CompletableFuture<ClusterClient> CreateClient(
}

@Override
public CompletableFuture<RedisValue> customCommand(String[] args) {
public CompletableFuture<RedisValue<Object>> customCommand(String[] args) {
Command command =
Command.builder().requestType(Command.RequestType.CUSTOM_COMMAND).arguments(args).build();
return commandManager.submitNewCommand(command, this::handleObjectResponse);
return commandManager.submitNewCommand(
command, obj -> RedisValue.of(handleObjectResponse(obj)));
}

@Override
public CompletableFuture<RedisValue> customCommand(String[] args, Route route) {
public CompletableFuture<RedisValue<Object>> customCommand(String[] args, Route route) {
Command command =
Command.builder().requestType(Command.RequestType.CUSTOM_COMMAND).arguments(args).build();
return commandManager.submitNewCommand(command, this::handleObjectResponse, route);
return commandManager.submitNewCommand(
command, obj -> RedisValue.of(handleObjectResponse(obj)), route);
}
}
4 changes: 2 additions & 2 deletions java/client/src/main/java/glide/api/RedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
* Async (non-blocking) client for Redis in Standalone mode. Use {@link #CreateClient} to request a
* client to Redis.
*/
public class RedisClient extends BaseClient<Object> implements BaseCommands<Object> {
public class RedisClient extends BaseClient implements BaseCommands<Object> {

protected RedisClient(ConnectionManager connectionManager, CommandManager commandManager) {
super(connectionManager, commandManager, obj -> obj);
super(connectionManager, commandManager);
}

/**
Expand Down
20 changes: 12 additions & 8 deletions java/client/src/main/java/glide/api/models/RedisValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@
import java.util.Map;
import lombok.Getter;

/** A union-like type which can store single or bulk value retrieved from Redis. */
/**
* A union-like type which can store single or bulk value retrieved from Redis.
*
* @param <T> The wrapped data type
*/
@Getter
public class RedisValue {
public class RedisValue<T> {
/** Get per-node value. */
private Map<String, Object> multiValue = null;
private Map<String, T> multiValue = null;

/** Get the single value. */
private Object singleValue = null;
private T singleValue = null;

private RedisValue() {}

@SuppressWarnings("unchecked")
public static RedisValue of(Object data) {
var res = new RedisValue();
public static <T> RedisValue<T> of(Object data) {
var res = new RedisValue<T>();
if (data instanceof Map) {
res.multiValue = (Map<String, Object>) data;
res.multiValue = (Map<String, T>) data;
} else {
res.singleValue = data;
res.singleValue = (T) data;
}
return res;
}
Expand Down

0 comments on commit 2b05632

Please sign in to comment.