Skip to content

Commit

Permalink
Java: Add Zmscore command. (Sorted Set Command Group) (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
SanHalacogluImproving authored Apr 4, 2024
1 parent 078a4cd commit 73cd123
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* Core: Enabled Cluster Mode periodic checks by default ([#1089](https://github.com/aws/glide-for-redis/pull/1089))
* Node: Added Rename command. ([#1124](https://github.com/aws/glide-for-redis/pull/1124))
* Python: Added JSON.TOGGLE command ([#1184](https://github.com/aws/glide-for-redis/pull/1184))
* Java: Added ZMSCORE command (TODO ADD HERE PR #)

#### Features

Expand Down
1 change: 1 addition & 0 deletions glide-core/src/protobuf/redis_request.proto
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ enum RequestType {
Rename = 91;
DBSize = 92;
Brpop = 93;
ZMScore = 94;
}

message Command {
Expand Down
1 change: 1 addition & 0 deletions glide-core/src/socket_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::Rename => Some(cmd("RENAME")),
RequestType::DBSize => Some(cmd("DBSIZE")),
RequestType::Brpop => Some(cmd("BRPOP")),
RequestType::ZMScore => Some(cmd("ZMSCORE")),
}
}

Expand Down
10 changes: 10 additions & 0 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.TTL;
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.ZMScore;
import static redis_request.RedisRequestOuterClass.RequestType.Zadd;
import static redis_request.RedisRequestOuterClass.RequestType.Zcard;
import static redis_request.RedisRequestOuterClass.RequestType.Zrem;
Expand Down Expand Up @@ -594,6 +595,15 @@ public CompletableFuture<Long> zcard(@NonNull String key) {
return commandManager.submitNewCommand(Zcard, new String[] {key}, this::handleLongResponse);
}

@Override
public CompletableFuture<Double[]> zmscore(@NonNull String key, @NonNull String[] members) {
String[] arguments = ArrayUtils.addFirst(members, key);
return commandManager.submitNewCommand(
ZMScore,
arguments,
response -> castArray(handleArrayOrNullResponse(response), Double.class));
}

@Override
public CompletableFuture<String> type(@NonNull String key) {
return commandManager.submitNewCommand(Type, new String[] {key}, this::handleStringResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,23 @@ CompletableFuture<Double> zaddIncr(
* }</pre>
*/
CompletableFuture<Long> zcard(String key);

/**
* Returns the scores associated with the specified <code>members</code> in the sorted set stored
* at <code>key</code>.
*
* @see <a href="https://redis.io/commands/zmscore/">redis.io</a> for more details.
* @param key The key of the sorted set.
* @param members An array of members whose scores are to be retrieved.
* @return An <code>array</code> representing the scores for the specified <code>members</code>.
* <br>
* If a <code>member</code> does not exist, the corresponding value in the <code>array</code>
* will be <code>null</code>.
* @example
* <pre>{@code
* Double[] payload = client.zmscore(key1, new String[] {"one", "nonExistentMember", "three"}).get();
* assert payload.equals(new Double[] {1.0, null, 3.0});
* }</pre>
*/
CompletableFuture<Double[]> zmscore(String key, String[] members);
}
19 changes: 19 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 @@ -53,6 +53,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.TTL;
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.ZMScore;
import static redis_request.RedisRequestOuterClass.RequestType.Zadd;
import static redis_request.RedisRequestOuterClass.RequestType.Zcard;
import static redis_request.RedisRequestOuterClass.RequestType.Zrem;
Expand Down Expand Up @@ -1272,6 +1273,24 @@ public T zcard(@NonNull String key) {
return getThis();
}

/**
* Returns the scores associated with the specified <code>members</code> in the sorted set stored
* at <code>key</code>.
*
* @see <a href="https://redis.io/commands/zmscore/">redis.io</a> for more details.
* @param key The key of the sorted set.
* @param members An array of members whose scores are to be retrieved.
* @return Command Response - An <code>array</code> representing the scores for the specified
* <code>members</code>. <br>
* If a <code>member</code> does not exist, the corresponding value in the <code>array</code>
* will be <code>null</code>.
*/
public T zmscore(@NonNull String key, @NonNull String[] members) {
ArgsArray commandArgs = buildArgs(ArrayUtils.addFirst(members, key));
protobufTransaction.addCommands(buildCommand(ZMScore, commandArgs));
return getThis();
}

/**
* Returns the string representation of the type of the value stored at <code>key</code>.
*
Expand Down
26 changes: 26 additions & 0 deletions java/client/src/test/java/glide/api/RedisClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.TTL;
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.ZMScore;
import static redis_request.RedisRequestOuterClass.RequestType.Zadd;
import static redis_request.RedisRequestOuterClass.RequestType.Zcard;
import static redis_request.RedisRequestOuterClass.RequestType.Zrem;
Expand Down Expand Up @@ -1685,6 +1686,31 @@ public void zcard_returns_success() {
assertEquals(value, payload);
}

@SneakyThrows
@Test
public void zmscore_returns_success() {
// setup
String key = "testKey";
String[] members = new String[] {"member1", "member2"};
String[] arguments = new String[] {key, "member1", "member2"};
Double[] value = new Double[] {2.5, 8.2};

CompletableFuture<Double[]> testResponse = new CompletableFuture<>();
testResponse.complete(value);

// match on protobuf request
when(commandManager.<Double[]>submitNewCommand(eq(ZMScore), eq(arguments), any()))
.thenReturn(testResponse);

// exercise
CompletableFuture<Double[]> response = service.zmscore(key, members);
Double[] payload = response.get();

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

@SneakyThrows
@Test
public void type_returns_success() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.TTL;
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.ZMScore;
import static redis_request.RedisRequestOuterClass.RequestType.Zadd;
import static redis_request.RedisRequestOuterClass.RequestType.Zcard;
import static redis_request.RedisRequestOuterClass.RequestType.Zrem;
Expand Down Expand Up @@ -381,6 +382,12 @@ public void transaction_builds_protobuf_request(BaseTransaction<?> transaction)
transaction.zcard("key");
results.add(Pair.of(Zcard, ArgsArray.newBuilder().addArgs("key").build()));

transaction.zmscore("key", new String[] {"member1", "member2"});
results.add(
Pair.of(
ZMScore,
ArgsArray.newBuilder().addArgs("key").addArgs("member1").addArgs("member2").build()));

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

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

@SneakyThrows
@ParameterizedTest
@MethodSource("getClients")
public void zmscore(BaseClient client) {
String key1 = UUID.randomUUID().toString();
String key2 = UUID.randomUUID().toString();
Map<String, Double> membersScores = Map.of("one", 1.0, "two", 2.0, "three", 3.0);
assertEquals(3, client.zadd(key1, membersScores).get());
assertArrayEquals(
new Double[] {1.0, 2.0, 3.0},
client.zmscore(key1, new String[] {"one", "two", "three"}).get());
assertArrayEquals(
new Double[] {2.0, 3.0}, client.zmscore(key1, new String[] {"two", "three"}).get());
assertArrayEquals(
new Double[] {1.0, null, 3.0},
client.zmscore(key1, new String[] {"one", "nonExistentMember", "three"}).get());
assertArrayEquals(
new Double[] {null},
client.zmscore("nonExistentKey", new String[] {"nonExistentMember"}).get());

// Key exists, but it is not a set
assertEquals(OK, client.set(key2, "bar").get());
ExecutionException executionException =
assertThrows(
ExecutionException.class, () -> client.zmscore(key2, new String[] {"one"}).get());
assertTrue(executionException.getCause() instanceof RequestException);
}

@SneakyThrows
@ParameterizedTest
@MethodSource("getClients")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public static BaseTransaction<?> transactionTest(BaseTransaction<?> baseTransact
baseTransaction.zaddIncr(key8, "one", 3);
baseTransaction.zrem(key8, new String[] {"one"});
baseTransaction.zcard(key8);
baseTransaction.zmscore(key8, new String[] {"two", "three"});

baseTransaction.configSet(Map.of("timeout", "1000"));
baseTransaction.configGet(new String[] {"timeout"});
Expand Down Expand Up @@ -145,6 +146,7 @@ public static Object[] transactionTestResult() {
4.0,
1L,
2L,
new Double[] {2.0, 3.0}, // zmscore(key8, new String[] {"two", "three"})
OK,
Map.of("timeout", "1000"),
OK,
Expand Down

0 comments on commit 73cd123

Please sign in to comment.