Skip to content

Commit

Permalink
Java: Add hlen command. (Hash Command Group) (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
SanHalacogluImproving authored Apr 1, 2024
1 parent be7e0f6 commit 7164805
Show file tree
Hide file tree
Showing 7 changed files with 94 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 @@ -13,6 +13,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.Expire;
import static redis_request.RedisRequestOuterClass.RequestType.ExpireAt;
import static redis_request.RedisRequestOuterClass.RequestType.GetString;
import static redis_request.RedisRequestOuterClass.RequestType.HLen;
import static redis_request.RedisRequestOuterClass.RequestType.HashDel;
import static redis_request.RedisRequestOuterClass.RequestType.HashExists;
import static redis_request.RedisRequestOuterClass.RequestType.HashGet;
Expand Down Expand Up @@ -323,6 +324,11 @@ public CompletableFuture<Long> hdel(@NonNull String key, @NonNull String[] field
return commandManager.submitNewCommand(HashDel, args, this::handleLongResponse);
}

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

@Override
public CompletableFuture<String[]> hmget(@NonNull String key, @NonNull String[] fields) {
String[] arguments = ArrayUtils.addFirst(fields, key);
Expand Down
18 changes: 18 additions & 0 deletions java/client/src/main/java/glide/api/commands/HashBaseCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ public interface HashBaseCommands {
*/
CompletableFuture<Long> hdel(String key, String[] fields);

/**
* Returns the number of fields contained in the hash stored at <code>key</code>.
*
* @see <a href="https://redis.io/commands/hlen/">redis.io</a> for details.
* @param key The key of the hash.
* @return The number of fields in the hash, or <code>0</code> when the key does not exist.<br>
* If <code>key</code> holds a value that is not a hash, an error is returned.
* @example
* <pre>{@code
* Long num1 = client.hlen("myHash").get();
* assert num1 == 3L;
*
* Long num2 = client.hlen("nonExistingKey").get();
* assert num2 == 0L;
* }</pre>
*/
CompletableFuture<Long> hlen(String key);

/**
* Returns the values associated with the specified fields in the hash stored at <code>key</code>.
*
Expand Down
17 changes: 17 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 @@ -18,6 +18,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.Expire;
import static redis_request.RedisRequestOuterClass.RequestType.ExpireAt;
import static redis_request.RedisRequestOuterClass.RequestType.GetString;
import static redis_request.RedisRequestOuterClass.RequestType.HLen;
import static redis_request.RedisRequestOuterClass.RequestType.HashDel;
import static redis_request.RedisRequestOuterClass.RequestType.HashExists;
import static redis_request.RedisRequestOuterClass.RequestType.HashGet;
Expand Down Expand Up @@ -394,6 +395,22 @@ public T hdel(@NonNull String key, @NonNull String[] fields) {
return getThis();
}

/**
* Returns the number of fields contained in the hash stored at <code>key</code>.
*
* @see <a href="https://redis.io/commands/hlen/">redis.io</a> for details.
* @param key The key of the hash.
* @return Command Response - The number of fields in the hash, or <code>0</code> when the key
* does not exist.<br>
* If <code>key</code> holds a value that is not a hash, an error is returned.
*/
public T hlen(@NonNull String key) {
ArgsArray commandArgs = buildArgs(key);

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

/**
* Returns the values associated with the specified fields in the hash stored at <code>key</code>.
*
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 @@ -29,6 +29,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.Expire;
import static redis_request.RedisRequestOuterClass.RequestType.ExpireAt;
import static redis_request.RedisRequestOuterClass.RequestType.GetString;
import static redis_request.RedisRequestOuterClass.RequestType.HLen;
import static redis_request.RedisRequestOuterClass.RequestType.HashDel;
import static redis_request.RedisRequestOuterClass.RequestType.HashExists;
import static redis_request.RedisRequestOuterClass.RequestType.HashGet;
Expand Down Expand Up @@ -855,6 +856,29 @@ public void hdel_success() {
assertEquals(value, payload);
}

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

CompletableFuture<Long> testResponse = new CompletableFuture<>();
testResponse.complete(value);

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

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

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

@SneakyThrows
@Test
public void hmget_success() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.Expire;
import static redis_request.RedisRequestOuterClass.RequestType.ExpireAt;
import static redis_request.RedisRequestOuterClass.RequestType.GetString;
import static redis_request.RedisRequestOuterClass.RequestType.HLen;
import static redis_request.RedisRequestOuterClass.RequestType.HashDel;
import static redis_request.RedisRequestOuterClass.RequestType.HashExists;
import static redis_request.RedisRequestOuterClass.RequestType.HashGet;
Expand Down Expand Up @@ -146,6 +147,9 @@ public void transaction_builds_protobuf_request(BaseTransaction<?> transaction)
transaction.hdel("key", new String[] {"field"});
results.add(Pair.of(HashDel, ArgsArray.newBuilder().addArgs("key").addArgs("field").build()));

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

transaction.hmget("key", new String[] {"field"});
results.add(Pair.of(HashMGet, ArgsArray.newBuilder().addArgs("key").addArgs("field").build()));

Expand Down
23 changes: 23 additions & 0 deletions java/integTest/src/test/java/glide/SharedCommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,29 @@ public void hdel_multiple_existing_fields_non_existing_field_non_existing_key(Ba
assertEquals(0, client.hdel("non_existing_key", new String[] {field3}).get());
}

@SneakyThrows
@ParameterizedTest
@MethodSource("getClients")
public void hlen(BaseClient client) {
String key1 = UUID.randomUUID().toString();
String key2 = UUID.randomUUID().toString();
String field1 = UUID.randomUUID().toString();
String field2 = UUID.randomUUID().toString();
String value = UUID.randomUUID().toString();
Map<String, String> fieldValueMap = Map.of(field1, value, field2, value);

assertEquals(2, client.hset(key1, fieldValueMap).get());
assertEquals(2, client.hlen(key1).get());
assertEquals(1, client.hdel(key1, new String[] {field1}).get());
assertEquals(1, client.hlen(key1).get());
assertEquals(0, client.hlen("nonExistingHash").get());

assertEquals(OK, client.set(key2, "value").get());
ExecutionException executionException =
assertThrows(ExecutionException.class, () -> client.hlen(key2).get());
assertTrue(executionException.getCause() instanceof RequestException);
}

@SneakyThrows
@ParameterizedTest
@MethodSource("getClients")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static BaseTransaction<?> transactionTest(BaseTransaction<?> baseTransact

baseTransaction.hset(key4, Map.of(field1, value1, field2, value2));
baseTransaction.hget(key4, field1);
baseTransaction.hlen(key4);
baseTransaction.hexists(key4, field2);
baseTransaction.hmget(key4, new String[] {field1, "non_existing_field", field2});
baseTransaction.hgetall(key4);
Expand Down Expand Up @@ -115,6 +116,7 @@ public static Object[] transactionTestResult() {
1L,
2L,
value1,
2L, // hlen(key4)
true,
new String[] {value1, null, value2},
Map.of(field1, value1, field2, value2),
Expand Down

0 comments on commit 7164805

Please sign in to comment.