Merge branch 'slowlog' of git://github.com/ivowiblo/jedis into slowlog
Conflicts: src/main/java/redis/clients/jedis/BinaryClient.java src/main/java/redis/clients/jedis/BinaryJedis.java src/main/java/redis/clients/jedis/Jedis.java src/main/java/redis/clients/jedis/Protocol.java
This commit is contained in:
@@ -7,6 +7,8 @@ import static redis.clients.jedis.Protocol.Keyword.NO;
|
||||
import static redis.clients.jedis.Protocol.Keyword.ONE;
|
||||
import static redis.clients.jedis.Protocol.Keyword.STORE;
|
||||
import static redis.clients.jedis.Protocol.Keyword.WITHSCORES;
|
||||
import static redis.clients.jedis.Protocol.Keyword.RESET;
|
||||
import static redis.clients.jedis.Protocol.Keyword.LEN;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -725,7 +727,8 @@ public class BinaryClient extends Connection {
|
||||
super.disconnect();
|
||||
}
|
||||
|
||||
private void sendEvalCommand(Command command, byte[] script, byte[] keyCount, byte[][] params){
|
||||
private void sendEvalCommand(Command command, byte[] script,
|
||||
byte[] keyCount, byte[][] params) {
|
||||
|
||||
final byte[][] allArgs = new byte[params.length + 2][];
|
||||
|
||||
@@ -766,4 +769,20 @@ public class BinaryClient extends Connection {
|
||||
public void scriptKill() {
|
||||
sendCommand(SCRIPT, Keyword.KILL.raw);
|
||||
}
|
||||
|
||||
public void slowlogGet() {
|
||||
sendCommand(SLOWLOG, Keyword.GET.raw);
|
||||
}
|
||||
|
||||
public void slowlogGet(long entries) {
|
||||
sendCommand(SLOWLOG, Keyword.GET.raw, toByteArray(entries));
|
||||
}
|
||||
|
||||
public void slowlogReset() {
|
||||
sendCommand(SLOWLOG, RESET.raw);
|
||||
}
|
||||
|
||||
public void slowlogLen() {
|
||||
sendCommand(SLOWLOG, LEN.raw);
|
||||
}
|
||||
}
|
||||
@@ -185,8 +185,8 @@ public class BinaryJedis implements BinaryJedisCommands {
|
||||
public Set<byte[]> keys(final byte[] pattern) {
|
||||
checkIsInMulti();
|
||||
client.keys(pattern);
|
||||
final HashSet<byte[]> keySet = new HashSet<byte[]>(client
|
||||
.getBinaryMultiBulkReply());
|
||||
final HashSet<byte[]> keySet = new HashSet<byte[]>(
|
||||
client.getBinaryMultiBulkReply());
|
||||
return keySet;
|
||||
}
|
||||
|
||||
@@ -3012,7 +3012,8 @@ public class BinaryJedis implements BinaryJedisCommands {
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates scripts using the Lua interpreter built into Redis starting from version 2.6.0.
|
||||
* Evaluates scripts using the Lua interpreter built into Redis starting
|
||||
* from version 2.6.0.
|
||||
* <p>
|
||||
*
|
||||
* @return Script result
|
||||
@@ -3022,6 +3023,7 @@ public class BinaryJedis implements BinaryJedisCommands {
|
||||
client.eval(script, toByteArray(keys.size()), getParams(keys, args));
|
||||
return client.getOne();
|
||||
}
|
||||
|
||||
private byte[][] getParams(List<byte[]> keys, List<byte[]> args) {
|
||||
int keyCount = keys.size();
|
||||
byte[][] params = new byte[keyCount + args.size()][];
|
||||
@@ -3034,6 +3036,7 @@ public class BinaryJedis implements BinaryJedisCommands {
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
public Object eval(byte[] script, byte[] keyCount, byte[][] params) {
|
||||
client.setTimeoutInfinite();
|
||||
client.eval(script, keyCount, params);
|
||||
@@ -3059,4 +3062,24 @@ public class BinaryJedis implements BinaryJedisCommands {
|
||||
client.scriptKill();
|
||||
return client.getBinaryBulkReply();
|
||||
}
|
||||
|
||||
public byte[] slowlogReset() {
|
||||
client.slowlogReset();
|
||||
return client.getBinaryBulkReply();
|
||||
}
|
||||
|
||||
public long slowlogLen() {
|
||||
client.slowlogLen();
|
||||
return client.getIntegerReply();
|
||||
}
|
||||
|
||||
public List<byte[]> slowlogGetBinary() {
|
||||
client.slowlogGet();
|
||||
return client.getBinaryMultiBulkReply();
|
||||
}
|
||||
|
||||
public List<byte[]> slowlogGetBinary(long entries) {
|
||||
client.slowlogGet(entries);
|
||||
return client.getBinaryMultiBulkReply();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.util.Set;
|
||||
|
||||
import redis.clients.jedis.BinaryClient.LIST_POSITION;
|
||||
import redis.clients.util.SafeEncoder;
|
||||
import redis.clients.util.Slowlog;
|
||||
|
||||
public class Jedis extends BinaryJedis implements JedisCommands {
|
||||
public Jedis(final String host) {
|
||||
@@ -947,7 +948,8 @@ public class Jedis extends BinaryJedis implements JedisCommands {
|
||||
* @return Multi bulk reply, specifically a list of elements in the
|
||||
* specified range.
|
||||
*/
|
||||
public List<String> lrange(final String key, final long start, final long end) {
|
||||
public List<String> lrange(final String key, final long start,
|
||||
final long end) {
|
||||
checkIsInMulti();
|
||||
client.lrange(key, start, end);
|
||||
return client.getMultiBulkReply();
|
||||
@@ -1267,8 +1269,8 @@ public class Jedis extends BinaryJedis implements JedisCommands {
|
||||
/**
|
||||
* Return the members of a set resulting from the intersection of all the
|
||||
* sets hold at the specified keys. Like in
|
||||
* {@link #lrange(String, long, long) LRANGE} the result is sent to the client
|
||||
* as a multi-bulk reply (see the protocol specification for more
|
||||
* {@link #lrange(String, long, long) LRANGE} the result is sent to the
|
||||
* client as a multi-bulk reply (see the protocol specification for more
|
||||
* information). If just a single key is specified, then this command
|
||||
* produces the same result as {@link #smembers(String) SMEMBERS}. Actually
|
||||
* SMEMBERS is just syntax sugar for SINTER.
|
||||
@@ -2253,9 +2255,7 @@ public class Jedis extends BinaryJedis implements JedisCommands {
|
||||
Set<Tuple> set = new LinkedHashSet<Tuple>();
|
||||
Iterator<String> iterator = membersWithScores.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
set
|
||||
.add(new Tuple(iterator.next(), Double.valueOf(iterator
|
||||
.next())));
|
||||
set.add(new Tuple(iterator.next(), Double.valueOf(iterator.next())));
|
||||
}
|
||||
return set;
|
||||
}
|
||||
@@ -2775,4 +2775,14 @@ public class Jedis extends BinaryJedis implements JedisCommands {
|
||||
client.scriptLoad(script);
|
||||
return client.getBulkReply();
|
||||
}
|
||||
|
||||
public List<Slowlog> slowlogGet() {
|
||||
client.slowlogGet();
|
||||
return Slowlog.from(client.getObjectMultiBulkReply());
|
||||
}
|
||||
|
||||
public List<Slowlog> slowlogGet(long entries) {
|
||||
client.slowlogGet(entries);
|
||||
return Slowlog.from(client.getObjectMultiBulkReply());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,14 +29,12 @@ public final class Protocol {
|
||||
}
|
||||
|
||||
public static void sendCommand(final RedisOutputStream os,
|
||||
final Command command,
|
||||
final byte[]... args) {
|
||||
final Command command, final byte[]... args) {
|
||||
sendCommand(os, command.raw, args);
|
||||
}
|
||||
|
||||
private static void sendCommand(final RedisOutputStream os,
|
||||
final byte[] command,
|
||||
final byte[]... args) {
|
||||
final byte[] command, final byte[]... args) {
|
||||
try {
|
||||
os.write(ASTERISK_BYTE);
|
||||
os.writeIntCrLf(args.length + 1);
|
||||
@@ -146,7 +144,7 @@ public final class Protocol {
|
||||
}
|
||||
|
||||
public static enum Command {
|
||||
PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREVRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT, SETRANGE, GETRANGE, EVAL, EVALSHA, SCRIPT;
|
||||
PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREVRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT, SETRANGE, GETRANGE, EVAL, EVALSHA, SCRIPT, SLOWLOG;
|
||||
|
||||
public final byte[] raw;
|
||||
|
||||
@@ -156,7 +154,7 @@ public final class Protocol {
|
||||
}
|
||||
|
||||
public static enum Keyword {
|
||||
AGGREGATE, ALPHA, ASC, BY, DESC, GET, LIMIT, MESSAGE, NO, NOSORT, PMESSAGE, PSUBSCRIBE, PUNSUBSCRIBE, OK, ONE, QUEUED, SET, STORE, SUBSCRIBE, UNSUBSCRIBE, WEIGHTS, WITHSCORES, RESETSTAT, FLUSH, EXISTS, LOAD, KILL;
|
||||
AGGREGATE, ALPHA, ASC, BY, DESC, GET, LIMIT, MESSAGE, NO, NOSORT, PMESSAGE, PSUBSCRIBE, PUNSUBSCRIBE, OK, ONE, QUEUED, SET, STORE, SUBSCRIBE, UNSUBSCRIBE, WEIGHTS, WITHSCORES, RESETSTAT, RESET, FLUSH, EXISTS, LOAD, KILL, LEN;
|
||||
public final byte[] raw;
|
||||
|
||||
Keyword() {
|
||||
|
||||
53
src/main/java/redis/clients/util/Slowlog.java
Normal file
53
src/main/java/redis/clients/util/Slowlog.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package redis.clients.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Slowlog {
|
||||
private final long id;
|
||||
private final long timeStamp;
|
||||
private final long executionTime;
|
||||
private final List<String> args;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<Slowlog> from(List<Object> nestedMultiBulkReply){
|
||||
List<Slowlog> logs = new ArrayList<Slowlog>(nestedMultiBulkReply.size());
|
||||
for(Object obj : nestedMultiBulkReply){
|
||||
List<Object> properties = (List<Object>)obj;
|
||||
logs.add(new Slowlog(properties));
|
||||
}
|
||||
|
||||
return logs;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Slowlog(List<Object> properties) {
|
||||
super();
|
||||
this.id = (Long)properties.get(0);
|
||||
this.timeStamp = (Long)properties.get(1);
|
||||
this.executionTime = (Long)properties.get(2);
|
||||
|
||||
List<byte[]> bargs = (List<byte[]>)properties.get(3);
|
||||
this.args = new ArrayList<String>(bargs.size());
|
||||
|
||||
for(byte[] barg:bargs){
|
||||
this.args.add(SafeEncoder.encode(barg));
|
||||
}
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public long getTimeStamp() {
|
||||
return timeStamp;
|
||||
}
|
||||
|
||||
public long getExecutionTime() {
|
||||
return executionTime;
|
||||
}
|
||||
|
||||
public List<String> getArgs() {
|
||||
return args;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package redis.clients.jedis.tests.commands;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.util.Slowlog;
|
||||
|
||||
public class SlowlogCommandsTest extends JedisCommandTestBase {
|
||||
|
||||
@Test
|
||||
public void slowlog() {
|
||||
//do something
|
||||
jedis.set("foo", "bar");
|
||||
jedis.set("foo2", "bar2");
|
||||
|
||||
List<Slowlog> reducedLog = jedis.slowlogGet(1);
|
||||
assertEquals(1, reducedLog.size());
|
||||
|
||||
Slowlog log = reducedLog.get(0);
|
||||
assertTrue(log.getId() > 0);
|
||||
assertTrue(log.getTimeStamp() > 0);
|
||||
assertTrue(log.getExecutionTime() > 0);
|
||||
assertNotNull(log.getArgs());
|
||||
|
||||
List<byte[]> breducedLog = jedis.slowlogGetBinary(1);
|
||||
assertEquals(1, breducedLog.size());
|
||||
|
||||
List<Slowlog> log1 = jedis.slowlogGet();
|
||||
List<byte[]> blog1 = jedis.slowlogGetBinary();
|
||||
|
||||
assertNotNull(log1);
|
||||
assertNotNull(blog1);
|
||||
|
||||
long len1 = jedis.slowlogLen();
|
||||
|
||||
jedis.slowlogReset();
|
||||
|
||||
List<Slowlog> log2 = jedis.slowlogGet();
|
||||
List<byte[]> blog2 = jedis.slowlogGetBinary();
|
||||
long len2 = jedis.slowlogLen();
|
||||
|
||||
assertTrue(len1 > len2);
|
||||
assertTrue(log1.size() > log2.size());
|
||||
assertTrue(blog1.size() > blog2.size());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user