Skip to content

Commit

Permalink
Java: Add command Touch. (Generic Command Group) (#1354)
Browse files Browse the repository at this point in the history
* Java: Add command `Touch`. (Generic Command Group) (#249)

* PR comments.
  • Loading branch information
SanHalacogluImproving authored Apr 29, 2024
1 parent e9844c8 commit 0cede8d
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 0 deletions.
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 @@ -171,6 +171,7 @@ enum RequestType {
GeoPos = 128;
BZPopMax = 129;
ObjectFreq = 130;
Touch = 132;
}

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 @@ -139,6 +139,7 @@ pub enum RequestType {
GeoPos = 128,
BZPopMax = 129,
ObjectFreq = 130,
Touch = 132,
}

fn get_two_word_command(first: &str, second: &str) -> Cmd {
Expand Down Expand Up @@ -281,6 +282,7 @@ impl From<::protobuf::EnumOrUnknown<ProtobufRequestType>> for RequestType {
ProtobufRequestType::LOLWUT => RequestType::LOLWUT,
ProtobufRequestType::GeoPos => RequestType::GeoPos,
ProtobufRequestType::BZPopMax => RequestType::BZPopMax,
ProtobufRequestType::Touch => RequestType::Touch,
}
}
}
Expand Down Expand Up @@ -419,6 +421,7 @@ impl RequestType {
RequestType::LOLWUT => Some(cmd("LOLWUT")),
RequestType::GeoPos => Some(cmd("GEOPOS")),
RequestType::BZPopMax => Some(cmd("BZPOPMAX")),
RequestType::Touch => Some(cmd("TOUCH")),
}
}
}
6 changes: 6 additions & 0 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.SetString;
import static redis_request.RedisRequestOuterClass.RequestType.Strlen;
import static redis_request.RedisRequestOuterClass.RequestType.TTL;
import static redis_request.RedisRequestOuterClass.RequestType.Touch;
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.XAdd;
Expand Down Expand Up @@ -1034,4 +1035,9 @@ public CompletableFuture<String> pfmerge(
String[] arguments = ArrayUtils.addFirst(sourceKeys, destination);
return commandManager.submitNewCommand(PfMerge, arguments, this::handleStringResponse);
}

@Override
public CompletableFuture<Long> touch(@NonNull String[] keys) {
return commandManager.submitNewCommand(Touch, keys, this::handleLongResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,20 @@ CompletableFuture<Boolean> pexpireAt(
* }</pre>
*/
CompletableFuture<Long> objectRefcount(String key);

/**
* Updates the last access time of specified <code>keys</code>.
*
* @apiNote When in cluster mode, the command may route to multiple nodes when <code>keys</code>
* map to different <code>hash slots</code>.
* @see <a href="https://redis.io/commands/touch/">redis.io</a> for details.
* @param keys The keys to update last access time.
* @return The number of keys that were updated.
* @example
* <pre>{@code
* Long payload = client.touch(new String[] {"myKey1", "myKey2", "nonExistentKey"}).get();
* assert payload == 2L; // Last access time of 2 keys has been updated.
* }</pre>
*/
CompletableFuture<Long> touch(String[] keys);
}
14 changes: 14 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 @@ -87,6 +87,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.Strlen;
import static redis_request.RedisRequestOuterClass.RequestType.TTL;
import static redis_request.RedisRequestOuterClass.RequestType.Time;
import static redis_request.RedisRequestOuterClass.RequestType.Touch;
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.XAdd;
Expand Down Expand Up @@ -2424,6 +2425,19 @@ public T objectRefcount(@NonNull String key) {
return getThis();
}

/**
* Updates the last access time of specified <code>keys</code>.
*
* @see <a href="https://redis.io/commands/touch/">redis.io</a> for details.
* @param keys The keys to update last access time.
* @return Command Response - The number of keys that were updated.
*/
public T touch(@NonNull String[] keys) {
ArgsArray commandArgs = buildArgs(keys);
protobufTransaction.addCommands(buildCommand(Touch, commandArgs));
return getThis();
}

/** Build protobuf {@link Command} object for given command and arguments. */
protected Command buildCommand(RequestType requestType) {
return buildCommand(requestType, buildArgs());
Expand Down
23 changes: 23 additions & 0 deletions java/client/src/test/java/glide/api/RedisClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.Strlen;
import static redis_request.RedisRequestOuterClass.RequestType.TTL;
import static redis_request.RedisRequestOuterClass.RequestType.Time;
import static redis_request.RedisRequestOuterClass.RequestType.Touch;
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.XAdd;
Expand Down Expand Up @@ -3533,4 +3534,26 @@ public void objectRefcount_returns_success() {
assertEquals(testResponse, response);
assertEquals(refcount, payload);
}

@SneakyThrows
@Test
public void touch_returns_success() {
// setup
String[] keys = new String[] {"testKey1", "testKey2"};
Long value = 2L;
CompletableFuture<Long> testResponse = new CompletableFuture<>();
testResponse.complete(value);

// match on protobuf request
when(commandManager.<Long>submitNewCommand(eq(Touch), eq(keys), any()))
.thenReturn(testResponse);

// exercise
CompletableFuture<Long> response = service.touch(keys);
Long payload = response.get();

// verify
assertEquals(testResponse, response);
assertEquals(value, payload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.Strlen;
import static redis_request.RedisRequestOuterClass.RequestType.TTL;
import static redis_request.RedisRequestOuterClass.RequestType.Time;
import static redis_request.RedisRequestOuterClass.RequestType.Touch;
import static redis_request.RedisRequestOuterClass.RequestType.Type;
import static redis_request.RedisRequestOuterClass.RequestType.Unlink;
import static redis_request.RedisRequestOuterClass.RequestType.XAdd;
Expand Down Expand Up @@ -547,6 +548,9 @@ InfScoreBound.NEGATIVE_INFINITY, new ScoreBoundary(3, false), new Limit(1, 2)),
transaction.objectRefcount("key");
results.add(Pair.of(ObjectRefcount, buildArgs("key")));

transaction.touch(new String[] {"key1", "key2"});
results.add(Pair.of(Touch, buildArgs("key1", "key2")));

var protobufTransaction = transaction.getProtobufTransaction().build();

for (int idx = 0; idx < protobufTransaction.getCommandsCount(); idx++) {
Expand Down
16 changes: 16 additions & 0 deletions java/integTest/src/test/java/glide/SharedCommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2842,4 +2842,20 @@ public void objectRefcount(BaseClient client) {
assertEquals(OK, client.set(key, "").get());
assertTrue(client.objectRefcount(key).get() >= 0L);
}

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

assertEquals(OK, client.set(key1, value).get());
assertEquals(OK, client.set(key2, value).get());

assertEquals(2, client.touch(new String[] {key1, key2}).get());
assertEquals(0, client.touch(new String[] {key3}).get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public static BaseTransaction<?> transactionTest(BaseTransaction<?> baseTransact
baseTransaction.exists(new String[] {key1});
baseTransaction.persist(key1);

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

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

Expand Down Expand Up @@ -186,6 +188,7 @@ public static Object[] transactionTestResult() {
new String[] {value1, value2},
1L,
Boolean.FALSE, // persist(key1)
1L, // touch(new String[] {key1})
1L,
null,
1L,
Expand Down

0 comments on commit 0cede8d

Please sign in to comment.