Skip to content

Commit

Permalink
Java: Added Zpopmin command. (Sorted Set Commands) (valkey-io#1165)
Browse files Browse the repository at this point in the history
* Java: Added Zpopmin command. (Sorted Set Commands) (#150)
  • Loading branch information
SanHalacogluImproving authored and cyip10 committed Jun 24, 2024
1 parent 84f8c2b commit 4c34fd0
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 0 deletions.
12 changes: 12 additions & 0 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.ZPopMax;
import static redis_request.RedisRequestOuterClass.RequestType.ZPopMin;
import static redis_request.RedisRequestOuterClass.RequestType.ZScore;
import static redis_request.RedisRequestOuterClass.RequestType.Zadd;
import static redis_request.RedisRequestOuterClass.RequestType.Zcard;
Expand Down Expand Up @@ -614,6 +615,17 @@ public CompletableFuture<Long> zcard(@NonNull String key) {
return commandManager.submitNewCommand(Zcard, new String[] {key}, this::handleLongResponse);
}

@Override
public CompletableFuture<Map<String, Double>> zpopmin(@NonNull String key, long count) {
return commandManager.submitNewCommand(
ZPopMin, new String[] {key, Long.toString(count)}, this::handleMapResponse);
}

@Override
public CompletableFuture<Map<String, Double>> zpopmin(@NonNull String key) {
return commandManager.submitNewCommand(ZPopMin, new String[] {key}, this::handleMapResponse);
}

@Override
public CompletableFuture<Map<String, Double>> zpopmax(@NonNull String key, long count) {
return commandManager.submitNewCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,44 @@ CompletableFuture<Double> zaddIncr(
*/
CompletableFuture<Long> zcard(String key);

/**
* Removes and returns up to <code>count</code> members with the lowest scores from the sorted set
* stored at the specified <code>key</code>.
*
* @see <a href="https://redis.io/commands/zpopmin/">redis.io</a> for more details.
* @param key The key of the sorted set.
* @param count Specifies the quantity of members to pop.<br>
* If <code>count</code> is higher than the sorted set's cardinality, returns all members and
* their scores, ordered from lowest to highest.
* @return A map of the removed members and their scores, ordered from the one with the lowest
* score to the one with the highest.<br>
* If <code>key</code> doesn't exist, it will be treated as an empty sorted set and the
* command returns an empty <code>Map</code>.
* @example
* <pre>{@code
* Map<String, Double> payload = client.zpopmax("mySortedSet", 2).get();
* assert payload.equals(Map.of('member3', 7.5 , 'member2', 8.0)); // Indicates that 'member3' with a score of 7.5 and 'member2' with a score of 8.0 have been removed from the sorted set.
* }</pre>
*/
CompletableFuture<Map<String, Double>> zpopmin(String key, long count);

/**
* Removes and returns the member with the lowest score from the sorted set stored at the
* specified <code>key</code>.
*
* @see <a href="https://redis.io/commands/zpopmin/">redis.io</a> for more details.
* @param key The key of the sorted set.
* @return A map containing the removed member and its corresponding score.<br>
* If <code>key</code> doesn't exist, it will be treated as an empty sorted set and the
* command returns an empty <code>Map</code>.
* @example
* <pre>{@code
* Map<String, Double> payload = client.zpopmin("mySortedSet").get();
* assert payload.equals(Map.of('member1', 5.0)); // Indicates that 'member1' with a score of 5.0 has been removed from the sorted set.
* }</pre>
*/
CompletableFuture<Map<String, Double>> zpopmin(String key);

/**
* Removes and returns up to <code>count</code> members with the highest scores from the sorted
* set stored at the specified <code>key</code>.
Expand Down
37 changes: 37 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 @@ -57,6 +57,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.ZPopMax;
import static redis_request.RedisRequestOuterClass.RequestType.ZPopMin;
import static redis_request.RedisRequestOuterClass.RequestType.ZScore;
import static redis_request.RedisRequestOuterClass.RequestType.Zadd;
import static redis_request.RedisRequestOuterClass.RequestType.Zcard;
Expand Down Expand Up @@ -1277,6 +1278,42 @@ public T zcard(@NonNull String key) {
return getThis();
}

/**
* Removes and returns up to <code>count</code> members with the lowest scores from the sorted set
* stored at the specified <code>key</code>.
*
* @see <a href="https://redis.io/commands/zpopmin/">redis.io</a> for more details.
* @param key The key of the sorted set.
* @param count Specifies the quantity of members to pop.<br>
* If <code>count</code> is higher than the sorted set's cardinality, returns all members and
* their scores, ordered from lowest to highest.
* @return Command Response - A map of the removed members and their scores, ordered from the one
* with the lowest score to the one with the highest.<br>
* If <code>key</code> doesn't exist, it will be treated as an empty sorted set and the
* command returns an empty <code>Map</code>.
*/
public T zpopmin(@NonNull String key, long count) {
ArgsArray commandArgs = buildArgs(new String[] {key, Long.toString(count)});
protobufTransaction.addCommands(buildCommand(ZPopMin, commandArgs));
return getThis();
}

/**
* Removes and returns the member with the lowest score from the sorted set stored at the
* specified <code>key</code>.
*
* @see <a href="https://redis.io/commands/zpopmin/">redis.io</a> for more details.
* @param key The key of the sorted set.
* @return Command Response - A map containing the removed member and its corresponding score.<br>
* If <code>key</code> doesn't exist, it will be treated as an empty sorted set and the
* command returns an empty <code>Map</code>.
*/
public T zpopmin(@NonNull String key) {
ArgsArray commandArgs = buildArgs(new String[] {key});
protobufTransaction.addCommands(buildCommand(ZPopMin, commandArgs));
return getThis();
}

/**
* Removes and returns up to <code>count</code> members with the highest scores from the sorted
* set stored at the specified <code>key</code>.
Expand Down
50 changes: 50 additions & 0 deletions java/client/src/test/java/glide/api/RedisClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.ZPopMax;
import static redis_request.RedisRequestOuterClass.RequestType.ZPopMin;
import static redis_request.RedisRequestOuterClass.RequestType.ZScore;
import static redis_request.RedisRequestOuterClass.RequestType.Zadd;
import static redis_request.RedisRequestOuterClass.RequestType.Zcard;
Expand Down Expand Up @@ -1790,6 +1791,55 @@ public void zcard_returns_success() {
assertEquals(value, payload);
}

@SneakyThrows
@Test
public void zpopmin_returns_success() {
// setup
String key = "testKey";
String[] arguments = new String[] {key};
Map<String, Double> value = Map.of("member1", 2.5);

CompletableFuture<Map<String, Double>> testResponse = new CompletableFuture<>();
testResponse.complete(value);

// match on protobuf request
when(commandManager.<Map<String, Double>>submitNewCommand(eq(ZPopMin), eq(arguments), any()))
.thenReturn(testResponse);

// exercise
CompletableFuture<Map<String, Double>> response = service.zpopmin(key);
Map<String, Double> payload = response.get();

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

@SneakyThrows
@Test
public void zpopmin_with_count_returns_success() {
// setup
String key = "testKey";
long count = 2L;
String[] arguments = new String[] {key, Long.toString(count)};
Map<String, Double> value = Map.of("member1", 2.0, "member2", 3.0);

CompletableFuture<Map<String, Double>> testResponse = new CompletableFuture<>();
testResponse.complete(value);

// match on protobuf request
when(commandManager.<Map<String, Double>>submitNewCommand(eq(ZPopMin), eq(arguments), any()))
.thenReturn(testResponse);

// exercise
CompletableFuture<Map<String, Double>> response = service.zpopmin(key, count);
Map<String, Double> payload = response.get();

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

@SneakyThrows
@Test
public void zpopmax_returns_success() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.ZPopMax;
import static redis_request.RedisRequestOuterClass.RequestType.ZPopMin;
import static redis_request.RedisRequestOuterClass.RequestType.ZScore;
import static redis_request.RedisRequestOuterClass.RequestType.Zadd;
import static redis_request.RedisRequestOuterClass.RequestType.Zcard;
Expand Down Expand Up @@ -389,6 +390,12 @@ public void transaction_builds_protobuf_request(BaseTransaction<?> transaction)
transaction.zcard("key");
results.add(Pair.of(Zcard, ArgsArray.newBuilder().addArgs("key").build()));

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

transaction.zpopmin("key", 2);
results.add(Pair.of(ZPopMin, ArgsArray.newBuilder().addArgs("key").addArgs("2").build()));

transaction.zpopmax("key");
results.add(Pair.of(ZPopMax, ArgsArray.newBuilder().addArgs("key").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 @@ -1113,6 +1113,25 @@ public void zcard(BaseClient client) {
assertTrue(executionException.getCause() instanceof RequestException);
}

@SneakyThrows
@ParameterizedTest
@MethodSource("getClients")
public void zpopmin(BaseClient client) {
String key = UUID.randomUUID().toString();
Map<String, Double> membersScores = Map.of("a", 1.0, "b", 2.0, "c", 3.0);
assertEquals(3, client.zadd(key, membersScores).get());
assertEquals(Map.of("a", 1.0), client.zpopmin(key).get());
assertEquals(Map.of("b", 2.0, "c", 3.0), client.zpopmin(key, 3).get());
assertTrue(client.zpopmin(key).get().isEmpty());
assertTrue(client.zpopmin("non_existing_key").get().isEmpty());

// Key exists, but it is not a set
assertEquals(OK, client.set(key, "value").get());
ExecutionException executionException =
assertThrows(ExecutionException.class, () -> client.zpopmin(key).get());
assertTrue(executionException.getCause() instanceof RequestException);
}

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

baseTransaction.configSet(Map.of("timeout", "1000"));
Expand Down Expand Up @@ -150,6 +151,7 @@ public static Object[] transactionTestResult() {
1L,
2L,
2.0, // zscore(key8, "two")
Map.of("two", 2.0), // zpopmin(key8)
Map.of("three", 3.0), // zpopmax(key8)
OK,
Map.of("timeout", "1000"),
Expand Down

0 comments on commit 4c34fd0

Please sign in to comment.