Skip to content

Commit

Permalink
Added part of the 'String Commands' group to RedisClient as specified…
Browse files Browse the repository at this point in the history
… on the official Redis website.
  • Loading branch information
SanHalacogluImproving committed Jan 25, 2024
1 parent 21e0cbc commit 6340d2f
Show file tree
Hide file tree
Showing 6 changed files with 450 additions and 1 deletion.
54 changes: 54 additions & 0 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,60 @@ protected static String handleStringResponse(Response response) {
+ " expected String");
}

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

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

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

/**
* Extracts the response value from the Redis response and either throws an exception or returns
* the * value as a HashMap
Expand Down
140 changes: 140 additions & 0 deletions java/client/src/main/java/glide/api/RedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import glide.managers.CommandManager;
import glide.managers.ConnectionManager;
import glide.managers.models.Command;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -194,4 +196,142 @@ public CompletableFuture<String> set(String key, String value, SetOptions option
.build();
return commandManager.submitNewCommand(command, BaseClient::handleStringResponse);
}

/**
* Decrements the number stored at `key` by one. If `key` does not exist, it is set to 0 before
* performing the operation.
*
* @see <a href="https://redis.io/commands/decr/">redis.io</a> for details.
* @param key - The key to decrement its value.
* @return the value of `key` after the decrement. An error is raised if `key` contains a value of
* the wrong type or contains a string that can not be represented as integer.
*/
@Override
public CompletableFuture<Long> decr(String key) {
Command command =
Command.builder()
.requestType(Command.RequestType.DECR)
.arguments(new String[] {key})
.build();
return commandManager.submitNewCommand(command, BaseClient::handleLongResponse);
}

/**
* Decrements the number stored at `key` by `amount`. If `key` does not exist, it is set to 0
* before performing the operation.
*
* @see <a href="https://redis.io/commands/decrby/">redis.io</a> for details.
* @param key - The key to decrement its value.
* @param amount - The amount to decrement.
* @return the value of `key` after the decrement. An error is raised if `key` contains a value of
* the wrong type or contains a string that can not be represented as integer.
*/
@Override
public CompletableFuture<Long> decrBy(String key, long amount) {
Command command =
Command.builder()
.requestType(Command.RequestType.DECR_BY)
.arguments(new String[] {key, Long.toString(amount)})
.build();
return commandManager.submitNewCommand(command, BaseClient::handleLongResponse);
}

/**
* Increments the number stored at `key` by one. If `key` does not exist, it is set to 0 before
* performing the operation.
*
* @see <a href="https://redis.io/commands/incr/">redis.io</a> for details.
* @param key - The key to increment its value.
* @return the value of `key` after the increment, An error is raised if `key` contains a value of
* the wrong type or contains a string that can not be represented as integer.
*/
@Override
public CompletableFuture<Long> incr(String key) {
Command command =
Command.builder()
.requestType(Command.RequestType.INCR)
.arguments(new String[] {key})
.build();
return commandManager.submitNewCommand(command, BaseClient::handleLongResponse);
}

/**
* Increments the number stored at `key` by `amount`. If `key` does not exist, it is set to 0
* before performing the operation.
*
* @see <a href="https://redis.io/commands/incrby/">redis.io</a> for details.
* @param key - The key to increment its value.
* @param amount - The amount to increment.
* @returns the value of `key` after the increment, An error is raised if `key` contains a value
* of the wrong type or contains a string that can not be represented as integer.
*/
@Override
public CompletableFuture<Long> incrBy(String key, long amount) {
Command command =
Command.builder()
.requestType(Command.RequestType.INCR_BY)
.arguments(new String[] {key, Long.toString(amount)})
.build();
return commandManager.submitNewCommand(command, BaseClient::handleLongResponse);
}

/**
* Increment the string representing a floating point number stored at `key` by `amount`. By using
* a negative increment value, the result is that the value stored at `key` is decremented. If
* `key` does not exist, it is set to 0 before performing the operation.
*
* @see <a href="https://redis.io/commands/incrbyfloat/">redis.io</a> for details.
* @param key - The key to increment its value.
* @param amount - The amount to increment.
* @returns the value of `key` after the increment. An error is raised if `key` contains a value
* of the wrong type, or the current key content is not parsable as a double precision
* floating point number.
*/
@Override
public CompletableFuture<Double> incrByFloat(String key, double amount) {
Command command =
Command.builder()
.requestType(Command.RequestType.INCR_BY_FLOAT)
.arguments(new String[] {key, Double.toString(amount)})
.build();
return commandManager.submitNewCommand(command, BaseClient::handleDoubleResponse);
}

/**
* Retrieve the values of multiple keys.
*
* @see <a href="https://redis.io/commands/mget/">redis.io</a> for details.
* @param keys - A list of keys to retrieve values for.
* @returns A list of values corresponding to the provided keys. If a key is not found, its
* corresponding value in the list will be null.
*/
@Override
public CompletableFuture<Object[]> mget(String[] keys) {
Command command =
Command.builder().requestType(Command.RequestType.MGET).arguments(keys).build();
return commandManager.submitNewCommand(command, BaseClient::handleObjectArrayResponse);
}

/**
* Set multiple keys to multiple values in a single operation.
*
* @see <a href="https://redis.io/commands/mset/">redis.io</a> for details.
* @param keyValueMap - A key-value map consisting of keys and their respective values to set.
* @returns null
*/
@Override
public CompletableFuture<Void> mset(HashMap<String, String> keyValueMap) {
List<String> flatMap = new ArrayList<>();

for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
flatMap.add(entry.getKey());
flatMap.add(entry.getValue());
}

String[] args = flatMap.toArray(new String[0]);

Command command =
Command.builder().requestType(Command.RequestType.MSET).arguments(args).build();
return commandManager.submitNewCommand(command, BaseClient::handleVoidResponse);
}
}
21 changes: 20 additions & 1 deletion java/client/src/main/java/glide/api/commands/StringCommands.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
package glide.api.commands;

import glide.api.models.commands.SetOptions;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;

/** String Commands interface to handle single commands that return Strings. */
/**
* String Commands interface.
*
* @see: <a href="https://redis.io/commands/?group=string">String Commands</a>
*/
public interface StringCommands {

CompletableFuture<String> get(String key);

CompletableFuture<Void> set(String key, String value);

CompletableFuture<String> set(String key, String value, SetOptions options);

CompletableFuture<Long> decr(String key);

CompletableFuture<Long> decrBy(String key, long amount);

CompletableFuture<Long> incr(String key);

CompletableFuture<Long> incrBy(String key, long amount);

CompletableFuture<Double> incrByFloat(String key, double amount);

CompletableFuture<Object[]> mget(String[] keys);

CompletableFuture<Void> mset(HashMap<String, String> keyValueMap);
}
14 changes: 14 additions & 0 deletions java/client/src/main/java/glide/managers/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ private RequestType mapRequestTypes(Command.RequestType inType) {
return RequestType.GetString;
case SET_STRING:
return RequestType.SetString;
case DECR:
return RequestType.Decr;
case DECR_BY:
return RequestType.DecrBy;
case INCR:
return RequestType.Incr;
case INCR_BY:
return RequestType.IncrBy;
case INCR_BY_FLOAT:
return RequestType.IncrByFloat;
case MGET:
return RequestType.MGet;
case MSET:
return RequestType.MSet;
}
throw new RuntimeException("Unsupported request type");
}
Expand Down
56 changes: 56 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 @@ -44,5 +44,61 @@ public enum RequestType {
* @see: <href=https://redis.io/commands/set/>command reference</a>
*/
SET_STRING,

/**
* Decrement the number stored at `key` by one. If the key does not exist, it is set to 0 before
* performing the operation.
*
* @see <a href="https://redis.io/commands/decr/">redis.io</a> for details.
*/
DECR,

/**
* Decrements the number stored at `key` by `amount`. If `key` does not exist, it is set to 0
* before performing the operation. Sets the specified fields to their respective values in the
* hash stored at `key`.
*
* @see <a href="https://redis.io/commands/decrby/">redis.io</a> for details.
*/
DECR_BY,

/**
* Increments the number stored at `key` by one. If `key` does not exist, it is set to 0 before
* performing the operation.
*
* @see <a href="https://redis.io/commands/incr/">redis.io</a> for details.
*/
INCR,

/**
* Increments the number stored at `key` by `amount`. If `key` does not exist, it is set to 0
* before performing the operation.
*
* @see <a href="https://redis.io/commands/incrby/">redis.io</a> for details.
*/
INCR_BY,

/**
* Increment the string representing a floating point number stored at `key` by `amount`. By
* using a negative increment value, the result is that the value stored at `key` is
* decremented. If `key` does not exist, it is set to 0 before performing the operation.
*
* @see <a href="https://redis.io/commands/incrbyfloat/">redis.io</a> for details.
*/
INCR_BY_FLOAT,

/**
* Retrieve the values of multiple keys.
*
* @see <a href="https://redis.io/commands/mget/">redis.io</a> for details.
*/
MGET,

/**
* Set multiple keys to multiple values in a single operation.
*
* @see <a href="https://redis.io/commands/mset/">redis.io</a> for details.
*/
MSET,
}
}
Loading

0 comments on commit 6340d2f

Please sign in to comment.