Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support objectEncoding, objectFreq, objectIdletime and objectRefcount… #1642

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -438,6 +438,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 @@ -458,6 +476,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 @@ -476,6 +514,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 @@ -494,6 +550,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 @@ -5376,6 +5376,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 @@ -5398,6 +5421,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 @@ -5420,6 +5465,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 @@ -5442,6 +5510,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
17 changes: 9 additions & 8 deletions java/integTest/src/test/java/glide/SharedCommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -4332,11 +4332,11 @@ public void objectEncoding_returns_null(BaseClient client) {
@ParameterizedTest(autoCloseArguments = false)
@MethodSource("getClients")
public void objectEncoding_returns_string_raw(BaseClient client) {
String stringRawKey = UUID.randomUUID().toString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have both uses cases: String and GlideString

GlideString stringRawKey = gs(UUID.randomUUID().toString());
assertEquals(
OK,
client
.set(stringRawKey, "a really loooooooooooooooooooooooooooooooooooooooong value")
.set(stringRawKey, gs("a really loooooooooooooooooooooooooooooooooooooooong value"))
.get());
assertEquals("raw", client.objectEncoding(stringRawKey).get());
}
Expand All @@ -4345,8 +4345,8 @@ public void objectEncoding_returns_string_raw(BaseClient client) {
@ParameterizedTest(autoCloseArguments = false)
@MethodSource("getClients")
public void objectEncoding_returns_string_int(BaseClient client) {
String stringIntKey = UUID.randomUUID().toString();
assertEquals(OK, client.set(stringIntKey, "2").get());
GlideString stringIntKey = gs(UUID.randomUUID().toString());
assertEquals(OK, client.set(stringIntKey, gs("2")).get());
assertEquals("int", client.objectEncoding(stringIntKey).get());
}

Expand Down Expand Up @@ -4465,6 +4465,7 @@ public void objectEncoding_returns_stream(BaseClient client) {
public void objectFreq_returns_null(BaseClient client) {
String nonExistingKey = UUID.randomUUID().toString();
assertNull(client.objectFreq(nonExistingKey).get());
assertNull(client.objectFreq(gs(nonExistingKey)).get());
}

@SneakyThrows
Expand All @@ -4479,8 +4480,8 @@ public void objectIdletime_returns_null(BaseClient client) {
@ParameterizedTest(autoCloseArguments = false)
@MethodSource("getClients")
public void objectIdletime(BaseClient client) {
String key = UUID.randomUUID().toString();
assertEquals(OK, client.set(key, "").get());
GlideString key = gs(UUID.randomUUID().toString());
assertEquals(OK, client.set(key, gs("")).get());
Thread.sleep(2000);
assertTrue(client.objectIdletime(key).get() > 0L);
}
Expand All @@ -4497,8 +4498,8 @@ public void objectRefcount_returns_null(BaseClient client) {
@ParameterizedTest(autoCloseArguments = false)
@MethodSource("getClients")
public void objectRefcount(BaseClient client) {
String key = UUID.randomUUID().toString();
assertEquals(OK, client.set(key, "").get());
GlideString key = gs(UUID.randomUUID().toString());
assertEquals(OK, client.set(key, gs("")).get());
assertTrue(client.objectRefcount(key).get() >= 0L);
}

Expand Down
Loading