Skip to content

Commit

Permalink
Java: add XDEL command (#318) (#1490)
Browse files Browse the repository at this point in the history
* moving out of place method

* Revert "moving out of place method"

This reverts commit 05a75f0.

* Java: Add XDEL command



* SPOTLESS



* Clean up documentation



* Update test for XDEL



---------

Signed-off-by: Andrew Carbonetto <[email protected]>
Co-authored-by: TJ Zhang <[email protected]>
  • Loading branch information
acarbonetto and TJ Zhang authored May 29, 2024
1 parent 47e5123 commit b78c9d1
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 1 deletion.
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 @@ -196,6 +196,7 @@ enum RequestType {
BLMPop = 158;
XLen = 159;
LSet = 165;
XDel = 166;
}

message Command {
Expand Down
3 changes: 3 additions & 0 deletions glide-core/src/request_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub enum RequestType {
BLMPop = 158,
XLen = 159,
LSet = 165,
XDel = 166,
}

fn get_two_word_command(first: &str, second: &str) -> Cmd {
Expand Down Expand Up @@ -335,6 +336,7 @@ impl From<::protobuf::EnumOrUnknown<ProtobufRequestType>> for RequestType {
ProtobufRequestType::PExpireTime => RequestType::PExpireTime,
ProtobufRequestType::XLen => RequestType::XLen,
ProtobufRequestType::LSet => RequestType::LSet,
ProtobufRequestType::XDel => RequestType::XDel,
}
}
}
Expand Down Expand Up @@ -500,6 +502,7 @@ impl RequestType {
RequestType::PExpireTime => Some(cmd("PEXPIRETIME")),
RequestType::XLen => Some(cmd("XLEN")),
RequestType::LSet => Some(cmd("LSET")),
RequestType::XDel => Some(cmd("XDEL")),
}
}
}
7 changes: 7 additions & 0 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.XAdd;
import static redis_request.RedisRequestOuterClass.RequestType.XDel;
import static redis_request.RedisRequestOuterClass.RequestType.XLen;
import static redis_request.RedisRequestOuterClass.RequestType.XTrim;
import static redis_request.RedisRequestOuterClass.RequestType.ZAdd;
Expand Down Expand Up @@ -1236,6 +1237,12 @@ public CompletableFuture<Long> xlen(@NonNull String key) {
return commandManager.submitNewCommand(XLen, new String[] {key}, this::handleLongResponse);
}

@Override
public CompletableFuture<Long> xdel(@NonNull String key, @NonNull String[] ids) {
String[] arguments = ArrayUtils.addFirst(ids, key);
return commandManager.submitNewCommand(XDel, arguments, this::handleLongResponse);
}

@Override
public CompletableFuture<Long> pttl(@NonNull String key) {
return commandManager.submitNewCommand(PTTL, new String[] {key}, this::handleLongResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,21 @@ public interface StreamBaseCommands {
* }</pre>
*/
CompletableFuture<Long> xlen(String key);

/**
* Removes the specified entries by id from a stream, and returns the number of entries deleted.
*
* @see <a href="https://valkey.io/commands/xdel/">valkey.io</a> for details.
* @param key The key of the stream.
* @param ids An array of entry ids.
* @return The number of entries removed from the stream. This number may be less than the number
* of entries in <code>ids</code>, if the specified <code>ids</code> don't exist in the
* stream.
* @example
* <pre>{@code
* Long num = client.xdel("key", new String[] {"1538561698944-0", "1538561698944-1"}).get();
* assert num == 2L; // Stream marked 2 entries as deleted
* }</pre>
*/
CompletableFuture<Long> xdel(String key, String[] ids);
}
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 @@ -120,6 +120,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.XAdd;
import static redis_request.RedisRequestOuterClass.RequestType.XDel;
import static redis_request.RedisRequestOuterClass.RequestType.XLen;
import static redis_request.RedisRequestOuterClass.RequestType.XTrim;
import static redis_request.RedisRequestOuterClass.RequestType.ZAdd;
Expand Down Expand Up @@ -2647,6 +2648,22 @@ public T xlen(@NonNull String key) {
return getThis();
}

/**
* Removes the specified entries by id from a stream, and returns the number of entries deleted.
*
* @see <a href="https://valkey.io/commands/xdel/">valkey.io</a> for details.
* @param key The key of the stream.
* @param ids An array of entry ids.
* @return Command Response - The number of entries removed from the stream. This number may be
* less than the number of entries in <code>ids</code>, if the specified <code>ids</code>
* don't exist in the stream.
*/
public T xdel(String key, String[] ids) {
ArgsArray commandArgs = buildArgs(ArrayUtils.addFirst(ids, key));
protobufTransaction.addCommands(buildCommand(XDel, commandArgs));
return getThis();
}

/**
* Returns the remaining time to live of <code>key</code> that has a timeout, in milliseconds.
*
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 @@ -141,6 +141,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.XAdd;
import static redis_request.RedisRequestOuterClass.RequestType.XDel;
import static redis_request.RedisRequestOuterClass.RequestType.XLen;
import static redis_request.RedisRequestOuterClass.RequestType.XTrim;
import static redis_request.RedisRequestOuterClass.RequestType.ZAdd;
Expand Down Expand Up @@ -3999,6 +4000,31 @@ public void xlen_returns_success() {
assertEquals(completedResult, payload);
}

@Test
@SneakyThrows
public void xdel_returns_success() {
// setup
String key = "testKey";
String[] ids = {"one-1", "two-2", "three-3"};
Long completedResult = 69L;

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

// match on protobuf request
when(commandManager.<Long>submitNewCommand(
eq(XDel), eq(new String[] {key, "one-1", "two-2", "three-3"}), any()))
.thenReturn(testResponse);

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

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

@SneakyThrows
@Test
public void type_returns_success() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.XAdd;
import static redis_request.RedisRequestOuterClass.RequestType.XDel;
import static redis_request.RedisRequestOuterClass.RequestType.XLen;
import static redis_request.RedisRequestOuterClass.RequestType.XTrim;
import static redis_request.RedisRequestOuterClass.RequestType.ZAdd;
Expand Down Expand Up @@ -681,6 +682,9 @@ InfScoreBound.NEGATIVE_INFINITY, new ScoreBoundary(3, false), new Limit(1, 2)),
transaction.xlen("key");
results.add(Pair.of(XLen, buildArgs("key")));

transaction.xdel("key", new String[] {"12345-1", "98765-4"});
results.add(Pair.of(XDel, buildArgs("key", "12345-1", "98765-4")));

transaction.time();
results.add(Pair.of(Time, buildArgs()));

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

@SneakyThrows
@ParameterizedTest(autoCloseArguments = false)
@MethodSource("getClients")
public void xdel(BaseClient client) {

String key = UUID.randomUUID().toString();
String key2 = UUID.randomUUID().toString();
String streamId1 = "0-1";
String streamId2 = "0-2";
String streamId3 = "0-3";

assertEquals(
streamId1,
client
.xadd(
key,
Map.of("f1", "foo1", "f2", "bar2"),
StreamAddOptions.builder().id(streamId1).build())
.get());
assertEquals(
streamId2,
client
.xadd(
key,
Map.of("f1", "foo1", "f2", "bar2"),
StreamAddOptions.builder().id(streamId2).build())
.get());
assertEquals(2L, client.xlen(key).get());

// Deletes one stream id, and ignores anything invalid:
assertEquals(1L, client.xdel(key, new String[] {streamId1, streamId3}).get());
assertEquals(0L, client.xdel(key2, new String[] {streamId3}).get());

// Throw Exception: Key exists - but it is not a stream
assertEquals(OK, client.set(key2, "xdeltest").get());

ExecutionException executionException =
assertThrows(
ExecutionException.class, () -> client.xdel(key2, new String[] {streamId3}).get());
assertTrue(executionException.getCause() instanceof RequestException);
}

@SneakyThrows
@ParameterizedTest(autoCloseArguments = false)
@MethodSource("getClients")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,14 +540,16 @@ private static Object[] streamCommands(BaseTransaction<?> transaction) {
.xadd(streamKey1, Map.of("field2", "value2"), StreamAddOptions.builder().id("0-2").build())
.xadd(streamKey1, Map.of("field3", "value3"), StreamAddOptions.builder().id("0-3").build())
.xlen(streamKey1)
.xtrim(streamKey1, new MinId(true, "0-2"));
.xtrim(streamKey1, new MinId(true, "0-2"))
.xdel(streamKey1, new String[] {"0-3", "0-5"});

return new Object[] {
"0-1", // xadd(streamKey1, Map.of("field1", "value1"), ... .id("0-1").build());
"0-2", // xadd(streamKey1, Map.of("field2", "value2"), ... .id("0-2").build());
"0-3", // xadd(streamKey1, Map.of("field3", "value3"), ... .id("0-3").build());
3L, // xlen(streamKey1)
1L, // xtrim(streamKey1, new MinId(true, "0-2"))
1L, // .xdel(streamKey1, new String[] {"0-1", "0-5"});
};
}

Expand Down

0 comments on commit b78c9d1

Please sign in to comment.