diff --git a/java/client/src/main/java/glide/api/models/BaseTransaction.java b/java/client/src/main/java/glide/api/models/BaseTransaction.java index 92248e5047..5c4812dfd5 100644 --- a/java/client/src/main/java/glide/api/models/BaseTransaction.java +++ b/java/client/src/main/java/glide/api/models/BaseTransaction.java @@ -3,6 +3,9 @@ import static redis_request.RedisRequestOuterClass.RequestType.CustomCommand; import static redis_request.RedisRequestOuterClass.RequestType.GetString; +import static redis_request.RedisRequestOuterClass.RequestType.Incr; +import static redis_request.RedisRequestOuterClass.RequestType.IncrBy; +import static redis_request.RedisRequestOuterClass.RequestType.IncrByFloat; import static redis_request.RedisRequestOuterClass.RequestType.Info; import static redis_request.RedisRequestOuterClass.RequestType.Ping; import static redis_request.RedisRequestOuterClass.RequestType.SetString; @@ -13,6 +16,7 @@ import glide.api.models.commands.SetOptions.ConditionalSet; import glide.api.models.commands.SetOptions.SetOptionsBuilder; import lombok.Getter; +import lombok.NonNull; import org.apache.commons.lang3.ArrayUtils; import redis_request.RedisRequestOuterClass.Command; import redis_request.RedisRequestOuterClass.Command.ArgsArray; @@ -165,6 +169,61 @@ public T set(String key, String value, SetOptions options) { return getThis(); } + /** + * Increments the number stored at key by one. If key does not exist, it + * is set to 0 before performing the operation. + * + * @see redis.io 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. + */ + public T incr(@NonNull String key) { + ArgsArray commandArgs = buildArgs(key); + + protobufTransaction.addCommands(buildCommand(Incr, commandArgs)); + return getThis(); + } + + /** + * Increments the number stored at key by amount. If key + * does not exist, it is set to 0 before performing the operation. + * + * @see redis.io for details. + * @param key The key to increment its value. + * @param amount The amount to increment. + * @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 cannot be represented + * as integer. + */ + public T incrBy(@NonNull String key, long amount) { + ArgsArray commandArgs = buildArgs(key, Long.toString(amount)); + + protobufTransaction.addCommands(buildCommand(IncrBy, commandArgs)); + return getThis(); + } + + /** + * 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 redis.io for details. + * @param key The key to increment its value. + * @param amount The amount to increment. + * @return 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. + */ + public T incrByFloat(@NonNull String key, double amount) { + ArgsArray commandArgs = buildArgs(key, Double.toString(amount)); + + protobufTransaction.addCommands(buildCommand(IncrByFloat, commandArgs)); + return getThis(); + } + /** Build protobuf {@link Command} object for given command and arguments. */ protected Command buildCommand(RequestType requestType) { return buildCommand(requestType, buildArgs()); diff --git a/java/integTest/src/test/java/glide/SharedCommandTests.java b/java/integTest/src/test/java/glide/SharedCommandTests.java index 51697dbee0..e174e41c51 100644 --- a/java/integTest/src/test/java/glide/SharedCommandTests.java +++ b/java/integTest/src/test/java/glide/SharedCommandTests.java @@ -22,6 +22,7 @@ import glide.api.models.configuration.RedisClusterClientConfiguration; import glide.api.models.exceptions.RequestException; import java.util.List; +import java.util.UUID; import java.util.concurrent.ExecutionException; import lombok.Getter; import lombok.SneakyThrows; @@ -257,7 +258,7 @@ public void set_missing_value_and_returnOldValue_is_null(BaseClient client) { @ParameterizedTest @MethodSource("getClients") public void incr_commands_existing_key(BaseClient client) { - String key = RandomStringUtils.randomAlphabetic(10); + String key = UUID.randomUUID().toString(); assertEquals(OK, client.set(key, "10").get(10, SECONDS)); @@ -275,9 +276,9 @@ public void incr_commands_existing_key(BaseClient client) { @ParameterizedTest @MethodSource("getClients") public void incr_commands_non_existing_key(BaseClient client) { - String key1 = RandomStringUtils.randomAlphabetic(10); - String key2 = RandomStringUtils.randomAlphabetic(10); - String key3 = RandomStringUtils.randomAlphabetic(10); + String key1 = UUID.randomUUID().toString(); + String key2 = UUID.randomUUID().toString(); + String key3 = UUID.randomUUID().toString(); assertNull(client.get(key1).get(10, SECONDS)); assertEquals(1, client.incr(key1).get(10, SECONDS)); @@ -296,7 +297,7 @@ public void incr_commands_non_existing_key(BaseClient client) { @ParameterizedTest @MethodSource("getClients") public void test_incr_commands_type_error(BaseClient client) { - String key1 = RandomStringUtils.randomAlphabetic(10); + String key1 = UUID.randomUUID().toString(); assertEquals(OK, client.set(key1, "foo").get(10, SECONDS));