Skip to content

Commit

Permalink
Implements objecthelp and objectFREQ method. (redis#2141)
Browse files Browse the repository at this point in the history
* Implements objecthelp and objectFREQ method.
  • Loading branch information
dengliming authored Apr 13, 2020
1 parent ab36b36 commit 8135738
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pidfile /tmp/redis8.pid
logfile /tmp/redis8.log
save ""
appendonly no
maxmemory-policy allkeys-lfu
endef

# SENTINELS
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</scm>

<properties>
<redis-hosts>localhost:6379,localhost:6380,localhost:6381,localhost:6382,localhost:6383,localhost:6384,localhost:6385</redis-hosts>
<redis-hosts>localhost:6379,localhost:6380,localhost:6381,localhost:6382,localhost:6383,localhost:6384,localhost:6385,localhost:6386</redis-hosts>
<sentinel-hosts>localhost:26379,localhost:26380,localhost:26381</sentinel-hosts>
<cluster-hosts>localhost:7379,localhost:7380,localhost:7381,localhost:7382,localhost:7383,localhost:7384,localhost:7385</cluster-hosts>
<github.global.server>github</github.global.server>
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import static redis.clients.jedis.Protocol.Keyword.RESET;
import static redis.clients.jedis.Protocol.Keyword.STORE;
import static redis.clients.jedis.Protocol.Keyword.WITHSCORES;
import static redis.clients.jedis.Protocol.Keyword.FREQ;
import static redis.clients.jedis.Protocol.Keyword.HELP;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -1003,6 +1005,14 @@ public void objectEncoding(final byte[] key) {
sendCommand(OBJECT, ENCODING.raw, key);
}

public void objectHelp() {
sendCommand(OBJECT, HELP.raw);
}

public void objectFreq(final byte[] key) {
sendCommand(OBJECT, FREQ.raw, key);
}

public void bitcount(final byte[] key) {
sendCommand(BITCOUNT, key);
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -3482,6 +3482,18 @@ public Long objectIdletime(final byte[] key) {
return client.getIntegerReply();
}

@Override
public List<byte[]> objectHelpBinary() {
client.objectHelp();
return client.getBinaryMultiBulkReply();
}

@Override
public Long objectFreq(final byte[] key) {
client.objectFreq(key);
return client.getIntegerReply();
}

@Override
public Long bitcount(final byte[] key) {
checkIsInMultiOrPipeline();
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryShardedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,16 @@ public Long objectIdletime(final byte[] key) {
return j.objectIdletime(key);
}

public List<String> objectHelp() {
Jedis j = getShard("null");
return j.objectHelp();
}

public Long objectFreq(final byte[] key) {
Jedis j = getShard(key);
return j.objectIdletime(key);
}

@Override
public Boolean setbit(final byte[] key, final long offset, boolean value) {
Jedis j = getShard(key);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/redis/clients/jedis/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,11 @@ public void objectEncoding(final String key) {
objectEncoding(SafeEncoder.encode(key));
}

@Override
public void objectFreq(final String key) {
objectFreq(SafeEncoder.encode(key));
}

@Override
public void bitcount(final String key) {
bitcount(SafeEncoder.encode(key));
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -2962,6 +2962,18 @@ public Long objectIdletime(final String key) {
return client.getIntegerReply();
}

@Override
public List<String> objectHelp() {
client.objectHelp();
return client.getMultiBulkReply();
}

@Override
public Long objectFreq(final String key) {
client.objectFreq(key);
return client.getIntegerReply();
}

@Override
public Long bitcount(final String key) {
checkIsInMultiOrPipeline();
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/redis/clients/jedis/PipelineBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,18 @@ public Response<Long> objectIdletime(final byte[] key) {
return getResponse(BuilderFactory.LONG);
}

@Override
public Response<Long> objectFreq(byte[] key) {
getClient(key).objectFreq(key);
return getResponse(BuilderFactory.LONG);
}

@Override
public Response<Long> objectFreq(String key) {
getClient(key).objectFreq(key);
return getResponse(BuilderFactory.LONG);
}

@Override
public Response<Long> pexpire(final String key, final long milliseconds) {
getClient(key).pexpire(key, milliseconds);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/redis/clients/jedis/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public static enum Keyword {
RESETSTAT, REWRITE, RESET, FLUSH, EXISTS, LOAD, KILL, LEN, REFCOUNT, ENCODING, IDLETIME,
GETNAME, SETNAME, LIST, MATCH, COUNT, PING, PONG, UNLOAD, REPLACE, KEYS, PAUSE, DOCTOR,
BLOCK, NOACK, STREAMS, KEY, CREATE, MKSTREAM, SETID, DESTROY, DELCONSUMER, MAXLEN, GROUP,
IDLE, TIME, RETRYCOUNT, FORCE, STREAM, GROUPS, CONSUMERS,
IDLE, TIME, RETRYCOUNT, FORCE, STREAM, GROUPS, CONSUMERS, HELP, FREQ,
SETUSER, GETUSER, DELUSER, WHOAMI, CAT, GENPASS, USERS;

public final byte[] raw;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public interface AdvancedBinaryJedisCommands {

Long objectIdletime(byte[] key);

List<byte[]> objectHelpBinary();

Long objectFreq(byte[] key);

String migrate(String host, int port, byte[] key, int destinationDB, int timeout);

String migrate(String host, int port, int destinationDB, int timeout, MigrateParams params, byte[]... keys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public interface AdvancedJedisCommands {

Long objectIdletime(String key);

List<String> objectHelp();

Long objectFreq(String key);

String migrate(String host, int port, String key, int destinationDB, int timeout);

String migrate(String host, int port, int destinationDB, int timeout, MigrateParams params, String... keys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ Response<List<byte[]>> xclaim(byte[] key, byte[] group, byte[] consumername, lon

Response<Long> objectIdletime(byte[] key);

Response<Long> objectFreq(byte[] key);

Response<Double> incrByFloat(byte[] key, double increment);

Response<String> psetex(byte[] key, long milliseconds, byte[] value);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/commands/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ void zrevrangeByScoreWithScores(String key, String max, String min,

void objectEncoding(String key);

void objectHelp();

void objectFreq(String key);

void bitcount(String key);

void bitcount(String key, long start, long end);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/redis/clients/jedis/commands/RedisPipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ Response<Set<Tuple>> zrangeByScoreWithScores(String key, String min, String max,

Response<Long> objectIdletime(String key);

Response<Long> objectFreq(String key);

Response<Double> incrByFloat(String key, double increment);

Response<String> psetex(String key, long milliseconds, String value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
package redis.clients.jedis.tests.commands;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.util.SafeEncoder;

import java.util.List;

public class ObjectCommandsTest extends JedisCommandTestBase {

private String key = "mylist";
private byte[] binaryKey = SafeEncoder.encode(key);
private static final HostAndPort lfuHnp = HostAndPortUtil.getRedisServers().get(7);
private Jedis lfuJedis;

@Before
public void setUp() throws Exception {
super.setUp();

lfuJedis = new Jedis(lfuHnp.getHost(), lfuHnp.getPort(), 500);
lfuJedis.connect();
lfuJedis.flushAll();
}

@After
public void tearDown() throws Exception {
lfuJedis.disconnect();
super.tearDown();
}

@Test
public void objectRefcount() {
Expand Down Expand Up @@ -45,4 +74,36 @@ public void objectIdletime() throws InterruptedException {
time = jedis.objectIdletime(binaryKey);
assertEquals(new Long(0), time);
}
}

@Test
public void objectHelp() {
// String
List<String> helpTexts = jedis.objectHelp();
assertNotNull(helpTexts);

// Binary
List<byte[]> helpBinaryTexts = jedis.objectHelpBinary();
assertNotNull(helpBinaryTexts);
}

@Test
public void objectFreq() {
lfuJedis.set(key, "test1");
lfuJedis.get(key);
// String
Long count = lfuJedis.objectFreq(key);
assertTrue(count > 0);

// Binary
count = lfuJedis.objectFreq(binaryKey);
assertTrue(count > 0);

assertNull(lfuJedis.objectFreq("no_such_key"));

try {
jedis.set(key, "test2");
jedis.objectFreq(key);
fail("Freq is only allowed with LFU policy");
}catch(JedisDataException e) {}
}
}

0 comments on commit 8135738

Please sign in to comment.