Skip to content

Commit

Permalink
Java: Add DEL command (valkey-io#961)
Browse files Browse the repository at this point in the history
* Java: Add DEL command (#88)

Signed-off-by: Andrew Carbonetto <[email protected]>

---------

Signed-off-by: Andrew Carbonetto <[email protected]>
  • Loading branch information
acarbonetto authored Feb 20, 2024
1 parent 2b9f9f7 commit c7ee84b
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 0 deletions.
8 changes: 8 additions & 0 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static glide.utils.ArrayTransformUtils.convertMapToArgArray;
import static redis_request.RedisRequestOuterClass.RequestType.Decr;
import static redis_request.RedisRequestOuterClass.RequestType.DecrBy;
import static redis_request.RedisRequestOuterClass.RequestType.Del;
import static redis_request.RedisRequestOuterClass.RequestType.GetString;
import static redis_request.RedisRequestOuterClass.RequestType.HashDel;
import static redis_request.RedisRequestOuterClass.RequestType.HashGet;
Expand All @@ -23,6 +24,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.SetString;

import glide.api.commands.ConnectionManagementCommands;
import glide.api.commands.GenericBaseCommands;
import glide.api.commands.HashCommands;
import glide.api.commands.SetCommands;
import glide.api.commands.StringCommands;
Expand Down Expand Up @@ -53,6 +55,7 @@
@AllArgsConstructor
public abstract class BaseClient
implements AutoCloseable,
GenericBaseCommands,
ConnectionManagementCommands,
StringCommands,
HashCommands,
Expand Down Expand Up @@ -210,6 +213,11 @@ public CompletableFuture<String> ping(@NonNull String str) {
return commandManager.submitNewCommand(Ping, new String[] {str}, this::handleStringResponse);
}

@Override
public CompletableFuture<Long> del(@NonNull String[] keys) {
return commandManager.submitNewCommand(Del, keys, this::handleLongResponse);
}

@Override
public CompletableFuture<String> get(@NonNull String key) {
return commandManager.submitNewCommand(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */
package glide.api.commands;

import java.util.concurrent.CompletableFuture;

/**
* Generic Commands interface to handle generic commands for all server requests for both standalone
* and cluster clients.
*
* @see <a href="https://redis.io/commands/?group=generic">Generic Commands</a>
*/
public interface GenericBaseCommands {

/**
* Removes the specified <code>keys</code> from the database. A key is ignored if it does not
* exist.
*
* @see <a href="https://redis.io/commands/del/">redis.io</a> for details.
* @param keys The keys we wanted to remove.
* @return The number of keys that were removed.
*/
CompletableFuture<Long> del(String[] keys);
}
16 changes: 16 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 @@ -5,6 +5,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.CustomCommand;
import static redis_request.RedisRequestOuterClass.RequestType.Decr;
import static redis_request.RedisRequestOuterClass.RequestType.DecrBy;
import static redis_request.RedisRequestOuterClass.RequestType.Del;
import static redis_request.RedisRequestOuterClass.RequestType.GetString;
import static redis_request.RedisRequestOuterClass.RequestType.HashDel;
import static redis_request.RedisRequestOuterClass.RequestType.HashGet;
Expand Down Expand Up @@ -130,6 +131,21 @@ public T info(InfoOptions options) {
return getThis();
}

/**
* Removes the specified <code>keys</code> from the database. A key is ignored if it does not
* exist.
*
* @see <a href="https://redis.io/commands/del/">redis.io</a> for details.
* @param keys The keys we wanted to remove.
* @return Command Response - The number of keys that were removed.
*/
public T del(String[] keys) {
ArgsArray commandArgs = buildArgs(keys);

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

/**
* Get the value associated with the given key, or null if no such value exists.
*
Expand Down
20 changes: 20 additions & 0 deletions java/client/src/test/java/glide/api/RedisClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.CustomCommand;
import static redis_request.RedisRequestOuterClass.RequestType.Decr;
import static redis_request.RedisRequestOuterClass.RequestType.DecrBy;
import static redis_request.RedisRequestOuterClass.RequestType.Del;
import static redis_request.RedisRequestOuterClass.RequestType.GetString;
import static redis_request.RedisRequestOuterClass.RequestType.HashDel;
import static redis_request.RedisRequestOuterClass.RequestType.HashGet;
Expand Down Expand Up @@ -127,6 +128,25 @@ public void ping_with_message_returns_success() {
assertEquals(message, pong);
}

@SneakyThrows
@Test
public void del_returns_long_success() {
// setup
String[] keys = new String[] {"testKey1", "testKey2"};
Long numberDeleted = 1L;
CompletableFuture<Long> testResponse = mock(CompletableFuture.class);
when(testResponse.get()).thenReturn(numberDeleted);
when(commandManager.<Long>submitNewCommand(eq(Del), eq(keys), any())).thenReturn(testResponse);

// exercise
CompletableFuture<Long> response = service.del(keys);
Long result = response.get();

// verify
assertEquals(testResponse, response);
assertEquals(numberDeleted, result);
}

@SneakyThrows
@Test
public void get_returns_success() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static redis_request.RedisRequestOuterClass.RequestType.Decr;
import static redis_request.RedisRequestOuterClass.RequestType.DecrBy;
import static redis_request.RedisRequestOuterClass.RequestType.Del;
import static redis_request.RedisRequestOuterClass.RequestType.GetString;
import static redis_request.RedisRequestOuterClass.RequestType.HashDel;
import static redis_request.RedisRequestOuterClass.RequestType.HashGet;
Expand Down Expand Up @@ -57,6 +58,9 @@ public void transaction_builds_protobuf_request() {
.addArgs(RETURN_OLD_VALUE)
.build()));

transaction.del(new String[] {"key1", "key2"});
results.add(Pair.of(Del, ArgsArray.newBuilder().addArgs("key1").addArgs("key2").build()));

transaction.ping();
results.add(Pair.of(Ping, ArgsArray.newBuilder().build()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static redis_request.RedisRequestOuterClass.RequestType.Decr;
import static redis_request.RedisRequestOuterClass.RequestType.DecrBy;
import static redis_request.RedisRequestOuterClass.RequestType.Del;
import static redis_request.RedisRequestOuterClass.RequestType.GetString;
import static redis_request.RedisRequestOuterClass.RequestType.HashDel;
import static redis_request.RedisRequestOuterClass.RequestType.HashGet;
Expand Down Expand Up @@ -56,6 +57,9 @@ public void transaction_builds_protobuf_request() {
.addArgs(RETURN_OLD_VALUE)
.build()));

transaction.del(new String[] {"key1", "key2"});
results.add(Pair.of(Del, ArgsArray.newBuilder().addArgs("key1").addArgs("key2").build()));

transaction.ping();
results.add(Pair.of(Ping, ArgsArray.newBuilder().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 @@ -111,6 +111,34 @@ public void get_missing_value(BaseClient client) {
assertNull(data);
}

@SneakyThrows
@ParameterizedTest
@MethodSource("getClients")
public void del_multiple_keys(BaseClient client) {
String key1 = "{key}" + UUID.randomUUID();
String key2 = "{key}" + UUID.randomUUID();
String key3 = "{key}" + UUID.randomUUID();
String value = UUID.randomUUID().toString();

String setResult = client.set(key1, value).get();
assertEquals(OK, setResult);
setResult = client.set(key2, value).get();
assertEquals(OK, setResult);
setResult = client.set(key3, value).get();
assertEquals(OK, setResult);

Long deletedKeysNum = client.del(new String[] {key1, key2, key3}).get();
assertEquals(3L, deletedKeysNum);
}

@SneakyThrows
@ParameterizedTest
@MethodSource("getClients")
public void del_non_existent_key(BaseClient client) {
Long deletedKeysNum = client.del(new String[] {UUID.randomUUID().toString()}).get();
assertEquals(0L, deletedKeysNum);
}

@SneakyThrows
@ParameterizedTest
@MethodSource("getClients")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public static BaseTransaction<?> transactionTest(BaseTransaction<?> baseTransact
baseTransaction.set(key2, value2, SetOptions.builder().returnOldValue(true).build());
baseTransaction.customCommand(new String[] {"MGET", key1, key2});

baseTransaction.del(new String[] {key1});
baseTransaction.get(key1);

baseTransaction.mset(Map.of(key1, value2, key2, value1));
baseTransaction.mget(new String[] {key1, key2});

Expand Down Expand Up @@ -55,6 +58,8 @@ public static Object[] transactionTestResult() {
value1,
null,
new String[] {value1, value2},
1L,
null,
"OK",
new String[] {value2, value1},
1L,
Expand Down

0 comments on commit c7ee84b

Please sign in to comment.