Skip to content

Commit

Permalink
support objectEncoding, objectFreq, objectIdletime and objectRefcount… (
Browse files Browse the repository at this point in the history
valkey-io#1642)

* support objectEncoding, objectFreq, objectIdletime and objectRefcount with GlideString

* add to integration tests the use of the API with GlideString parameters

* add binary version integration tests

* nit: spotlessApply

* fix use of GlideString in objectRefcount_returns_null

---------

Co-authored-by: Ubuntu <[email protected]>
  • Loading branch information
2 people authored and jduo committed Jun 28, 2024
1 parent 595576d commit 1235351
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 0 deletions.
24 changes: 24 additions & 0 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -621,24 +621,48 @@ public CompletableFuture<String> objectEncoding(@NonNull String key) {
ObjectEncoding, new String[] {key}, this::handleStringOrNullResponse);
}

@Override
public CompletableFuture<String> objectEncoding(@NonNull GlideString key) {
return commandManager.submitNewCommand(
ObjectEncoding, new GlideString[] {key}, this::handleStringOrNullResponse);
}

@Override
public CompletableFuture<Long> objectFreq(@NonNull String key) {
return commandManager.submitNewCommand(
ObjectFreq, new String[] {key}, this::handleLongOrNullResponse);
}

@Override
public CompletableFuture<Long> objectFreq(@NonNull GlideString key) {
return commandManager.submitNewCommand(
ObjectFreq, new GlideString[] {key}, this::handleLongOrNullResponse);
}

@Override
public CompletableFuture<Long> objectIdletime(@NonNull String key) {
return commandManager.submitNewCommand(
ObjectIdleTime, new String[] {key}, this::handleLongOrNullResponse);
}

@Override
public CompletableFuture<Long> objectIdletime(@NonNull GlideString key) {
return commandManager.submitNewCommand(
ObjectIdleTime, new GlideString[] {key}, this::handleLongOrNullResponse);
}

@Override
public CompletableFuture<Long> objectRefcount(@NonNull String key) {
return commandManager.submitNewCommand(
ObjectRefCount, new String[] {key}, this::handleLongOrNullResponse);
}

@Override
public CompletableFuture<Long> objectRefcount(@NonNull GlideString key) {
return commandManager.submitNewCommand(
ObjectRefCount, new GlideString[] {key}, this::handleLongOrNullResponse);
}

@Override
public CompletableFuture<String> rename(@NonNull String key, @NonNull String newKey) {
return commandManager.submitNewCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,24 @@ CompletableFuture<Boolean> pexpireAt(
*/
CompletableFuture<String> objectEncoding(String key);

/**
* Returns the internal encoding for the Redis object stored at <code>key</code>.
*
* @see <a href="https://redis.io/commands/object-encoding/">redis.io</a> for details.
* @param key The <code>key</code> of the object to get the internal encoding of.
* @return If <code>key</code> exists, returns the internal encoding of the object stored at
* <code>key</code> as a <code>String</code>. Otherwise, returns <code>null</code>.
* @example
* <pre>{@code
* String encoding = client.objectEncoding(gs("my_hash")).get();
* assert encoding.equals("listpack");
*
* encoding = client.objectEncoding(gs("non_existing_key")).get();
* assert encoding == null;
* }</pre>
*/
CompletableFuture<String> objectEncoding(GlideString key);

/**
* Returns the logarithmic access frequency counter of a Redis object stored at <code>key</code>.
*
Expand All @@ -475,6 +493,26 @@ CompletableFuture<Boolean> pexpireAt(
*/
CompletableFuture<Long> objectFreq(String key);

/**
* Returns the logarithmic access frequency counter of a Redis object stored at <code>key</code>.
*
* @see <a href="https://redis.io/commands/object-freq/">redis.io</a> for details.
* @param key The <code>key</code> of the object to get the logarithmic access frequency counter
* of.
* @return If <code>key</code> exists, returns the logarithmic access frequency counter of the
* object stored at <code>key</code> as a <code>Long</code>. Otherwise, returns <code>null
* </code>.
* @example
* <pre>{@code
* Long frequency = client.objectFreq(gs("my_hash")).get();
* assert frequency == 2L;
*
* frequency = client.objectFreq(gs("non_existing_key")).get();
* assert frequency == null;
* }</pre>
*/
CompletableFuture<Long> objectFreq(GlideString key);

/**
* Returns the time in seconds since the last access to the value stored at <code>key</code>.
*
Expand All @@ -493,6 +531,24 @@ CompletableFuture<Boolean> pexpireAt(
*/
CompletableFuture<Long> objectIdletime(String key);

/**
* Returns the time in seconds since the last access to the value stored at <code>key</code>.
*
* @see <a href="https://redis.io/commands/object-idletime/">redis.io</a> for details.
* @param key The <code>key</code> of the object to get the idle time of.
* @return If <code>key</code> exists, returns the idle time in seconds. Otherwise, returns <code>
* null</code>.
* @example
* <pre>{@code
* Long idletime = client.objectIdletime(gs("my_hash")).get();
* assert idletime == 2L;
*
* idletime = client.objectIdletime(gs("non_existing_key")).get();
* assert idletime == null;
* }</pre>
*/
CompletableFuture<Long> objectIdletime(GlideString key);

/**
* Returns the reference count of the object stored at <code>key</code>.
*
Expand All @@ -511,6 +567,24 @@ CompletableFuture<Boolean> pexpireAt(
*/
CompletableFuture<Long> objectRefcount(String key);

/**
* Returns the reference count of the object stored at <code>key</code>.
*
* @see <a href="https://redis.io/commands/object-refcount/">redis.io</a> for details.
* @param key The <code>key</code> of the object to get the reference count of.
* @return If <code>key</code> exists, returns the reference count of the object stored at <code>
* key</code> as a <code>Long</code>. Otherwise, returns <code>null</code>.
* @example
* <pre>{@code
* Long refcount = client.objectRefcount(gs("my_hash")).get();
* assert refcount == 2L;
*
* refcount = client.objectRefcount(gs("non_existing_key")).get();
* assert refcount == null;
* }</pre>
*/
CompletableFuture<Long> objectRefcount(GlideString key);

/**
* Renames <code>key</code> to <code>newKey</code>.<br>
* If <code>newKey</code> already exists it is overwritten.
Expand Down
91 changes: 91 additions & 0 deletions java/client/src/test/java/glide/api/RedisClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5825,6 +5825,29 @@ public void objectEncoding_returns_success() {
assertEquals(encoding, payload);
}

@SneakyThrows
@Test
public void objectEncoding_binary_returns_success() {
// setup
GlideString key = gs("testKey");
String encoding = "testEncoding";
CompletableFuture<String> testResponse = new CompletableFuture<>();
testResponse.complete(encoding);

// match on protobuf request
when(commandManager.<String>submitNewCommand(
eq(ObjectEncoding), eq(new GlideString[] {key}), any()))
.thenReturn(testResponse);

// exercise
CompletableFuture<String> response = service.objectEncoding(key);
String payload = response.get();

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

@SneakyThrows
@Test
public void objectFreq_returns_success() {
Expand All @@ -5847,6 +5870,28 @@ public void objectFreq_returns_success() {
assertEquals(frequency, payload);
}

@SneakyThrows
@Test
public void objectFreq_binary_returns_success() {
// setup
GlideString key = gs("testKey");
Long frequency = 0L;
CompletableFuture<Long> testResponse = new CompletableFuture<>();
testResponse.complete(frequency);

// match on protobuf request
when(commandManager.<Long>submitNewCommand(eq(ObjectFreq), eq(new GlideString[] {key}), any()))
.thenReturn(testResponse);

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

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

@SneakyThrows
@Test
public void objectIdletime_returns_success() {
Expand All @@ -5869,6 +5914,29 @@ public void objectIdletime_returns_success() {
assertEquals(idletime, payload);
}

@SneakyThrows
@Test
public void objectIdletime_binary_returns_success() {
// setup
GlideString key = gs("testKey");
Long idletime = 0L;
CompletableFuture<Long> testResponse = new CompletableFuture<>();
testResponse.complete(idletime);

// match on protobuf request
when(commandManager.<Long>submitNewCommand(
eq(ObjectIdleTime), eq(new GlideString[] {key}), any()))
.thenReturn(testResponse);

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

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

@SneakyThrows
@Test
public void objectRefcount_returns_success() {
Expand All @@ -5891,6 +5959,29 @@ public void objectRefcount_returns_success() {
assertEquals(refcount, payload);
}

@SneakyThrows
@Test
public void objectRefcount_binary_returns_success() {
// setup
GlideString key = gs("testKey");
Long refcount = 0L;
CompletableFuture<Long> testResponse = new CompletableFuture<>();
testResponse.complete(refcount);

// match on protobuf request
when(commandManager.<Long>submitNewCommand(
eq(ObjectRefCount), eq(new GlideString[] {key}), any()))
.thenReturn(testResponse);

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

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

@SneakyThrows
@Test
public void touch_returns_success() {
Expand Down
Loading

0 comments on commit 1235351

Please sign in to comment.