Skip to content

Commit

Permalink
Java: Add binary version of ZRANK WITHSCORE (#2896)
Browse files Browse the repository at this point in the history
* Java: Add binary version of ZRANK WITHSCORE

---------

Signed-off-by: James Xin <[email protected]>
  • Loading branch information
jamesx-improving authored Dec 31, 2024
1 parent abec885 commit a26f914
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Go: Add `SScan` and `SMove` ([#2789](https://github.com/valkey-io/valkey-glide/issues/2789))
* Go: Add `ZADD` ([#2813](https://github.com/valkey-io/valkey-glide/issues/2813))
* Go: Add `ZPopMin` and `ZPopMax` ([#2850](https://github.com/valkey-io/valkey-glide/pull/2850))
* Java: Add binary version of `ZRANK WITHSCORE` ([#2896](https://github.com/valkey-io/valkey-glide/pull/2896))

#### Breaking Changes

Expand Down
9 changes: 9 additions & 0 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2207,6 +2207,15 @@ public CompletableFuture<Object[]> zrankWithScore(@NonNull String key, @NonNull
ZRank, new String[] {key, member, WITH_SCORE_VALKEY_API}, this::handleArrayOrNullResponse);
}

@Override
public CompletableFuture<Object[]> zrankWithScore(
@NonNull GlideString key, @NonNull GlideString member) {
return commandManager.submitNewCommand(
ZRank,
new GlideString[] {key, member, gs(WITH_SCORE_VALKEY_API)},
this::handleArrayOrNullResponse);
}

@Override
public CompletableFuture<Long> zrevrank(@NonNull String key, @NonNull String member) {
return commandManager.submitNewCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,28 @@ CompletableFuture<Long> zrangestore(
*/
CompletableFuture<Object[]> zrankWithScore(String key, String member);

/**
* Returns the rank of <code>member</code> in the sorted set stored at <code>key</code> with its
* score, where scores are ordered from the lowest to highest, starting from <code>0</code>.<br>
*
* @see <a href="https://valkey.io/commands/zrank/">valkey.io</a> for more details.
* @param key The key of the sorted set.
* @param member The member whose rank is to be retrieved.
* @return An array containing the rank (as <code>Long</code>) and score (as <code>Double</code>)
* of <code>member</code> in the sorted set.<br>
* If <code>key</code> doesn't exist, or if <code>member</code> is not present in the set,
* <code>null</code> will be returned.
* @example
* <pre>{@code
* Object[] result1 = client.zrankWithScore(gs("mySortedSet"), gs("member2")).get();
* assert ((Long) result1[0]) == 1L && ((Double) result1[1]) == 6.0; // Indicates that "member2" with score 6.0 has the second-lowest score in the sorted set "mySortedSet".
*
* Object[] result2 = client.zrankWithScore(gs("mySortedSet"), gs("nonExistingMember")).get();
* assert result2 == null; // Indicates that "nonExistingMember" is not present in the sorted set "mySortedSet".
* }</pre>
*/
CompletableFuture<Object[]> zrankWithScore(GlideString key, GlideString member);

/**
* Returns the rank of <code>member</code> in the sorted set stored at <code>key</code>, where
* scores are ordered from the highest to lowest, starting from <code>0</code>.<br>
Expand Down
25 changes: 25 additions & 0 deletions java/client/src/test/java/glide/api/GlideClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5652,6 +5652,31 @@ public void zrankWithScore_returns_success() {
assertEquals(value, payload);
}

@SneakyThrows
@Test
public void zrankWithScore_binary_returns_success() {
// setup
GlideString key = gs("testKey");
GlideString member = gs("testMember");
GlideString[] arguments = new GlideString[] {key, member, gs(WITH_SCORE_VALKEY_API)};
Object[] value = new Object[] {1, 6.0};

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

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

// exercise
CompletableFuture<Object[]> response = service.zrankWithScore(key, member);
Object[] payload = response.get();

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

@SneakyThrows
@Test
public void zrevrank_returns_success() {
Expand Down
1 change: 1 addition & 0 deletions java/integTest/src/test/java/glide/SharedCommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3924,6 +3924,7 @@ public void zrank(BaseClient client) {

if (SERVER_VERSION.isGreaterThanOrEqualTo("7.2.0")) {
assertArrayEquals(new Object[] {0L, 1.5}, client.zrankWithScore(key, "one").get());
assertArrayEquals(new Object[] {0L, 1.5}, client.zrankWithScore(gs(key), gs("one")).get());
assertNull(client.zrankWithScore(key, "nonExistingMember").get());
assertNull(client.zrankWithScore("nonExistingKey", "nonExistingMember").get());
}
Expand Down

0 comments on commit a26f914

Please sign in to comment.