Skip to content

Commit

Permalink
Java: Add LLen command. (List Commands) (valkey-io#1042)
Browse files Browse the repository at this point in the history
* Java: Add LLen command. (List Commands)

* Added examples.

* Star import fix.

* Rebase Spotless.
  • Loading branch information
SanHalacogluImproving authored Mar 4, 2024
1 parent 5f687a3 commit 10186a3
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
6 changes: 6 additions & 0 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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.LLen;
import static redis_request.RedisRequestOuterClass.RequestType.LPop;
import static redis_request.RedisRequestOuterClass.RequestType.LPush;
import static redis_request.RedisRequestOuterClass.RequestType.LRange;
Expand Down Expand Up @@ -377,6 +378,11 @@ public CompletableFuture<String> ltrim(@NonNull String key, long start, long end
this::handleStringResponse);
}

@Override
public CompletableFuture<Long> llen(@NonNull String key) {
return commandManager.submitNewCommand(LLen, new String[] {key}, this::handleLongResponse);
}

@Override
public CompletableFuture<Long> rpush(@NonNull String key, @NonNull String[] elements) {
String[] arguments = ArrayUtils.addFirst(elements, key);
Expand Down
15 changes: 15 additions & 0 deletions java/client/src/main/java/glide/api/commands/ListBaseCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ public interface ListBaseCommands {
*/
CompletableFuture<String> ltrim(String key, long start, long end);

/**
* Returns the length of the list stored at <code>key</code>.
*
* @see <a href="https://redis.io/commands/llen/">redis.io</a> for details.
* @param key The key of the list.
* @return The length of the list at <code>key</code>.<br>
* If <code>key</code> does not exist, it is interpreted as an empty list and 0 is returned.
* @example
* <pre>
* Long lenList = client.llen("my_list").get()
* assert lenList == 3L //Indicates that there are 3 elements in the list.
* </pre>
*/
CompletableFuture<Long> llen(String key);

/**
* Inserts all the specified values at the tail of the list stored at <code>key</code>.<br>
* <code>elements</code> are inserted one after the other to the tail of the list, from the
Expand Down
16 changes: 16 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 @@ -22,6 +22,7 @@
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.LLen;
import static redis_request.RedisRequestOuterClass.RequestType.LPop;
import static redis_request.RedisRequestOuterClass.RequestType.LPush;
import static redis_request.RedisRequestOuterClass.RequestType.LRange;
Expand Down Expand Up @@ -580,6 +581,21 @@ public T ltrim(@NonNull String key, long start, long end) {
return getThis();
}

/**
* Returns the length of the list stored at <code>key</code>.
*
* @see <a href="https://redis.io/commands/llen/">redis.io</a> for details.
* @param key The key of the list.
* @return Command Response - The length of the list at <code>key</code>.<br>
* If <code>key</code> does not exist, it is interpreted as an empty list and 0 is returned.
*/
public T llen(@NonNull String key) {
ArgsArray commandArgs = buildArgs(key);

protobufTransaction.addCommands(buildCommand(LLen, commandArgs));
return getThis();
}

/**
* Inserts all the specified values at the tail of the list stored at <code>key</code>.<br>
* <code>elements</code> are inserted one after the other to the tail of the list, from the
Expand Down
24 changes: 24 additions & 0 deletions java/client/src/test/java/glide/api/RedisClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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.LLen;
import static redis_request.RedisRequestOuterClass.RequestType.LPop;
import static redis_request.RedisRequestOuterClass.RequestType.LPush;
import static redis_request.RedisRequestOuterClass.RequestType.LRange;
Expand Down Expand Up @@ -1093,6 +1094,29 @@ public void ltrim_returns_success() {
assertEquals(OK, payload);
}

@SneakyThrows
@Test
public void llen_returns_success() {
// setup
String key = "testKey";
String[] args = new String[] {key};
long value = 2L;

CompletableFuture<Long> testResponse = mock(CompletableFuture.class);
when(testResponse.get()).thenReturn(value);

// match on protobuf request
when(commandManager.<Long>submitNewCommand(eq(LLen), eq(args), any())).thenReturn(testResponse);

// exercise
CompletableFuture<Long> response = service.llen(key);
Long payload = response.get();

// verify
assertEquals(testResponse, response);
assertEquals(value, payload);
}

@SneakyThrows
@Test
public void rpush_returns_success() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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.LLen;
import static redis_request.RedisRequestOuterClass.RequestType.LPop;
import static redis_request.RedisRequestOuterClass.RequestType.LPush;
import static redis_request.RedisRequestOuterClass.RequestType.LRange;
Expand Down Expand Up @@ -175,6 +176,9 @@ public void transaction_builds_protobuf_request(BaseTransaction<?> transaction)
results.add(
Pair.of(LTrim, ArgsArray.newBuilder().addArgs("key").addArgs("1").addArgs("2").build()));

transaction.llen("key");
results.add(Pair.of(LLen, ArgsArray.newBuilder().addArgs("key").build()));

transaction.rpush("key", new String[] {"element"});
results.add(Pair.of(RPush, ArgsArray.newBuilder().addArgs("key").addArgs("element").build()));

Expand Down
19 changes: 19 additions & 0 deletions java/integTest/src/test/java/glide/SharedCommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,25 @@ public void ltrim_existing_non_existing_key_and_type_error(BaseClient client) {
assertTrue(ltrimException.getCause() instanceof RequestException);
}

@SneakyThrows
@ParameterizedTest
@MethodSource("getClients")
public void llen_existing_non_existing_key_and_type_error(BaseClient client) {
String key1 = UUID.randomUUID().toString();
String key2 = UUID.randomUUID().toString();
String[] valueArray = new String[] {"value4", "value3", "value2", "value1"};

assertEquals(4, client.lpush(key1, valueArray).get());
assertEquals(4, client.llen(key1).get());
assertEquals(0, client.llen("non_existing_key").get());

assertEquals(OK, client.set(key2, "foo").get());

Exception lrangeException =
assertThrows(ExecutionException.class, () -> client.llen(key2).get());
assertTrue(lrangeException.getCause() instanceof RequestException);
}

@SneakyThrows
@ParameterizedTest
@MethodSource("getClients")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public static BaseTransaction<?> transactionTest(BaseTransaction<?> baseTransact
baseTransaction.hincrByFloat(key4, field3, 5.5);

baseTransaction.lpush(key5, new String[] {value1, value2, value3, value3});
baseTransaction.llen(key5);
baseTransaction.ltrim(key5, 1, -1);
baseTransaction.lrange(key5, 0, -2);
baseTransaction.lpop(key5);
Expand Down Expand Up @@ -109,6 +110,7 @@ public static Object[] transactionTestResult() {
5L,
10.5,
4L,
4L,
OK,
new String[] {value3, value2},
value3,
Expand Down

0 comments on commit 10186a3

Please sign in to comment.