Skip to content

Commit c8a54bb

Browse files
Added part of the 'String Commands' group to RedisClient as specified on the official Redis website.
1 parent 5f14dba commit c8a54bb

File tree

6 files changed

+450
-1
lines changed

6 files changed

+450
-1
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,60 @@ protected static String handleStringResponse(Response response) {
6565
+ " expected String");
6666
}
6767

68+
/**
69+
* Extracts the response value from the Redis response and either throws an exception or returns
70+
* the value as a Long.
71+
*
72+
* @param response Redis protobuf message
73+
* @return Response as a Long
74+
*/
75+
public static Long handleLongResponse(Response response) {
76+
Object value = handleObjectResponse(response);
77+
if (value instanceof Long) {
78+
return (Long) value;
79+
}
80+
throw new RedisException(
81+
"Unexpected return type from Redis: got "
82+
+ value.getClass().getSimpleName()
83+
+ " expected Long");
84+
}
85+
86+
/**
87+
* Extracts the response value from the Redis response and either throws an exception or returns
88+
* the value as a Double.
89+
*
90+
* @param response Redis protobuf message
91+
* @return Response as a Double
92+
*/
93+
public static Double handleDoubleResponse(Response response) {
94+
Object value = handleObjectResponse(response);
95+
if (value instanceof Double) {
96+
return (Double) value;
97+
}
98+
throw new RedisException(
99+
"Unexpected return type from Redis: got "
100+
+ value.getClass().getSimpleName()
101+
+ " expected Double");
102+
}
103+
104+
/**
105+
* Extracts the response value from the Redis response and either throws an exception or returns
106+
* the value as an Object Array.
107+
*
108+
* @param response Redis protobuf message
109+
* @return Response as an Object Array
110+
*/
111+
public static Object[] handleObjectArrayResponse(Response response) {
112+
Object value = handleObjectResponse(response);
113+
if (value instanceof Object[]) {
114+
return (Object[]) value;
115+
}
116+
throw new RedisException(
117+
"Unexpected return type from Redis: got "
118+
+ value.getClass().getSimpleName()
119+
+ " expected Object Array");
120+
}
121+
68122
/**
69123
* Extracts the response value from the Redis response and either throws an exception or returns
70124
* the * value as a HashMap

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

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import glide.managers.CommandManager;
1616
import glide.managers.ConnectionManager;
1717
import glide.managers.models.Command;
18+
import java.util.ArrayList;
19+
import java.util.HashMap;
1820
import java.util.List;
1921
import java.util.Map;
2022
import java.util.concurrent.CompletableFuture;
@@ -201,4 +203,142 @@ public CompletableFuture<String> set(String key, String value, SetOptions option
201203
.build();
202204
return commandManager.submitNewCommand(command, BaseClient::handleStringResponse);
203205
}
206+
207+
/**
208+
* Decrements the number stored at `key` by one. If `key` does not exist, it is set to 0 before
209+
* performing the operation.
210+
*
211+
* @see <a href="https://redis.io/commands/decr/">redis.io</a> for details.
212+
* @param key - The key to decrement its value.
213+
* @return the value of `key` after the decrement. An error is raised if `key` contains a value of
214+
* the wrong type or contains a string that can not be represented as integer.
215+
*/
216+
@Override
217+
public CompletableFuture<Long> decr(String key) {
218+
Command command =
219+
Command.builder()
220+
.requestType(Command.RequestType.DECR)
221+
.arguments(new String[] {key})
222+
.build();
223+
return commandManager.submitNewCommand(command, BaseClient::handleLongResponse);
224+
}
225+
226+
/**
227+
* Decrements the number stored at `key` by `amount`. If `key` does not exist, it is set to 0
228+
* before performing the operation.
229+
*
230+
* @see <a href="https://redis.io/commands/decrby/">redis.io</a> for details.
231+
* @param key - The key to decrement its value.
232+
* @param amount - The amount to decrement.
233+
* @return the value of `key` after the decrement. An error is raised if `key` contains a value of
234+
* the wrong type or contains a string that can not be represented as integer.
235+
*/
236+
@Override
237+
public CompletableFuture<Long> decrBy(String key, long amount) {
238+
Command command =
239+
Command.builder()
240+
.requestType(Command.RequestType.DECR_BY)
241+
.arguments(new String[] {key, Long.toString(amount)})
242+
.build();
243+
return commandManager.submitNewCommand(command, BaseClient::handleLongResponse);
244+
}
245+
246+
/**
247+
* Increments the number stored at `key` by one. If `key` does not exist, it is set to 0 before
248+
* performing the operation.
249+
*
250+
* @see <a href="https://redis.io/commands/incr/">redis.io</a> for details.
251+
* @param key - The key to increment its value.
252+
* @return the value of `key` after the increment, An error is raised if `key` contains a value of
253+
* the wrong type or contains a string that can not be represented as integer.
254+
*/
255+
@Override
256+
public CompletableFuture<Long> incr(String key) {
257+
Command command =
258+
Command.builder()
259+
.requestType(Command.RequestType.INCR)
260+
.arguments(new String[] {key})
261+
.build();
262+
return commandManager.submitNewCommand(command, BaseClient::handleLongResponse);
263+
}
264+
265+
/**
266+
* Increments the number stored at `key` by `amount`. If `key` does not exist, it is set to 0
267+
* before performing the operation.
268+
*
269+
* @see <a href="https://redis.io/commands/incrby/">redis.io</a> for details.
270+
* @param key - The key to increment its value.
271+
* @param amount - The amount to increment.
272+
* @returns the value of `key` after the increment, An error is raised if `key` contains a value
273+
* of the wrong type or contains a string that can not be represented as integer.
274+
*/
275+
@Override
276+
public CompletableFuture<Long> incrBy(String key, long amount) {
277+
Command command =
278+
Command.builder()
279+
.requestType(Command.RequestType.INCR_BY)
280+
.arguments(new String[] {key, Long.toString(amount)})
281+
.build();
282+
return commandManager.submitNewCommand(command, BaseClient::handleLongResponse);
283+
}
284+
285+
/**
286+
* Increment the string representing a floating point number stored at `key` by `amount`. By using
287+
* a negative increment value, the result is that the value stored at `key` is decremented. If
288+
* `key` does not exist, it is set to 0 before performing the operation.
289+
*
290+
* @see <a href="https://redis.io/commands/incrbyfloat/">redis.io</a> for details.
291+
* @param key - The key to increment its value.
292+
* @param amount - The amount to increment.
293+
* @returns the value of `key` after the increment. An error is raised if `key` contains a value
294+
* of the wrong type, or the current key content is not parsable as a double precision
295+
* floating point number.
296+
*/
297+
@Override
298+
public CompletableFuture<Double> incrByFloat(String key, double amount) {
299+
Command command =
300+
Command.builder()
301+
.requestType(Command.RequestType.INCR_BY_FLOAT)
302+
.arguments(new String[] {key, Double.toString(amount)})
303+
.build();
304+
return commandManager.submitNewCommand(command, BaseClient::handleDoubleResponse);
305+
}
306+
307+
/**
308+
* Retrieve the values of multiple keys.
309+
*
310+
* @see <a href="https://redis.io/commands/mget/">redis.io</a> for details.
311+
* @param keys - A list of keys to retrieve values for.
312+
* @returns A list of values corresponding to the provided keys. If a key is not found, its
313+
* corresponding value in the list will be null.
314+
*/
315+
@Override
316+
public CompletableFuture<Object[]> mget(String[] keys) {
317+
Command command =
318+
Command.builder().requestType(Command.RequestType.MGET).arguments(keys).build();
319+
return commandManager.submitNewCommand(command, BaseClient::handleObjectArrayResponse);
320+
}
321+
322+
/**
323+
* Set multiple keys to multiple values in a single operation.
324+
*
325+
* @see <a href="https://redis.io/commands/mset/">redis.io</a> for details.
326+
* @param keyValueMap - A key-value map consisting of keys and their respective values to set.
327+
* @returns null
328+
*/
329+
@Override
330+
public CompletableFuture<Void> mset(HashMap<String, String> keyValueMap) {
331+
List<String> flatMap = new ArrayList<>();
332+
333+
for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
334+
flatMap.add(entry.getKey());
335+
flatMap.add(entry.getValue());
336+
}
337+
338+
String[] args = flatMap.toArray(new String[0]);
339+
340+
Command command =
341+
Command.builder().requestType(Command.RequestType.MSET).arguments(args).build();
342+
return commandManager.submitNewCommand(command, BaseClient::handleVoidResponse);
343+
}
204344
}
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
package glide.api.commands;
22

33
import glide.api.models.commands.SetOptions;
4+
import java.util.HashMap;
45
import java.util.concurrent.CompletableFuture;
56

6-
/** String Commands interface to handle single commands that return Strings. */
7+
/**
8+
* String Commands interface.
9+
*
10+
* @see: <a href="https://redis.io/commands/?group=string">String Commands</a>
11+
*/
712
public interface StringCommands {
813

914
CompletableFuture<String> get(String key);
1015

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

1318
CompletableFuture<String> set(String key, String value, SetOptions options);
19+
20+
CompletableFuture<Long> decr(String key);
21+
22+
CompletableFuture<Long> decrBy(String key, long amount);
23+
24+
CompletableFuture<Long> incr(String key);
25+
26+
CompletableFuture<Long> incrBy(String key, long amount);
27+
28+
CompletableFuture<Double> incrByFloat(String key, double amount);
29+
30+
CompletableFuture<Object[]> mget(String[] keys);
31+
32+
CompletableFuture<Void> mset(HashMap<String, String> keyValueMap);
1433
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ private RequestType mapRequestTypes(Command.RequestType inType) {
7676
return RequestType.GetString;
7777
case SET_STRING:
7878
return RequestType.SetString;
79+
case DECR:
80+
return RequestType.Decr;
81+
case DECR_BY:
82+
return RequestType.DecrBy;
83+
case INCR:
84+
return RequestType.Incr;
85+
case INCR_BY:
86+
return RequestType.IncrBy;
87+
case INCR_BY_FLOAT:
88+
return RequestType.IncrByFloat;
89+
case MGET:
90+
return RequestType.MGet;
91+
case MSET:
92+
return RequestType.MSet;
7993
}
8094
throw new RuntimeException("Unsupported request type");
8195
}

java/client/src/main/java/glide/managers/models/Command.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,61 @@ public enum RequestType {
4444
* @see: <href=https://redis.io/commands/set/>command reference</a>
4545
*/
4646
SET_STRING,
47+
48+
/**
49+
* Decrement the number stored at `key` by one. If the key does not exist, it is set to 0 before
50+
* performing the operation.
51+
*
52+
* @see <a href="https://redis.io/commands/decr/">redis.io</a> for details.
53+
*/
54+
DECR,
55+
56+
/**
57+
* Decrements the number stored at `key` by `amount`. If `key` does not exist, it is set to 0
58+
* before performing the operation. Sets the specified fields to their respective values in the
59+
* hash stored at `key`.
60+
*
61+
* @see <a href="https://redis.io/commands/decrby/">redis.io</a> for details.
62+
*/
63+
DECR_BY,
64+
65+
/**
66+
* Increments the number stored at `key` by one. If `key` does not exist, it is set to 0 before
67+
* performing the operation.
68+
*
69+
* @see <a href="https://redis.io/commands/incr/">redis.io</a> for details.
70+
*/
71+
INCR,
72+
73+
/**
74+
* Increments the number stored at `key` by `amount`. If `key` does not exist, it is set to 0
75+
* before performing the operation.
76+
*
77+
* @see <a href="https://redis.io/commands/incrby/">redis.io</a> for details.
78+
*/
79+
INCR_BY,
80+
81+
/**
82+
* Increment the string representing a floating point number stored at `key` by `amount`. By
83+
* using a negative increment value, the result is that the value stored at `key` is
84+
* decremented. If `key` does not exist, it is set to 0 before performing the operation.
85+
*
86+
* @see <a href="https://redis.io/commands/incrbyfloat/">redis.io</a> for details.
87+
*/
88+
INCR_BY_FLOAT,
89+
90+
/**
91+
* Retrieve the values of multiple keys.
92+
*
93+
* @see <a href="https://redis.io/commands/mget/">redis.io</a> for details.
94+
*/
95+
MGET,
96+
97+
/**
98+
* Set multiple keys to multiple values in a single operation.
99+
*
100+
* @see <a href="https://redis.io/commands/mset/">redis.io</a> for details.
101+
*/
102+
MSET,
47103
}
48104
}

0 commit comments

Comments
 (0)