From ce876e2b5673c069984e4648236334e4d8a88674 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 09:21:18 +0100 Subject: [PATCH 01/40] Add a binary sendCommand(...) --- .../java/redis/clients/jedis/Protocol.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index e9e6211..cd23107 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -43,6 +43,26 @@ public final class Protocol { } } + public void sendCommand(RedisOutputStream os, String name, byte[]... args) { + try { + os.write(ASTERISK_BYTE); + os.writeIntCrLf(args.length + 1); + os.write(DOLLAR_BYTE); + os.writeIntCrLf(name.length()); + os.writeAsciiCrLf(name); + + for (final byte[] arg : args) { + os.write(DOLLAR_BYTE); + os.writeIntCrLf(arg.length); + os.write(arg); + os.writeCrLf(); + } + os.flush(); + } catch (IOException e) { + throw new JedisException(e); + } + } + private void processError(RedisInputStream is) { String message = is.readLine(); throw new JedisException(message); From 77590f5b21e7f3335a5d7f0f5b7283c9ce27c718 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 09:25:10 +0100 Subject: [PATCH 02/40] Added final to methods params --- .../java/redis/clients/jedis/Protocol.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index cd23107..972cb31 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -19,7 +19,7 @@ public final class Protocol { public static final byte MINUS_BYTE = '-'; public static final byte COLON_BYTE = ':'; - public void sendCommand(RedisOutputStream os, String name, String... args) { + public void sendCommand(final RedisOutputStream os, final String name, final String... args) { try { os.write(ASTERISK_BYTE); os.writeIntCrLf(args.length + 1); @@ -43,7 +43,7 @@ public final class Protocol { } } - public void sendCommand(RedisOutputStream os, String name, byte[]... args) { + public void sendCommand(final RedisOutputStream os, final String name, final byte[]... args) { try { os.write(ASTERISK_BYTE); os.writeIntCrLf(args.length + 1); @@ -63,12 +63,12 @@ public final class Protocol { } } - private void processError(RedisInputStream is) { + private void processError(final RedisInputStream is) { String message = is.readLine(); throw new JedisException(message); } - private Object process(RedisInputStream is) { + private Object process(final RedisInputStream is) { try { byte b = is.readByte(); if (b == MINUS_BYTE) { @@ -90,11 +90,11 @@ public final class Protocol { return null; } - private String processStatusCodeReply(RedisInputStream is) { + private String processStatusCodeReply(final RedisInputStream is) { return is.readLine(); } - private String processBulkReply(RedisInputStream is) { + private String processBulkReply(final RedisInputStream is) { int len = Integer.parseInt(is.readLine()); if (len == -1) { return null; @@ -115,12 +115,12 @@ public final class Protocol { return new String(read, CHARSET); } - private Integer processInteger(RedisInputStream is) { + private Integer processInteger(final RedisInputStream is) { String num = is.readLine(); return Integer.valueOf(num); } - private List processMultiBulkReply(RedisInputStream is) { + private List processMultiBulkReply(final RedisInputStream is) { int num = Integer.parseInt(is.readLine()); if (num == -1) { return null; @@ -132,7 +132,7 @@ public final class Protocol { return ret; } - public Object read(RedisInputStream is) { + public Object read(final RedisInputStream is) { return process(is); } } \ No newline at end of file From 69c1c644ea1a5097e417ef72abf20690ad6cc716 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 09:26:26 +0100 Subject: [PATCH 03/40] processBulkReply(...) now return a byte[] (instead of String). --- src/main/java/redis/clients/jedis/Protocol.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index 972cb31..0a7f075 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -7,8 +7,6 @@ import java.io.*; import java.util.ArrayList; import java.util.List; -import static redis.clients.util.RedisOutputStream.CHARSET; - public final class Protocol { public static final int DEFAULT_PORT = 6379; @@ -94,7 +92,7 @@ public final class Protocol { return is.readLine(); } - private String processBulkReply(final RedisInputStream is) { + private byte[] processBulkReply(final RedisInputStream is) { int len = Integer.parseInt(is.readLine()); if (len == -1) { return null; @@ -112,7 +110,7 @@ public final class Protocol { throw new JedisException(e); } - return new String(read, CHARSET); + return read; } private Integer processInteger(final RedisInputStream is) { From daec806c5a33b43b9d2cba783ba0c4bda7a54f75 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 09:31:21 +0100 Subject: [PATCH 04/40] UTF-8 charset declared in Protocol, added a getBinaryBulkReply() in Connection --- src/main/java/redis/clients/jedis/Connection.java | 6 +++++- src/main/java/redis/clients/jedis/Protocol.java | 3 +++ src/main/java/redis/clients/util/RedisOutputStream.java | 2 -- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 96bf728..6d0943a 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -122,8 +122,12 @@ public class Connection { } public String getBulkReply() { + return new String(getBinaryBulkReply(), Protocol.UTF8); + } + + public byte[] getBinaryBulkReply() { pipelinedCommands--; - return (String) protocol.read(inputStream); + return (byte[]) protocol.read(inputStream); } public Integer getIntegerReply() { diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index 0a7f075..aef07c3 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -4,6 +4,7 @@ import redis.clients.util.RedisInputStream; import redis.clients.util.RedisOutputStream; import java.io.*; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; @@ -11,6 +12,8 @@ public final class Protocol { public static final int DEFAULT_PORT = 6379; + public static final Charset UTF8 = Charset.forName("UTF-8"); + public static final byte DOLLAR_BYTE = '$'; public static final byte ASTERISK_BYTE = '*'; public static final byte PLUS_BYTE = '+'; diff --git a/src/main/java/redis/clients/util/RedisOutputStream.java b/src/main/java/redis/clients/util/RedisOutputStream.java index c366e02..c5f4bc2 100644 --- a/src/main/java/redis/clients/util/RedisOutputStream.java +++ b/src/main/java/redis/clients/util/RedisOutputStream.java @@ -1,7 +1,6 @@ package redis.clients.util; import java.io.*; -import java.nio.charset.Charset; /** * The class implements a buffered output stream without synchronization @@ -12,7 +11,6 @@ public final class RedisOutputStream extends FilterOutputStream { protected final byte buf[]; protected int count; - public static final Charset CHARSET = Charset.forName("UTF-8"); public RedisOutputStream(OutputStream out) { this(out, 8192); From ddd3932906290e99d7f8706f8d52a0ee5c2903be Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 10:22:39 +0100 Subject: [PATCH 05/40] Code cleanup (final method params) --- src/main/java/redis/clients/jedis/Client.java | 1269 +++++++++-------- .../java/redis/clients/jedis/Connection.java | 25 +- 2 files changed, 699 insertions(+), 595 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index 78d3d73..e52cb66 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -5,603 +5,698 @@ import java.util.List; import java.util.Map; public class Client extends Connection { - public enum LIST_POSITION { - BEFORE, AFTER - } + public enum LIST_POSITION { + BEFORE, AFTER + } - private boolean isInMulti; + private boolean isInMulti; - public boolean isInMulti() { - return isInMulti; - } + public boolean isInMulti() { + return isInMulti; + } - public Client(String host) { - super(host); - } + public Client(final String host) { + super(host); + } - public Client(String host, int port) { - super(host, port); - } + public Client(final String host, final int port) { + super(host, port); + } - public void ping() { - sendCommand("PING"); - } + public void ping() { + sendCommand("PING"); + } - public void set(String key, String value) { - sendCommand("SET", key, value); - } + public void set(final String key, final String value) { + sendCommand("SET", key, value); + } - public void get(String key) { - sendCommand("GET", key); - } + public void get(final String key) { + sendCommand("GET", key); + } - public void quit() { - sendCommand("QUIT"); - } + public void quit() { + sendCommand("QUIT"); + } - public void exists(String key) { - sendCommand("EXISTS", key); - } + public void exists(final String key) { + sendCommand("EXISTS", key); + } - public void del(String... keys) { - sendCommand("DEL", keys); - } + public void del(final String... keys) { + sendCommand("DEL", keys); + } - public void type(String key) { - sendCommand("TYPE", key); - } + public void type(final String key) { + sendCommand("TYPE", key); + } - public void flushDB() { - sendCommand("FLUSHDB"); - } - - public void keys(String pattern) { - sendCommand("KEYS", pattern); - } - - public void randomKey() { - sendCommand("RANDOMKEY"); - } - - public void rename(String oldkey, String newkey) { - sendCommand("RENAME", oldkey, newkey); - } - - public void renamenx(String oldkey, String newkey) { - sendCommand("RENAMENX", oldkey, newkey); - } - - public void dbSize() { - sendCommand("DBSIZE"); - } - - public void expire(String key, int seconds) { - sendCommand("EXPIRE", key, String.valueOf(seconds)); - } - - public void expireAt(String key, long unixTime) { - sendCommand("EXPIREAT", key, String.valueOf(unixTime)); - } - - public void ttl(String key) { - sendCommand("TTL", key); - } - - public void select(int index) { - sendCommand("SELECT", String.valueOf(index)); - } - - public void move(String key, int dbIndex) { - sendCommand("MOVE", key, String.valueOf(dbIndex)); - } - - public void flushAll() { - sendCommand("FLUSHALL"); - } - - public void getSet(String key, String value) { - sendCommand("GETSET", key, value); - } - - public void mget(String... keys) { - sendCommand("MGET", keys); - } - - public void setnx(String key, String value) { - sendCommand("SETNX", key, value); - } - - public void setex(String key, int seconds, String value) { - sendCommand("SETEX", key, String.valueOf(seconds), value); - } - - public void mset(String... keysvalues) { - sendCommand("MSET", keysvalues); - } - - public void msetnx(String... keysvalues) { - sendCommand("MSETNX", keysvalues); - } - - public void decrBy(String key, int integer) { - sendCommand("DECRBY", key, String.valueOf(integer)); - } - - public void decr(String key) { - sendCommand("DECR", key); - } - - public void incrBy(String key, int integer) { - sendCommand("INCRBY", key, String.valueOf(integer)); - } - - public void incr(String key) { - sendCommand("INCR", key); - } - - public void append(String key, String value) { - sendCommand("APPEND", key, value); - } - - public void substr(String key, int start, int end) { - sendCommand("SUBSTR", key, String.valueOf(start), String.valueOf(end)); - } - - public void hset(String key, String field, String value) { - sendCommand("HSET", key, field, value); - } - - public void hget(String key, String field) { - sendCommand("HGET", key, field); - } - - public void hsetnx(String key, String field, String value) { - sendCommand("HSETNX", key, field, value); - } - - public void hmset(String key, Map hash) { - List params = new ArrayList(); - params.add(key); - - for (String field : hash.keySet()) { - params.add(field); - params.add(hash.get(field)); - } - sendCommand("HMSET", params.toArray(new String[params.size()])); - } - - public void hmget(String key, String... fields) { - String[] params = new String[fields.length + 1]; - params[0] = key; - System.arraycopy(fields, 0, params, 1, fields.length); - sendCommand("HMGET", params); - } - - public void hincrBy(String key, String field, int value) { - sendCommand("HINCRBY", key, field, String.valueOf(value)); - } - - public void hexists(String key, String field) { - sendCommand("HEXISTS", key, field); - } - - public void hdel(String key, String field) { - sendCommand("HDEL", key, field); - } - - public void hlen(String key) { - sendCommand("HLEN", key); - } - - public void hkeys(String key) { - sendCommand("HKEYS", key); - } - - public void hvals(String key) { - sendCommand("HVALS", key); - } - - public void hgetAll(String key) { - sendCommand("HGETALL", key); - } - - public void rpush(String key, String string) { - sendCommand("RPUSH", key, string); - } - - public void lpush(String key, String string) { - sendCommand("LPUSH", key, string); - } - - public void llen(String key) { - sendCommand("LLEN", key); - } - - public void lrange(String key, int start, int end) { - sendCommand("LRANGE", key, String.valueOf(start), String.valueOf(end)); - } - - public void ltrim(String key, int start, int end) { - sendCommand("LTRIM", key, String.valueOf(start), String.valueOf(end)); - } - - public void lindex(String key, int index) { - sendCommand("LINDEX", key, String.valueOf(index)); - } - - public void lset(String key, int index, String value) { - sendCommand("LSET", key, String.valueOf(index), value); - } - - public void lrem(String key, int count, String value) { - sendCommand("LREM", key, String.valueOf(count), value); - } - - public void lpop(String key) { - sendCommand("LPOP", key); - } - - public void rpop(String key) { - sendCommand("RPOP", key); - } - - public void rpoplpush(String srckey, String dstkey) { - sendCommand("RPOPLPUSH", srckey, dstkey); - } - - public void sadd(String key, String member) { - sendCommand("SADD", key, member); - } - - public void smembers(String key) { - sendCommand("SMEMBERS", key); - } - - public void srem(String key, String member) { - sendCommand("SREM", key, member); - } - - public void spop(String key) { - sendCommand("SPOP", key); - } - - public void smove(String srckey, String dstkey, String member) { - sendCommand("SMOVE", srckey, dstkey, member); - } - - public void scard(String key) { - sendCommand("SCARD", key); - } - - public void sismember(String key, String member) { - sendCommand("SISMEMBER", key, member); - } - - public void sinter(String... keys) { - sendCommand("SINTER", keys); - } - - public void sinterstore(String dstkey, String... keys) { - String[] params = new String[keys.length + 1]; - params[0] = dstkey; - System.arraycopy(keys, 0, params, 1, keys.length); - sendCommand("SINTERSTORE", params); - } - - public void sunion(String... keys) { - sendCommand("SUNION", keys); - } - - public void sunionstore(String dstkey, String... keys) { - String[] params = new String[keys.length + 1]; - params[0] = dstkey; - System.arraycopy(keys, 0, params, 1, keys.length); - sendCommand("SUNIONSTORE", params); - } - - public void sdiff(String... keys) { - sendCommand("SDIFF", keys); - } - - public void sdiffstore(String dstkey, String... keys) { - String[] params = new String[keys.length + 1]; - params[0] = dstkey; - System.arraycopy(keys, 0, params, 1, keys.length); - sendCommand("SDIFFSTORE", params); - } - - public void srandmember(String key) { - sendCommand("SRANDMEMBER", key); - } - - public void zadd(String key, double score, String member) { - sendCommand("ZADD", key, String.valueOf(score), member); - } - - public void zrange(String key, int start, int end) { - sendCommand("ZRANGE", key, String.valueOf(start), String.valueOf(end)); - } - - public void zrem(String key, String member) { - sendCommand("ZREM", key, member); - } - - public void zincrby(String key, double score, String member) { - sendCommand("ZINCRBY", key, String.valueOf(score), member); - } - - public void zrank(String key, String member) { - sendCommand("ZRANK", key, member); - } - - public void zrevrank(String key, String member) { - sendCommand("ZREVRANK", key, member); - } - - public void zrevrange(String key, int start, int end) { - sendCommand("ZREVRANGE", key, String.valueOf(start), String - .valueOf(end)); - } - - public void zrangeWithScores(String key, int start, int end) { - sendCommand("ZRANGE", key, String.valueOf(start), String.valueOf(end), - "WITHSCORES"); - } - - public void zrevrangeWithScores(String key, int start, int end) { - sendCommand("ZREVRANGE", key, String.valueOf(start), String - .valueOf(end), "WITHSCORES"); - } - - public void zcard(String key) { - sendCommand("ZCARD", key); - } - - public void zscore(String key, String member) { - sendCommand("ZSCORE", key, member); - } - - public void multi() { - sendCommand("MULTI"); - isInMulti = true; - } - - public void discard() { - sendCommand("DISCARD"); - isInMulti = false; - } - - public void exec() { - sendCommand("EXEC"); - isInMulti = false; - } - - public void watch(String key) { - sendCommand("WATCH", key); - } - - public void unwatch() { - sendCommand("UNWATCH"); - } - - public void sort(String key) { - sendCommand("SORT", key); - } - - public void sort(String key, SortingParams sortingParameters) { - List args = new ArrayList(); - args.add(key); - args.addAll(sortingParameters.getParams()); - sendCommand("SORT", args.toArray(new String[args.size()])); - } - - public void blpop(String[] args) { - sendCommand("BLPOP", args); - } - - public void sort(String key, SortingParams sortingParameters, String dstkey) { - List args = new ArrayList(); - args.add(key); - args.addAll(sortingParameters.getParams()); - args.add("STORE"); - args.add(dstkey); - sendCommand("SORT", args.toArray(new String[args.size()])); - } - - public void sort(String key, String dstkey) { - sendCommand("SORT", key, "STORE", dstkey); - } - - public void brpop(String[] args) { - sendCommand("BRPOP", args); - } - - public void auth(String password) { - sendCommand("AUTH", password); - } - - public void subscribe(String... channels) { - sendCommand("SUBSCRIBE", channels); - } - - public void publish(String channel, String message) { - sendCommand("PUBLISH", channel, message); - } - - public void unsubscribe() { - sendCommand("UNSUBSCRIBE"); - } - - public void unsubscribe(String... channels) { - sendCommand("UNSUBSCRIBE", channels); - } - - public void psubscribe(String[] patterns) { - sendCommand("PSUBSCRIBE", patterns); - } - - public void punsubscribe() { - sendCommand("PUNSUBSCRIBE"); - } - - public void punsubscribe(String... patterns) { - sendCommand("PUNSUBSCRIBE", patterns); - } - - public void zcount(String key, double min, double max) { - sendCommand("ZCOUNT", key, String.valueOf(min), String.valueOf(max)); - } - - public void zrangeByScore(String key, double min, double max) { - sendCommand("ZRANGEBYSCORE", key, String.valueOf(min), String - .valueOf(max)); - } - - public void zrangeByScore(String key, String min, String max) { - sendCommand("ZRANGEBYSCORE", key, min, max); - } - - public void zrangeByScore(String key, double min, double max, int offset, - int count) { - sendCommand("ZRANGEBYSCORE", key, String.valueOf(min), String - .valueOf(max), "LIMIT", String.valueOf(offset), String - .valueOf(count)); - } - - public void zrangeByScoreWithScores(String key, double min, double max) { - sendCommand("ZRANGEBYSCORE", key, String.valueOf(min), String - .valueOf(max), "WITHSCORES"); - } - - public void zrangeByScoreWithScores(String key, double min, double max, - int offset, int count) { - sendCommand("ZRANGEBYSCORE", key, String.valueOf(min), String - .valueOf(max), "LIMIT", String.valueOf(offset), String - .valueOf(count), "WITHSCORES"); - } - - public void zremrangeByRank(String key, int start, int end) { - sendCommand("ZREMRANGEBYRANK", key, String.valueOf(start), String - .valueOf(end)); - } - - public void zremrangeByScore(String key, double start, double end) { - sendCommand("ZREMRANGEBYSCORE", key, String.valueOf(start), String - .valueOf(end)); - } - - public void zunionstore(String dstkey, String... sets) { - String[] params = new String[sets.length + 2]; - params[0] = dstkey; - params[1] = String.valueOf(sets.length); - System.arraycopy(sets, 0, params, 2, sets.length); - sendCommand("ZUNIONSTORE", params); - } - - public void zunionstore(String dstkey, ZParams params, String... sets) { - List args = new ArrayList(); - args.add(dstkey); - args.add(String.valueOf(sets.length)); - for (String set : sets) { - args.add(set); - } - args.addAll(params.getParams()); - sendCommand("ZUNIONSTORE", args.toArray(new String[args.size()])); - } - - public void zinterstore(String dstkey, String... sets) { - String[] params = new String[sets.length + 2]; - params[0] = dstkey; - params[1] = String.valueOf(sets.length); - System.arraycopy(sets, 0, params, 2, sets.length); - sendCommand("ZINTERSTORE", params); - } - - public void zinterstore(String dstkey, ZParams params, String... sets) { - List args = new ArrayList(); - args.add(dstkey); - args.add(String.valueOf(sets.length)); - for (String set : sets) { - args.add(set); - } - args.addAll(params.getParams()); - sendCommand("ZINTERSTORE", args.toArray(new String[args.size()])); - } - - public void save() { - sendCommand("SAVE"); - } - - public void bgsave() { - sendCommand("BGSAVE"); - } - - public void bgrewriteaof() { - sendCommand("BGREWRITEAOF"); - } - - public void lastsave() { - sendCommand("LASTSAVE"); - } - - public void shutdown() { - sendCommand("SHUTDOWN"); - } - - public void info() { - sendCommand("INFO"); - } - - public void monitor() { - sendCommand("MONITOR"); - } - - public void slaveof(String host, int port) { - sendCommand("SLAVEOF", host, String.valueOf(port)); - } - - public void slaveofNoOne() { - sendCommand("SLAVEOF", "no", "one"); - } - - public void configGet(String pattern) { - sendCommand("CONFIG", "GET", pattern); - } - - public void configSet(String parameter, String value) { - sendCommand("CONFIG", "SET", parameter, value); - } - - public void strlen(String key) { - sendCommand("STRLEN", key); - } - - public void sync() { - sendCommand("SYNC"); - } - - public void lpushx(String key, String string) { - sendCommand("LPUSHX", key, string); - } - - public void persist(String key) { - sendCommand("PERSIST", key); - } - - public void rpushx(String key, String string) { - sendCommand("RPUSHX", key, string); - } - - public void echo(String string) { - sendCommand("ECHO", string); - } - - public void linsert(String key, LIST_POSITION where, String pivot, - String value) { - sendCommand("LINSERT", key, where.toString(), pivot, value); - } - - public void debug(DebugParams params) { - sendCommand("DEBUG", params.getCommand()); - } + public void flushDB() { + sendCommand("FLUSHDB"); + } + + public void keys(final String pattern) { + sendCommand("KEYS", pattern); + } + + public void randomKey() { + sendCommand("RANDOMKEY"); + } + + public void rename(final String oldkey, final String newkey) { + sendCommand("RENAME", oldkey, newkey); + } + + public void renamenx(final String oldkey, final String newkey) { + sendCommand("RENAMENX", oldkey, newkey); + } + + public void dbSize() { + sendCommand("DBSIZE"); + } + + public void expire(final String key, final int seconds) { + sendCommand("EXPIRE", key, String.valueOf(seconds)); + } + + public void expireAt(final String key, final long unixTime) { + sendCommand("EXPIREAT", key, String.valueOf(unixTime)); + } + + public void ttl(final String key) { + sendCommand("TTL", key); + } + + public void select(final int index) { + sendCommand("SELECT", String.valueOf(index)); + } + + public void move(final String key, final int dbIndex) { + sendCommand("MOVE", key, String.valueOf(dbIndex)); + } + + public void flushAll() { + sendCommand("FLUSHALL"); + } + + public void getSet(final String key, final String value) { + sendCommand("GETSET", key, value); + } + + public void mget(final String... keys) { + sendCommand("MGET", keys); + } + + public void setnx(final String key, final String value) { + sendCommand("SETNX", key, value); + } + + public void setex( + final String key, + final int seconds, + final String value) { + sendCommand("SETEX", key, String.valueOf(seconds), value); + } + + public void mset(final String... keysvalues) { + sendCommand("MSET", keysvalues); + } + + public void msetnx(final String... keysvalues) { + sendCommand("MSETNX", keysvalues); + } + + public void decrBy(final String key, final int integer) { + sendCommand("DECRBY", key, String.valueOf(integer)); + } + + public void decr(final String key) { + sendCommand("DECR", key); + } + + public void incrBy(final String key, final int integer) { + sendCommand("INCRBY", key, String.valueOf(integer)); + } + + public void incr(final String key) { + sendCommand("INCR", key); + } + + public void append(final String key, final String value) { + sendCommand("APPEND", key, value); + } + + public void substr(final String key, final int start, final int end) { + sendCommand("SUBSTR", key, String.valueOf(start), String.valueOf(end)); + } + + public void hset( + final String key, + final String field, + final String value) { + sendCommand("HSET", key, field, value); + } + + public void hget(final String key, final String field) { + sendCommand("HGET", key, field); + } + + public void hsetnx( + final String key, + final String field, + final String value) { + sendCommand("HSETNX", key, field, value); + } + + public void hmset(final String key, final Map hash) { + final List params = new ArrayList(); + params.add(key); + + for (final String field : hash.keySet()) { + params.add(field); + params.add(hash.get(field)); + } + sendCommand("HMSET", params.toArray(new String[params.size()])); + } + + public void hmget(final String key, final String... fields) { + final String[] params = new String[fields.length + 1]; + params[0] = key; + System.arraycopy(fields, 0, params, 1, fields.length); + sendCommand("HMGET", params); + } + + public void hincrBy(final String key, final String field, final int value) { + sendCommand("HINCRBY", key, field, String.valueOf(value)); + } + + public void hexists(final String key, final String field) { + sendCommand("HEXISTS", key, field); + } + + public void hdel(final String key, final String field) { + sendCommand("HDEL", key, field); + } + + public void hlen(final String key) { + sendCommand("HLEN", key); + } + + public void hkeys(final String key) { + sendCommand("HKEYS", key); + } + + public void hvals(final String key) { + sendCommand("HVALS", key); + } + + public void hgetAll(final String key) { + sendCommand("HGETALL", key); + } + + public void rpush(final String key, final String string) { + sendCommand("RPUSH", key, string); + } + + public void lpush(final String key, final String string) { + sendCommand("LPUSH", key, string); + } + + public void llen(final String key) { + sendCommand("LLEN", key); + } + + public void lrange(final String key, final int start, final int end) { + sendCommand("LRANGE", key, String.valueOf(start), String.valueOf(end)); + } + + public void ltrim(final String key, final int start, final int end) { + sendCommand("LTRIM", key, String.valueOf(start), String.valueOf(end)); + } + + public void lindex(final String key, final int index) { + sendCommand("LINDEX", key, String.valueOf(index)); + } + + public void lset(final String key, final int index, final String value) { + sendCommand("LSET", key, String.valueOf(index), value); + } + + public void lrem(final String key, int count, final String value) { + sendCommand("LREM", key, String.valueOf(count), value); + } + + public void lpop(final String key) { + sendCommand("LPOP", key); + } + + public void rpop(final String key) { + sendCommand("RPOP", key); + } + + public void rpoplpush(final String srckey, final String dstkey) { + sendCommand("RPOPLPUSH", srckey, dstkey); + } + + public void sadd(final String key, final String member) { + sendCommand("SADD", key, member); + } + + public void smembers(final String key) { + sendCommand("SMEMBERS", key); + } + + public void srem(final String key, final String member) { + sendCommand("SREM", key, member); + } + + public void spop(final String key) { + sendCommand("SPOP", key); + } + + public void smove( + final String srckey, + final String dstkey, + final String member) { + sendCommand("SMOVE", srckey, dstkey, member); + } + + public void scard(final String key) { + sendCommand("SCARD", key); + } + + public void sismember(final String key, final String member) { + sendCommand("SISMEMBER", key, member); + } + + public void sinter(final String... keys) { + sendCommand("SINTER", keys); + } + + public void sinterstore(final String dstkey, final String... keys) { + String[] params = new String[keys.length + 1]; + params[0] = dstkey; + System.arraycopy(keys, 0, params, 1, keys.length); + sendCommand("SINTERSTORE", params); + } + + public void sunion(final String... keys) { + sendCommand("SUNION", keys); + } + + public void sunionstore(final String dstkey, final String... keys) { + String[] params = new String[keys.length + 1]; + params[0] = dstkey; + System.arraycopy(keys, 0, params, 1, keys.length); + sendCommand("SUNIONSTORE", params); + } + + public void sdiff(final String... keys) { + sendCommand("SDIFF", keys); + } + + public void sdiffstore(final String dstkey, final String... keys) { + String[] params = new String[keys.length + 1]; + params[0] = dstkey; + System.arraycopy(keys, 0, params, 1, keys.length); + sendCommand("SDIFFSTORE", params); + } + + public void srandmember(final String key) { + sendCommand("SRANDMEMBER", key); + } + + public void zadd( + final String key, + final double score, + final String member) { + sendCommand("ZADD", key, String.valueOf(score), member); + } + + public void zrange(final String key, final int start, final int end) { + sendCommand("ZRANGE", key, String.valueOf(start), String.valueOf(end)); + } + + public void zrem(final String key, final String member) { + sendCommand("ZREM", key, member); + } + + public void zincrby( + final String key, + final double score, + final String member) { + sendCommand("ZINCRBY", key, String.valueOf(score), member); + } + + public void zrank(final String key, final String member) { + sendCommand("ZRANK", key, member); + } + + public void zrevrank(final String key, final String member) { + sendCommand("ZREVRANK", key, member); + } + + public void zrevrange( + final String key, + final int start, + final int end) { + sendCommand( + "ZREVRANGE", + key, + String.valueOf(start), + String.valueOf(end)); + } + + public void zrangeWithScores( + final String key, + final int start, + final int end) { + sendCommand( + "ZRANGE", + key, + String.valueOf(start), + String.valueOf(end), + "WITHSCORES"); + } + + public void zrevrangeWithScores( + final String key, + final int start, + final int end) { + sendCommand( + "ZREVRANGE", + key, + String.valueOf(start), + String.valueOf(end), + "WITHSCORES"); + } + + public void zcard(final String key) { + sendCommand("ZCARD", key); + } + + public void zscore(final String key, final String member) { + sendCommand("ZSCORE", key, member); + } + + public void multi() { + sendCommand("MULTI"); + isInMulti = true; + } + + public void discard() { + sendCommand("DISCARD"); + isInMulti = false; + } + + public void exec() { + sendCommand("EXEC"); + isInMulti = false; + } + + public void watch(final String key) { + sendCommand("WATCH", key); + } + + public void unwatch() { + sendCommand("UNWATCH"); + } + + public void sort(final String key) { + sendCommand("SORT", key); + } + + public void sort(final String key, final SortingParams sortingParameters) { + List args = new ArrayList(); + args.add(key); + args.addAll(sortingParameters.getParams()); + sendCommand("SORT", args.toArray(new String[args.size()])); + } + + public void blpop(final String[] args) { + sendCommand("BLPOP", args); + } + + public void sort( + final String key, + final SortingParams sortingParameters, + final String dstkey) { + List args = new ArrayList(); + args.add(key); + args.addAll(sortingParameters.getParams()); + args.add("STORE"); + args.add(dstkey); + sendCommand("SORT", args.toArray(new String[args.size()])); + } + + public void sort(final String key, final String dstkey) { + sendCommand("SORT", key, "STORE", dstkey); + } + + public void brpop(final String[] args) { + sendCommand("BRPOP", args); + } + + public void auth(final String password) { + sendCommand("AUTH", password); + } + + public void subscribe(final String... channels) { + sendCommand("SUBSCRIBE", channels); + } + + public void publish(final String channel, final String message) { + sendCommand("PUBLISH", channel, message); + } + + public void unsubscribe() { + sendCommand("UNSUBSCRIBE"); + } + + public void unsubscribe(final String... channels) { + sendCommand("UNSUBSCRIBE", channels); + } + + public void psubscribe(final String[] patterns) { + sendCommand("PSUBSCRIBE", patterns); + } + + public void punsubscribe() { + sendCommand("PUNSUBSCRIBE"); + } + + public void punsubscribe(final String... patterns) { + sendCommand("PUNSUBSCRIBE", patterns); + } + + public void zcount(final String key, final double min, final double max) { + sendCommand("ZCOUNT", key, String.valueOf(min), String.valueOf(max)); + } + + public void zrangeByScore( + final String key, + final double min, + final double max) { + sendCommand( + "ZRANGEBYSCORE", + key, + String.valueOf(min), + String.valueOf(max)); + } + + public void zrangeByScore( + final String key, + final String min, + final String max) { + sendCommand("ZRANGEBYSCORE", key, min, max); + } + + public void zrangeByScore( + final String key, + final double min, + final double max, + final int offset, + int count) { + sendCommand( + "ZRANGEBYSCORE", + key, String.valueOf(min), + String.valueOf(max), + "LIMIT", + String.valueOf(offset), + String.valueOf(count)); + } + + public void zrangeByScoreWithScores( + final String key, + final double min, + final double max) { + sendCommand( + "ZRANGEBYSCORE", + key, + String.valueOf(min), + String.valueOf(max), + "WITHSCORES"); + } + + public void zrangeByScoreWithScores( + final String key, + final double min, + final double max, + final int offset, + final int count) { + sendCommand( + "ZRANGEBYSCORE", + key, String.valueOf(min), + String.valueOf(max), + "LIMIT", + String.valueOf(offset), + String.valueOf(count), + "WITHSCORES"); + } + + public void zremrangeByRank( + final String key, + final int start, + final int end) { + sendCommand( + "ZREMRANGEBYRANK", + key, + String.valueOf(start), + String.valueOf(end)); + } + + public void zremrangeByScore( + final String key, + final double start, + final double end) { + sendCommand( + "ZREMRANGEBYSCORE", + key, + String.valueOf(start), + String.valueOf(end)); + } + + public void zunionstore(final String dstkey, final String... sets) { + final String[] params = new String[sets.length + 2]; + params[0] = dstkey; + params[1] = String.valueOf(sets.length); + System.arraycopy(sets, 0, params, 2, sets.length); + sendCommand("ZUNIONSTORE", params); + } + + public void zunionstore( + final String dstkey, + final ZParams params, + final String... sets) { + final List args = new ArrayList(); + args.add(dstkey); + args.add(String.valueOf(sets.length)); + for (final String set : sets) { + args.add(set); + } + args.addAll(params.getParams()); + sendCommand("ZUNIONSTORE", args.toArray(new String[args.size()])); + } + + public void zinterstore(final String dstkey, final String... sets) { + final String[] params = new String[sets.length + 2]; + params[0] = dstkey; + params[1] = String.valueOf(sets.length); + System.arraycopy(sets, 0, params, 2, sets.length); + sendCommand("ZINTERSTORE", params); + } + + public void zinterstore( + final String dstkey, + final ZParams params, + final String... sets) { + final List args = new ArrayList(); + args.add(dstkey); + args.add(String.valueOf(sets.length)); + for (final String set : sets) { + args.add(set); + } + args.addAll(params.getParams()); + sendCommand("ZINTERSTORE", args.toArray(new String[args.size()])); + } + + public void save() { + sendCommand("SAVE"); + } + + public void bgsave() { + sendCommand("BGSAVE"); + } + + public void bgrewriteaof() { + sendCommand("BGREWRITEAOF"); + } + + public void lastsave() { + sendCommand("LASTSAVE"); + } + + public void shutdown() { + sendCommand("SHUTDOWN"); + } + + public void info() { + sendCommand("INFO"); + } + + public void monitor() { + sendCommand("MONITOR"); + } + + public void slaveof(final String host, final int port) { + sendCommand("SLAVEOF", host, String.valueOf(port)); + } + + public void slaveofNoOne() { + sendCommand("SLAVEOF", "no", "one"); + } + + public void configGet(final String pattern) { + sendCommand("CONFIG", "GET", pattern); + } + + public void configSet(final String parameter, final String value) { + sendCommand("CONFIG", "SET", parameter, value); + } + + public void strlen(final String key) { + sendCommand("STRLEN", key); + } + + public void sync() { + sendCommand("SYNC"); + } + + public void lpushx(final String key, final String string) { + sendCommand("LPUSHX", key, string); + } + + public void persist(final String key) { + sendCommand("PERSIST", key); + } + + public void rpushx(final String key, final String string) { + sendCommand("RPUSHX", key, string); + } + + public void echo(final String string) { + sendCommand("ECHO", string); + } + + public void linsert( + final String key, + final LIST_POSITION where, + final String pivot, + final String value) { + sendCommand("LINSERT", key, where.toString(), pivot, value); + } + + public void debug(final DebugParams params) { + sendCommand("DEBUG", params.getCommand()); + } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 6d0943a..0efea87 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -24,7 +24,7 @@ public class Connection { return timeout; } - public void setTimeout(int timeout) { + public void setTimeout(final int timeout) { this.timeout = timeout; } @@ -44,12 +44,12 @@ public class Connection { } } - public Connection(String host) { + public Connection(final String host) { super(); this.host = host; } - protected Connection sendCommand(String name, String... args) { + protected Connection sendCommand(final String name, final String... args) { try { connect(); } catch (UnknownHostException e) { @@ -62,7 +62,7 @@ public class Connection { return this; } - public Connection(String host, int port) { + public Connection(final String host, final int port) { super(); this.host = host; this.port = port; @@ -72,7 +72,7 @@ public class Connection { return host; } - public void setHost(String host) { + public void setHost(final String host) { this.host = host; } @@ -80,7 +80,7 @@ public class Connection { return port; } - public void setPort(int port) { + public void setPort(final int port) { this.port = port; } @@ -135,10 +135,19 @@ public class Connection { return (Integer) protocol.read(inputStream); } - @SuppressWarnings("unchecked") public List getMultiBulkReply() { + final List bresult = getBinaryMultiBulkReply(); + final ArrayList result = new ArrayList(bresult.size()); + for(final byte[] barray : bresult) { + result.add(new String(barray, Protocol.UTF8)); + } + return result; + } + + @SuppressWarnings("unchecked") + public List getBinaryMultiBulkReply() { pipelinedCommands--; - return (List) protocol.read(inputStream); + return (List) protocol.read(inputStream); } @SuppressWarnings("unchecked") From 377c266dc1e7678b6a91d8dc9c41ab20115b947b Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 10:31:20 +0100 Subject: [PATCH 06/40] Add a sendCommand(String byte[]...) method. --- src/main/java/redis/clients/jedis/Connection.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 0efea87..4802570 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -62,6 +62,19 @@ public class Connection { return this; } + protected Connection sendCommand(final String name, final byte[]... args) { + try { + connect(); + } catch (UnknownHostException e) { + throw new JedisException("Could not connect to redis-server", e); + } catch (IOException e) { + throw new JedisException("Could not connect to redis-server", e); + } + protocol.sendCommand(outputStream, name, args); + pipelinedCommands++; + return this; + } + public Connection(final String host, final int port) { super(); this.host = host; From 85ce39ca3e251cf79904088f6f0ecf3c91432070 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 12:22:40 +0100 Subject: [PATCH 07/40] BinaryClient is born --- .../redis/clients/jedis/BinaryClient.java | 714 ++++++++++++++++++ .../java/redis/clients/jedis/Connection.java | 17 +- .../java/redis/clients/jedis/Protocol.java | 173 ++++- .../redis/clients/jedis/SortingParams.java | 35 +- .../java/redis/clients/jedis/ZParams.java | 29 +- .../redis/clients/util/RedisOutputStream.java | 22 +- 6 files changed, 945 insertions(+), 45 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/BinaryClient.java diff --git a/src/main/java/redis/clients/jedis/BinaryClient.java b/src/main/java/redis/clients/jedis/BinaryClient.java new file mode 100644 index 0000000..245a0c9 --- /dev/null +++ b/src/main/java/redis/clients/jedis/BinaryClient.java @@ -0,0 +1,714 @@ +package redis.clients.jedis; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import redis.clients.jedis.Protocol.Command; +import redis.clients.jedis.Protocol.Keyword; +import static redis.clients.jedis.Protocol.toByteArray; + +import static redis.clients.jedis.Protocol.Command.*; +import static redis.clients.jedis.Protocol.Keyword.*; +public class BinaryClient extends Connection { + public enum LIST_POSITION { + BEFORE, AFTER; + public final byte[] raw; + private LIST_POSITION() { + raw = name().getBytes(Protocol.UTF8); + } + } + + private boolean isInMulti; + + public boolean isInMulti() { + return isInMulti; + } + + public BinaryClient(final String host) { + super(host); + } + + public BinaryClient(final String host, final int port) { + super(host, port); + } + + public void ping() { + sendCommand(PING); + } + + public void set(final byte[] key, final byte[] value) { + sendCommand(Command.SET, key, value); + } + + public void get(final byte[] key) { + sendCommand(Command.GET, key); + } + + public void quit() { + sendCommand(QUIT); + } + + public void exists(final byte[] key) { + sendCommand(EXISTS, key); + } + + public void del(final byte[]... keys) { + sendCommand(DEL, keys); + } + + public void type(final byte[] key) { + sendCommand(TYPE, key); + } + + public void flushDB() { + sendCommand(FLUSHDB); + } + + public void keys(final byte[] pattern) { + sendCommand(KEYS, pattern); + } + + public void randomKey() { + sendCommand(RANDOMKEY); + } + + public void rename(final byte[] oldkey, final byte[] newkey) { + sendCommand(RENAME, oldkey, newkey); + } + + public void renamenx(final byte[] oldkey, final byte[] newkey) { + sendCommand(RENAMENX, oldkey, newkey); + } + + public void dbSize() { + sendCommand(DBSIZE); + } + + public void expire(final byte[] key, final int seconds) { + sendCommand(EXPIRE, key, toByteArray(seconds)); + } + + public void expireAt(final byte[] key, final long unixTime) { + sendCommand(EXPIREAT, key, toByteArray(unixTime)); + } + + public void ttl(final byte[] key) { + sendCommand(TTL, key); + } + + public void select(final int index) { + sendCommand(SELECT, toByteArray(index)); + } + + public void move(final byte[] key, final int dbIndex) { + sendCommand(MOVE, key, toByteArray(dbIndex)); + } + + public void flushAll() { + sendCommand(FLUSHALL); + } + + public void getSet(final byte[] key, final byte[] value) { + sendCommand(GETSET, key, value); + } + + public void mget(final byte[]... keys) { + sendCommand(MGET, keys); + } + + public void setnx(final byte[] key, final byte[] value) { + sendCommand(SETNX, key, value); + } + + public void setex( + final byte[] key, + final int seconds, + final byte[] value) { + sendCommand(SETEX, key, toByteArray(seconds), value); + } + + public void mset(final byte[]... keysvalues) { + sendCommand(MSET, keysvalues); + } + + public void msetnx(final byte[]... keysvalues) { + sendCommand(MSETNX, keysvalues); + } + + public void decrBy(final byte[] key, final int integer) { + sendCommand(DECRBY, key, toByteArray(integer)); + } + + public void decr(final byte[] key) { + sendCommand(DECR, key); + } + + public void incrBy(final byte[] key, final int integer) { + sendCommand(INCRBY, key, toByteArray(integer)); + } + + public void incr(final byte[] key) { + sendCommand(INCR, key); + } + + public void append(final byte[] key, final byte[] value) { + sendCommand(APPEND, key, value); + } + + public void substr(final byte[] key, final int start, final int end) { + sendCommand(SUBSTR, key, toByteArray(start), toByteArray(end)); + } + + public void hset( + final byte[] key, + final byte[] field, + final byte[] value) { + sendCommand(HSET, key, field, value); + } + + public void hget(final byte[] key, final byte[] field) { + sendCommand(HGET, key, field); + } + + public void hsetnx( + final byte[] key, + final byte[] field, + final byte[] value) { + sendCommand(HSETNX, key, field, value); + } + + public void hmset(final byte[] key, final Map hash) { + final List params = new ArrayList(); + params.add(key); + + for (final byte[] field : hash.keySet()) { + params.add(field); + params.add(hash.get(field)); + } + sendCommand(HMSET, params.toArray(new byte[params.size()][])); + } + + public void hmget(final byte[] key, final byte[]... fields) { + final byte[][] params = new byte[fields.length + 1][]; + params[0] = key; + System.arraycopy(fields, 0, params, 1, fields.length); + sendCommand(HMGET, params); + } + + public void hincrBy(final byte[] key, final byte[] field, final int value) { + sendCommand(HINCRBY, key, field, toByteArray(value)); + } + + public void hexists(final byte[] key, final byte[] field) { + sendCommand(HEXISTS, key, field); + } + + public void hdel(final byte[] key, final byte[] field) { + sendCommand(HDEL, key, field); + } + + public void hlen(final byte[] key) { + sendCommand(HLEN, key); + } + + public void hkeys(final byte[] key) { + sendCommand(HKEYS, key); + } + + public void hvals(final byte[] key) { + sendCommand(HVALS, key); + } + + public void hgetAll(final byte[] key) { + sendCommand(HGETALL, key); + } + + public void rpush(final byte[] key, final byte[] string) { + sendCommand(RPUSH, key, string); + } + + public void lpush(final byte[] key, final byte[] string) { + sendCommand(LPUSH, key, string); + } + + public void llen(final byte[] key) { + sendCommand(LLEN, key); + } + + public void lrange(final byte[] key, final int start, final int end) { + sendCommand(LRANGE, key, toByteArray(start), toByteArray(end)); + } + + public void ltrim(final byte[] key, final int start, final int end) { + sendCommand(LTRIM, key, toByteArray(start), toByteArray(end)); + } + + public void lindex(final byte[] key, final int index) { + sendCommand(LINDEX, key, toByteArray(index)); + } + + public void lset(final byte[] key, final int index, final byte[] value) { + sendCommand(LSET, key, toByteArray(index), value); + } + + public void lrem(final byte[] key, int count, final byte[] value) { + sendCommand(LREM, key, toByteArray(count), value); + } + + public void lpop(final byte[] key) { + sendCommand(LPOP, key); + } + + public void rpop(final byte[] key) { + sendCommand(RPOP, key); + } + + public void rpoplpush(final byte[] srckey, final byte[] dstkey) { + sendCommand(RPOPLPUSH, srckey, dstkey); + } + + public void sadd(final byte[] key, final byte[] member) { + sendCommand(SADD, key, member); + } + + public void smembers(final byte[] key) { + sendCommand(SMEMBERS, key); + } + + public void srem(final byte[] key, final byte[] member) { + sendCommand(SREM, key, member); + } + + public void spop(final byte[] key) { + sendCommand(SPOP, key); + } + + public void smove( + final byte[] srckey, + final byte[] dstkey, + final byte[] member) { + sendCommand(SMOVE, srckey, dstkey, member); + } + + public void scard(final byte[] key) { + sendCommand(SCARD, key); + } + + public void sismember(final byte[] key, final byte[] member) { + sendCommand(SISMEMBER, key, member); + } + + public void sinter(final byte[]... keys) { + sendCommand(SINTER, keys); + } + + public void sinterstore(final byte[] dstkey, final byte[]... keys) { + final byte[][] params = new byte[keys.length + 1][]; + params[0] = dstkey; + System.arraycopy(keys, 0, params, 1, keys.length); + sendCommand(SINTERSTORE, params); + } + + public void sunion(final byte[]... keys) { + sendCommand(SUNION, keys); + } + + public void sunionstore(final byte[] dstkey, final byte[]... keys) { + byte[][] params = new byte[keys.length + 1][]; + params[0] = dstkey; + System.arraycopy(keys, 0, params, 1, keys.length); + sendCommand(SUNIONSTORE, params); + } + + public void sdiff(final byte[]... keys) { + sendCommand(SDIFF, keys); + } + + public void sdiffstore(final byte[] dstkey, final byte[]... keys) { + byte[][] params = new byte[keys.length + 1][]; + params[0] = dstkey; + System.arraycopy(keys, 0, params, 1, keys.length); + sendCommand(SDIFFSTORE, params); + } + + public void srandmember(final byte[] key) { + sendCommand(SRANDMEMBER, key); + } + + public void zadd( + final byte[] key, + final double score, + final byte[] member) { + sendCommand(ZADD, key, toByteArray(score), member); + } + + public void zrange(final byte[] key, final int start, final int end) { + sendCommand(ZRANGE, key, toByteArray(start), toByteArray(end)); + } + + public void zrem(final byte[] key, final byte[] member) { + sendCommand(ZREM, key, member); + } + + public void zincrby( + final byte[] key, + final double score, + final byte[] member) { + sendCommand(ZINCRBY, key, toByteArray(score), member); + } + + public void zrank(final byte[] key, final byte[] member) { + sendCommand(ZRANK, key, member); + } + + public void zrevrank(final byte[] key, final byte[] member) { + sendCommand(ZREVRANK, key, member); + } + + public void zrevrange( + final byte[] key, + final int start, + final int end) { + sendCommand( + ZREVRANGE, + key, + toByteArray(start), + toByteArray(end)); + } + + public void zrangeWithScores( + final byte[] key, + final int start, + final int end) { + sendCommand( + ZRANGE, + key, + toByteArray(start), + toByteArray(end), + WITHSCORES.raw); + } + + public void zrevrangeWithScores( + final byte[] key, + final int start, + final int end) { + sendCommand( + ZREVRANGE, + key, + toByteArray(start), + toByteArray(end), + WITHSCORES.raw); + } + + public void zcard(final byte[] key) { + sendCommand(ZCARD, key); + } + + public void zscore(final byte[] key, final byte[] member) { + sendCommand(ZSCORE, key, member); + } + + public void multi() { + sendCommand(MULTI); + isInMulti = true; + } + + public void discard() { + sendCommand(DISCARD); + isInMulti = false; + } + + public void exec() { + sendCommand(EXEC); + isInMulti = false; + } + + public void watch(final byte[] key) { + sendCommand(WATCH, key); + } + + public void unwatch() { + sendCommand(UNWATCH); + } + + public void sort(final byte[] key) { + sendCommand(SORT, key); + } + + public void sort(final byte[] key, final SortingParams sortingParameters) { + final List args = new ArrayList(); + args.add(key); + args.addAll(sortingParameters.getParams()); + sendCommand(SORT, args.toArray(new byte[args.size()][])); + } + + public void blpop(final byte[][] args) { + sendCommand(BLPOP, args); + } + + public void sort( + final byte[] key, + final SortingParams sortingParameters, + final byte[] dstkey) { + final List args = new ArrayList(); + args.add(key); + args.addAll(sortingParameters.getParams()); + args.add(STORE.raw); + args.add(dstkey); + sendCommand(SORT, args.toArray(new byte[args.size()][])); + } + + public void sort(final byte[] key, final byte[] dstkey) { + sendCommand(SORT, key, STORE.raw, dstkey); + } + + public void brpop(final byte[][] args) { + sendCommand(BRPOP, args); + } + + public void auth(final String password) { + sendCommand(AUTH, password); + } + + public void subscribe(final String... channels) { + sendCommand(SUBSCRIBE, channels); + } + + public void publish(final String channel, final String message) { + sendCommand(PUBLISH, channel, message); + } + + public void unsubscribe() { + sendCommand(UNSUBSCRIBE); + } + + public void unsubscribe(final String... channels) { + sendCommand(UNSUBSCRIBE, channels); + } + + public void psubscribe(final String[] patterns) { + sendCommand(PSUBSCRIBE, patterns); + } + + public void punsubscribe() { + sendCommand(PUNSUBSCRIBE); + } + + public void punsubscribe(final String... patterns) { + sendCommand(PUNSUBSCRIBE, patterns); + } + + public void zcount(final byte[] key, final double min, final double max) { + sendCommand(ZCOUNT, key, toByteArray(min), toByteArray(max)); + } + + public void zrangeByScore( + final byte[] key, + final double min, + final double max) { + sendCommand( + ZRANGEBYSCORE, + key, + toByteArray(min), + toByteArray(max)); + } + + public void zrangeByScore( + final byte[] key, + final byte[] min, + final byte[] max) { + sendCommand(ZRANGEBYSCORE, key, min, max); + } + + public void zrangeByScore( + final byte[] key, + final double min, + final double max, + final int offset, + int count) { + sendCommand( + ZRANGEBYSCORE, + key, + toByteArray(min), + toByteArray(max), + LIMIT.raw, + toByteArray(offset), + toByteArray(count)); + } + + public void zrangeByScoreWithScores( + final byte[] key, + final double min, + final double max) { + sendCommand( + ZRANGEBYSCORE, + key, + toByteArray(min), + toByteArray(max), + WITHSCORES.raw); + } + + public void zrangeByScoreWithScores( + final byte[] key, + final double min, + final double max, + final int offset, + final int count) { + sendCommand( + ZRANGEBYSCORE, + key, + toByteArray(min), + toByteArray(max), + LIMIT.raw, + toByteArray(offset), + toByteArray(count), + WITHSCORES.raw); + } + + public void zremrangeByRank( + final byte[] key, + final int start, + final int end) { + sendCommand( + ZREMRANGEBYRANK, + key, + toByteArray(start), + toByteArray(end)); + } + + public void zremrangeByScore( + final byte[] key, + final double start, + final double end) { + sendCommand( + ZREMRANGEBYSCORE, + key, + toByteArray(start), + toByteArray(end)); + } + + public void zunionstore(final byte[] dstkey, final byte[]... sets) { + final byte[][] params = new byte[sets.length + 2][]; + params[0] = dstkey; + params[1] = toByteArray(sets.length); + System.arraycopy(sets, 0, params, 2, sets.length); + sendCommand(ZUNIONSTORE, params); + } + + public void zunionstore( + final byte[] dstkey, + final ZParams params, + final byte[]... sets) { + final List args = new ArrayList(); + args.add(dstkey); + args.add(Protocol.toByteArray(sets.length)); + for (final byte[] set : sets) { + args.add(set); + } + args.addAll(params.getParams()); + sendCommand(ZUNIONSTORE, args.toArray(new byte[args.size()][])); + } + + public void zinterstore(final byte[] dstkey, final byte[]... sets) { + final byte[][] params = new byte[sets.length + 2][]; + params[0] = dstkey; + params[1] = Protocol.toByteArray(sets.length); + System.arraycopy(sets, 0, params, 2, sets.length); + sendCommand(ZINTERSTORE, params); + } + + public void zinterstore( + final byte[] dstkey, + final ZParams params, + final byte[]... sets) { + final List args = new ArrayList(); + args.add(dstkey); + args.add(Protocol.toByteArray(sets.length)); + for (final byte[] set : sets) { + args.add(set); + } + args.addAll(params.getParams()); + sendCommand(ZINTERSTORE, args.toArray(new byte[args.size()][])); + } + + public void save() { + sendCommand(SAVE); + } + + public void bgsave() { + sendCommand(BGSAVE); + } + + public void bgrewriteaof() { + sendCommand(BGREWRITEAOF); + } + + public void lastsave() { + sendCommand(LASTSAVE); + } + + public void shutdown() { + sendCommand(SHUTDOWN); + } + + public void info() { + sendCommand(INFO); + } + + public void monitor() { + sendCommand(MONITOR); + } + + public void slaveof(final String host, final int port) { + sendCommand(SLAVEOF, host, String.valueOf(port)); + } + + public void slaveofNoOne() { + sendCommand(SLAVEOF, NO.raw, ONE.raw); + } + + public void configGet(final String pattern) { + sendCommand(CONFIG, Keyword.GET.name(), pattern); + } + + public void configSet(final String parameter, final String value) { + sendCommand(CONFIG, Keyword.SET.name(), parameter, value); + } + + public void strlen(final byte[] key) { + sendCommand(STRLEN, key); + } + + public void sync() { + sendCommand(SYNC); + } + + public void lpushx(final byte[] key, final byte[] string) { + sendCommand(LPUSHX, key, string); + } + + public void persist(final byte[] key) { + sendCommand(PERSIST, key); + } + + public void rpushx(final byte[] key, final byte[] string) { + sendCommand(RPUSHX, key, string); + } + + public void echo(final byte[] string) { + sendCommand(ECHO, string); + } + + public void linsert( + final byte[] key, + final LIST_POSITION where, + final byte[] pivot, + final byte[] value) { + sendCommand(LINSERT, key, where.raw, pivot, value); + } + + public void debug(final DebugParams params) { + sendCommand(DEBUG, params.getCommand()); + } +} \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 4802570..d375592 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -7,6 +7,7 @@ import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; +import redis.clients.jedis.Protocol.Command; import redis.clients.util.RedisInputStream; import redis.clients.util.RedisOutputStream; @@ -49,7 +50,15 @@ public class Connection { this.host = host; } - protected Connection sendCommand(final String name, final String... args) { + protected Connection sendCommand(final Command cmd, final String... args) { + final byte[][] bargs = new byte[args.length][]; + for(int i=0; i < args.length; i++) { + bargs[i] = args[i].getBytes(Protocol.UTF8); + } + return sendCommand(cmd, bargs); + } + + protected Connection sendCommand(final Command cmd, final byte[]... args) { try { connect(); } catch (UnknownHostException e) { @@ -57,12 +66,12 @@ public class Connection { } catch (IOException e) { throw new JedisException("Could not connect to redis-server", e); } - protocol.sendCommand(outputStream, name, args); + protocol.sendCommand(outputStream, cmd, args); pipelinedCommands++; return this; } - protected Connection sendCommand(final String name, final byte[]... args) { + protected Connection sendCommand(final Command cmd) { try { connect(); } catch (UnknownHostException e) { @@ -70,7 +79,7 @@ public class Connection { } catch (IOException e) { throw new JedisException("Could not connect to redis-server", e); } - protocol.sendCommand(outputStream, name, args); + protocol.sendCommand(outputStream, cmd, (byte[])null); pipelinedCommands++; return this; } diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index aef07c3..c8ed6fc 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -19,7 +19,7 @@ public final class Protocol { public static final byte PLUS_BYTE = '+'; public static final byte MINUS_BYTE = '-'; public static final byte COLON_BYTE = ':'; - +/* public void sendCommand(final RedisOutputStream os, final String name, final String... args) { try { os.write(ASTERISK_BYTE); @@ -43,14 +43,20 @@ public final class Protocol { throw new JedisException(e); } } +*/ + public void sendCommand(final RedisOutputStream os, final Command command, final byte[]... args) { + sendCommand(os, command.raw, args); + } - public void sendCommand(final RedisOutputStream os, final String name, final byte[]... args) { + private void sendCommand(final RedisOutputStream os, final byte[] command, final byte[]... args) { try { os.write(ASTERISK_BYTE); os.writeIntCrLf(args.length + 1); os.write(DOLLAR_BYTE); - os.writeIntCrLf(name.length()); - os.writeAsciiCrLf(name); + os.writeIntCrLf(command.length); + os.write(command); + os.writeCrLf(); +// os.writeAsciiCrLf(command); for (final byte[] arg : args) { os.write(DOLLAR_BYTE); @@ -136,4 +142,163 @@ public final class Protocol { public Object read(final RedisInputStream is) { return process(is); } + + public static final byte[] toByteArray(final int value) { + return String.valueOf(value).getBytes(Protocol.UTF8); + } + + public static final byte[] toByteArray(final long value) { + return String.valueOf(value).getBytes(Protocol.UTF8); + } + + public static final byte[] toByteArray(final double value) { + return String.valueOf(value).getBytes(Protocol.UTF8); + } + + 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, + ZREMRANGEBYRANK, + ZREMRANGEBYSCORE, + ZUNIONSTORE, + ZINTERSTORE, + SAVE, + BGSAVE, + BGREWRITEAOF, + LASTSAVE, + SHUTDOWN, + INFO, + MONITOR, + SLAVEOF, + CONFIG, + STRLEN, + SYNC, + LPUSHX, + PERSIST, + RPUSHX, + ECHO, + LINSERT, + DEBUG; + + public final byte[] raw; + + Command() { + raw = this.name().getBytes(UTF8); + } + } + + public static enum Keyword { + AGGREGATE, + ALPHA, + ASC, + BY, + DESC, + GET, + NO, + NOSORT, + ONE, + LIMIT, + SET, + STORE, + WEIGHTS, + WITHSCORES; + public final byte[] raw; + + Keyword() { + raw = this.name().getBytes(UTF8); + } + + } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/SortingParams.java b/src/main/java/redis/clients/jedis/SortingParams.java index dcd415d..66fd997 100644 --- a/src/main/java/redis/clients/jedis/SortingParams.java +++ b/src/main/java/redis/clients/jedis/SortingParams.java @@ -4,13 +4,13 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; - +import static redis.clients.jedis.Protocol.Keyword.*; /** * Builder Class for {@link Jedis#sort(String, SortingParams) SORT} Parameters. * */ public class SortingParams { - private List params = new ArrayList(); + private List params = new ArrayList(); /** * Sort by weight in keys. @@ -25,9 +25,9 @@ public class SortingParams { * @param pattern * @return the SortingParams Object */ - public SortingParams by(String pattern) { - params.add("BY"); - params.add(pattern); + public SortingParams by(final String pattern) { + params.add(BY.raw); + params.add(pattern.getBytes(Protocol.UTF8)); return this; } @@ -40,11 +40,12 @@ public class SortingParams { * @return the SortingParams Object */ public SortingParams nosort() { - params.add("BY nosort"); + params.add(BY.raw); + params.add(NOSORT.raw); return this; } - public Collection getParams() { + public Collection getParams() { return Collections.unmodifiableCollection(params); } @@ -54,7 +55,7 @@ public class SortingParams { * @return the sortingParams Object */ public SortingParams desc() { - params.add("DESC"); + params.add(DESC.raw); return this; } @@ -64,7 +65,7 @@ public class SortingParams { * @return the SortingParams Object */ public SortingParams asc() { - params.add("ASC"); + params.add(ASC.raw); return this; } @@ -76,10 +77,10 @@ public class SortingParams { * @param count * @return the SortingParams Object */ - public SortingParams limit(int start, int count) { - params.add("LIMIT"); - params.add(String.valueOf(start)); - params.add(String.valueOf(count)); + public SortingParams limit(final int start, final int count) { + params.add(LIMIT.raw); + params.add(Protocol.toByteArray(start)); + params.add(Protocol.toByteArray(count)); return this; } @@ -90,7 +91,7 @@ public class SortingParams { * @return the SortingParams Object */ public SortingParams alpha() { - params.add("ALPHA"); + params.add(ALPHA.raw); return this; } @@ -110,9 +111,9 @@ public class SortingParams { * @return the SortingParams Object */ public SortingParams get(String... patterns) { - for (String pattern : patterns) { - params.add("GET"); - params.add(pattern); + for (final String pattern : patterns) { + params.add(GET.raw); + params.add(pattern.getBytes(Protocol.UTF8)); } return this; } diff --git a/src/main/java/redis/clients/jedis/ZParams.java b/src/main/java/redis/clients/jedis/ZParams.java index 86d9078..a260397 100644 --- a/src/main/java/redis/clients/jedis/ZParams.java +++ b/src/main/java/redis/clients/jedis/ZParams.java @@ -5,29 +5,36 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import static redis.clients.jedis.Protocol.Keyword.*; + public class ZParams { public enum Aggregate { - SUM, MIN, MAX + SUM, MIN, MAX; + + public final byte[] raw; + Aggregate() { + raw = name().getBytes(Protocol.UTF8); + } } - private List params = new ArrayList(); + private List params = new ArrayList(); - public ZParams weights(int... weights) { - params.add("WEIGHTS"); - for (int weight : weights) { - params.add(String.valueOf(weight)); + public ZParams weights(final int... weights) { + params.add(WEIGHTS.raw); + for (final int weight : weights) { + params.add(Protocol.toByteArray(weight)); } return this; } - public Collection getParams() { - return Collections.unmodifiableCollection(params); + public Collection getParams() { + return Collections.unmodifiableCollection(params); } - public ZParams aggregate(Aggregate aggregate) { - params.add("AGGREGATE"); - params.add(aggregate.name()); + public ZParams aggregate(final Aggregate aggregate) { + params.add(AGGREGATE.raw); + params.add(aggregate.raw); return this; } } diff --git a/src/main/java/redis/clients/util/RedisOutputStream.java b/src/main/java/redis/clients/util/RedisOutputStream.java index c5f4bc2..5708d43 100644 --- a/src/main/java/redis/clients/util/RedisOutputStream.java +++ b/src/main/java/redis/clients/util/RedisOutputStream.java @@ -12,11 +12,11 @@ public final class RedisOutputStream extends FilterOutputStream { protected int count; - public RedisOutputStream(OutputStream out) { + public RedisOutputStream(final OutputStream out) { this(out, 8192); } - public RedisOutputStream(OutputStream out, int size) { + public RedisOutputStream(final OutputStream out, final int size) { super(out); if (size <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); @@ -31,14 +31,18 @@ public final class RedisOutputStream extends FilterOutputStream { } } - public void write(int b) throws IOException { - buf[count++] = (byte) b; + public void write(final byte b) throws IOException { + buf[count++] = b; if (count == buf.length) { flushBuffer(); } } + + public void write(final byte[] b) throws IOException { + write(b, 0, b.length); + } - public void write(byte b[], int off, int len) throws IOException { + public void write(final byte b[], final int off, final int len) throws IOException { if (len >= buf.length) { flushBuffer(); out.write(b, off, len); @@ -52,7 +56,7 @@ public final class RedisOutputStream extends FilterOutputStream { } } - public void writeAsciiCrLf(String in) throws IOException { + public void writeAsciiCrLf(final String in) throws IOException { final int size = in.length(); for (int i = 0; i != size; ++i) { @@ -65,11 +69,11 @@ public final class RedisOutputStream extends FilterOutputStream { writeCrLf(); } - public static boolean isSurrogate(char ch) { + public static boolean isSurrogate(final char ch) { return ch >= Character.MIN_SURROGATE && ch <= Character.MAX_SURROGATE; } - public static int utf8Length (String str) { + public static int utf8Length (final String str) { int strLen = str.length(), utfLen = 0; for(int i = 0; i != strLen; ++i) { char c = str.charAt(i); @@ -96,7 +100,7 @@ public final class RedisOutputStream extends FilterOutputStream { buf[count++] = '\n'; } - public void writeUtf8CrLf(String str) throws IOException { + public void writeUtf8CrLf(final String str) throws IOException { int strLen = str.length(); int i; From 78e7b653c29b2b396558dca0061ae50d3a99223f Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 13:45:04 +0100 Subject: [PATCH 08/40] Client inherits from BinaryClient... --- src/main/java/redis/clients/jedis/Client.java | 526 +++++++----------- 1 file changed, 197 insertions(+), 329 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index e52cb66..e902573 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -1,20 +1,10 @@ package redis.clients.jedis; -import java.util.ArrayList; -import java.util.List; +import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; -public class Client extends Connection { - public enum LIST_POSITION { - BEFORE, AFTER - } - - private boolean isInMulti; - - public boolean isInMulti() { - return isInMulti; - } - +public class Client extends BinaryClient { public Client(final String host) { super(host); } @@ -23,492 +13,450 @@ public class Client extends Connection { super(host, port); } - public void ping() { - sendCommand("PING"); - } - public void set(final String key, final String value) { - sendCommand("SET", key, value); + set(key.getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8)); } public void get(final String key) { - sendCommand("GET", key); - } - - public void quit() { - sendCommand("QUIT"); + get(key.getBytes(Protocol.UTF8)); } public void exists(final String key) { - sendCommand("EXISTS", key); + exists(key.getBytes(Protocol.UTF8)); } public void del(final String... keys) { - sendCommand("DEL", keys); + final byte[][] bkeys = new byte[keys.length][]; + for(int i=0; i < keys.length; i++) { + bkeys[i] = keys[i].getBytes(Protocol.UTF8); + } + del(bkeys); } public void type(final String key) { - sendCommand("TYPE", key); - } - - public void flushDB() { - sendCommand("FLUSHDB"); + del(key.getBytes(Protocol.UTF8)); } public void keys(final String pattern) { - sendCommand("KEYS", pattern); + keys(pattern.getBytes(Protocol.UTF8)); } - public void randomKey() { - sendCommand("RANDOMKEY"); - } public void rename(final String oldkey, final String newkey) { - sendCommand("RENAME", oldkey, newkey); + rename(oldkey.getBytes(Protocol.UTF8), newkey.getBytes(Protocol.UTF8)); } public void renamenx(final String oldkey, final String newkey) { - sendCommand("RENAMENX", oldkey, newkey); - } - - public void dbSize() { - sendCommand("DBSIZE"); + renamenx( + oldkey.getBytes(Protocol.UTF8), + newkey.getBytes(Protocol.UTF8)); } public void expire(final String key, final int seconds) { - sendCommand("EXPIRE", key, String.valueOf(seconds)); + expire(key.getBytes(Protocol.UTF8), seconds); } public void expireAt(final String key, final long unixTime) { - sendCommand("EXPIREAT", key, String.valueOf(unixTime)); + expireAt(key.getBytes(Protocol.UTF8), unixTime); } public void ttl(final String key) { - sendCommand("TTL", key); - } - - public void select(final int index) { - sendCommand("SELECT", String.valueOf(index)); + ttl(key.getBytes(Protocol.UTF8)); } public void move(final String key, final int dbIndex) { - sendCommand("MOVE", key, String.valueOf(dbIndex)); - } - - public void flushAll() { - sendCommand("FLUSHALL"); + move(key.getBytes(Protocol.UTF8), dbIndex); } public void getSet(final String key, final String value) { - sendCommand("GETSET", key, value); + getSet(key.getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8)); } public void mget(final String... keys) { - sendCommand("MGET", keys); + final byte[][] bkeys = new byte[keys.length][]; + for (int i = 0; i < bkeys.length; i++) { + bkeys[i] = keys[i].getBytes(Protocol.UTF8); + } + mget(bkeys); } public void setnx(final String key, final String value) { - sendCommand("SETNX", key, value); + setnx(key.getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8)); } public void setex( final String key, final int seconds, final String value) { - sendCommand("SETEX", key, String.valueOf(seconds), value); + setex( + key.getBytes(Protocol.UTF8), + seconds, + value.getBytes(Protocol.UTF8)); } public void mset(final String... keysvalues) { - sendCommand("MSET", keysvalues); + final byte[][] bkeysvalues = new byte[keysvalues.length][]; + for (int i = 0; i < keysvalues.length; i++) { + bkeysvalues[i] = keysvalues[i].getBytes(Protocol.UTF8); + } + mset(bkeysvalues); } public void msetnx(final String... keysvalues) { - sendCommand("MSETNX", keysvalues); + final byte[][] bkeysvalues = new byte[keysvalues.length][]; + for (int i = 0; i < keysvalues.length; i++) { + bkeysvalues[i] = keysvalues[i].getBytes(Protocol.UTF8); + } + msetnx(bkeysvalues); } public void decrBy(final String key, final int integer) { - sendCommand("DECRBY", key, String.valueOf(integer)); + decrBy(key.getBytes(Protocol.UTF8), integer); } public void decr(final String key) { - sendCommand("DECR", key); + decr(key.getBytes(Protocol.UTF8)); } public void incrBy(final String key, final int integer) { - sendCommand("INCRBY", key, String.valueOf(integer)); + incrBy(key.getBytes(Protocol.UTF8), integer); } public void incr(final String key) { - sendCommand("INCR", key); + incr(key.getBytes(Protocol.UTF8)); } public void append(final String key, final String value) { - sendCommand("APPEND", key, value); + append(key.getBytes(Protocol.UTF8), value.getBytes(Protocol.UTF8)); } public void substr(final String key, final int start, final int end) { - sendCommand("SUBSTR", key, String.valueOf(start), String.valueOf(end)); + substr(key.getBytes(Protocol.UTF8), start, end); } public void hset( final String key, final String field, final String value) { - sendCommand("HSET", key, field, value); + hset( + key.getBytes(Protocol.UTF8), + field.getBytes(Protocol.UTF8), + value.getBytes(Protocol.UTF8)); } public void hget(final String key, final String field) { - sendCommand("HGET", key, field); + hget(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8)); } public void hsetnx( final String key, final String field, final String value) { - sendCommand("HSETNX", key, field, value); + hsetnx( + key.getBytes(Protocol.UTF8), + field.getBytes(Protocol.UTF8), + value.getBytes(Protocol.UTF8)); } public void hmset(final String key, final Map hash) { - final List params = new ArrayList(); - params.add(key); - - for (final String field : hash.keySet()) { - params.add(field); - params.add(hash.get(field)); + final Map bhash = new HashMap(hash.size()); + for (final Entry entry : hash.entrySet()) { + bhash.put( + entry.getKey().getBytes(Protocol.UTF8), + entry.getValue().getBytes(Protocol.UTF8)); } - sendCommand("HMSET", params.toArray(new String[params.size()])); + hmset(key.getBytes(Protocol.UTF8), bhash); } public void hmget(final String key, final String... fields) { - final String[] params = new String[fields.length + 1]; - params[0] = key; - System.arraycopy(fields, 0, params, 1, fields.length); - sendCommand("HMGET", params); + final byte[][] bfields = new byte[fields.length][]; + for (int i = 0; i < bfields.length; i++) { + bfields[i] = fields[i].getBytes(Protocol.UTF8); + } + hmget(key.getBytes(Protocol.UTF8), bfields); } public void hincrBy(final String key, final String field, final int value) { - sendCommand("HINCRBY", key, field, String.valueOf(value)); + hincrBy( + key.getBytes(Protocol.UTF8), + field.getBytes(Protocol.UTF8), + value); } public void hexists(final String key, final String field) { - sendCommand("HEXISTS", key, field); + hexists(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8)); } public void hdel(final String key, final String field) { - sendCommand("HDEL", key, field); + hdel(key.getBytes(Protocol.UTF8), field.getBytes(Protocol.UTF8)); } public void hlen(final String key) { - sendCommand("HLEN", key); + hlen(key.getBytes(Protocol.UTF8)); } public void hkeys(final String key) { - sendCommand("HKEYS", key); + hkeys(key.getBytes(Protocol.UTF8)); } public void hvals(final String key) { - sendCommand("HVALS", key); + hvals(key.getBytes(Protocol.UTF8)); } public void hgetAll(final String key) { - sendCommand("HGETALL", key); + hgetAll(key.getBytes(Protocol.UTF8)); } public void rpush(final String key, final String string) { - sendCommand("RPUSH", key, string); + rpush(key.getBytes(Protocol.UTF8), string.getBytes(Protocol.UTF8)); } public void lpush(final String key, final String string) { - sendCommand("LPUSH", key, string); + lpush(key.getBytes(Protocol.UTF8), string.getBytes(Protocol.UTF8)); } public void llen(final String key) { - sendCommand("LLEN", key); + llen(key.getBytes(Protocol.UTF8)); } public void lrange(final String key, final int start, final int end) { - sendCommand("LRANGE", key, String.valueOf(start), String.valueOf(end)); + lrange(key.getBytes(Protocol.UTF8), start, end); } public void ltrim(final String key, final int start, final int end) { - sendCommand("LTRIM", key, String.valueOf(start), String.valueOf(end)); + ltrim(key.getBytes(Protocol.UTF8), start, end); } public void lindex(final String key, final int index) { - sendCommand("LINDEX", key, String.valueOf(index)); + lindex(key.getBytes(Protocol.UTF8), index); } public void lset(final String key, final int index, final String value) { - sendCommand("LSET", key, String.valueOf(index), value); + lset(key.getBytes(Protocol.UTF8), index, value.getBytes(Protocol.UTF8)); } public void lrem(final String key, int count, final String value) { - sendCommand("LREM", key, String.valueOf(count), value); + lrem(key.getBytes(Protocol.UTF8), count, value.getBytes(Protocol.UTF8)); } public void lpop(final String key) { - sendCommand("LPOP", key); + lpop(key.getBytes(Protocol.UTF8)); } public void rpop(final String key) { - sendCommand("RPOP", key); + rpop(key.getBytes(Protocol.UTF8)); } public void rpoplpush(final String srckey, final String dstkey) { - sendCommand("RPOPLPUSH", srckey, dstkey); + rpoplpush( + srckey.getBytes(Protocol.UTF8), + dstkey.getBytes(Protocol.UTF8)); } public void sadd(final String key, final String member) { - sendCommand("SADD", key, member); + sadd(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); } public void smembers(final String key) { - sendCommand("SMEMBERS", key); + smembers(key.getBytes(Protocol.UTF8)); } public void srem(final String key, final String member) { - sendCommand("SREM", key, member); + srem(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); } public void spop(final String key) { - sendCommand("SPOP", key); + spop(key.getBytes(Protocol.UTF8)); } public void smove( final String srckey, final String dstkey, final String member) { - sendCommand("SMOVE", srckey, dstkey, member); + smove(srckey.getBytes(Protocol.UTF8), dstkey.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); } public void scard(final String key) { - sendCommand("SCARD", key); + scard(key.getBytes(Protocol.UTF8)); } public void sismember(final String key, final String member) { - sendCommand("SISMEMBER", key, member); + sismember(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); } public void sinter(final String... keys) { - sendCommand("SINTER", keys); + final byte[][] bkeys = new byte[keys.length][]; + for (int i = 0; i < bkeys.length; i++) { + bkeys[i] = keys[i].getBytes(Protocol.UTF8); + } + sinter(bkeys); } public void sinterstore(final String dstkey, final String... keys) { - String[] params = new String[keys.length + 1]; - params[0] = dstkey; - System.arraycopy(keys, 0, params, 1, keys.length); - sendCommand("SINTERSTORE", params); + final byte[][] bkeys = new byte[keys.length][]; + for (int i = 0; i < bkeys.length; i++) { + bkeys[i] = keys[i].getBytes(Protocol.UTF8); + } + sinterstore(dstkey.getBytes(Protocol.UTF8), bkeys); } public void sunion(final String... keys) { - sendCommand("SUNION", keys); + final byte[][] bkeys = new byte[keys.length][]; + for (int i = 0; i < bkeys.length; i++) { + bkeys[i] = keys[i].getBytes(Protocol.UTF8); + } + sunion(bkeys); } public void sunionstore(final String dstkey, final String... keys) { - String[] params = new String[keys.length + 1]; - params[0] = dstkey; - System.arraycopy(keys, 0, params, 1, keys.length); - sendCommand("SUNIONSTORE", params); + final byte[][] bkeys = new byte[keys.length][]; + for (int i = 0; i < bkeys.length; i++) { + bkeys[i] = keys[i].getBytes(Protocol.UTF8); + } + sunionstore(dstkey.getBytes(Protocol.UTF8), bkeys); } public void sdiff(final String... keys) { - sendCommand("SDIFF", keys); + final byte[][] bkeys = new byte[keys.length][]; + for (int i = 0; i < bkeys.length; i++) { + bkeys[i] = keys[i].getBytes(Protocol.UTF8); + } + sdiff(bkeys); } public void sdiffstore(final String dstkey, final String... keys) { - String[] params = new String[keys.length + 1]; - params[0] = dstkey; - System.arraycopy(keys, 0, params, 1, keys.length); - sendCommand("SDIFFSTORE", params); + final byte[][] bkeys = new byte[keys.length][]; + for (int i = 0; i < bkeys.length; i++) { + bkeys[i] = keys[i].getBytes(Protocol.UTF8); + } + sdiffstore(dstkey.getBytes(Protocol.UTF8), bkeys); } public void srandmember(final String key) { - sendCommand("SRANDMEMBER", key); + srandmember(key.getBytes(Protocol.UTF8)); } public void zadd( final String key, final double score, final String member) { - sendCommand("ZADD", key, String.valueOf(score), member); + zadd(key.getBytes(Protocol.UTF8), score, member.getBytes(Protocol.UTF8)); } public void zrange(final String key, final int start, final int end) { - sendCommand("ZRANGE", key, String.valueOf(start), String.valueOf(end)); + zrange(key.getBytes(Protocol.UTF8), start, end); } public void zrem(final String key, final String member) { - sendCommand("ZREM", key, member); + zrem(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); } public void zincrby( final String key, final double score, final String member) { - sendCommand("ZINCRBY", key, String.valueOf(score), member); + zincrby(key.getBytes(Protocol.UTF8), score, member.getBytes(Protocol.UTF8)); } public void zrank(final String key, final String member) { - sendCommand("ZRANK", key, member); + zrank(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); } public void zrevrank(final String key, final String member) { - sendCommand("ZREVRANK", key, member); + zrevrank(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); } public void zrevrange( final String key, final int start, final int end) { - sendCommand( - "ZREVRANGE", - key, - String.valueOf(start), - String.valueOf(end)); + zrevrange(key.getBytes(Protocol.UTF8), start, end); } public void zrangeWithScores( final String key, final int start, final int end) { - sendCommand( - "ZRANGE", - key, - String.valueOf(start), - String.valueOf(end), - "WITHSCORES"); + zrangeWithScores(key.getBytes(Protocol.UTF8), start, end); } public void zrevrangeWithScores( final String key, final int start, final int end) { - sendCommand( - "ZREVRANGE", - key, - String.valueOf(start), - String.valueOf(end), - "WITHSCORES"); + zrevrangeWithScores(key.getBytes(Protocol.UTF8), start, end); } public void zcard(final String key) { - sendCommand("ZCARD", key); + zcard(key.getBytes(Protocol.UTF8)); } public void zscore(final String key, final String member) { - sendCommand("ZSCORE", key, member); + zscore(key.getBytes(Protocol.UTF8), member.getBytes(Protocol.UTF8)); } - public void multi() { - sendCommand("MULTI"); - isInMulti = true; - } - - public void discard() { - sendCommand("DISCARD"); - isInMulti = false; - } - - public void exec() { - sendCommand("EXEC"); - isInMulti = false; - } public void watch(final String key) { - sendCommand("WATCH", key); - } - - public void unwatch() { - sendCommand("UNWATCH"); + watch(key.getBytes(Protocol.UTF8)); } public void sort(final String key) { - sendCommand("SORT", key); + sort(key.getBytes(Protocol.UTF8)); } public void sort(final String key, final SortingParams sortingParameters) { - List args = new ArrayList(); - args.add(key); - args.addAll(sortingParameters.getParams()); - sendCommand("SORT", args.toArray(new String[args.size()])); + sort(key.getBytes(Protocol.UTF8),sortingParameters); } public void blpop(final String[] args) { - sendCommand("BLPOP", args); + final byte[][] bargs = new byte[args.length][]; + for (int i = 0; i < bargs.length; i++) { + bargs[i] = args[i].getBytes(Protocol.UTF8); + } + blpop(bargs); } public void sort( final String key, final SortingParams sortingParameters, final String dstkey) { - List args = new ArrayList(); - args.add(key); - args.addAll(sortingParameters.getParams()); - args.add("STORE"); - args.add(dstkey); - sendCommand("SORT", args.toArray(new String[args.size()])); + sort( + key.getBytes(Protocol.UTF8), + sortingParameters, + dstkey.getBytes(Protocol.UTF8)); } public void sort(final String key, final String dstkey) { - sendCommand("SORT", key, "STORE", dstkey); + sort(key.getBytes(Protocol.UTF8), dstkey.getBytes(Protocol.UTF8)); } public void brpop(final String[] args) { - sendCommand("BRPOP", args); - } - - public void auth(final String password) { - sendCommand("AUTH", password); - } - - public void subscribe(final String... channels) { - sendCommand("SUBSCRIBE", channels); - } - - public void publish(final String channel, final String message) { - sendCommand("PUBLISH", channel, message); - } - - public void unsubscribe() { - sendCommand("UNSUBSCRIBE"); - } - - public void unsubscribe(final String... channels) { - sendCommand("UNSUBSCRIBE", channels); - } - - public void psubscribe(final String[] patterns) { - sendCommand("PSUBSCRIBE", patterns); - } - - public void punsubscribe() { - sendCommand("PUNSUBSCRIBE"); - } - - public void punsubscribe(final String... patterns) { - sendCommand("PUNSUBSCRIBE", patterns); + final byte[][] bargs = new byte[args.length][]; + for (int i = 0; i < bargs.length; i++) { + bargs[i] = args[i].getBytes(Protocol.UTF8); + } + brpop(bargs); } public void zcount(final String key, final double min, final double max) { - sendCommand("ZCOUNT", key, String.valueOf(min), String.valueOf(max)); + zcount(key.getBytes(Protocol.UTF8), min, max); } public void zrangeByScore( final String key, final double min, final double max) { - sendCommand( - "ZRANGEBYSCORE", - key, - String.valueOf(min), - String.valueOf(max)); + zrangeByScore(key.getBytes(Protocol.UTF8), min, max); } public void zrangeByScore( final String key, final String min, final String max) { - sendCommand("ZRANGEBYSCORE", key, min, max); + zrangeByScore( + key.getBytes(Protocol.UTF8), + min.getBytes(Protocol.UTF8), + max.getBytes(Protocol.UTF8)); } public void zrangeByScore( @@ -517,25 +465,14 @@ public class Client extends Connection { final double max, final int offset, int count) { - sendCommand( - "ZRANGEBYSCORE", - key, String.valueOf(min), - String.valueOf(max), - "LIMIT", - String.valueOf(offset), - String.valueOf(count)); + zrangeByScore(key.getBytes(Protocol.UTF8), min, max, offset, count); } public void zrangeByScoreWithScores( final String key, final double min, final double max) { - sendCommand( - "ZRANGEBYSCORE", - key, - String.valueOf(min), - String.valueOf(max), - "WITHSCORES"); + zrangeByScoreWithScores(key.getBytes(Protocol.UTF8), min, max); } public void zrangeByScoreWithScores( @@ -544,148 +481,79 @@ public class Client extends Connection { final double max, final int offset, final int count) { - sendCommand( - "ZRANGEBYSCORE", - key, String.valueOf(min), - String.valueOf(max), - "LIMIT", - String.valueOf(offset), - String.valueOf(count), - "WITHSCORES"); + zrangeByScoreWithScores(key.getBytes(Protocol.UTF8), min, max, offset, count); } public void zremrangeByRank( final String key, final int start, final int end) { - sendCommand( - "ZREMRANGEBYRANK", - key, - String.valueOf(start), - String.valueOf(end)); + zremrangeByRank(key.getBytes(Protocol.UTF8), start, end); } public void zremrangeByScore( final String key, final double start, final double end) { - sendCommand( - "ZREMRANGEBYSCORE", - key, - String.valueOf(start), - String.valueOf(end)); + zremrangeByScore(key.getBytes(Protocol.UTF8), start, end); } public void zunionstore(final String dstkey, final String... sets) { - final String[] params = new String[sets.length + 2]; - params[0] = dstkey; - params[1] = String.valueOf(sets.length); - System.arraycopy(sets, 0, params, 2, sets.length); - sendCommand("ZUNIONSTORE", params); + final byte[][] bsets = new byte[sets.length][]; + for (int i = 0; i < bsets.length; i++) { + bsets[i] = sets[i].getBytes(Protocol.UTF8); + } + zunionstore(dstkey.getBytes(Protocol.UTF8), bsets); } public void zunionstore( final String dstkey, final ZParams params, final String... sets) { - final List args = new ArrayList(); - args.add(dstkey); - args.add(String.valueOf(sets.length)); - for (final String set : sets) { - args.add(set); + final byte[][] bsets = new byte[sets.length][]; + for (int i = 0; i < bsets.length; i++) { + bsets[i] = sets[i].getBytes(Protocol.UTF8); } - args.addAll(params.getParams()); - sendCommand("ZUNIONSTORE", args.toArray(new String[args.size()])); + zunionstore(dstkey.getBytes(Protocol.UTF8), params, bsets); } public void zinterstore(final String dstkey, final String... sets) { - final String[] params = new String[sets.length + 2]; - params[0] = dstkey; - params[1] = String.valueOf(sets.length); - System.arraycopy(sets, 0, params, 2, sets.length); - sendCommand("ZINTERSTORE", params); + final byte[][] bsets = new byte[sets.length][]; + for (int i = 0; i < bsets.length; i++) { + bsets[i] = sets[i].getBytes(Protocol.UTF8); + } + zinterstore(dstkey.getBytes(Protocol.UTF8), bsets); } public void zinterstore( final String dstkey, final ZParams params, final String... sets) { - final List args = new ArrayList(); - args.add(dstkey); - args.add(String.valueOf(sets.length)); - for (final String set : sets) { - args.add(set); + final byte[][] bsets = new byte[sets.length][]; + for (int i = 0; i < bsets.length; i++) { + bsets[i] = sets[i].getBytes(Protocol.UTF8); } - args.addAll(params.getParams()); - sendCommand("ZINTERSTORE", args.toArray(new String[args.size()])); - } - - public void save() { - sendCommand("SAVE"); - } - - public void bgsave() { - sendCommand("BGSAVE"); - } - - public void bgrewriteaof() { - sendCommand("BGREWRITEAOF"); - } - - public void lastsave() { - sendCommand("LASTSAVE"); - } - - public void shutdown() { - sendCommand("SHUTDOWN"); - } - - public void info() { - sendCommand("INFO"); - } - - public void monitor() { - sendCommand("MONITOR"); - } - - public void slaveof(final String host, final int port) { - sendCommand("SLAVEOF", host, String.valueOf(port)); - } - - public void slaveofNoOne() { - sendCommand("SLAVEOF", "no", "one"); - } - - public void configGet(final String pattern) { - sendCommand("CONFIG", "GET", pattern); - } - - public void configSet(final String parameter, final String value) { - sendCommand("CONFIG", "SET", parameter, value); + zinterstore(dstkey.getBytes(Protocol.UTF8), params, bsets); } public void strlen(final String key) { - sendCommand("STRLEN", key); - } - - public void sync() { - sendCommand("SYNC"); + strlen(key.getBytes(Protocol.UTF8)); } public void lpushx(final String key, final String string) { - sendCommand("LPUSHX", key, string); + lpushx(key.getBytes(Protocol.UTF8), string.getBytes(Protocol.UTF8)); } public void persist(final String key) { - sendCommand("PERSIST", key); + persist(key.getBytes(Protocol.UTF8)); } public void rpushx(final String key, final String string) { - sendCommand("RPUSHX", key, string); + rpushx(key.getBytes(Protocol.UTF8), string.getBytes(Protocol.UTF8)); } public void echo(final String string) { - sendCommand("ECHO", string); + echo(string.getBytes(Protocol.UTF8)); } public void linsert( @@ -693,10 +561,10 @@ public class Client extends Connection { final LIST_POSITION where, final String pivot, final String value) { - sendCommand("LINSERT", key, where.toString(), pivot, value); - } - - public void debug(final DebugParams params) { - sendCommand("DEBUG", params.getCommand()); + linsert( + key.getBytes(Protocol.UTF8), + where, + pivot.getBytes(Protocol.UTF8), + value.getBytes(Protocol.UTF8)); } } \ No newline at end of file From 9614a99a5a300575f315b35a68668a213326150c Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 13:51:42 +0100 Subject: [PATCH 09/40] BinaryJedisCommands skeleton --- .../clients/jedis/BinaryJedisCommands.java | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 src/main/java/redis/clients/jedis/BinaryJedisCommands.java diff --git a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java new file mode 100644 index 0000000..ccec1b8 --- /dev/null +++ b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java @@ -0,0 +1,158 @@ +package redis.clients.jedis; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import redis.clients.jedis.BinaryClient.LIST_POSITION; + +/** + * Common interface for sharded and non-sharded BinaryJedis + */ +public interface BinaryJedisCommands { + String set(byte[] key, byte[] value); + + String get(byte[] key); + + Integer exists(byte[] key); + + String type(byte[] key); + + Integer expire(byte[] key, int seconds); + + Integer expireAt(byte[] key, long unixTime); + + Integer ttl(byte[] key); + + String getSet(byte[] key, byte[] value); + + Integer setnx(byte[] key, byte[] value); + + String setex(byte[] key, int seconds, byte[] value); + + Integer decrBy(byte[] key, int integer); + + Integer decr(byte[] key); + + Integer incrBy(byte[] key, int integer); + + Integer incr(byte[] key); + + Integer append(byte[] key, byte[] value); + + String substr(byte[] key, int start, int end); + + Integer hset(byte[] key, byte[] field, byte[] value); + + String hget(byte[] key, byte[] field); + + Integer hsetnx(byte[] key, byte[] field, byte[] value); + + String hmset(byte[] key, Map hash); + + List hmget(byte[] key, String... fields); + + Integer hincrBy(byte[] key, byte[] field, int value); + + Integer hexists(byte[] key, byte[] field); + + Integer hdel(byte[] key, byte[] field); + + Integer hlen(byte[] key); + + List hkeys(byte[] key); + + List hvals(byte[] key); + + Map hgetAll(byte[] key); + + Integer rpush(byte[] key, byte[] string); + + Integer lpush(byte[] key, byte[] string); + + Integer llen(byte[] key); + + List lrange(byte[] key, int start, int end); + + String ltrim(byte[] key, int start, int end); + + String lindex(byte[] key, int index); + + String lset(byte[] key, int index, byte[] value); + + Integer lrem(byte[] key, int count, byte[] value); + + String lpop(byte[] key); + + String rpop(byte[] key); + + Integer sadd(byte[] key, byte[] member); + + Set smembers(byte[] key); + + Integer srem(byte[] key, byte[] member); + + String spop(byte[] key); + + Integer scard(byte[] key); + + Integer sismember(byte[] key, byte[] member); + + String srandmember(byte[] key); + + Integer zadd(byte[] key, double score, byte[] member); + + Set zrange(byte[] key, int start, int end); + + Integer zrem(byte[] key, byte[] member); + + Double zincrby(byte[] key, double score, byte[] member); + + Integer zrank(byte[] key, byte[] member); + + Integer zrevrank(byte[] key, byte[] member); + + Set zrevrange(byte[] key, int start, int end); + + Set zrangeWithScores(byte[] key, int start, int end); + + Set zrevrangeWithScores(byte[] key, int start, int end); + + Integer zcard(byte[] key); + + Double zscore(byte[] key, byte[] member); + + List sort(byte[] key); + + List sort(byte[] key, SortingParams sortingParameters); + + Integer zcount(byte[] key, double min, double max); + + Set zrangeByScore(byte[] key, double min, double max); + + Set zrangeByScore( + byte[] key, + double min, + double max, + int offset, + int count); + + Set zrangeByScoreWithScores(byte[] key, double min, double max); + + Set zrangeByScoreWithScores( + byte[] key, + double min, + double max, + int offset, + int count); + + Integer zremrangeByRank(byte[] key, int start, int end); + + Integer zremrangeByScore(byte[] key, double start, double end); + + Integer linsert( + byte[] key, + LIST_POSITION where, + byte[] pivot, + byte[] value); +} From 7213955d62dc4c3f90ad6ac180482735001d300c Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 13:59:26 +0100 Subject: [PATCH 10/40] JedisCommands inherit from BinaryJediCommands --- .../clients/jedis/BinaryJedisCommands.java | 42 +++++++++---------- .../redis/clients/jedis/JedisCommands.java | 2 +- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java index ccec1b8..ffd6789 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java +++ b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java @@ -12,7 +12,7 @@ import redis.clients.jedis.BinaryClient.LIST_POSITION; public interface BinaryJedisCommands { String set(byte[] key, byte[] value); - String get(byte[] key); + byte[] get(byte[] key); Integer exists(byte[] key); @@ -24,7 +24,7 @@ public interface BinaryJedisCommands { Integer ttl(byte[] key); - String getSet(byte[] key, byte[] value); + byte[] getSet(byte[] key, byte[] value); Integer setnx(byte[] key, byte[] value); @@ -40,17 +40,17 @@ public interface BinaryJedisCommands { Integer append(byte[] key, byte[] value); - String substr(byte[] key, int start, int end); + byte[] substr(byte[] key, int start, int end); Integer hset(byte[] key, byte[] field, byte[] value); - String hget(byte[] key, byte[] field); + byte[] hget(byte[] key, byte[] field); Integer hsetnx(byte[] key, byte[] field, byte[] value); String hmset(byte[] key, Map hash); - List hmget(byte[] key, String... fields); + List hmget(byte[] key, String... fields); Integer hincrBy(byte[] key, byte[] field, int value); @@ -60,11 +60,11 @@ public interface BinaryJedisCommands { Integer hlen(byte[] key); - List hkeys(byte[] key); + List hkeys(byte[] key); - List hvals(byte[] key); + List hvals(byte[] key); - Map hgetAll(byte[] key); + Map hgetAll(byte[] key); Integer rpush(byte[] key, byte[] string); @@ -72,37 +72,37 @@ public interface BinaryJedisCommands { Integer llen(byte[] key); - List lrange(byte[] key, int start, int end); + List lrange(byte[] key, int start, int end); String ltrim(byte[] key, int start, int end); - String lindex(byte[] key, int index); + byte[] lindex(byte[] key, int index); String lset(byte[] key, int index, byte[] value); Integer lrem(byte[] key, int count, byte[] value); - String lpop(byte[] key); + byte[] lpop(byte[] key); - String rpop(byte[] key); + byte[] rpop(byte[] key); Integer sadd(byte[] key, byte[] member); - Set smembers(byte[] key); + Set smembers(byte[] key); Integer srem(byte[] key, byte[] member); - String spop(byte[] key); + byte[] spop(byte[] key); Integer scard(byte[] key); Integer sismember(byte[] key, byte[] member); - String srandmember(byte[] key); + byte[] srandmember(byte[] key); Integer zadd(byte[] key, double score, byte[] member); - Set zrange(byte[] key, int start, int end); + Set zrange(byte[] key, int start, int end); Integer zrem(byte[] key, byte[] member); @@ -112,7 +112,7 @@ public interface BinaryJedisCommands { Integer zrevrank(byte[] key, byte[] member); - Set zrevrange(byte[] key, int start, int end); + Set zrevrange(byte[] key, int start, int end); Set zrangeWithScores(byte[] key, int start, int end); @@ -122,15 +122,15 @@ public interface BinaryJedisCommands { Double zscore(byte[] key, byte[] member); - List sort(byte[] key); + List sort(byte[] key); - List sort(byte[] key, SortingParams sortingParameters); + List sort(byte[] key, SortingParams sortingParameters); Integer zcount(byte[] key, double min, double max); - Set zrangeByScore(byte[] key, double min, double max); + Set zrangeByScore(byte[] key, double min, double max); - Set zrangeByScore( + Set zrangeByScore( byte[] key, double min, double max, diff --git a/src/main/java/redis/clients/jedis/JedisCommands.java b/src/main/java/redis/clients/jedis/JedisCommands.java index 126e28d..4896784 100644 --- a/src/main/java/redis/clients/jedis/JedisCommands.java +++ b/src/main/java/redis/clients/jedis/JedisCommands.java @@ -7,7 +7,7 @@ import java.util.Set; /** * Common interface for sharded and non-sharded Jedis */ -public interface JedisCommands { +public interface JedisCommands extends BinaryJedisCommands { String set(String key, String value); String get(String key); From ef1cbfafc91e924f648dfc7f28b20c3fdd7dc7a0 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 14:08:18 +0100 Subject: [PATCH 11/40] JedisCommands DOES NOT inherit from BinaryJedisCommand --- src/main/java/redis/clients/jedis/JedisCommands.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/redis/clients/jedis/JedisCommands.java b/src/main/java/redis/clients/jedis/JedisCommands.java index 4896784..126e28d 100644 --- a/src/main/java/redis/clients/jedis/JedisCommands.java +++ b/src/main/java/redis/clients/jedis/JedisCommands.java @@ -7,7 +7,7 @@ import java.util.Set; /** * Common interface for sharded and non-sharded Jedis */ -public interface JedisCommands extends BinaryJedisCommands { +public interface JedisCommands { String set(String key, String value); String get(String key); From 37a39152984e1b9a6b2c053d7730ae05c38c6f3b Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 14:49:23 +0100 Subject: [PATCH 12/40] BinaryJedis is born, Jedis inherits from it --- .../java/redis/clients/jedis/BinaryJedis.java | 2856 +++++++++++++++++ .../clients/jedis/BinaryJedisCommands.java | 4 +- src/main/java/redis/clients/jedis/Jedis.java | 679 +--- 3 files changed, 2984 insertions(+), 555 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/BinaryJedis.java diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java new file mode 100644 index 0000000..c6dd983 --- /dev/null +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -0,0 +1,2856 @@ +package redis.clients.jedis; + +import java.io.IOException; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import redis.clients.jedis.BinaryClient.LIST_POSITION; + +public class BinaryJedis implements BinaryJedisCommands { + protected Client client = null; + + public BinaryJedis(final String host) { + client = new Client(host); + } + + public BinaryJedis(final String host, final int port) { + client = new Client(host, port); + } + + public BinaryJedis(final String host, final int port, final int timeout) { + client = new Client(host, port); + client.setTimeout(timeout); + } + + public BinaryJedis(final JedisShardInfo shardInfo) { + client = new Client(shardInfo.getHost(), shardInfo.getPort()); + client.setTimeout(shardInfo.getTimeout()); + if (shardInfo.getPassword() != null) { + this.auth(shardInfo.getPassword()); + } + } + + public String ping() { + checkIsInMulti(); + client.ping(); + return client.getStatusCodeReply(); + } + + /** + * Set the string value as value of the key. The string can't be longer than + * 1073741824 bytes (1 GB). + *

+ * Time complexity: O(1) + * + * @param key + * @param value + * @return Status code reply + */ + public String set(final byte[] key, final byte[] value) { + checkIsInMulti(); + client.set(key, value); + return client.getStatusCodeReply(); + } + + /** + * Get the value of the specified key. If the key does not exist the special + * value 'nil' is returned. If the value stored at key is not a string an + * error is returned because GET can only handle string values. + *

+ * Time complexity: O(1) + * + * @param key + * @return Bulk reply + */ + public byte[] get(final byte[] key) { + checkIsInMulti(); + client.get(key); + return client.getBinaryBulkReply(); + } + + /** + * Ask the server to silently close the connection. + */ + public void quit() { + checkIsInMulti(); + client.quit(); + } + + /** + * Test if the specified key exists. The command returns "0" if the key + * exists, otherwise "1" is returned. Note that even keys set with an empty + * string as value will return "1". + * + * Time complexity: O(1) + * + * @param key + * @return Integer reply, "0" if the key exists, otherwise "1" + */ + public Integer exists(final byte[] key) { + checkIsInMulti(); + client.exists(key); + return client.getIntegerReply(); + } + + /** + * Remove the specified keys. If a given key does not exist no operation is + * performed for this key. The command returns the number of keys removed. + * + * Time complexity: O(1) + * + * @param keys + * @return Integer reply, specifically: an integer greater than 0 if one or + * more keys were removed 0 if none of the specified key existed + */ + public Integer del(final byte[]... keys) { + checkIsInMulti(); + client.del(keys); + return client.getIntegerReply(); + } + + /** + * Return the type of the value stored at key in form of a string. The type + * can be one of "none", "string", "list", "set". "none" is returned if the + * key does not exist. + * + * Time complexity: O(1) + * + * @param key + * @return Status code reply, specifically: "none" if the key does not exist + * "string" if the key contains a String value "list" if the key + * contains a List value "set" if the key contains a Set value + * "zset" if the key contains a Sorted Set value "hash" if the key + * contains a Hash value + */ + public String type(final byte[] key) { + checkIsInMulti(); + client.type(key); + return client.getStatusCodeReply(); + } + + /** + * Delete all the keys of the currently selected DB. This command never + * fails. + * + * @return Status code reply + */ + public String flushDB() { + checkIsInMulti(); + client.flushDB(); + return client.getStatusCodeReply(); + } + + /** + * Returns all the keys matching the glob-style pattern as space separated + * strings. For example if you have in the database the keys "foo" and + * "foobar" the command "KEYS foo*" will return "foo foobar". + *

+ * Note that while the time complexity for this operation is O(n) the + * constant times are pretty low. For example Redis running on an entry + * level laptop can scan a 1 million keys database in 40 milliseconds. + * Still it's better to consider this one of the slow commands that may + * ruin the DB performance if not used with care. + *

+ * In other words this command is intended only for debugging and special + * operations like creating a script to change the DB schema. Don't use it + * in your normal code. Use Redis Sets in order to group together a subset + * of objects. + *

+ * Glob style patterns examples: + *

    + *
  • h?llo will match hello hallo hhllo + *
  • h*llo will match hllo heeeello + *
  • h[ae]llo will match hello and hallo, but not hillo + *
+ *

+ * Use \ to escape special chars if you want to match them verbatim. + *

+ * Time complexity: O(n) (with n being the number of keys in the DB, and + * assuming keys and pattern of limited length) + * + * @param pattern + * @return Multi bulk reply + */ + public List keys(final byte[] pattern) { + checkIsInMulti(); + client.keys(pattern); + return client.getBinaryMultiBulkReply(); + } + + /** + * Return a randomly selected key from the currently selected DB. + *

+ * Time complexity: O(1) + * + * @return Singe line reply, specifically the randomly selected key or an + * empty string is the database is empty + */ + public byte[] randomBinaryKey() { + checkIsInMulti(); + client.randomKey(); + return client.getBinaryBulkReply(); + } + + /** + * Atomically renames the key oldkey to newkey. If the source and + * destination name are the same an error is returned. If newkey already + * exists it is overwritten. + *

+ * Time complexity: O(1) + * + * @param oldkey + * @param newkey + * @return Status code repy + */ + public String rename(final byte[] oldkey, final byte[] newkey) { + checkIsInMulti(); + client.rename(oldkey, newkey); + return client.getStatusCodeReply(); + } + + /** + * Rename oldkey into newkey but fails if the destination key newkey already + * exists. + *

+ * Time complexity: O(1) + * + * @param oldkey + * @param newkey + * @return Integer reply, specifically: 1 if the key was renamed 0 if the + * target key already exist + */ + public Integer renamenx(final byte[] oldkey, final byte[] newkey) { + checkIsInMulti(); + client.renamenx(oldkey, newkey); + return client.getIntegerReply(); + } + + /** + * Return the number of keys in the currently selected database. + * + * @return Integer reply + */ + public Integer dbSize() { + checkIsInMulti(); + client.dbSize(); + return client.getIntegerReply(); + } + + /** + * Set a timeout on the specified key. After the timeout the key will be + * automatically deleted by the server. A key with an associated timeout is + * said to be volatile in Redis terminology. + *

+ * Voltile keys are stored on disk like the other keys, the timeout is + * persistent too like all the other aspects of the dataset. Saving a + * dataset containing expires and stopping the server does not stop the flow + * of time as Redis stores on disk the time when the key will no longer be + * available as Unix time, and not the remaining seconds. + *

+ * Since Redis 2.1.3 you can update the value of the timeout of a key + * already having an expire set. It is also possible to undo the expire at + * all turning the key into a normal key using the {@link #persist(String) + * PERSIST} command. + *

+ * Time complexity: O(1) + * + * @see ExpireCommand + * + * @param key + * @param seconds + * @return Integer reply, specifically: 1: the timeout was set. 0: the + * timeout was not set since the key already has an associated + * timeout (this may happen only in Redis versions < 2.1.3, Redis >= + * 2.1.3 will happily update the timeout), or the key does not + * exist. + */ + public Integer expire(final byte[] key, final int seconds) { + checkIsInMulti(); + client.expire(key, seconds); + return client.getIntegerReply(); + } + + /** + * EXPIREAT works exctly like {@link #expire(String, int) EXPIRE} but + * instead to get the number of seconds representing the Time To Live of the + * key as a second argument (that is a relative way of specifing the TTL), + * it takes an absolute one in the form of a UNIX timestamp (Number of + * seconds elapsed since 1 Gen 1970). + *

+ * EXPIREAT was introduced in order to implement the Append Only File + * persistence mode so that EXPIRE commands are automatically translated + * into EXPIREAT commands for the append only file. Of course EXPIREAT can + * also used by programmers that need a way to simply specify that a given + * key should expire at a given time in the future. + *

+ * Since Redis 2.1.3 you can update the value of the timeout of a key + * already having an expire set. It is also possible to undo the expire at + * all turning the key into a normal key using the {@link #persist(String) + * PERSIST} command. + *

+ * Time complexity: O(1) + * + * @see ExpireCommand + * + * @param key + * @param unixTime + * @return Integer reply, specifically: 1: the timeout was set. 0: the + * timeout was not set since the key already has an associated + * timeout (this may happen only in Redis versions < 2.1.3, Redis >= + * 2.1.3 will happily update the timeout), or the key does not + * exist. + */ + public Integer expireAt(final byte[] key, final long unixTime) { + checkIsInMulti(); + client.expireAt(key, unixTime); + return client.getIntegerReply(); + } + + /** + * The TTL command returns the remaining time to live in seconds of a key + * that has an {@link #expire(String, int) EXPIRE} set. This introspection + * capability allows a Redis client to check how many seconds a given key + * will continue to be part of the dataset. + * + * @param key + * @return Integer reply, returns the remaining time to live in seconds of a + * key that has an EXPIRE. If the Key does not exists or does not + * have an associated expire, -1 is returned. + */ + public Integer ttl(final byte[] key) { + checkIsInMulti(); + client.ttl(key); + return client.getIntegerReply(); + } + + /** + * Select the DB with having the specified zero-based numeric index. For + * default every new client connection is automatically selected to DB 0. + * + * @param index + * @return Status code reply + */ + public String select(final int index) { + checkIsInMulti(); + client.select(index); + return client.getStatusCodeReply(); + } + + /** + * Move the specified key from the currently selected DB to the specified + * destination DB. Note that this command returns 1 only if the key was + * successfully moved, and 0 if the target key was already there or if the + * source key was not found at all, so it is possible to use MOVE as a + * locking primitive. + * + * @param key + * @param dbIndex + * @return Integer reply, specifically: 1 if the key was moved 0 if the key + * was not moved because already present on the target DB or was not + * found in the current DB. + */ + public Integer move(final byte[] key, final int dbIndex) { + checkIsInMulti(); + client.move(key, dbIndex); + return client.getIntegerReply(); + } + + /** + * Delete all the keys of all the existing databases, not just the currently + * selected one. This command never fails. + * + * @return Status code reply + */ + public String flushAll() { + checkIsInMulti(); + client.flushAll(); + return client.getStatusCodeReply(); + } + + /** + * GETSET is an atomic set this value and return the old value command. Set + * key to the string value and return the old value stored at key. The + * string can't be longer than 1073741824 bytes (1 GB). + *

+ * Time complexity: O(1) + * + * @param key + * @param value + * @return Bulk reply + */ + public byte[] getSet(final byte[] key, final byte[] value) { + checkIsInMulti(); + client.getSet(key, value); + return client.getBinaryBulkReply(); + } + + /** + * Get the values of all the specified keys. If one or more keys dont exist + * or is not of type String, a 'nil' value is returned instead of the value + * of the specified key, but the operation never fails. + *

+ * Time complexity: O(1) for every key + * + * @param keys + * @return Multi bulk reply + */ + public List mget(final byte[]... keys) { + checkIsInMulti(); + client.mget(keys); + return client.getBinaryMultiBulkReply(); + } + + /** + * SETNX works exactly like {@link #set(String, String) SET} with the only + * difference that if the key already exists no operation is performed. + * SETNX actually means "SET if Not eXists". + *

+ * Time complexity: O(1) + * + * @param key + * @param value + * @return Integer reply, specifically: 1 if the key was set 0 if the key + * was not set + */ + public Integer setnx(final byte[] key, final byte[] value) { + checkIsInMulti(); + client.setnx(key, value); + return client.getIntegerReply(); + } + + /** + * The command is exactly equivalent to the following group of commands: + * {@link #set(String, String) SET} + {@link #expire(String, int) EXPIRE}. + * The operation is atomic. + *

+ * Time complexity: O(1) + * + * @param key + * @param seconds + * @param value + * @return Status code reply + */ + public String setex(final byte[] key, final int seconds, final byte[] value) { + checkIsInMulti(); + client.setex(key, seconds, value); + return client.getStatusCodeReply(); + } + + /** + * Set the the respective keys to the respective values. MSET will replace + * old values with new values, while {@link #msetnx(String...) MSETNX} will + * not perform any operation at all even if just a single key already + * exists. + *

+ * Because of this semantic MSETNX can be used in order to set different + * keys representing different fields of an unique logic object in a way + * that ensures that either all the fields or none at all are set. + *

+ * Both MSET and MSETNX are atomic operations. This means that for instance + * if the keys A and B are modified, another client talking to Redis can + * either see the changes to both A and B at once, or no modification at + * all. + * + * @see #msetnx(String...) + * + * @param keysvalues + * @return Status code reply Basically +OK as MSET can't fail + */ + public String mset(final byte[]... keysvalues) { + checkIsInMulti(); + client.mset(keysvalues); + return client.getStatusCodeReply(); + } + + /** + * Set the the respective keys to the respective values. + * {@link #mset(String...) MSET} will replace old values with new values, + * while MSETNX will not perform any operation at all even if just a single + * key already exists. + *

+ * Because of this semantic MSETNX can be used in order to set different + * keys representing different fields of an unique logic object in a way + * that ensures that either all the fields or none at all are set. + *

+ * Both MSET and MSETNX are atomic operations. This means that for instance + * if the keys A and B are modified, another client talking to Redis can + * either see the changes to both A and B at once, or no modification at + * all. + * + * @see #mset(String...) + * + * @param keysvalues + * @return Integer reply, specifically: 1 if the all the keys were set 0 if + * no key was set (at least one key already existed) + */ + public Integer msetnx(final byte[]... keysvalues) { + checkIsInMulti(); + client.msetnx(keysvalues); + return client.getIntegerReply(); + } + + /** + * IDECRBY work just like {@link #decr(String) INCR} but instead to + * decrement by 1 the decrement is integer. + *

+ * INCR commands are limited to 64 bit signed integers. + *

+ * Note: this is actually a string operation, that is, in Redis there are + * not "integer" types. Simply the string stored at the key is parsed as a + * base 10 64 bit signed integer, incremented, and then converted back as a + * string. + *

+ * Time complexity: O(1) + * + * @see #incr(String) + * @see #decr(String) + * @see #incrBy(String, int) + * + * @param key + * @param integer + * @return Integer reply, this commands will reply with the new value of key + * after the increment. + */ + public Integer decrBy(final byte[] key, final int integer) { + checkIsInMulti(); + client.decrBy(key, integer); + return client.getIntegerReply(); + } + + /** + * Decrement the number stored at key by one. If the key does not exist or + * contains a value of a wrong type, set the key to the value of "0" before + * to perform the decrement operation. + *

+ * INCR commands are limited to 64 bit signed integers. + *

+ * Note: this is actually a string operation, that is, in Redis there are + * not "integer" types. Simply the string stored at the key is parsed as a + * base 10 64 bit signed integer, incremented, and then converted back as a + * string. + *

+ * Time complexity: O(1) + * + * @see #incr(String) + * @see #incrBy(String, int) + * @see #decrBy(String, int) + * + * @param key + * @return Integer reply, this commands will reply with the new value of key + * after the increment. + */ + public Integer decr(final byte[] key) { + checkIsInMulti(); + client.decr(key); + return client.getIntegerReply(); + } + + /** + * INCRBY work just like {@link #incr(String) INCR} but instead to increment + * by 1 the increment is integer. + *

+ * INCR commands are limited to 64 bit signed integers. + *

+ * Note: this is actually a string operation, that is, in Redis there are + * not "integer" types. Simply the string stored at the key is parsed as a + * base 10 64 bit signed integer, incremented, and then converted back as a + * string. + *

+ * Time complexity: O(1) + * + * @see #incr(String) + * @see #decr(String) + * @see #decrBy(String, int) + * + * @param key + * @param integer + * @return Integer reply, this commands will reply with the new value of key + * after the increment. + */ + public Integer incrBy(final byte[] key, final int integer) { + checkIsInMulti(); + client.incrBy(key, integer); + return client.getIntegerReply(); + } + + /** + * Increment the number stored at key by one. If the key does not exist or + * contains a value of a wrong type, set the key to the value of "0" before + * to perform the increment operation. + *

+ * INCR commands are limited to 64 bit signed integers. + *

+ * Note: this is actually a string operation, that is, in Redis there are + * not "integer" types. Simply the string stored at the key is parsed as a + * base 10 64 bit signed integer, incremented, and then converted back as a + * string. + *

+ * Time complexity: O(1) + * + * @see #incrBy(String, int) + * @see #decr(String) + * @see #decrBy(String, int) + * + * @param key + * @return Integer reply, this commands will reply with the new value of key + * after the increment. + */ + public Integer incr(final byte[] key) { + checkIsInMulti(); + client.incr(key); + return client.getIntegerReply(); + } + + /** + * If the key already exists and is a string, this command appends the + * provided value at the end of the string. If the key does not exist it is + * created and set as an empty string, so APPEND will be very similar to SET + * in this special case. + *

+ * Time complexity: O(1). The amortized time complexity is O(1) assuming the + * appended value is small and the already present value is of any size, + * since the dynamic string library used by Redis will double the free space + * available on every reallocation. + * + * @param key + * @param value + * @return Integer reply, specifically the total length of the string after + * the append operation. + */ + public Integer append(final byte[] key, final byte[] value) { + checkIsInMulti(); + client.append(key, value); + return client.getIntegerReply(); + } + + /** + * Return a subset of the string from offset start to offset end (both + * offsets are inclusive). Negative offsets can be used in order to provide + * an offset starting from the end of the string. So -1 means the last char, + * -2 the penultimate and so forth. + *

+ * The function handles out of range requests without raising an error, but + * just limiting the resulting range to the actual length of the string. + *

+ * Time complexity: O(start+n) (with start being the start index and n the + * total length of the requested range). Note that the lookup part of this + * command is O(1) so for small strings this is actually an O(1) command. + * + * @param key + * @param start + * @param end + * @return Bulk reply + */ + public byte[] substr(final byte[] key, final int start, final int end) { + checkIsInMulti(); + client.substr(key, start, end); + return client.getBinaryBulkReply(); + } + + /** + * + * Set the specified hash field to the specified value. + *

+ * If key does not exist, a new key holding a hash is created. + *

+ * Time complexity: O(1) + * + * @param key + * @param field + * @param value + * @return If the field already exists, and the HSET just produced an update + * of the value, 0 is returned, otherwise if a new field is created + * 1 is returned. + */ + public Integer hset(final byte[] key, final byte[] field, final byte[] value) { + checkIsInMulti(); + client.hset(key, field, value); + return client.getIntegerReply(); + } + + /** + * If key holds a hash, retrieve the value associated to the specified + * field. + *

+ * If the field is not found or the key does not exist, a special 'nil' + * value is returned. + *

+ * Time complexity: O(1) + * + * @param key + * @param field + * @return Bulk reply + */ + public byte[] hget(final byte[] key, final byte[] field) { + checkIsInMulti(); + client.hget(key, field); + return client.getBinaryBulkReply(); + } + + /** + * + * Set the specified hash field to the specified value if the field not + * exists. Time complexity: O(1) + * + * @param key + * @param field + * @param value + * @return If the field already exists, 0 is returned, otherwise if a new + * field is created 1 is returned. + */ + public Integer hsetnx(final byte[] key, final byte[] field, final byte[] value) { + checkIsInMulti(); + client.hsetnx(key, field, value); + return client.getIntegerReply(); + } + + /** + * Set the respective fields to the respective values. HMSET replaces old + * values with new values. + *

+ * If key does not exist, a new key holding a hash is created. + *

+ * Time complexity: O(N) (with N being the number of fields) + * + * @param key + * @param hash + * @return Always OK because HMSET can't fail + */ + public String hmset(final byte[] key, final Map hash) { + checkIsInMulti(); + client.hmset(key, hash); + return client.getStatusCodeReply(); + } + + /** + * Retrieve the values associated to the specified fields. + *

+ * If some of the specified fields do not exist, nil values are returned. + * Non existing keys are considered like empty hashes. + *

+ * Time complexity: O(N) (with N being the number of fields) + * + * @param key + * @param fields + * @return Multi Bulk Reply specifically a list of all the values associated + * with the specified fields, in the same order of the request. + */ + public List hmget(final byte[] key, final byte[]... fields) { + checkIsInMulti(); + client.hmget(key, fields); + return client.getBinaryMultiBulkReply(); + } + + /** + * Increment the number stored at field in the hash at key by value. If key + * does not exist, a new key holding a hash is created. If field does not + * exist or holds a string, the value is set to 0 before applying the + * operation. Since the value argument is signed you can use this command to + * perform both increments and decrements. + *

+ * The range of values supported by HINCRBY is limited to 64 bit signed + * integers. + *

+ * Time complexity: O(1) + * + * @param key + * @param field + * @param value + * @return Integer reply The new value at field after the increment + * operation. + */ + public Integer hincrBy(final byte[] key, final byte[] field, final int value) { + checkIsInMulti(); + client.hincrBy(key, field, value); + return client.getIntegerReply(); + } + + /** + * Test for existence of a specified field in a hash. + * + * Time complexity: O(1) + * + * @param key + * @param field + * @return Return 1 if the hash stored at key contains the specified field. + * Return 0 if the key is not found or the field is not present. + */ + public Integer hexists(final byte[] key, final byte[] field) { + checkIsInMulti(); + client.hexists(key, field); + return client.getIntegerReply(); + } + + /** + * Remove the specified field from an hash stored at key. + *

+ * Time complexity: O(1) + * + * @param key + * @param field + * @return If the field was present in the hash it is deleted and 1 is + * returned, otherwise 0 is returned and no operation is performed. + */ + public Integer hdel(final byte[] key, final byte[] field) { + checkIsInMulti(); + client.hdel(key, field); + return client.getIntegerReply(); + } + + /** + * Return the number of items in a hash. + *

+ * Time complexity: O(1) + * + * @param key + * @return The number of entries (fields) contained in the hash stored at + * key. If the specified key does not exist, 0 is returned assuming + * an empty hash. + */ + public Integer hlen(final byte[] key) { + checkIsInMulti(); + client.hlen(key); + return client.getIntegerReply(); + } + + /** + * Return all the fields in a hash. + *

+ * Time complexity: O(N), where N is the total number of entries + * + * @param key + * @return All the fields names contained into a hash. + */ + public List hkeys(final byte[] key) { + checkIsInMulti(); + client.hkeys(key); + return client.getBinaryMultiBulkReply(); + } + + /** + * Return all the values in a hash. + *

+ * Time complexity: O(N), where N is the total number of entries + * + * @param key + * @return All the fields values contained into a hash. + */ + public List hvals(final byte[] key) { + checkIsInMulti(); + client.hvals(key); + return client.getBinaryMultiBulkReply(); + } + + /** + * Return all the fields and associated values in a hash. + *

+ * Time complexity: O(N), where N is the total number of entries + * + * @param key + * @return All the fields and values contained into a hash. + */ + public Map hgetAll(final byte[] key) { + checkIsInMulti(); + client.hgetAll(key); + final List flatHash = client.getBinaryMultiBulkReply(); + final Map hash = new HashMap(); + final Iterator iterator = flatHash.iterator(); + while (iterator.hasNext()) { + hash.put(iterator.next(), iterator.next()); + } + + return hash; + } + + /** + * Add the string value to the head (LPUSH) or tail (RPUSH) of the list + * stored at key. If the key does not exist an empty list is created just + * before the append operation. If the key exists but is not a List an error + * is returned. + *

+ * Time complexity: O(1) + * + * @see BinaryJedis#lpush(String, String) + * + * @param key + * @param string + * @return Integer reply, specifically, the number of elements inside the + * list after the push operation. + */ + public Integer rpush(final byte[] key, final byte[] string) { + checkIsInMulti(); + client.rpush(key, string); + return client.getIntegerReply(); + } + + /** + * Add the string value to the head (LPUSH) or tail (RPUSH) of the list + * stored at key. If the key does not exist an empty list is created just + * before the append operation. If the key exists but is not a List an error + * is returned. + *

+ * Time complexity: O(1) + * + * @see BinaryJedis#rpush(String, String) + * + * @param key + * @param string + * @return Integer reply, specifically, the number of elements inside the + * list after the push operation. + */ + public Integer lpush(final byte[] key, final byte[] string) { + checkIsInMulti(); + client.lpush(key, string); + return client.getIntegerReply(); + } + + /** + * Return the length of the list stored at the specified key. If the key + * does not exist zero is returned (the same behaviour as for empty lists). + * If the value stored at key is not a list an error is returned. + *

+ * Time complexity: O(1) + * + * @param key + * @return The length of the list. + */ + public Integer llen(final byte[] key) { + checkIsInMulti(); + client.llen(key); + return client.getIntegerReply(); + } + + /** + * Return the specified elements of the list stored at the specified key. + * Start and end are zero-based indexes. 0 is the first element of the list + * (the list head), 1 the next element and so on. + *

+ * For example LRANGE foobar 0 2 will return the first three elements of the + * list. + *

+ * start and end can also be negative numbers indicating offsets from the + * end of the list. For example -1 is the last element of the list, -2 the + * penultimate element and so on. + *

+ * Consistency with range functions in various programming languages + *

+ * Note that if you have a list of numbers from 0 to 100, LRANGE 0 10 will + * return 11 elements, that is, rightmost item is included. This may or may + * not be consistent with behavior of range-related functions in your + * programming language of choice (think Ruby's Range.new, Array#slice or + * Python's range() function). + *

+ * LRANGE behavior is consistent with one of Tcl. + *

+ * Out-of-range indexes + *

+ * Indexes out of range will not produce an error: if start is over the end + * of the list, or start > end, an empty list is returned. If end is over + * the end of the list Redis will threat it just like the last element of + * the list. + *

+ * Time complexity: O(start+n) (with n being the length of the range and + * start being the start offset) + * + * @param key + * @param start + * @param end + * @return Multi bulk reply, specifically a list of elements in the + * specified range. + */ + public List lrange(final byte[] key, final int start, final int end) { + checkIsInMulti(); + client.lrange(key, start, end); + return client.getBinaryMultiBulkReply(); + } + + /** + * Trim an existing list so that it will contain only the specified range of + * elements specified. Start and end are zero-based indexes. 0 is the first + * element of the list (the list head), 1 the next element and so on. + *

+ * For example LTRIM foobar 0 2 will modify the list stored at foobar key so + * that only the first three elements of the list will remain. + *

+ * start and end can also be negative numbers indicating offsets from the + * end of the list. For example -1 is the last element of the list, -2 the + * penultimate element and so on. + *

+ * Indexes out of range will not produce an error: if start is over the end + * of the list, or start > end, an empty list is left as value. If end over + * the end of the list Redis will threat it just like the last element of + * the list. + *

+ * Hint: the obvious use of LTRIM is together with LPUSH/RPUSH. For example: + *

+ * {@code lpush("mylist", "someelement"); ltrim("mylist", 0, 99); * } + *

+ * The above two commands will push elements in the list taking care that + * the list will not grow without limits. This is very useful when using + * Redis to store logs for example. It is important to note that when used + * in this way LTRIM is an O(1) operation because in the average case just + * one element is removed from the tail of the list. + *

+ * Time complexity: O(n) (with n being len of list - len of range) + * + * @param key + * @param start + * @param end + * @return Status code reply + */ + public String ltrim(final byte[] key, final int start, final int end) { + checkIsInMulti(); + client.ltrim(key, start, end); + return client.getStatusCodeReply(); + } + + /** + * Return the specified element of the list stored at the specified key. 0 + * is the first element, 1 the second and so on. Negative indexes are + * supported, for example -1 is the last element, -2 the penultimate and so + * on. + *

+ * If the value stored at key is not of list type an error is returned. If + * the index is out of range a 'nil' reply is returned. + *

+ * Note that even if the average time complexity is O(n) asking for the + * first or the last element of the list is O(1). + *

+ * Time complexity: O(n) (with n being the length of the list) + * + * @param key + * @param index + * @return Bulk reply, specifically the requested element + */ + public byte[] lindex(final byte[] key, final int index) { + checkIsInMulti(); + client.lindex(key, index); + return client.getBinaryBulkReply(); + } + + /** + * Set a new value as the element at index position of the List at key. + *

+ * Out of range indexes will generate an error. + *

+ * Similarly to other list commands accepting indexes, the index can be + * negative to access elements starting from the end of the list. So -1 is + * the last element, -2 is the penultimate, and so forth. + *

+ * Time complexity: + *

+ * O(N) (with N being the length of the list), setting the first or last + * elements of the list is O(1). + * + * @see #lindex(String, int) + * + * @param key + * @param index + * @param value + * @return Status code reply + */ + public String lset(final byte[] key, final int index, final byte[] value) { + checkIsInMulti(); + client.lset(key, index, value); + return client.getStatusCodeReply(); + } + + /** + * Remove the first count occurrences of the value element from the list. If + * count is zero all the elements are removed. If count is negative elements + * are removed from tail to head, instead to go from head to tail that is + * the normal behaviour. So for example LREM with count -2 and hello as + * value to remove against the list (a,b,c,hello,x,hello,hello) will lave + * the list (a,b,c,hello,x). The number of removed elements is returned as + * an integer, see below for more information about the returned value. Note + * that non existing keys are considered like empty lists by LREM, so LREM + * against non existing keys will always return 0. + *

+ * Time complexity: O(N) (with N being the length of the list) + * + * @param key + * @param count + * @param value + * @return Integer Reply, specifically: The number of removed elements if + * the operation succeeded + */ + public Integer lrem(final byte[] key, final int count, final byte[] value) { + checkIsInMulti(); + client.lrem(key, count, value); + return client.getIntegerReply(); + } + + /** + * Atomically return and remove the first (LPOP) or last (RPOP) element of + * the list. For example if the list contains the elements "a","b","c" LPOP + * will return "a" and the list will become "b","c". + *

+ * If the key does not exist or the list is already empty the special value + * 'nil' is returned. + * + * @see #rpop(String) + * + * @param key + * @return Bulk reply + */ + public byte[] lpop(final byte[] key) { + checkIsInMulti(); + client.lpop(key); + return client.getBinaryBulkReply(); + } + + /** + * Atomically return and remove the first (LPOP) or last (RPOP) element of + * the list. For example if the list contains the elements "a","b","c" LPOP + * will return "a" and the list will become "b","c". + *

+ * If the key does not exist or the list is already empty the special value + * 'nil' is returned. + * + * @see #lpop(String) + * + * @param key + * @return Bulk reply + */ + public byte[] rpop(final byte[] key) { + checkIsInMulti(); + client.rpop(key); + return client.getBinaryBulkReply(); + } + + /** + * Atomically return and remove the last (tail) element of the srckey list, + * and push the element as the first (head) element of the dstkey list. For + * example if the source list contains the elements "a","b","c" and the + * destination list contains the elements "foo","bar" after an RPOPLPUSH + * command the content of the two lists will be "a","b" and "c","foo","bar". + *

+ * If the key does not exist or the list is already empty the special value + * 'nil' is returned. If the srckey and dstkey are the same the operation is + * equivalent to removing the last element from the list and pusing it as + * first element of the list, so it's a "list rotation" command. + *

+ * Time complexity: O(1) + * + * @param srckey + * @param dstkey + * @return Bulk reply + */ + public byte[] rpoplpush(final byte[] srckey, final byte[] dstkey) { + checkIsInMulti(); + client.rpoplpush(srckey, dstkey); + return client.getBinaryBulkReply(); + } + + /** + * Add the specified member to the set value stored at key. If member is + * already a member of the set no operation is performed. If key does not + * exist a new set with the specified member as sole member is created. If + * the key exists but does not hold a set value an error is returned. + *

+ * Time complexity O(1) + * + * @param key + * @param member + * @return Integer reply, specifically: 1 if the new element was added 0 if + * the element was already a member of the set + */ + public Integer sadd(final byte[] key, final byte[] member) { + checkIsInMulti(); + client.sadd(key, member); + return client.getIntegerReply(); + } + + /** + * Return all the members (elements) of the set value stored at key. This is + * just syntax glue for {@link #sinter(String...) SINTER}. + *

+ * Time complexity O(N) + * + * @param key + * @return Multi bulk reply + */ + public Set smembers(final byte[] key) { + checkIsInMulti(); + client.smembers(key); + final List members = client.getBinaryMultiBulkReply(); + return new LinkedHashSet(members); + } + + /** + * Remove the specified member from the set value stored at key. If member + * was not a member of the set no operation is performed. If key does not + * hold a set value an error is returned. + *

+ * Time complexity O(1) + * + * @param key + * @param member + * @return Integer reply, specifically: 1 if the new element was removed 0 + * if the new element was not a member of the set + */ + public Integer srem(final byte[] key, final byte[] member) { + checkIsInMulti(); + client.srem(key, member); + return client.getIntegerReply(); + } + + /** + * Remove a random element from a Set returning it as return value. If the + * Set is empty or the key does not exist, a nil object is returned. + *

+ * The {@link #srandmember(String)} command does a similar work but the + * returned element is not removed from the Set. + *

+ * Time complexity O(1) + * + * @param key + * @return Bulk reply + */ + public byte[] spop(final byte[] key) { + checkIsInMulti(); + client.spop(key); + return client.getBinaryBulkReply(); + } + + /** + * Move the specifided member from the set at srckey to the set at dstkey. + * This operation is atomic, in every given moment the element will appear + * to be in the source or destination set for accessing clients. + *

+ * If the source set does not exist or does not contain the specified + * element no operation is performed and zero is returned, otherwise the + * element is removed from the source set and added to the destination set. + * On success one is returned, even if the element was already present in + * the destination set. + *

+ * An error is raised if the source or destination keys contain a non Set + * value. + *

+ * Time complexity O(1) + * + * @param srckey + * @param dstkey + * @param member + * @return Integer reply, specifically: 1 if the element was moved 0 if the + * element was not found on the first set and no operation was + * performed + */ + public Integer smove(final byte[] srckey, final byte[] dstkey, final byte[] member) { + checkIsInMulti(); + client.smove(srckey, dstkey, member); + return client.getIntegerReply(); + } + + /** + * Return the set cardinality (number of elements). If the key does not + * exist 0 is returned, like for empty sets. + * + * @param key + * @return Integer reply, specifically: the cardinality (number of elements) + * of the set as an integer. + */ + public Integer scard(final byte[] key) { + checkIsInMulti(); + client.scard(key); + return client.getIntegerReply(); + } + + /** + * Return 1 if member is a member of the set stored at key, otherwise 0 is + * returned. + *

+ * Time complexity O(1) + * + * @param key + * @param member + * @return Integer reply, specifically: 1 if the element is a member of the + * set 0 if the element is not a member of the set OR if the key + * does not exist + */ + public Integer sismember(final byte[] key, final byte[] member) { + checkIsInMulti(); + client.sismember(key, member); + return client.getIntegerReply(); + } + + /** + * Return the members of a set resulting from the intersection of all the + * sets hold at the specified keys. Like in + * {@link #lrange(String, int, int) 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. + *

+ * Non existing keys are considered like empty sets, so if one of the keys + * is missing an empty set is returned (since the intersection with an empty + * set always is an empty set). + *

+ * Time complexity O(N*M) worst case where N is the cardinality of the + * smallest set and M the number of sets + * + * @param keys + * @return Multi bulk reply, specifically the list of common elements. + */ + public Set sinter(final byte[]... keys) { + checkIsInMulti(); + client.sinter(keys); + final List members = client.getBinaryMultiBulkReply(); + return new LinkedHashSet(members); + } + + /** + * This commnad works exactly like {@link #sinter(String...) SINTER} but + * instead of being returned the resulting set is sotred as dstkey. + *

+ * Time complexity O(N*M) worst case where N is the cardinality of the + * smallest set and M the number of sets + * + * @param dstkey + * @param keys + * @return Status code reply + */ + public Integer sinterstore(final byte[] dstkey, final byte[]... keys) { + checkIsInMulti(); + client.sinterstore(dstkey, keys); + return client.getIntegerReply(); + } + + /** + * Return the members of a set resulting from the union of all the sets hold + * at the specified keys. Like in {@link #lrange(String, int, int) 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}. + *

+ * Non existing keys are considered like empty sets. + *

+ * Time complexity O(N) where N is the total number of elements in all the + * provided sets + * + * @param keys + * @return Multi bulk reply, specifically the list of common elements. + */ + public Set sunion(final byte[]... keys) { + checkIsInMulti(); + client.sunion(keys); + final List members = client.getBinaryMultiBulkReply(); + return new LinkedHashSet(members); + } + + /** + * This command works exactly like {@link #sunion(String...) SUNION} but + * instead of being returned the resulting set is stored as dstkey. Any + * existing value in dstkey will be over-written. + *

+ * Time complexity O(N) where N is the total number of elements in all the + * provided sets + * + * @param dstkey + * @param keys + * @return Status code reply + */ + public Integer sunionstore(final byte[] dstkey, final byte[]... keys) { + checkIsInMulti(); + client.sunionstore(dstkey, keys); + return client.getIntegerReply(); + } + + /** + * Return the difference between the Set stored at key1 and all the Sets + * key2, ..., keyN + *

+ * Example: + * + *

+     * key1 = [x, a, b, c]
+     * key2 = [c]
+     * key3 = [a, d]
+     * SDIFF key1,key2,key3 => [x, b]
+     * 
+ * + * Non existing keys are considered like empty sets. + *

+ * Time complexity: + *

+ * O(N) with N being the total number of elements of all the sets + * + * @param keys + * @return Return the members of a set resulting from the difference between + * the first set provided and all the successive sets. + */ + public Set sdiff(final byte[]... keys) { + checkIsInMulti(); + client.sdiff(keys); + final List members = client.getBinaryMultiBulkReply(); + return new LinkedHashSet(members); + } + + /** + * This command works exactly like {@link #sdiff(String...) SDIFF} but + * instead of being returned the resulting set is stored in dstkey. + * + * @param dstkey + * @param keys + * @return Status code reply + */ + public Integer sdiffstore(final byte[] dstkey, final byte[]... keys) { + checkIsInMulti(); + client.sdiffstore(dstkey, keys); + return client.getIntegerReply(); + } + + /** + * Return a random element from a Set, without removing the element. If the + * Set is empty or the key does not exist, a nil object is returned. + *

+ * The SPOP command does a similar work but the returned element is popped + * (removed) from the Set. + *

+ * Time complexity O(1) + * + * @param key + * @return Bulk reply + */ + public byte[] srandmember(final byte[] key) { + checkIsInMulti(); + client.srandmember(key); + return client.getBinaryBulkReply(); + } + + /** + * Add the specified member having the specifeid score to the sorted set + * stored at key. If member is already a member of the sorted set the score + * is updated, and the element reinserted in the right position to ensure + * sorting. If key does not exist a new sorted set with the specified member + * as sole member is crated. If the key exists but does not hold a sorted + * set value an error is returned. + *

+ * The score value can be the string representation of a double precision + * floating point number. + *

+ * Time complexity O(log(N)) with N being the number of elements in the + * sorted set + * + * @param key + * @param score + * @param member + * @return Integer reply, specifically: 1 if the new element was added 0 if + * the element was already a member of the sorted set and the score + * was updated + */ + public Integer zadd(final byte[] key, final double score, final byte[] member) { + checkIsInMulti(); + client.zadd(key, score, member); + return client.getIntegerReply(); + } + + public Set zrange(final byte[] key, final int start, final int end) { + checkIsInMulti(); + client.zrange(key, start, end); + final List members = client.getBinaryMultiBulkReply(); + return new LinkedHashSet(members); + } + + /** + * Remove the specified member from the sorted set value stored at key. If + * member was not a member of the set no operation is performed. If key does + * not not hold a set value an error is returned. + *

+ * Time complexity O(log(N)) with N being the number of elements in the + * sorted set + * + * + * + * @param key + * @param member + * @return Integer reply, specifically: 1 if the new element was removed 0 + * if the new element was not a member of the set + */ + public Integer zrem(final byte[] key, final byte[] member) { + checkIsInMulti(); + client.zrem(key, member); + return client.getIntegerReply(); + } + + /** + * If member already exists in the sorted set adds the increment to its + * score and updates the position of the element in the sorted set + * accordingly. If member does not already exist in the sorted set it is + * added with increment as score (that is, like if the previous score was + * virtually zero). If key does not exist a new sorted set with the + * specified member as sole member is crated. If the key exists but does not + * hold a sorted set value an error is returned. + *

+ * The score value can be the string representation of a double precision + * floating point number. It's possible to provide a negative value to + * perform a decrement. + *

+ * For an introduction to sorted sets check the Introduction to Redis data + * types page. + *

+ * Time complexity O(log(N)) with N being the number of elements in the + * sorted set + * + * @param key + * @param score + * @param member + * @return The new score + */ + public Double zincrby(final byte[] key, final double score, final byte[] member) { + checkIsInMulti(); + client.zincrby(key, score, member); + String newscore = client.getBulkReply(); + return Double.valueOf(newscore); + } + + /** + * Return the rank (or index) or member in the sorted set at key, with + * scores being ordered from low to high. + *

+ * When the given member does not exist in the sorted set, the special value + * 'nil' is returned. The returned rank (or index) of the member is 0-based + * for both commands. + *

+ * Time complexity: + *

+ * O(log(N)) + * + * @see #zrevrank(String, String) + * + * @param key + * @param member + * @return Integer reply or a nil bulk reply, specifically: the rank of the + * element as an integer reply if the element exists. A nil bulk + * reply if there is no such element. + */ + public Integer zrank(final byte[] key, final byte[] member) { + checkIsInMulti(); + client.zrank(key, member); + return client.getIntegerReply(); + } + + /** + * Return the rank (or index) or member in the sorted set at key, with + * scores being ordered from high to low. + *

+ * When the given member does not exist in the sorted set, the special value + * 'nil' is returned. The returned rank (or index) of the member is 0-based + * for both commands. + *

+ * Time complexity: + *

+ * O(log(N)) + * + * @see #zrank(String, String) + * + * @param key + * @param member + * @return Integer reply or a nil bulk reply, specifically: the rank of the + * element as an integer reply if the element exists. A nil bulk + * reply if there is no such element. + */ + public Integer zrevrank(final byte[] key, final byte[] member) { + checkIsInMulti(); + client.zrevrank(key, member); + return client.getIntegerReply(); + } + + public Set zrevrange(final byte[] key, final int start, final int end) { + checkIsInMulti(); + client.zrevrange(key, start, end); + final List members = client.getBinaryMultiBulkReply(); + return new LinkedHashSet(members); + } + + public Set zrangeWithScores(final byte[] key, final int start, final int end) { + checkIsInMulti(); + client.zrangeWithScores(key, start, end); + Set set = getTupledSet(); + return set; + } + + public Set zrevrangeWithScores(final byte[] key, final int start, final int end) { + checkIsInMulti(); + client.zrevrangeWithScores(key, start, end); + Set set = getTupledSet(); + return set; + } + + /** + * Return the sorted set cardinality (number of elements). If the key does + * not exist 0 is returned, like for empty sorted sets. + *

+ * Time complexity O(1) + * + * @param key + * @return the cardinality (number of elements) of the set as an integer. + */ + public Integer zcard(final byte[] key) { + checkIsInMulti(); + client.zcard(key); + return client.getIntegerReply(); + } + + /** + * Return the score of the specified element of the sorted set at key. If + * the specified element does not exist in the sorted set, or the key does + * not exist at all, a special 'nil' value is returned. + *

+ * Time complexity: O(1) + * + * @param key + * @param member + * @return the score + */ + public Double zscore(final byte[] key, final byte[] member) { + checkIsInMulti(); + client.zscore(key, member); + final String score = client.getBulkReply(); + return (score != null ? new Double(score) : null); + } + + public Transaction multi() { + client.multi(); + client.getStatusCodeReply(); + return new Transaction(client); + } + + public List multi(final TransactionBlock jedisTransaction) { + List results = null; + try { + jedisTransaction.setClient(client); + multi(); + jedisTransaction.execute(); + results = jedisTransaction.exec(); + } catch (Exception ex) { + client.discard(); + } + return results; + } + + protected void checkIsInMulti() { + if (client.isInMulti()) { + throw new JedisException( + "Cannot use Jedis when in Multi. Please use JedisTransaction instead."); + } + } + + public void connect() throws UnknownHostException, IOException { + client.connect(); + } + + public void disconnect() throws IOException { + client.disconnect(); + } + + public String watch(final byte[] key) { + client.watch(key); + return client.getStatusCodeReply(); + } + + public String unwatch() { + client.unwatch(); + return client.getStatusCodeReply(); + } + + /** + * Sort a Set or a List. + *

+ * Sort the elements contained in the List, Set, or Sorted Set value at key. + * By default sorting is numeric with elements being compared as double + * precision floating point numbers. This is the simplest form of SORT. + * + * @see #sort(String, String) + * @see #sort(String, SortingParams) + * @see #sort(String, SortingParams, String) + * + * + * @param key + * @return Assuming the Set/List at key contains a list of numbers, the + * return value will be the list of numbers ordered from the + * smallest to the biggest number. + */ + public List sort(final byte[] key) { + checkIsInMulti(); + client.sort(key); + return client.getBinaryMultiBulkReply(); + } + + /** + * Sort a Set or a List accordingly to the specified parameters. + *

+ * examples: + *

+ * Given are the following sets and key/values: + * + *

+     * x = [1, 2, 3]
+     * y = [a, b, c]
+     * 
+     * k1 = z
+     * k2 = y
+     * k3 = x
+     * 
+     * w1 = 9
+     * w2 = 8
+     * w3 = 7
+     * 
+ * + * Sort Order: + * + *
+     * sort(x) or sort(x, sp.asc())
+     * -> [1, 2, 3]
+     * 
+     * sort(x, sp.desc())
+     * -> [3, 2, 1]
+     * 
+     * sort(y)
+     * -> [c, a, b]
+     * 
+     * sort(y, sp.alpha())
+     * -> [a, b, c]
+     * 
+     * sort(y, sp.alpha().desc())
+     * -> [c, a, b]
+     * 
+ * + * Limit (e.g. for Pagination): + * + *
+     * sort(x, sp.limit(0, 2))
+     * -> [1, 2]
+     * 
+     * sort(y, sp.alpha().desc().limit(1, 2))
+     * -> [b, a]
+     * 
+ * + * Sorting by external keys: + * + *
+     * sort(x, sb.by(w*))
+     * -> [3, 2, 1]
+     * 
+     * sort(x, sb.by(w*).desc())
+     * -> [1, 2, 3]
+     * 
+ * + * Getting external keys: + * + *
+     * sort(x, sp.by(w*).get(k*))
+     * -> [x, y, z]
+     * 
+     * sort(x, sp.by(w*).get(#).get(k*))
+     * -> [3, x, 2, y, 1, z]
+     * 
+ * + * @see #sort(String) + * @see #sort(String, SortingParams, String) + * + * @param key + * @param sortingParameters + * @return a list of sorted elements. + */ + public List sort(final byte[] key, final SortingParams sortingParameters) { + checkIsInMulti(); + client.sort(key, sortingParameters); + return client.getBinaryMultiBulkReply(); + } + + /** + * BLPOP (and BRPOP) is a blocking list pop primitive. You can see this + * commands as blocking versions of LPOP and RPOP able to block if the + * specified keys don't exist or contain empty lists. + *

+ * The following is a description of the exact semantic. We describe BLPOP + * but the two commands are identical, the only difference is that BLPOP + * pops the element from the left (head) of the list, and BRPOP pops from + * the right (tail). + *

+ * Non blocking behavior + *

+ * When BLPOP is called, if at least one of the specified keys contain a non + * empty list, an element is popped from the head of the list and returned + * to the caller together with the name of the key (BLPOP returns a two + * elements array, the first element is the key, the second the popped + * value). + *

+ * Keys are scanned from left to right, so for instance if you issue BLPOP + * list1 list2 list3 0 against a dataset where list1 does not exist but + * list2 and list3 contain non empty lists, BLPOP guarantees to return an + * element from the list stored at list2 (since it is the first non empty + * list starting from the left). + *

+ * Blocking behavior + *

+ * If none of the specified keys exist or contain non empty lists, BLPOP + * blocks until some other client performs a LPUSH or an RPUSH operation + * against one of the lists. + *

+ * Once new data is present on one of the lists, the client finally returns + * with the name of the key unblocking it and the popped value. + *

+ * When blocking, if a non-zero timeout is specified, the client will + * unblock returning a nil special value if the specified amount of seconds + * passed without a push operation against at least one of the specified + * keys. + *

+ * The timeout argument is interpreted as an integer value. A timeout of + * zero means instead to block forever. + *

+ * Multiple clients blocking for the same keys + *

+ * Multiple clients can block for the same key. They are put into a queue, + * so the first to be served will be the one that started to wait earlier, + * in a first-blpopping first-served fashion. + *

+ * blocking POP inside a MULTI/EXEC transaction + *

+ * BLPOP and BRPOP can be used with pipelining (sending multiple commands + * and reading the replies in batch), but it does not make sense to use + * BLPOP or BRPOP inside a MULTI/EXEC block (a Redis transaction). + *

+ * The behavior of BLPOP inside MULTI/EXEC when the list is empty is to + * return a multi-bulk nil reply, exactly what happens when the timeout is + * reached. If you like science fiction, think at it like if inside + * MULTI/EXEC the time will flow at infinite speed :) + *

+ * Time complexity: O(1) + * + * @see #brpop(int, String...) + * + * @param timeout + * @param keys + * @return BLPOP returns a two-elements array via a multi bulk reply in + * order to return both the unblocking key and the popped value. + *

+ * When a non-zero timeout is specified, and the BLPOP operation + * timed out, the return value is a nil multi bulk reply. Most + * client values will return false or nil accordingly to the + * programming language used. + */ + public List blpop(final int timeout, final byte[]... keys) { + checkIsInMulti(); + final List args = new ArrayList(); + for (final byte[] arg : keys) { + args.add(arg); + } + args.add(Protocol.toByteArray(timeout)); + + client.blpop(args.toArray(new byte[args.size()][])); + client.setTimeoutInfinite(); + final List multiBulkReply = client.getBinaryMultiBulkReply(); + client.rollbackTimeout(); + return multiBulkReply; + } + + /** + * Sort a Set or a List accordingly to the specified parameters and store + * the result at dstkey. + * + * @see #sort(String, SortingParams) + * @see #sort(String) + * @see #sort(String, String) + * + * @param key + * @param sortingParameters + * @param dstkey + * @return The number of elements of the list at dstkey. + */ + public Integer sort(final byte[] key, final SortingParams sortingParameters, + final byte[] dstkey) { + checkIsInMulti(); + client.sort(key, sortingParameters, dstkey); + return client.getIntegerReply(); + } + + /** + * Sort a Set or a List and Store the Result at dstkey. + *

+ * Sort the elements contained in the List, Set, or Sorted Set value at key + * and store the result at dstkey. By default sorting is numeric with + * elements being compared as double precision floating point numbers. This + * is the simplest form of SORT. + * + * @see #sort(String) + * @see #sort(String, SortingParams) + * @see #sort(String, SortingParams, String) + * + * @param key + * @param dstkey + * @return The number of elements of the list at dstkey. + */ + public Integer sort(final byte[] key, final byte[] dstkey) { + checkIsInMulti(); + client.sort(key, dstkey); + return client.getIntegerReply(); + } + + /** + * BLPOP (and BRPOP) is a blocking list pop primitive. You can see this + * commands as blocking versions of LPOP and RPOP able to block if the + * specified keys don't exist or contain empty lists. + *

+ * The following is a description of the exact semantic. We describe BLPOP + * but the two commands are identical, the only difference is that BLPOP + * pops the element from the left (head) of the list, and BRPOP pops from + * the right (tail). + *

+ * Non blocking behavior + *

+ * When BLPOP is called, if at least one of the specified keys contain a non + * empty list, an element is popped from the head of the list and returned + * to the caller together with the name of the key (BLPOP returns a two + * elements array, the first element is the key, the second the popped + * value). + *

+ * Keys are scanned from left to right, so for instance if you issue BLPOP + * list1 list2 list3 0 against a dataset where list1 does not exist but + * list2 and list3 contain non empty lists, BLPOP guarantees to return an + * element from the list stored at list2 (since it is the first non empty + * list starting from the left). + *

+ * Blocking behavior + *

+ * If none of the specified keys exist or contain non empty lists, BLPOP + * blocks until some other client performs a LPUSH or an RPUSH operation + * against one of the lists. + *

+ * Once new data is present on one of the lists, the client finally returns + * with the name of the key unblocking it and the popped value. + *

+ * When blocking, if a non-zero timeout is specified, the client will + * unblock returning a nil special value if the specified amount of seconds + * passed without a push operation against at least one of the specified + * keys. + *

+ * The timeout argument is interpreted as an integer value. A timeout of + * zero means instead to block forever. + *

+ * Multiple clients blocking for the same keys + *

+ * Multiple clients can block for the same key. They are put into a queue, + * so the first to be served will be the one that started to wait earlier, + * in a first-blpopping first-served fashion. + *

+ * blocking POP inside a MULTI/EXEC transaction + *

+ * BLPOP and BRPOP can be used with pipelining (sending multiple commands + * and reading the replies in batch), but it does not make sense to use + * BLPOP or BRPOP inside a MULTI/EXEC block (a Redis transaction). + *

+ * The behavior of BLPOP inside MULTI/EXEC when the list is empty is to + * return a multi-bulk nil reply, exactly what happens when the timeout is + * reached. If you like science fiction, think at it like if inside + * MULTI/EXEC the time will flow at infinite speed :) + *

+ * Time complexity: O(1) + * + * @see #blpop(int, String...) + * + * @param timeout + * @param keys + * @return BLPOP returns a two-elements array via a multi bulk reply in + * order to return both the unblocking key and the popped value. + *

+ * When a non-zero timeout is specified, and the BLPOP operation + * timed out, the return value is a nil multi bulk reply. Most + * client values will return false or nil accordingly to the + * programming language used. + */ + public List brpop(final int timeout, final byte[]... keys) { + checkIsInMulti(); + final List args = new ArrayList(); + for (final byte[] arg : keys) { + args.add(arg); + } + args.add(Protocol.toByteArray(timeout)); + + client.brpop(args.toArray(new byte[args.size()][])); + client.setTimeoutInfinite(); + final List multiBulkReply = client.getBinaryMultiBulkReply(); + client.rollbackTimeout(); + + return multiBulkReply; + } + + /** + * Request for authentication in a password protected Redis server. A Redis + * server can be instructed to require a password before to allow clients to + * issue commands. This is done using the requirepass directive in the Redis + * configuration file. If the password given by the client is correct the + * server replies with an OK status code reply and starts accepting commands + * from the client. Otherwise an error is returned and the clients needs to + * try a new password. Note that for the high performance nature of Redis it + * is possible to try a lot of passwords in parallel in very short time, so + * make sure to generate a strong and very long password so that this attack + * is infeasible. + * + * @param password + * @return Status code reply + */ + public String auth(final String password) { + checkIsInMulti(); + client.auth(password); + return client.getStatusCodeReply(); + } + + public List pipelined(final JedisPipeline jedisPipeline) { + jedisPipeline.setClient(client); + jedisPipeline.execute(); + return client.getAll(); + } + + public void subscribe(final JedisPubSub jedisPubSub, final String... channels) { + client.setTimeoutInfinite(); + jedisPubSub.proceed(client, channels); + client.rollbackTimeout(); + } + + public Integer publish(final String channel, final String message) { + client.publish(channel, message); + return client.getIntegerReply(); + } + + public void psubscribe(final JedisPubSub jedisPubSub, final String... patterns) { + client.setTimeoutInfinite(); + jedisPubSub.proceedWithPatterns(client, patterns); + client.rollbackTimeout(); + } + + public Integer zcount(final byte[] key, final double min, final double max) { + checkIsInMulti(); + client.zcount(key, min, max); + return client.getIntegerReply(); + } + + /** + * Return the all the elements in the sorted set at key with a score between + * min and max (including elements with score equal to min or max). + *

+ * The elements having the same score are returned sorted lexicographically + * as ASCII strings (this follows from a property of Redis sorted sets and + * does not involve further computation). + *

+ * Using the optional + * {@link #zrangeByScore(String, double, double, int, int) LIMIT} it's + * possible to get only a range of the matching elements in an SQL-alike + * way. Note that if offset is large the commands needs to traverse the list + * for offset elements and this adds up to the O(M) figure. + *

+ * The {@link #zcount(String, double, double) ZCOUNT} command is similar to + * {@link #zrangeByScore(String, double, double) ZRANGEBYSCORE} but instead + * of returning the actual elements in the specified interval, it just + * returns the number of matching elements. + *

+ * Exclusive intervals and infinity + *

+ * min and max can be -inf and +inf, so that you are not required to know + * what's the greatest or smallest element in order to take, for instance, + * elements "up to a given value". + *

+ * Also while the interval is for default closed (inclusive) it's possible + * to specify open intervals prefixing the score with a "(" character, so + * for instance: + *

+ * {@code ZRANGEBYSCORE zset (1.3 5} + *

+ * Will return all the values with score > 1.3 and <= 5, while for instance: + *

+ * {@code ZRANGEBYSCORE zset (5 (10} + *

+ * Will return all the values with score > 5 and < 10 (5 and 10 excluded). + *

+ * Time complexity: + *

+ * O(log(N))+O(M) with N being the number of elements in the sorted set and + * M the number of elements returned by the command, so if M is constant + * (for instance you always ask for the first ten elements with LIMIT) you + * can consider it O(log(N)) + * + * @see #zrangeByScore(String, double, double) + * @see #zrangeByScore(String, double, double, int, int) + * @see #zrangeByScoreWithScores(String, double, double) + * @see #zrangeByScoreWithScores(String, String, String) + * @see #zrangeByScoreWithScores(String, double, double, int, int) + * @see #zcount(String, double, double) + * + * @param key + * @param min + * @param max + * @return Multi bulk reply specifically a list of elements in the specified + * score range. + */ + public Set zrangeByScore(final byte[] key, final double min, final double max) { + checkIsInMulti(); + client.zrangeByScore(key, min, max); + return new LinkedHashSet(client.getBinaryMultiBulkReply()); + } + + public Set zrangeByScore(final byte[] key, final byte[] min, final byte[] max) { + checkIsInMulti(); + client.zrangeByScore(key, min, max); + return new LinkedHashSet(client.getBinaryMultiBulkReply()); + } + + /** + * Return the all the elements in the sorted set at key with a score between + * min and max (including elements with score equal to min or max). + *

+ * The elements having the same score are returned sorted lexicographically + * as ASCII strings (this follows from a property of Redis sorted sets and + * does not involve further computation). + *

+ * Using the optional + * {@link #zrangeByScore(String, double, double, int, int) LIMIT} it's + * possible to get only a range of the matching elements in an SQL-alike + * way. Note that if offset is large the commands needs to traverse the list + * for offset elements and this adds up to the O(M) figure. + *

+ * The {@link #zcount(String, double, double) ZCOUNT} command is similar to + * {@link #zrangeByScore(String, double, double) ZRANGEBYSCORE} but instead + * of returning the actual elements in the specified interval, it just + * returns the number of matching elements. + *

+ * Exclusive intervals and infinity + *

+ * min and max can be -inf and +inf, so that you are not required to know + * what's the greatest or smallest element in order to take, for instance, + * elements "up to a given value". + *

+ * Also while the interval is for default closed (inclusive) it's possible + * to specify open intervals prefixing the score with a "(" character, so + * for instance: + *

+ * {@code ZRANGEBYSCORE zset (1.3 5} + *

+ * Will return all the values with score > 1.3 and <= 5, while for instance: + *

+ * {@code ZRANGEBYSCORE zset (5 (10} + *

+ * Will return all the values with score > 5 and < 10 (5 and 10 excluded). + *

+ * Time complexity: + *

+ * O(log(N))+O(M) with N being the number of elements in the sorted set and + * M the number of elements returned by the command, so if M is constant + * (for instance you always ask for the first ten elements with LIMIT) you + * can consider it O(log(N)) + * + * @see #zrangeByScore(String, double, double) + * @see #zrangeByScore(String, double, double, int, int) + * @see #zrangeByScoreWithScores(String, double, double) + * @see #zrangeByScoreWithScores(String, double, double, int, int) + * @see #zcount(String, double, double) + * + * @param key + * @param min + * @param max + * @return Multi bulk reply specifically a list of elements in the specified + * score range. + */ + public Set zrangeByScore(final byte[] key, final double min, final double max, + final int offset, final int count) { + checkIsInMulti(); + client.zrangeByScore(key, min, max, offset, count); + return new LinkedHashSet(client.getBinaryMultiBulkReply()); + } + + /** + * Return the all the elements in the sorted set at key with a score between + * min and max (including elements with score equal to min or max). + *

+ * The elements having the same score are returned sorted lexicographically + * as ASCII strings (this follows from a property of Redis sorted sets and + * does not involve further computation). + *

+ * Using the optional + * {@link #zrangeByScore(String, double, double, int, int) LIMIT} it's + * possible to get only a range of the matching elements in an SQL-alike + * way. Note that if offset is large the commands needs to traverse the list + * for offset elements and this adds up to the O(M) figure. + *

+ * The {@link #zcount(String, double, double) ZCOUNT} command is similar to + * {@link #zrangeByScore(String, double, double) ZRANGEBYSCORE} but instead + * of returning the actual elements in the specified interval, it just + * returns the number of matching elements. + *

+ * Exclusive intervals and infinity + *

+ * min and max can be -inf and +inf, so that you are not required to know + * what's the greatest or smallest element in order to take, for instance, + * elements "up to a given value". + *

+ * Also while the interval is for default closed (inclusive) it's possible + * to specify open intervals prefixing the score with a "(" character, so + * for instance: + *

+ * {@code ZRANGEBYSCORE zset (1.3 5} + *

+ * Will return all the values with score > 1.3 and <= 5, while for instance: + *

+ * {@code ZRANGEBYSCORE zset (5 (10} + *

+ * Will return all the values with score > 5 and < 10 (5 and 10 excluded). + *

+ * Time complexity: + *

+ * O(log(N))+O(M) with N being the number of elements in the sorted set and + * M the number of elements returned by the command, so if M is constant + * (for instance you always ask for the first ten elements with LIMIT) you + * can consider it O(log(N)) + * + * @see #zrangeByScore(String, double, double) + * @see #zrangeByScore(String, double, double, int, int) + * @see #zrangeByScoreWithScores(String, double, double) + * @see #zrangeByScoreWithScores(String, double, double, int, int) + * @see #zcount(String, double, double) + * + * @param key + * @param min + * @param max + * @return Multi bulk reply specifically a list of elements in the specified + * score range. + */ + public Set zrangeByScoreWithScores(final byte[] key, final double min, final double max) { + checkIsInMulti(); + client.zrangeByScoreWithScores(key, min, max); + Set set = getTupledSet(); + return set; + } + + /** + * Return the all the elements in the sorted set at key with a score between + * min and max (including elements with score equal to min or max). + *

+ * The elements having the same score are returned sorted lexicographically + * as ASCII strings (this follows from a property of Redis sorted sets and + * does not involve further computation). + *

+ * Using the optional + * {@link #zrangeByScore(String, double, double, int, int) LIMIT} it's + * possible to get only a range of the matching elements in an SQL-alike + * way. Note that if offset is large the commands needs to traverse the list + * for offset elements and this adds up to the O(M) figure. + *

+ * The {@link #zcount(String, double, double) ZCOUNT} command is similar to + * {@link #zrangeByScore(String, double, double) ZRANGEBYSCORE} but instead + * of returning the actual elements in the specified interval, it just + * returns the number of matching elements. + *

+ * Exclusive intervals and infinity + *

+ * min and max can be -inf and +inf, so that you are not required to know + * what's the greatest or smallest element in order to take, for instance, + * elements "up to a given value". + *

+ * Also while the interval is for default closed (inclusive) it's possible + * to specify open intervals prefixing the score with a "(" character, so + * for instance: + *

+ * {@code ZRANGEBYSCORE zset (1.3 5} + *

+ * Will return all the values with score > 1.3 and <= 5, while for instance: + *

+ * {@code ZRANGEBYSCORE zset (5 (10} + *

+ * Will return all the values with score > 5 and < 10 (5 and 10 excluded). + *

+ * Time complexity: + *

+ * O(log(N))+O(M) with N being the number of elements in the sorted set and + * M the number of elements returned by the command, so if M is constant + * (for instance you always ask for the first ten elements with LIMIT) you + * can consider it O(log(N)) + * + * @see #zrangeByScore(String, double, double) + * @see #zrangeByScore(String, double, double, int, int) + * @see #zrangeByScoreWithScores(String, double, double) + * @see #zrangeByScoreWithScores(String, double, double, int, int) + * @see #zcount(String, double, double) + * + * @param key + * @param min + * @param max + * @return Multi bulk reply specifically a list of elements in the specified + * score range. + */ + public Set zrangeByScoreWithScores(final byte[] key, final double min, + final double max, final int offset, final int count) { + checkIsInMulti(); + client.zrangeByScoreWithScores(key, min, max, offset, count); + Set set = getTupledSet(); + return set; + } + + private Set getTupledSet() { + checkIsInMulti(); + List membersWithScores = client.getMultiBulkReply(); + Set set = new LinkedHashSet(); + Iterator iterator = membersWithScores.iterator(); + while (iterator.hasNext()) { + set + .add(new Tuple(iterator.next(), Double.valueOf(iterator + .next()))); + } + return set; + } + + /** + * Remove all elements in the sorted set at key with rank between start and + * end. Start and end are 0-based with rank 0 being the element with the + * lowest score. Both start and end can be negative numbers, where they + * indicate offsets starting at the element with the highest rank. For + * example: -1 is the element with the highest score, -2 the element with + * the second highest score and so forth. + *

+ * Time complexity: O(log(N))+O(M) with N being the number of + * elements in the sorted set and M the number of elements removed by the + * operation + * + */ + public Integer zremrangeByRank(final byte[] key, final int start, final int end) { + checkIsInMulti(); + client.zremrangeByRank(key, start, end); + return client.getIntegerReply(); + } + + /** + * Remove all the elements in the sorted set at key with a score between min + * and max (including elements with score equal to min or max). + *

+ * Time complexity: + *

+ * O(log(N))+O(M) with N being the number of elements in the sorted set and + * M the number of elements removed by the operation + * + * @param key + * @param start + * @param end + * @return Integer reply, specifically the number of elements removed. + */ + public Integer zremrangeByScore(final byte[] key, final double start, final double end) { + checkIsInMulti(); + client.zremrangeByScore(key, start, end); + return client.getIntegerReply(); + } + + /** + * Creates a union or intersection of N sorted sets given by keys k1 through + * kN, and stores it at dstkey. It is mandatory to provide the number of + * input keys N, before passing the input keys and the other (optional) + * arguments. + *

+ * As the terms imply, the {@link #zinterstore(String, String...) + * ZINTERSTORE} command requires an element to be present in each of the + * given inputs to be inserted in the result. The + * {@link #zunionstore(String, String...) ZUNIONSTORE} command inserts all + * elements across all inputs. + *

+ * Using the WEIGHTS option, it is possible to add weight to each input + * sorted set. This means that the score of each element in the sorted set + * is first multiplied by this weight before being passed to the + * aggregation. When this option is not given, all weights default to 1. + *

+ * With the AGGREGATE option, it's possible to specify how the results of + * the union or intersection are aggregated. This option defaults to SUM, + * where the score of an element is summed across the inputs where it + * exists. When this option is set to be either MIN or MAX, the resulting + * set will contain the minimum or maximum score of an element across the + * inputs where it exists. + *

+ * Time complexity: O(N) + O(M log(M)) with N being the sum of the + * sizes of the input sorted sets, and M being the number of elements in the + * resulting sorted set + * + * @see #zunionstore(String, String...) + * @see #zunionstore(String, ZParams, String...) + * @see #zinterstore(String, String...) + * @see #zinterstore(String, ZParams, String...) + * + * @param dstkey + * @param sets + * @return Integer reply, specifically the number of elements in the sorted + * set at dstkey + */ + public Integer zunionstore(final byte[] dstkey, final byte[]... sets) { + checkIsInMulti(); + client.zunionstore(dstkey, sets); + return client.getIntegerReply(); + } + + /** + * Creates a union or intersection of N sorted sets given by keys k1 through + * kN, and stores it at dstkey. It is mandatory to provide the number of + * input keys N, before passing the input keys and the other (optional) + * arguments. + *

+ * As the terms imply, the {@link #zinterstore(String, String...) + * ZINTERSTORE} command requires an element to be present in each of the + * given inputs to be inserted in the result. The + * {@link #zunionstore(String, String...) ZUNIONSTORE} command inserts all + * elements across all inputs. + *

+ * Using the WEIGHTS option, it is possible to add weight to each input + * sorted set. This means that the score of each element in the sorted set + * is first multiplied by this weight before being passed to the + * aggregation. When this option is not given, all weights default to 1. + *

+ * With the AGGREGATE option, it's possible to specify how the results of + * the union or intersection are aggregated. This option defaults to SUM, + * where the score of an element is summed across the inputs where it + * exists. When this option is set to be either MIN or MAX, the resulting + * set will contain the minimum or maximum score of an element across the + * inputs where it exists. + *

+ * Time complexity: O(N) + O(M log(M)) with N being the sum of the + * sizes of the input sorted sets, and M being the number of elements in the + * resulting sorted set + * + * @see #zunionstore(String, String...) + * @see #zunionstore(String, ZParams, String...) + * @see #zinterstore(String, String...) + * @see #zinterstore(String, ZParams, String...) + * + * @param dstkey + * @param sets + * @param params + * @return Integer reply, specifically the number of elements in the sorted + * set at dstkey + */ + public Integer zunionstore(final byte[] dstkey, final ZParams params, final byte[]... sets) { + checkIsInMulti(); + client.zunionstore(dstkey, params, sets); + return client.getIntegerReply(); + } + + /** + * Creates a union or intersection of N sorted sets given by keys k1 through + * kN, and stores it at dstkey. It is mandatory to provide the number of + * input keys N, before passing the input keys and the other (optional) + * arguments. + *

+ * As the terms imply, the {@link #zinterstore(String, String...) + * ZINTERSTORE} command requires an element to be present in each of the + * given inputs to be inserted in the result. The + * {@link #zunionstore(String, String...) ZUNIONSTORE} command inserts all + * elements across all inputs. + *

+ * Using the WEIGHTS option, it is possible to add weight to each input + * sorted set. This means that the score of each element in the sorted set + * is first multiplied by this weight before being passed to the + * aggregation. When this option is not given, all weights default to 1. + *

+ * With the AGGREGATE option, it's possible to specify how the results of + * the union or intersection are aggregated. This option defaults to SUM, + * where the score of an element is summed across the inputs where it + * exists. When this option is set to be either MIN or MAX, the resulting + * set will contain the minimum or maximum score of an element across the + * inputs where it exists. + *

+ * Time complexity: O(N) + O(M log(M)) with N being the sum of the + * sizes of the input sorted sets, and M being the number of elements in the + * resulting sorted set + * + * @see #zunionstore(String, String...) + * @see #zunionstore(String, ZParams, String...) + * @see #zinterstore(String, String...) + * @see #zinterstore(String, ZParams, String...) + * + * @param dstkey + * @param sets + * @return Integer reply, specifically the number of elements in the sorted + * set at dstkey + */ + public Integer zinterstore(final byte[] dstkey, final byte[]... sets) { + checkIsInMulti(); + client.zinterstore(dstkey, sets); + return client.getIntegerReply(); + } + + /** + * Creates a union or intersection of N sorted sets given by keys k1 through + * kN, and stores it at dstkey. It is mandatory to provide the number of + * input keys N, before passing the input keys and the other (optional) + * arguments. + *

+ * As the terms imply, the {@link #zinterstore(String, String...) + * ZINTERSTORE} command requires an element to be present in each of the + * given inputs to be inserted in the result. The + * {@link #zunionstore(String, String...) ZUNIONSTORE} command inserts all + * elements across all inputs. + *

+ * Using the WEIGHTS option, it is possible to add weight to each input + * sorted set. This means that the score of each element in the sorted set + * is first multiplied by this weight before being passed to the + * aggregation. When this option is not given, all weights default to 1. + *

+ * With the AGGREGATE option, it's possible to specify how the results of + * the union or intersection are aggregated. This option defaults to SUM, + * where the score of an element is summed across the inputs where it + * exists. When this option is set to be either MIN or MAX, the resulting + * set will contain the minimum or maximum score of an element across the + * inputs where it exists. + *

+ * Time complexity: O(N) + O(M log(M)) with N being the sum of the + * sizes of the input sorted sets, and M being the number of elements in the + * resulting sorted set + * + * @see #zunionstore(String, String...) + * @see #zunionstore(String, ZParams, String...) + * @see #zinterstore(String, String...) + * @see #zinterstore(String, ZParams, String...) + * + * @param dstkey + * @param sets + * @param params + * @return Integer reply, specifically the number of elements in the sorted + * set at dstkey + */ + public Integer zinterstore(final byte[] dstkey, final ZParams params, final byte[]... sets) { + checkIsInMulti(); + client.zinterstore(dstkey, params, sets); + return client.getIntegerReply(); + } + + /** + * Synchronously save the DB on disk. + *

+ * Save the whole dataset on disk (this means that all the databases are + * saved, as well as keys with an EXPIRE set (the expire is preserved). The + * server hangs while the saving is not completed, no connection is served + * in the meanwhile. An OK code is returned when the DB was fully stored in + * disk. + *

+ * The background variant of this command is {@link #bgsave() BGSAVE} that + * is able to perform the saving in the background while the server + * continues serving other clients. + *

+ * + * @return Status code reply + */ + public String save() { + client.save(); + return client.getStatusCodeReply(); + } + + /** + * Asynchronously save the DB on disk. + *

+ * Save the DB in background. The OK code is immediately returned. Redis + * forks, the parent continues to server the clients, the child saves the DB + * on disk then exit. A client my be able to check if the operation + * succeeded using the LASTSAVE command. + * + * @return Status code reply + */ + public String bgsave() { + client.bgsave(); + return client.getStatusCodeReply(); + } + + /** + * Rewrite the append only file in background when it gets too big. Please + * for detailed information about the Redis Append Only File check the Append + * Only File Howto. + *

+ * BGREWRITEAOF rewrites the Append Only File in background when it gets too + * big. The Redis Append Only File is a Journal, so every operation + * modifying the dataset is logged in the Append Only File (and replayed at + * startup). This means that the Append Only File always grows. In order to + * rebuild its content the BGREWRITEAOF creates a new version of the append + * only file starting directly form the dataset in memory in order to + * guarantee the generation of the minimal number of commands needed to + * rebuild the database. + *

+ * + * @return Status code reply + */ + public String bgrewriteaof() { + client.bgrewriteaof(); + return client.getStatusCodeReply(); + } + + /** + * Return the UNIX time stamp of the last successfully saving of the dataset + * on disk. + *

+ * Return the UNIX TIME of the last DB save executed with success. A client + * may check if a {@link #bgsave() BGSAVE} command succeeded reading the + * LASTSAVE value, then issuing a BGSAVE command and checking at regular + * intervals every N seconds if LASTSAVE changed. + * + * @return Integer reply, specifically an UNIX time stamp. + */ + public Integer lastsave() { + client.lastsave(); + return client.getIntegerReply(); + } + + /** + * Synchronously save the DB on disk, then shutdown the server. + *

+ * Stop all the clients, save the DB, then quit the server. This commands + * makes sure that the DB is switched off without the lost of any data. This + * is not guaranteed if the client uses simply {@link #save() SAVE} and then + * {@link #quit() QUIT} because other clients may alter the DB data between + * the two commands. + * + * @return Status code reply on error. On success nothing is returned since + * the server quits and the connection is closed. + */ + public String shutdown() { + client.shutdown(); + String status = null; + try { + status = client.getStatusCodeReply(); + } catch (JedisException ex) { + status = null; + } + return status; + } + + /** + * Provide information and statistics about the server. + *

+ * The info command returns different information and statistics about the + * server in an format that's simple to parse by computers and easy to read + * by humans. + *

+ * Format of the returned String: + *

+ * All the fields are in the form field:value + * + *

+     * edis_version:0.07
+     * connected_clients:1
+     * connected_slaves:0
+     * used_memory:3187
+     * changes_since_last_save:0
+     * last_save_time:1237655729
+     * total_connections_received:1
+     * total_commands_processed:1
+     * uptime_in_seconds:25
+     * uptime_in_days:0
+     * 
+ * + * Notes + *

+ * used_memory is returned in bytes, and is the total number of bytes + * allocated by the program using malloc. + *

+ * uptime_in_days is redundant since the uptime in seconds contains already + * the full uptime information, this field is only mainly present for + * humans. + *

+ * changes_since_last_save does not refer to the number of key changes, but + * to the number of operations that produced some kind of change in the + * dataset. + *

+ * + * @return Bulk reply + */ + public String info() { + client.info(); + return client.getBulkReply(); + } + + /** + * Dump all the received requests in real time. + *

+ * MONITOR is a debugging command that outputs the whole sequence of + * commands received by the Redis server. is very handy in order to + * understand what is happening into the database. This command is used + * directly via telnet. + * + * @param jedisMonitor + */ + public void monitor(final JedisMonitor jedisMonitor) { + client.monitor(); + jedisMonitor.proceed(client); + } + + /** + * Change the replication settings. + *

+ * The SLAVEOF command can change the replication settings of a slave on the + * fly. If a Redis server is arleady acting as slave, the command SLAVEOF NO + * ONE will turn off the replicaiton turning the Redis server into a MASTER. + * In the proper form SLAVEOF hostname port will make the server a slave of + * the specific server listening at the specified hostname and port. + *

+ * If a server is already a slave of some master, SLAVEOF hostname port will + * stop the replication against the old server and start the + * synchrnonization against the new one discarding the old dataset. + *

+ * The form SLAVEOF no one will stop replication turning the server into a + * MASTER but will not discard the replication. So if the old master stop + * working it is possible to turn the slave into a master and set the + * application to use the new master in read/write. Later when the other + * Redis server will be fixed it can be configured in order to work as + * slave. + *

+ * + * @param host + * @param port + * @return Status code reply + */ + public String slaveof(final String host, final int port) { + client.slaveof(host, port); + return client.getStatusCodeReply(); + } + + public String slaveofNoOne() { + client.slaveofNoOne(); + return client.getStatusCodeReply(); + } + + /** + * Retrieve the configuration of a running Redis server. Not all the + * configuration parameters are supported. + *

+ * CONFIG GET returns the current configuration parameters. This sub command + * only accepts a single argument, that is glob style pattern. All the + * configuration parameters matching this parameter are reported as a list + * of key-value pairs. + *

+ * Example: + * + *

+     * $ redis-cli config get '*'
+     * 1. "dbfilename"
+     * 2. "dump.rdb"
+     * 3. "requirepass"
+     * 4. (nil)
+     * 5. "masterauth"
+     * 6. (nil)
+     * 7. "maxmemory"
+     * 8. "0\n"
+     * 9. "appendfsync"
+     * 10. "everysec"
+     * 11. "save"
+     * 12. "3600 1 300 100 60 10000"
+     * 
+     * $ redis-cli config get 'm*'
+     * 1. "masterauth"
+     * 2. (nil)
+     * 3. "maxmemory"
+     * 4. "0\n"
+     * 
+ * + * @param pattern + * @return Bulk reply. + */ + public List configGet(final String pattern) { + client.configGet(pattern); + return client.getMultiBulkReply(); + } + + /** + * Alter the configuration of a running Redis server. Not all the + * configuration parameters are supported. + *

+ * The list of configuration parameters supported by CONFIG SET can be + * obtained issuing a {@link #configGet(String) CONFIG GET *} command. + *

+ * The configuration set using CONFIG SET is immediately loaded by the Redis + * server that will start acting as specified starting from the next + * command. + *

+ * + * Parameters value format + *

+ * The value of the configuration parameter is the same as the one of the + * same parameter in the Redis configuration file, with the following + * exceptions: + *

+ *

    + *
  • The save paramter is a list of space-separated integers. Every pair + * of integers specify the time and number of changes limit to trigger a + * save. For instance the command CONFIG SET save "3600 10 60 10000" will + * configure the server to issue a background saving of the RDB file every + * 3600 seconds if there are at least 10 changes in the dataset, and every + * 60 seconds if there are at least 10000 changes. To completely disable + * automatic snapshots just set the parameter as an empty string. + *
  • All the integer parameters representing memory are returned and + * accepted only using bytes as unit. + *
+ * + * @param parameter + * @param value + * @return Status code reply + */ + public String configSet(final String parameter, final String value) { + client.configSet(parameter, value); + return client.getStatusCodeReply(); + } + + public boolean isConnected() { + return client.isConnected(); + } + + public Integer strlen(final byte[] key) { + client.strlen(key); + return client.getIntegerReply(); + } + + public void sync() { + client.sync(); + } + + public Integer lpushx(final byte[] key, final byte[] string) { + client.lpushx(key, string); + return client.getIntegerReply(); + } + + /** + * Undo a {@link #expire(String, int) expire} at turning the expire key into + * a normal key. + *

+ * Time complexity: O(1) + * + * @param key + * @return Integer reply, specifically: 1: the key is now persist. 0: the + * key is not persist (only happens when key not set). + */ + public Integer persist(final byte[] key) { + client.persist(key); + return client.getIntegerReply(); + } + + public Integer rpushx(final byte[] key, final byte[] string) { + client.rpushx(key, string); + return client.getIntegerReply(); + } + + public String echo(final byte[] string) { + client.echo(string); + return client.getBulkReply(); + } + + public Integer linsert(final byte[] key, final LIST_POSITION where, final byte[] pivot, + final byte[] value) { + client.linsert(key, where, pivot, value); + return client.getIntegerReply(); + } + + public String debug(final DebugParams params) { + client.debug(params); + return client.getStatusCodeReply(); + } + + public Client getClient() { + return client; + } +} \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java index ffd6789..009efb1 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java +++ b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java @@ -48,9 +48,9 @@ public interface BinaryJedisCommands { Integer hsetnx(byte[] key, byte[] field, byte[] value); - String hmset(byte[] key, Map hash); + String hmset(byte[] key, Map hash); - List hmget(byte[] key, String... fields); + List hmget(byte[] key, byte[]... fields); Integer hincrBy(byte[] key, byte[] field, int value); diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index 6feaf31..22bca49 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -1,7 +1,5 @@ package redis.clients.jedis; -import java.io.IOException; -import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -10,30 +8,23 @@ import java.util.List; import java.util.Map; import java.util.Set; -import redis.clients.jedis.Client.LIST_POSITION; +import redis.clients.jedis.BinaryClient.LIST_POSITION; -public class Jedis implements JedisCommands { - private Client client = null; - - public Jedis(String host) { - client = new Client(host); +public class Jedis extends BinaryJedis implements JedisCommands { + public Jedis(final String host) { + super(host); } - public Jedis(String host, int port) { - client = new Client(host, port); + public Jedis(final String host, final int port) { + super(host, port); } - public Jedis(String host, int port, int timeout) { - client = new Client(host, port); - client.setTimeout(timeout); + public Jedis(final String host, final int port, final int timeout) { + super(host, port, timeout); } - public Jedis(JedisShardInfo shardInfo) { - client = new Client(shardInfo.getHost(), shardInfo.getPort()); - client.setTimeout(shardInfo.getTimeout()); - if (shardInfo.getPassword() != null) { - this.auth(shardInfo.getPassword()); - } + public Jedis(final JedisShardInfo shardInfo) { + super(shardInfo); } public String ping() { @@ -52,7 +43,7 @@ public class Jedis implements JedisCommands { * @param value * @return Status code reply */ - public String set(String key, String value) { + public String set(final String key, final String value) { checkIsInMulti(); client.set(key, value); return client.getStatusCodeReply(); @@ -68,20 +59,12 @@ public class Jedis implements JedisCommands { * @param key * @return Bulk reply */ - public String get(String key) { + public String get(final String key) { checkIsInMulti(); - client.sendCommand("GET", key); + client.get(key); return client.getBulkReply(); } - /** - * Ask the server to silently close the connection. - */ - public void quit() { - checkIsInMulti(); - client.quit(); - } - /** * Test if the specified key exists. The command returns "0" if the key * exists, otherwise "1" is returned. Note that even keys set with an empty @@ -92,7 +75,7 @@ public class Jedis implements JedisCommands { * @param key * @return Integer reply, "0" if the key exists, otherwise "1" */ - public Integer exists(String key) { + public Integer exists(final String key) { checkIsInMulti(); client.exists(key); return client.getIntegerReply(); @@ -108,7 +91,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically: an integer greater than 0 if one or * more keys were removed 0 if none of the specified key existed */ - public Integer del(String... keys) { + public Integer del(final String... keys) { checkIsInMulti(); client.del(keys); return client.getIntegerReply(); @@ -128,24 +111,12 @@ public class Jedis implements JedisCommands { * "zset" if the key contains a Sorted Set value "hash" if the key * contains a Hash value */ - public String type(String key) { + public String type(final String key) { checkIsInMulti(); client.type(key); return client.getStatusCodeReply(); } - /** - * Delete all the keys of the currently selected DB. This command never - * fails. - * - * @return Status code reply - */ - public String flushDB() { - checkIsInMulti(); - client.flushDB(); - return client.getStatusCodeReply(); - } - /** * Returns all the keys matching the glob-style pattern as space separated * strings. For example if you have in the database the keys "foo" and @@ -177,7 +148,7 @@ public class Jedis implements JedisCommands { * @param pattern * @return Multi bulk reply */ - public List keys(String pattern) { + public List keys(final String pattern) { checkIsInMulti(); client.keys(pattern); return client.getMultiBulkReply(); @@ -208,7 +179,7 @@ public class Jedis implements JedisCommands { * @param newkey * @return Status code repy */ - public String rename(String oldkey, String newkey) { + public String rename(final String oldkey, final String newkey) { checkIsInMulti(); client.rename(oldkey, newkey); return client.getStatusCodeReply(); @@ -225,23 +196,12 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically: 1 if the key was renamed 0 if the * target key already exist */ - public Integer renamenx(String oldkey, String newkey) { + public Integer renamenx(final String oldkey, final String newkey) { checkIsInMulti(); client.renamenx(oldkey, newkey); return client.getIntegerReply(); } - /** - * Return the number of keys in the currently selected database. - * - * @return Integer reply - */ - public Integer dbSize() { - checkIsInMulti(); - client.dbSize(); - return client.getIntegerReply(); - } - /** * Set a timeout on the specified key. After the timeout the key will be * automatically deleted by the server. A key with an associated timeout is @@ -270,7 +230,7 @@ public class Jedis implements JedisCommands { * 2.1.3 will happily update the timeout), or the key does not * exist. */ - public Integer expire(String key, int seconds) { + public Integer expire(final String key, final int seconds) { checkIsInMulti(); client.expire(key, seconds); return client.getIntegerReply(); @@ -306,7 +266,7 @@ public class Jedis implements JedisCommands { * 2.1.3 will happily update the timeout), or the key does not * exist. */ - public Integer expireAt(String key, long unixTime) { + public Integer expireAt(final String key, final long unixTime) { checkIsInMulti(); client.expireAt(key, unixTime); return client.getIntegerReply(); @@ -323,7 +283,7 @@ public class Jedis implements JedisCommands { * key that has an EXPIRE. If the Key does not exists or does not * have an associated expire, -1 is returned. */ - public Integer ttl(String key) { + public Integer ttl(final String key) { checkIsInMulti(); client.ttl(key); return client.getIntegerReply(); @@ -336,7 +296,7 @@ public class Jedis implements JedisCommands { * @param index * @return Status code reply */ - public String select(int index) { + public String select(final int index) { checkIsInMulti(); client.select(index); return client.getStatusCodeReply(); @@ -355,24 +315,12 @@ public class Jedis implements JedisCommands { * was not moved because already present on the target DB or was not * found in the current DB. */ - public Integer move(String key, int dbIndex) { + public Integer move(final String key, final int dbIndex) { checkIsInMulti(); client.move(key, dbIndex); return client.getIntegerReply(); } - /** - * Delete all the keys of all the existing databases, not just the currently - * selected one. This command never fails. - * - * @return Status code reply - */ - public String flushAll() { - checkIsInMulti(); - client.flushAll(); - return client.getStatusCodeReply(); - } - /** * GETSET is an atomic set this value and return the old value command. Set * key to the string value and return the old value stored at key. The @@ -384,7 +332,7 @@ public class Jedis implements JedisCommands { * @param value * @return Bulk reply */ - public String getSet(String key, String value) { + public String getSet(final String key, final String value) { checkIsInMulti(); client.getSet(key, value); return client.getBulkReply(); @@ -400,7 +348,7 @@ public class Jedis implements JedisCommands { * @param keys * @return Multi bulk reply */ - public List mget(String... keys) { + public List mget(final String... keys) { checkIsInMulti(); client.mget(keys); return client.getMultiBulkReply(); @@ -418,7 +366,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically: 1 if the key was set 0 if the key * was not set */ - public Integer setnx(String key, String value) { + public Integer setnx(final String key, final String value) { checkIsInMulti(); client.setnx(key, value); return client.getIntegerReply(); @@ -436,7 +384,7 @@ public class Jedis implements JedisCommands { * @param value * @return Status code reply */ - public String setex(String key, int seconds, String value) { + public String setex(final String key, final int seconds, final String value) { checkIsInMulti(); client.setex(key, seconds, value); return client.getStatusCodeReply(); @@ -462,7 +410,7 @@ public class Jedis implements JedisCommands { * @param keysvalues * @return Status code reply Basically +OK as MSET can't fail */ - public String mset(String... keysvalues) { + public String mset(final String... keysvalues) { checkIsInMulti(); client.mset(keysvalues); return client.getStatusCodeReply(); @@ -489,7 +437,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically: 1 if the all the keys were set 0 if * no key was set (at least one key already existed) */ - public Integer msetnx(String... keysvalues) { + public Integer msetnx(final String... keysvalues) { checkIsInMulti(); client.msetnx(keysvalues); return client.getIntegerReply(); @@ -517,7 +465,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, this commands will reply with the new value of key * after the increment. */ - public Integer decrBy(String key, int integer) { + public Integer decrBy(final String key, final int integer) { checkIsInMulti(); client.decrBy(key, integer); return client.getIntegerReply(); @@ -545,7 +493,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, this commands will reply with the new value of key * after the increment. */ - public Integer decr(String key) { + public Integer decr(final String key) { checkIsInMulti(); client.decr(key); return client.getIntegerReply(); @@ -573,7 +521,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, this commands will reply with the new value of key * after the increment. */ - public Integer incrBy(String key, int integer) { + public Integer incrBy(final String key, final int integer) { checkIsInMulti(); client.incrBy(key, integer); return client.getIntegerReply(); @@ -601,7 +549,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, this commands will reply with the new value of key * after the increment. */ - public Integer incr(String key) { + public Integer incr(final String key) { checkIsInMulti(); client.incr(key); return client.getIntegerReply(); @@ -623,7 +571,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically the total length of the string after * the append operation. */ - public Integer append(String key, String value) { + public Integer append(final String key, final String value) { checkIsInMulti(); client.append(key, value); return client.getIntegerReply(); @@ -647,7 +595,7 @@ public class Jedis implements JedisCommands { * @param end * @return Bulk reply */ - public String substr(String key, int start, int end) { + public String substr(final String key, final int start, final int end) { checkIsInMulti(); client.substr(key, start, end); return client.getBulkReply(); @@ -668,7 +616,7 @@ public class Jedis implements JedisCommands { * of the value, 0 is returned, otherwise if a new field is created * 1 is returned. */ - public Integer hset(String key, String field, String value) { + public Integer hset(final String key, final String field, final String value) { checkIsInMulti(); client.hset(key, field, value); return client.getIntegerReply(); @@ -687,7 +635,7 @@ public class Jedis implements JedisCommands { * @param field * @return Bulk reply */ - public String hget(String key, String field) { + public String hget(final String key, final String field) { checkIsInMulti(); client.hget(key, field); return client.getBulkReply(); @@ -704,7 +652,7 @@ public class Jedis implements JedisCommands { * @return If the field already exists, 0 is returned, otherwise if a new * field is created 1 is returned. */ - public Integer hsetnx(String key, String field, String value) { + public Integer hsetnx(final String key, final String field, final String value) { checkIsInMulti(); client.hsetnx(key, field, value); return client.getIntegerReply(); @@ -722,7 +670,7 @@ public class Jedis implements JedisCommands { * @param hash * @return Always OK because HMSET can't fail */ - public String hmset(String key, Map hash) { + public String hmset(final String key, final Map hash) { checkIsInMulti(); client.hmset(key, hash); return client.getStatusCodeReply(); @@ -741,7 +689,7 @@ public class Jedis implements JedisCommands { * @return Multi Bulk Reply specifically a list of all the values associated * with the specified fields, in the same order of the request. */ - public List hmget(String key, String... fields) { + public List hmget(final String key, final String... fields) { checkIsInMulti(); client.hmget(key, fields); return client.getMultiBulkReply(); @@ -765,7 +713,7 @@ public class Jedis implements JedisCommands { * @return Integer reply The new value at field after the increment * operation. */ - public Integer hincrBy(String key, String field, int value) { + public Integer hincrBy(final String key, final String field, final int value) { checkIsInMulti(); client.hincrBy(key, field, value); return client.getIntegerReply(); @@ -781,7 +729,7 @@ public class Jedis implements JedisCommands { * @return Return 1 if the hash stored at key contains the specified field. * Return 0 if the key is not found or the field is not present. */ - public Integer hexists(String key, String field) { + public Integer hexists(final String key, final String field) { checkIsInMulti(); client.hexists(key, field); return client.getIntegerReply(); @@ -797,7 +745,7 @@ public class Jedis implements JedisCommands { * @return If the field was present in the hash it is deleted and 1 is * returned, otherwise 0 is returned and no operation is performed. */ - public Integer hdel(String key, String field) { + public Integer hdel(final String key, final String field) { checkIsInMulti(); client.hdel(key, field); return client.getIntegerReply(); @@ -813,7 +761,7 @@ public class Jedis implements JedisCommands { * key. If the specified key does not exist, 0 is returned assuming * an empty hash. */ - public Integer hlen(String key) { + public Integer hlen(final String key) { checkIsInMulti(); client.hlen(key); return client.getIntegerReply(); @@ -827,7 +775,7 @@ public class Jedis implements JedisCommands { * @param key * @return All the fields names contained into a hash. */ - public List hkeys(String key) { + public List hkeys(final String key) { checkIsInMulti(); client.hkeys(key); return client.getMultiBulkReply(); @@ -841,7 +789,7 @@ public class Jedis implements JedisCommands { * @param key * @return All the fields values contained into a hash. */ - public List hvals(String key) { + public List hvals(final String key) { checkIsInMulti(); client.hvals(key); return client.getMultiBulkReply(); @@ -855,12 +803,12 @@ public class Jedis implements JedisCommands { * @param key * @return All the fields and values contained into a hash. */ - public Map hgetAll(String key) { + public Map hgetAll(final String key) { checkIsInMulti(); client.hgetAll(key); - List flatHash = client.getMultiBulkReply(); - Map hash = new HashMap(); - Iterator iterator = flatHash.iterator(); + final List flatHash = client.getMultiBulkReply(); + final Map hash = new HashMap(); + final Iterator iterator = flatHash.iterator(); while (iterator.hasNext()) { hash.put(iterator.next(), iterator.next()); } @@ -883,7 +831,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically, the number of elements inside the * list after the push operation. */ - public Integer rpush(String key, String string) { + public Integer rpush(final String key, final String string) { checkIsInMulti(); client.rpush(key, string); return client.getIntegerReply(); @@ -904,7 +852,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically, the number of elements inside the * list after the push operation. */ - public Integer lpush(String key, String string) { + public Integer lpush(final String key, final String string) { checkIsInMulti(); client.lpush(key, string); return client.getIntegerReply(); @@ -920,7 +868,7 @@ public class Jedis implements JedisCommands { * @param key * @return The length of the list. */ - public Integer llen(String key) { + public Integer llen(final String key) { checkIsInMulti(); client.llen(key); return client.getIntegerReply(); @@ -964,7 +912,7 @@ public class Jedis implements JedisCommands { * @return Multi bulk reply, specifically a list of elements in the * specified range. */ - public List lrange(String key, int start, int end) { + public List lrange(final String key, final int start, final int end) { checkIsInMulti(); client.lrange(key, start, end); return client.getMultiBulkReply(); @@ -1004,7 +952,7 @@ public class Jedis implements JedisCommands { * @param end * @return Status code reply */ - public String ltrim(String key, int start, int end) { + public String ltrim(final String key, final int start, final int end) { checkIsInMulti(); client.ltrim(key, start, end); return client.getStatusCodeReply(); @@ -1028,7 +976,7 @@ public class Jedis implements JedisCommands { * @param index * @return Bulk reply, specifically the requested element */ - public String lindex(String key, int index) { + public String lindex(final String key, final int index) { checkIsInMulti(); client.lindex(key, index); return client.getBulkReply(); @@ -1055,7 +1003,7 @@ public class Jedis implements JedisCommands { * @param value * @return Status code reply */ - public String lset(String key, int index, String value) { + public String lset(final String key, final int index, final String value) { checkIsInMulti(); client.lset(key, index, value); return client.getStatusCodeReply(); @@ -1080,7 +1028,7 @@ public class Jedis implements JedisCommands { * @return Integer Reply, specifically: The number of removed elements if * the operation succeeded */ - public Integer lrem(String key, int count, String value) { + public Integer lrem(final String key, final int count, final String value) { checkIsInMulti(); client.lrem(key, count, value); return client.getIntegerReply(); @@ -1099,7 +1047,7 @@ public class Jedis implements JedisCommands { * @param key * @return Bulk reply */ - public String lpop(String key) { + public String lpop(final String key) { checkIsInMulti(); client.lpop(key); return client.getBulkReply(); @@ -1118,7 +1066,7 @@ public class Jedis implements JedisCommands { * @param key * @return Bulk reply */ - public String rpop(String key) { + public String rpop(final String key) { checkIsInMulti(); client.rpop(key); return client.getBulkReply(); @@ -1142,7 +1090,7 @@ public class Jedis implements JedisCommands { * @param dstkey * @return Bulk reply */ - public String rpoplpush(String srckey, String dstkey) { + public String rpoplpush(final String srckey, final String dstkey) { checkIsInMulti(); client.rpoplpush(srckey, dstkey); return client.getBulkReply(); @@ -1161,7 +1109,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically: 1 if the new element was added 0 if * the element was already a member of the set */ - public Integer sadd(String key, String member) { + public Integer sadd(final String key, final String member) { checkIsInMulti(); client.sadd(key, member); return client.getIntegerReply(); @@ -1176,10 +1124,10 @@ public class Jedis implements JedisCommands { * @param key * @return Multi bulk reply */ - public Set smembers(String key) { + public Set smembers(final String key) { checkIsInMulti(); client.smembers(key); - List members = client.getMultiBulkReply(); + final List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } @@ -1195,7 +1143,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically: 1 if the new element was removed 0 * if the new element was not a member of the set */ - public Integer srem(String key, String member) { + public Integer srem(final String key, final String member) { checkIsInMulti(); client.srem(key, member); return client.getIntegerReply(); @@ -1213,7 +1161,7 @@ public class Jedis implements JedisCommands { * @param key * @return Bulk reply */ - public String spop(String key) { + public String spop(final String key) { checkIsInMulti(); client.spop(key); return client.getBulkReply(); @@ -1242,7 +1190,7 @@ public class Jedis implements JedisCommands { * element was not found on the first set and no operation was * performed */ - public Integer smove(String srckey, String dstkey, String member) { + public Integer smove(final String srckey, final String dstkey, final String member) { checkIsInMulti(); client.smove(srckey, dstkey, member); return client.getIntegerReply(); @@ -1256,7 +1204,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically: the cardinality (number of elements) * of the set as an integer. */ - public Integer scard(String key) { + public Integer scard(final String key) { checkIsInMulti(); client.scard(key); return client.getIntegerReply(); @@ -1274,7 +1222,7 @@ public class Jedis implements JedisCommands { * set 0 if the element is not a member of the set OR if the key * does not exist */ - public Integer sismember(String key, String member) { + public Integer sismember(final String key, final String member) { checkIsInMulti(); client.sismember(key, member); return client.getIntegerReply(); @@ -1299,10 +1247,10 @@ public class Jedis implements JedisCommands { * @param keys * @return Multi bulk reply, specifically the list of common elements. */ - public Set sinter(String... keys) { + public Set sinter(final String... keys) { checkIsInMulti(); client.sinter(keys); - List members = client.getMultiBulkReply(); + final List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } @@ -1317,7 +1265,7 @@ public class Jedis implements JedisCommands { * @param keys * @return Status code reply */ - public Integer sinterstore(String dstkey, String... keys) { + public Integer sinterstore(final String dstkey, final String... keys) { checkIsInMulti(); client.sinterstore(dstkey, keys); return client.getIntegerReply(); @@ -1339,10 +1287,10 @@ public class Jedis implements JedisCommands { * @param keys * @return Multi bulk reply, specifically the list of common elements. */ - public Set sunion(String... keys) { + public Set sunion(final String... keys) { checkIsInMulti(); client.sunion(keys); - List members = client.getMultiBulkReply(); + final List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } @@ -1358,7 +1306,7 @@ public class Jedis implements JedisCommands { * @param keys * @return Status code reply */ - public Integer sunionstore(String dstkey, String... keys) { + public Integer sunionstore(final String dstkey, final String... keys) { checkIsInMulti(); client.sunionstore(dstkey, keys); return client.getIntegerReply(); @@ -1387,10 +1335,10 @@ public class Jedis implements JedisCommands { * @return Return the members of a set resulting from the difference between * the first set provided and all the successive sets. */ - public Set sdiff(String... keys) { + public Set sdiff(final String... keys) { checkIsInMulti(); client.sdiff(keys); - List members = client.getMultiBulkReply(); + final List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } @@ -1402,7 +1350,7 @@ public class Jedis implements JedisCommands { * @param keys * @return Status code reply */ - public Integer sdiffstore(String dstkey, String... keys) { + public Integer sdiffstore(final String dstkey, final String... keys) { checkIsInMulti(); client.sdiffstore(dstkey, keys); return client.getIntegerReply(); @@ -1420,7 +1368,7 @@ public class Jedis implements JedisCommands { * @param key * @return Bulk reply */ - public String srandmember(String key) { + public String srandmember(final String key) { checkIsInMulti(); client.srandmember(key); return client.getBulkReply(); @@ -1447,16 +1395,16 @@ public class Jedis implements JedisCommands { * the element was already a member of the sorted set and the score * was updated */ - public Integer zadd(String key, double score, String member) { + public Integer zadd(final String key, final double score, final String member) { checkIsInMulti(); client.zadd(key, score, member); return client.getIntegerReply(); } - public Set zrange(String key, int start, int end) { + public Set zrange(final String key, final int start, final int end) { checkIsInMulti(); client.zrange(key, start, end); - List members = client.getMultiBulkReply(); + final List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } @@ -1475,7 +1423,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically: 1 if the new element was removed 0 * if the new element was not a member of the set */ - public Integer zrem(String key, String member) { + public Integer zrem(final String key, final String member) { checkIsInMulti(); client.zrem(key, member); return client.getIntegerReply(); @@ -1505,7 +1453,7 @@ public class Jedis implements JedisCommands { * @param member * @return The new score */ - public Double zincrby(String key, double score, String member) { + public Double zincrby(final String key, final double score, final String member) { checkIsInMulti(); client.zincrby(key, score, member); String newscore = client.getBulkReply(); @@ -1532,7 +1480,7 @@ public class Jedis implements JedisCommands { * element as an integer reply if the element exists. A nil bulk * reply if there is no such element. */ - public Integer zrank(String key, String member) { + public Integer zrank(final String key, final String member) { checkIsInMulti(); client.zrank(key, member); return client.getIntegerReply(); @@ -1558,27 +1506,27 @@ public class Jedis implements JedisCommands { * element as an integer reply if the element exists. A nil bulk * reply if there is no such element. */ - public Integer zrevrank(String key, String member) { + public Integer zrevrank(final String key, final String member) { checkIsInMulti(); client.zrevrank(key, member); return client.getIntegerReply(); } - public Set zrevrange(String key, int start, int end) { + public Set zrevrange(final String key, final int start, final int end) { checkIsInMulti(); client.zrevrange(key, start, end); - List members = client.getMultiBulkReply(); + final List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } - public Set zrangeWithScores(String key, int start, int end) { + public Set zrangeWithScores(final String key, final int start, final int end) { checkIsInMulti(); client.zrangeWithScores(key, start, end); Set set = getTupledSet(); return set; } - public Set zrevrangeWithScores(String key, int start, int end) { + public Set zrevrangeWithScores(final String key, final int start, final int end) { checkIsInMulti(); client.zrevrangeWithScores(key, start, end); Set set = getTupledSet(); @@ -1594,7 +1542,7 @@ public class Jedis implements JedisCommands { * @param key * @return the cardinality (number of elements) of the set as an integer. */ - public Integer zcard(String key) { + public Integer zcard(final String key) { checkIsInMulti(); client.zcard(key); return client.getIntegerReply(); @@ -1611,57 +1559,17 @@ public class Jedis implements JedisCommands { * @param member * @return the score */ - public Double zscore(String key, String member) { + public Double zscore(final String key, final String member) { checkIsInMulti(); client.zscore(key, member); - String score = client.getBulkReply(); + final String score = client.getBulkReply(); return (score != null ? new Double(score) : null); } - public Transaction multi() { - client.multi(); - client.getStatusCodeReply(); - return new Transaction(client); - } - - public List multi(TransactionBlock jedisTransaction) { - List results = null; - try { - jedisTransaction.setClient(client); - multi(); - jedisTransaction.execute(); - results = jedisTransaction.exec(); - } catch (Exception ex) { - client.discard(); - } - return results; - } - - private void checkIsInMulti() { - if (client.isInMulti()) { - throw new JedisException( - "Cannot use Jedis when in Multi. Please use JedisTransaction instead."); - } - } - - public void connect() throws UnknownHostException, IOException { - client.connect(); - } - - public void disconnect() throws IOException { - client.disconnect(); - } - - public String watch(String key) { + public String watch(final String key) { client.watch(key); return client.getStatusCodeReply(); } - - public String unwatch() { - client.unwatch(); - return client.getStatusCodeReply(); - } - /** * Sort a Set or a List. *

@@ -1679,7 +1587,7 @@ public class Jedis implements JedisCommands { * return value will be the list of numbers ordered from the * smallest to the biggest number. */ - public List sort(String key) { + public List sort(final String key) { checkIsInMulti(); client.sort(key); return client.getMultiBulkReply(); @@ -1761,7 +1669,7 @@ public class Jedis implements JedisCommands { * @param sortingParameters * @return a list of sorted elements. */ - public List sort(String key, SortingParams sortingParameters) { + public List sort(final String key, final SortingParams sortingParameters) { checkIsInMulti(); client.sort(key, sortingParameters); return client.getMultiBulkReply(); @@ -1839,17 +1747,17 @@ public class Jedis implements JedisCommands { * client values will return false or nil accordingly to the * programming language used. */ - public List blpop(int timeout, String... keys) { + public List blpop(final int timeout, final String... keys) { checkIsInMulti(); - List args = new ArrayList(); - for (String arg : keys) { + final List args = new ArrayList(); + for (final String arg : keys) { args.add(arg); } args.add(String.valueOf(timeout)); client.blpop(args.toArray(new String[args.size()])); client.setTimeoutInfinite(); - List multiBulkReply = client.getMultiBulkReply(); + final List multiBulkReply = client.getMultiBulkReply(); client.rollbackTimeout(); return multiBulkReply; } @@ -1867,8 +1775,8 @@ public class Jedis implements JedisCommands { * @param dstkey * @return The number of elements of the list at dstkey. */ - public Integer sort(String key, SortingParams sortingParameters, - String dstkey) { + public Integer sort(final String key, final SortingParams sortingParameters, + final String dstkey) { checkIsInMulti(); client.sort(key, sortingParameters, dstkey); return client.getIntegerReply(); @@ -1890,7 +1798,7 @@ public class Jedis implements JedisCommands { * @param dstkey * @return The number of elements of the list at dstkey. */ - public Integer sort(String key, String dstkey) { + public Integer sort(final String key, final String dstkey) { checkIsInMulti(); client.sort(key, dstkey); return client.getIntegerReply(); @@ -1968,7 +1876,7 @@ public class Jedis implements JedisCommands { * client values will return false or nil accordingly to the * programming language used. */ - public List brpop(int timeout, String... keys) { + public List brpop(final int timeout, final String... keys) { checkIsInMulti(); List args = new ArrayList(); for (String arg : keys) { @@ -1984,51 +1892,7 @@ public class Jedis implements JedisCommands { return multiBulkReply; } - /** - * Request for authentication in a password protected Redis server. A Redis - * server can be instructed to require a password before to allow clients to - * issue commands. This is done using the requirepass directive in the Redis - * configuration file. If the password given by the client is correct the - * server replies with an OK status code reply and starts accepting commands - * from the client. Otherwise an error is returned and the clients needs to - * try a new password. Note that for the high performance nature of Redis it - * is possible to try a lot of passwords in parallel in very short time, so - * make sure to generate a strong and very long password so that this attack - * is infeasible. - * - * @param password - * @return Status code reply - */ - public String auth(String password) { - checkIsInMulti(); - client.auth(password); - return client.getStatusCodeReply(); - } - - public List pipelined(JedisPipeline jedisPipeline) { - jedisPipeline.setClient(client); - jedisPipeline.execute(); - return client.getAll(); - } - - public void subscribe(JedisPubSub jedisPubSub, String... channels) { - client.setTimeoutInfinite(); - jedisPubSub.proceed(client, channels); - client.rollbackTimeout(); - } - - public Integer publish(String channel, String message) { - client.publish(channel, message); - return client.getIntegerReply(); - } - - public void psubscribe(JedisPubSub jedisPubSub, String... patterns) { - client.setTimeoutInfinite(); - jedisPubSub.proceedWithPatterns(client, patterns); - client.rollbackTimeout(); - } - - public Integer zcount(String key, double min, double max) { + public Integer zcount(final String key, final double min, final double max) { checkIsInMulti(); client.zcount(key, min, max); return client.getIntegerReply(); @@ -2091,13 +1955,13 @@ public class Jedis implements JedisCommands { * @return Multi bulk reply specifically a list of elements in the specified * score range. */ - public Set zrangeByScore(String key, double min, double max) { + public Set zrangeByScore(final String key, final double min, final double max) { checkIsInMulti(); client.zrangeByScore(key, min, max); return new LinkedHashSet(client.getMultiBulkReply()); } - public Set zrangeByScore(String key, String min, String max) { + public Set zrangeByScore(final String key, final String min, final String max) { checkIsInMulti(); client.zrangeByScore(key, min, max); return new LinkedHashSet(client.getMultiBulkReply()); @@ -2159,8 +2023,8 @@ public class Jedis implements JedisCommands { * @return Multi bulk reply specifically a list of elements in the specified * score range. */ - public Set zrangeByScore(String key, double min, double max, - int offset, int count) { + public Set zrangeByScore(final String key, final double min, final double max, + final int offset, final int count) { checkIsInMulti(); client.zrangeByScore(key, min, max, offset, count); return new LinkedHashSet(client.getMultiBulkReply()); @@ -2222,7 +2086,7 @@ public class Jedis implements JedisCommands { * @return Multi bulk reply specifically a list of elements in the specified * score range. */ - public Set zrangeByScoreWithScores(String key, double min, double max) { + public Set zrangeByScoreWithScores(final String key, final double min, final double max) { checkIsInMulti(); client.zrangeByScoreWithScores(key, min, max); Set set = getTupledSet(); @@ -2285,8 +2149,8 @@ public class Jedis implements JedisCommands { * @return Multi bulk reply specifically a list of elements in the specified * score range. */ - public Set zrangeByScoreWithScores(String key, double min, - double max, int offset, int count) { + public Set zrangeByScoreWithScores(final String key, final double min, + final double max, final int offset, final int count) { checkIsInMulti(); client.zrangeByScoreWithScores(key, min, max, offset, count); Set set = getTupledSet(); @@ -2319,7 +2183,7 @@ public class Jedis implements JedisCommands { * operation * */ - public Integer zremrangeByRank(String key, int start, int end) { + public Integer zremrangeByRank(final String key, final int start, final int end) { checkIsInMulti(); client.zremrangeByRank(key, start, end); return client.getIntegerReply(); @@ -2339,7 +2203,7 @@ public class Jedis implements JedisCommands { * @param end * @return Integer reply, specifically the number of elements removed. */ - public Integer zremrangeByScore(String key, double start, double end) { + public Integer zremrangeByScore(final String key, final double start, final double end) { checkIsInMulti(); client.zremrangeByScore(key, start, end); return client.getIntegerReply(); @@ -2383,7 +2247,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically the number of elements in the sorted * set at dstkey */ - public Integer zunionstore(String dstkey, String... sets) { + public Integer zunionstore(final String dstkey, final String... sets) { checkIsInMulti(); client.zunionstore(dstkey, sets); return client.getIntegerReply(); @@ -2428,7 +2292,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically the number of elements in the sorted * set at dstkey */ - public Integer zunionstore(String dstkey, ZParams params, String... sets) { + public Integer zunionstore(final String dstkey, final ZParams params, final String... sets) { checkIsInMulti(); client.zunionstore(dstkey, params, sets); return client.getIntegerReply(); @@ -2472,7 +2336,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically the number of elements in the sorted * set at dstkey */ - public Integer zinterstore(String dstkey, String... sets) { + public Integer zinterstore(final String dstkey, final String... sets) { checkIsInMulti(); client.zinterstore(dstkey, sets); return client.getIntegerReply(); @@ -2517,299 +2381,17 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically the number of elements in the sorted * set at dstkey */ - public Integer zinterstore(String dstkey, ZParams params, String... sets) { + public Integer zinterstore(final String dstkey, final ZParams params, final String... sets) { checkIsInMulti(); client.zinterstore(dstkey, params, sets); return client.getIntegerReply(); } - /** - * Synchronously save the DB on disk. - *

- * Save the whole dataset on disk (this means that all the databases are - * saved, as well as keys with an EXPIRE set (the expire is preserved). The - * server hangs while the saving is not completed, no connection is served - * in the meanwhile. An OK code is returned when the DB was fully stored in - * disk. - *

- * The background variant of this command is {@link #bgsave() BGSAVE} that - * is able to perform the saving in the background while the server - * continues serving other clients. - *

- * - * @return Status code reply - */ - public String save() { - client.save(); - return client.getStatusCodeReply(); - } - - /** - * Asynchronously save the DB on disk. - *

- * Save the DB in background. The OK code is immediately returned. Redis - * forks, the parent continues to server the clients, the child saves the DB - * on disk then exit. A client my be able to check if the operation - * succeeded using the LASTSAVE command. - * - * @return Status code reply - */ - public String bgsave() { - client.bgsave(); - return client.getStatusCodeReply(); - } - - /** - * Rewrite the append only file in background when it gets too big. Please - * for detailed information about the Redis Append Only File check the Append - * Only File Howto. - *

- * BGREWRITEAOF rewrites the Append Only File in background when it gets too - * big. The Redis Append Only File is a Journal, so every operation - * modifying the dataset is logged in the Append Only File (and replayed at - * startup). This means that the Append Only File always grows. In order to - * rebuild its content the BGREWRITEAOF creates a new version of the append - * only file starting directly form the dataset in memory in order to - * guarantee the generation of the minimal number of commands needed to - * rebuild the database. - *

- * - * @return Status code reply - */ - public String bgrewriteaof() { - client.bgrewriteaof(); - return client.getStatusCodeReply(); - } - - /** - * Return the UNIX time stamp of the last successfully saving of the dataset - * on disk. - *

- * Return the UNIX TIME of the last DB save executed with success. A client - * may check if a {@link #bgsave() BGSAVE} command succeeded reading the - * LASTSAVE value, then issuing a BGSAVE command and checking at regular - * intervals every N seconds if LASTSAVE changed. - * - * @return Integer reply, specifically an UNIX time stamp. - */ - public Integer lastsave() { - client.lastsave(); - return client.getIntegerReply(); - } - - /** - * Synchronously save the DB on disk, then shutdown the server. - *

- * Stop all the clients, save the DB, then quit the server. This commands - * makes sure that the DB is switched off without the lost of any data. This - * is not guaranteed if the client uses simply {@link #save() SAVE} and then - * {@link #quit() QUIT} because other clients may alter the DB data between - * the two commands. - * - * @return Status code reply on error. On success nothing is returned since - * the server quits and the connection is closed. - */ - public String shutdown() { - client.shutdown(); - String status = null; - try { - status = client.getStatusCodeReply(); - } catch (JedisException ex) { - status = null; - } - return status; - } - - /** - * Provide information and statistics about the server. - *

- * The info command returns different information and statistics about the - * server in an format that's simple to parse by computers and easy to read - * by humans. - *

- * Format of the returned String: - *

- * All the fields are in the form field:value - * - *

-     * edis_version:0.07
-     * connected_clients:1
-     * connected_slaves:0
-     * used_memory:3187
-     * changes_since_last_save:0
-     * last_save_time:1237655729
-     * total_connections_received:1
-     * total_commands_processed:1
-     * uptime_in_seconds:25
-     * uptime_in_days:0
-     * 
- * - * Notes - *

- * used_memory is returned in bytes, and is the total number of bytes - * allocated by the program using malloc. - *

- * uptime_in_days is redundant since the uptime in seconds contains already - * the full uptime information, this field is only mainly present for - * humans. - *

- * changes_since_last_save does not refer to the number of key changes, but - * to the number of operations that produced some kind of change in the - * dataset. - *

- * - * @return Bulk reply - */ - public String info() { - client.info(); - return client.getBulkReply(); - } - - /** - * Dump all the received requests in real time. - *

- * MONITOR is a debugging command that outputs the whole sequence of - * commands received by the Redis server. is very handy in order to - * understand what is happening into the database. This command is used - * directly via telnet. - * - * @param jedisMonitor - */ - public void monitor(JedisMonitor jedisMonitor) { - client.monitor(); - jedisMonitor.proceed(client); - } - - /** - * Change the replication settings. - *

- * The SLAVEOF command can change the replication settings of a slave on the - * fly. If a Redis server is arleady acting as slave, the command SLAVEOF NO - * ONE will turn off the replicaiton turning the Redis server into a MASTER. - * In the proper form SLAVEOF hostname port will make the server a slave of - * the specific server listening at the specified hostname and port. - *

- * If a server is already a slave of some master, SLAVEOF hostname port will - * stop the replication against the old server and start the - * synchrnonization against the new one discarding the old dataset. - *

- * The form SLAVEOF no one will stop replication turning the server into a - * MASTER but will not discard the replication. So if the old master stop - * working it is possible to turn the slave into a master and set the - * application to use the new master in read/write. Later when the other - * Redis server will be fixed it can be configured in order to work as - * slave. - *

- * - * @param host - * @param port - * @return Status code reply - */ - public String slaveof(String host, int port) { - client.slaveof(host, port); - return client.getStatusCodeReply(); - } - - public String slaveofNoOne() { - client.slaveofNoOne(); - return client.getStatusCodeReply(); - } - - /** - * Retrieve the configuration of a running Redis server. Not all the - * configuration parameters are supported. - *

- * CONFIG GET returns the current configuration parameters. This sub command - * only accepts a single argument, that is glob style pattern. All the - * configuration parameters matching this parameter are reported as a list - * of key-value pairs. - *

- * Example: - * - *

-     * $ redis-cli config get '*'
-     * 1. "dbfilename"
-     * 2. "dump.rdb"
-     * 3. "requirepass"
-     * 4. (nil)
-     * 5. "masterauth"
-     * 6. (nil)
-     * 7. "maxmemory"
-     * 8. "0\n"
-     * 9. "appendfsync"
-     * 10. "everysec"
-     * 11. "save"
-     * 12. "3600 1 300 100 60 10000"
-     * 
-     * $ redis-cli config get 'm*'
-     * 1. "masterauth"
-     * 2. (nil)
-     * 3. "maxmemory"
-     * 4. "0\n"
-     * 
- * - * @param pattern - * @return Bulk reply. - */ - public List configGet(String pattern) { - client.configGet(pattern); - return client.getMultiBulkReply(); - } - - /** - * Alter the configuration of a running Redis server. Not all the - * configuration parameters are supported. - *

- * The list of configuration parameters supported by CONFIG SET can be - * obtained issuing a {@link #configGet(String) CONFIG GET *} command. - *

- * The configuration set using CONFIG SET is immediately loaded by the Redis - * server that will start acting as specified starting from the next - * command. - *

- * - * Parameters value format - *

- * The value of the configuration parameter is the same as the one of the - * same parameter in the Redis configuration file, with the following - * exceptions: - *

- *

    - *
  • The save paramter is a list of space-separated integers. Every pair - * of integers specify the time and number of changes limit to trigger a - * save. For instance the command CONFIG SET save "3600 10 60 10000" will - * configure the server to issue a background saving of the RDB file every - * 3600 seconds if there are at least 10 changes in the dataset, and every - * 60 seconds if there are at least 10000 changes. To completely disable - * automatic snapshots just set the parameter as an empty string. - *
  • All the integer parameters representing memory are returned and - * accepted only using bytes as unit. - *
- * - * @param parameter - * @param value - * @return Status code reply - */ - public String configSet(String parameter, String value) { - client.configSet(parameter, value); - return client.getStatusCodeReply(); - } - - public boolean isConnected() { - return client.isConnected(); - } - - public Integer strlen(String key) { + public Integer strlen(final String key) { client.strlen(key); return client.getIntegerReply(); } - - public void sync() { - client.sync(); - } - - public Integer lpushx(String key, String string) { + public Integer lpushx(final String key, final String string) { client.lpushx(key, string); return client.getIntegerReply(); } @@ -2824,33 +2406,24 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically: 1: the key is now persist. 0: the * key is not persist (only happens when key not set). */ - public Integer persist(String key) { + public Integer persist(final String key) { client.persist(key); return client.getIntegerReply(); } - public Integer rpushx(String key, String string) { + public Integer rpushx(final String key, final String string) { client.rpushx(key, string); return client.getIntegerReply(); } - public String echo(String string) { + public String echo(final String string) { client.echo(string); return client.getBulkReply(); } - public Integer linsert(String key, LIST_POSITION where, String pivot, - String value) { + public Integer linsert(final String key, final LIST_POSITION where, final String pivot, + final String value) { client.linsert(key, where, pivot, value); return client.getIntegerReply(); } - - public String debug(DebugParams params) { - client.debug(params); - return client.getStatusCodeReply(); - } - - public Client getClient() { - return client; - } } \ No newline at end of file From 84bb16dd5e51b6f4d1e4156f05a74027cf813deb Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 15:08:50 +0100 Subject: [PATCH 13/40] BinaryTransactiob is born, Transaction inherits from it --- .../clients/jedis/BinaryTransaction.java | 430 ++++++++++ .../java/redis/clients/jedis/Transaction.java | 807 +++++++++--------- 2 files changed, 817 insertions(+), 420 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/BinaryTransaction.java diff --git a/src/main/java/redis/clients/jedis/BinaryTransaction.java b/src/main/java/redis/clients/jedis/BinaryTransaction.java new file mode 100644 index 0000000..5ce4f88 --- /dev/null +++ b/src/main/java/redis/clients/jedis/BinaryTransaction.java @@ -0,0 +1,430 @@ +package redis.clients.jedis; + +import java.util.List; +import java.util.Map; + +public class BinaryTransaction { + protected Client client = null; + + public BinaryTransaction() { + } + + public BinaryTransaction(final Client client) { + this.client = client; + } + + public String ping() { + client.ping(); + return client.getStatusCodeReply(); + } + + public String set(final byte[] key, final byte[] value) { + client.set(key, value); + return client.getStatusCodeReply(); + } + + public String get(final byte[] key) { + client.get(key); + return client.getStatusCodeReply(); + } + + public String exists(final byte[] key) { + client.exists(key); + return client.getStatusCodeReply(); + } + + public String del(final byte[]... keys) { + client.del(keys); + return client.getStatusCodeReply(); + } + + public String type(final byte[] key) { + client.type(key); + return client.getStatusCodeReply(); + } + + public String flushDB() { + client.flushDB(); + return client.getStatusCodeReply(); + } + + public String keys(final byte[] pattern) { + client.keys(pattern); + return client.getStatusCodeReply(); + } + + public byte[] randomBinaryKey() { + client.randomKey(); + return client.getBinaryBulkReply(); + } + + public String rename(final byte[] oldkey, final byte[] newkey) { + client.rename(oldkey, newkey); + return client.getStatusCodeReply(); + } + + public String renamenx(final byte[] oldkey, final byte[] newkey) { + client.renamenx(oldkey, newkey); + return client.getStatusCodeReply(); + } + + public String dbSize() { + client.dbSize(); + return client.getStatusCodeReply(); + } + + public String expire(final byte[] key, final int seconds) { + client.expire(key, seconds); + return client.getStatusCodeReply(); + } + + public String expireAt(final byte[] key, final long unixTime) { + client.expireAt(key, unixTime); + return client.getStatusCodeReply(); + } + + public String ttl(final byte[] key) { + client.ttl(key); + return client.getStatusCodeReply(); + } + + public String select(final int index) { + client.select(index); + return client.getStatusCodeReply(); + } + + public String move(final byte[] key, final int dbIndex) { + client.move(key, dbIndex); + return client.getStatusCodeReply(); + } + + public String flushAll() { + client.flushAll(); + return client.getStatusCodeReply(); + } + + public String getSet(final byte[] key, final byte[] value) { + client.getSet(key, value); + return client.getStatusCodeReply(); + } + + public String mget(final byte[]... keys) { + client.mget(keys); + return client.getStatusCodeReply(); + } + + public String setnx(final byte[] key, final byte[] value) { + client.setnx(key, value); + return client.getStatusCodeReply(); + } + + public String setex(final byte[] key, final int seconds, final byte[] value) { + client.setex(key, seconds, value); + return client.getStatusCodeReply(); + } + + public String mset(final byte[]... keysvalues) { + client.mset(keysvalues); + return client.getStatusCodeReply(); + } + + public String msetnx(final byte[]... keysvalues) { + client.msetnx(keysvalues); + return client.getStatusCodeReply(); + } + + public String decrBy(final byte[] key, final int integer) { + client.decrBy(key, integer); + return client.getStatusCodeReply(); + } + + public String decr(final byte[] key) { + client.decr(key); + return client.getStatusCodeReply(); + } + + public String incrBy(final byte[] key, final int integer) { + client.incrBy(key, integer); + return client.getStatusCodeReply(); + } + + public String incr(final byte[] key) { + client.incr(key); + return client.getStatusCodeReply(); + } + + public String append(final byte[] key, final byte[] value) { + client.append(key, value); + return client.getStatusCodeReply(); + } + + public String substr(final byte[] key, final int start, final int end) { + client.substr(key, start, end); + return client.getStatusCodeReply(); + } + + public String hset(final byte[] key, final byte[] field, final byte[] value) { + client.hset(key, field, value); + return client.getStatusCodeReply(); + } + + public String hget(final byte[] key, final byte[] field) { + client.hget(key, field); + return client.getStatusCodeReply(); + } + + public String hsetnx(final byte[] key, final byte[] field, + final byte[] value) { + client.hsetnx(key, field, value); + return client.getStatusCodeReply(); + } + + public String hmset(final byte[] key, final Map hash) { + client.hmset(key, hash); + return client.getStatusCodeReply(); + } + + public String hmget(final byte[] key, final byte[]... fields) { + client.hmget(key, fields); + return client.getStatusCodeReply(); + } + + public String hincrBy(final byte[] key, final byte[] field, final int value) { + client.hincrBy(key, field, value); + return client.getStatusCodeReply(); + } + + public String hexists(final byte[] key, final byte[] field) { + client.hexists(key, field); + return client.getStatusCodeReply(); + } + + public String hdel(final byte[] key, final byte[] field) { + client.hdel(key, field); + return client.getStatusCodeReply(); + } + + public String hlen(final byte[] key) { + client.hlen(key); + return client.getStatusCodeReply(); + } + + public String hkeys(final byte[] key) { + client.hkeys(key); + return client.getStatusCodeReply(); + } + + public String hvals(final byte[] key) { + client.hvals(key); + return client.getStatusCodeReply(); + } + + public String hgetAll(final byte[] key) { + client.hgetAll(key); + return client.getStatusCodeReply(); + } + + public String rpush(final byte[] key, final byte[] string) { + client.rpush(key, string); + return client.getStatusCodeReply(); + } + + public String lpush(final byte[] key, final byte[] string) { + client.lpush(key, string); + return client.getStatusCodeReply(); + } + + public String llen(final byte[] key) { + client.llen(key); + return client.getStatusCodeReply(); + } + + public String lrange(final byte[] key, final int start, final int end) { + client.lrange(key, start, end); + return client.getStatusCodeReply(); + } + + public String ltrim(final byte[] key, final int start, final int end) { + client.ltrim(key, start, end); + return client.getStatusCodeReply(); + } + + public String lindex(final byte[] key, final int index) { + client.lindex(key, index); + return client.getStatusCodeReply(); + } + + public String lset(final byte[] key, final int index, final byte[] value) { + client.lset(key, index, value); + return client.getStatusCodeReply(); + } + + public String lrem(final byte[] key, final int count, final byte[] value) { + client.lrem(key, count, value); + return client.getStatusCodeReply(); + } + + public String lpop(final byte[] key) { + client.lpop(key); + return client.getStatusCodeReply(); + } + + public String rpop(final byte[] key) { + client.rpop(key); + return client.getStatusCodeReply(); + } + + public String rpoplpush(final byte[] srckey, final byte[] dstkey) { + client.rpoplpush(srckey, dstkey); + return client.getStatusCodeReply(); + } + + public String sadd(final byte[] key, final byte[] member) { + client.sadd(key, member); + return client.getStatusCodeReply(); + } + + public String smembers(final byte[] key) { + client.smembers(key); + return client.getStatusCodeReply(); + } + + public String srem(final byte[] key, final byte[] member) { + client.srem(key, member); + return client.getStatusCodeReply(); + } + + public String spop(final byte[] key) { + client.spop(key); + return client.getStatusCodeReply(); + } + + public String smove(final byte[] srckey, final byte[] dstkey, + final byte[] member) { + client.smove(srckey, dstkey, member); + return client.getStatusCodeReply(); + } + + public String scard(final byte[] key) { + client.scard(key); + return client.getStatusCodeReply(); + } + + public String sismember(final byte[] key, final byte[] member) { + client.sismember(key, member); + return client.getStatusCodeReply(); + } + + public String sinter(final byte[]... keys) { + client.sinter(keys); + return client.getStatusCodeReply(); + } + + public String sinterstore(final byte[] dstkey, final byte[]... keys) { + client.sinterstore(dstkey, keys); + return client.getStatusCodeReply(); + } + + public String sunion(final byte[]... keys) { + client.sunion(keys); + return client.getStatusCodeReply(); + } + + public String sunionstore(final byte[] dstkey, final byte[]... keys) { + client.sunionstore(dstkey, keys); + return client.getStatusCodeReply(); + } + + public String sdiff(final byte[]... keys) { + client.sdiff(keys); + return client.getStatusCodeReply(); + } + + public String sdiffstore(final byte[] dstkey, final byte[]... keys) { + client.sdiffstore(dstkey, keys); + return client.getStatusCodeReply(); + } + + public String srandmember(final byte[] key) { + client.srandmember(key); + return client.getStatusCodeReply(); + } + + public String zadd(final byte[] key, final double score, final byte[] member) { + client.zadd(key, score, member); + return client.getStatusCodeReply(); + } + + public String zrange(final byte[] key, final int start, final int end) { + client.zrange(key, start, end); + return client.getStatusCodeReply(); + } + + public String zrem(final byte[] key, final byte[] member) { + client.zrem(key, member); + return client.getStatusCodeReply(); + } + + public String zincrby(final byte[] key, final double score, + final byte[] member) { + client.zincrby(key, score, member); + return client.getStatusCodeReply(); + } + + public String zrank(final byte[] key, final byte[] member) { + client.zrank(key, member); + return client.getStatusCodeReply(); + } + + public String zrevrank(final byte[] key, final byte[] member) { + client.zrevrank(key, member); + return client.getStatusCodeReply(); + } + + public String zrevrange(final byte[] key, final int start, final int end) { + client.zrevrange(key, start, end); + return client.getStatusCodeReply(); + } + + public String zrangeWithScores(final byte[] key, final int start, + final int end) { + client.zrangeWithScores(key, start, end); + return client.getStatusCodeReply(); + } + + public String zrevrangeWithScores(final byte[] key, final int start, + final int end) { + client.zrevrangeWithScores(key, start, end); + return client.getStatusCodeReply(); + } + + public String zcard(final byte[] key) { + client.zcard(key); + return client.getStatusCodeReply(); + } + + public String zscore(final byte[] key, final byte[] member) { + client.zscore(key, member); + return client.getStatusCodeReply(); + } + + public List exec() { + client.exec(); + + return client.getObjectMultiBulkReply(); + } + + public String sort(final byte[] key) { + client.sort(key); + return client.getStatusCodeReply(); + } + + public String sort(final byte[] key, final SortingParams sortingParameters) { + client.sort(key, sortingParameters); + return client.getStatusCodeReply(); + } + + public void discard() { + client.discard(); + } +} \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Transaction.java b/src/main/java/redis/clients/jedis/Transaction.java index 51a7fe3..f0ddbe1 100644 --- a/src/main/java/redis/clients/jedis/Transaction.java +++ b/src/main/java/redis/clients/jedis/Transaction.java @@ -1,425 +1,392 @@ package redis.clients.jedis; -import java.util.List; import java.util.Map; -public class Transaction { - protected Client client = null; - - public Transaction() { - } - - public Transaction(Client client) { - this.client = client; - } - - public String ping() { - client.ping(); - return client.getStatusCodeReply(); - } - - public String set(String key, String value) { - client.set(key, value); - return client.getStatusCodeReply(); - } - - public String get(String key) { - client.sendCommand("GET", key); - return client.getStatusCodeReply(); - } - - public String exists(String key) { - client.exists(key); - return client.getStatusCodeReply(); - } - - public String del(String... keys) { - client.del(keys); - return client.getStatusCodeReply(); - } - - public String type(String key) { - client.type(key); - return client.getStatusCodeReply(); - } - - public String flushDB() { - client.flushDB(); - return client.getStatusCodeReply(); - } - - public String keys(String pattern) { - client.keys(pattern); - return client.getStatusCodeReply(); - } - - public String randomKey() { - client.randomKey(); - return client.getStatusCodeReply(); - } - - public String rename(String oldkey, String newkey) { - client.rename(oldkey, newkey); - return client.getStatusCodeReply(); - } - - public String renamenx(String oldkey, String newkey) { - client.renamenx(oldkey, newkey); - return client.getStatusCodeReply(); - } - - public String dbSize() { - client.dbSize(); - return client.getStatusCodeReply(); - } - - public String expire(String key, int seconds) { - client.expire(key, seconds); - return client.getStatusCodeReply(); - } - - public String expireAt(String key, long unixTime) { - client.expireAt(key, unixTime); - return client.getStatusCodeReply(); - } - - public String ttl(String key) { - client.ttl(key); - return client.getStatusCodeReply(); - } - - public String select(int index) { - client.select(index); - return client.getStatusCodeReply(); - } - - public String move(String key, int dbIndex) { - client.move(key, dbIndex); - return client.getStatusCodeReply(); - } - - public String flushAll() { - client.flushAll(); - return client.getStatusCodeReply(); - } - - public String getSet(String key, String value) { - client.getSet(key, value); - return client.getStatusCodeReply(); - } - - public String mget(String... keys) { - client.mget(keys); - return client.getStatusCodeReply(); - } - - public String setnx(String key, String value) { - client.setnx(key, value); - return client.getStatusCodeReply(); - } - - public String setex(String key, int seconds, String value) { - client.setex(key, seconds, value); - return client.getStatusCodeReply(); - } - - public String mset(String... keysvalues) { - client.mset(keysvalues); - return client.getStatusCodeReply(); - } - - public String msetnx(String... keysvalues) { - client.msetnx(keysvalues); - return client.getStatusCodeReply(); - } - - public String decrBy(String key, int integer) { - client.decrBy(key, integer); - return client.getStatusCodeReply(); - } - - public String decr(String key) { - client.decr(key); - return client.getStatusCodeReply(); - } - - public String incrBy(String key, int integer) { - client.incrBy(key, integer); - return client.getStatusCodeReply(); - } - - public String incr(String key) { - client.incr(key); - return client.getStatusCodeReply(); - } - - public String append(String key, String value) { - client.append(key, value); - return client.getStatusCodeReply(); - } - - public String substr(String key, int start, int end) { - client.substr(key, start, end); - return client.getStatusCodeReply(); - } - - public String hset(String key, String field, String value) { - client.hset(key, field, value); - return client.getStatusCodeReply(); - } - - public String hget(String key, String field) { - client.hget(key, field); - return client.getStatusCodeReply(); - } - - public String hsetnx(String key, String field, String value) { - client.hsetnx(key, field, value); - return client.getStatusCodeReply(); - } - - public String hmset(String key, Map hash) { - client.hmset(key, hash); - return client.getStatusCodeReply(); - } - - public String hmget(String key, String... fields) { - client.hmget(key, fields); - return client.getStatusCodeReply(); - } - - public String hincrBy(String key, String field, int value) { - client.hincrBy(key, field, value); - return client.getStatusCodeReply(); - } - - public String hexists(String key, String field) { - client.hexists(key, field); - return client.getStatusCodeReply(); - } - - public String hdel(String key, String field) { - client.hdel(key, field); - return client.getStatusCodeReply(); - } - - public String hlen(String key) { - client.hlen(key); - return client.getStatusCodeReply(); - } - - public String hkeys(String key) { - client.hkeys(key); - return client.getStatusCodeReply(); - } - - public String hvals(String key) { - client.hvals(key); - return client.getStatusCodeReply(); - } - - public String hgetAll(String key) { - client.hgetAll(key); - return client.getStatusCodeReply(); - } - - public String rpush(String key, String string) { - client.rpush(key, string); - return client.getStatusCodeReply(); - } - - public String lpush(String key, String string) { - client.lpush(key, string); - return client.getStatusCodeReply(); - } - - public String llen(String key) { - client.llen(key); - return client.getStatusCodeReply(); - } - - public String lrange(String key, int start, int end) { - client.lrange(key, start, end); - return client.getStatusCodeReply(); - } - - public String ltrim(String key, int start, int end) { - client.ltrim(key, start, end); - return client.getStatusCodeReply(); - } - - public String lindex(String key, int index) { - client.lindex(key, index); - return client.getStatusCodeReply(); - } - - public String lset(String key, int index, String value) { - client.lset(key, index, value); - return client.getStatusCodeReply(); - } - - public String lrem(String key, int count, String value) { - client.lrem(key, count, value); - return client.getStatusCodeReply(); - } - - public String lpop(String key) { - client.lpop(key); - return client.getStatusCodeReply(); - } - - public String rpop(String key) { - client.rpop(key); - return client.getStatusCodeReply(); - } - - public String rpoplpush(String srckey, String dstkey) { - client.rpoplpush(srckey, dstkey); - return client.getStatusCodeReply(); - } - - public String sadd(String key, String member) { - client.sadd(key, member); - return client.getStatusCodeReply(); - } - - public String smembers(String key) { - client.smembers(key); - return client.getStatusCodeReply(); - } - - public String srem(String key, String member) { - client.srem(key, member); - return client.getStatusCodeReply(); - } - - public String spop(String key) { - client.spop(key); - return client.getStatusCodeReply(); - } - - public String smove(String srckey, String dstkey, String member) { - client.smove(srckey, dstkey, member); - return client.getStatusCodeReply(); - } - - public String scard(String key) { - client.scard(key); - return client.getStatusCodeReply(); - } - - public String sismember(String key, String member) { - client.sismember(key, member); - return client.getStatusCodeReply(); - } - - public String sinter(String... keys) { - client.sinter(keys); - return client.getStatusCodeReply(); - } - - public String sinterstore(String dstkey, String... keys) { - client.sinterstore(dstkey, keys); - return client.getStatusCodeReply(); - } - - public String sunion(String... keys) { - client.sunion(keys); - return client.getStatusCodeReply(); - } - - public String sunionstore(String dstkey, String... keys) { - client.sunionstore(dstkey, keys); - return client.getStatusCodeReply(); - } - - public String sdiff(String... keys) { - client.sdiff(keys); - return client.getStatusCodeReply(); - } - - public String sdiffstore(String dstkey, String... keys) { - client.sdiffstore(dstkey, keys); - return client.getStatusCodeReply(); - } - - public String srandmember(String key) { - client.srandmember(key); - return client.getStatusCodeReply(); - } - - public String zadd(String key, double score, String member) { - client.zadd(key, score, member); - return client.getStatusCodeReply(); - } - - public String zrange(String key, int start, int end) { - client.zrange(key, start, end); - return client.getStatusCodeReply(); - } - - public String zrem(String key, String member) { - client.zrem(key, member); - return client.getStatusCodeReply(); - } - - public String zincrby(String key, double score, String member) { - client.zincrby(key, score, member); - return client.getStatusCodeReply(); - } - - public String zrank(String key, String member) { - client.zrank(key, member); - return client.getStatusCodeReply(); - } - - public String zrevrank(String key, String member) { - client.zrevrank(key, member); - return client.getStatusCodeReply(); - } - - public String zrevrange(String key, int start, int end) { - client.zrevrange(key, start, end); - return client.getStatusCodeReply(); - } - - public String zrangeWithScores(String key, int start, int end) { - client.zrangeWithScores(key, start, end); - return client.getStatusCodeReply(); - } - - public String zrevrangeWithScores(String key, int start, int end) { - client.zrevrangeWithScores(key, start, end); - return client.getStatusCodeReply(); - } - - public String zcard(String key) { - client.zcard(key); - return client.getStatusCodeReply(); - } - - public String zscore(String key, String member) { - client.zscore(key, member); - return client.getStatusCodeReply(); - } - - public List exec() { - client.exec(); - - return client.getObjectMultiBulkReply(); - } - - public String sort(String key) { - client.sort(key); - return client.getStatusCodeReply(); - } - - public String sort(String key, SortingParams sortingParameters) { - client.sort(key, sortingParameters); - return client.getStatusCodeReply(); - } - - public void discard() { - client.discard(); - } +public class Transaction extends BinaryTransaction { + public Transaction() { + } + + public Transaction(final Client client) { + super(client); + } + + public String set(final String key, final String value) { + client.set(key, value); + return client.getStatusCodeReply(); + } + + public String get(final String key) { + client.get(key); + return client.getStatusCodeReply(); + } + + public String exists(final String key) { + client.exists(key); + return client.getStatusCodeReply(); + } + + public String del(final String... keys) { + client.del(keys); + return client.getStatusCodeReply(); + } + + public String type(final String key) { + client.type(key); + return client.getStatusCodeReply(); + } + + public String keys(final String pattern) { + client.keys(pattern); + return client.getStatusCodeReply(); + } + + public String randomKey() { + client.randomKey(); + return client.getBulkReply(); + } + + public String rename(final String oldkey, final String newkey) { + client.rename(oldkey, newkey); + return client.getStatusCodeReply(); + } + + public String renamenx(final String oldkey, final String newkey) { + client.renamenx(oldkey, newkey); + return client.getStatusCodeReply(); + } + + public String expire(final String key, final int seconds) { + client.expire(key, seconds); + return client.getStatusCodeReply(); + } + + public String expireAt(final String key, final long unixTime) { + client.expireAt(key, unixTime); + return client.getStatusCodeReply(); + } + + public String ttl(final String key) { + client.ttl(key); + return client.getStatusCodeReply(); + } + + public String move(final String key, final int dbIndex) { + client.move(key, dbIndex); + return client.getStatusCodeReply(); + } + + public String getSet(final String key, final String value) { + client.getSet(key, value); + return client.getStatusCodeReply(); + } + + public String mget(final String... keys) { + client.mget(keys); + return client.getStatusCodeReply(); + } + + public String setnx(final String key, final String value) { + client.setnx(key, value); + return client.getStatusCodeReply(); + } + + public String setex(final String key, final int seconds, final String value) { + client.setex(key, seconds, value); + return client.getStatusCodeReply(); + } + + public String mset(final String... keysvalues) { + client.mset(keysvalues); + return client.getStatusCodeReply(); + } + + public String msetnx(final String... keysvalues) { + client.msetnx(keysvalues); + return client.getStatusCodeReply(); + } + + public String decrBy(final String key, final int integer) { + client.decrBy(key, integer); + return client.getStatusCodeReply(); + } + + public String decr(final String key) { + client.decr(key); + return client.getStatusCodeReply(); + } + + public String incrBy(final String key, final int integer) { + client.incrBy(key, integer); + return client.getStatusCodeReply(); + } + + public String incr(final String key) { + client.incr(key); + return client.getStatusCodeReply(); + } + + public String append(final String key, final String value) { + client.append(key, value); + return client.getStatusCodeReply(); + } + + public String substr(final String key, final int start, final int end) { + client.substr(key, start, end); + return client.getStatusCodeReply(); + } + + public String hset(final String key, final String field, final String value) { + client.hset(key, field, value); + return client.getStatusCodeReply(); + } + + public String hget(final String key, final String field) { + client.hget(key, field); + return client.getStatusCodeReply(); + } + + public String hsetnx(final String key, final String field, + final String value) { + client.hsetnx(key, field, value); + return client.getStatusCodeReply(); + } + + public String hmset(final String key, final Map hash) { + client.hmset(key, hash); + return client.getStatusCodeReply(); + } + + public String hmget(final String key, final String... fields) { + client.hmget(key, fields); + return client.getStatusCodeReply(); + } + + public String hincrBy(final String key, final String field, final int value) { + client.hincrBy(key, field, value); + return client.getStatusCodeReply(); + } + + public String hexists(final String key, final String field) { + client.hexists(key, field); + return client.getStatusCodeReply(); + } + + public String hdel(final String key, final String field) { + client.hdel(key, field); + return client.getStatusCodeReply(); + } + + public String hlen(final String key) { + client.hlen(key); + return client.getStatusCodeReply(); + } + + public String hkeys(final String key) { + client.hkeys(key); + return client.getStatusCodeReply(); + } + + public String hvals(final String key) { + client.hvals(key); + return client.getStatusCodeReply(); + } + + public String hgetAll(final String key) { + client.hgetAll(key); + return client.getStatusCodeReply(); + } + + public String rpush(final String key, final String string) { + client.rpush(key, string); + return client.getStatusCodeReply(); + } + + public String lpush(final String key, final String string) { + client.lpush(key, string); + return client.getStatusCodeReply(); + } + + public String llen(final String key) { + client.llen(key); + return client.getStatusCodeReply(); + } + + public String lrange(final String key, final int start, final int end) { + client.lrange(key, start, end); + return client.getStatusCodeReply(); + } + + public String ltrim(String key, final int start, final int end) { + client.ltrim(key, start, end); + return client.getStatusCodeReply(); + } + + public String lindex(final String key, final int index) { + client.lindex(key, index); + return client.getStatusCodeReply(); + } + + public String lset(final String key, final int index, final String value) { + client.lset(key, index, value); + return client.getStatusCodeReply(); + } + + public String lrem(final String key, final int count, final String value) { + client.lrem(key, count, value); + return client.getStatusCodeReply(); + } + + public String lpop(final String key) { + client.lpop(key); + return client.getStatusCodeReply(); + } + + public String rpop(final String key) { + client.rpop(key); + return client.getStatusCodeReply(); + } + + public String rpoplpush(final String srckey, final String dstkey) { + client.rpoplpush(srckey, dstkey); + return client.getStatusCodeReply(); + } + + public String sadd(final String key, final String member) { + client.sadd(key, member); + return client.getStatusCodeReply(); + } + + public String smembers(final String key) { + client.smembers(key); + return client.getStatusCodeReply(); + } + + public String srem(final String key, final String member) { + client.srem(key, member); + return client.getStatusCodeReply(); + } + + public String spop(final String key) { + client.spop(key); + return client.getStatusCodeReply(); + } + + public String smove(final String srckey, final String dstkey, + final String member) { + client.smove(srckey, dstkey, member); + return client.getStatusCodeReply(); + } + + public String scard(final String key) { + client.scard(key); + return client.getStatusCodeReply(); + } + + public String sismember(final String key, final String member) { + client.sismember(key, member); + return client.getStatusCodeReply(); + } + + public String sinter(final String... keys) { + client.sinter(keys); + return client.getStatusCodeReply(); + } + + public String sinterstore(final String dstkey, final String... keys) { + client.sinterstore(dstkey, keys); + return client.getStatusCodeReply(); + } + + public String sunion(final String... keys) { + client.sunion(keys); + return client.getStatusCodeReply(); + } + + public String sunionstore(final String dstkey, final String... keys) { + client.sunionstore(dstkey, keys); + return client.getStatusCodeReply(); + } + + public String sdiff(final String... keys) { + client.sdiff(keys); + return client.getStatusCodeReply(); + } + + public String sdiffstore(final String dstkey, final String... keys) { + client.sdiffstore(dstkey, keys); + return client.getStatusCodeReply(); + } + + public String srandmember(final String key) { + client.srandmember(key); + return client.getStatusCodeReply(); + } + + public String zadd(final String key, final double score, final String member) { + client.zadd(key, score, member); + return client.getStatusCodeReply(); + } + + public String zrange(final String key, final int start, final int end) { + client.zrange(key, start, end); + return client.getStatusCodeReply(); + } + + public String zrem(final String key, final String member) { + client.zrem(key, member); + return client.getStatusCodeReply(); + } + + public String zincrby(final String key, final double score, + final String member) { + client.zincrby(key, score, member); + return client.getStatusCodeReply(); + } + + public String zrank(final String key, final String member) { + client.zrank(key, member); + return client.getStatusCodeReply(); + } + + public String zrevrank(final String key, final String member) { + client.zrevrank(key, member); + return client.getStatusCodeReply(); + } + + public String zrevrange(final String key, final int start, final int end) { + client.zrevrange(key, start, end); + return client.getStatusCodeReply(); + } + + public String zrangeWithScores(final String key, final int start, + final int end) { + client.zrangeWithScores(key, start, end); + return client.getStatusCodeReply(); + } + + public String zrevrangeWithScores(final String key, final int start, + final int end) { + client.zrevrangeWithScores(key, start, end); + return client.getStatusCodeReply(); + } + + public String zcard(final String key) { + client.zcard(key); + return client.getStatusCodeReply(); + } + + public String zscore(final String key, final String member) { + client.zscore(key, member); + return client.getStatusCodeReply(); + } + + public String sort(final String key) { + client.sort(key); + return client.getStatusCodeReply(); + } + + public String sort(final String key, final SortingParams sortingParameters) { + client.sort(key, sortingParameters); + return client.getStatusCodeReply(); + } } \ No newline at end of file From e2d814880236ef7dad06152033140d607d5b6aac Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 15:59:50 +0100 Subject: [PATCH 14/40] Binary key & values seems to be implemented now --- .../clients/jedis/BinaryShardedJedis.java | 380 ++++++++++++++++++ .../redis/clients/jedis/ShardedJedis.java | 26 +- .../clients/jedis/ShardedJedisPipeline.java | 6 +- src/main/java/redis/clients/util/Hashing.java | 11 +- .../java/redis/clients/util/MurmurHash.java | 8 +- src/main/java/redis/clients/util/Sharded.java | 17 +- .../redis/clients/jedis/tests/JedisTest.java | 2 +- .../clients/jedis/tests/ProtocolTest.java | 2 +- 8 files changed, 417 insertions(+), 35 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/BinaryShardedJedis.java diff --git a/src/main/java/redis/clients/jedis/BinaryShardedJedis.java b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java new file mode 100644 index 0000000..4f6821d --- /dev/null +++ b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java @@ -0,0 +1,380 @@ +package redis.clients.jedis; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.regex.Pattern; + +import redis.clients.jedis.BinaryClient.LIST_POSITION; +import redis.clients.util.Hashing; +import redis.clients.util.Sharded; + + +public class BinaryShardedJedis extends Sharded implements BinaryJedisCommands { + public BinaryShardedJedis(List shards) { + super(shards); + } + + public BinaryShardedJedis(List shards, Hashing algo) { + super(shards, algo); + } + + public BinaryShardedJedis(List shards, Pattern keyTagPattern) { + super(shards, keyTagPattern); + } + + public BinaryShardedJedis(List shards, Hashing algo, + Pattern keyTagPattern) { + super(shards, algo, keyTagPattern); + } + + public void disconnect() throws IOException { + for (JedisShardInfo jedis : getAllShards()) { + jedis.getResource().disconnect(); + } + } + + protected Jedis create(JedisShardInfo shard) { + return new Jedis(shard); + } + + public String set(byte[] key, byte[] value) { + Jedis j = getShard(key); + return j.set(key, value); + } + + public byte[] get(byte[] key) { + Jedis j = getShard(key); + return j.get(key); + } + + public Integer exists(byte[] key) { + Jedis j = getShard(key); + return j.exists(key); + } + + public String type(byte[] key) { + Jedis j = getShard(key); + return j.type(key); + } + + public Integer expire(byte[] key, int seconds) { + Jedis j = getShard(key); + return j.expire(key, seconds); + } + + public Integer expireAt(byte[] key, long unixTime) { + Jedis j = getShard(key); + return j.expireAt(key, unixTime); + } + + public Integer ttl(byte[] key) { + Jedis j = getShard(key); + return j.ttl(key); + } + + public byte[] getSet(byte[] key, byte[] value) { + Jedis j = getShard(key); + return j.getSet(key, value); + } + + public Integer setnx(byte[] key, byte[] value) { + Jedis j = getShard(key); + return j.setnx(key, value); + } + + public String setex(byte[] key, int seconds, byte[] value) { + Jedis j = getShard(key); + return j.setex(key, seconds, value); + } + + public Integer decrBy(byte[] key, int integer) { + Jedis j = getShard(key); + return j.decrBy(key, integer); + } + + public Integer decr(byte[] key) { + Jedis j = getShard(key); + return j.decr(key); + } + + public Integer incrBy(byte[] key, int integer) { + Jedis j = getShard(key); + return j.incrBy(key, integer); + } + + public Integer incr(byte[] key) { + Jedis j = getShard(key); + return j.incr(key); + } + + public Integer append(byte[] key, byte[] value) { + Jedis j = getShard(key); + return j.append(key, value); + } + + public byte[] substr(byte[] key, int start, int end) { + Jedis j = getShard(key); + return j.substr(key, start, end); + } + + public Integer hset(byte[] key, byte[] field, byte[] value) { + Jedis j = getShard(key); + return j.hset(key, field, value); + } + + public byte[] hget(byte[] key, byte[] field) { + Jedis j = getShard(key); + return j.hget(key, field); + } + + public Integer hsetnx(byte[] key, byte[] field, byte[] value) { + Jedis j = getShard(key); + return j.hsetnx(key, field, value); + } + + public String hmset(byte[] key, Map hash) { + Jedis j = getShard(key); + return j.hmset(key, hash); + } + + public List hmget(byte[] key, byte[]... fields) { + Jedis j = getShard(key); + return j.hmget(key, fields); + } + + public Integer hincrBy(byte[] key, byte[] field, int value) { + Jedis j = getShard(key); + return j.hincrBy(key, field, value); + } + + public Integer hexists(byte[] key, byte[] field) { + Jedis j = getShard(key); + return j.hexists(key, field); + } + + public Integer hdel(byte[] key, byte[] field) { + Jedis j = getShard(key); + return j.hdel(key, field); + } + + public Integer hlen(byte[] key) { + Jedis j = getShard(key); + return j.hlen(key); + } + + public List hkeys(byte[] key) { + Jedis j = getShard(key); + return j.hkeys(key); + } + + public List hvals(byte[] key) { + Jedis j = getShard(key); + return j.hvals(key); + } + + public Map hgetAll(byte[] key) { + Jedis j = getShard(key); + return j.hgetAll(key); + } + + public Integer rpush(byte[] key, byte[] string) { + Jedis j = getShard(key); + return j.rpush(key, string); + } + + public Integer lpush(byte[] key, byte[] string) { + Jedis j = getShard(key); + return j.lpush(key, string); + } + + public Integer llen(byte[] key) { + Jedis j = getShard(key); + return j.llen(key); + } + + public List lrange(byte[] key, int start, int end) { + Jedis j = getShard(key); + return j.lrange(key, start, end); + } + + public String ltrim(byte[] key, int start, int end) { + Jedis j = getShard(key); + return j.ltrim(key, start, end); + } + + public byte[] lindex(byte[] key, int index) { + Jedis j = getShard(key); + return j.lindex(key, index); + } + + public String lset(byte[] key, int index, byte[] value) { + Jedis j = getShard(key); + return j.lset(key, index, value); + } + + public Integer lrem(byte[] key, int count, byte[] value) { + Jedis j = getShard(key); + return j.lrem(key, count, value); + } + + public byte[] lpop(byte[] key) { + Jedis j = getShard(key); + return j.lpop(key); + } + + public byte[] rpop(byte[] key) { + Jedis j = getShard(key); + return j.rpop(key); + } + + public Integer sadd(byte[] key, byte[] member) { + Jedis j = getShard(key); + return j.sadd(key, member); + } + + public Set smembers(byte[] key) { + Jedis j = getShard(key); + return j.smembers(key); + } + + public Integer srem(byte[] key, byte[] member) { + Jedis j = getShard(key); + return j.srem(key, member); + } + + public byte[] spop(byte[] key) { + Jedis j = getShard(key); + return j.spop(key); + } + + public Integer scard(byte[] key) { + Jedis j = getShard(key); + return j.scard(key); + } + + public Integer sismember(byte[] key, byte[] member) { + Jedis j = getShard(key); + return j.sismember(key, member); + } + + public byte[] srandmember(byte[] key) { + Jedis j = getShard(key); + return j.srandmember(key); + } + + public Integer zadd(byte[] key, double score, byte[] member) { + Jedis j = getShard(key); + return j.zadd(key, score, member); + } + + public Set zrange(byte[] key, int start, int end) { + Jedis j = getShard(key); + return j.zrange(key, start, end); + } + + public Integer zrem(byte[] key, byte[] member) { + Jedis j = getShard(key); + return j.zrem(key, member); + } + + public Double zincrby(byte[] key, double score, byte[] member) { + Jedis j = getShard(key); + return j.zincrby(key, score, member); + } + + public Integer zrank(byte[] key, byte[] member) { + Jedis j = getShard(key); + return j.zrank(key, member); + } + + public Integer zrevrank(byte[] key, byte[] member) { + Jedis j = getShard(key); + return j.zrevrank(key, member); + } + + public Set zrevrange(byte[] key, int start, int end) { + Jedis j = getShard(key); + return j.zrevrange(key, start, end); + } + + public Set zrangeWithScores(byte[] key, int start, int end) { + Jedis j = getShard(key); + return j.zrangeWithScores(key, start, end); + } + + public Set zrevrangeWithScores(byte[] key, int start, int end) { + Jedis j = getShard(key); + return j.zrevrangeWithScores(key, start, end); + } + + public Integer zcard(byte[] key) { + Jedis j = getShard(key); + return j.zcard(key); + } + + public Double zscore(byte[] key, byte[] member) { + Jedis j = getShard(key); + return j.zscore(key, member); + } + + public List sort(byte[] key) { + Jedis j = getShard(key); + return j.sort(key); + } + + public List sort(byte[] key, SortingParams sortingParameters) { + Jedis j = getShard(key); + return j.sort(key, sortingParameters); + } + + public Integer zcount(byte[] key, double min, double max) { + Jedis j = getShard(key); + return j.zcount(key, min, max); + } + + public Set zrangeByScore(byte[] key, double min, double max) { + Jedis j = getShard(key); + return j.zrangeByScore(key, min, max); + } + + public Set zrangeByScore(byte[] key, double min, double max, + int offset, int count) { + Jedis j = getShard(key); + return j.zrangeByScore(key, min, max, offset, count); + } + + public Set zrangeByScoreWithScores(byte[] key, double min, double max) { + Jedis j = getShard(key); + return j.zrangeByScoreWithScores(key, min, max); + } + + public Set zrangeByScoreWithScores(byte[] key, double min, + double max, int offset, int count) { + Jedis j = getShard(key); + return j.zrangeByScoreWithScores(key, min, max, offset, count); + } + + public Integer zremrangeByRank(byte[] key, int start, int end) { + Jedis j = getShard(key); + return j.zremrangeByRank(key, start, end); + } + + public Integer zremrangeByScore(byte[] key, double start, double end) { + Jedis j = getShard(key); + return j.zremrangeByScore(key, start, end); + } + + public Integer linsert(byte[] key, LIST_POSITION where, byte[] pivot, + byte[] value) { + Jedis j = getShard(key); + return j.linsert(key, where, pivot, value); + } + + public List pipelined(ShardedJedisPipeline shardedJedisPipeline) { + shardedJedisPipeline.setShardedJedis(this); + shardedJedisPipeline.execute(); + return shardedJedisPipeline.getResults(); + } +} \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/ShardedJedis.java b/src/main/java/redis/clients/jedis/ShardedJedis.java index a56f7dc..1fda008 100644 --- a/src/main/java/redis/clients/jedis/ShardedJedis.java +++ b/src/main/java/redis/clients/jedis/ShardedJedis.java @@ -1,17 +1,14 @@ package redis.clients.jedis; -import java.io.IOException; import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; -import redis.clients.jedis.Client.LIST_POSITION; +import redis.clients.jedis.BinaryClient.LIST_POSITION; import redis.clients.util.Hashing; -import redis.clients.util.Sharded; -public class ShardedJedis extends Sharded implements - JedisCommands { +public class ShardedJedis extends BinaryShardedJedis implements JedisCommands { public ShardedJedis(List shards) { super(shards); } @@ -24,21 +21,10 @@ public class ShardedJedis extends Sharded implements super(shards, keyTagPattern); } - public ShardedJedis(List shards, Hashing algo, - Pattern keyTagPattern) { + public ShardedJedis(List shards, Hashing algo, Pattern keyTagPattern) { super(shards, algo, keyTagPattern); } - public void disconnect() throws IOException { - for (JedisShardInfo jedis : getAllShards()) { - jedis.getResource().disconnect(); - } - } - - protected Jedis create(JedisShardInfo shard) { - return new Jedis(shard); - } - public String set(String key, String value) { Jedis j = getShard(key); return j.set(key, value); @@ -371,10 +357,4 @@ public class ShardedJedis extends Sharded implements Jedis j = getShard(key); return j.linsert(key, where, pivot, value); } - - public List pipelined(ShardedJedisPipeline shardedJedisPipeline) { - shardedJedisPipeline.setShardedJedis(this); - shardedJedisPipeline.execute(); - return shardedJedisPipeline.getResults(); - } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/ShardedJedisPipeline.java b/src/main/java/redis/clients/jedis/ShardedJedisPipeline.java index c4e4881..ad57c0f 100644 --- a/src/main/java/redis/clients/jedis/ShardedJedisPipeline.java +++ b/src/main/java/redis/clients/jedis/ShardedJedisPipeline.java @@ -4,10 +4,10 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import redis.clients.jedis.Client.LIST_POSITION; +import redis.clients.jedis.BinaryClient.LIST_POSITION; public abstract class ShardedJedisPipeline { - private ShardedJedis jedis; + private BinaryShardedJedis jedis; private List results = new ArrayList(); private class FutureResult { @@ -22,7 +22,7 @@ public abstract class ShardedJedisPipeline { } } - public void setShardedJedis(ShardedJedis jedis) { + public void setShardedJedis(BinaryShardedJedis jedis) { this.jedis = jedis; } diff --git a/src/main/java/redis/clients/util/Hashing.java b/src/main/java/redis/clients/util/Hashing.java index 291416f..5eaa023 100644 --- a/src/main/java/redis/clients/util/Hashing.java +++ b/src/main/java/redis/clients/util/Hashing.java @@ -3,13 +3,19 @@ package redis.clients.util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import redis.clients.jedis.Protocol; + public interface Hashing { public static final Hashing MURMUR_HASH = new MurmurHash(); public static final Hashing MD5 = new Hashing() { private MessageDigest md5 = null; // avoid recurring construction - + public long hash(String key) { + return hash(key.getBytes(Protocol.UTF8)); + } + + public long hash(byte[] key) { if (md5 == null) { try { md5 = MessageDigest.getInstance("MD5"); @@ -20,7 +26,7 @@ public interface Hashing { } md5.reset(); - md5.update(key.getBytes()); + md5.update(key); byte[] bKey = md5.digest(); long res = ((long) (bKey[3] & 0xFF) << 24) | ((long) (bKey[2] & 0xFF) << 16) @@ -30,4 +36,5 @@ public interface Hashing { }; public long hash(String key); + public long hash(byte[] key); } \ No newline at end of file diff --git a/src/main/java/redis/clients/util/MurmurHash.java b/src/main/java/redis/clients/util/MurmurHash.java index 0294a25..934f7cd 100644 --- a/src/main/java/redis/clients/util/MurmurHash.java +++ b/src/main/java/redis/clients/util/MurmurHash.java @@ -20,6 +20,8 @@ package redis.clients.util; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import redis.clients.jedis.Protocol; + /** * This is a very fast, non-cryptographic hash suitable for general hash-based * lookup. See http://murmurhash.googlepages.com/ for more details. @@ -156,7 +158,11 @@ public class MurmurHash implements Hashing { return h; } + public long hash(byte[] key) { + return hash64A(key, 0x1234ABCD); + } + public long hash(String key) { - return hash64A(key.getBytes(), 0x1234ABCD); + return hash(key.getBytes(Protocol.UTF8)); } } \ No newline at end of file diff --git a/src/main/java/redis/clients/util/Sharded.java b/src/main/java/redis/clients/util/Sharded.java index 9a4d7b7..d0f4f75 100644 --- a/src/main/java/redis/clients/util/Sharded.java +++ b/src/main/java/redis/clients/util/Sharded.java @@ -69,10 +69,19 @@ public class Sharded> { } } - public R getShard(String key) { - return nodes.floorEntry(algo.hash(getKeyTag(key))).getValue() - .getResource(); - } + public R getShard(byte[] key) { + return nodes + .floorEntry(algo.hash(key)) + .getValue() + .getResource(); + } + + public R getShard(String key) { + return nodes + .floorEntry(algo.hash(getKeyTag(key))) + .getValue() + .getResource(); + } public S getShardInfo(String key) { return nodes.floorEntry(algo.hash(getKeyTag(key))).getValue(); diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index 851c796..d1ebf0a 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -26,7 +26,7 @@ public class JedisTest extends JedisCommandTestBase { bigdata[b] = (byte) ((byte) b % 255); } Map hash = new HashMap(); - hash.put("data", new String(bigdata, RedisOutputStream.CHARSET)); + hash.put("data", new String(bigdata, Protocol.UTF8)); String status = jedis.hmset("foo", hash); assertEquals("OK", status); diff --git a/src/test/java/redis/clients/jedis/tests/ProtocolTest.java b/src/test/java/redis/clients/jedis/tests/ProtocolTest.java index f8f4723..0ba6f54 100644 --- a/src/test/java/redis/clients/jedis/tests/ProtocolTest.java +++ b/src/test/java/redis/clients/jedis/tests/ProtocolTest.java @@ -25,7 +25,7 @@ public class ProtocolTest extends Assert { PipedOutputStream pos = new PipedOutputStream(pis); Protocol protocol = new Protocol(); - protocol.sendCommand(new RedisOutputStream(pos), "GET", "SOMEKEY"); + protocol.sendCommand(new RedisOutputStream(pos), Protocol.Command.GET, "SOMEKEY".getBytes(Protocol.UTF8)); pos.close(); String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n"; From fd8e3116ab4706dcc678ac8eaea5508858c61ed4 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 17:14:06 +0100 Subject: [PATCH 15/40] Fix broken U tests --- .../redis/clients/jedis/tests/JedisTest.java | 26 ++++++++- .../clients/jedis/tests/ProtocolTest.java | 54 +++++++++++++------ .../clients/jedis/tests/ShardedJedisTest.java | 10 +++- 3 files changed, 71 insertions(+), 19 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index d1ebf0a..6812f2c 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -1,10 +1,12 @@ package redis.clients.jedis.tests; import java.util.HashMap; +import java.util.Iterator; +import java.util.List; import java.util.Map; import org.junit.Test; - +import org.junit.Assert; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.Protocol; @@ -41,4 +43,26 @@ public class JedisTest extends JedisCommandTestBase { Jedis jedis = new Jedis(shardInfo); jedis.get("foo"); } + + @SuppressWarnings("rawtypes") + public static void compareList(List expected, List result) { + final Iterator expectedit = expected.iterator(); + final Iterator responseit = result.iterator(); + while(expectedit.hasNext()) { + final Object exp = expectedit.next(); + final Object resp = responseit.next(); + if(exp instanceof byte[]) { + final byte[] bexp = (byte[]) exp; + final byte[] bresp = (byte[]) resp; + Assert.assertArrayEquals(bexp, bresp); + } else if (exp instanceof List) { + final List subexp = (List) exp; + final List subresp = (List) resp; + compareList(subexp, subresp); + } else { + assertEquals(exp, resp); + } + } + } + } diff --git a/src/test/java/redis/clients/jedis/tests/ProtocolTest.java b/src/test/java/redis/clients/jedis/tests/ProtocolTest.java index 0ba6f54..cfb1eb3 100644 --- a/src/test/java/redis/clients/jedis/tests/ProtocolTest.java +++ b/src/test/java/redis/clients/jedis/tests/ProtocolTest.java @@ -7,9 +7,11 @@ import java.io.InputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; import java.util.List; -import junit.framework.Assert; +import org.junit.Assert; import org.junit.Test; @@ -87,34 +89,54 @@ public class ProtocolTest extends Assert { "*4\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$5\r\nHello\r\n$5\r\nWorld\r\n" .getBytes()); Protocol protocol = new Protocol(); - List response = (List) (List) protocol - .read(new RedisInputStream(is)); - List expected = new ArrayList(); - expected.add("foo"); - expected.add("bar"); - expected.add("Hello"); - expected.add("World"); + List response = (List) protocol.read(new RedisInputStream(is)); + List expected = new ArrayList(); + expected.add("foo".getBytes(Protocol.UTF8)); + expected.add("bar".getBytes(Protocol.UTF8)); + expected.add("Hello".getBytes(Protocol.UTF8)); + expected.add("World".getBytes(Protocol.UTF8)); - assertEquals(expected, response); + assertEquals(expected.size(), response.size()); + JedisTest.compareList(expected, response); +// final Iterator expectedit = expected.iterator(); +// final Iterator responseit = response.iterator(); +// while(expectedit.hasNext()) { +// final byte[] exp = expectedit.next(); +// final byte[] resp = responseit.next(); +// assertArrayEquals(exp, resp); +// } is = new ByteArrayInputStream( "*4\r\n$3\r\nfoo\r\n+OK\r\n:1000\r\n*2\r\n$3\r\nfoo\r\n$3\r\nbar" .getBytes()); protocol = new Protocol(); - List response2 = (List) protocol - .read(new RedisInputStream(is)); + List response2 = (List) protocol.read(new RedisInputStream(is)); List expected2 = new ArrayList(); - expected2.add("foo"); + expected2.add("foo".getBytes(Protocol.UTF8)); expected2.add("OK"); expected2.add(1000); List sub = new ArrayList(); - sub.add("foo"); - sub.add("bar"); + sub.add("foo".getBytes(Protocol.UTF8)); + sub.add("bar".getBytes(Protocol.UTF8)); expected2.add(sub); - assertEquals(expected2, response2); + assertEquals(expected2.size(), response2.size()); + JedisTest.compareList(expected2, response2); +// final Iterator expectedit2 = expected2.iterator(); +// final Iterator responseit2 = response2.iterator(); +// while(expectedit2.hasNext()) { +// final Object exp = expectedit2.next(); +// final Object resp = responseit2.next(); +// if(exp instanceof byte[]) { +// final byte[] bexp = (byte[]) exp; +// final byte[] bresp = (byte[]) resp; +// assertArrayEquals(bexp, bresp); +// } else { +// assertEquals(exp, resp); +// } +// } } - + @SuppressWarnings("unchecked") @Test public void nullMultiBulkReply() { diff --git a/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java b/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java index 5190306..1a5c93d 100644 --- a/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java @@ -9,6 +9,7 @@ import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisShardInfo; +import redis.clients.jedis.Protocol; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPipeline; import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort; @@ -141,7 +142,12 @@ public class ShardedJedisTest extends Assert { } }); - assertEquals("a", results.get(0)); - assertEquals("b", results.get(1)); + List expected = new ArrayList(2); + expected.add("a".getBytes(Protocol.UTF8)); + expected.add("b".getBytes(Protocol.UTF8)); + + JedisTest.compareList(expected, results); +// assertArrayEquals("a".getBytes(Protocol.UTF8), results.get(0)); +// assertArrayEquals("b".getBytes(Protocol.UTF8), results.get(1)); } } \ No newline at end of file From abd464f9bd52f1072e4af1fbaebcc9c2e5407657 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 17:48:58 +0100 Subject: [PATCH 16/40] Implemented commands are retreived from Protocol.Command enum --- .../tests/JedisNewCommandsCheckTest.java | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/JedisNewCommandsCheckTest.java b/src/test/java/redis/clients/jedis/tests/JedisNewCommandsCheckTest.java index 3fd5cb6..db2db14 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisNewCommandsCheckTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisNewCommandsCheckTest.java @@ -15,6 +15,7 @@ import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPubSub; +import redis.clients.jedis.Protocol.Command; import redis.clients.jedis.Transaction; public class JedisNewCommandsCheckTest extends Assert { @@ -36,23 +37,28 @@ public class JedisNewCommandsCheckTest extends Assert { } private Set getImplementedCommands() { - Method[] methods = Jedis.class.getDeclaredMethods(); - Set implementedCommands = new HashSet(); - for (Method method : methods) { - implementedCommands.add(method.getName().trim().toLowerCase()); - } - - methods = JedisPubSub.class.getDeclaredMethods(); - for (Method method : methods) { - implementedCommands.add(method.getName().trim().toLowerCase()); - } - - methods = Transaction.class.getDeclaredMethods(); - for (Method method : methods) { - implementedCommands.add(method.getName().trim().toLowerCase()); - } - implementedCommands.add("config"); - return implementedCommands; +// Method[] methods = Jedis.class.getDeclaredMethods(); +// Set implementedCommands = new HashSet(); +// for (Method method : methods) { +// implementedCommands.add(method.getName().trim().toLowerCase()); +// } +// +// methods = JedisPubSub.class.getDeclaredMethods(); +// for (Method method : methods) { +// implementedCommands.add(method.getName().trim().toLowerCase()); +// } +// +// methods = Transaction.class.getDeclaredMethods(); +// for (Method method : methods) { +// implementedCommands.add(method.getName().trim().toLowerCase()); +// } +// implementedCommands.add("config"); +// return implementedCommands; + Set implementedCommands = new HashSet(); + for(Command cmd : Command.values()){ + implementedCommands.add(cmd.name().toLowerCase()); + } + return implementedCommands; } private String[] getAvailableCommands() throws MalformedURLException, From ead39394a11e75d1f26989b2477d40c62404b60f Mon Sep 17 00:00:00 2001 From: Yaourt Date: Thu, 4 Nov 2010 18:55:00 +0100 Subject: [PATCH 17/40] Fix some broken U tests (there are stille broken tests, working on it) --- pom.xml | 2 +- src/main/java/redis/clients/jedis/Client.java | 2 +- .../java/redis/clients/jedis/Connection.java | 9 ++- .../redis/clients/jedis/tests/JedisTest.java | 2 + .../commands/AllKindOfValuesCommandsTest.java | 72 +++++++++++++++++++ .../tests/commands/JedisCommandTestBase.java | 2 +- .../tests/commands/SortedSetCommandsTest.java | 4 +- 7 files changed, 86 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index b550c90..2f5723b 100644 --- a/pom.xml +++ b/pom.xml @@ -54,7 +54,7 @@ junit 4.8.1 jar - compile + test diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index e902573..8d18840 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -34,7 +34,7 @@ public class Client extends BinaryClient { } public void type(final String key) { - del(key.getBytes(Protocol.UTF8)); + type(key.getBytes(Protocol.UTF8)); } public void keys(final String pattern) { diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index d375592..524d850 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -79,7 +79,7 @@ public class Connection { } catch (IOException e) { throw new JedisException("Could not connect to redis-server", e); } - protocol.sendCommand(outputStream, cmd, (byte[])null); + protocol.sendCommand(outputStream, cmd, new byte[0][]); pipelinedCommands++; return this; } @@ -144,7 +144,12 @@ public class Connection { } public String getBulkReply() { - return new String(getBinaryBulkReply(), Protocol.UTF8); + final byte[] result = getBinaryBulkReply(); + if (null != result) { + return new String(result, Protocol.UTF8); + } else { + return null; + } } public byte[] getBinaryBulkReply() { diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index 6812f2c..f91a23b 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -46,6 +46,8 @@ public class JedisTest extends JedisCommandTestBase { @SuppressWarnings("rawtypes") public static void compareList(List expected, List result) { + assertEquals(expected.size(), result.size()); + final Iterator expectedit = expected.iterator(); final Iterator responseit = result.iterator(); while(expectedit.hasNext()) { diff --git a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java index 8ee7691..3c2acb3 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java @@ -6,8 +6,20 @@ import java.util.List; import org.junit.Test; import redis.clients.jedis.JedisException; +import redis.clients.jedis.tests.JedisTest; public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { + final byte[] bfoo = {0x01, 0x02, 0x03, 0x04}; + final byte[] bfoo1 = {0x01, 0x02, 0x03, 0x04, 0x0A}; + final byte[] bfoo2 = {0x01, 0x02, 0x03, 0x04, 0x0B}; + final byte[] bfoo3 = {0x01, 0x02, 0x03, 0x04, 0x0C}; + final byte[] bbar = {0x05, 0x06, 0x07, 0x08}; + final byte[] bbar1 = {0x05, 0x06, 0x07, 0x08, 0x0A}; + final byte[] bbar2 = {0x05, 0x06, 0x07, 0x08, 0x0B}; + final byte[] bbar3 = {0x05, 0x06, 0x07, 0x08, 0x0C}; + + final byte[] bfoobar = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + @Test public void ping() { String status = jedis.ping(); @@ -19,14 +31,26 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { String status = jedis.set("foo", "bar"); assertEquals("OK", status); + status = jedis.set(bfoo, bbar); + assertEquals("OK", status); + int reply = jedis.exists("foo"); assertEquals(1, reply); + reply = jedis.exists(bfoo); + assertEquals(1, reply); + reply = jedis.del("foo"); assertEquals(1, reply); + reply = jedis.del(bfoo); + assertEquals(1, reply); + reply = jedis.exists("foo"); assertEquals(0, reply); + + reply = jedis.exists(bfoo); + assertEquals(0, reply); } @Test @@ -52,6 +76,29 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { reply = jedis.del("foo1", "foo2"); assertEquals(0, reply); + + // Binary ... + jedis.set(bfoo1, bbar1); + jedis.set(bfoo2, bbar2); + jedis.set(bfoo3, bbar3); + + reply = jedis.del(bfoo1, bfoo2, bfoo3); + assertEquals(3, reply); + + reply = jedis.exists(bfoo1); + assertEquals(0, reply); + reply = jedis.exists(bfoo2); + assertEquals(0, reply); + reply = jedis.exists(bfoo3); + assertEquals(0, reply); + + jedis.set(bfoo1, bbar1); + + reply = jedis.del(bfoo1, bfoo2); + assertEquals(1, reply); + + reply = jedis.del(bfoo1, bfoo2); + assertEquals(0, reply); } @Test @@ -59,6 +106,11 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { jedis.set("foo", "bar"); String status = jedis.type("foo"); assertEquals("string", status); + + // Binary + jedis.set(bfoo, bbar); + status = jedis.type(bfoo); + assertEquals("string", status); } @Test @@ -76,6 +128,26 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { keys = jedis.keys("bar*"); assertEquals(expected, keys); + + // Binary + jedis.set(bfoo, bbar); + jedis.set(bfoobar, bbar); + + byte[] bfoostar = new byte[bfoo.length+1]; + System.arraycopy(bfoo, 0, bfoostar, 0, bfoo.length); + bfoostar[bfoostar.length-1] = '*'; + List bkeys = jedis.keys(bfoostar); + List bexpected = new ArrayList(); + bexpected.add(bfoo); + bexpected.add(bfoobar); + JedisTest.compareList(bexpected, bkeys); +// assertEquals(expected, keys); + + expected = new ArrayList(); + keys = jedis.keys("bar*"); + + assertEquals(expected, keys); + } @Test diff --git a/src/test/java/redis/clients/jedis/tests/commands/JedisCommandTestBase.java b/src/test/java/redis/clients/jedis/tests/commands/JedisCommandTestBase.java index f7150f3..084541d 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/JedisCommandTestBase.java +++ b/src/test/java/redis/clients/jedis/tests/commands/JedisCommandTestBase.java @@ -3,7 +3,7 @@ package redis.clients.jedis.tests.commands; import java.io.IOException; import java.net.UnknownHostException; -import junit.framework.Assert; +import org.junit.Assert; import org.junit.After; import org.junit.Before; diff --git a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java index ad238fc..4fe03f0 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java @@ -179,10 +179,10 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { jedis.zadd("foo", 2d, "a"); Double score = jedis.zscore("foo", "b"); - assertEquals(10d, score); + assertEquals((Double)10d, score); score = jedis.zscore("foo", "c"); - assertEquals(0.1d, score); + assertEquals((Double)0.1d, score); score = jedis.zscore("foo", "s"); assertNull(score); From dab21609e63f850517121ae188088716d45da4e8 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 09:33:57 +0100 Subject: [PATCH 18/40] Fix some more U tests ... --- .../redis/clients/jedis/tests/JedisTest.java | 48 ++++++++++++++----- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index f91a23b..5c5bd7a 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -49,22 +49,44 @@ public class JedisTest extends JedisCommandTestBase { assertEquals(expected.size(), result.size()); final Iterator expectedit = expected.iterator(); - final Iterator responseit = result.iterator(); while(expectedit.hasNext()) { final Object exp = expectedit.next(); - final Object resp = responseit.next(); - if(exp instanceof byte[]) { - final byte[] bexp = (byte[]) exp; - final byte[] bresp = (byte[]) resp; - Assert.assertArrayEquals(bexp, bresp); - } else if (exp instanceof List) { - final List subexp = (List) exp; - final List subresp = (List) resp; - compareList(subexp, subresp); - } else { - assertEquals(exp, resp); - } + final Iterator responseit = result.iterator(); + boolean found = false; + while(responseit.hasNext() && !found) { + final Object resp = responseit.next(); + if(exp instanceof byte[]) { + final byte[] bexp = (byte[]) exp; + final byte[] bresp = (byte[]) resp; + if(arraysAreEquals(bexp, bresp)) { + found = true; + } +// Assert.assertArrayEquals(bexp, bresp); + } else if (exp instanceof List) { + final List subexp = (List) exp; + final List subresp = (List) resp; + compareList(subexp, subresp); + } else { + assertEquals(exp, resp); + } + } + if(!found){ + fail("Result doesn't contain " + exp.toString()); + } } } + public static boolean arraysAreEquals(final byte[] expected, final byte[] result) { + if(expected.length != result.length) { + return false; + } + + for(int i=0; i < expected.length; i++) { + if(expected[i] != result[i]) { + return false; + } + } + + return true; + } } From f8afa09aa531365cbd36559a14588830f46bf123 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 09:53:37 +0100 Subject: [PATCH 19/40] Fix some more U tests ... --- .../redis/clients/jedis/tests/JedisTest.java | 26 +++++++++++++------ .../clients/jedis/tests/ProtocolTest.java | 14 +++++----- .../clients/jedis/tests/ShardedJedisTest.java | 2 +- .../commands/AllKindOfValuesCommandsTest.java | 2 +- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index 5c5bd7a..2a0f3d9 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -5,6 +5,8 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import javax.swing.tree.ExpandVetoException; + import org.junit.Test; import org.junit.Assert; import redis.clients.jedis.Jedis; @@ -45,8 +47,10 @@ public class JedisTest extends JedisCommandTestBase { } @SuppressWarnings("rawtypes") - public static void compareList(List expected, List result) { - assertEquals(expected.size(), result.size()); + public static boolean isListAreEquals(List expected, List result) { + if(expected.size() != result.size()) { + return false; + } final Iterator expectedit = expected.iterator(); while(expectedit.hasNext()) { @@ -55,28 +59,34 @@ public class JedisTest extends JedisCommandTestBase { boolean found = false; while(responseit.hasNext() && !found) { final Object resp = responseit.next(); - if(exp instanceof byte[]) { + if(exp instanceof byte[] && resp instanceof byte[]) { final byte[] bexp = (byte[]) exp; final byte[] bresp = (byte[]) resp; - if(arraysAreEquals(bexp, bresp)) { + if(isArraysAreEquals(bexp, bresp)) { found = true; } // Assert.assertArrayEquals(bexp, bresp); - } else if (exp instanceof List) { + } else if (exp instanceof List && resp instanceof List) { final List subexp = (List) exp; final List subresp = (List) resp; - compareList(subexp, subresp); + if(isListAreEquals(subexp, subresp)) { + found = true; + } } else { - assertEquals(exp, resp); + if (exp.equals(resp)){ + found = true; + } } } if(!found){ fail("Result doesn't contain " + exp.toString()); } } + + return true; } - public static boolean arraysAreEquals(final byte[] expected, final byte[] result) { + public static boolean isArraysAreEquals(final byte[] expected, final byte[] result) { if(expected.length != result.length) { return false; } diff --git a/src/test/java/redis/clients/jedis/tests/ProtocolTest.java b/src/test/java/redis/clients/jedis/tests/ProtocolTest.java index cfb1eb3..00ccbc7 100644 --- a/src/test/java/redis/clients/jedis/tests/ProtocolTest.java +++ b/src/test/java/redis/clients/jedis/tests/ProtocolTest.java @@ -45,8 +45,8 @@ public class ProtocolTest extends Assert { public void bulkReply() { InputStream is = new ByteArrayInputStream("$6\r\nfoobar\r\n".getBytes()); Protocol protocol = new Protocol(); - String response = (String) protocol.read(new RedisInputStream(is)); - assertEquals("foobar", response); + byte[] response = (byte[]) protocol.read(new RedisInputStream(is)); + assertArrayEquals("foobar".getBytes(Protocol.UTF8), response); } @Test @@ -54,8 +54,8 @@ public class ProtocolTest extends Assert { FragmentedByteArrayInputStream fis = new FragmentedByteArrayInputStream( "$30\r\n012345678901234567890123456789\r\n".getBytes()); Protocol protocol = new Protocol(); - String response = (String) protocol.read(new RedisInputStream(fis)); - assertEquals("012345678901234567890123456789", response); + byte[] response = (byte[]) protocol.read(new RedisInputStream(fis)); + assertArrayEquals("012345678901234567890123456789".getBytes(Protocol.UTF8), response); } @Test @@ -96,8 +96,7 @@ public class ProtocolTest extends Assert { expected.add("Hello".getBytes(Protocol.UTF8)); expected.add("World".getBytes(Protocol.UTF8)); - assertEquals(expected.size(), response.size()); - JedisTest.compareList(expected, response); + assertTrue(JedisTest.isListAreEquals(expected, response)); // final Iterator expectedit = expected.iterator(); // final Iterator responseit = response.iterator(); // while(expectedit.hasNext()) { @@ -120,8 +119,7 @@ public class ProtocolTest extends Assert { sub.add("bar".getBytes(Protocol.UTF8)); expected2.add(sub); - assertEquals(expected2.size(), response2.size()); - JedisTest.compareList(expected2, response2); + assertTrue(JedisTest.isListAreEquals(expected2, response2)); // final Iterator expectedit2 = expected2.iterator(); // final Iterator responseit2 = response2.iterator(); // while(expectedit2.hasNext()) { diff --git a/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java b/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java index 1a5c93d..6718014 100644 --- a/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java @@ -146,7 +146,7 @@ public class ShardedJedisTest extends Assert { expected.add("a".getBytes(Protocol.UTF8)); expected.add("b".getBytes(Protocol.UTF8)); - JedisTest.compareList(expected, results); + assertTrue(JedisTest.isListAreEquals(expected, results)); // assertArrayEquals("a".getBytes(Protocol.UTF8), results.get(0)); // assertArrayEquals("b".getBytes(Protocol.UTF8), results.get(1)); } diff --git a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java index 3c2acb3..5c3ec3c 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java @@ -140,7 +140,7 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { List bexpected = new ArrayList(); bexpected.add(bfoo); bexpected.add(bfoobar); - JedisTest.compareList(bexpected, bkeys); + assertTrue(JedisTest.isListAreEquals(bexpected, bkeys)); // assertEquals(expected, keys); expected = new ArrayList(); From afad308e29df790e4d789d4538783794d4a7d1e6 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 09:57:54 +0100 Subject: [PATCH 20/40] Fix some more U tests ... --- src/main/java/redis/clients/jedis/Connection.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 524d850..7a5323c 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -164,6 +164,9 @@ public class Connection { public List getMultiBulkReply() { final List bresult = getBinaryMultiBulkReply(); + if(null == bresult) { + return null; + } final ArrayList result = new ArrayList(bresult.size()); for(final byte[] barray : bresult) { result.add(new String(barray, Protocol.UTF8)); From 2836d78c44f13f87a1056b41568f404a9afb479f Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 10:17:03 +0100 Subject: [PATCH 21/40] Fix some more U tests ... --- src/main/java/redis/clients/jedis/Connection.java | 13 +++++++++++-- src/main/java/redis/clients/jedis/Protocol.java | 4 ++-- .../jedis/tests/commands/ControlCommandsTest.java | 5 +++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 7a5323c..a10f8c3 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -140,7 +140,12 @@ public class Connection { protected String getStatusCodeReply() { pipelinedCommands--; - return (String) protocol.read(inputStream); + final byte[] resp = (byte[]) protocol.read(inputStream); + if (null == resp) { + return null; + } else { + return new String(resp, Protocol.UTF8); + } } public String getBulkReply() { @@ -169,7 +174,11 @@ public class Connection { } final ArrayList result = new ArrayList(bresult.size()); for(final byte[] barray : bresult) { - result.add(new String(barray, Protocol.UTF8)); + if( barray == null) { + result.add(null); + } else { + result.add(new String(barray, Protocol.UTF8)); + } } return result; } diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index c8ed6fc..a6a89e4 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -97,8 +97,8 @@ public final class Protocol { return null; } - private String processStatusCodeReply(final RedisInputStream is) { - return is.readLine(); + private byte[] processStatusCodeReply(final RedisInputStream is) { + return is.readLine().getBytes(UTF8); } private byte[] processBulkReply(final RedisInputStream is) { diff --git a/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java index 0bc4020..68be673 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java @@ -21,8 +21,9 @@ public class ControlCommandsTest extends JedisCommandTestBase { String status = jedis.bgsave(); assertEquals("Background saving started", status); } catch (JedisException e) { - assertEquals("ERR Background save already in progress", e - .getMessage()); + assertTrue( + "ERR Background save already in progress" + .equalsIgnoreCase(e.getMessage())); } } From ad5f44edc44e97d66f789d0c60d02f04f14e83d2 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 10:21:38 +0100 Subject: [PATCH 22/40] Fix some more U tests ... --- src/test/java/redis/clients/jedis/tests/ProtocolTest.java | 6 +++--- .../clients/jedis/tests/commands/SortedSetCommandsTest.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/ProtocolTest.java b/src/test/java/redis/clients/jedis/tests/ProtocolTest.java index 00ccbc7..e871802 100644 --- a/src/test/java/redis/clients/jedis/tests/ProtocolTest.java +++ b/src/test/java/redis/clients/jedis/tests/ProtocolTest.java @@ -70,8 +70,8 @@ public class ProtocolTest extends Assert { public void singleLineReply() { InputStream is = new ByteArrayInputStream("+OK\r\n".getBytes()); Protocol protocol = new Protocol(); - String response = (String) protocol.read(new RedisInputStream(is)); - assertEquals("OK", response); + byte[] response = (byte[]) protocol.read(new RedisInputStream(is)); + assertArrayEquals("OK".getBytes(Protocol.UTF8), response); } @Test @@ -112,7 +112,7 @@ public class ProtocolTest extends Assert { List response2 = (List) protocol.read(new RedisInputStream(is)); List expected2 = new ArrayList(); expected2.add("foo".getBytes(Protocol.UTF8)); - expected2.add("OK"); + expected2.add("OK".getBytes(Protocol.UTF8)); expected2.add(1000); List sub = new ArrayList(); sub.add("foo".getBytes(Protocol.UTF8)); diff --git a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java index 4fe03f0..6bb404f 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java @@ -91,7 +91,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { expected.add("a"); expected.add("b"); - assertEquals(3d, score); + assertEquals(3d, score, 0); assertEquals(expected, jedis.zrange("foo", 0, 100)); } From a2468dff58b8b5a5cfcbab75e6ff00561a53f082 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 10:25:41 +0100 Subject: [PATCH 23/40] Fix some more U tests ... --- .../clients/jedis/tests/commands/HashesCommandsTest.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java index 6355b03..d2a8882 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java @@ -8,6 +8,8 @@ import java.util.Map; import org.junit.Test; +import redis.clients.jedis.tests.JedisTest; + public class HashesCommandsTest extends JedisCommandTestBase { @Test public void hset() { @@ -124,7 +126,8 @@ public class HashesCommandsTest extends JedisCommandTestBase { List expected = new ArrayList(); expected.add("bar"); expected.add("car"); - assertEquals(expected, keys); + assertTrue(JedisTest.isListAreEquals(expected, keys)); +// assertEquals(expected, keys); } @Test @@ -138,7 +141,8 @@ public class HashesCommandsTest extends JedisCommandTestBase { List expected = new ArrayList(); expected.add("car"); expected.add("bar"); - assertEquals(expected, vals); + assertTrue(JedisTest.isListAreEquals(expected, vals)); +// assertEquals(expected, vals); } @Test From 47a39cb520d4dc7e7868357712b8a4f740da435c Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 10:28:19 +0100 Subject: [PATCH 24/40] Fix some more U tests ... --- .../java/redis/clients/jedis/tests/PipeliningTest.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/PipeliningTest.java b/src/test/java/redis/clients/jedis/tests/PipeliningTest.java index eacecee..8df99d6 100644 --- a/src/test/java/redis/clients/jedis/tests/PipeliningTest.java +++ b/src/test/java/redis/clients/jedis/tests/PipeliningTest.java @@ -4,13 +4,14 @@ import java.io.IOException; import java.net.UnknownHostException; import java.util.List; -import junit.framework.Assert; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPipeline; +import redis.clients.jedis.Protocol; import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort; public class PipeliningTest extends Assert { @@ -36,7 +37,7 @@ public class PipeliningTest extends Assert { }); assertEquals(2, results.size()); - assertEquals("OK", results.get(0)); - assertEquals("bar", results.get(1)); + assertArrayEquals("OK".getBytes(Protocol.UTF8), (byte[])results.get(0)); + assertArrayEquals("bar".getBytes(Protocol.UTF8), (byte[])results.get(1)); } } From a2b3417fce4373f1d2563089c5690de02f187e1c Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 10:30:02 +0100 Subject: [PATCH 25/40] Fix some more U tests ... Now only PubSub are still broken ... (little refactoring required) --- .../jedis/tests/commands/TransactionCommandsTest.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java index ca43163..715ed88 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java @@ -10,8 +10,10 @@ import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisException; +import redis.clients.jedis.Protocol; import redis.clients.jedis.Transaction; import redis.clients.jedis.TransactionBlock; +import redis.clients.jedis.tests.JedisTest; public class TransactionCommandsTest extends JedisCommandTestBase { Jedis nj; @@ -102,8 +104,9 @@ public class TransactionCommandsTest extends JedisCommandTestBase { t.set("mykey", val); List resp = t.exec(); List expected = new ArrayList(); - expected.add("OK"); - assertEquals(expected, resp); + expected.add("OK".getBytes(Protocol.UTF8)); + JedisTest.isListAreEquals(expected, resp); +// assertEquals(expected, resp); } @Test(expected = JedisException.class) From 9ff9ab350537e02a79eb0bf5f1b67bf0735b7042 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 10:56:54 +0100 Subject: [PATCH 26/40] Use Arrays.equals(...) instead of hand written method for arrays comparison --- .../redis/clients/jedis/tests/JedisTest.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index 2a0f3d9..4a320a9 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -1,5 +1,6 @@ package redis.clients.jedis.tests; +import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -62,10 +63,9 @@ public class JedisTest extends JedisCommandTestBase { if(exp instanceof byte[] && resp instanceof byte[]) { final byte[] bexp = (byte[]) exp; final byte[] bresp = (byte[]) resp; - if(isArraysAreEquals(bexp, bresp)) { + if(Arrays.equals(bexp, bresp)) { found = true; } -// Assert.assertArrayEquals(bexp, bresp); } else if (exp instanceof List && resp instanceof List) { final List subexp = (List) exp; final List subresp = (List) resp; @@ -86,17 +86,17 @@ public class JedisTest extends JedisCommandTestBase { return true; } - public static boolean isArraysAreEquals(final byte[] expected, final byte[] result) { - if(expected.length != result.length) { - return false; - } - - for(int i=0; i < expected.length; i++) { - if(expected[i] != result[i]) { - return false; - } - } - - return true; - } +// public static boolean isArraysAreEqualsPlop(final byte[] expected, final byte[] result) { +// if(expected.length != result.length) { +// return false; +// } +// +// for(int i=0; i < expected.length; i++) { +// if(expected[i] != result[i]) { +// return false; +// } +// } +// +// return true; +// } } From 11fec5c182bdace70b32ff8e4099fb402cb4f815 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 14:46:37 +0100 Subject: [PATCH 27/40] Ready to be merged back to master ... Last buggy U tests fixed. --- .../redis/clients/jedis/BinaryClient.java | 12 ++- .../java/redis/clients/jedis/JedisPubSub.java | 85 +++++++++++++++---- .../java/redis/clients/jedis/Protocol.java | 10 ++- 3 files changed, 86 insertions(+), 21 deletions(-) diff --git a/src/main/java/redis/clients/jedis/BinaryClient.java b/src/main/java/redis/clients/jedis/BinaryClient.java index 245a0c9..fb0cc7e 100644 --- a/src/main/java/redis/clients/jedis/BinaryClient.java +++ b/src/main/java/redis/clients/jedis/BinaryClient.java @@ -1,15 +1,19 @@ package redis.clients.jedis; +import static redis.clients.jedis.Protocol.toByteArray; +import static redis.clients.jedis.Protocol.Command.*; +import static redis.clients.jedis.Protocol.Keyword.LIMIT; +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 java.util.ArrayList; import java.util.List; import java.util.Map; import redis.clients.jedis.Protocol.Command; import redis.clients.jedis.Protocol.Keyword; -import static redis.clients.jedis.Protocol.toByteArray; - -import static redis.clients.jedis.Protocol.Command.*; -import static redis.clients.jedis.Protocol.Keyword.*; public class BinaryClient extends Connection { public enum LIST_POSITION { BEFORE, AFTER; diff --git a/src/main/java/redis/clients/jedis/JedisPubSub.java b/src/main/java/redis/clients/jedis/JedisPubSub.java index ac6918d..e0c5dec 100644 --- a/src/main/java/redis/clients/jedis/JedisPubSub.java +++ b/src/main/java/redis/clients/jedis/JedisPubSub.java @@ -1,7 +1,10 @@ package redis.clients.jedis; +import java.util.Arrays; import java.util.List; +import static redis.clients.jedis.Protocol.Keyword.*; + public abstract class JedisPubSub { private int subscribedChannels = 0; private Client client; @@ -62,26 +65,78 @@ public abstract class JedisPubSub { private void process(Client client) { do { List reply = client.getObjectMultiBulkReply(); - if (reply.get(0).equals("subscribe")) { + final Object firstObj = reply.get(0); + if (!(firstObj instanceof byte[])) { + throw + new JedisException("Unknown message type: "+ firstObj); + } + final byte[] resp = (byte[]) firstObj; + if(Arrays.equals(SUBSCRIBE.raw, resp)) { subscribedChannels = ((Integer) reply.get(2)).intValue(); - onSubscribe((String) reply.get(1), subscribedChannels); - } else if (reply.get(0).equals("unsubscribe")) { + final byte[] bchannel = (byte[]) reply.get(1); + final String strchannel = + (bchannel == null) ? + null : + new String(bchannel, Protocol.UTF8); + onSubscribe(strchannel, subscribedChannels); + } else if (Arrays.equals(UNSUBSCRIBE.raw, resp)) { subscribedChannels = ((Integer) reply.get(2)).intValue(); - onUnsubscribe((String) reply.get(1), subscribedChannels); - } else if (reply.get(0).equals("message")) { - onMessage((String) reply.get(1), (String) reply.get(2)); - } else if (reply.get(0).equals("pmessage")) { - onPMessage((String) reply.get(1), (String) reply.get(2), - (String) reply.get(3)); - } else if (reply.get(0).equals("psubscribe")) { + final byte[] bchannel = (byte[]) reply.get(1); + final String strchannel = + (bchannel == null) ? + null : + new String(bchannel, Protocol.UTF8); + onUnsubscribe(strchannel, subscribedChannels); + } else if (Arrays.equals(MESSAGE.raw, resp)) { + final byte[] bchannel = (byte[]) reply.get(1); + final byte[] bmesg = (byte[]) reply.get(2); + final String strchannel = + (bchannel == null) ? + null : + new String(bchannel, Protocol.UTF8); + final String strmesg = + (bmesg == null) ? + null : + new String(bmesg, Protocol.UTF8); + onMessage(strchannel, strmesg); + } else if (Arrays.equals(PMESSAGE.raw, resp)) { + final byte[] bpattern = (byte[]) reply.get(1); + final byte[] bchannel = (byte[]) reply.get(2); + final byte[] bmesg = (byte[]) reply.get(3); + final String strpattern = + (bpattern == null) ? + null : + new String(bpattern, Protocol.UTF8); + final String strchannel = + (bchannel == null) ? + null : + new String(bchannel, Protocol.UTF8); + final String strmesg = + (bmesg == null) ? + null : + new String(bmesg, Protocol.UTF8); + onPMessage( + strpattern, + strchannel, + strmesg); + } else if (Arrays.equals(PSUBSCRIBE.raw, resp)) { subscribedChannels = ((Integer) reply.get(2)).intValue(); - onPSubscribe((String) reply.get(1), subscribedChannels); - } else if (reply.get(0).equals("punsubscribe")) { + final byte[] bpattern = (byte[]) reply.get(1); + final String strpattern = + (bpattern == null) ? + null : + new String(bpattern, Protocol.UTF8); + onPSubscribe(strpattern, subscribedChannels); + } else if (Arrays.equals(PUNSUBSCRIBE.raw, resp)) { subscribedChannels = ((Integer) reply.get(2)).intValue(); - onPUnsubscribe((String) reply.get(1), subscribedChannels); + final byte[] bpattern = (byte[]) reply.get(1); + final String strpattern = + (bpattern == null) ? + null : + new String(bpattern, Protocol.UTF8); + onPUnsubscribe(strpattern, subscribedChannels); } else { - throw new JedisException("Unknown message type: " - + reply.get(0)); + throw new JedisException("Unknown message type: "+ firstObj); } } while (isSubscribed()); } diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index a6a89e4..7ae5b2a 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -286,18 +286,24 @@ public final class Protocol { BY, DESC, GET, + LIMIT, + MESSAGE, NO, NOSORT, + PMESSAGE, + PSUBSCRIBE, + PUNSUBSCRIBE, ONE, - LIMIT, SET, STORE, + SUBSCRIBE, + UNSUBSCRIBE, WEIGHTS, WITHSCORES; public final byte[] raw; Keyword() { - raw = this.name().getBytes(UTF8); + raw = this.name().toLowerCase().getBytes(UTF8); } } From 70cd1768cf82a7a88029896832c3bf8b8bb6a44d Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 15:42:38 +0100 Subject: [PATCH 28/40] Remove duplicates ... --- src/main/java/redis/clients/jedis/Jedis.java | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index 22bca49..4c95282 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -27,12 +27,6 @@ public class Jedis extends BinaryJedis implements JedisCommands { super(shardInfo); } - public String ping() { - checkIsInMulti(); - client.ping(); - return client.getStatusCodeReply(); - } - /** * Set the string value as value of the key. The string can't be longer than * 1073741824 bytes (1 GB). @@ -289,19 +283,6 @@ public class Jedis extends BinaryJedis implements JedisCommands { return client.getIntegerReply(); } - /** - * Select the DB with having the specified zero-based numeric index. For - * default every new client connection is automatically selected to DB 0. - * - * @param index - * @return Status code reply - */ - public String select(final int index) { - checkIsInMulti(); - client.select(index); - return client.getStatusCodeReply(); - } - /** * Move the specified key from the currently selected DB to the specified * destination DB. Note that this command returns 1 only if the key was From c62a6762be31cdb0420c5c579621476be5cb0404 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 15:42:51 +0100 Subject: [PATCH 29/40] Add Binary oriented U tests ... --- .../java/redis/clients/jedis/BinaryJedis.java | 4 +- .../commands/AllKindOfValuesCommandsTest.java | 156 +++++++++++++++++- 2 files changed, 155 insertions(+), 5 deletions(-) diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index c6dd983..9d2c315 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -2834,9 +2834,9 @@ public class BinaryJedis implements BinaryJedisCommands { return client.getIntegerReply(); } - public String echo(final byte[] string) { + public byte[] echo(final byte[] string) { client.echo(string); - return client.getBulkReply(); + return client.getBinaryBulkReply(); } public Integer linsert(final byte[] key, final LIST_POSITION where, final byte[] pivot, diff --git a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java index 5c3ec3c..2fc058c 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java @@ -1,11 +1,13 @@ package redis.clients.jedis.tests.commands; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.junit.Test; import redis.clients.jedis.JedisException; +import redis.clients.jedis.Protocol; import redis.clients.jedis.tests.JedisTest; public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { @@ -162,6 +164,23 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { String randomkey = jedis.randomKey(); assertTrue(randomkey.equals("foo") || randomkey.equals("bar")); + + // Binary + jedis.del("foo"); + jedis.del("bar"); + assertEquals(null, jedis.randomKey()); + + jedis.set(bfoo, bbar); + + assertArrayEquals(bfoo, jedis.randomBinaryKey()); + + jedis.set(bbar, bfoo); + + byte[] randomBkey = jedis.randomBinaryKey(); + assertTrue( + Arrays.equals(randomBkey, bfoo) || + Arrays.equals(randomBkey, bbar)); + } @Test @@ -175,12 +194,34 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { value = jedis.get("bar"); assertEquals("bar", value); + + //Binary + jedis.set(bfoo, bbar); + String bstatus = jedis.rename(bfoo, bbar); + assertEquals("OK", bstatus); + + byte[] bvalue = jedis.get(bfoo); + assertEquals(null, bvalue); + + bvalue = jedis.get(bbar); + assertArrayEquals(bbar, bvalue); } - @Test(expected = JedisException.class) + @Test public void renameOldAndNewAreTheSame() { - jedis.set("foo", "bar"); - jedis.rename("foo", "foo"); + try { + jedis.set("foo", "bar"); + jedis.rename("foo", "foo"); + fail("JedisException expected"); + } catch(final JedisException e){} + + //Binary + try { + jedis.set(bfoo, bbar); + jedis.rename(bfoo, bfoo); + fail("JedisException expected"); + } catch(final JedisException e){} + } @Test @@ -192,6 +233,16 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { jedis.set("foo", "bar"); status = jedis.renamenx("foo", "bar"); assertEquals(0, status); + + //Binary + jedis.set(bfoo, bbar); + int bstatus = jedis.renamenx(bfoo, bbar); + assertEquals(1, bstatus); + + jedis.set(bfoo, bbar); + bstatus = jedis.renamenx(bfoo, bbar); + assertEquals(0, bstatus); + } @Test @@ -202,6 +253,11 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { jedis.set("foo", "bar"); size = jedis.dbSize(); assertEquals(1, size); + + //Binary + jedis.set(bfoo, bbar); + size = jedis.dbSize(); + assertEquals(2, size); } @Test @@ -212,6 +268,15 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { jedis.set("foo", "bar"); status = jedis.expire("foo", 20); assertEquals(1, status); + + // Binary + int bstatus = jedis.expire(bfoo, 20); + assertEquals(0, bstatus); + + jedis.set(bfoo, bbar); + bstatus = jedis.expire(bfoo, 20); + assertEquals(1, bstatus); + } @Test @@ -225,6 +290,16 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { unixTime = (System.currentTimeMillis() / 1000L) + 20; status = jedis.expireAt("foo", unixTime); assertEquals(1, status); + + //Binary + int bstatus = jedis.expireAt(bfoo, unixTime); + assertEquals(0, bstatus); + + jedis.set(bfoo, bbar); + unixTime = (System.currentTimeMillis() / 1000L) + 20; + bstatus = jedis.expireAt(bfoo, unixTime); + assertEquals(1, bstatus); + } @Test @@ -239,6 +314,19 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { jedis.expire("foo", 20); ttl = jedis.ttl("foo"); assertTrue(ttl >= 0 && ttl <= 20); + + //Binary + int bttl = jedis.ttl(bfoo); + assertEquals(-1, bttl); + + jedis.set(bfoo, bbar); + bttl = jedis.ttl(bfoo); + assertEquals(-1, bttl); + + jedis.expire(bfoo, 20); + bttl = jedis.ttl(bfoo); + assertTrue(bttl >= 0 && bttl <= 20); + } @Test @@ -250,6 +338,14 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { status = jedis.select(0); assertEquals("OK", status); assertEquals("bar", jedis.get("foo")); + //Binary + jedis.set(bfoo, bbar); + String bstatus = jedis.select(1); + assertEquals("OK", bstatus); + assertEquals(null, jedis.get(bfoo)); + bstatus = jedis.select(0); + assertEquals("OK", bstatus); + assertArrayEquals(bbar, jedis.get(bfoo)); } @Test @@ -264,6 +360,20 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { jedis.select(1); assertEquals("bar", jedis.get("foo")); + + //Binary + jedis.select(0); + int bstatus = jedis.move(bfoo, 1); + assertEquals(0, bstatus); + + jedis.set(bfoo, bbar); + bstatus = jedis.move(bfoo, 1); + assertEquals(1, bstatus); + assertEquals(null, jedis.get(bfoo)); + + jedis.select(1); + assertArrayEquals(bbar, jedis.get(bfoo)); + } @Test @@ -277,6 +387,20 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { assertEquals(0, jedis.dbSize().intValue()); jedis.select(1); assertEquals(1, jedis.dbSize().intValue()); + jedis.del("bar"); + + //Binary + jedis.select(0); + jedis.set(bfoo, bbar); + assertEquals(1, jedis.dbSize().intValue()); + jedis.set(bbar, bfoo); + jedis.move(bbar, 1); + String bstatus = jedis.flushDB(); + assertEquals("OK", bstatus); + assertEquals(0, jedis.dbSize().intValue()); + jedis.select(1); + assertEquals(1, jedis.dbSize().intValue()); + } @Test @@ -290,6 +414,19 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { assertEquals(0, jedis.dbSize().intValue()); jedis.select(1); assertEquals(0, jedis.dbSize().intValue()); + + //Binary + jedis.select(0); + jedis.set(bfoo, bbar); + assertEquals(1, jedis.dbSize().intValue()); + jedis.set(bbar, bfoo); + jedis.move(bbar, 1); + String bstatus = jedis.flushAll(); + assertEquals("OK", bstatus); + assertEquals(0, jedis.dbSize().intValue()); + jedis.select(1); + assertEquals(0, jedis.dbSize().intValue()); + } @Test @@ -299,12 +436,25 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { int status = jedis.persist("foo"); assertEquals(1, status); assertEquals(-1, jedis.ttl("foo").intValue()); + + //Binary + jedis.setex(bfoo, 60 * 60, bbar); + assertTrue(jedis.ttl(bfoo) > 0); + int bstatus = jedis.persist(bfoo); + assertEquals(1, bstatus); + assertEquals(-1, jedis.ttl(bfoo).intValue()); + } + @Test public void echo() { String result = jedis.echo("hello world"); assertEquals("hello world", result); + + // Binary + byte[] bresult = jedis.echo("hello world".getBytes(Protocol.UTF8)); + assertArrayEquals("hello world".getBytes(Protocol.UTF8), bresult); } } \ No newline at end of file From 7d58a68d2d818b5e5df850f5486de025bd06b3dd Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 16:09:32 +0100 Subject: [PATCH 30/40] Binary U test on Hashes ... --- .../redis/clients/jedis/tests/JedisTest.java | 21 ++- .../tests/commands/HashesCommandsTest.java | 145 +++++++++++++++++- 2 files changed, 157 insertions(+), 9 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index 4a320a9..57db5b3 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -1,6 +1,7 @@ package redis.clients.jedis.tests; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -48,7 +49,7 @@ public class JedisTest extends JedisCommandTestBase { } @SuppressWarnings("rawtypes") - public static boolean isListAreEquals(List expected, List result) { + public static boolean isListAreEquals(Collection expected, Collection result) { if(expected.size() != result.size()) { return false; } @@ -66,20 +67,26 @@ public class JedisTest extends JedisCommandTestBase { if(Arrays.equals(bexp, bresp)) { found = true; } - } else if (exp instanceof List && resp instanceof List) { - final List subexp = (List) exp; - final List subresp = (List) resp; + } else if (exp instanceof Collection && resp instanceof Collection) { + final Collection subexp = (Collection) exp; + final Collection subresp = (Collection) resp; if(isListAreEquals(subexp, subresp)) { found = true; } } else { - if (exp.equals(resp)){ - found = true; + if (null != exp) { + if (exp.equals(resp)){ + found = true; + } + } else { + if(resp == null) { + found = true; + } } } } if(!found){ - fail("Result doesn't contain " + exp.toString()); + fail("Result doesn't contain " + (null != exp ? exp.toString() : null)); } } diff --git a/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java index d2a8882..9dd3a72 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java @@ -1,6 +1,7 @@ package redis.clients.jedis.tests.commands; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -11,12 +12,22 @@ import org.junit.Test; import redis.clients.jedis.tests.JedisTest; public class HashesCommandsTest extends JedisCommandTestBase { + final byte[] bfoo = {0x01, 0x02, 0x03, 0x04}; + final byte[] bbar = {0x05, 0x06, 0x07, 0x08}; + final byte[] bcar = {0x09, 0x0A, 0x0B, 0x0C}; @Test public void hset() { int status = jedis.hset("foo", "bar", "car"); assertEquals(1, status); status = jedis.hset("foo", "bar", "foo"); assertEquals(0, status); + + //Binary + int bstatus = jedis.hset(bfoo, bbar, bcar); + assertEquals(1, bstatus); + bstatus = jedis.hset(bfoo, bbar, bfoo); + assertEquals(0, bstatus); + } @Test @@ -25,6 +36,12 @@ public class HashesCommandsTest extends JedisCommandTestBase { assertEquals(null, jedis.hget("bar", "foo")); assertEquals(null, jedis.hget("foo", "car")); assertEquals("car", jedis.hget("foo", "bar")); + + //Binary + jedis.hset(bfoo, bbar, bcar); + assertEquals(null, jedis.hget(bbar, bfoo)); + assertEquals(null, jedis.hget(bfoo, bcar)); + assertArrayEquals(bcar, jedis.hget(bfoo, bbar)); } @Test @@ -40,6 +57,20 @@ public class HashesCommandsTest extends JedisCommandTestBase { status = jedis.hsetnx("foo", "car", "bar"); assertEquals(1, status); assertEquals("bar", jedis.hget("foo", "car")); + + //Binary + int bstatus = jedis.hsetnx(bfoo, bbar, bcar); + assertEquals(1, bstatus); + assertArrayEquals(bcar, jedis.hget(bfoo, bbar)); + + bstatus = jedis.hsetnx(bfoo, bbar, bfoo); + assertEquals(0, bstatus); + assertArrayEquals(bcar, jedis.hget(bfoo, bbar)); + + bstatus = jedis.hsetnx(bfoo, bcar, bbar); + assertEquals(1, bstatus); + assertArrayEquals(bbar, jedis.hget(bfoo, bcar)); + } @Test @@ -51,7 +82,18 @@ public class HashesCommandsTest extends JedisCommandTestBase { assertEquals("OK", status); assertEquals("car", jedis.hget("foo", "bar")); assertEquals("bar", jedis.hget("foo", "car")); + + //Binary + Map bhash = new HashMap(); + bhash.put(bbar, bcar); + bhash.put(bcar, bbar); + String bstatus = jedis.hmset(bfoo, bhash); + assertEquals("OK", bstatus); + assertArrayEquals(bcar, jedis.hget(bfoo, bbar)); + assertArrayEquals(bbar, jedis.hget(bfoo, bcar)); + } + @Test public void hmget() { @@ -67,6 +109,20 @@ public class HashesCommandsTest extends JedisCommandTestBase { expected.add(null); assertEquals(expected, values); + + //Binary + Map bhash = new HashMap(); + bhash.put(bbar, bcar); + bhash.put(bcar, bbar); + jedis.hmset(bfoo, bhash); + + List bvalues = jedis.hmget(bfoo, bbar, bcar, bfoo); + List bexpected = new ArrayList(); + bexpected.add(bcar); + bexpected.add(bbar); + bexpected.add(null); + + JedisTest.isListAreEquals(bexpected, bvalues); } @Test @@ -77,6 +133,15 @@ public class HashesCommandsTest extends JedisCommandTestBase { assertEquals(0, value); value = jedis.hincrBy("foo", "bar", -10); assertEquals(-10, value); + + //Binary + int bvalue = jedis.hincrBy(bfoo, bbar, 1); + assertEquals(1, bvalue); + bvalue = jedis.hincrBy(bfoo, bbar, -1); + assertEquals(0, bvalue); + bvalue = jedis.hincrBy(bfoo, bbar, -10); + assertEquals(-10, bvalue); + } @Test @@ -89,6 +154,17 @@ public class HashesCommandsTest extends JedisCommandTestBase { assertEquals(0, jedis.hexists("bar", "foo").intValue()); assertEquals(0, jedis.hexists("foo", "foo").intValue()); assertEquals(1, jedis.hexists("foo", "bar").intValue()); + + //Binary + Map bhash = new HashMap(); + bhash.put(bbar, bcar); + bhash.put(bcar, bbar); + jedis.hmset(bfoo, bhash); + + assertEquals(0, jedis.hexists(bbar, bfoo).intValue()); + assertEquals(0, jedis.hexists(bfoo, bfoo).intValue()); + assertEquals(1, jedis.hexists(bfoo, bbar).intValue()); + } @Test @@ -102,6 +178,18 @@ public class HashesCommandsTest extends JedisCommandTestBase { assertEquals(0, jedis.hdel("foo", "foo").intValue()); assertEquals(1, jedis.hdel("foo", "bar").intValue()); assertEquals(null, jedis.hget("foo", "bar")); + + //Binary + Map bhash = new HashMap(); + bhash.put(bbar, bcar); + bhash.put(bcar, bbar); + jedis.hmset(bfoo, bhash); + + assertEquals(0, jedis.hdel(bbar, bfoo).intValue()); + assertEquals(0, jedis.hdel(bfoo, bfoo).intValue()); + assertEquals(1, jedis.hdel(bfoo, bbar).intValue()); + assertEquals(null, jedis.hget(bfoo, bbar)); + } @Test @@ -113,6 +201,16 @@ public class HashesCommandsTest extends JedisCommandTestBase { assertEquals(0, jedis.hlen("bar").intValue()); assertEquals(2, jedis.hlen("foo").intValue()); + + //Binary + Map bhash = new HashMap(); + bhash.put(bbar, bcar); + bhash.put(bcar, bbar); + jedis.hmset(bfoo, bhash); + + assertEquals(0, jedis.hlen(bbar).intValue()); + assertEquals(2, jedis.hlen(bfoo).intValue()); + } @Test @@ -127,7 +225,19 @@ public class HashesCommandsTest extends JedisCommandTestBase { expected.add("bar"); expected.add("car"); assertTrue(JedisTest.isListAreEquals(expected, keys)); -// assertEquals(expected, keys); + + //Binary + Map bhash = new LinkedHashMap(); + bhash.put(bbar, bcar); + bhash.put(bcar, bbar); + jedis.hmset(bfoo, bhash); + + List bkeys = jedis.hkeys(bfoo); + List bexpected = new ArrayList(); + bexpected.add(bbar); + bexpected.add(bcar); + assertTrue(JedisTest.isListAreEquals(bexpected, bkeys)); + } @Test @@ -142,7 +252,20 @@ public class HashesCommandsTest extends JedisCommandTestBase { expected.add("car"); expected.add("bar"); assertTrue(JedisTest.isListAreEquals(expected, vals)); -// assertEquals(expected, vals); + + //Binary + Map bhash = new LinkedHashMap(); + bhash.put(bbar, bcar); + bhash.put(bcar, bbar); + jedis.hmset(bfoo, bhash); + + List bvals = jedis.hvals(bfoo); + List bexpected = new ArrayList(); + bexpected.add(bcar); + bexpected.add(bbar); + assertTrue(JedisTest.isListAreEquals(bexpected, bvals)); + + } @Test @@ -157,5 +280,23 @@ public class HashesCommandsTest extends JedisCommandTestBase { expected.put("bar", "car"); expected.put("car", "bar"); assertEquals(expected, hash); + + //Binary + Map bh = new HashMap(); + bh.put(bbar, bcar); + bh.put(bcar, bbar); + jedis.hmset(bfoo, bh); + + Map bhash = jedis.hgetAll(bfoo); + Map bexpected = new HashMap(); + bexpected.put(bbar, bcar); + bexpected.put(bcar, bbar); + final Collection keysetExpected = bexpected.keySet(); + final Collection keysetResult = bhash.keySet(); + assertTrue(JedisTest.isListAreEquals(keysetExpected, keysetResult)); + final Collection valsExpected = bexpected.values(); + final Collection valsResult = bhash.values(); + assertTrue(JedisTest.isListAreEquals(valsExpected, valsResult)); + } } From ad25921ecaf46dd182f5bb3ce5e7492611168c15 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 16:41:38 +0100 Subject: [PATCH 31/40] Binary U test on Lists ... --- .../tests/commands/ListCommandsTest.java | 296 +++++++++++++++++- 1 file changed, 291 insertions(+), 5 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java index b861a59..e99c461 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java @@ -8,14 +8,35 @@ import org.junit.Test; import redis.clients.jedis.Client; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisException; +import redis.clients.jedis.tests.JedisTest; public class ListCommandsTest extends JedisCommandTestBase { + final byte[] bfoo = {0x01, 0x02, 0x03, 0x04}; + final byte[] bbar = {0x05, 0x06, 0x07, 0x08}; + final byte[] bcar = {0x09, 0x0A, 0x0B, 0x0C}; + final byte[] bA = {0x0A}; + final byte[] bB = {0x0B}; + final byte[] bC = {0x0C}; + final byte[] b1 = {0x01}; + final byte[] b2 = {0x02}; + final byte[] b3 = {0x03}; + final byte[] bhello = {0x04, 0x02}; + final byte[] bx = {0x02, 0x04}; + final byte[] bdst = {0x11, 0x12, 0x13, 0x14}; + @Test public void rpush() { int size = jedis.rpush("foo", "bar"); assertEquals(1, size); size = jedis.rpush("foo", "foo"); assertEquals(2, size); + + //Binary + int bsize = jedis.rpush(bfoo, bbar); + assertEquals(1, bsize); + bsize = jedis.rpush(bfoo, bfoo); + assertEquals(2, bsize); + } @Test @@ -24,7 +45,15 @@ public class ListCommandsTest extends JedisCommandTestBase { assertEquals(1, size); size = jedis.lpush("foo", "foo"); assertEquals(2, size); + + //Binary + int bsize = jedis.lpush(bfoo, bbar); + assertEquals(1, bsize); + bsize = jedis.lpush(bfoo, bfoo); + assertEquals(2, bsize); + } + @Test public void llen() { @@ -32,12 +61,30 @@ public class ListCommandsTest extends JedisCommandTestBase { jedis.lpush("foo", "bar"); jedis.lpush("foo", "car"); assertEquals(2, jedis.llen("foo").intValue()); + + //Binary + assertEquals(0, jedis.llen(bfoo).intValue()); + jedis.lpush(bfoo, bbar); + jedis.lpush(bfoo, bcar); + assertEquals(2, jedis.llen(bfoo).intValue()); + } - @Test(expected = JedisException.class) + @Test public void llenNotOnList() { - jedis.set("foo", "bar"); - jedis.llen("foo"); + try { + jedis.set("foo", "bar"); + jedis.llen("foo"); + fail("JedisException expected"); + } catch (final JedisException e){} + + //Binary + try { + jedis.set(bfoo, bbar); + jedis.llen(bfoo); + fail("JedisException expected"); + } catch (final JedisException e){} + } @Test @@ -67,6 +114,35 @@ public class ListCommandsTest extends JedisCommandTestBase { expected = new ArrayList(); range = jedis.lrange("foo", 2, 1); assertEquals(expected, range); + + //Binary + jedis.rpush(bfoo, bA); + jedis.rpush(bfoo, bB); + jedis.rpush(bfoo, bC); + + List bexpected = new ArrayList(); + bexpected.add(bA); + bexpected.add(bB); + bexpected.add(bC); + + List brange = jedis.lrange(bfoo, 0, 2); + JedisTest.isListAreEquals(bexpected, brange); + + brange = jedis.lrange(bfoo, 0, 20); + JedisTest.isListAreEquals(bexpected, brange); + + bexpected = new ArrayList(); + bexpected.add(bB); + bexpected.add(bC); + + brange = jedis.lrange(bfoo, 1, 2); + JedisTest.isListAreEquals(bexpected, brange); + + bexpected = new ArrayList(); + brange = jedis.lrange(bfoo, 2, 1); + JedisTest.isListAreEquals(bexpected, brange); + + } @Test @@ -83,6 +159,21 @@ public class ListCommandsTest extends JedisCommandTestBase { assertEquals("OK", status); assertEquals(2, jedis.llen("foo").intValue()); assertEquals(expected, jedis.lrange("foo", 0, 100)); + + //Binary + jedis.lpush(bfoo, b1); + jedis.lpush(bfoo, b2); + jedis.lpush(bfoo, b3); + String bstatus = jedis.ltrim(bfoo, 0, 1); + + List bexpected = new ArrayList(); + bexpected.add(b3); + bexpected.add(b2); + + assertEquals("OK", bstatus); + assertEquals(2, jedis.llen(bfoo).intValue()); + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.lrange(bfoo, 0, 100))); + } @Test @@ -100,6 +191,21 @@ public class ListCommandsTest extends JedisCommandTestBase { assertEquals("OK", status); assertEquals(expected, jedis.lrange("foo", 0, 100)); + + //Binary + jedis.lpush(bfoo, b1); + jedis.lpush(bfoo, b2); + jedis.lpush(bfoo, b3); + + List bexpected = new ArrayList(); + bexpected.add(b3); + bexpected.add(bbar); + bexpected.add(b1); + + String bstatus = jedis.lset(bfoo, 1, bbar); + + assertEquals("OK", bstatus); + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.lrange(bfoo, 0, 100))); } @Test @@ -110,7 +216,17 @@ public class ListCommandsTest extends JedisCommandTestBase { assertEquals("3", jedis.lindex("foo", 0)); assertEquals(null, jedis.lindex("foo", 100)); + + //Binary + jedis.lpush(bfoo, b1); + jedis.lpush(bfoo, b2); + jedis.lpush(bfoo, b3); + + assertArrayEquals(b3, jedis.lindex(bfoo, 0)); + assertEquals(null, jedis.lindex(bfoo, 100)); + } + @Test public void lrem() { @@ -134,6 +250,29 @@ public class ListCommandsTest extends JedisCommandTestBase { assertEquals(2, count); assertEquals(expected, jedis.lrange("foo", 0, 1000)); assertEquals(0, jedis.lrem("bar", 100, "foo").intValue()); + + //Binary + jedis.lpush(bfoo, bhello); + jedis.lpush(bfoo, bhello); + jedis.lpush(bfoo, bx); + jedis.lpush(bfoo, bhello); + jedis.lpush(bfoo, bC); + jedis.lpush(bfoo, bB); + jedis.lpush(bfoo, bA); + + int bcount = jedis.lrem(bfoo, -2, bhello); + + List bexpected = new ArrayList(); + bexpected.add(bA); + bexpected.add(bB); + bexpected.add(bC); + bexpected.add(bhello); + bexpected.add(bx); + + assertEquals(2, bcount); + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.lrange(bfoo, 0, 1000))); + assertEquals(0, jedis.lrem(bbar, 100, bfoo).intValue()); + } @Test @@ -155,6 +294,26 @@ public class ListCommandsTest extends JedisCommandTestBase { element = jedis.lpop("foo"); assertEquals(null, element); + + //Binary + jedis.rpush(bfoo, bA); + jedis.rpush(bfoo, bB); + jedis.rpush(bfoo, bC); + + byte[] belement = jedis.lpop(bfoo); + assertArrayEquals(bA, belement); + + List bexpected = new ArrayList(); + bexpected.add(bB); + bexpected.add(bC); + + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.lrange(bfoo, 0, 1000))); + jedis.lpop(bfoo); + jedis.lpop(bfoo); + + belement = jedis.lpop(bfoo); + assertEquals(null, belement); + } @Test @@ -176,6 +335,26 @@ public class ListCommandsTest extends JedisCommandTestBase { element = jedis.rpop("foo"); assertEquals(null, element); + + //Binary + jedis.rpush(bfoo, bA); + jedis.rpush(bfoo, bB); + jedis.rpush(bfoo, bC); + + byte[] belement = jedis.rpop(bfoo); + assertArrayEquals(bC, belement); + + List bexpected = new ArrayList(); + bexpected.add(bA); + bexpected.add(bB); + + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.lrange(bfoo, 0, 1000))); + jedis.rpop(bfoo); + jedis.rpop(bfoo); + + belement = jedis.rpop(bfoo); + assertEquals(null, belement); + } @Test @@ -202,6 +381,31 @@ public class ListCommandsTest extends JedisCommandTestBase { assertEquals(srcExpected, jedis.lrange("foo", 0, 1000)); assertEquals(dstExpected, jedis.lrange("dst", 0, 1000)); + + //Binary + jedis.rpush(bfoo, bA); + jedis.rpush(bfoo, bB); + jedis.rpush(bfoo, bC); + + jedis.rpush(bdst, bfoo); + jedis.rpush(bdst, bbar); + + byte[] belement = jedis.rpoplpush(bfoo, bdst); + + assertArrayEquals(bC, belement); + + List bsrcExpected = new ArrayList(); + bsrcExpected.add(bA); + bsrcExpected.add(bB); + + List bdstExpected = new ArrayList(); + bdstExpected.add(bC); + bdstExpected.add(bfoo); + bdstExpected.add(bbar); + + assertTrue(JedisTest.isListAreEquals(bsrcExpected, jedis.lrange(bfoo, 0, 1000))); + assertTrue(JedisTest.isListAreEquals(bdstExpected, jedis.lrange(bdst, 0, 1000))); + } @Test @@ -226,6 +430,29 @@ public class ListCommandsTest extends JedisCommandTestBase { assertEquals(2, result.size()); assertEquals("foo", result.get(0)); assertEquals("bar", result.get(1)); + + //Binary + List bresult = jedis.blpop(1, bfoo); + assertNull(bresult); + + new Thread(new Runnable() { + public void run() { + try { + Jedis j = createJedis(); + j.lpush(bfoo, bbar); + j.disconnect(); + } catch (Exception ex) { + fail(ex.getMessage()); + } + } + }).start(); + + bresult = jedis.blpop(1, bfoo); + assertNotNull(bresult); + assertEquals(2, bresult.size()); + assertArrayEquals(bfoo, bresult.get(0)); + assertArrayEquals(bbar, bresult.get(1)); + } @Test @@ -250,6 +477,29 @@ public class ListCommandsTest extends JedisCommandTestBase { assertEquals(2, result.size()); assertEquals("foo", result.get(0)); assertEquals("bar", result.get(1)); + + //Binary + List bresult = jedis.brpop(1, bfoo); + assertNull(bresult); + + new Thread(new Runnable() { + public void run() { + try { + Jedis j = createJedis(); + j.lpush(bfoo, bbar); + j.disconnect(); + } catch (Exception ex) { + fail(ex.getMessage()); + } + } + }).start(); + + bresult = jedis.brpop(1, bfoo); + assertNotNull(bresult); + assertEquals(2, bresult.size()); + assertArrayEquals(bfoo, bresult.get(0)); + assertArrayEquals(bbar, bresult.get(1)); + } @Test @@ -260,6 +510,15 @@ public class ListCommandsTest extends JedisCommandTestBase { jedis.lpush("foo", "a"); status = jedis.lpushx("foo", "b"); assertEquals(2, status); + + //Binary + int bstatus = jedis.lpushx(bfoo, bbar); + assertEquals(0, bstatus); + + jedis.lpush(bfoo, bA); + bstatus = jedis.lpushx(bfoo, bB); + assertEquals(2, bstatus); + } @Test @@ -270,12 +529,19 @@ public class ListCommandsTest extends JedisCommandTestBase { jedis.lpush("foo", "a"); status = jedis.rpushx("foo", "b"); assertEquals(2, status); + + //Binary + int bstatus = jedis.rpushx(bfoo, bbar); + assertEquals(0, bstatus); + + jedis.lpush(bfoo, bA); + bstatus = jedis.rpushx(bfoo, bB); + assertEquals(2, bstatus); } @Test public void linsert() { - int status = jedis.linsert("foo", Client.LIST_POSITION.BEFORE, "bar", - "car"); + int status = jedis.linsert("foo", Client.LIST_POSITION.BEFORE, "bar", "car"); assertEquals(0, status); jedis.lpush("foo", "a"); @@ -292,5 +558,25 @@ public class ListCommandsTest extends JedisCommandTestBase { status = jedis .linsert("foo", Client.LIST_POSITION.BEFORE, "bar", "car"); assertEquals(-1, status); + + //Binary + int bstatus = jedis.linsert(bfoo, Client.LIST_POSITION.BEFORE, bbar, bcar); + assertEquals(0, bstatus); + + jedis.lpush(bfoo, bA); + bstatus = jedis.linsert(bfoo, Client.LIST_POSITION.AFTER, bA, bB); + assertEquals(2, bstatus); + + List bactual = jedis.lrange(bfoo, 0, 100); + List bexpected = new ArrayList(); + bexpected.add(bA); + bexpected.add(bB); + + assertTrue(JedisTest.isListAreEquals(bexpected, bactual)); + + bstatus = jedis + .linsert(bfoo, Client.LIST_POSITION.BEFORE, bbar, bcar); + assertEquals(-1, bstatus); + } } \ No newline at end of file From 5a87ae465ee9b6606f5613b3de5895a93c28cfec Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 17:11:31 +0100 Subject: [PATCH 32/40] Binary U test on Sets ... --- .../jedis/tests/commands/SetCommandsTest.java | 229 +++++++++++++++++- 1 file changed, 228 insertions(+), 1 deletion(-) diff --git a/src/test/java/redis/clients/jedis/tests/commands/SetCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/SetCommandsTest.java index d9126c5..f5c2c2e 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/SetCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/SetCommandsTest.java @@ -1,11 +1,24 @@ package redis.clients.jedis.tests.commands; +import java.util.Arrays; import java.util.LinkedHashSet; import java.util.Set; import org.junit.Test; +import redis.clients.jedis.tests.JedisTest; + public class SetCommandsTest extends JedisCommandTestBase { + final byte[] bfoo = {0x01, 0x02, 0x03, 0x04}; + final byte[] bbar = {0x05, 0x06, 0x07, 0x08}; + final byte[] bcar = {0x09, 0x0A, 0x0B, 0x0C}; + final byte[] ba = {0x0A}; + final byte[] bb = {0x0B}; + final byte[] bc = {0x0C}; + final byte[] bd = {0x0D}; + final byte[] bx = {0x42}; + + @Test public void sadd() { int status = jedis.sadd("foo", "a"); @@ -13,6 +26,13 @@ public class SetCommandsTest extends JedisCommandTestBase { status = jedis.sadd("foo", "a"); assertEquals(0, status); + + int bstatus = jedis.sadd(bfoo, ba); + assertEquals(1, bstatus); + + bstatus = jedis.sadd(bfoo, ba); + assertEquals(0, bstatus); + } @Test @@ -27,7 +47,20 @@ public class SetCommandsTest extends JedisCommandTestBase { Set members = jedis.smembers("foo"); assertEquals(expected, members); + + //Binary + jedis.sadd(bfoo, ba); + jedis.sadd(bfoo, bb); + + Set bexpected = new LinkedHashSet(); + bexpected.add(ba); + bexpected.add(bb); + + Set bmembers = jedis.smembers(bfoo); + + assertTrue(JedisTest.isListAreEquals(bexpected, bmembers)); } + @Test public void srem() { @@ -45,6 +78,24 @@ public class SetCommandsTest extends JedisCommandTestBase { status = jedis.srem("foo", "bar"); assertEquals(0, status); + + //Binary + + jedis.sadd(bfoo, ba); + jedis.sadd(bfoo, bb); + + int bstatus = jedis.srem(bfoo, ba); + + Set bexpected = new LinkedHashSet(); + bexpected.add(bb); + + assertEquals(1, bstatus); + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.smembers(bfoo))); + + bstatus = jedis.srem(bfoo, bbar); + + assertEquals(0, bstatus); + } @Test @@ -59,6 +110,19 @@ public class SetCommandsTest extends JedisCommandTestBase { member = jedis.spop("bar"); assertNull(member); + + //Binary + jedis.sadd(bfoo, ba); + jedis.sadd(bfoo, bb); + + byte[] bmember = jedis.spop(bfoo); + + assertTrue(Arrays.equals(ba, bmember) || Arrays.equals(bb,bmember)); + assertEquals(1, jedis.smembers(bfoo).size()); + + bmember = jedis.spop(bbar); + assertNull(bmember); + } @Test @@ -81,8 +145,32 @@ public class SetCommandsTest extends JedisCommandTestBase { assertEquals(expectedSrc, jedis.smembers("foo")); assertEquals(expectedDst, jedis.smembers("bar")); - status = jedis.smove("foo", "bar", "a"); + status = jedis.smove("foo", "bar", "a"); final byte[] bD = {0x0D}; + assertEquals(status, 0); + + //Binary + jedis.sadd(bfoo, ba); + jedis.sadd(bfoo, bb); + + jedis.sadd(bbar, bc); + + int bstatus = jedis.smove(bfoo, bbar, ba); + + Set bexpectedSrc = new LinkedHashSet(); + bexpectedSrc.add(bb); + + Set bexpectedDst = new LinkedHashSet(); + bexpectedDst.add(bc); + bexpectedDst.add(ba); + + assertEquals(bstatus, 1); + assertTrue(JedisTest.isListAreEquals(bexpectedSrc, jedis.smembers(bfoo))); + assertTrue(JedisTest.isListAreEquals(bexpectedDst, jedis.smembers(bbar))); + + bstatus = jedis.smove(bfoo, bbar, ba); + assertEquals(bstatus, 0); + } @Test @@ -96,6 +184,18 @@ public class SetCommandsTest extends JedisCommandTestBase { card = jedis.scard("bar"); assertEquals(0, card); + + //Binary + jedis.sadd(bfoo, ba); + jedis.sadd(bfoo, bb); + + int bcard = jedis.scard(bfoo); + + assertEquals(2, bcard); + + bcard = jedis.scard(bbar); + assertEquals(0, bcard); + } @Test @@ -108,6 +208,17 @@ public class SetCommandsTest extends JedisCommandTestBase { status = jedis.sismember("foo", "c"); assertEquals(0, status); + + //Binary + jedis.sadd(bfoo, ba); + jedis.sadd(bfoo, bb); + + int bstatus = jedis.sismember(bfoo, ba); + assertEquals(1, bstatus); + + bstatus = jedis.sismember(bfoo, bc); + assertEquals(0, bstatus); + } @Test @@ -123,6 +234,20 @@ public class SetCommandsTest extends JedisCommandTestBase { Set intersection = jedis.sinter("foo", "bar"); assertEquals(expected, intersection); + + //Binary + jedis.sadd(bfoo, ba); + jedis.sadd(bfoo, bb); + + jedis.sadd(bbar, bb); + jedis.sadd(bbar, bc); + + Set bexpected = new LinkedHashSet(); + bexpected.add(bb); + + Set bintersection = jedis.sinter(bfoo, bbar); + assertTrue(JedisTest.isListAreEquals(bexpected, bintersection)); + } @Test @@ -140,6 +265,22 @@ public class SetCommandsTest extends JedisCommandTestBase { assertEquals(1, status); assertEquals(expected, jedis.smembers("car")); + + //Binary + jedis.sadd(bfoo, ba); + jedis.sadd(bfoo, bb); + + jedis.sadd(bbar, bb); + jedis.sadd(bbar, bc); + + Set bexpected = new LinkedHashSet(); + bexpected.add(bb); + + int bstatus = jedis.sinterstore(bcar, bfoo, bbar); + assertEquals(1, bstatus); + + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.smembers(bcar))); + } @Test @@ -157,6 +298,22 @@ public class SetCommandsTest extends JedisCommandTestBase { Set union = jedis.sunion("foo", "bar"); assertEquals(expected, union); + + //Binary + jedis.sadd(bfoo, ba); + jedis.sadd(bfoo, bb); + + jedis.sadd(bbar, bb); + jedis.sadd(bbar, bc); + + Set bexpected = new LinkedHashSet(); + bexpected.add(ba); + bexpected.add(bb); + bexpected.add(bc); + + Set bunion = jedis.sunion(bfoo, bbar); + assertTrue(JedisTest.isListAreEquals(bexpected, bunion)); + } @Test @@ -176,6 +333,24 @@ public class SetCommandsTest extends JedisCommandTestBase { assertEquals(3, status); assertEquals(expected, jedis.smembers("car")); + + //Binary + jedis.sadd(bfoo, ba); + jedis.sadd(bfoo, bb); + + jedis.sadd(bbar, bb); + jedis.sadd(bbar, bc); + + Set bexpected = new LinkedHashSet(); + bexpected.add(ba); + bexpected.add(bb); + bexpected.add(bc); + + int bstatus = jedis.sunionstore(bcar, bfoo, bbar); + assertEquals(3, bstatus); + + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.smembers(bcar))); + } @Test @@ -196,6 +371,25 @@ public class SetCommandsTest extends JedisCommandTestBase { Set diff = jedis.sdiff("foo", "bar", "car"); assertEquals(expected, diff); + + //Binary + jedis.sadd(bfoo, bx); + jedis.sadd(bfoo, ba); + jedis.sadd(bfoo, bb); + jedis.sadd(bfoo, bc); + + jedis.sadd(bbar, bc); + + jedis.sadd(bcar, ba); + jedis.sadd(bcar, bd); + + Set bexpected = new LinkedHashSet(); + bexpected.add(bx); + bexpected.add(bb); + + Set bdiff = jedis.sdiff(bfoo, bbar, bcar); + assertTrue(JedisTest.isListAreEquals(bexpected, bdiff)); + } @Test @@ -217,6 +411,26 @@ public class SetCommandsTest extends JedisCommandTestBase { int status = jedis.sdiffstore("tar", "foo", "bar", "car"); assertEquals(2, status); assertEquals(expected, jedis.smembers("car")); + + //Binary + jedis.sadd(bfoo, bx); + jedis.sadd(bfoo, ba); + jedis.sadd(bfoo, bb); + jedis.sadd(bfoo, bc); + + jedis.sadd(bbar, bc); + + jedis.sadd(bcar, ba); + jedis.sadd(bcar, bd); + + Set bexpected = new LinkedHashSet(); + bexpected.add(bd); + bexpected.add(ba); + + int bstatus = jedis.sdiffstore("tar".getBytes(), bfoo, bbar, bcar); + assertEquals(2, bstatus); + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.smembers(bcar))); + } @Test @@ -231,6 +445,19 @@ public class SetCommandsTest extends JedisCommandTestBase { member = jedis.srandmember("bar"); assertNull(member); + + //Binary + jedis.sadd(bfoo, ba); + jedis.sadd(bfoo, bb); + + byte[] bmember = jedis.srandmember(bfoo); + + assertTrue(Arrays.equals(ba, bmember) || Arrays.equals(bb, bmember)); + assertEquals(2, jedis.smembers(bfoo).size()); + + bmember = jedis.srandmember(bbar); + assertNull(bmember); + } } \ No newline at end of file From 0beeddce5db2fa6e9e5dc39eeb5d23d46b2ea775 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 5 Nov 2010 18:16:16 +0100 Subject: [PATCH 33/40] Binary U test on SortedSets ... --- .../java/redis/clients/jedis/BinaryJedis.java | 23 +- src/main/java/redis/clients/jedis/Tuple.java | 35 +- .../jedis/tests/commands/SetCommandsTest.java | 2 +- .../tests/commands/SortedSetCommandsTest.java | 353 ++++++++++++++++++ 4 files changed, 397 insertions(+), 16 deletions(-) diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index 9d2c315..c6a0cdb 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -1574,14 +1574,14 @@ public class BinaryJedis implements BinaryJedisCommands { public Set zrangeWithScores(final byte[] key, final int start, final int end) { checkIsInMulti(); client.zrangeWithScores(key, start, end); - Set set = getTupledSet(); + Set set = getBinaryTupledSet(); return set; } public Set zrevrangeWithScores(final byte[] key, final int start, final int end) { checkIsInMulti(); client.zrevrangeWithScores(key, start, end); - Set set = getTupledSet(); + Set set = getBinaryTupledSet(); return set; } @@ -2225,7 +2225,7 @@ public class BinaryJedis implements BinaryJedisCommands { public Set zrangeByScoreWithScores(final byte[] key, final double min, final double max) { checkIsInMulti(); client.zrangeByScoreWithScores(key, min, max); - Set set = getTupledSet(); + Set set = getBinaryTupledSet(); return set; } @@ -2289,19 +2289,22 @@ public class BinaryJedis implements BinaryJedisCommands { final double max, final int offset, final int count) { checkIsInMulti(); client.zrangeByScoreWithScores(key, min, max, offset, count); - Set set = getTupledSet(); + Set set = getBinaryTupledSet(); return set; } - private Set getTupledSet() { + private Set getBinaryTupledSet() { checkIsInMulti(); - List membersWithScores = client.getMultiBulkReply(); + List membersWithScores = client.getBinaryMultiBulkReply(); Set set = new LinkedHashSet(); - Iterator iterator = membersWithScores.iterator(); + Iterator iterator = membersWithScores.iterator(); while (iterator.hasNext()) { - set - .add(new Tuple(iterator.next(), Double.valueOf(iterator - .next()))); + set.add( + new Tuple( + iterator.next(), + Double.valueOf(new String(iterator.next(), Protocol.UTF8)) + ) + ); } return set; } diff --git a/src/main/java/redis/clients/jedis/Tuple.java b/src/main/java/redis/clients/jedis/Tuple.java index 30e4e53..425748d 100644 --- a/src/main/java/redis/clients/jedis/Tuple.java +++ b/src/main/java/redis/clients/jedis/Tuple.java @@ -1,14 +1,21 @@ package redis.clients.jedis; +import java.util.Arrays; + public class Tuple { - private String element; + private byte[] element; private Double score; @Override public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + ((element == null) ? 0 : element.hashCode()); + result = prime * result; + if (null != element) { + for(final byte b : element) { + result = prime * result + b; + } + } long temp; temp = Double.doubleToLongBits(score); result = prime * result + (int) (temp ^ (temp >>> 32)); @@ -27,7 +34,7 @@ public class Tuple { if (element == null) { if (other.element != null) return false; - } else if (!element.equals(other.element)) + } else if (!Arrays.equals(element, other.element)) return false; if (Double.doubleToLongBits(score) != Double .doubleToLongBits(other.score)) @@ -37,15 +44,33 @@ public class Tuple { public Tuple(String element, Double score) { super(); - this.element = element; + this.element = element.getBytes(Protocol.UTF8); this.score = score; } + public Tuple(byte[] element, Double score) { + super(); + this.element = element; + this.score = score; + } + public String getElement() { - return element; + if(null != element) { + return new String(element, Protocol.UTF8); + } else { + return null; + } + } + + public byte[] getBinaryElement() { + return element; } public double getScore() { return score; } + + public String toString() { + return '['+Arrays.toString(element)+','+score+']'; + } } diff --git a/src/test/java/redis/clients/jedis/tests/commands/SetCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/SetCommandsTest.java index f5c2c2e..9d41a9d 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/SetCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/SetCommandsTest.java @@ -145,7 +145,7 @@ public class SetCommandsTest extends JedisCommandTestBase { assertEquals(expectedSrc, jedis.smembers("foo")); assertEquals(expectedDst, jedis.smembers("bar")); - status = jedis.smove("foo", "bar", "a"); final byte[] bD = {0x0D}; + status = jedis.smove("foo", "bar", "a"); assertEquals(status, 0); diff --git a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java index 6bb404f..6a6b0c5 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java @@ -5,10 +5,19 @@ import java.util.Set; import org.junit.Test; +import redis.clients.jedis.Protocol; import redis.clients.jedis.Tuple; import redis.clients.jedis.ZParams; +import redis.clients.jedis.tests.JedisTest; public class SortedSetCommandsTest extends JedisCommandTestBase { + final byte[] bfoo = {0x01, 0x02, 0x03, 0x04}; + final byte[] bbar = {0x05, 0x06, 0x07, 0x08}; + final byte[] bcar = {0x09, 0x0A, 0x0B, 0x0C}; + final byte[] ba = {0x0A}; + final byte[] bb = {0x0B}; + final byte[] bc = {0x0C}; + @Test public void zadd() { int status = jedis.zadd("foo", 1d, "a"); @@ -22,6 +31,20 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { status = jedis.zadd("foo", 2d, "a"); assertEquals(0, status); + + //Binary + int bstatus = jedis.zadd(bfoo, 1d, ba); + assertEquals(1, bstatus); + + bstatus = jedis.zadd(bfoo, 10d, bb); + assertEquals(1, bstatus); + + bstatus = jedis.zadd(bfoo, 0.1d, bc); + assertEquals(1, bstatus); + + bstatus = jedis.zadd(bfoo, 2d, ba); + assertEquals(0, bstatus); + } @Test @@ -41,6 +64,24 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { expected.add("b"); range = jedis.zrange("foo", 0, 100); assertEquals(expected, range); + + //Binary + jedis.zadd(bfoo, 1d, ba); + jedis.zadd(bfoo, 10d, bb); + jedis.zadd(bfoo, 0.1d, bc); + jedis.zadd(bfoo, 2d, ba); + + Set bexpected = new LinkedHashSet(); + bexpected.add(bc); + bexpected.add(ba); + + Set brange = jedis.zrange(bfoo, 0, 1); + assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + + bexpected.add(bb); + brange = jedis.zrange(bfoo, 0, 100); + assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + } @Test @@ -60,6 +101,24 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { expected.add("c"); range = jedis.zrevrange("foo", 0, 100); assertEquals(expected, range); + + //Binary + jedis.zadd(bfoo, 1d, ba); + jedis.zadd(bfoo, 10d, bb); + jedis.zadd(bfoo, 0.1d, bc); + jedis.zadd(bfoo, 2d, ba); + + Set bexpected = new LinkedHashSet(); + bexpected.add(bb); + bexpected.add(ba); + + Set brange = jedis.zrevrange(bfoo, 0, 1); + assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + + bexpected.add(bc); + brange = jedis.zrevrange(bfoo, 0, 100); + assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + } @Test @@ -78,6 +137,23 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { status = jedis.zrem("foo", "bar"); assertEquals(0, status); + + //Binary + jedis.zadd(bfoo, 1d, ba); + jedis.zadd(bfoo, 2d, bb); + + int bstatus = jedis.zrem(bfoo, ba); + + Set bexpected = new LinkedHashSet(); + bexpected.add(bb); + + assertEquals(1, bstatus); + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrange(bfoo, 0, 100))); + + bstatus = jedis.zrem(bfoo, bbar); + + assertEquals(0, bstatus); + } @Test @@ -93,6 +169,20 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { assertEquals(3d, score, 0); assertEquals(expected, jedis.zrange("foo", 0, 100)); + + //Binary + jedis.zadd(bfoo, 1d, ba); + jedis.zadd(bfoo, 2d, bb); + + double bscore = jedis.zincrby(bfoo, 2d, ba); + + Set bexpected = new LinkedHashSet(); + bexpected.add(ba); + bexpected.add(bb); + + assertEquals(3d, bscore, 0); + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrange(bfoo, 0, 100))); + } @Test @@ -108,6 +198,20 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { rank = jedis.zrank("car", "b"); assertNull(rank); + + //Binary + jedis.zadd(bfoo, 1d, ba); + jedis.zadd(bfoo, 2d, bb); + + Integer brank = jedis.zrank(bfoo, ba); + assertEquals(0, brank.intValue()); + + brank = jedis.zrank(bfoo, bb); + assertEquals(1, brank.intValue()); + + brank = jedis.zrank(bcar, bb); + assertNull(brank); + } @Test @@ -120,6 +224,17 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { rank = jedis.zrevrank("foo", "b"); assertEquals(0, rank); + + //Binary + jedis.zadd(bfoo, 1d, ba); + jedis.zadd(bfoo, 2d, bb); + + int brank = jedis.zrevrank(bfoo, ba); + assertEquals(1, brank); + + brank = jedis.zrevrank(bfoo, bb); + assertEquals(0, brank); + } @Test @@ -139,6 +254,24 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { expected.add(new Tuple("b", 10d)); range = jedis.zrangeWithScores("foo", 0, 100); assertEquals(expected, range); + + //Binary + jedis.zadd(bfoo, 1d, ba); + jedis.zadd(bfoo, 10d, bb); + jedis.zadd(bfoo, 0.1d, bc); + jedis.zadd(bfoo, 2d, ba); + + Set bexpected = new LinkedHashSet(); + bexpected.add(new Tuple(bc, 0.1d)); + bexpected.add(new Tuple(ba, 2d)); + + Set brange = jedis.zrangeWithScores(bfoo, 0, 1); + assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + + bexpected.add(new Tuple(bb, 10d)); + brange = jedis.zrangeWithScores(bfoo, 0, 100); + assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + } @Test @@ -158,6 +291,24 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { expected.add(new Tuple("c", 0.1d)); range = jedis.zrevrangeWithScores("foo", 0, 100); assertEquals(expected, range); + + //Binary + jedis.zadd(bfoo, 1d, ba); + jedis.zadd(bfoo, 10d, bb); + jedis.zadd(bfoo, 0.1d, bc); + jedis.zadd(bfoo, 2d, ba); + + Set bexpected = new LinkedHashSet(); + bexpected.add(new Tuple(bb, 10d)); + bexpected.add(new Tuple(ba, 2d)); + + Set brange = jedis.zrevrangeWithScores(bfoo, 0, 1); + assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + + bexpected.add(new Tuple(bc, 0.1d)); + brange = jedis.zrevrangeWithScores(bfoo, 0, 100); + assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + } @Test @@ -169,6 +320,16 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { int size = jedis.zcard("foo"); assertEquals(3, size); + + //Binary + jedis.zadd(bfoo, 1d, ba); + jedis.zadd(bfoo, 10d, bb); + jedis.zadd(bfoo, 0.1d, bc); + jedis.zadd(bfoo, 2d, ba); + + int bsize = jedis.zcard(bfoo); + assertEquals(3, bsize); + } @Test @@ -186,6 +347,22 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { score = jedis.zscore("foo", "s"); assertNull(score); + + //Binary + jedis.zadd(bfoo, 1d, ba); + jedis.zadd(bfoo, 10d, bb); + jedis.zadd(bfoo, 0.1d, bc); + jedis.zadd(bfoo, 2d, ba); + + Double bscore = jedis.zscore(bfoo, bb); + assertEquals((Double)10d, bscore); + + bscore = jedis.zscore(bfoo, bc); + assertEquals((Double)0.1d, bscore); + + bscore = jedis.zscore(bfoo, "s".getBytes(Protocol.UTF8)); + assertNull(bscore); + } @Test @@ -198,6 +375,17 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { int result = jedis.zcount("foo", 0.01d, 2.1d); assertEquals(2, result); + + //Binary + jedis.zadd(bfoo, 1d, ba); + jedis.zadd(bfoo, 10d, bb); + jedis.zadd(bfoo, 0.1d, bc); + jedis.zadd(bfoo, 2d, ba); + + int bresult = jedis.zcount(bfoo, 0.01d, 2.1d); + + assertEquals(2, bresult); + } @Test @@ -230,6 +418,37 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { expected.add("a"); assertEquals(expected, range); + + //Binary + jedis.zadd(bfoo, 1d, ba); + jedis.zadd(bfoo, 10d, bb); + jedis.zadd(bfoo, 0.1d,bc); + jedis.zadd(bfoo, 2d, ba); + + Set brange = jedis.zrangeByScore(bfoo, 0d, 2d); + + Set bexpected = new LinkedHashSet(); + bexpected.add(bc); + bexpected.add(ba); + + assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + + brange = jedis.zrangeByScore(bfoo, 0d, 2d, 0, 1); + + bexpected = new LinkedHashSet(); + bexpected.add(bc); + + assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + + brange = jedis.zrangeByScore(bfoo, 0d, 2d, 1, 1); + Set brange2 = jedis.zrangeByScore(bfoo, "-inf".getBytes(Protocol.UTF8), "(2".getBytes(Protocol.UTF8)); + assertTrue(JedisTest.isListAreEquals(bexpected, brange2)); + + bexpected = new LinkedHashSet(); + bexpected.add(ba); + + assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + } @Test @@ -260,6 +479,36 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { expected.add(new Tuple("a", 2d)); assertEquals(expected, range); + + //Binary + + jedis.zadd(bfoo, 1d, ba); + jedis.zadd(bfoo, 10d, bb); + jedis.zadd(bfoo, 0.1d, bc); + jedis.zadd(bfoo, 2d, ba); + + Set brange = jedis.zrangeByScoreWithScores(bfoo, 0d, 2d); + + Set bexpected = new LinkedHashSet(); + bexpected.add(new Tuple(bc, 0.1d)); + bexpected.add(new Tuple(ba, 2d)); + + assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + + brange = jedis.zrangeByScoreWithScores(bfoo, 0d, 2d, 0, 1); + + bexpected = new LinkedHashSet(); + bexpected.add(new Tuple(bc, 0.1d)); + + assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + + brange = jedis.zrangeByScoreWithScores(bfoo, 0d, 2d, 1, 1); + + bexpected = new LinkedHashSet(); + bexpected.add(new Tuple(ba, 2d)); + + assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + } @Test @@ -278,6 +527,23 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { expected.add("b"); assertEquals(expected, jedis.zrange("foo", 0, 100)); + + //Binary + jedis.zadd(bfoo, 1d, ba); + jedis.zadd(bfoo, 10d, bb); + jedis.zadd(bfoo, 0.1d, bc); + jedis.zadd(bfoo, 2d, ba); + + int bresult = jedis.zremrangeByRank(bfoo, 0, 0); + + assertEquals(1, bresult); + + Set bexpected = new LinkedHashSet(); + bexpected.add(ba); + bexpected.add(bb); + + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrange(bfoo, 0, 100))); + } @Test @@ -295,6 +561,22 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { expected.add("b"); assertEquals(expected, jedis.zrange("foo", 0, 100)); + + //Binary + jedis.zadd(bfoo, 1d, ba); + jedis.zadd(bfoo, 10d, bb); + jedis.zadd(bfoo, 0.1d,bc); + jedis.zadd(bfoo, 2d, ba); + + int bresult = jedis.zremrangeByScore(bfoo, 0, 2); + + assertEquals(2, bresult); + + Set bexpected = new LinkedHashSet(); + bexpected.add(bb); + + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrange(bfoo, 0, 100))); + } @Test @@ -313,6 +595,23 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { expected.add(new Tuple("a", new Double(3))); assertEquals(expected, jedis.zrangeWithScores("dst", 0, 100)); + + //Binary + jedis.zadd(bfoo, 1, ba); + jedis.zadd(bfoo, 2, bb); + jedis.zadd(bbar, 2, ba); + jedis.zadd(bbar, 2, bb); + + int bresult = jedis.zunionstore("dst".getBytes(Protocol.UTF8), bfoo, bbar); + + assertEquals(2, bresult); + + Set bexpected = new LinkedHashSet(); + bexpected.add(new Tuple(bb, new Double(4))); + bexpected.add(new Tuple(ba, new Double(3))); + + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrangeWithScores("dst".getBytes(Protocol.UTF8), 0, 100))); + } @Test @@ -334,6 +633,26 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { expected.add(new Tuple("a", new Double(6))); assertEquals(expected, jedis.zrangeWithScores("dst", 0, 100)); + + //Binary + jedis.zadd(bfoo, 1, ba); + jedis.zadd(bfoo, 2, bb); + jedis.zadd(bbar, 2, ba); + jedis.zadd(bbar, 2, bb); + + ZParams bparams = new ZParams(); + bparams.weights(2, 2); + bparams.aggregate(ZParams.Aggregate.SUM); + int bresult = jedis.zunionstore("dst".getBytes(Protocol.UTF8), bparams, bfoo, bbar); + + assertEquals(2, bresult); + + Set bexpected = new LinkedHashSet(); + bexpected.add(new Tuple(bb, new Double(8))); + bexpected.add(new Tuple(ba, new Double(6))); + + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrangeWithScores("dst".getBytes(Protocol.UTF8), 0, 100))); + } @Test @@ -350,7 +669,23 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { expected.add(new Tuple("a", new Double(3))); assertEquals(expected, jedis.zrangeWithScores("dst", 0, 100)); + + //Binary + jedis.zadd(bfoo, 1, ba); + jedis.zadd(bfoo, 2, bb); + jedis.zadd(bbar, 2, ba); + + int bresult = jedis.zinterstore("dst".getBytes(Protocol.UTF8), bfoo, bbar); + + assertEquals(1, bresult); + + Set bexpected = new LinkedHashSet(); + bexpected.add(new Tuple(ba, new Double(3))); + + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrangeWithScores("dst".getBytes(Protocol.UTF8), 0, 100))); + } + @Test public void zintertoreParams() { @@ -369,5 +704,23 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { expected.add(new Tuple("a", new Double(6))); assertEquals(expected, jedis.zrangeWithScores("dst", 0, 100)); + + //Binary + jedis.zadd(bfoo, 1, ba); + jedis.zadd(bfoo, 2, bb); + jedis.zadd(bbar, 2, ba); + + ZParams bparams = new ZParams(); + bparams.weights(2, 2); + bparams.aggregate(ZParams.Aggregate.SUM); + int bresult = jedis.zinterstore("dst".getBytes(Protocol.UTF8), bparams, bfoo, bbar); + + assertEquals(1, bresult); + + Set bexpected = new LinkedHashSet(); + bexpected.add(new Tuple(ba, new Double(6))); + + assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrangeWithScores("dst".getBytes(Protocol.UTF8), 0, 100))); + } } \ No newline at end of file From 424512d4e655c72b7982d136e37df8c5b28dbded Mon Sep 17 00:00:00 2001 From: Yaourt Date: Sun, 7 Nov 2010 10:48:23 +0100 Subject: [PATCH 34/40] keys command now return a set (instead of a list). The assumption that keys are ordered seems to be false,on my laptop, with the keys 'foo' and 'foobar', keys('foo*') returns ['foobar', 'foo'] --- src/main/java/redis/clients/jedis/BinaryJedis.java | 6 ++++-- src/main/java/redis/clients/jedis/Jedis.java | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index c6a0cdb..93056a6 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; @@ -177,10 +178,11 @@ public class BinaryJedis implements BinaryJedisCommands { * @param pattern * @return Multi bulk reply */ - public List keys(final byte[] pattern) { + public Set keys(final byte[] pattern) { checkIsInMulti(); client.keys(pattern); - return client.getBinaryMultiBulkReply(); + final HashSet keySet = new HashSet(client.getBinaryMultiBulkReply()); + return keySet; } /** diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index 4c95282..ee76869 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -2,6 +2,7 @@ package redis.clients.jedis; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; @@ -142,10 +143,11 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @param pattern * @return Multi bulk reply */ - public List keys(final String pattern) { + public Set keys(final String pattern) { checkIsInMulti(); client.keys(pattern); - return client.getMultiBulkReply(); + final HashSet keySet = new HashSet( client.getMultiBulkReply()); + return keySet; } /** From 3281bafd2e292591646022b2bb9e328352fd8790 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Sun, 7 Nov 2010 10:49:45 +0100 Subject: [PATCH 35/40] Fix wrong test on lists / sets. Lists comparison now take care of the order, which is not the case for Sets. --- .../redis/clients/jedis/tests/JedisTest.java | 66 +++++++++++++++++-- .../commands/AllKindOfValuesCommandsTest.java | 28 ++++---- .../tests/commands/HashesCommandsTest.java | 14 ++-- .../jedis/tests/commands/SetCommandsTest.java | 20 +++--- .../tests/commands/SortedSetCommandsTest.java | 46 ++++++------- 5 files changed, 118 insertions(+), 56 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index 57db5b3..f968b0c 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -6,6 +6,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import javax.swing.tree.ExpandVetoException; @@ -49,7 +50,7 @@ public class JedisTest extends JedisCommandTestBase { } @SuppressWarnings("rawtypes") - public static boolean isListAreEquals(Collection expected, Collection result) { + public static boolean isSetAreEquals(Set expected, Set result) { if(expected.size() != result.size()) { return false; } @@ -67,9 +68,15 @@ public class JedisTest extends JedisCommandTestBase { if(Arrays.equals(bexp, bresp)) { found = true; } - } else if (exp instanceof Collection && resp instanceof Collection) { - final Collection subexp = (Collection) exp; - final Collection subresp = (Collection) resp; + } else if (exp instanceof Set && resp instanceof Set) { + final Set subexp = (Set) exp; + final Set subresp = (Set) resp; + if(isSetAreEquals(subexp, subresp)) { + found = true; + } + } else if (exp instanceof List && resp instanceof List) { + final List subexp = (List) exp; + final List subresp = (List) resp; if(isListAreEquals(subexp, subresp)) { found = true; } @@ -93,7 +100,56 @@ public class JedisTest extends JedisCommandTestBase { return true; } -// public static boolean isArraysAreEqualsPlop(final byte[] expected, final byte[] result) { + @SuppressWarnings("rawtypes") + public static boolean isListAreEquals(List expected, List result) { + if(expected.size() != result.size()) { + return false; + } + + final Iterator expectedit = expected.iterator(); + final Iterator responseit = result.iterator(); + int index = -1; + while(expectedit.hasNext() && responseit.hasNext()) { + index++; + final Object exp = expectedit.next(); + final Object resp = responseit.next(); + if(exp instanceof byte[] && resp instanceof byte[]) { + final byte[] bexp = (byte[]) exp; + final byte[] bresp = (byte[]) resp; + if(Arrays.equals(bexp, bresp)) { + continue; + } + } else if (exp instanceof Set && resp instanceof Set) { + final Set subexp = (Set) exp; + final Set subresp = (Set) resp; + if(isSetAreEquals(subexp, subresp)) { + continue; + } + } else if (exp instanceof List && resp instanceof List) { + final List subexp = (List) exp; + final List subresp = (List) resp; + if(isListAreEquals(subexp, subresp)) { + continue; + } + } else { + if (null != exp) { + if (exp.equals(resp)){ + continue; + } + } else { + if(resp == null) { + continue; + } + } + } + + fail("Element at index "+index+" differs. Expecting "+(null == exp ? null : exp.toString())+" but was "+(resp == null ? null : resp.toString())); + } + + return true; + } + + // public static boolean isArraysAreEqualsPlop(final byte[] expected, final byte[] result) { // if(expected.length != result.length) { // return false; // } diff --git a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java index 2fc058c..bc2a941 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java @@ -2,7 +2,9 @@ package redis.clients.jedis.tests.commands; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.junit.Test; @@ -21,6 +23,8 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { final byte[] bbar3 = {0x05, 0x06, 0x07, 0x08, 0x0C}; final byte[] bfoobar = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + final byte[] bfoostar = {0x01, 0x02, 0x03, 0x04, '*'}; + final byte[] bbarstar = {0x05, 0x06, 0x07, 0x08, '*'}; @Test public void ping() { @@ -120,13 +124,13 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { jedis.set("foo", "bar"); jedis.set("foobar", "bar"); - List keys = jedis.keys("foo*"); - List expected = new ArrayList(); + Set keys = jedis.keys("foo*"); + Set expected = new HashSet(); expected.add("foo"); expected.add("foobar"); assertEquals(expected, keys); - expected = new ArrayList(); + expected = new HashSet(); keys = jedis.keys("bar*"); assertEquals(expected, keys); @@ -135,20 +139,20 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { jedis.set(bfoo, bbar); jedis.set(bfoobar, bbar); - byte[] bfoostar = new byte[bfoo.length+1]; - System.arraycopy(bfoo, 0, bfoostar, 0, bfoo.length); - bfoostar[bfoostar.length-1] = '*'; - List bkeys = jedis.keys(bfoostar); - List bexpected = new ArrayList(); +// byte[] bfoostar = new byte[bfoo.length+1]; +// System.arraycopy(bfoo, 0, bfoostar, 0, bfoo.length); +// bfoostar[bfoostar.length-1] = '*'; + Set bkeys = jedis.keys(bfoostar); + Set bexpected = new HashSet(); bexpected.add(bfoo); bexpected.add(bfoobar); - assertTrue(JedisTest.isListAreEquals(bexpected, bkeys)); + assertTrue(JedisTest.isSetAreEquals(bexpected, bkeys)); // assertEquals(expected, keys); - expected = new ArrayList(); - keys = jedis.keys("bar*"); + bexpected = new HashSet(); + bkeys = jedis.keys(bbarstar); - assertEquals(expected, keys); + assertTrue(JedisTest.isSetAreEquals(bexpected, bkeys)); } diff --git a/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java index 9dd3a72..0dd6dda 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java @@ -3,9 +3,11 @@ package redis.clients.jedis.tests.commands; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Set; import org.junit.Test; @@ -291,12 +293,12 @@ public class HashesCommandsTest extends JedisCommandTestBase { Map bexpected = new HashMap(); bexpected.put(bbar, bcar); bexpected.put(bcar, bbar); - final Collection keysetExpected = bexpected.keySet(); - final Collection keysetResult = bhash.keySet(); - assertTrue(JedisTest.isListAreEquals(keysetExpected, keysetResult)); - final Collection valsExpected = bexpected.values(); - final Collection valsResult = bhash.values(); - assertTrue(JedisTest.isListAreEquals(valsExpected, valsResult)); + final Set keysetExpected = bexpected.keySet(); + final Set keysetResult = bhash.keySet(); + assertTrue(JedisTest.isSetAreEquals(keysetExpected, keysetResult)); + final Set valsExpected = new HashSet(bexpected.values()); + final Set valsResult = new HashSet(bhash.values()); + assertTrue(JedisTest.isSetAreEquals(valsExpected, valsResult)); } } diff --git a/src/test/java/redis/clients/jedis/tests/commands/SetCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/SetCommandsTest.java index 9d41a9d..23d6f39 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/SetCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/SetCommandsTest.java @@ -58,7 +58,7 @@ public class SetCommandsTest extends JedisCommandTestBase { Set bmembers = jedis.smembers(bfoo); - assertTrue(JedisTest.isListAreEquals(bexpected, bmembers)); + assertTrue(JedisTest.isSetAreEquals(bexpected, bmembers)); } @@ -90,7 +90,7 @@ public class SetCommandsTest extends JedisCommandTestBase { bexpected.add(bb); assertEquals(1, bstatus); - assertTrue(JedisTest.isListAreEquals(bexpected, jedis.smembers(bfoo))); + assertTrue(JedisTest.isSetAreEquals(bexpected, jedis.smembers(bfoo))); bstatus = jedis.srem(bfoo, bbar); @@ -165,8 +165,8 @@ public class SetCommandsTest extends JedisCommandTestBase { bexpectedDst.add(ba); assertEquals(bstatus, 1); - assertTrue(JedisTest.isListAreEquals(bexpectedSrc, jedis.smembers(bfoo))); - assertTrue(JedisTest.isListAreEquals(bexpectedDst, jedis.smembers(bbar))); + assertTrue(JedisTest.isSetAreEquals(bexpectedSrc, jedis.smembers(bfoo))); + assertTrue(JedisTest.isSetAreEquals(bexpectedDst, jedis.smembers(bbar))); bstatus = jedis.smove(bfoo, bbar, ba); assertEquals(bstatus, 0); @@ -246,7 +246,7 @@ public class SetCommandsTest extends JedisCommandTestBase { bexpected.add(bb); Set bintersection = jedis.sinter(bfoo, bbar); - assertTrue(JedisTest.isListAreEquals(bexpected, bintersection)); + assertTrue(JedisTest.isSetAreEquals(bexpected, bintersection)); } @@ -279,7 +279,7 @@ public class SetCommandsTest extends JedisCommandTestBase { int bstatus = jedis.sinterstore(bcar, bfoo, bbar); assertEquals(1, bstatus); - assertTrue(JedisTest.isListAreEquals(bexpected, jedis.smembers(bcar))); + assertTrue(JedisTest.isSetAreEquals(bexpected, jedis.smembers(bcar))); } @@ -312,7 +312,7 @@ public class SetCommandsTest extends JedisCommandTestBase { bexpected.add(bc); Set bunion = jedis.sunion(bfoo, bbar); - assertTrue(JedisTest.isListAreEquals(bexpected, bunion)); + assertTrue(JedisTest.isSetAreEquals(bexpected, bunion)); } @@ -349,7 +349,7 @@ public class SetCommandsTest extends JedisCommandTestBase { int bstatus = jedis.sunionstore(bcar, bfoo, bbar); assertEquals(3, bstatus); - assertTrue(JedisTest.isListAreEquals(bexpected, jedis.smembers(bcar))); + assertTrue(JedisTest.isSetAreEquals(bexpected, jedis.smembers(bcar))); } @@ -388,7 +388,7 @@ public class SetCommandsTest extends JedisCommandTestBase { bexpected.add(bb); Set bdiff = jedis.sdiff(bfoo, bbar, bcar); - assertTrue(JedisTest.isListAreEquals(bexpected, bdiff)); + assertTrue(JedisTest.isSetAreEquals(bexpected, bdiff)); } @@ -429,7 +429,7 @@ public class SetCommandsTest extends JedisCommandTestBase { int bstatus = jedis.sdiffstore("tar".getBytes(), bfoo, bbar, bcar); assertEquals(2, bstatus); - assertTrue(JedisTest.isListAreEquals(bexpected, jedis.smembers(bcar))); + assertTrue(JedisTest.isSetAreEquals(bexpected, jedis.smembers(bcar))); } diff --git a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java index 6a6b0c5..a56f220 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java @@ -76,11 +76,11 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { bexpected.add(ba); Set brange = jedis.zrange(bfoo, 0, 1); - assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + assertTrue(JedisTest.isSetAreEquals(bexpected, brange)); bexpected.add(bb); brange = jedis.zrange(bfoo, 0, 100); - assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + assertTrue(JedisTest.isSetAreEquals(bexpected, brange)); } @@ -113,11 +113,11 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { bexpected.add(ba); Set brange = jedis.zrevrange(bfoo, 0, 1); - assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + assertTrue(JedisTest.isSetAreEquals(bexpected, brange)); bexpected.add(bc); brange = jedis.zrevrange(bfoo, 0, 100); - assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + assertTrue(JedisTest.isSetAreEquals(bexpected, brange)); } @@ -148,7 +148,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { bexpected.add(bb); assertEquals(1, bstatus); - assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrange(bfoo, 0, 100))); + assertTrue(JedisTest.isSetAreEquals(bexpected, jedis.zrange(bfoo, 0, 100))); bstatus = jedis.zrem(bfoo, bbar); @@ -181,7 +181,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { bexpected.add(bb); assertEquals(3d, bscore, 0); - assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrange(bfoo, 0, 100))); + assertTrue(JedisTest.isSetAreEquals(bexpected, jedis.zrange(bfoo, 0, 100))); } @@ -266,11 +266,11 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { bexpected.add(new Tuple(ba, 2d)); Set brange = jedis.zrangeWithScores(bfoo, 0, 1); - assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + assertTrue(JedisTest.isSetAreEquals(bexpected, brange)); bexpected.add(new Tuple(bb, 10d)); brange = jedis.zrangeWithScores(bfoo, 0, 100); - assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + assertTrue(JedisTest.isSetAreEquals(bexpected, brange)); } @@ -303,11 +303,11 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { bexpected.add(new Tuple(ba, 2d)); Set brange = jedis.zrevrangeWithScores(bfoo, 0, 1); - assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + assertTrue(JedisTest.isSetAreEquals(bexpected, brange)); bexpected.add(new Tuple(bc, 0.1d)); brange = jedis.zrevrangeWithScores(bfoo, 0, 100); - assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + assertTrue(JedisTest.isSetAreEquals(bexpected, brange)); } @@ -431,23 +431,23 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { bexpected.add(bc); bexpected.add(ba); - assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + assertTrue(JedisTest.isSetAreEquals(bexpected, brange)); brange = jedis.zrangeByScore(bfoo, 0d, 2d, 0, 1); bexpected = new LinkedHashSet(); bexpected.add(bc); - assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + assertTrue(JedisTest.isSetAreEquals(bexpected, brange)); brange = jedis.zrangeByScore(bfoo, 0d, 2d, 1, 1); Set brange2 = jedis.zrangeByScore(bfoo, "-inf".getBytes(Protocol.UTF8), "(2".getBytes(Protocol.UTF8)); - assertTrue(JedisTest.isListAreEquals(bexpected, brange2)); + assertTrue(JedisTest.isSetAreEquals(bexpected, brange2)); bexpected = new LinkedHashSet(); bexpected.add(ba); - assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + assertTrue(JedisTest.isSetAreEquals(bexpected, brange)); } @@ -493,21 +493,21 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { bexpected.add(new Tuple(bc, 0.1d)); bexpected.add(new Tuple(ba, 2d)); - assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + assertTrue(JedisTest.isSetAreEquals(bexpected, brange)); brange = jedis.zrangeByScoreWithScores(bfoo, 0d, 2d, 0, 1); bexpected = new LinkedHashSet(); bexpected.add(new Tuple(bc, 0.1d)); - assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + assertTrue(JedisTest.isSetAreEquals(bexpected, brange)); brange = jedis.zrangeByScoreWithScores(bfoo, 0d, 2d, 1, 1); bexpected = new LinkedHashSet(); bexpected.add(new Tuple(ba, 2d)); - assertTrue(JedisTest.isListAreEquals(bexpected, brange)); + assertTrue(JedisTest.isSetAreEquals(bexpected, brange)); } @@ -542,7 +542,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { bexpected.add(ba); bexpected.add(bb); - assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrange(bfoo, 0, 100))); + assertTrue(JedisTest.isSetAreEquals(bexpected, jedis.zrange(bfoo, 0, 100))); } @@ -575,7 +575,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { Set bexpected = new LinkedHashSet(); bexpected.add(bb); - assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrange(bfoo, 0, 100))); + assertTrue(JedisTest.isSetAreEquals(bexpected, jedis.zrange(bfoo, 0, 100))); } @@ -610,7 +610,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { bexpected.add(new Tuple(bb, new Double(4))); bexpected.add(new Tuple(ba, new Double(3))); - assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrangeWithScores("dst".getBytes(Protocol.UTF8), 0, 100))); + assertTrue(JedisTest.isSetAreEquals(bexpected, jedis.zrangeWithScores("dst".getBytes(Protocol.UTF8), 0, 100))); } @@ -651,7 +651,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { bexpected.add(new Tuple(bb, new Double(8))); bexpected.add(new Tuple(ba, new Double(6))); - assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrangeWithScores("dst".getBytes(Protocol.UTF8), 0, 100))); + assertTrue(JedisTest.isSetAreEquals(bexpected, jedis.zrangeWithScores("dst".getBytes(Protocol.UTF8), 0, 100))); } @@ -682,7 +682,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { Set bexpected = new LinkedHashSet(); bexpected.add(new Tuple(ba, new Double(3))); - assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrangeWithScores("dst".getBytes(Protocol.UTF8), 0, 100))); + assertTrue(JedisTest.isSetAreEquals(bexpected, jedis.zrangeWithScores("dst".getBytes(Protocol.UTF8), 0, 100))); } @@ -720,7 +720,7 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { Set bexpected = new LinkedHashSet(); bexpected.add(new Tuple(ba, new Double(6))); - assertTrue(JedisTest.isListAreEquals(bexpected, jedis.zrangeWithScores("dst".getBytes(Protocol.UTF8), 0, 100))); + assertTrue(JedisTest.isSetAreEquals(bexpected, jedis.zrangeWithScores("dst".getBytes(Protocol.UTF8), 0, 100))); } } \ No newline at end of file From 4a71dff0060565593a4581a90ee6d21ec16b3d7b Mon Sep 17 00:00:00 2001 From: Yaourt Date: Sun, 7 Nov 2010 18:10:07 +0100 Subject: [PATCH 36/40] Binary Sorting fixed, and under U tests. --- .../redis/clients/jedis/SortingParams.java | 42 ++++- .../tests/commands/SortingCommandsTest.java | 158 ++++++++++++++++++ 2 files changed, 199 insertions(+), 1 deletion(-) diff --git a/src/main/java/redis/clients/jedis/SortingParams.java b/src/main/java/redis/clients/jedis/SortingParams.java index 66fd997..2129025 100644 --- a/src/main/java/redis/clients/jedis/SortingParams.java +++ b/src/main/java/redis/clients/jedis/SortingParams.java @@ -26,8 +26,25 @@ public class SortingParams { * @return the SortingParams Object */ public SortingParams by(final String pattern) { + return by(pattern.getBytes(Protocol.UTF8)); + } + + /** + * Sort by weight in keys. + *

+ * Takes a pattern that is used in order to generate the key names of the + * weights used for sorting. Weight key names are obtained substituting the + * first occurrence of * with the actual value of the elements on the list. + *

+ * The pattern for a normal key/value pair is "keyname*" and for a value in + * a hash "keyname*->fieldname". + * + * @param pattern + * @return the SortingParams Object + */ + public SortingParams by(final byte[] pattern) { params.add(BY.raw); - params.add(pattern.getBytes(Protocol.UTF8)); + params.add(pattern); return this; } @@ -117,4 +134,27 @@ public class SortingParams { } return this; } + + /** + * Retrieving external keys from the result of the search. + *

+ * Takes a pattern that is used in order to generate the key names of the + * result of sorting. The key names are obtained substituting the first + * occurrence of * with the actual value of the elements on the list. + *

+ * The pattern for a normal key/value pair is "keyname*" and for a value in + * a hash "keyname*->fieldname". + *

+ * To get the list itself use the char # as pattern. + * + * @param patterns + * @return the SortingParams Object + */ + public SortingParams get(byte[]... patterns) { + for (final byte[] pattern : patterns) { + params.add(GET.raw); + params.add(pattern); + } + return this; + } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/commands/SortingCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/SortingCommandsTest.java index 6864dae..4032eb3 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/SortingCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/SortingCommandsTest.java @@ -6,8 +6,29 @@ import java.util.List; import org.junit.Test; import redis.clients.jedis.SortingParams; +import redis.clients.jedis.tests.JedisTest; public class SortingCommandsTest extends JedisCommandTestBase { + final byte[] bfoo = {0x01, 0x02, 0x03, 0x04}; +// final byte[] bfoo = {'b', 'f', 'o', 'o'}; + final byte[] bbar1 = {0x05, 0x06, 0x07, 0x08, '1'}; + final byte[] bbar2 = {0x05, 0x06, 0x07, 0x08, '2'}; + final byte[] bbar3 = {0x05, 0x06, 0x07, 0x08, '3'}; + final byte[] bbar10 = {0x05, 0x06, 0x07, 0x08, '1', '0'}; + final byte[] bbarstar = {0x05, 0x06, 0x07, 0x08, '*'}; +// final byte[] bbar1 = {'b', 'b', 'a', 'r', '1'}; +// final byte[] bbar2 = {'b', 'b', 'a', 'r', '2'}; +// final byte[] bbar3 = {'b', 'b', 'a', 'r', '3'}; +// final byte[] bbarstar = {'b', 'b', 'a', 'r', '*'}; +// final byte[] bbarstar = {0x05, 0x06, 0x07, 0x08, '*'}; + final byte[] bcar1 = {0x0A, 0x0B, 0x0C, 0x0D, '1'}; + final byte[] bcar2 = {0x0A, 0x0B, 0x0C, 0x0D, '2'}; + final byte[] bcar10 = {0x0A, 0x0B, 0x0C, 0x0D, '1', '0'}; + final byte[] bcarstar = {0x0A, 0x0B, 0x0C, 0x0D, '*'}; + final byte[] b1 = {'1'}; + final byte[] b2 = {'2'}; + final byte[] b3 = {'3'}; + final byte[] b10 = {'1', '0'}; @Test public void sort() { jedis.lpush("foo", "3"); @@ -22,6 +43,20 @@ public class SortingCommandsTest extends JedisCommandTestBase { expected.add("3"); assertEquals(expected, result); + + //Binary + jedis.lpush(bfoo, b3); + jedis.lpush(bfoo, b2); + jedis.lpush(bfoo, b1); + + List bresult = jedis.sort(bfoo); + + List bexpected = new ArrayList(); + bexpected.add(b1); + bexpected.add(b2); + bexpected.add(b3); + + assertTrue(JedisTest.isListAreEquals(bexpected, bresult)); } @Test @@ -45,6 +80,28 @@ public class SortingCommandsTest extends JedisCommandTestBase { expected.add("1"); assertEquals(expected, result); + + //Binary + jedis.lpush(bfoo, b2); + jedis.lpush(bfoo, b3); + jedis.lpush(bfoo, b1); + + jedis.set(bbar1, b3); + jedis.set(bbar2, b2); + jedis.set(bbar3, b1); + + SortingParams bsp = new SortingParams(); + bsp.by(bbarstar); + + List bresult = jedis.sort(bfoo, bsp); + + List bexpected = new ArrayList(); + bexpected.add(b3); + bexpected.add(b2); + bexpected.add(b1); + + assertTrue(JedisTest.isListAreEquals(bexpected, bresult)); + } @Test @@ -64,6 +121,24 @@ public class SortingCommandsTest extends JedisCommandTestBase { expected.add("1"); assertEquals(expected, result); + + //Binary + jedis.lpush(bfoo, b3); + jedis.lpush(bfoo, b2); + jedis.lpush(bfoo, b1); + + SortingParams bsp = new SortingParams(); + bsp.desc(); + + List bresult = jedis.sort(bfoo, bsp); + + List bexpected = new ArrayList(); + bexpected.add(b3); + bexpected.add(b2); + bexpected.add(b1); + + JedisTest.isListAreEquals(bexpected, bresult); + } @Test @@ -83,7 +158,26 @@ public class SortingCommandsTest extends JedisCommandTestBase { expected.add("3"); assertEquals(expected, result); + + //Binary + for (int n = 10; n > 0; n--) { + jedis.lpush(bfoo, new byte[]{(byte)n}); + } + + SortingParams bsp = new SortingParams(); + sp.limit(0, 3); + + List bresult = jedis.sort(bfoo, bsp); + + List bexpected = new ArrayList(); + bexpected.add(b1); + bexpected.add(b2); + bexpected.add(b3); + + JedisTest.isListAreEquals(bexpected, bresult); + } + @Test public void sortAlpha() { @@ -102,6 +196,23 @@ public class SortingCommandsTest extends JedisCommandTestBase { expected.add("2"); assertEquals(expected, result); + + //Binary + jedis.lpush(bfoo, b1); + jedis.lpush(bfoo, b2); + jedis.lpush(bfoo, b10); + + SortingParams bsp = new SortingParams(); + bsp.alpha(); + + List bresult = jedis.sort(bfoo, bsp); + + List bexpected = new ArrayList(); + bexpected.add(b1); + bexpected.add(b10); + bexpected.add(b2); + + JedisTest.isListAreEquals(bexpected, bresult); } @Test @@ -132,7 +243,37 @@ public class SortingCommandsTest extends JedisCommandTestBase { expected.add("bar10"); assertEquals(expected, result); + + //Binary + jedis.lpush(bfoo, b1); + jedis.lpush(bfoo, b2); + jedis.lpush(bfoo, b10); + + jedis.set(bbar1, bbar1); + jedis.set(bbar2, bbar2); + jedis.set(bbar10, bbar10); + + jedis.set(bcar1, bcar1); + jedis.set(bcar2, bcar2); + jedis.set(bcar10, bcar10); + + SortingParams bsp = new SortingParams(); + sp.get(bcarstar, bbarstar); + + List bresult = jedis.sort(bfoo, bsp); + + List bexpected = new ArrayList(); + bexpected.add(bcar1); + bexpected.add(bbar1); + bexpected.add(bcar2); + bexpected.add(bbar2); + bexpected.add(bcar10); + bexpected.add(bbar10); + + JedisTest.isListAreEquals(bexpected, bresult); + } + @Test public void sortStore() { @@ -149,6 +290,23 @@ public class SortingCommandsTest extends JedisCommandTestBase { assertEquals(3, result); assertEquals(expected, jedis.lrange("result", 0, 1000)); + + //Binary + jedis.lpush(bfoo, b1); + jedis.lpush(bfoo, b2); + jedis.lpush(bfoo, b10); + + byte[] bkresult = new byte[]{0X09, 0x0A, 0x0B, 0x0C}; + int bresult = jedis.sort(bfoo, bkresult); + + List bexpected = new ArrayList(); + bexpected.add(b1); + bexpected.add(b2); + bexpected.add(b10); + + assertEquals(3, bresult); + JedisTest.isListAreEquals(bexpected, jedis.lrange(bkresult, 0, 1000)); + } } \ No newline at end of file From deb049d1958efa0c09d33e2799d200f17fb3223a Mon Sep 17 00:00:00 2001 From: Yaourt Date: Mon, 8 Nov 2010 11:11:43 +0100 Subject: [PATCH 37/40] U tests for binary values manipulations. --- .../java/redis/clients/jedis/Protocol.java | 1 + .../commands/BinaryValuesCommandsTest.java | 221 ++++++++++++++++++ src/test/resources/MySimpson.png | Bin 0 -> 29542 bytes 3 files changed, 222 insertions(+) create mode 100644 src/test/java/redis/clients/jedis/tests/commands/BinaryValuesCommandsTest.java create mode 100644 src/test/resources/MySimpson.png diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index 7ae5b2a..5d40492 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -293,6 +293,7 @@ public final class Protocol { PMESSAGE, PSUBSCRIBE, PUNSUBSCRIBE, + OK, ONE, SET, STORE, diff --git a/src/test/java/redis/clients/jedis/tests/commands/BinaryValuesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/BinaryValuesCommandsTest.java new file mode 100644 index 0000000..444a805 --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/commands/BinaryValuesCommandsTest.java @@ -0,0 +1,221 @@ +package redis.clients.jedis.tests.commands; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +import redis.clients.jedis.JedisException; +import redis.clients.jedis.Protocol.Keyword; +import redis.clients.jedis.tests.JedisTest; + +public class BinaryValuesCommandsTest extends JedisCommandTestBase { + byte[] bfoo = {0x01, 0x02, 0x03, 0x04}; + byte[] bbar = {0x05, 0x06, 0x07, 0x08}; + byte[] binaryValue = null; + @Before + public void setUp() throws Exception { + super.setUp(); + final InputStream is = this.getClass().getResourceAsStream("/MySimpson.png"); + byte[] resource = new byte[512];//29542 + byte[] buffer = new byte[2048]; + int length = 0; + int idx = 0; + while(-1 != (length = is.read(buffer))) { + if ((idx + length) > resource.length) { + byte[] newresource = new byte[resource.length + ((idx + length) - resource.length)]; + System.arraycopy(resource, 0, newresource, 0, idx); + resource = newresource; + } + System.arraycopy(buffer, 0, resource, idx, length); + idx += length; + } + binaryValue = resource; + } + + @Test + public void setAndGet() { + String status = jedis.set(bfoo, binaryValue); + assertTrue(Keyword.OK.name().equalsIgnoreCase(status)); + + byte[] value = jedis.get(bfoo); + assertTrue(Arrays.equals(binaryValue, value)); + + assertNull(jedis.get(bbar)); + } + + @Test + public void getSet() { + byte[] value = jedis.getSet(bfoo, binaryValue); + assertNull(value); + value = jedis.get(bfoo); + assertTrue(Arrays.equals(binaryValue, value)); + } + + @Test + public void mget() { + List values = jedis.mget(bfoo, bbar); + List expected = new ArrayList(); + expected.add(null); + expected.add(null); + + JedisTest.isListAreEquals(expected, values); + + jedis.set(bfoo, binaryValue); + + expected = new ArrayList(); + expected.add(binaryValue); + expected.add(null); + values = jedis.mget(bfoo, bbar); + + JedisTest.isListAreEquals(expected, values); + + jedis.set(bbar, bfoo); + + expected = new ArrayList(); + expected.add(binaryValue); + expected.add(bfoo); + values = jedis.mget(bfoo, bbar); + + JedisTest.isListAreEquals(expected, values); + } + + @Test + public void setnx() { + int status = jedis.setnx(bfoo, binaryValue); + assertEquals(1, status); + assertTrue(Arrays.equals(binaryValue, jedis.get(bfoo))); + + status = jedis.setnx(bfoo, bbar); + assertEquals(0, status); + assertTrue(Arrays.equals(binaryValue, jedis.get(bfoo))); + } + + @Test + public void setex() { + String status = jedis.setex(bfoo, 20, binaryValue); + assertEquals(Keyword.OK.name(), status); + int ttl = jedis.ttl(bfoo); + assertTrue(ttl > 0 && ttl <= 20); + } + + @Test + public void mset() { + String status = jedis.mset(bfoo, binaryValue, bbar, bfoo); + assertEquals(Keyword.OK.name(), status); + assertTrue(Arrays.equals(binaryValue, jedis.get(bfoo))); + assertTrue(Arrays.equals(bfoo, jedis.get(bbar))); + } + + @Test + public void msetnx() { + int status = jedis.msetnx(bfoo, binaryValue, bbar, bfoo); + assertEquals(1, status); + assertTrue(Arrays.equals(binaryValue, jedis.get(bfoo))); + assertTrue(Arrays.equals(bfoo, jedis.get(bbar))); + + status = jedis.msetnx(bfoo, bbar, "bar2".getBytes(), "foo2".getBytes()); + assertEquals(0, status); + assertTrue(Arrays.equals(binaryValue, jedis.get(bfoo))); + assertTrue(Arrays.equals(bfoo, jedis.get(bbar))); + } + + @Test(expected = JedisException.class) + public void incrWrongValue() { + jedis.set(bfoo, binaryValue); + jedis.incr(bfoo); + } + + @Test + public void incr() { + int value = jedis.incr(bfoo); + assertEquals(1, value); + value = jedis.incr(bfoo); + assertEquals(2, value); + } + + @Test(expected = JedisException.class) + public void incrByWrongValue() { + jedis.set(bfoo, binaryValue); + jedis.incrBy(bfoo, 2); + } + + @Test + public void incrBy() { + int value = jedis.incrBy(bfoo, 2); + assertEquals(2, value); + value = jedis.incrBy(bfoo, 2); + assertEquals(4, value); + } + + @Test(expected = JedisException.class) + public void decrWrongValue() { + jedis.set(bfoo, binaryValue); + jedis.decr(bfoo); + } + + @Test + public void decr() { + int value = jedis.decr(bfoo); + assertEquals(-1, value); + value = jedis.decr(bfoo); + assertEquals(-2, value); + } + + @Test(expected = JedisException.class) + public void decrByWrongValue() { + jedis.set(bfoo, binaryValue); + jedis.decrBy(bfoo, 2); + } + + @Test + public void decrBy() { + int value = jedis.decrBy(bfoo, 2); + assertEquals(-2, value); + value = jedis.decrBy(bfoo, 2); + assertEquals(-4, value); + } + + @Test + public void append() { + byte[] first512 = new byte[512]; + System.arraycopy(binaryValue, 0, first512, 0, 512); + int value = jedis.append(bfoo, first512); + assertEquals(512, value); + assertTrue(Arrays.equals(first512, jedis.get(bfoo))); + + byte[] rest = new byte[binaryValue.length - 512]; + System.arraycopy(binaryValue, 512, rest, 0, binaryValue.length - 512); + value = jedis.append(bfoo, rest); + assertEquals(binaryValue.length, value); + + assertTrue(Arrays.equals(binaryValue, jedis.get(bfoo))); + } + + @Test + public void substr() { + jedis.set(bfoo, binaryValue); + + byte[] first512 = new byte[512]; + System.arraycopy(binaryValue, 0, first512, 0, 512); + byte[] rfirst512 = jedis.substr(bfoo, 0, 511); + assertTrue(Arrays.equals(first512, rfirst512)); + + byte[] last512 = new byte[512]; + System.arraycopy(binaryValue, binaryValue.length - 512, last512, 0, 512); + assertTrue(Arrays.equals(last512, jedis.substr(bfoo, -512, -1))); + + assertTrue(Arrays.equals(binaryValue, jedis.substr(bfoo, 0, -1))); + + assertTrue(Arrays.equals(last512, jedis.substr(bfoo, binaryValue.length - 512, 100000))); + } + + @Test + public void strlen() { + jedis.set(bfoo, binaryValue); + assertEquals(binaryValue.length, jedis.strlen(bfoo).intValue()); + } +} \ No newline at end of file diff --git a/src/test/resources/MySimpson.png b/src/test/resources/MySimpson.png new file mode 100644 index 0000000000000000000000000000000000000000..025c83dd04a0e1567995e157cd59a16e08750f85 GIT binary patch literal 29542 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⁡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 literal 0 HcmV?d00001 From e8704296fdb7b71174c9c3bb7c5290be17aa0060 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Mon, 8 Nov 2010 14:59:57 +0100 Subject: [PATCH 38/40] Binary U test on Transaction ... --- .../java/redis/clients/jedis/Protocol.java | 1 + .../commands/TransactionCommandsTest.java | 100 ++++++++++++++++-- 2 files changed, 93 insertions(+), 8 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index 5d40492..171e356 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -295,6 +295,7 @@ public final class Protocol { PUNSUBSCRIBE, OK, ONE, + QUEUED, SET, STORE, SUBSCRIBE, diff --git a/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java index 715ed88..0c9a252 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java @@ -3,6 +3,7 @@ package redis.clients.jedis.tests.commands; import java.io.IOException; import java.net.UnknownHostException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.junit.Before; @@ -13,9 +14,17 @@ import redis.clients.jedis.JedisException; import redis.clients.jedis.Protocol; import redis.clients.jedis.Transaction; import redis.clients.jedis.TransactionBlock; +import redis.clients.jedis.Protocol.Keyword; import redis.clients.jedis.tests.JedisTest; public class TransactionCommandsTest extends JedisCommandTestBase { + final byte[] bfoo = {0x01, 0x02, 0x03, 0x04}; + final byte[] bbar = {0x05, 0x06, 0x07, 0x08}; + final byte[] ba = {0x0A}; + final byte[] bb = {0x0B}; + + final byte[] bmykey = {0x42, 0x02, 0x03, 0x04}; + Jedis nj; @Before public void setUp() throws Exception { @@ -32,13 +41,13 @@ public class TransactionCommandsTest extends JedisCommandTestBase { Transaction trans = jedis.multi(); String status = trans.sadd("foo", "a"); - assertEquals("QUEUED", status); + assertEquals(Keyword.QUEUED.name(), status); status = trans.sadd("foo", "b"); - assertEquals("QUEUED", status); + assertEquals(Keyword.QUEUED.name(), status); status = trans.scard("foo"); - assertEquals("QUEUED", status); + assertEquals(Keyword.QUEUED.name(), status); List response = trans.exec(); @@ -47,6 +56,27 @@ public class TransactionCommandsTest extends JedisCommandTestBase { expected.add(1); expected.add(2); assertEquals(expected, response); + + //Binary + trans = jedis.multi(); + + status = trans.sadd(bfoo, ba); + assertEquals(Keyword.QUEUED.name(), status); + + status = trans.sadd(bfoo, bb); + assertEquals(Keyword.QUEUED.name(), status); + + status = trans.scard(bfoo); + assertEquals(Keyword.QUEUED.name(), status); + + response = trans.exec(); + + expected = new ArrayList(); + expected.add(1); + expected.add(1); + expected.add(2); + assertEquals(expected, response); + } @Test @@ -54,13 +84,13 @@ public class TransactionCommandsTest extends JedisCommandTestBase { List response = jedis.multi(new TransactionBlock() { public void execute() { String status = sadd("foo", "a"); - assertEquals("QUEUED", status); + assertEquals(Keyword.QUEUED.name(), status); status = sadd("foo", "b"); - assertEquals("QUEUED", status); + assertEquals(Keyword.QUEUED.name(), status); status = scard("foo"); - assertEquals("QUEUED", status); + assertEquals(Keyword.QUEUED.name(), status); } }); @@ -69,6 +99,27 @@ public class TransactionCommandsTest extends JedisCommandTestBase { expected.add(1); expected.add(2); assertEquals(expected, response); + + //Binary + response = jedis.multi(new TransactionBlock() { + public void execute() { + String status = sadd(bfoo, ba); + assertEquals(Keyword.QUEUED.name(), status); + + status = sadd(bfoo, bb); + assertEquals(Keyword.QUEUED.name(), status); + + status = scard(bfoo); + assertEquals(Keyword.QUEUED.name(), status); + } + }); + + expected = new ArrayList(); + expected.add(1); + expected.add(1); + expected.add(2); + assertEquals(expected, response); + } @Test @@ -85,6 +136,20 @@ public class TransactionCommandsTest extends JedisCommandTestBase { List resp = t.exec(); assertEquals(null, resp); assertEquals("bar", jedis.get("mykey")); + + //Binary + jedis.watch(bmykey); + t = jedis.multi(); + + nj.connect(); + nj.auth("foobared"); + nj.set(bmykey, bbar); + nj.disconnect(); + + t.set(bmykey, bfoo); + resp = t.exec(); + assertEquals(null, resp); + assertTrue(Arrays.equals(bbar, jedis.get(bmykey))); } @Test @@ -104,9 +169,28 @@ public class TransactionCommandsTest extends JedisCommandTestBase { t.set("mykey", val); List resp = t.exec(); List expected = new ArrayList(); - expected.add("OK".getBytes(Protocol.UTF8)); + expected.add(Keyword.OK.name().getBytes(Protocol.UTF8)); JedisTest.isListAreEquals(expected, resp); -// assertEquals(expected, resp); + + //Binary + jedis.watch(bmykey); + byte[] bval = jedis.get(bmykey); + bval = bfoo; + status = jedis.unwatch(); + assertEquals(Keyword.OK.name(), status); + t = jedis.multi(); + + nj.connect(); + nj.auth("foobared"); + nj.set(bmykey, bbar); + nj.disconnect(); + + t.set(bmykey, bval); + resp = t.exec(); + expected = new ArrayList(); + expected.add(Keyword.OK.name().toUpperCase().getBytes()); + JedisTest.isListAreEquals(expected, resp); + } @Test(expected = JedisException.class) From dd6874aa7adc0c735be2c33e61eb657cb730c60a Mon Sep 17 00:00:00 2001 From: Yaourt Date: Mon, 8 Nov 2010 15:57:43 +0100 Subject: [PATCH 39/40] Hashes hkeys and hvals refactored to return a set instead of a list. It looks like the order is not guaranted, so a set seems much adapted than a list. --- .../java/redis/clients/jedis/BinaryJedis.java | 10 ++++---- .../clients/jedis/BinaryJedisCommands.java | 4 ++-- .../clients/jedis/BinaryShardedJedis.java | 4 ++-- src/main/java/redis/clients/jedis/Jedis.java | 10 ++++---- .../redis/clients/jedis/JedisCommands.java | 4 ++-- .../redis/clients/jedis/ShardedJedis.java | 4 ++-- .../tests/commands/HashesCommandsTest.java | 24 +++++++++---------- 7 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index 93056a6..410cc6d 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -829,10 +829,11 @@ public class BinaryJedis implements BinaryJedisCommands { * @param key * @return All the fields names contained into a hash. */ - public List hkeys(final byte[] key) { + public Set hkeys(final byte[] key) { checkIsInMulti(); client.hkeys(key); - return client.getBinaryMultiBulkReply(); + final List lresult = client.getBinaryMultiBulkReply(); + return new HashSet(lresult); } /** @@ -843,10 +844,11 @@ public class BinaryJedis implements BinaryJedisCommands { * @param key * @return All the fields values contained into a hash. */ - public List hvals(final byte[] key) { + public Set hvals(final byte[] key) { checkIsInMulti(); client.hvals(key); - return client.getBinaryMultiBulkReply(); + final List lresult = client.getBinaryMultiBulkReply(); + return new HashSet(lresult); } /** diff --git a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java index 009efb1..3b593b3 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java +++ b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java @@ -60,9 +60,9 @@ public interface BinaryJedisCommands { Integer hlen(byte[] key); - List hkeys(byte[] key); + Set hkeys(byte[] key); - List hvals(byte[] key); + Set hvals(byte[] key); Map hgetAll(byte[] key); diff --git a/src/main/java/redis/clients/jedis/BinaryShardedJedis.java b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java index 4f6821d..f438226 100644 --- a/src/main/java/redis/clients/jedis/BinaryShardedJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java @@ -164,12 +164,12 @@ public class BinaryShardedJedis extends Sharded implement return j.hlen(key); } - public List hkeys(byte[] key) { + public Set hkeys(byte[] key) { Jedis j = getShard(key); return j.hkeys(key); } - public List hvals(byte[] key) { + public Set hvals(byte[] key) { Jedis j = getShard(key); return j.hvals(key); } diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index ee76869..3d46c18 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -758,10 +758,11 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @param key * @return All the fields names contained into a hash. */ - public List hkeys(final String key) { + public Set hkeys(final String key) { checkIsInMulti(); client.hkeys(key); - return client.getMultiBulkReply(); + final List lresult = client.getMultiBulkReply(); + return new HashSet(lresult); } /** @@ -772,10 +773,11 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @param key * @return All the fields values contained into a hash. */ - public List hvals(final String key) { + public Set hvals(final String key) { checkIsInMulti(); client.hvals(key); - return client.getMultiBulkReply(); + final List lresult = client.getMultiBulkReply(); + return new HashSet(lresult); } /** diff --git a/src/main/java/redis/clients/jedis/JedisCommands.java b/src/main/java/redis/clients/jedis/JedisCommands.java index 126e28d..0d7a1d4 100644 --- a/src/main/java/redis/clients/jedis/JedisCommands.java +++ b/src/main/java/redis/clients/jedis/JedisCommands.java @@ -58,9 +58,9 @@ public interface JedisCommands { Integer hlen(String key); - List hkeys(String key); + Set hkeys(String key); - List hvals(String key); + Set hvals(String key); Map hgetAll(String key); diff --git a/src/main/java/redis/clients/jedis/ShardedJedis.java b/src/main/java/redis/clients/jedis/ShardedJedis.java index 1fda008..0ffacb4 100644 --- a/src/main/java/redis/clients/jedis/ShardedJedis.java +++ b/src/main/java/redis/clients/jedis/ShardedJedis.java @@ -150,12 +150,12 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands { return j.hlen(key); } - public List hkeys(String key) { + public Set hkeys(String key) { Jedis j = getShard(key); return j.hkeys(key); } - public List hvals(String key) { + public Set hvals(String key) { Jedis j = getShard(key); return j.hvals(key); } diff --git a/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java index 0dd6dda..567de4b 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java @@ -222,11 +222,11 @@ public class HashesCommandsTest extends JedisCommandTestBase { hash.put("car", "bar"); jedis.hmset("foo", hash); - List keys = jedis.hkeys("foo"); - List expected = new ArrayList(); + Set keys = jedis.hkeys("foo"); + Set expected = new HashSet(); expected.add("bar"); expected.add("car"); - assertTrue(JedisTest.isListAreEquals(expected, keys)); + assertTrue(JedisTest.isSetAreEquals(expected, keys)); //Binary Map bhash = new LinkedHashMap(); @@ -234,11 +234,11 @@ public class HashesCommandsTest extends JedisCommandTestBase { bhash.put(bcar, bbar); jedis.hmset(bfoo, bhash); - List bkeys = jedis.hkeys(bfoo); - List bexpected = new ArrayList(); + Set bkeys = jedis.hkeys(bfoo); + Set bexpected = new HashSet(); bexpected.add(bbar); bexpected.add(bcar); - assertTrue(JedisTest.isListAreEquals(bexpected, bkeys)); + assertTrue(JedisTest.isSetAreEquals(bexpected, bkeys)); } @@ -249,11 +249,11 @@ public class HashesCommandsTest extends JedisCommandTestBase { hash.put("car", "bar"); jedis.hmset("foo", hash); - List vals = jedis.hvals("foo"); - List expected = new ArrayList(); + Set vals = jedis.hvals("foo"); + Set expected = new HashSet(); expected.add("car"); expected.add("bar"); - assertTrue(JedisTest.isListAreEquals(expected, vals)); + assertTrue(JedisTest.isSetAreEquals(expected, vals)); //Binary Map bhash = new LinkedHashMap(); @@ -261,11 +261,11 @@ public class HashesCommandsTest extends JedisCommandTestBase { bhash.put(bcar, bbar); jedis.hmset(bfoo, bhash); - List bvals = jedis.hvals(bfoo); - List bexpected = new ArrayList(); + Set bvals = jedis.hvals(bfoo); + Set bexpected = new HashSet(); bexpected.add(bcar); bexpected.add(bbar); - assertTrue(JedisTest.isListAreEquals(bexpected, bvals)); + assertTrue(JedisTest.isSetAreEquals(bexpected, bvals)); } From d3362da12c774be493b63c9b0584eb29e6ec0a50 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 12 Nov 2010 15:42:42 +0100 Subject: [PATCH 40/40] hvals now return a Collection, not a Set neither a List. --- .../java/redis/clients/jedis/BinaryJedis.java | 5 +- .../clients/jedis/BinaryJedisCommands.java | 3 +- .../clients/jedis/BinaryShardedJedis.java | 3 +- src/main/java/redis/clients/jedis/Jedis.java | 5 +- .../redis/clients/jedis/JedisCommands.java | 3 +- .../redis/clients/jedis/ShardedJedis.java | 3 +- .../redis/clients/jedis/tests/JedisTest.java | 69 +++++++++++++++++++ .../tests/commands/HashesCommandsTest.java | 12 ++-- 8 files changed, 89 insertions(+), 14 deletions(-) diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index 410cc6d..a72e937 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -3,6 +3,7 @@ package redis.clients.jedis; import java.io.IOException; import java.net.UnknownHostException; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -844,11 +845,11 @@ public class BinaryJedis implements BinaryJedisCommands { * @param key * @return All the fields values contained into a hash. */ - public Set hvals(final byte[] key) { + public Collection hvals(final byte[] key) { checkIsInMulti(); client.hvals(key); final List lresult = client.getBinaryMultiBulkReply(); - return new HashSet(lresult); + return lresult; } /** diff --git a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java index 3b593b3..052c686 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java +++ b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java @@ -1,5 +1,6 @@ package redis.clients.jedis; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; @@ -62,7 +63,7 @@ public interface BinaryJedisCommands { Set hkeys(byte[] key); - Set hvals(byte[] key); + Collection hvals(byte[] key); Map hgetAll(byte[] key); diff --git a/src/main/java/redis/clients/jedis/BinaryShardedJedis.java b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java index f438226..948714b 100644 --- a/src/main/java/redis/clients/jedis/BinaryShardedJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java @@ -1,6 +1,7 @@ package redis.clients.jedis; import java.io.IOException; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; @@ -169,7 +170,7 @@ public class BinaryShardedJedis extends Sharded implement return j.hkeys(key); } - public Set hvals(byte[] key) { + public Collection hvals(byte[] key) { Jedis j = getShard(key); return j.hvals(key); } diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index 3d46c18..194e3ea 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -1,6 +1,7 @@ package redis.clients.jedis; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -773,11 +774,11 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @param key * @return All the fields values contained into a hash. */ - public Set hvals(final String key) { + public Collection hvals(final String key) { checkIsInMulti(); client.hvals(key); final List lresult = client.getMultiBulkReply(); - return new HashSet(lresult); + return lresult; } /** diff --git a/src/main/java/redis/clients/jedis/JedisCommands.java b/src/main/java/redis/clients/jedis/JedisCommands.java index 0d7a1d4..a821519 100644 --- a/src/main/java/redis/clients/jedis/JedisCommands.java +++ b/src/main/java/redis/clients/jedis/JedisCommands.java @@ -1,5 +1,6 @@ package redis.clients.jedis; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; @@ -60,7 +61,7 @@ public interface JedisCommands { Set hkeys(String key); - Set hvals(String key); + Collection hvals(String key); Map hgetAll(String key); diff --git a/src/main/java/redis/clients/jedis/ShardedJedis.java b/src/main/java/redis/clients/jedis/ShardedJedis.java index 0ffacb4..a327a3e 100644 --- a/src/main/java/redis/clients/jedis/ShardedJedis.java +++ b/src/main/java/redis/clients/jedis/ShardedJedis.java @@ -1,5 +1,6 @@ package redis.clients.jedis; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; @@ -155,7 +156,7 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands { return j.hkeys(key); } - public Set hvals(String key) { + public Collection hvals(String key) { Jedis j = getShard(key); return j.hvals(key); } diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index f968b0c..3dceb1c 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -49,6 +49,63 @@ public class JedisTest extends JedisCommandTestBase { jedis.get("foo"); } + @SuppressWarnings("rawtypes") + public static boolean isCollectionAreEquals(Collection expected, Collection result) { + if(expected.size() != result.size()) { + return false; + } + + final Iterator expectedit = expected.iterator(); + while(expectedit.hasNext()) { + final Object exp = expectedit.next(); + final Iterator responseit = result.iterator(); + boolean found = false; + while(responseit.hasNext() && !found) { + final Object resp = responseit.next(); + if(exp instanceof byte[] && resp instanceof byte[]) { + final byte[] bexp = (byte[]) exp; + final byte[] bresp = (byte[]) resp; + if(Arrays.equals(bexp, bresp)) { + found = true; + } + } else if (exp instanceof Set && resp instanceof Set) { + final Set subexp = (Set) exp; + final Set subresp = (Set) resp; + if(isSetAreEquals(subexp, subresp)) { + found = true; + } + } else if (exp instanceof List && resp instanceof List) { + final List subexp = (List) exp; + final List subresp = (List) resp; + if(isListAreEquals(subexp, subresp)) { + found = true; + } + } else if (exp instanceof Collection && resp instanceof Collection) { + final Collection subexp = (Collection) exp; + final Collection subresp = (Collection) resp; + if(isCollectionAreEquals(subexp, subresp)) { + found = true; + } + } else { + if (null != exp) { + if (exp.equals(resp)){ + found = true; + } + } else { + if(resp == null) { + found = true; + } + } + } + } + if(!found){ + fail("Result doesn't contain " + (null != exp ? exp.toString() : null)); + } + } + + return true; + } + @SuppressWarnings("rawtypes") public static boolean isSetAreEquals(Set expected, Set result) { if(expected.size() != result.size()) { @@ -80,6 +137,12 @@ public class JedisTest extends JedisCommandTestBase { if(isListAreEquals(subexp, subresp)) { found = true; } + } else if (exp instanceof Collection && resp instanceof Collection) { + final Collection subexp = (Collection) exp; + final Collection subresp = (Collection) resp; + if(isCollectionAreEquals(subexp, subresp)) { + found = true; + } } else { if (null != exp) { if (exp.equals(resp)){ @@ -131,6 +194,12 @@ public class JedisTest extends JedisCommandTestBase { if(isListAreEquals(subexp, subresp)) { continue; } + } else if (exp instanceof Collection && resp instanceof Collection) { + final Collection subexp = (Collection) exp; + final Collection subresp = (Collection) resp; + if(isCollectionAreEquals(subexp, subresp)) { + continue; + } } else { if (null != exp) { if (exp.equals(resp)){ diff --git a/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java index 567de4b..9467ce0 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java @@ -249,11 +249,11 @@ public class HashesCommandsTest extends JedisCommandTestBase { hash.put("car", "bar"); jedis.hmset("foo", hash); - Set vals = jedis.hvals("foo"); - Set expected = new HashSet(); + Collection vals = jedis.hvals("foo"); + List expected = new ArrayList(); expected.add("car"); expected.add("bar"); - assertTrue(JedisTest.isSetAreEquals(expected, vals)); + assertTrue(JedisTest.isCollectionAreEquals(expected, vals)); //Binary Map bhash = new LinkedHashMap(); @@ -261,11 +261,11 @@ public class HashesCommandsTest extends JedisCommandTestBase { bhash.put(bcar, bbar); jedis.hmset(bfoo, bhash); - Set bvals = jedis.hvals(bfoo); - Set bexpected = new HashSet(); + Collection bvals = jedis.hvals(bfoo); + List bexpected = new ArrayList(); bexpected.add(bcar); bexpected.add(bbar); - assertTrue(JedisTest.isSetAreEquals(bexpected, bvals)); + assertTrue(JedisTest.isCollectionAreEquals(bexpected, bvals)); }