Skip to content

Commit

Permalink
Added Incr commands to transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
SanHalacogluImproving committed Feb 13, 2024
1 parent 3cecdf6 commit 9b74df5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
59 changes: 59 additions & 0 deletions java/client/src/main/java/glide/api/models/BaseTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -165,6 +169,61 @@ public T set(String key, String value, SetOptions options) {
return getThis();
}

/**
* Increments the number stored at <code>key</code> by one. If <code>key</code> 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 <code>key</code> after the increment. An error is raised if <code>key
* </code> 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 <code>key</code> by <code>amount</code>. If <code>key</code>
* 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.
* @return The value of <code>key</code> after the increment, An error is raised if <code>key
* </code> 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 <code>key</code> by <code>
* amount</code>. By using a negative increment value, the result is that the value stored at
* <code>key</code> is decremented. If <code>key</code> 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.
* @return The value of <code>key</code> after the increment. An error is raised if <code>key
* </code> 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());
Expand Down
11 changes: 6 additions & 5 deletions java/integTest/src/test/java/glide/SharedCommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));

Expand All @@ -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));
Expand All @@ -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));

Expand Down

0 comments on commit 9b74df5

Please sign in to comment.