diff --git a/src/main/java/redis/clients/jedis/BinaryClient.java b/src/main/java/redis/clients/jedis/BinaryClient.java index ebdd539..b12d9ec 100644 --- a/src/main/java/redis/clients/jedis/BinaryClient.java +++ b/src/main/java/redis/clients/jedis/BinaryClient.java @@ -28,6 +28,10 @@ public class BinaryClient extends Connection { private boolean isInMulti; + private String password; + + private long db; + public boolean isInMulti() { return isInMulti; } @@ -40,6 +44,21 @@ public class BinaryClient extends Connection { super(host, port); } + public void setPassword(final String password) { + this.password = password; + } + + @Override + public void connect() { + if (!isConnected()) { + super.connect(); + if (password != null) { + sendCommand(AUTH, password); + getStatusCodeReply(); + } + } + } + public void ping() { sendCommand(PING); } @@ -53,6 +72,7 @@ public class BinaryClient extends Connection { } public void quit() { + db = 0; sendCommand(QUIT); } @@ -105,6 +125,7 @@ public class BinaryClient extends Connection { } public void select(final int index) { + db = index; sendCommand(SELECT, toByteArray(index)); } @@ -234,23 +255,23 @@ public class BinaryClient extends Connection { sendCommand(LLEN, key); } - public void lrange(final byte[] key, final int start, final int end) { + public void lrange(final byte[] key, final long start, final long end) { sendCommand(LRANGE, key, toByteArray(start), toByteArray(end)); } - public void ltrim(final byte[] key, final int start, final int end) { + public void ltrim(final byte[] key, final long start, final long end) { sendCommand(LTRIM, key, toByteArray(start), toByteArray(end)); } - public void lindex(final byte[] key, final int index) { + public void lindex(final byte[] key, final long index) { sendCommand(LINDEX, key, toByteArray(index)); } - public void lset(final byte[] key, final int index, final byte[] value) { + public void lset(final byte[] key, final long index, final byte[] value) { sendCommand(LSET, key, toByteArray(index), value); } - public void lrem(final byte[] key, int count, final byte[] value) { + public void lrem(final byte[] key, long count, final byte[] value) { sendCommand(LREM, key, toByteArray(count), value); } @@ -438,6 +459,7 @@ public class BinaryClient extends Connection { } public void auth(final String password) { + setPassword(password); sendCommand(AUTH, password); } @@ -478,23 +500,45 @@ public class BinaryClient extends Connection { sendCommand(ZRANGEBYSCORE, key, toByteArray(min), toByteArray(max)); } + public void zrevrangeByScore(final byte[] key, final double max, + final double min) { + sendCommand(ZREVRANGEBYSCORE, key, toByteArray(max), toByteArray(min)); + } + public void zrangeByScore(final byte[] key, final byte[] min, final byte[] max) { sendCommand(ZRANGEBYSCORE, key, min, max); } + public void zrevrangeByScore(final byte[] key, final byte[] max, + final byte[] min) { + sendCommand(ZREVRANGEBYSCORE, key, max, min); + } + 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 zrevrangeByScore(final byte[] key, final double max, + final double min, final int offset, int count) { + sendCommand(ZREVRANGEBYSCORE, key, toByteArray(max), toByteArray(min), + 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 zrevrangeByScoreWithScores(final byte[] key, final double max, + final double min) { + sendCommand(ZREVRANGEBYSCORE, key, toByteArray(max), toByteArray(min), + 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), @@ -502,6 +546,13 @@ public class BinaryClient extends Connection { WITHSCORES.raw); } + public void zrevrangeByScoreWithScores(final byte[] key, final double max, + final double min, final int offset, final int count) { + sendCommand(ZREVRANGEBYSCORE, key, toByteArray(max), toByteArray(min), + 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)); } @@ -650,6 +701,16 @@ public class BinaryClient extends Connection { } public void getrange(byte[] key, long startOffset, long endOffset) { - sendCommand(GETRANGE, key, toByteArray(startOffset), toByteArray(endOffset)); + sendCommand(GETRANGE, key, toByteArray(startOffset), + toByteArray(endOffset)); } -} \ No newline at end of file + + public Long getDB() { + return db; + } + + public void disconnect() { + db = 0; + super.disconnect(); + } +} diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index 4401042..cea0874 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -1,7 +1,5 @@ package redis.clients.jedis; -import java.io.IOException; -import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; @@ -11,13 +9,13 @@ import java.util.Map; import java.util.Set; import redis.clients.jedis.BinaryClient.LIST_POSITION; +import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.exceptions.JedisException; import redis.clients.util.JedisByteHashMap; import redis.clients.util.SafeEncoder; public class BinaryJedis implements BinaryJedisCommands { protected Client client = null; - protected String password = null; public BinaryJedis(final String host) { client = new Client(host); @@ -35,7 +33,7 @@ public class BinaryJedis implements BinaryJedisCommands { public BinaryJedis(final JedisShardInfo shardInfo) { client = new Client(shardInfo.getHost(), shardInfo.getPort()); client.setTimeout(shardInfo.getTimeout()); - this.password = shardInfo.getPassword(); + client.setPassword(shardInfo.getPassword()); } public String ping() { @@ -79,9 +77,10 @@ public class BinaryJedis implements BinaryJedisCommands { /** * Ask the server to silently close the connection. */ - public void quit() { + public String quit() { checkIsInMulti(); client.quit(); + return client.getStatusCodeReply(); } /** @@ -1649,16 +1648,16 @@ public class BinaryJedis implements BinaryJedisCommands { protected void checkIsInMulti() { if (client.isInMulti()) { - throw new JedisException( + throw new JedisDataException( "Cannot use Jedis when in Multi. Please use JedisTransaction instead."); } } - public void connect() throws UnknownHostException, IOException { + public void connect() { client.connect(); } - public void disconnect() throws IOException { + public void disconnect() { client.disconnect(); } @@ -2019,11 +2018,13 @@ public class BinaryJedis implements BinaryJedisCommands { public List pipelined(final PipelineBlock jedisPipeline) { jedisPipeline.setClient(client); jedisPipeline.execute(); - return client.getAll(); + return jedisPipeline.sync(); } public Pipeline pipelined() { - return new Pipeline(client); + Pipeline pipeline = new Pipeline(); + pipeline.setClient(client); + return pipeline; } public void subscribe(final JedisPubSub jedisPubSub, @@ -2326,6 +2327,44 @@ public class BinaryJedis implements BinaryJedisCommands { return set; } + public Set zrevrangeByScore(final byte[] key, final double max, + final double min) { + checkIsInMulti(); + client.zrevrangeByScore(key, max, min); + return new LinkedHashSet(client.getBinaryMultiBulkReply()); + } + + public Set zrevrangeByScore(final byte[] key, final byte[] max, + final byte[] min) { + checkIsInMulti(); + client.zrevrangeByScore(key, max, min); + return new LinkedHashSet(client.getBinaryMultiBulkReply()); + } + + public Set zrevrangeByScore(final byte[] key, final double max, + final double min, final int offset, final int count) { + checkIsInMulti(); + client.zrevrangeByScore(key, max, min, offset, count); + return new LinkedHashSet(client.getBinaryMultiBulkReply()); + } + + public Set zrevrangeByScoreWithScores(final byte[] key, + final double max, final double min) { + checkIsInMulti(); + client.zrevrangeByScoreWithScores(key, max, min); + Set set = getBinaryTupledSet(); + return set; + } + + public Set zrevrangeByScoreWithScores(final byte[] key, + final double max, final double min, final int offset, + final int count) { + checkIsInMulti(); + client.zrevrangeByScoreWithScores(key, max, min, offset, count); + Set set = getBinaryTupledSet(); + 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 @@ -2898,7 +2937,10 @@ public class BinaryJedis implements BinaryJedisCommands { */ public byte[] brpoplpush(byte[] source, byte[] destination, int timeout) { client.brpoplpush(source, destination, timeout); - return client.getBinaryBulkReply(); + client.setTimeoutInfinite(); + byte[] reply = client.getBinaryBulkReply(); + client.rollbackTimeout(); + return reply; } /** @@ -2952,4 +2994,8 @@ public class BinaryJedis implements BinaryJedisCommands { jedisPubSub.proceedWithPatterns(client, patterns); client.rollbackTimeout(); } + + public Long getDB() { + return client.getDB(); + } } \ 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 241734f..ef8aebd 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java +++ b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java @@ -139,6 +139,16 @@ public interface BinaryJedisCommands { Set zrangeByScoreWithScores(byte[] key, double min, double max, int offset, int count); + Set zrevrangeByScore(byte[] key, double max, double min); + + Set zrevrangeByScore(byte[] key, double max, double min, int offset, + int count); + + Set zrevrangeByScoreWithScores(byte[] key, double max, double min); + + Set zrevrangeByScoreWithScores(byte[] key, double max, double min, + int offset, int count); + Long zremrangeByRank(byte[] key, int start, int end); Long zremrangeByScore(byte[] key, double start, double end); diff --git a/src/main/java/redis/clients/jedis/BinaryShardedJedis.java b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java index 1143121..113511b 100644 --- a/src/main/java/redis/clients/jedis/BinaryShardedJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java @@ -357,6 +357,28 @@ public class BinaryShardedJedis extends Sharded return j.zrangeByScoreWithScores(key, min, max, offset, count); } + public Set zrevrangeByScore(byte[] key, double max, double min) { + Jedis j = getShard(key); + return j.zrevrangeByScore(key, max, min); + } + + public Set zrevrangeByScore(byte[] key, double max, double min, + int offset, int count) { + Jedis j = getShard(key); + return j.zrevrangeByScore(key, max, min, offset, count); + } + + public Set zrevrangeByScoreWithScores(byte[] key, double max, double min) { + Jedis j = getShard(key); + return j.zrevrangeByScoreWithScores(key, max, min); + } + + public Set zrevrangeByScoreWithScores(byte[] key, double max, + double min, int offset, int count) { + Jedis j = getShard(key); + return j.zrevrangeByScoreWithScores(key, max, min, offset, count); + } + public Long zremrangeByRank(byte[] key, int start, int end) { Jedis j = getShard(key); return j.zremrangeByRank(key, start, end); diff --git a/src/main/java/redis/clients/jedis/BinaryTransaction.java b/src/main/java/redis/clients/jedis/BinaryTransaction.java index 3ae9656..9fba829 100644 --- a/src/main/java/redis/clients/jedis/BinaryTransaction.java +++ b/src/main/java/redis/clients/jedis/BinaryTransaction.java @@ -1,11 +1,13 @@ package redis.clients.jedis; +import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Set; import redis.clients.jedis.BinaryClient.LIST_POSITION; -public class BinaryTransaction { +public class BinaryTransaction extends Queable { protected Client client = null; protected boolean inTransaction = true; @@ -16,363 +18,533 @@ public class BinaryTransaction { this.client = client; } - public void ping() { - client.ping(); - } - - public void set(final byte[] key, final byte[] value) { - client.set(key, value); - } - - public void get(final byte[] key) { - client.get(key); - } - - public void exists(final byte[] key) { - client.exists(key); - } - - public void del(final byte[]... keys) { - client.del(keys); - } - - public void type(final byte[] key) { - client.type(key); - } - - public void flushDB() { - client.flushDB(); - } - - public void keys(final byte[] pattern) { - client.keys(pattern); - } - - public void randomBinaryKey() { - client.randomKey(); - } - - public void rename(final byte[] oldkey, final byte[] newkey) { - client.rename(oldkey, newkey); - } - - public void renamenx(final byte[] oldkey, final byte[] newkey) { - client.renamenx(oldkey, newkey); - } - - public void dbSize() { - client.dbSize(); - } - - public void expire(final byte[] key, final int seconds) { - client.expire(key, seconds); - } - - public void expireAt(final byte[] key, final long unixTime) { - client.expireAt(key, unixTime); - } - - public void ttl(final byte[] key) { - client.ttl(key); - } - - public void select(final int index) { - client.select(index); - } - - public void move(final byte[] key, final int dbIndex) { - client.move(key, dbIndex); - } - - public void flushAll() { - client.flushAll(); - } - - public void getSet(final byte[] key, final byte[] value) { - client.getSet(key, value); - } - - public void mget(final byte[]... keys) { - client.mget(keys); - } - - public void setnx(final byte[] key, final byte[] value) { - client.setnx(key, value); - } - - public void setex(final byte[] key, final int seconds, final byte[] value) { - client.setex(key, seconds, value); - } - - public void mset(final byte[]... keysvalues) { - client.mset(keysvalues); - } - - public void msetnx(final byte[]... keysvalues) { - client.msetnx(keysvalues); - } - - public void decrBy(final byte[] key, final int integer) { - client.decrBy(key, integer); - } - - public void decr(final byte[] key) { - client.decr(key); - } - - public void incrBy(final byte[] key, final int integer) { - client.incrBy(key, integer); - } - - public void incr(final byte[] key) { - client.incr(key); - } - - public void append(final byte[] key, final byte[] value) { - client.append(key, value); - } - - public void substr(final byte[] key, final int start, final int end) { - client.substr(key, start, end); - } - - public void hset(final byte[] key, final byte[] field, final byte[] value) { - client.hset(key, field, value); - } - - public void hget(final byte[] key, final byte[] field) { - client.hget(key, field); - } - - public void hsetnx(final byte[] key, final byte[] field, - final byte[] value) { - client.hsetnx(key, field, value); - } - - public void hmset(final byte[] key, final Map hash) { - client.hmset(key, hash); - } - - public void hmget(final byte[] key, final byte[]... fields) { - client.hmget(key, fields); - } - - public void hincrBy(final byte[] key, final byte[] field, final int value) { - client.hincrBy(key, field, value); - } - - public void hexists(final byte[] key, final byte[] field) { - client.hexists(key, field); - } - - public void hdel(final byte[] key, final byte[] field) { - client.hdel(key, field); - } - - public void hlen(final byte[] key) { - client.hlen(key); - } - - public void hkeys(final byte[] key) { - client.hkeys(key); - } - - public void hvals(final byte[] key) { - client.hvals(key); - } - - public void hgetAll(final byte[] key) { - client.hgetAll(key); - } - - public void rpush(final byte[] key, final byte[] string) { - client.rpush(key, string); - } - - public void lpush(final byte[] key, final byte[] string) { - client.lpush(key, string); - } - - public void llen(final byte[] key) { - client.llen(key); - } - - public void lrange(final byte[] key, final int start, final int end) { - client.lrange(key, start, end); - } - - public void ltrim(final byte[] key, final int start, final int end) { - client.ltrim(key, start, end); - } - - public void lindex(final byte[] key, final int index) { - client.lindex(key, index); - } - - public void lset(final byte[] key, final int index, final byte[] value) { - client.lset(key, index, value); - } - - public void lrem(final byte[] key, final int count, final byte[] value) { - client.lrem(key, count, value); - } - - public void lpop(final byte[] key) { - client.lpop(key); - } - - public void rpop(final byte[] key) { - client.rpop(key); - } - - public void rpoplpush(final byte[] srckey, final byte[] dstkey) { - client.rpoplpush(srckey, dstkey); - } - - public void sadd(final byte[] key, final byte[] member) { - client.sadd(key, member); - } - - public void smembers(final byte[] key) { - client.smembers(key); - } - - public void srem(final byte[] key, final byte[] member) { - client.srem(key, member); - } - - public void spop(final byte[] key) { - client.spop(key); - } - - public void smove(final byte[] srckey, final byte[] dstkey, - final byte[] member) { - client.smove(srckey, dstkey, member); - } - - public void scard(final byte[] key) { - client.scard(key); - } - - public void sismember(final byte[] key, final byte[] member) { - client.sismember(key, member); - } - - public void sinter(final byte[]... keys) { - client.sinter(keys); - } - - public void sinterstore(final byte[] dstkey, final byte[]... keys) { - client.sinterstore(dstkey, keys); - } - - public void sunion(final byte[]... keys) { - client.sunion(keys); - } - - public void sunionstore(final byte[] dstkey, final byte[]... keys) { - client.sunionstore(dstkey, keys); - } - - public void sdiff(final byte[]... keys) { - client.sdiff(keys); - } - - public void sdiffstore(final byte[] dstkey, final byte[]... keys) { - client.sdiffstore(dstkey, keys); - } - - public void srandmember(final byte[] key) { - client.srandmember(key); - } - - public void zadd(final byte[] key, final double score, final byte[] member) { - client.zadd(key, score, member); - } - - public void zrange(final byte[] key, final int start, final int end) { - client.zrange(key, start, end); - } - - public void zrem(final byte[] key, final byte[] member) { - client.zrem(key, member); - } - - public void zincrby(final byte[] key, final double score, - final byte[] member) { - client.zincrby(key, score, member); - } - - public void zrank(final byte[] key, final byte[] member) { - client.zrank(key, member); - } - - public void zrevrank(final byte[] key, final byte[] member) { - client.zrevrank(key, member); - } - - public void zrevrange(final byte[] key, final int start, final int end) { - client.zrevrange(key, start, end); - } - - public void zrangeWithScores(final byte[] key, final int start, - final int end) { - client.zrangeWithScores(key, start, end); - } - - public void zrevrangeWithScores(final byte[] key, final int start, - final int end) { - client.zrevrangeWithScores(key, start, end); - } - - public void zcard(final byte[] key) { - client.zcard(key); - } - - public void zscore(final byte[] key, final byte[] member) { - client.zscore(key, member); - } - public List exec() { client.exec(); client.getAll(1); // Discard all but the last reply - return client.getObjectMultiBulkReply(); + + List unformatted = client.getObjectMultiBulkReply(); + if (unformatted == null) { + return null; + } + List formatted = new ArrayList(); + for (Object o : unformatted) { + formatted.add(generateResponse(o).get()); + } + return formatted; } public String discard() { client.discard(); client.getAll(1); // Discard all but the last reply inTransaction = false; + clean(); return client.getStatusCodeReply(); } - public void sort(final byte[] key) { - client.sort(key); + public Response append(byte[] key, byte[] value) { + client.append(key, value); + return getResponse(BuilderFactory.LONG); } - public void sort(final byte[] key, final SortingParams sortingParameters) { - client.sort(key, sortingParameters); + public Response> blpop(byte[]... args) { + client.blpop(args); + return getResponse(BuilderFactory.STRING_LIST); } - public void sort(final byte[] key, final SortingParams sortingParameters, - final byte[] dstkey) { - client.sort(key, sortingParameters, dstkey); + public Response> brpop(byte[]... args) { + client.brpop(args); + return getResponse(BuilderFactory.STRING_LIST); } - public void sort(final byte[] key, final byte[] dstkey) { - client.sort(key, dstkey); + public Response decr(byte[] key) { + client.decr(key); + return getResponse(BuilderFactory.LONG); } - public void setbit(byte[] key, long offset, byte[] value) { - client.setbit(key, offset, value); + public Response decrBy(byte[] key, long integer) { + client.decrBy(key, integer); + return getResponse(BuilderFactory.LONG); } - public void getbit(byte[] key, long offset) { - client.getbit(key, offset); + public Response del(byte[]... keys) { + client.del(keys); + return getResponse(BuilderFactory.LONG); } - public void linsert(final byte[] key, final LIST_POSITION where, - final byte[] pivot, final byte[] value) { + public Response echo(byte[] string) { + client.echo(string); + return getResponse(BuilderFactory.STRING); + } + + public Response exists(byte[] key) { + client.exists(key); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response expire(byte[] key, int seconds) { + client.expire(key, seconds); + return getResponse(BuilderFactory.LONG); + } + + public Response expireAt(byte[] key, long unixTime) { + client.expireAt(key, unixTime); + return getResponse(BuilderFactory.LONG); + } + + public Response get(byte[] key) { + client.get(key); + return getResponse(BuilderFactory.STRING); + } + + public Response getSet(byte[] key, byte[] value) { + client.getSet(key, value); + return getResponse(BuilderFactory.STRING); + } + + public Response hdel(byte[] key, byte[] field) { + client.hdel(key, field); + return getResponse(BuilderFactory.LONG); + } + + public Response hexists(byte[] key, byte[] field) { + client.hexists(key, field); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response hget(byte[] key, byte[] field) { + client.hget(key, field); + return getResponse(BuilderFactory.STRING); + } + + public Response> hgetAll(byte[] key) { + client.hgetAll(key); + return getResponse(BuilderFactory.STRING_MAP); + } + + public Response hincrBy(byte[] key, byte[] field, long value) { + client.hincrBy(key, field, value); + return getResponse(BuilderFactory.LONG); + } + + public Response> hkeys(byte[] key) { + client.hkeys(key); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response hlen(byte[] key) { + client.hlen(key); + return getResponse(BuilderFactory.LONG); + } + + public Response> hmget(byte[] key, byte[]... fields) { + client.hmget(key, fields); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response hmset(byte[] key, Map hash) { + client.hmset(key, hash); + return getResponse(BuilderFactory.STRING); + } + + public Response hset(byte[] key, byte[] field, byte[] value) { + client.hset(key, field, value); + return getResponse(BuilderFactory.LONG); + } + + public Response hsetnx(byte[] key, byte[] field, byte[] value) { + client.hsetnx(key, field, value); + return getResponse(BuilderFactory.LONG); + } + + public Response> hvals(byte[] key) { + client.hvals(key); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response incr(byte[] key) { + client.incr(key); + return getResponse(BuilderFactory.LONG); + } + + public Response incrBy(byte[] key, long integer) { + client.incrBy(key, integer); + return getResponse(BuilderFactory.LONG); + } + + public Response> keys(byte[] pattern) { + client.keys(pattern); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response lindex(byte[] key, int index) { + client.lindex(key, index); + return getResponse(BuilderFactory.STRING); + } + + public Response linsert(byte[] key, LIST_POSITION where, + byte[] pivot, byte[] value) { client.linsert(key, where, pivot, value); + return getResponse(BuilderFactory.LONG); + } + + public Response llen(byte[] key) { + client.llen(key); + return getResponse(BuilderFactory.LONG); + } + + public Response lpop(byte[] key) { + client.lpop(key); + return getResponse(BuilderFactory.STRING); + } + + public Response lpush(byte[] key, byte[] string) { + client.lpush(key, string); + return getResponse(BuilderFactory.LONG); + } + + public Response lpushx(byte[] key, byte[] bytes) { + client.lpushx(key, bytes); + return getResponse(BuilderFactory.LONG); + } + + public Response> lrange(byte[] key, long start, int end) { + client.lrange(key, start, end); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response lrem(byte[] key, long count, byte[] value) { + client.lrem(key, count, value); + return getResponse(BuilderFactory.LONG); + } + + public Response lset(byte[] key, long index, byte[] value) { + client.lset(key, index, value); + return getResponse(BuilderFactory.STRING); + } + + public Response ltrim(byte[] key, long start, long end) { + client.ltrim(key, start, end); + return getResponse(BuilderFactory.STRING); + } + + public Response> mget(byte[]... keys) { + client.mget(keys); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response move(byte[] key, int dbIndex) { + client.move(key, dbIndex); + return getResponse(BuilderFactory.LONG); + } + + public Response mset(byte[]... keysvalues) { + client.mset(keysvalues); + return getResponse(BuilderFactory.STRING); + } + + public Response msetnx(byte[]... keysvalues) { + client.msetnx(keysvalues); + return getResponse(BuilderFactory.LONG); + } + + public Response persist(byte[] key) { + client.persist(key); + return getResponse(BuilderFactory.LONG); + } + + public Response rename(byte[] oldkey, byte[] newkey) { + client.rename(oldkey, newkey); + return getResponse(BuilderFactory.STRING); + } + + public Response renamenx(byte[] oldkey, byte[] newkey) { + client.renamenx(oldkey, newkey); + return getResponse(BuilderFactory.LONG); + } + + public Response rpop(byte[] key) { + client.rpop(key); + return getResponse(BuilderFactory.STRING); + } + + public Response rpoplpush(byte[] srckey, byte[] dstkey) { + client.rpoplpush(srckey, dstkey); + return getResponse(BuilderFactory.STRING); + } + + public Response rpush(byte[] key, byte[] string) { + client.rpush(key, string); + return getResponse(BuilderFactory.LONG); + } + + public Response rpushx(byte[] key, byte[] string) { + client.rpushx(key, string); + return getResponse(BuilderFactory.LONG); + } + + public Response sadd(byte[] key, byte[] member) { + client.sadd(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response scard(byte[] key) { + client.scard(key); + return getResponse(BuilderFactory.LONG); + } + + public Response> sdiff(byte[]... keys) { + client.sdiff(keys); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response sdiffstore(byte[] dstkey, byte[]... keys) { + client.sdiffstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); + } + + public Response set(byte[] key, byte[] value) { + client.set(key, value); + return getResponse(BuilderFactory.STRING); + } + + public Response setbit(String key, long offset, boolean value) { + client.setbit(key, offset, value); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response setex(byte[] key, int seconds, byte[] value) { + client.setex(key, seconds, value); + return getResponse(BuilderFactory.STRING); + } + + public Response setnx(byte[] key, byte[] value) { + client.setnx(key, value); + return getResponse(BuilderFactory.LONG); + } + + public Response> sinter(byte[]... keys) { + client.sinter(keys); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response sinterstore(byte[] dstkey, byte[]... keys) { + client.sinterstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); + } + + public Response sismember(byte[] key, byte[] member) { + client.sismember(key, member); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response> smembers(byte[] key) { + client.smembers(key); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response smove(byte[] srckey, byte[] dstkey, byte[] member) { + client.smove(srckey, dstkey, member); + return getResponse(BuilderFactory.LONG); + } + + public Response> sort(byte[] key) { + client.sort(key); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> sort(byte[] key, + SortingParams sortingParameters) { + client.sort(key, sortingParameters); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> sort(byte[] key, + SortingParams sortingParameters, byte[] dstkey) { + client.sort(key, sortingParameters, dstkey); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> sort(byte[] key, byte[] dstkey) { + client.sort(key, dstkey); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response spop(byte[] key) { + client.spop(key); + return getResponse(BuilderFactory.STRING); + } + + public Response srandmember(byte[] key) { + client.srandmember(key); + return getResponse(BuilderFactory.STRING); + } + + public Response srem(byte[] key, byte[] member) { + client.srem(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response strlen(byte[] key) { + client.strlen(key); + return getResponse(BuilderFactory.LONG); + } + + public Response substr(byte[] key, int start, int end) { + client.substr(key, start, end); + return getResponse(BuilderFactory.STRING); + } + + public Response> sunion(byte[]... keys) { + client.sunion(keys); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response sunionstore(byte[] dstkey, byte[]... keys) { + client.sunionstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); + } + + public Response ttl(byte[] key) { + client.ttl(key); + return getResponse(BuilderFactory.LONG); + } + + public Response type(byte[] key) { + client.type(key); + return getResponse(BuilderFactory.STRING); + } + + public Response watch(byte[]... keys) { + client.watch(keys); + return getResponse(BuilderFactory.STRING); + } + + public Response zadd(byte[] key, double score, byte[] member) { + client.zadd(key, score, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zcard(byte[] key) { + client.zcard(key); + return getResponse(BuilderFactory.LONG); + } + + public Response zcount(byte[] key, double min, double max) { + client.zcount(key, min, max); + return getResponse(BuilderFactory.LONG); + } + + public Response zincrby(byte[] key, double score, byte[] member) { + client.zincrby(key, score, member); + return getResponse(BuilderFactory.DOUBLE); + } + + public Response zinterstore(byte[] dstkey, byte[]... sets) { + client.zinterstore(dstkey, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response zinterstore(byte[] dstkey, ZParams params, + byte[]... sets) { + client.zinterstore(dstkey, params, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response> zrange(byte[] key, int start, int end) { + client.zrange(key, start, end); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrangeByScore(byte[] key, double min, + double max) { + client.zrangeByScore(key, min, max); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrangeByScore(byte[] key, byte[] min, + byte[] max) { + client.zrangeByScore(key, min, max); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrangeByScore(byte[] key, double min, + double max, int offset, int count) { + client.zrangeByScore(key, min, max, offset, count); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrangeByScoreWithScores(byte[] key, double min, + double max) { + client.zrangeByScoreWithScores(key, min, max); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrangeByScoreWithScores(byte[] key, double min, + double max, int offset, int count) { + client.zrangeByScoreWithScores(key, min, max, offset, count); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrangeWithScores(byte[] key, int start, int end) { + client.zrangeWithScores(key, start, end); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response zrank(byte[] key, byte[] member) { + client.zrank(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zrem(byte[] key, byte[] member) { + client.zrem(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zremrangeByRank(byte[] key, int start, int end) { + client.zremrangeByRank(key, start, end); + return getResponse(BuilderFactory.LONG); + } + + public Response zremrangeByScore(byte[] key, double start, double end) { + client.zremrangeByScore(key, start, end); + return getResponse(BuilderFactory.LONG); + } + + public Response> zrevrange(byte[] key, int start, int end) { + client.zrevrange(key, start, end); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrevrangeWithScores(byte[] key, int start, + int end) { + client.zrevrangeWithScores(key, start, end); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response zrevrank(byte[] key, byte[] member) { + client.zrevrank(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zscore(byte[] key, byte[] member) { + client.zscore(key, member); + return getResponse(BuilderFactory.DOUBLE); + } + + public Response zunionstore(byte[] dstkey, byte[]... sets) { + client.zunionstore(dstkey, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response zunionstore(byte[] dstkey, ZParams params, + byte[]... sets) { + client.zunionstore(dstkey, params, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response brpoplpush(byte[] source, byte[] destination, + int timeout) { + client.brpoplpush(source, destination, timeout); + return getResponse(BuilderFactory.STRING); } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Builder.java b/src/main/java/redis/clients/jedis/Builder.java new file mode 100644 index 0000000..e13117c --- /dev/null +++ b/src/main/java/redis/clients/jedis/Builder.java @@ -0,0 +1,5 @@ +package redis.clients.jedis; + +public abstract class Builder { + public abstract T build(Object data); +} diff --git a/src/main/java/redis/clients/jedis/BuilderFactory.java b/src/main/java/redis/clients/jedis/BuilderFactory.java new file mode 100644 index 0000000..fcbcd5c --- /dev/null +++ b/src/main/java/redis/clients/jedis/BuilderFactory.java @@ -0,0 +1,163 @@ +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; +import java.util.Map; +import java.util.Set; + +import redis.clients.util.SafeEncoder; + +public class BuilderFactory { + public static final Builder DOUBLE = new Builder() { + public Double build(Object data) { + return Double.valueOf(STRING.build(data)); + } + + public String toString() { + return "double"; + } + }; + public static final Builder BOOLEAN = new Builder() { + public Boolean build(Object data) { + return ((Long) data) == 1; + } + + public String toString() { + return "boolean"; + } + }; + public static final Builder LONG = new Builder() { + public Long build(Object data) { + return (Long) data; + } + + public String toString() { + return "long"; + } + + }; + public static final Builder STRING = new Builder() { + public String build(Object data) { + return SafeEncoder.encode((byte[]) data); + } + + public String toString() { + return "string"; + } + + }; + public static final Builder> STRING_LIST = new Builder>() { + @SuppressWarnings("unchecked") + public List build(Object data) { + if (null == data) { + return null; + } + List l = (List) data; + final ArrayList result = new ArrayList(l.size()); + for (final byte[] barray : l) { + if (barray == null) { + result.add(null); + } else { + result.add(SafeEncoder.encode(barray)); + } + } + return result; + } + + public String toString() { + return "List"; + } + + }; + public static final Builder> STRING_MAP = new Builder>() { + @SuppressWarnings("unchecked") + public Map build(Object data) { + final List flatHash = (List) data; + final Map hash = new HashMap(); + final Iterator iterator = flatHash.iterator(); + while (iterator.hasNext()) { + hash.put(SafeEncoder.encode(iterator.next()), SafeEncoder + .encode(iterator.next())); + } + + return hash; + } + + public String toString() { + return "Map"; + } + + }; + public static final Builder> STRING_SET = new Builder>() { + @SuppressWarnings("unchecked") + public Set build(Object data) { + if (null == data) { + return null; + } + List l = (List) data; + final Set result = new HashSet(l.size()); + for (final byte[] barray : l) { + if (barray == null) { + result.add(null); + } else { + result.add(SafeEncoder.encode(barray)); + } + } + return result; + } + + public String toString() { + return "Set"; + } + + }; + public static final Builder> STRING_ZSET = new Builder>() { + @SuppressWarnings("unchecked") + public Set build(Object data) { + if (null == data) { + return null; + } + List l = (List) data; + final Set result = new LinkedHashSet(l.size()); + for (final byte[] barray : l) { + if (barray == null) { + result.add(null); + } else { + result.add(SafeEncoder.encode(barray)); + } + } + return result; + } + + public String toString() { + return "ZSet"; + } + + }; + public static final Builder> TUPLE_ZSET = new Builder>() { + @SuppressWarnings("unchecked") + public Set build(Object data) { + if (null == data) { + return null; + } + List l = (List) data; + final Set result = new LinkedHashSet(l.size()); + Iterator iterator = l.iterator(); + while (iterator.hasNext()) { + result.add(new Tuple(SafeEncoder.encode(iterator.next()), + Double.valueOf(SafeEncoder.encode(iterator.next())))); + } + return result; + } + + public String toString() { + return "ZSet"; + } + + }; + +} diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index 493ef44..fb35c10 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -1,12 +1,12 @@ package redis.clients.jedis; -import static redis.clients.jedis.Protocol.toByteArray; +import redis.clients.util.SafeEncoder; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; -import redis.clients.util.SafeEncoder; +import static redis.clients.jedis.Protocol.toByteArray; public class Client extends BinaryClient implements Commands { public Client(final String host) { @@ -201,23 +201,23 @@ public class Client extends BinaryClient implements Commands { llen(SafeEncoder.encode(key)); } - public void lrange(final String key, final int start, final int end) { + public void lrange(final String key, final long start, final long end) { lrange(SafeEncoder.encode(key), start, end); } - public void ltrim(final String key, final int start, final int end) { + public void ltrim(final String key, final long start, final long end) { ltrim(SafeEncoder.encode(key), start, end); } - public void lindex(final String key, final int index) { + public void lindex(final String key, final long index) { lindex(SafeEncoder.encode(key), index); } - public void lset(final String key, final int index, final String value) { + public void lset(final String key, final long index, final String value) { lset(SafeEncoder.encode(key), index, SafeEncoder.encode(value)); } - public void lrem(final String key, int count, final String value) { + public void lrem(final String key, long count, final String value) { lrem(SafeEncoder.encode(key), count, SafeEncoder.encode(value)); } @@ -435,6 +435,33 @@ public class Client extends BinaryClient implements Commands { count); } + public void zrevrangeByScore(final String key, final double max, + final double min) { + zrevrangeByScore(SafeEncoder.encode(key), max, min); + } + + public void zrevrangeByScore(final String key, final String max, + final String min) { + zrevrangeByScore(SafeEncoder.encode(key), SafeEncoder.encode(max), + SafeEncoder.encode(min)); + } + + public void zrevrangeByScore(final String key, final double max, + final double min, final int offset, int count) { + zrevrangeByScore(SafeEncoder.encode(key), max, min, offset, count); + } + + public void zrevrangeByScoreWithScores(final String key, final double max, + final double min) { + zrevrangeByScoreWithScores(SafeEncoder.encode(key), max, min); + } + + public void zrevrangeByScoreWithScores(final String key, final double max, + final double min, final int offset, final int count) { + zrevrangeByScoreWithScores(SafeEncoder.encode(key), max, min, offset, + count); + } + public void zremrangeByRank(final String key, final int start, final int end) { zremrangeByRank(SafeEncoder.encode(key), start, end); } @@ -510,7 +537,7 @@ public class Client extends BinaryClient implements Commands { } public void setbit(final String key, final long offset, final boolean value) { - setbit(SafeEncoder.encode(key), offset, toByteArray(value ? 0 : 1)); + setbit(SafeEncoder.encode(key), offset, toByteArray(value ? 1 : 0)); } public void getbit(String key, long offset) { @@ -560,4 +587,4 @@ public class Client extends BinaryClient implements Commands { } subscribe(cs); } -} \ No newline at end of file +} diff --git a/src/main/java/redis/clients/jedis/Commands.java b/src/main/java/redis/clients/jedis/Commands.java index 36db6c0..235ee88 100644 --- a/src/main/java/redis/clients/jedis/Commands.java +++ b/src/main/java/redis/clients/jedis/Commands.java @@ -1,9 +1,9 @@ package redis.clients.jedis; -import redis.clients.jedis.BinaryClient.LIST_POSITION; - import java.util.Map; +import redis.clients.jedis.BinaryClient.LIST_POSITION; + public interface Commands { public void set(final String key, final String value); @@ -92,15 +92,15 @@ public interface Commands { public void llen(final String key); - public void lrange(final String key, final int start, final int end); + public void lrange(final String key, final long start, final long end); - public void ltrim(final String key, final int start, final int end); + public void ltrim(final String key, final long start, final long end); - public void lindex(final String key, final int index); + public void lindex(final String key, final long index); - public void lset(final String key, final int index, final String value); + public void lset(final String key, final long index, final String value); - public void lrem(final String key, int count, final String value); + public void lrem(final String key, final long count, final String value); public void lpop(final String key); @@ -197,6 +197,21 @@ public interface Commands { public void zrangeByScoreWithScores(final String key, final double min, final double max, final int offset, final int count); + public void zrevrangeByScore(final String key, final double max, + final double min); + + public void zrevrangeByScore(final String key, final String max, + final String min); + + public void zrevrangeByScore(final String key, final double max, + final double min, final int offset, int count); + + public void zrevrangeByScoreWithScores(final String key, final double max, + final double min); + + public void zrevrangeByScoreWithScores(final String key, final double max, + final double min, final int offset, final int count); + public void zremrangeByRank(final String key, final int start, final int end); public void zremrangeByScore(final String key, final double start, @@ -238,10 +253,10 @@ public interface Commands { public void configGet(final String pattern); public void configResetStat(); - + public void multi(); - + public void exec(); - + public void discard(); } \ 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 523d562..876cf51 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -193,19 +193,7 @@ 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) { - if (barray == null) { - result.add(null); - } else { - result.add(SafeEncoder.encode(barray)); - } - } - return result; + return BuilderFactory.STRING_LIST.build(getBinaryMultiBulkReply()); } @SuppressWarnings("unchecked") diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index c311857..497277d 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -1,7 +1,6 @@ package redis.clients.jedis; import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; @@ -10,7 +9,6 @@ import java.util.Map; import java.util.Set; import redis.clients.jedis.BinaryClient.LIST_POSITION; -import redis.clients.jedis.exceptions.JedisDataException; public class Jedis extends BinaryJedis implements JedisCommands { public Jedis(final String host) { @@ -30,7 +28,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { } public String ping() { - runChecks(); + checkIsInMulti(); client.ping(); return client.getStatusCodeReply(); } @@ -46,7 +44,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code reply */ public String set(final String key, String value) { - runChecks(); + checkIsInMulti(); client.set(key, value); return client.getStatusCodeReply(); } @@ -62,7 +60,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String get(final String key) { - runChecks(); + checkIsInMulti(); client.sendCommand(Protocol.Command.GET, key); return client.getBulkReply(); } @@ -71,9 +69,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * Ask the server to silently close the connection. */ - public void quit() { - runChecks(); + public String quit() { + checkIsInMulti(); client.quit(); + return client.getStatusCodeReply(); } /** @@ -84,10 +83,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * Time complexity: O(1) * * @param key - * @return Integer reply, "0" if the key exists, otherwise "1" + * @return Boolean reply, true if the key exists, otherwise false */ public Boolean exists(final String key) { - runChecks(); + checkIsInMulti(); client.exists(key); return client.getIntegerReply() == 1; } @@ -103,7 +102,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * more keys were removed 0 if none of the specified key existed */ public Long del(final String... keys) { - runChecks(); + checkIsInMulti(); client.del(keys); return client.getIntegerReply(); } @@ -123,7 +122,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * contains a Hash value */ public String type(final String key) { - runChecks(); + checkIsInMulti(); client.type(key); return client.getStatusCodeReply(); } @@ -136,7 +135,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public String flushDB() { - runChecks(); + checkIsInMulti(); client.flushDB(); return client.getStatusCodeReply(); } @@ -173,11 +172,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Multi bulk reply */ public Set keys(final String pattern) { - runChecks(); + checkIsInMulti(); client.keys(pattern); - final HashSet keySet = new HashSet(client - .getMultiBulkReply()); - return keySet; + return BuilderFactory.STRING_SET + .build(client.getBinaryMultiBulkReply()); } /** @@ -189,7 +187,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * empty string is the database is empty */ public String randomKey() { - runChecks(); + checkIsInMulti(); client.randomKey(); return client.getBulkReply(); } @@ -206,7 +204,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code repy */ public String rename(final String oldkey, final String newkey) { - runChecks(); + checkIsInMulti(); client.rename(oldkey, newkey); return client.getStatusCodeReply(); } @@ -223,7 +221,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * target key already exist */ public Long renamenx(final String oldkey, final String newkey) { - runChecks(); + checkIsInMulti(); client.renamenx(oldkey, newkey); return client.getIntegerReply(); } @@ -235,7 +233,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public Long dbSize() { - runChecks(); + checkIsInMulti(); client.dbSize(); return client.getIntegerReply(); } @@ -269,7 +267,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * exist. */ public Long expire(final String key, final int seconds) { - runChecks(); + checkIsInMulti(); client.expire(key, seconds); return client.getIntegerReply(); } @@ -305,7 +303,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * exist. */ public Long expireAt(final String key, final long unixTime) { - runChecks(); + checkIsInMulti(); client.expireAt(key, unixTime); return client.getIntegerReply(); } @@ -322,7 +320,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * have an associated expire, -1 is returned. */ public Long ttl(final String key) { - runChecks(); + checkIsInMulti(); client.ttl(key); return client.getIntegerReply(); } @@ -336,7 +334,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public String select(final int index) { - runChecks(); + checkIsInMulti(); client.select(index); return client.getStatusCodeReply(); } @@ -355,7 +353,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * found in the current DB. */ public Long move(final String key, final int dbIndex) { - runChecks(); + checkIsInMulti(); client.move(key, dbIndex); return client.getIntegerReply(); } @@ -368,7 +366,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public String flushAll() { - runChecks(); + checkIsInMulti(); client.flushAll(); return client.getStatusCodeReply(); } @@ -385,7 +383,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String getSet(final String key, final String value) { - runChecks(); + checkIsInMulti(); client.getSet(key, value); return client.getBulkReply(); } @@ -401,7 +399,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Multi bulk reply */ public List mget(final String... keys) { - runChecks(); + checkIsInMulti(); client.mget(keys); return client.getMultiBulkReply(); } @@ -419,7 +417,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * was not set */ public Long setnx(final String key, final String value) { - runChecks(); + checkIsInMulti(); client.setnx(key, value); return client.getIntegerReply(); } @@ -437,7 +435,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code reply */ public String setex(final String key, final int seconds, final String value) { - runChecks(); + checkIsInMulti(); client.setex(key, seconds, value); return client.getStatusCodeReply(); } @@ -463,7 +461,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code reply Basically +OK as MSET can't fail */ public String mset(final String... keysvalues) { - runChecks(); + checkIsInMulti(); client.mset(keysvalues); return client.getStatusCodeReply(); } @@ -490,7 +488,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * no key was set (at least one key already existed) */ public Long msetnx(final String... keysvalues) { - runChecks(); + checkIsInMulti(); client.msetnx(keysvalues); return client.getIntegerReply(); } @@ -518,7 +516,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * after the increment. */ public Long decrBy(final String key, final long integer) { - runChecks(); + checkIsInMulti(); client.decrBy(key, integer); return client.getIntegerReply(); } @@ -546,7 +544,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * after the increment. */ public Long decr(final String key) { - runChecks(); + checkIsInMulti(); client.decr(key); return client.getIntegerReply(); } @@ -574,7 +572,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * after the increment. */ public Long incrBy(final String key, final long integer) { - runChecks(); + checkIsInMulti(); client.incrBy(key, integer); return client.getIntegerReply(); } @@ -602,7 +600,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * after the increment. */ public Long incr(final String key) { - runChecks(); + checkIsInMulti(); client.incr(key); return client.getIntegerReply(); } @@ -624,7 +622,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * the append operation. */ public Long append(final String key, final String value) { - runChecks(); + checkIsInMulti(); client.append(key, value); return client.getIntegerReply(); } @@ -648,7 +646,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String substr(final String key, final int start, final int end) { - runChecks(); + checkIsInMulti(); client.substr(key, start, end); return client.getBulkReply(); } @@ -669,7 +667,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * 1 is returned. */ public Long hset(final String key, final String field, final String value) { - runChecks(); + checkIsInMulti(); client.hset(key, field, value); return client.getIntegerReply(); } @@ -688,7 +686,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String hget(final String key, final String field) { - runChecks(); + checkIsInMulti(); client.hget(key, field); return client.getBulkReply(); } @@ -705,7 +703,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * field is created 1 is returned. */ public Long hsetnx(final String key, final String field, final String value) { - runChecks(); + checkIsInMulti(); client.hsetnx(key, field, value); return client.getIntegerReply(); } @@ -720,10 +718,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * * @param key * @param hash - * @return Always OK because HMSET can't fail + * @return Return OK or Exception if hash is empty */ public String hmset(final String key, final Map hash) { - runChecks(); + checkIsInMulti(); client.hmset(key, hash); return client.getStatusCodeReply(); } @@ -742,7 +740,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * with the specified fields, in the same order of the request. */ public List hmget(final String key, final String... fields) { - runChecks(); + checkIsInMulti(); client.hmget(key, fields); return client.getMultiBulkReply(); } @@ -766,7 +764,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * operation. */ public Long hincrBy(final String key, final String field, final long value) { - runChecks(); + checkIsInMulti(); client.hincrBy(key, field, value); return client.getIntegerReply(); } @@ -782,7 +780,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * Return 0 if the key is not found or the field is not present. */ public Boolean hexists(final String key, final String field) { - runChecks(); + checkIsInMulti(); client.hexists(key, field); return client.getIntegerReply() == 1; } @@ -798,7 +796,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * returned, otherwise 0 is returned and no operation is performed. */ public Long hdel(final String key, final String field) { - runChecks(); + checkIsInMulti(); client.hdel(key, field); return client.getIntegerReply(); } @@ -814,7 +812,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * an empty hash. */ public Long hlen(final String key) { - runChecks(); + checkIsInMulti(); client.hlen(key); return client.getIntegerReply(); } @@ -828,10 +826,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return All the fields names contained into a hash. */ public Set hkeys(final String key) { - runChecks(); + checkIsInMulti(); client.hkeys(key); - final List lresult = client.getMultiBulkReply(); - return new HashSet(lresult); + return BuilderFactory.STRING_SET + .build(client.getBinaryMultiBulkReply()); } /** @@ -843,7 +841,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return All the fields values contained into a hash. */ public List hvals(final String key) { - runChecks(); + checkIsInMulti(); client.hvals(key); final List lresult = client.getMultiBulkReply(); return lresult; @@ -858,16 +856,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return All the fields and values contained into a hash. */ public Map hgetAll(final String key) { - runChecks(); + checkIsInMulti(); client.hgetAll(key); - final List flatHash = client.getMultiBulkReply(); - final Map hash = new HashMap(); - final Iterator iterator = flatHash.iterator(); - while (iterator.hasNext()) { - hash.put(iterator.next(), iterator.next()); - } - - return hash; + return BuilderFactory.STRING_MAP + .build(client.getBinaryMultiBulkReply()); } /** @@ -886,7 +878,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * list after the push operation. */ public Long rpush(final String key, final String string) { - runChecks(); + checkIsInMulti(); client.rpush(key, string); return client.getIntegerReply(); } @@ -907,7 +899,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * list after the push operation. */ public Long lpush(final String key, final String string) { - runChecks(); + checkIsInMulti(); client.lpush(key, string); return client.getIntegerReply(); } @@ -923,7 +915,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return The length of the list. */ public Long llen(final String key) { - runChecks(); + checkIsInMulti(); client.llen(key); return client.getIntegerReply(); } @@ -966,8 +958,8 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Multi bulk reply, specifically a list of elements in the * specified range. */ - public List lrange(final String key, final int start, final int end) { - runChecks(); + public List lrange(final String key, final long start, final long end) { + checkIsInMulti(); client.lrange(key, start, end); return client.getMultiBulkReply(); } @@ -1006,8 +998,8 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @param end * @return Status code reply */ - public String ltrim(final String key, final int start, final int end) { - runChecks(); + public String ltrim(final String key, final long start, final long end) { + checkIsInMulti(); client.ltrim(key, start, end); return client.getStatusCodeReply(); } @@ -1030,8 +1022,8 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @param index * @return Bulk reply, specifically the requested element */ - public String lindex(final String key, final int index) { - runChecks(); + public String lindex(final String key, final long index) { + checkIsInMulti(); client.lindex(key, index); return client.getBulkReply(); } @@ -1057,8 +1049,8 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @param value * @return Status code reply */ - public String lset(final String key, final int index, final String value) { - runChecks(); + public String lset(final String key, final long index, final String value) { + checkIsInMulti(); client.lset(key, index, value); return client.getStatusCodeReply(); } @@ -1082,8 +1074,8 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Integer Reply, specifically: The number of removed elements if * the operation succeeded */ - public Long lrem(final String key, final int count, final String value) { - runChecks(); + public Long lrem(final String key, final long count, final String value) { + checkIsInMulti(); client.lrem(key, count, value); return client.getIntegerReply(); } @@ -1102,7 +1094,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String lpop(final String key) { - runChecks(); + checkIsInMulti(); client.lpop(key); return client.getBulkReply(); } @@ -1121,7 +1113,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String rpop(final String key) { - runChecks(); + checkIsInMulti(); client.rpop(key); return client.getBulkReply(); } @@ -1145,7 +1137,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String rpoplpush(final String srckey, final String dstkey) { - runChecks(); + checkIsInMulti(); client.rpoplpush(srckey, dstkey); return client.getBulkReply(); } @@ -1164,7 +1156,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * the element was already a member of the set */ public Long sadd(final String key, final String member) { - runChecks(); + checkIsInMulti(); client.sadd(key, member); return client.getIntegerReply(); } @@ -1179,7 +1171,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Multi bulk reply */ public Set smembers(final String key) { - runChecks(); + checkIsInMulti(); client.smembers(key); final List members = client.getMultiBulkReply(); return new HashSet(members); @@ -1198,7 +1190,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * if the new element was not a member of the set */ public Long srem(final String key, final String member) { - runChecks(); + checkIsInMulti(); client.srem(key, member); return client.getIntegerReply(); } @@ -1216,7 +1208,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String spop(final String key) { - runChecks(); + checkIsInMulti(); client.spop(key); return client.getBulkReply(); } @@ -1246,7 +1238,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public Long smove(final String srckey, final String dstkey, final String member) { - runChecks(); + checkIsInMulti(); client.smove(srckey, dstkey, member); return client.getIntegerReply(); } @@ -1260,7 +1252,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * of the set as an integer. */ public Long scard(final String key) { - runChecks(); + checkIsInMulti(); client.scard(key); return client.getIntegerReply(); } @@ -1278,7 +1270,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * does not exist */ public Boolean sismember(final String key, final String member) { - runChecks(); + checkIsInMulti(); client.sismember(key, member); return client.getIntegerReply() == 1; } @@ -1303,7 +1295,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Multi bulk reply, specifically the list of common elements. */ public Set sinter(final String... keys) { - runChecks(); + checkIsInMulti(); client.sinter(keys); final List members = client.getMultiBulkReply(); return new HashSet(members); @@ -1321,7 +1313,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code reply */ public Long sinterstore(final String dstkey, final String... keys) { - runChecks(); + checkIsInMulti(); client.sinterstore(dstkey, keys); return client.getIntegerReply(); } @@ -1343,7 +1335,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Multi bulk reply, specifically the list of common elements. */ public Set sunion(final String... keys) { - runChecks(); + checkIsInMulti(); client.sunion(keys); final List members = client.getMultiBulkReply(); return new HashSet(members); @@ -1362,7 +1354,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code reply */ public Long sunionstore(final String dstkey, final String... keys) { - runChecks(); + checkIsInMulti(); client.sunionstore(dstkey, keys); return client.getIntegerReply(); } @@ -1391,10 +1383,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { * the first set provided and all the successive sets. */ public Set sdiff(final String... keys) { - runChecks(); + checkIsInMulti(); client.sdiff(keys); - final List members = client.getMultiBulkReply(); - return new HashSet(members); + return BuilderFactory.STRING_SET + .build(client.getBinaryMultiBulkReply()); } /** @@ -1406,7 +1398,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Status code reply */ public Long sdiffstore(final String dstkey, final String... keys) { - runChecks(); + checkIsInMulti(); client.sdiffstore(dstkey, keys); return client.getIntegerReply(); } @@ -1424,7 +1416,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return Bulk reply */ public String srandmember(final String key) { - runChecks(); + checkIsInMulti(); client.srandmember(key); return client.getBulkReply(); } @@ -1451,13 +1443,13 @@ public class Jedis extends BinaryJedis implements JedisCommands { * was updated */ public Long zadd(final String key, final double score, final String member) { - runChecks(); + checkIsInMulti(); client.zadd(key, score, member); return client.getIntegerReply(); } public Set zrange(final String key, final int start, final int end) { - runChecks(); + checkIsInMulti(); client.zrange(key, start, end); final List members = client.getMultiBulkReply(); return new LinkedHashSet(members); @@ -1479,7 +1471,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * if the new element was not a member of the set */ public Long zrem(final String key, final String member) { - runChecks(); + checkIsInMulti(); client.zrem(key, member); return client.getIntegerReply(); } @@ -1510,7 +1502,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public Double zincrby(final String key, final double score, final String member) { - runChecks(); + checkIsInMulti(); client.zincrby(key, score, member); String newscore = client.getBulkReply(); return Double.valueOf(newscore); @@ -1537,7 +1529,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * reply if there is no such element. */ public Long zrank(final String key, final String member) { - runChecks(); + checkIsInMulti(); client.zrank(key, member); return client.getIntegerReply(); } @@ -1563,14 +1555,14 @@ public class Jedis extends BinaryJedis implements JedisCommands { * reply if there is no such element. */ public Long zrevrank(final String key, final String member) { - runChecks(); + checkIsInMulti(); client.zrevrank(key, member); return client.getIntegerReply(); } public Set zrevrange(final String key, final int start, final int end) { - runChecks(); + checkIsInMulti(); client.zrevrange(key, start, end); final List members = client.getMultiBulkReply(); return new LinkedHashSet(members); @@ -1578,7 +1570,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { public Set zrangeWithScores(final String key, final int start, final int end) { - runChecks(); + checkIsInMulti(); client.zrangeWithScores(key, start, end); Set set = getTupledSet(); return set; @@ -1586,7 +1578,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { public Set zrevrangeWithScores(final String key, final int start, final int end) { - runChecks(); + checkIsInMulti(); client.zrevrangeWithScores(key, start, end); Set set = getTupledSet(); return set; @@ -1602,7 +1594,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return the cardinality (number of elements) of the set as an integer. */ public Long zcard(final String key) { - runChecks(); + checkIsInMulti(); client.zcard(key); return client.getIntegerReply(); } @@ -1619,33 +1611,12 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return the score */ public Double zscore(final String key, final String member) { - runChecks(); + checkIsInMulti(); client.zscore(key, member); final String score = client.getBulkReply(); return (score != null ? new Double(score) : null); } - private void runChecks() { - if (client.isInMulti()) { - throw new JedisDataException( - "Cannot use Jedis when in Multi. Please use JedisTransaction instead."); - } - this.connect(); - } - - public void connect() { - if (!client.isConnected()) { - client.connect(); - if (this.password != null) { - this.auth(this.password); - } - } - } - - public void disconnect() { - client.disconnect(); - } - public String watch(final String... keys) { client.watch(keys); return client.getStatusCodeReply(); @@ -1669,7 +1640,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * smallest to the biggest number. */ public List sort(final String key) { - runChecks(); + checkIsInMulti(); client.sort(key); return client.getMultiBulkReply(); } @@ -1752,7 +1723,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public List sort(final String key, final SortingParams sortingParameters) { - runChecks(); + checkIsInMulti(); client.sort(key, sortingParameters); return client.getMultiBulkReply(); } @@ -1830,7 +1801,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * programming language used. */ public List blpop(final int timeout, final String... keys) { - runChecks(); + checkIsInMulti(); List args = new ArrayList(); for (String arg : keys) { args.add(arg); @@ -1859,7 +1830,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public Long sort(final String key, final SortingParams sortingParameters, final String dstkey) { - runChecks(); + checkIsInMulti(); client.sort(key, sortingParameters, dstkey); return client.getIntegerReply(); } @@ -1881,7 +1852,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @return The number of elements of the list at dstkey. */ public Long sort(final String key, final String dstkey) { - runChecks(); + checkIsInMulti(); client.sort(key, dstkey); return client.getIntegerReply(); } @@ -1959,7 +1930,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * programming language used. */ public List brpop(final int timeout, final String... keys) { - runChecks(); + checkIsInMulti(); List args = new ArrayList(); for (String arg : keys) { args.add(arg); @@ -1991,33 +1962,35 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public String auth(final String password) { - runChecks(); + checkIsInMulti(); client.auth(password); return client.getStatusCodeReply(); } public void subscribe(JedisPubSub jedisPubSub, String... channels) { - runChecks(); + checkIsInMulti(); + connect(); client.setTimeoutInfinite(); jedisPubSub.proceed(client, channels); client.rollbackTimeout(); } public Long publish(String channel, String message) { - runChecks(); + checkIsInMulti(); client.publish(channel, message); return client.getIntegerReply(); } public void psubscribe(JedisPubSub jedisPubSub, String... patterns) { - runChecks(); + checkIsInMulti(); + connect(); client.setTimeoutInfinite(); jedisPubSub.proceedWithPatterns(client, patterns); client.rollbackTimeout(); } public Long zcount(final String key, final double min, final double max) { - runChecks(); + checkIsInMulti(); client.zcount(key, min, max); return client.getIntegerReply(); } @@ -2081,14 +2054,14 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public Set zrangeByScore(final String key, final double min, final double max) { - runChecks(); + checkIsInMulti(); client.zrangeByScore(key, min, max); return new LinkedHashSet(client.getMultiBulkReply()); } public Set zrangeByScore(final String key, final String min, final String max) { - runChecks(); + checkIsInMulti(); client.zrangeByScore(key, min, max); return new LinkedHashSet(client.getMultiBulkReply()); } @@ -2151,7 +2124,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public Set zrangeByScore(final String key, final double min, final double max, final int offset, final int count) { - runChecks(); + checkIsInMulti(); client.zrangeByScore(key, min, max, offset, count); return new LinkedHashSet(client.getMultiBulkReply()); } @@ -2214,7 +2187,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public Set zrangeByScoreWithScores(final String key, final double min, final double max) { - runChecks(); + checkIsInMulti(); client.zrangeByScoreWithScores(key, min, max); Set set = getTupledSet(); return set; @@ -2279,14 +2252,14 @@ public class Jedis extends BinaryJedis implements JedisCommands { public Set zrangeByScoreWithScores(final String key, final double min, final double max, final int offset, final int count) { - runChecks(); + checkIsInMulti(); client.zrangeByScoreWithScores(key, min, max, offset, count); Set set = getTupledSet(); return set; } private Set getTupledSet() { - runChecks(); + checkIsInMulti(); List membersWithScores = client.getMultiBulkReply(); Set set = new LinkedHashSet(); Iterator iterator = membersWithScores.iterator(); @@ -2298,6 +2271,44 @@ public class Jedis extends BinaryJedis implements JedisCommands { return set; } + public Set zrevrangeByScore(final String key, final double max, + final double min) { + checkIsInMulti(); + client.zrevrangeByScore(key, max, min); + return new LinkedHashSet(client.getMultiBulkReply()); + } + + public Set zrevrangeByScore(final String key, final String max, + final String min) { + checkIsInMulti(); + client.zrevrangeByScore(key, max, min); + return new LinkedHashSet(client.getMultiBulkReply()); + } + + public Set zrevrangeByScore(final String key, final double max, + final double min, final int offset, final int count) { + checkIsInMulti(); + client.zrevrangeByScore(key, max, min, offset, count); + return new LinkedHashSet(client.getMultiBulkReply()); + } + + public Set zrevrangeByScoreWithScores(final String key, + final double max, final double min) { + checkIsInMulti(); + client.zrevrangeByScoreWithScores(key, max, min); + Set set = getTupledSet(); + return set; + } + + public Set zrevrangeByScoreWithScores(final String key, + final double max, final double min, final int offset, + final int count) { + checkIsInMulti(); + client.zrevrangeByScoreWithScores(key, max, min, offset, count); + Set set = getTupledSet(); + 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 @@ -2312,7 +2323,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * */ public Long zremrangeByRank(final String key, final int start, final int end) { - runChecks(); + checkIsInMulti(); client.zremrangeByRank(key, start, end); return client.getIntegerReply(); } @@ -2333,7 +2344,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public Long zremrangeByScore(final String key, final double start, final double end) { - runChecks(); + checkIsInMulti(); client.zremrangeByScore(key, start, end); return client.getIntegerReply(); } @@ -2377,7 +2388,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * set at dstkey */ public Long zunionstore(final String dstkey, final String... sets) { - runChecks(); + checkIsInMulti(); client.zunionstore(dstkey, sets); return client.getIntegerReply(); } @@ -2423,7 +2434,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public Long zunionstore(final String dstkey, final ZParams params, final String... sets) { - runChecks(); + checkIsInMulti(); client.zunionstore(dstkey, params, sets); return client.getIntegerReply(); } @@ -2467,7 +2478,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { * set at dstkey */ public Long zinterstore(final String dstkey, final String... sets) { - runChecks(); + checkIsInMulti(); client.zinterstore(dstkey, sets); return client.getIntegerReply(); } @@ -2513,7 +2524,7 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public Long zinterstore(final String dstkey, final ZParams params, final String... sets) { - runChecks(); + checkIsInMulti(); client.zinterstore(dstkey, params, sets); return client.getIntegerReply(); } @@ -2570,7 +2581,10 @@ public class Jedis extends BinaryJedis implements JedisCommands { */ public String brpoplpush(String source, String destination, int timeout) { client.brpoplpush(source, destination, timeout); - return client.getBulkReply(); + client.setTimeoutInfinite(); + String reply = client.getBulkReply(); + client.rollbackTimeout(); + return reply; } /** @@ -2607,4 +2621,4 @@ public class Jedis extends BinaryJedis implements JedisCommands { client.getrange(key, startOffset, endOffset); return client.getBulkReply(); } -} \ No newline at end of file +} diff --git a/src/main/java/redis/clients/jedis/JedisCommands.java b/src/main/java/redis/clients/jedis/JedisCommands.java index 004b6ac..3ea49bc 100644 --- a/src/main/java/redis/clients/jedis/JedisCommands.java +++ b/src/main/java/redis/clients/jedis/JedisCommands.java @@ -79,15 +79,15 @@ public interface JedisCommands { Long llen(String key); - List lrange(String key, int start, int end); + List lrange(String key, long start, long end); - String ltrim(String key, int start, int end); + String ltrim(String key, long start, long end); - String lindex(String key, int index); + String lindex(String key, long index); - String lset(String key, int index, String value); + String lset(String key, long index, String value); - Long lrem(String key, int count, String value); + Long lrem(String key, long count, String value); String lpop(String key); @@ -137,14 +137,24 @@ public interface JedisCommands { Set zrangeByScore(String key, double min, double max); + Set zrevrangeByScore(String key, double max, double min); + Set zrangeByScore(String key, double min, double max, int offset, int count); + Set zrevrangeByScore(String key, double max, double min, + int offset, int count); + Set zrangeByScoreWithScores(String key, double min, double max); + Set zrevrangeByScoreWithScores(String key, double max, double min); + Set zrangeByScoreWithScores(String key, double min, double max, int offset, int count); + Set zrevrangeByScoreWithScores(String key, double max, double min, + int offset, int count); + Long zremrangeByRank(String key, int start, int end); Long zremrangeByScore(String key, double start, double end); diff --git a/src/main/java/redis/clients/jedis/JedisMonitor.java b/src/main/java/redis/clients/jedis/JedisMonitor.java index bd2654b..dff2672 100644 --- a/src/main/java/redis/clients/jedis/JedisMonitor.java +++ b/src/main/java/redis/clients/jedis/JedisMonitor.java @@ -5,6 +5,7 @@ public abstract class JedisMonitor { public void proceed(Client client) { this.client = client; + this.client.setTimeoutInfinite(); do { String command = client.getBulkReply(); onCommand(command); diff --git a/src/main/java/redis/clients/jedis/JedisPubSub.java b/src/main/java/redis/clients/jedis/JedisPubSub.java index 48a1a72..d07a409 100644 --- a/src/main/java/redis/clients/jedis/JedisPubSub.java +++ b/src/main/java/redis/clients/jedis/JedisPubSub.java @@ -10,6 +10,7 @@ import static redis.clients.jedis.Protocol.Keyword.UNSUBSCRIBE; import java.util.Arrays; import java.util.List; +import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisException; import redis.clients.util.SafeEncoder; @@ -31,6 +32,10 @@ public abstract class JedisPubSub { public abstract void onPSubscribe(String pattern, int subscribedChannels); public void unsubscribe() { + if (client == null) { + throw new JedisConnectionException( + "JedisPubSub was not subscribed to a Jedis instance."); + } client.unsubscribe(); client.flush(); } diff --git a/src/main/java/redis/clients/jedis/Pipeline.java b/src/main/java/redis/clients/jedis/Pipeline.java index 8dc7fd0..40d8030 100644 --- a/src/main/java/redis/clients/jedis/Pipeline.java +++ b/src/main/java/redis/clients/jedis/Pipeline.java @@ -1,857 +1,1151 @@ package redis.clients.jedis; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; + import redis.clients.jedis.BinaryClient.LIST_POSITION; -import java.util.List; -import java.util.Map; - -public class Pipeline implements Commands { +public class Pipeline extends Queable { private Client client; - public Pipeline(Client client) { + public void setClient(Client client) { this.client = client; } - public void append(String key, String value) { + public List sync() { + List unformatted = client.getAll(); + List formatted = new ArrayList(); + for (Object o : unformatted) { + formatted.add(generateResponse(o).get()); + } + return formatted; + } + + public Response append(String key, String value) { client.append(key, value); + return getResponse(BuilderFactory.LONG); } - public void append(byte[] key, byte[] value) { + public Response append(byte[] key, byte[] value) { client.append(key, value); + return getResponse(BuilderFactory.LONG); } - public void blpop(String[] args) { + public Response> blpop(String... args) { client.blpop(args); + return getResponse(BuilderFactory.STRING_LIST); } - public void blpop(byte[][] args) { + public Response> blpop(byte[]... args) { client.blpop(args); + return getResponse(BuilderFactory.STRING_LIST); } - public void brpop(String[] args) { + public Response> brpop(String... args) { client.brpop(args); + return getResponse(BuilderFactory.STRING_LIST); } - public void brpop(byte[][] args) { + public Response> brpop(byte[]... args) { client.brpop(args); + return getResponse(BuilderFactory.STRING_LIST); } - public void decr(String key) { + public Response decr(String key) { client.decr(key); + return getResponse(BuilderFactory.LONG); } - public void decr(byte[] key) { + public Response decr(byte[] key) { client.decr(key); + return getResponse(BuilderFactory.LONG); } - public void decrBy(String key, long integer) { + public Response decrBy(String key, long integer) { client.decrBy(key, integer); + return getResponse(BuilderFactory.LONG); } - public void decrBy(byte[] key, long integer) { + public Response decrBy(byte[] key, long integer) { client.decrBy(key, integer); + return getResponse(BuilderFactory.LONG); } - public void del(String... keys) { + public Response del(String... keys) { client.del(keys); + return getResponse(BuilderFactory.LONG); } - public void del(byte[]... keys) { + public Response del(byte[]... keys) { client.del(keys); + return getResponse(BuilderFactory.LONG); } - public void echo(String string) { + public Response echo(String string) { client.echo(string); + return getResponse(BuilderFactory.STRING); } - public void echo(byte[] string) { + public Response echo(byte[] string) { client.echo(string); + return getResponse(BuilderFactory.STRING); } - public void exists(String key) { + public Response exists(String key) { client.exists(key); + return getResponse(BuilderFactory.BOOLEAN); } - public void exists(byte[] key) { + public Response exists(byte[] key) { client.exists(key); + return getResponse(BuilderFactory.BOOLEAN); } - public void expire(String key, int seconds) { + public Response expire(String key, int seconds) { client.expire(key, seconds); + return getResponse(BuilderFactory.LONG); } - public void expire(byte[] key, int seconds) { + public Response expire(byte[] key, int seconds) { client.expire(key, seconds); + return getResponse(BuilderFactory.LONG); } - public void expireAt(String key, long unixTime) { + public Response expireAt(String key, long unixTime) { client.expireAt(key, unixTime); + return getResponse(BuilderFactory.LONG); } - public void expireAt(byte[] key, long unixTime) { + public Response expireAt(byte[] key, long unixTime) { client.expireAt(key, unixTime); + return getResponse(BuilderFactory.LONG); } - public void get(String key) { + public Response get(String key) { client.get(key); + return getResponse(BuilderFactory.STRING); } - public void get(byte[] key) { + public Response get(byte[] key) { client.get(key); + return getResponse(BuilderFactory.STRING); } - public void getbit(String key, long offset) { + public Response getbit(String key, long offset) { client.getbit(key, offset); + return getResponse(BuilderFactory.BOOLEAN); } - public void getrange(String key, long startOffset, long endOffset) { + public Response getrange(String key, long startOffset, + long endOffset) { client.getrange(key, startOffset, endOffset); + return getResponse(BuilderFactory.STRING); } - public void getSet(String key, String value) { + public Response getSet(String key, String value) { client.getSet(key, value); + return getResponse(BuilderFactory.STRING); } - public void getSet(byte[] key, byte[] value) { + public Response getSet(byte[] key, byte[] value) { client.getSet(key, value); + return getResponse(BuilderFactory.STRING); } - public void hdel(String key, String field) { + public Response hdel(String key, String field) { client.hdel(key, field); + return getResponse(BuilderFactory.LONG); } - public void hdel(byte[] key, byte[] field) { + public Response hdel(byte[] key, byte[] field) { client.hdel(key, field); + return getResponse(BuilderFactory.LONG); } - public void hexists(String key, String field) { + public Response hexists(String key, String field) { client.hexists(key, field); + return getResponse(BuilderFactory.BOOLEAN); } - public void hexists(byte[] key, byte[] field) { + public Response hexists(byte[] key, byte[] field) { client.hexists(key, field); + return getResponse(BuilderFactory.BOOLEAN); } - public void hget(String key, String field) { + public Response hget(String key, String field) { client.hget(key, field); + return getResponse(BuilderFactory.STRING); } - public void hget(byte[] key, byte[] field) { + public Response hget(byte[] key, byte[] field) { client.hget(key, field); + return getResponse(BuilderFactory.STRING); } - public void hgetAll(String key) { + public Response> hgetAll(String key) { client.hgetAll(key); + return getResponse(BuilderFactory.STRING_MAP); } - public void hgetAll(byte[] key) { + public Response> hgetAll(byte[] key) { client.hgetAll(key); + return getResponse(BuilderFactory.STRING_MAP); } - public void hincrBy(String key, String field, long value) { + public Response hincrBy(String key, String field, long value) { client.hincrBy(key, field, value); + return getResponse(BuilderFactory.LONG); } - public void hincrBy(byte[] key, byte[] field, long value) { + public Response hincrBy(byte[] key, byte[] field, long value) { client.hincrBy(key, field, value); + return getResponse(BuilderFactory.LONG); } - public void hkeys(String key) { + public Response> hkeys(String key) { client.hkeys(key); + return getResponse(BuilderFactory.STRING_SET); } - public void hkeys(byte[] key) { + public Response> hkeys(byte[] key) { client.hkeys(key); + return getResponse(BuilderFactory.STRING_SET); } - public void hlen(String key) { + public Response hlen(String key) { client.hlen(key); + return getResponse(BuilderFactory.LONG); } - public void hlen(byte[] key) { + public Response hlen(byte[] key) { client.hlen(key); + return getResponse(BuilderFactory.LONG); } - public void hmget(String key, String... fields) { + public Response> hmget(String key, String... fields) { client.hmget(key, fields); + return getResponse(BuilderFactory.STRING_LIST); } - public void hmget(byte[] key, byte[]... fields) { + public Response> hmget(byte[] key, byte[]... fields) { client.hmget(key, fields); + return getResponse(BuilderFactory.STRING_LIST); } - public void hmset(String key, Map hash) { + public Response hmset(String key, Map hash) { client.hmset(key, hash); + return getResponse(BuilderFactory.STRING); } - public void hmset(byte[] key, Map hash) { + public Response hmset(byte[] key, Map hash) { client.hmset(key, hash); + return getResponse(BuilderFactory.STRING); } - public void hset(String key, String field, String value) { + public Response hset(String key, String field, String value) { client.hset(key, field, value); + return getResponse(BuilderFactory.LONG); } - public void hset(byte[] key, byte[] field, byte[] value) { + public Response hset(byte[] key, byte[] field, byte[] value) { client.hset(key, field, value); + return getResponse(BuilderFactory.LONG); } - public void hsetnx(String key, String field, String value) { + public Response hsetnx(String key, String field, String value) { client.hsetnx(key, field, value); + return getResponse(BuilderFactory.LONG); } - public void hsetnx(byte[] key, byte[] field, byte[] value) { + public Response hsetnx(byte[] key, byte[] field, byte[] value) { client.hsetnx(key, field, value); + return getResponse(BuilderFactory.LONG); } - public void hvals(String key) { + public Response> hvals(String key) { client.hvals(key); + return getResponse(BuilderFactory.STRING_LIST); } - public void hvals(byte[] key) { + public Response> hvals(byte[] key) { client.hvals(key); + return getResponse(BuilderFactory.STRING_LIST); } - public void incr(String key) { + public Response incr(String key) { client.incr(key); + return getResponse(BuilderFactory.LONG); } - public void incr(byte[] key) { + public Response incr(byte[] key) { client.incr(key); + return getResponse(BuilderFactory.LONG); } - public void incrBy(String key, long integer) { + public Response incrBy(String key, long integer) { client.incrBy(key, integer); + return getResponse(BuilderFactory.LONG); } - public void incrBy(byte[] key, long integer) { + public Response incrBy(byte[] key, long integer) { client.incrBy(key, integer); + return getResponse(BuilderFactory.LONG); } - public void keys(String pattern) { + public Response> keys(String pattern) { client.keys(pattern); + return getResponse(BuilderFactory.STRING_SET); } - public void keys(byte[] pattern) { + public Response> keys(byte[] pattern) { client.keys(pattern); + return getResponse(BuilderFactory.STRING_SET); } - public void lindex(String key, int index) { + public Response lindex(String key, int index) { client.lindex(key, index); + return getResponse(BuilderFactory.STRING); } - public void lindex(byte[] key, int index) { + public Response lindex(byte[] key, int index) { client.lindex(key, index); + return getResponse(BuilderFactory.STRING); } - public void linsert(String key, LIST_POSITION where, String pivot, - String value) { + public Response linsert(String key, LIST_POSITION where, + String pivot, String value) { client.linsert(key, where, pivot, value); + return getResponse(BuilderFactory.LONG); } - public void linsert(byte[] key, LIST_POSITION where, byte[] pivot, - byte[] value) { + public Response linsert(byte[] key, LIST_POSITION where, + byte[] pivot, byte[] value) { client.linsert(key, where, pivot, value); + return getResponse(BuilderFactory.LONG); } - public void llen(String key) { + public Response llen(String key) { client.llen(key); + return getResponse(BuilderFactory.LONG); } - public void llen(byte[] key) { + public Response llen(byte[] key) { client.llen(key); + return getResponse(BuilderFactory.LONG); } - public void lpop(String key) { + public Response lpop(String key) { client.lpop(key); + return getResponse(BuilderFactory.STRING); } - public void lpop(byte[] key) { + public Response lpop(byte[] key) { client.lpop(key); + return getResponse(BuilderFactory.STRING); } - public void lpush(String key, String string) { + public Response lpush(String key, String string) { client.lpush(key, string); + return getResponse(BuilderFactory.LONG); } - public void lpush(byte[] key, byte[] string) { + public Response lpush(byte[] key, byte[] string) { client.lpush(key, string); + return getResponse(BuilderFactory.LONG); } - public void lpushx(String key, String string) { + public Response lpushx(String key, String string) { client.lpushx(key, string); + return getResponse(BuilderFactory.LONG); } - public void lpushx(byte[] key, byte[] bytes) { + public Response lpushx(byte[] key, byte[] bytes) { client.lpushx(key, bytes); + return getResponse(BuilderFactory.LONG); } - public void lrange(String key, int start, int end) { + public Response> lrange(String key, long start, long end) { client.lrange(key, start, end); + return getResponse(BuilderFactory.STRING_LIST); } - public void lrange(byte[] key, int start, int end) { + public Response> lrange(byte[] key, long start, long end) { client.lrange(key, start, end); + return getResponse(BuilderFactory.STRING_LIST); } - public void lrem(String key, int count, String value) { + public Response lrem(String key, long count, String value) { client.lrem(key, count, value); + return getResponse(BuilderFactory.LONG); } - public void lrem(byte[] key, int count, byte[] value) { + public Response lrem(byte[] key, long count, byte[] value) { client.lrem(key, count, value); + return getResponse(BuilderFactory.LONG); } - public void lset(String key, int index, String value) { + public Response lset(String key, long index, String value) { client.lset(key, index, value); + return getResponse(BuilderFactory.STRING); } - public void lset(byte[] key, int index, byte[] value) { + public Response lset(byte[] key, long index, byte[] value) { client.lset(key, index, value); + return getResponse(BuilderFactory.STRING); } - public void ltrim(String key, int start, int end) { + public Response ltrim(String key, long start, long end) { client.ltrim(key, start, end); + return getResponse(BuilderFactory.STRING); } - public void ltrim(byte[] key, int start, int end) { + public Response ltrim(byte[] key, long start, long end) { client.ltrim(key, start, end); + return getResponse(BuilderFactory.STRING); } - public void mget(String... keys) { + public Response> mget(String... keys) { client.mget(keys); + return getResponse(BuilderFactory.STRING_LIST); } - public void mget(byte[]... keys) { + public Response> mget(byte[]... keys) { client.mget(keys); + return getResponse(BuilderFactory.STRING_LIST); } - public void move(String key, int dbIndex) { + public Response move(String key, int dbIndex) { client.move(key, dbIndex); + return getResponse(BuilderFactory.LONG); } - public void move(byte[] key, int dbIndex) { + public Response move(byte[] key, int dbIndex) { client.move(key, dbIndex); + return getResponse(BuilderFactory.LONG); } - public void mset(String... keysvalues) { + public Response mset(String... keysvalues) { client.mset(keysvalues); + return getResponse(BuilderFactory.STRING); } - public void mset(byte[]... keysvalues) { + public Response mset(byte[]... keysvalues) { client.mset(keysvalues); + return getResponse(BuilderFactory.STRING); } - public void msetnx(String... keysvalues) { + public Response msetnx(String... keysvalues) { client.msetnx(keysvalues); + return getResponse(BuilderFactory.LONG); } - public void msetnx(byte[]... keysvalues) { + public Response msetnx(byte[]... keysvalues) { client.msetnx(keysvalues); + return getResponse(BuilderFactory.LONG); } - public void persist(String key) { + public Response persist(String key) { client.persist(key); + return getResponse(BuilderFactory.LONG); } - public void persist(byte[] key) { + public Response persist(byte[] key) { client.persist(key); + return getResponse(BuilderFactory.LONG); } - public void rename(String oldkey, String newkey) { + public Response rename(String oldkey, String newkey) { client.rename(oldkey, newkey); + return getResponse(BuilderFactory.STRING); } - public void rename(byte[] oldkey, byte[] newkey) { + public Response rename(byte[] oldkey, byte[] newkey) { client.rename(oldkey, newkey); + return getResponse(BuilderFactory.STRING); } - public void renamenx(String oldkey, String newkey) { + public Response renamenx(String oldkey, String newkey) { client.renamenx(oldkey, newkey); + return getResponse(BuilderFactory.LONG); } - public void renamenx(byte[] oldkey, byte[] newkey) { + public Response renamenx(byte[] oldkey, byte[] newkey) { client.renamenx(oldkey, newkey); + return getResponse(BuilderFactory.LONG); } - public void rpop(String key) { + public Response rpop(String key) { client.rpop(key); + return getResponse(BuilderFactory.STRING); } - public void rpop(byte[] key) { + public Response rpop(byte[] key) { client.rpop(key); + return getResponse(BuilderFactory.STRING); } - public void rpoplpush(String srckey, String dstkey) { + public Response rpoplpush(String srckey, String dstkey) { client.rpoplpush(srckey, dstkey); + return getResponse(BuilderFactory.STRING); } - public void rpoplpush(byte[] srckey, byte[] dstkey) { + public Response rpoplpush(byte[] srckey, byte[] dstkey) { client.rpoplpush(srckey, dstkey); + return getResponse(BuilderFactory.STRING); } - public void rpush(String key, String string) { + public Response rpush(String key, String string) { client.rpush(key, string); + return getResponse(BuilderFactory.LONG); } - public void rpush(byte[] key, byte[] string) { + public Response rpush(byte[] key, byte[] string) { client.rpush(key, string); + return getResponse(BuilderFactory.LONG); } - public void rpushx(String key, String string) { + public Response rpushx(String key, String string) { client.rpushx(key, string); + return getResponse(BuilderFactory.LONG); } - public void rpushx(byte[] key, byte[] string) { + public Response rpushx(byte[] key, byte[] string) { client.rpushx(key, string); + return getResponse(BuilderFactory.LONG); } - public void sadd(String key, String member) { + public Response sadd(String key, String member) { client.sadd(key, member); + return getResponse(BuilderFactory.LONG); } - public void sadd(byte[] key, byte[] member) { + public Response sadd(byte[] key, byte[] member) { client.sadd(key, member); + return getResponse(BuilderFactory.LONG); } - public void scard(String key) { + public Response scard(String key) { client.scard(key); + return getResponse(BuilderFactory.LONG); } - public void scard(byte[] key) { + public Response scard(byte[] key) { client.scard(key); + return getResponse(BuilderFactory.LONG); } - public void sdiff(String... keys) { + public Response> sdiff(String... keys) { client.sdiff(keys); + return getResponse(BuilderFactory.STRING_SET); } - public void sdiff(byte[]... keys) { + public Response> sdiff(byte[]... keys) { client.sdiff(keys); + return getResponse(BuilderFactory.STRING_SET); } - public void sdiffstore(String dstkey, String... keys) { + public Response sdiffstore(String dstkey, String... keys) { client.sdiffstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); } - public void sdiffstore(byte[] dstkey, byte[]... keys) { + public Response sdiffstore(byte[] dstkey, byte[]... keys) { client.sdiffstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); } - public void set(String key, String value) { + public Response set(String key, String value) { client.set(key, value); + return getResponse(BuilderFactory.STRING); } - public void set(byte[] key, byte[] value) { + public Response set(byte[] key, byte[] value) { client.set(key, value); + return getResponse(BuilderFactory.STRING); } - public void setbit(String key, long offset, boolean value) { + public Response setbit(String key, long offset, boolean value) { client.setbit(key, offset, value); + return getResponse(BuilderFactory.BOOLEAN); } - public void setex(String key, int seconds, String value) { + public Response setex(String key, int seconds, String value) { client.setex(key, seconds, value); + return getResponse(BuilderFactory.STRING); } - public void setex(byte[] key, int seconds, byte[] value) { + public Response setex(byte[] key, int seconds, byte[] value) { client.setex(key, seconds, value); + return getResponse(BuilderFactory.STRING); } - public void setnx(String key, String value) { + public Response setnx(String key, String value) { client.setnx(key, value); + return getResponse(BuilderFactory.LONG); } - public void setnx(byte[] key, byte[] value) { + public Response setnx(byte[] key, byte[] value) { client.setnx(key, value); + return getResponse(BuilderFactory.LONG); } - public void setrange(String key, long offset, String value) { + public Response setrange(String key, long offset, String value) { client.setrange(key, offset, value); + return getResponse(BuilderFactory.LONG); } - public void sinter(String... keys) { + public Response> sinter(String... keys) { client.sinter(keys); + return getResponse(BuilderFactory.STRING_SET); } - public void sinter(byte[]... keys) { + public Response> sinter(byte[]... keys) { client.sinter(keys); + return getResponse(BuilderFactory.STRING_SET); } - public void sinterstore(String dstkey, String... keys) { + public Response sinterstore(String dstkey, String... keys) { client.sinterstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); } - public void sinterstore(byte[] dstkey, byte[]... keys) { + public Response sinterstore(byte[] dstkey, byte[]... keys) { client.sinterstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); } - public void sismember(String key, String member) { + public Response sismember(String key, String member) { client.sismember(key, member); + return getResponse(BuilderFactory.BOOLEAN); } - public void sismember(byte[] key, byte[] member) { + public Response sismember(byte[] key, byte[] member) { client.sismember(key, member); + return getResponse(BuilderFactory.BOOLEAN); } - public void smembers(String key) { + public Response> smembers(String key) { client.smembers(key); + return getResponse(BuilderFactory.STRING_SET); } - public void smembers(byte[] key) { + public Response> smembers(byte[] key) { client.smembers(key); + return getResponse(BuilderFactory.STRING_SET); } - public void smove(String srckey, String dstkey, String member) { + public Response smove(String srckey, String dstkey, String member) { client.smove(srckey, dstkey, member); + return getResponse(BuilderFactory.LONG); } - public void smove(byte[] srckey, byte[] dstkey, byte[] member) { + public Response smove(byte[] srckey, byte[] dstkey, byte[] member) { client.smove(srckey, dstkey, member); + return getResponse(BuilderFactory.LONG); } - public void sort(String key) { + public Response> sort(String key) { client.sort(key); + return getResponse(BuilderFactory.STRING_LIST); } - public void sort(byte[] key) { + public Response> sort(byte[] key) { client.sort(key); + return getResponse(BuilderFactory.STRING_LIST); } - public void sort(String key, SortingParams sortingParameters) { + public Response> sort(String key, + SortingParams sortingParameters) { client.sort(key, sortingParameters); + return getResponse(BuilderFactory.STRING_LIST); } - public void sort(byte[] key, SortingParams sortingParameters) { + public Response> sort(byte[] key, + SortingParams sortingParameters) { client.sort(key, sortingParameters); + return getResponse(BuilderFactory.STRING_LIST); } - public void sort(String key, SortingParams sortingParameters, String dstkey) { + public Response> sort(String key, + SortingParams sortingParameters, String dstkey) { client.sort(key, sortingParameters, dstkey); + return getResponse(BuilderFactory.STRING_LIST); } - public void sort(byte[] key, SortingParams sortingParameters, byte[] dstkey) { + public Response> sort(byte[] key, + SortingParams sortingParameters, byte[] dstkey) { client.sort(key, sortingParameters, dstkey); + return getResponse(BuilderFactory.STRING_LIST); } - public void sort(String key, String dstkey) { + public Response> sort(String key, String dstkey) { client.sort(key, dstkey); + return getResponse(BuilderFactory.STRING_LIST); } - public void sort(byte[] key, byte[] dstkey) { + public Response> sort(byte[] key, byte[] dstkey) { client.sort(key, dstkey); + return getResponse(BuilderFactory.STRING_LIST); } - public void spop(String key) { + public Response spop(String key) { client.spop(key); + return getResponse(BuilderFactory.STRING); } - public void spop(byte[] key) { + public Response spop(byte[] key) { client.spop(key); + return getResponse(BuilderFactory.STRING); } - public void srandmember(String key) { + public Response srandmember(String key) { client.srandmember(key); + return getResponse(BuilderFactory.STRING); } - public void srandmember(byte[] key) { + public Response srandmember(byte[] key) { client.srandmember(key); + return getResponse(BuilderFactory.STRING); } - public void srem(String key, String member) { + public Response srem(String key, String member) { client.srem(key, member); + return getResponse(BuilderFactory.LONG); } - public void srem(byte[] key, byte[] member) { + public Response srem(byte[] key, byte[] member) { client.srem(key, member); + return getResponse(BuilderFactory.LONG); } - public void strlen(String key) { + public Response strlen(String key) { client.strlen(key); + return getResponse(BuilderFactory.LONG); } - public void strlen(byte[] key) { + public Response strlen(byte[] key) { client.strlen(key); + return getResponse(BuilderFactory.LONG); } - public void substr(String key, int start, int end) { + public Response substr(String key, int start, int end) { client.substr(key, start, end); + return getResponse(BuilderFactory.STRING); } - public void substr(byte[] key, int start, int end) { + public Response substr(byte[] key, int start, int end) { client.substr(key, start, end); + return getResponse(BuilderFactory.STRING); } - public void sunion(String... keys) { + public Response> sunion(String... keys) { client.sunion(keys); + return getResponse(BuilderFactory.STRING_SET); } - public void sunion(byte[]... keys) { + public Response> sunion(byte[]... keys) { client.sunion(keys); + return getResponse(BuilderFactory.STRING_SET); } - public void sunionstore(String dstkey, String... keys) { + public Response sunionstore(String dstkey, String... keys) { client.sunionstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); } - public void sunionstore(byte[] dstkey, byte[]... keys) { + public Response sunionstore(byte[] dstkey, byte[]... keys) { client.sunionstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); } - public void ttl(String key) { + public Response ttl(String key) { client.ttl(key); + return getResponse(BuilderFactory.LONG); } - public void ttl(byte[] key) { + public Response ttl(byte[] key) { client.ttl(key); + return getResponse(BuilderFactory.LONG); } - public void type(String key) { + public Response type(String key) { client.type(key); + return getResponse(BuilderFactory.STRING); } - public void type(byte[] key) { + public Response type(byte[] key) { client.type(key); + return getResponse(BuilderFactory.STRING); } - public void watch(String... keys) { + public Response watch(String... keys) { client.watch(keys); + return getResponse(BuilderFactory.STRING); } - public void watch(byte[]... keys) { + public Response watch(byte[]... keys) { client.watch(keys); + return getResponse(BuilderFactory.STRING); } - public void zadd(String key, double score, String member) { + public Response zadd(String key, double score, String member) { client.zadd(key, score, member); + return getResponse(BuilderFactory.LONG); } - public void zadd(byte[] key, double score, byte[] member) { + public Response zadd(byte[] key, double score, byte[] member) { client.zadd(key, score, member); + return getResponse(BuilderFactory.LONG); } - public void zcard(String key) { + public Response zcard(String key) { client.zcard(key); + return getResponse(BuilderFactory.LONG); } - public void zcard(byte[] key) { + public Response zcard(byte[] key) { client.zcard(key); + return getResponse(BuilderFactory.LONG); } - public void zcount(String key, double min, double max) { + public Response zcount(String key, double min, double max) { client.zcount(key, min, max); + return getResponse(BuilderFactory.LONG); } - public void zcount(byte[] key, double min, double max) { + public Response zcount(byte[] key, double min, double max) { client.zcount(key, min, max); + return getResponse(BuilderFactory.LONG); } - public void zincrby(String key, double score, String member) { + public Response zincrby(String key, double score, String member) { client.zincrby(key, score, member); + return getResponse(BuilderFactory.DOUBLE); } - public void zincrby(byte[] key, double score, byte[] member) { + public Response zincrby(byte[] key, double score, byte[] member) { client.zincrby(key, score, member); + return getResponse(BuilderFactory.DOUBLE); } - public void zinterstore(String dstkey, String... sets) { + public Response zinterstore(String dstkey, String... sets) { client.zinterstore(dstkey, sets); + return getResponse(BuilderFactory.LONG); } - public void zinterstore(byte[] dstkey, byte[]... sets) { + public Response zinterstore(byte[] dstkey, byte[]... sets) { client.zinterstore(dstkey, sets); + return getResponse(BuilderFactory.LONG); } - public void zinterstore(String dstkey, ZParams params, String... sets) { + public Response zinterstore(String dstkey, ZParams params, + String... sets) { client.zinterstore(dstkey, params, sets); + return getResponse(BuilderFactory.LONG); } - public void zinterstore(byte[] dstkey, ZParams params, byte[]... sets) { + public Response zinterstore(byte[] dstkey, ZParams params, + byte[]... sets) { client.zinterstore(dstkey, params, sets); + return getResponse(BuilderFactory.LONG); } - public void zrange(String key, int start, int end) { + public Response> zrange(String key, int start, int end) { client.zrange(key, start, end); + return getResponse(BuilderFactory.STRING_ZSET); } - public void zrange(byte[] key, int start, int end) { + public Response> zrange(byte[] key, int start, int end) { client.zrange(key, start, end); + return getResponse(BuilderFactory.STRING_ZSET); } - public void zrangeByScore(String key, double min, double max) { + public Response> zrangeByScore(String key, double min, + double max) { client.zrangeByScore(key, min, max); + return getResponse(BuilderFactory.STRING_ZSET); } - public void zrangeByScore(byte[] key, double min, double max) { + public Response> zrangeByScore(byte[] key, double min, + double max) { client.zrangeByScore(key, min, max); + return getResponse(BuilderFactory.STRING_ZSET); } - public void zrangeByScore(String key, String min, String max) { + public Response> zrangeByScore(String key, String min, + String max) { client.zrangeByScore(key, min, max); + return getResponse(BuilderFactory.STRING_ZSET); } - public void zrangeByScore(byte[] key, byte[] min, byte[] max) { + public Response> zrangeByScore(byte[] key, byte[] min, + byte[] max) { client.zrangeByScore(key, min, max); + return getResponse(BuilderFactory.STRING_ZSET); } - public void zrangeByScore(String key, double min, double max, int offset, - int count) { + public Response> zrangeByScore(String key, double min, + double max, int offset, int count) { client.zrangeByScore(key, min, max, offset, count); + return getResponse(BuilderFactory.STRING_ZSET); } - public void zrangeByScore(byte[] key, double min, double max, int offset, - int count) { + public Response> zrangeByScore(byte[] key, double min, + double max, int offset, int count) { client.zrangeByScore(key, min, max, offset, count); + return getResponse(BuilderFactory.STRING_ZSET); } - public void zrangeByScoreWithScores(String key, double min, double max) { + public Response> zrangeByScoreWithScores(String key, double min, + double max) { client.zrangeByScoreWithScores(key, min, max); + return getResponse(BuilderFactory.TUPLE_ZSET); } - public void zrangeByScoreWithScores(byte[] key, double min, double max) { + public Response> zrangeByScoreWithScores(byte[] key, double min, + double max) { client.zrangeByScoreWithScores(key, min, max); + return getResponse(BuilderFactory.TUPLE_ZSET); } - public void zrangeByScoreWithScores(String key, double min, double max, - int offset, int count) { + public Response> zrangeByScoreWithScores(String key, double min, + double max, int offset, int count) { client.zrangeByScoreWithScores(key, min, max, offset, count); + return getResponse(BuilderFactory.TUPLE_ZSET); } - public void zrangeByScoreWithScores(byte[] key, double min, double max, - int offset, int count) { + public Response> zrangeByScoreWithScores(byte[] key, double min, + double max, int offset, int count) { client.zrangeByScoreWithScores(key, min, max, offset, count); + return getResponse(BuilderFactory.TUPLE_ZSET); } - public void zrangeWithScores(String key, int start, int end) { + public Response> zrevrangeByScore(String key, double max, + double min) { + client.zrevrangeByScore(key, max, min); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrevrangeByScore(byte[] key, double max, + double min) { + client.zrevrangeByScore(key, max, min); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrevrangeByScore(String key, String max, + String min) { + client.zrevrangeByScore(key, max, min); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrevrangeByScore(byte[] key, byte[] max, + byte[] min) { + client.zrevrangeByScore(key, max, min); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrevrangeByScore(String key, double max, + double min, int offset, int count) { + client.zrevrangeByScore(key, max, min, offset, count); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrevrangeByScore(byte[] key, double max, + double min, int offset, int count) { + client.zrevrangeByScore(key, max, min, offset, count); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrevrangeByScoreWithScores(String key, + double max, double min) { + client.zrevrangeByScoreWithScores(key, max, min); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrevrangeByScoreWithScores(byte[] key, + double max, double min) { + client.zrevrangeByScoreWithScores(key, max, min); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrevrangeByScoreWithScores(String key, + double max, double min, int offset, int count) { + client.zrevrangeByScoreWithScores(key, max, min, offset, count); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrevrangeByScoreWithScores(byte[] key, + double max, double min, int offset, int count) { + client.zrevrangeByScoreWithScores(key, max, min, offset, count); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrangeWithScores(String key, int start, int end) { client.zrangeWithScores(key, start, end); + return getResponse(BuilderFactory.TUPLE_ZSET); } - public void zrangeWithScores(byte[] key, int start, int end) { + public Response> zrangeWithScores(byte[] key, int start, int end) { client.zrangeWithScores(key, start, end); + return getResponse(BuilderFactory.TUPLE_ZSET); } - public void zrank(String key, String member) { + public Response zrank(String key, String member) { client.zrank(key, member); + return getResponse(BuilderFactory.LONG); } - public void zrank(byte[] key, byte[] member) { + public Response zrank(byte[] key, byte[] member) { client.zrank(key, member); + return getResponse(BuilderFactory.LONG); } - public void zrem(String key, String member) { + public Response zrem(String key, String member) { client.zrem(key, member); + return getResponse(BuilderFactory.LONG); } - public void zrem(byte[] key, byte[] member) { + public Response zrem(byte[] key, byte[] member) { client.zrem(key, member); + return getResponse(BuilderFactory.LONG); } - public void zremrangeByRank(String key, int start, int end) { + public Response zremrangeByRank(String key, int start, int end) { client.zremrangeByRank(key, start, end); + return getResponse(BuilderFactory.LONG); } - public void zremrangeByRank(byte[] key, int start, int end) { + public Response zremrangeByRank(byte[] key, int start, int end) { client.zremrangeByRank(key, start, end); + return getResponse(BuilderFactory.LONG); } - public void zremrangeByScore(String key, double start, double end) { + public Response zremrangeByScore(String key, double start, double end) { client.zremrangeByScore(key, start, end); + return getResponse(BuilderFactory.LONG); } - public void zremrangeByScore(byte[] key, double start, double end) { + public Response zremrangeByScore(byte[] key, double start, double end) { client.zremrangeByScore(key, start, end); + return getResponse(BuilderFactory.LONG); } - public void zrevrange(String key, int start, int end) { + public Response> zrevrange(String key, int start, int end) { client.zrevrange(key, start, end); + return getResponse(BuilderFactory.STRING_ZSET); } - public void zrevrange(byte[] key, int start, int end) { + public Response> zrevrange(byte[] key, int start, int end) { client.zrevrange(key, start, end); + return getResponse(BuilderFactory.STRING_ZSET); } - public void zrevrangeWithScores(String key, int start, int end) { + public Response> zrevrangeWithScores(String key, int start, + int end) { client.zrevrangeWithScores(key, start, end); + return getResponse(BuilderFactory.TUPLE_ZSET); } - public void zrevrangeWithScores(byte[] key, int start, int end) { + public Response> zrevrangeWithScores(byte[] key, int start, + int end) { client.zrevrangeWithScores(key, start, end); + return getResponse(BuilderFactory.TUPLE_ZSET); } - public void zrevrank(String key, String member) { + public Response zrevrank(String key, String member) { client.zrevrank(key, member); + return getResponse(BuilderFactory.LONG); } - public void zrevrank(byte[] key, byte[] member) { + public Response zrevrank(byte[] key, byte[] member) { client.zrevrank(key, member); + return getResponse(BuilderFactory.LONG); } - public void zscore(String key, String member) { + public Response zscore(String key, String member) { client.zscore(key, member); + return getResponse(BuilderFactory.DOUBLE); } - public void zscore(byte[] key, byte[] member) { + public Response zscore(byte[] key, byte[] member) { client.zscore(key, member); + return getResponse(BuilderFactory.DOUBLE); } - public void zunionstore(String dstkey, String... sets) { + public Response zunionstore(String dstkey, String... sets) { client.zunionstore(dstkey, sets); + return getResponse(BuilderFactory.LONG); } - public void zunionstore(byte[] dstkey, byte[]... sets) { + public Response zunionstore(byte[] dstkey, byte[]... sets) { client.zunionstore(dstkey, sets); + return getResponse(BuilderFactory.LONG); } - public void zunionstore(String dstkey, ZParams params, String... sets) { + public Response zunionstore(String dstkey, ZParams params, + String... sets) { client.zunionstore(dstkey, params, sets); + return getResponse(BuilderFactory.LONG); } - public void zunionstore(byte[] dstkey, ZParams params, byte[]... sets) { + public Response zunionstore(byte[] dstkey, ZParams params, + byte[]... sets) { client.zunionstore(dstkey, params, sets); + return getResponse(BuilderFactory.LONG); } - public List execute() { - return client.getAll(); - } - - public void bgrewriteaof() { + public Response bgrewriteaof() { client.bgrewriteaof(); + return getResponse(BuilderFactory.STRING); } - public void bgsave() { + public Response bgsave() { client.bgsave(); + return getResponse(BuilderFactory.STRING); } - public void configGet(String pattern) { + public Response configGet(String pattern) { client.configGet(pattern); + return getResponse(BuilderFactory.STRING); } - public void configSet(String parameter, String value) { + public Response configSet(String parameter, String value) { client.configSet(parameter, value); + return getResponse(BuilderFactory.STRING); } - public void brpoplpush(String source, String destination, int timeout) { + public Response brpoplpush(String source, String destination, + int timeout) { client.brpoplpush(source, destination, timeout); + return getResponse(BuilderFactory.STRING); } - public void brpoplpush(byte[] source, byte[] destination, int timeout) { + public Response brpoplpush(byte[] source, byte[] destination, + int timeout) { client.brpoplpush(source, destination, timeout); + return getResponse(BuilderFactory.STRING); } - public void configResetStat() { + public Response configResetStat() { client.configResetStat(); + return getResponse(BuilderFactory.STRING); } - public void save() { + public Response save() { client.save(); + return getResponse(BuilderFactory.STRING); } - public void lastsave() { + public Response lastsave() { client.lastsave(); + return getResponse(BuilderFactory.LONG); } - public void discard() { + public Response discard() { client.discard(); + return getResponse(BuilderFactory.STRING); } public void exec() { @@ -861,4 +1155,14 @@ public class Pipeline implements Commands { public void multi() { client.multi(); } + + public Response publish(String channel, String message) { + client.publish(channel, message); + return getResponse(BuilderFactory.LONG); + } + + public Response publish(byte[] channel, byte[] message) { + client.publish(channel, message); + return getResponse(BuilderFactory.LONG); + } } diff --git a/src/main/java/redis/clients/jedis/PipelineBlock.java b/src/main/java/redis/clients/jedis/PipelineBlock.java index dbde9c4..9afc391 100644 --- a/src/main/java/redis/clients/jedis/PipelineBlock.java +++ b/src/main/java/redis/clients/jedis/PipelineBlock.java @@ -1,466 +1,6 @@ package redis.clients.jedis; -import redis.clients.jedis.BinaryClient.LIST_POSITION; - -import java.util.Map; - -public abstract class PipelineBlock implements Commands { - private Client client; - - public void setClient(Client client) { - this.client = client; - } - - public void append(String key, String value) { - client.append(key, value); - } - - public void blpop(String[] args) { - client.blpop(args); - } - - public void brpop(String[] args) { - client.brpop(args); - } - - public void decr(String key) { - client.decr(key); - } - - public void decrBy(String key, long integer) { - client.decrBy(key, integer); - } - - public void del(String... keys) { - client.del(keys); - } - - public void echo(String string) { - client.echo(string); - } - - public void exists(String key) { - client.exists(key); - } - - public void expire(String key, int seconds) { - client.expire(key, seconds); - } - - public void expireAt(String key, long unixTime) { - client.expireAt(key, unixTime); - } - - public void get(String key) { - client.get(key); - } - - public void getbit(String key, long offset) { - client.getbit(key, offset); - } - - public void getrange(String key, long startOffset, long endOffset) { - client.getrange(key, startOffset, endOffset); - } - - public void getSet(String key, String value) { - client.getSet(key, value); - } - - public void hdel(String key, String field) { - client.hdel(key, field); - } - - public void hexists(String key, String field) { - client.hexists(key, field); - } - - public void hget(String key, String field) { - client.hget(key, field); - } - - public void hgetAll(String key) { - client.hgetAll(key); - } - - public void hincrBy(String key, String field, long value) { - client.hincrBy(key, field, value); - } - - public void hkeys(String key) { - client.hkeys(key); - } - - public void hlen(String key) { - client.hlen(key); - } - - public void hmget(String key, String... fields) { - client.hmget(key, fields); - } - - public void hmset(String key, Map hash) { - client.hmset(key, hash); - } - - public void hset(String key, String field, String value) { - client.hset(key, field, value); - } - - public void hsetnx(String key, String field, String value) { - client.hsetnx(key, field, value); - } - - public void hvals(String key) { - client.hvals(key); - } - - public void incr(String key) { - client.incr(key); - } - - public void incrBy(String key, long integer) { - client.incrBy(key, integer); - } - - public void keys(String pattern) { - client.keys(pattern); - } - - public void lindex(String key, int index) { - client.lindex(key, index); - } - - public void linsert(String key, LIST_POSITION where, String pivot, - String value) { - client.linsert(key, where, pivot, value); - } - - public void llen(String key) { - client.llen(key); - } - - public void lpop(String key) { - client.lpop(key); - } - - public void lpush(String key, String string) { - client.lpush(key, string); - } - - public void lpushx(String key, String string) { - client.lpushx(key, string); - } - - public void lrange(String key, int start, int end) { - client.lrange(key, start, end); - } - - public void lrem(String key, int count, String value) { - client.lrem(key, count, value); - } - - public void lset(String key, int index, String value) { - client.lset(key, index, value); - } - - public void ltrim(String key, int start, int end) { - client.ltrim(key, start, end); - } - - public void mget(String... keys) { - client.mget(keys); - } - - public void move(String key, int dbIndex) { - client.move(key, dbIndex); - } - - public void mset(String... keysvalues) { - client.mset(keysvalues); - } - - public void msetnx(String... keysvalues) { - client.msetnx(keysvalues); - } - - public void persist(String key) { - client.persist(key); - } - - public void rename(String oldkey, String newkey) { - client.rename(oldkey, newkey); - } - - public void renamenx(String oldkey, String newkey) { - client.renamenx(oldkey, newkey); - } - - public void rpop(String key) { - client.rpop(key); - } - - public void rpoplpush(String srckey, String dstkey) { - client.rpoplpush(srckey, dstkey); - } - - public void rpush(String key, String string) { - client.rpush(key, string); - } - - public void rpushx(String key, String string) { - client.rpushx(key, string); - } - - public void sadd(String key, String member) { - client.sadd(key, member); - } - - public void scard(String key) { - client.scard(key); - } - - public void sdiff(String... keys) { - client.sdiff(keys); - } - - public void sdiffstore(String dstkey, String... keys) { - client.sdiffstore(dstkey, keys); - } - - public void set(String key, String value) { - client.set(key, value); - } - - public void setbit(String key, long offset, boolean value) { - client.setbit(key, offset, value); - } - - public void setex(String key, int seconds, String value) { - client.setex(key, seconds, value); - } - - public void setnx(String key, String value) { - client.setnx(key, value); - } - - public void setrange(String key, long offset, String value) { - client.setrange(key, offset, value); - } - - public void sinter(String... keys) { - client.sinter(keys); - } - - public void sinterstore(String dstkey, String... keys) { - client.sinterstore(dstkey, keys); - } - - public void sismember(String key, String member) { - client.sismember(key, member); - } - - public void smembers(String key) { - client.smembers(key); - } - - public void smove(String srckey, String dstkey, String member) { - client.smove(srckey, dstkey, member); - } - - public void sort(String key) { - client.sort(key); - } - - public void sort(String key, SortingParams sortingParameters) { - client.sort(key, sortingParameters); - } - - public void sort(String key, SortingParams sortingParameters, String dstkey) { - client.sort(key, sortingParameters, dstkey); - } - - public void sort(String key, String dstkey) { - client.sort(key, dstkey); - } - - public void spop(String key) { - client.spop(key); - } - - public void srandmember(String key) { - client.srandmember(key); - } - - public void srem(String key, String member) { - client.srem(key, member); - } - - public void strlen(String key) { - client.strlen(key); - } - - public void substr(String key, int start, int end) { - client.substr(key, start, end); - } - - public void sunion(String... keys) { - client.sunion(keys); - } - - public void sunionstore(String dstkey, String... keys) { - client.sunionstore(dstkey, keys); - } - - public void ttl(String key) { - client.ttl(key); - } - - public void type(String key) { - client.type(key); - } - - public void watch(String... keys) { - client.watch(keys); - } - - public void zadd(String key, double score, String member) { - client.zadd(key, score, member); - } - - public void zcard(String key) { - client.zcard(key); - } - - public void zcount(String key, double min, double max) { - client.zcount(key, min, max); - } - - public void zincrby(String key, double score, String member) { - client.zincrby(key, score, member); - } - - public void zinterstore(String dstkey, String... sets) { - client.zinterstore(dstkey, sets); - } - - public void zinterstore(String dstkey, ZParams params, String... sets) { - client.zinterstore(dstkey, params, sets); - } - - public void zrange(String key, int start, int end) { - client.zrange(key, start, end); - } - - public void zrangeByScore(String key, double min, double max) { - client.zrangeByScore(key, min, max); - } - - public void zrangeByScore(String key, String min, String max) { - client.zrangeByScore(key, min, max); - } - - public void zrangeByScore(String key, double min, double max, int offset, - int count) { - client.zrangeByScore(key, min, max, offset, count); - } - - public void zrangeByScoreWithScores(String key, double min, double max) { - client.zrangeByScoreWithScores(key, min, max); - } - - public void zrangeByScoreWithScores(String key, double min, double max, - int offset, int count) { - client.zrangeByScoreWithScores(key, min, max, offset, count); - } - - public void zrangeWithScores(String key, int start, int end) { - client.zrangeWithScores(key, start, end); - } - - public void zrank(String key, String member) { - client.zrank(key, member); - } - - public void zrem(String key, String member) { - client.zrem(key, member); - } - - public void zremrangeByRank(String key, int start, int end) { - client.zremrangeByRank(key, start, end); - } - - public void zremrangeByScore(String key, double start, double end) { - client.zremrangeByScore(key, start, end); - } - - public void zrevrange(String key, int start, int end) { - client.zrevrange(key, start, end); - } - - public void zrevrangeWithScores(String key, int start, int end) { - client.zrevrangeWithScores(key, start, end); - } - - public void zrevrank(String key, String member) { - client.zrevrank(key, member); - } - - public void zscore(String key, String member) { - client.zscore(key, member); - } - - public void zunionstore(String dstkey, String... sets) { - client.zunionstore(dstkey, sets); - } - - public void zunionstore(String dstkey, ZParams params, String... sets) { - client.zunionstore(dstkey, params, sets); - } - - public void bgrewriteaof() { - client.bgrewriteaof(); - } - - public void bgsave() { - client.bgsave(); - } - - public void configGet(String pattern) { - client.configGet(pattern); - } - - public void configSet(String parameter, String value) { - client.configSet(parameter, value); - } - - public void brpoplpush(String source, String destination, int timeout) { - client.brpoplpush(source, destination, timeout); - } - - public void configResetStat() { - client.configResetStat(); - } - - public void save() { - client.save(); - } - - public void lastsave() { - client.lastsave(); - } - - public void discard() { - client.discard(); - } - - public void exec() { - client.exec(); - } - - public void multi() { - client.multi(); - } +public abstract class PipelineBlock extends Pipeline { public abstract void execute(); } diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index d92b785..c2ad2b5 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -135,7 +135,7 @@ public final class Protocol { } public static enum Command { - PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT, SETRANGE, GETRANGE; + PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREVRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT, SETRANGE, GETRANGE; public final byte[] raw; @@ -153,4 +153,4 @@ public final class Protocol { } } -} \ No newline at end of file +} diff --git a/src/main/java/redis/clients/jedis/Queable.java b/src/main/java/redis/clients/jedis/Queable.java new file mode 100644 index 0000000..b8ff0e5 --- /dev/null +++ b/src/main/java/redis/clients/jedis/Queable.java @@ -0,0 +1,27 @@ +package redis.clients.jedis; + +import java.util.LinkedList; +import java.util.Queue; + +public class Queable { + private Queue> pipelinedResponses = new LinkedList>(); + + protected void clean() { + pipelinedResponses.clear(); + } + + protected Response generateResponse(Object data) { + Response response = pipelinedResponses.poll(); + if (response != null) { + response.set(data); + } + return response; + } + + protected Response getResponse(Builder builder) { + Response lr = new Response(builder); + pipelinedResponses.add(lr); + return lr; + } + +} diff --git a/src/main/java/redis/clients/jedis/Response.java b/src/main/java/redis/clients/jedis/Response.java new file mode 100644 index 0000000..13a77cb --- /dev/null +++ b/src/main/java/redis/clients/jedis/Response.java @@ -0,0 +1,31 @@ +package redis.clients.jedis; + +import redis.clients.jedis.exceptions.JedisDataException; + +public class Response { + protected T response = null; + private boolean built = false; + private Builder builder; + + public Response(Builder b) { + this.builder = b; + } + + public void set(Object data) { + response = builder.build(data); + built = true; + } + + public T get() { + if (!built) { + throw new JedisDataException( + "Please close pipeline or multi block before calling this method."); + } + return response; + } + + public String toString() { + return "Response " + builder.toString(); + } + +} diff --git a/src/main/java/redis/clients/jedis/ShardedJedis.java b/src/main/java/redis/clients/jedis/ShardedJedis.java index 6db8635..fbb8c96 100644 --- a/src/main/java/redis/clients/jedis/ShardedJedis.java +++ b/src/main/java/redis/clients/jedis/ShardedJedis.java @@ -169,6 +169,11 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands { return j.hexists(key, field); } + public Long del(String key) { + Jedis j = getShard(key); + return j.del(key); + } + public Long hdel(String key, String field) { Jedis j = getShard(key); return j.hdel(key, field); @@ -209,27 +214,27 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands { return j.llen(key); } - public List lrange(String key, int start, int end) { + public List lrange(String key, long start, long end) { Jedis j = getShard(key); return j.lrange(key, start, end); } - public String ltrim(String key, int start, int end) { + public String ltrim(String key, long start, long end) { Jedis j = getShard(key); return j.ltrim(key, start, end); } - public String lindex(String key, int index) { + public String lindex(String key, long index) { Jedis j = getShard(key); return j.lindex(key, index); } - public String lset(String key, int index, String value) { + public String lset(String key, long index, String value) { Jedis j = getShard(key); return j.lset(key, index, value); } - public Long lrem(String key, int count, String value) { + public Long lrem(String key, long count, String value) { Jedis j = getShard(key); return j.lrem(key, count, value); } @@ -354,23 +359,46 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands { return j.zrangeByScore(key, min, max); } + public Set zrevrangeByScore(String key, double max, double min) { + Jedis j = getShard(key); + return j.zrevrangeByScore(key, max, min); + } + public Set zrangeByScore(String key, double min, double max, int offset, int count) { Jedis j = getShard(key); return j.zrangeByScore(key, min, max, offset, count); } + public Set zrevrangeByScore(String key, double max, double min, + int offset, int count) { + Jedis j = getShard(key); + return j.zrevrangeByScore(key, max, min, offset, count); + } + public Set zrangeByScoreWithScores(String key, double min, double max) { Jedis j = getShard(key); return j.zrangeByScoreWithScores(key, min, max); } + public Set zrevrangeByScoreWithScores(String key, double max, + double min) { + Jedis j = getShard(key); + return j.zrevrangeByScoreWithScores(key, max, min); + } + public Set zrangeByScoreWithScores(String key, double min, double max, int offset, int count) { Jedis j = getShard(key); return j.zrangeByScoreWithScores(key, min, max, offset, count); } + public Set zrevrangeByScoreWithScores(String key, double max, + double min, int offset, int count) { + Jedis j = getShard(key); + return j.zrevrangeByScoreWithScores(key, max, min, offset, count); + } + public Long zremrangeByRank(String key, int start, int end) { Jedis j = getShard(key); return j.zremrangeByRank(key, start, end); @@ -386,4 +414,4 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands { Jedis j = getShard(key); return j.linsert(key, where, pivot, value); } -} \ 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 ad57c0f..544ce73 100644 --- a/src/main/java/redis/clients/jedis/ShardedJedisPipeline.java +++ b/src/main/java/redis/clients/jedis/ShardedJedisPipeline.java @@ -1,11 +1,11 @@ package redis.clients.jedis; +import redis.clients.jedis.BinaryClient.LIST_POSITION; + import java.util.ArrayList; import java.util.List; import java.util.Map; -import redis.clients.jedis.BinaryClient.LIST_POSITION; - public abstract class ShardedJedisPipeline { private BinaryShardedJedis jedis; private List results = new ArrayList(); @@ -425,6 +425,30 @@ public abstract class ShardedJedisPipeline { results.add(new FutureResult(c)); } + protected void getbit(String key, long offset) { + Client c = jedis.getShard(key).getClient(); + c.getbit(key, offset); + results.add(new FutureResult(c)); + } + + public void setbit(String key, long offset, boolean value) { + Client c = jedis.getShard(key).getClient(); + c.setbit(key, offset, value); + results.add(new FutureResult(c)); + } + + public void setrange(String key, long offset, String value) { + Client c = jedis.getShard(key).getClient(); + c.setrange(key, offset, value); + results.add(new FutureResult(c)); + } + + public void getrange(String key, long startOffset, long endOffset) { + Client c = jedis.getShard(key).getClient(); + c.getrange(key, startOffset, endOffset); + results.add(new FutureResult(c)); + } + public List getResults() { List r = new ArrayList(); for (FutureResult fr : results) { diff --git a/src/main/java/redis/clients/jedis/ShardedJedisPool.java b/src/main/java/redis/clients/jedis/ShardedJedisPool.java index 4287d9d..ce11884 100644 --- a/src/main/java/redis/clients/jedis/ShardedJedisPool.java +++ b/src/main/java/redis/clients/jedis/ShardedJedisPool.java @@ -12,7 +12,7 @@ import redis.clients.util.Pool; public class ShardedJedisPool extends Pool { public ShardedJedisPool(final GenericObjectPool.Config poolConfig, List shards) { - this(poolConfig, shards, Hashing.MD5); + this(poolConfig, shards, Hashing.MURMUR_HASH); } public ShardedJedisPool(final GenericObjectPool.Config poolConfig, @@ -22,7 +22,7 @@ public class ShardedJedisPool extends Pool { public ShardedJedisPool(final GenericObjectPool.Config poolConfig, List shards, Pattern keyTagPattern) { - this(poolConfig, shards, Hashing.MD5, keyTagPattern); + this(poolConfig, shards, Hashing.MURMUR_HASH, keyTagPattern); } public ShardedJedisPool(final GenericObjectPool.Config poolConfig, @@ -47,40 +47,32 @@ public class ShardedJedisPool extends Pool { public Object makeObject() throws Exception { ShardedJedis jedis = new ShardedJedis(shards, algo, keyTagPattern); - boolean done = false; - while (!done) { - try { - for (Jedis shard : jedis.getAllShards()) { - if (!shard.isConnected()) { - shard.connect(); - } - } - done = true; - } catch (Exception e) { - try { - Thread.sleep(100); - } catch (InterruptedException e1) { - } - } - } return jedis; } public void destroyObject(final Object obj) throws Exception { - if (obj != null) { - try { - ((ShardedJedis) obj).disconnect(); - } catch (Exception e) { + if ((obj != null) && (obj instanceof ShardedJedis)) { + ShardedJedis shardedJedis = (ShardedJedis) obj; + for (Jedis jedis : shardedJedis.getAllShards()) { + try { + try { + jedis.quit(); + } catch (Exception e) { + } + jedis.disconnect(); + } catch (Exception e) { + + } } } } public boolean validateObject(final Object obj) { - try { + try { ShardedJedis jedis = (ShardedJedis) obj; for (Jedis shard : jedis.getAllShards()) { - if (!shard.isConnected() || !shard.ping().equals("PONG")) { + if (!shard.ping().equals("PONG")) { return false; } } diff --git a/src/main/java/redis/clients/jedis/Transaction.java b/src/main/java/redis/clients/jedis/Transaction.java index 32529f8..b1dd14a 100644 --- a/src/main/java/redis/clients/jedis/Transaction.java +++ b/src/main/java/redis/clients/jedis/Transaction.java @@ -1,6 +1,8 @@ package redis.clients.jedis; +import java.util.List; import java.util.Map; +import java.util.Set; import redis.clients.jedis.BinaryClient.LIST_POSITION; @@ -12,339 +14,567 @@ public class Transaction extends BinaryTransaction { super(client); } - public void set(final String key, final String value) { - client.set(key, value); - } - - public void get(final String key) { - client.get(key); - } - - public void exists(final String key) { - client.exists(key); - } - - public void del(final String... keys) { - client.del(keys); - } - - public void type(final String key) { - client.type(key); - } - - public void keys(final String pattern) { - client.keys(pattern); - } - - public void randomKey() { - client.randomKey(); - } - - public void rename(final String oldkey, final String newkey) { - client.rename(oldkey, newkey); - } - - public void renamenx(final String oldkey, final String newkey) { - client.renamenx(oldkey, newkey); - } - - public void expire(final String key, final int seconds) { - client.expire(key, seconds); - } - - public void expireAt(final String key, final long unixTime) { - client.expireAt(key, unixTime); - } - - public void ttl(final String key) { - client.ttl(key); - } - - public void move(final String key, final int dbIndex) { - client.move(key, dbIndex); - } - - public void getSet(final String key, final String value) { - client.getSet(key, value); - } - - public void mget(final String... keys) { - client.mget(keys); - } - - public void setnx(final String key, final String value) { - client.setnx(key, value); - } - - public void setex(final String key, final int seconds, final String value) { - client.setex(key, seconds, value); - } - - public void mset(final String... keysvalues) { - client.mset(keysvalues); - } - - public void msetnx(final String... keysvalues) { - client.msetnx(keysvalues); - } - - public void decrBy(final String key, final int integer) { - client.decrBy(key, integer); - } - - public void decr(final String key) { - client.decr(key); - } - - public void incrBy(final String key, final int integer) { - client.incrBy(key, integer); - } - - public void incr(final String key) { - client.incr(key); - } - - public void append(final String key, final String value) { + public Response append(String key, String value) { client.append(key, value); + return getResponse(BuilderFactory.LONG); } - public void substr(final String key, final int start, final int end) { - client.substr(key, start, end); + public Response> blpop(String... args) { + client.blpop(args); + return getResponse(BuilderFactory.STRING_LIST); } - public void hset(final String key, final String field, final String value) { - client.hset(key, field, value); + public Response> brpop(String... args) { + client.brpop(args); + return getResponse(BuilderFactory.STRING_LIST); } - public void hget(final String key, final String field) { - client.hget(key, field); + public Response decr(String key) { + client.decr(key); + return getResponse(BuilderFactory.LONG); } - public void hsetnx(final String key, final String field, final String value) { - client.hsetnx(key, field, value); + public Response decrBy(String key, long integer) { + client.decrBy(key, integer); + return getResponse(BuilderFactory.LONG); } - public void hmset(final String key, final Map hash) { - client.hmset(key, hash); + public Response del(String... keys) { + client.del(keys); + return getResponse(BuilderFactory.LONG); } - public void hmget(final String key, final String... fields) { - client.hmget(key, fields); + public Response echo(String string) { + client.echo(string); + return getResponse(BuilderFactory.STRING); } - public void hincrBy(final String key, final String field, final int value) { - client.hincrBy(key, field, value); + public Response exists(String key) { + client.exists(key); + return getResponse(BuilderFactory.BOOLEAN); } - public void hexists(final String key, final String field) { - client.hexists(key, field); + public Response expire(String key, int seconds) { + client.expire(key, seconds); + return getResponse(BuilderFactory.LONG); } - public void hdel(final String key, final String field) { - client.hdel(key, field); + public Response expireAt(String key, long unixTime) { + client.expireAt(key, unixTime); + return getResponse(BuilderFactory.LONG); } - public void hlen(final String key) { - client.hlen(key); + public Response get(String key) { + client.get(key); + return getResponse(BuilderFactory.STRING); } - public void hkeys(final String key) { - client.hkeys(key); - } - - public void hvals(final String key) { - client.hvals(key); - } - - public void hgetAll(final String key) { - client.hgetAll(key); - } - - public void rpush(final String key, final String string) { - client.rpush(key, string); - } - - public void lpush(final String key, final String string) { - client.lpush(key, string); - } - - public void llen(final String key) { - client.llen(key); - } - - public void lrange(final String key, final int start, final int end) { - client.lrange(key, start, end); - } - - public void ltrim(String key, final int start, final int end) { - client.ltrim(key, start, end); - } - - public void lindex(final String key, final int index) { - client.lindex(key, index); - } - - public void lset(final String key, final int index, final String value) { - client.lset(key, index, value); - } - - public void lrem(final String key, final int count, final String value) { - client.lrem(key, count, value); - } - - public void lpop(final String key) { - client.lpop(key); - } - - public void rpop(final String key) { - client.rpop(key); - } - - public void rpoplpush(final String srckey, final String dstkey) { - client.rpoplpush(srckey, dstkey); - } - - public void sadd(final String key, final String member) { - client.sadd(key, member); - } - - public void smembers(final String key) { - client.smembers(key); - } - - public void srem(final String key, final String member) { - client.srem(key, member); - } - - public void spop(final String key) { - client.spop(key); - } - - public void smove(final String srckey, final String dstkey, - final String member) { - client.smove(srckey, dstkey, member); - } - - public void scard(final String key) { - client.scard(key); - } - - public void sismember(final String key, final String member) { - client.sismember(key, member); - } - - public void sinter(final String... keys) { - client.sinter(keys); - } - - public void sinterstore(final String dstkey, final String... keys) { - client.sinterstore(dstkey, keys); - } - - public void sunion(final String... keys) { - client.sunion(keys); - } - - public void sunionstore(final String dstkey, final String... keys) { - client.sunionstore(dstkey, keys); - } - - public void sdiff(final String... keys) { - client.sdiff(keys); - } - - public void sdiffstore(final String dstkey, final String... keys) { - client.sdiffstore(dstkey, keys); - } - - public void srandmember(final String key) { - client.srandmember(key); - } - - public void zadd(final String key, final double score, final String member) { - client.zadd(key, score, member); - } - - public void zrange(final String key, final int start, final int end) { - client.zrange(key, start, end); - } - - public void zrem(final String key, final String member) { - client.zrem(key, member); - } - - public void zincrby(final String key, final double score, - final String member) { - client.zincrby(key, score, member); - } - - public void zrank(final String key, final String member) { - client.zrank(key, member); - } - - public void zrevrank(final String key, final String member) { - client.zrevrank(key, member); - } - - public void zrevrange(final String key, final int start, final int end) { - client.zrevrange(key, start, end); - } - - public void zrangeWithScores(final String key, final int start, - final int end) { - client.zrangeWithScores(key, start, end); - } - - public void zrevrangeWithScores(final String key, final int start, - final int end) { - client.zrevrangeWithScores(key, start, end); - } - - public void zcard(final String key) { - client.zcard(key); - } - - public void zscore(final String key, final String member) { - client.zscore(key, member); - } - - public void sort(final String key) { - client.sort(key); - } - - public void sort(final String key, final SortingParams sortingParameters) { - client.sort(key, sortingParameters); - } - - public void sort(final String key, final String dstkey) { - client.sort(key, dstkey); - } - - public void sort(final String key, final SortingParams sortingParameters, - final String dstkey) { - client.sort(key, sortingParameters, dstkey); - } - - public void setbit(String key, long offset, boolean value) { - client.setbit(key, offset, value); - } - - public void getbit(String key, long offset) { + public Response getbit(String key, long offset) { client.getbit(key, offset); + return getResponse(BuilderFactory.BOOLEAN); } - public long setrange(String key, long offset, String value) { - client.setrange(key, offset, value); - return client.getIntegerReply(); - } - - public String getrange(String key, long startOffset, long endOffset) { + public Response getrange(String key, long startOffset, + long endOffset) { client.getrange(key, startOffset, endOffset); - return client.getBulkReply(); + return getResponse(BuilderFactory.STRING); } - public void linsert(final String key, final LIST_POSITION where, - final String pivot, final String value) { - client.linsert(key, where, pivot, value); + public Response getSet(String key, String value) { + client.getSet(key, value); + return getResponse(BuilderFactory.STRING); } + + public Response hdel(String key, String field) { + client.hdel(key, field); + return getResponse(BuilderFactory.LONG); + } + + public Response hexists(String key, String field) { + client.hexists(key, field); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response hget(String key, String field) { + client.hget(key, field); + return getResponse(BuilderFactory.STRING); + } + + public Response> hgetAll(String key) { + client.hgetAll(key); + return getResponse(BuilderFactory.STRING_MAP); + } + + public Response hincrBy(String key, String field, long value) { + client.hincrBy(key, field, value); + return getResponse(BuilderFactory.LONG); + } + + public Response> hkeys(String key) { + client.hkeys(key); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response hlen(String key) { + client.hlen(key); + return getResponse(BuilderFactory.LONG); + } + + public Response> hmget(String key, String... fields) { + client.hmget(key, fields); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response hmset(String key, Map hash) { + client.hmset(key, hash); + return getResponse(BuilderFactory.STRING); + } + + public Response hset(String key, String field, String value) { + client.hset(key, field, value); + return getResponse(BuilderFactory.LONG); + } + + public Response hsetnx(String key, String field, String value) { + client.hsetnx(key, field, value); + return getResponse(BuilderFactory.LONG); + } + + public Response> hvals(String key) { + client.hvals(key); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response incr(String key) { + client.incr(key); + return getResponse(BuilderFactory.LONG); + } + + public Response incrBy(String key, long integer) { + client.incrBy(key, integer); + return getResponse(BuilderFactory.LONG); + } + + public Response> keys(String pattern) { + client.keys(pattern); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response lindex(String key, int index) { + client.lindex(key, index); + return getResponse(BuilderFactory.STRING); + } + + public Response linsert(String key, LIST_POSITION where, + String pivot, String value) { + client.linsert(key, where, pivot, value); + return getResponse(BuilderFactory.LONG); + } + + public Response llen(String key) { + client.llen(key); + return getResponse(BuilderFactory.LONG); + } + + public Response lpop(String key) { + client.lpop(key); + return getResponse(BuilderFactory.STRING); + } + + public Response lpush(String key, String string) { + client.lpush(key, string); + return getResponse(BuilderFactory.LONG); + } + + public Response lpushx(String key, String string) { + client.lpushx(key, string); + return getResponse(BuilderFactory.LONG); + } + + public Response> lrange(String key, long start, long end) { + client.lrange(key, start, end); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response lrem(String key, long count, String value) { + client.lrem(key, count, value); + return getResponse(BuilderFactory.LONG); + } + + public Response lset(String key, long index, String value) { + client.lset(key, index, value); + return getResponse(BuilderFactory.STRING); + } + + public Response ltrim(String key, long start, long end) { + client.ltrim(key, start, end); + return getResponse(BuilderFactory.STRING); + } + + public Response> mget(String... keys) { + client.mget(keys); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response move(String key, int dbIndex) { + client.move(key, dbIndex); + return getResponse(BuilderFactory.LONG); + } + + public Response mset(String... keysvalues) { + client.mset(keysvalues); + return getResponse(BuilderFactory.STRING); + } + + public Response msetnx(String... keysvalues) { + client.msetnx(keysvalues); + return getResponse(BuilderFactory.LONG); + } + + public Response persist(String key) { + client.persist(key); + return getResponse(BuilderFactory.LONG); + } + + public Response rename(String oldkey, String newkey) { + client.rename(oldkey, newkey); + return getResponse(BuilderFactory.STRING); + } + + public Response renamenx(String oldkey, String newkey) { + client.renamenx(oldkey, newkey); + return getResponse(BuilderFactory.LONG); + } + + public Response rpop(String key) { + client.rpop(key); + return getResponse(BuilderFactory.STRING); + } + + public Response rpoplpush(String srckey, String dstkey) { + client.rpoplpush(srckey, dstkey); + return getResponse(BuilderFactory.STRING); + } + + public Response rpush(String key, String string) { + client.rpush(key, string); + return getResponse(BuilderFactory.LONG); + } + + public Response rpushx(String key, String string) { + client.rpushx(key, string); + return getResponse(BuilderFactory.LONG); + } + + public Response sadd(String key, String member) { + client.sadd(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response scard(String key) { + client.scard(key); + return getResponse(BuilderFactory.LONG); + } + + public Response> sdiff(String... keys) { + client.sdiff(keys); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response sdiffstore(String dstkey, String... keys) { + client.sdiffstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); + } + + public Response set(String key, String value) { + client.set(key, value); + return getResponse(BuilderFactory.STRING); + } + + public Response setbit(String key, long offset, boolean value) { + client.setbit(key, offset, value); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response setex(String key, int seconds, String value) { + client.setex(key, seconds, value); + return getResponse(BuilderFactory.STRING); + } + + public Response setnx(String key, String value) { + client.setnx(key, value); + return getResponse(BuilderFactory.LONG); + } + + public Response setrange(String key, long offset, String value) { + client.setrange(key, offset, value); + return getResponse(BuilderFactory.LONG); + } + + public Response> sinter(String... keys) { + client.sinter(keys); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response sinterstore(String dstkey, String... keys) { + client.sinterstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); + } + + public Response sismember(String key, String member) { + client.sismember(key, member); + return getResponse(BuilderFactory.BOOLEAN); + } + + public Response> smembers(String key) { + client.smembers(key); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response smove(String srckey, String dstkey, String member) { + client.smove(srckey, dstkey, member); + return getResponse(BuilderFactory.LONG); + } + + public Response> sort(String key) { + client.sort(key); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> sort(String key, + SortingParams sortingParameters) { + client.sort(key, sortingParameters); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> sort(String key, + SortingParams sortingParameters, String dstkey) { + client.sort(key, sortingParameters, dstkey); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response> sort(String key, String dstkey) { + client.sort(key, dstkey); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response spop(String key) { + client.spop(key); + return getResponse(BuilderFactory.STRING); + } + + public Response srandmember(String key) { + client.srandmember(key); + return getResponse(BuilderFactory.STRING); + } + + public Response srem(String key, String member) { + client.srem(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response strlen(String key) { + client.strlen(key); + return getResponse(BuilderFactory.LONG); + } + + public Response substr(String key, int start, int end) { + client.substr(key, start, end); + return getResponse(BuilderFactory.STRING); + } + + public Response> sunion(String... keys) { + client.sunion(keys); + return getResponse(BuilderFactory.STRING_SET); + } + + public Response sunionstore(String dstkey, String... keys) { + client.sunionstore(dstkey, keys); + return getResponse(BuilderFactory.LONG); + } + + public Response ttl(String key) { + client.ttl(key); + return getResponse(BuilderFactory.LONG); + } + + public Response type(String key) { + client.type(key); + return getResponse(BuilderFactory.STRING); + } + + public Response zadd(String key, double score, String member) { + client.zadd(key, score, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zcard(String key) { + client.zcard(key); + return getResponse(BuilderFactory.LONG); + } + + public Response zcount(String key, double min, double max) { + client.zcount(key, min, max); + return getResponse(BuilderFactory.LONG); + } + + public Response zincrby(String key, double score, String member) { + client.zincrby(key, score, member); + return getResponse(BuilderFactory.DOUBLE); + } + + public Response zinterstore(String dstkey, String... sets) { + client.zinterstore(dstkey, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response zinterstore(String dstkey, ZParams params, + String... sets) { + client.zinterstore(dstkey, params, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response> zrange(String key, int start, int end) { + client.zrange(key, start, end); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrangeByScore(String key, double min, + double max) { + client.zrangeByScore(key, min, max); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrangeByScore(String key, String min, + String max) { + client.zrangeByScore(key, min, max); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrangeByScore(String key, double min, + double max, int offset, int count) { + client.zrangeByScore(key, min, max, offset, count); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrangeByScoreWithScores(String key, double min, + double max) { + client.zrangeByScoreWithScores(key, min, max); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrangeByScoreWithScores(String key, double min, + double max, int offset, int count) { + client.zrangeByScoreWithScores(key, min, max, offset, count); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response> zrangeWithScores(String key, int start, int end) { + client.zrangeWithScores(key, start, end); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response zrank(String key, String member) { + client.zrank(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zrem(String key, String member) { + client.zrem(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zremrangeByRank(String key, int start, int end) { + client.zremrangeByRank(key, start, end); + return getResponse(BuilderFactory.LONG); + } + + public Response zremrangeByScore(String key, double start, double end) { + client.zremrangeByScore(key, start, end); + return getResponse(BuilderFactory.LONG); + } + + public Response> zrevrange(String key, int start, int end) { + client.zrevrange(key, start, end); + return getResponse(BuilderFactory.STRING_ZSET); + } + + public Response> zrevrangeWithScores(String key, int start, + int end) { + client.zrevrangeWithScores(key, start, end); + return getResponse(BuilderFactory.TUPLE_ZSET); + } + + public Response zrevrank(String key, String member) { + client.zrevrank(key, member); + return getResponse(BuilderFactory.LONG); + } + + public Response zscore(String key, String member) { + client.zscore(key, member); + return getResponse(BuilderFactory.DOUBLE); + } + + public Response zunionstore(String dstkey, String... sets) { + client.zunionstore(dstkey, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response zunionstore(String dstkey, ZParams params, + String... sets) { + client.zunionstore(dstkey, params, sets); + return getResponse(BuilderFactory.LONG); + } + + public Response bgrewriteaof() { + client.bgrewriteaof(); + return getResponse(BuilderFactory.STRING); + } + + public Response bgsave() { + client.bgsave(); + return getResponse(BuilderFactory.STRING); + } + + public Response configGet(String pattern) { + client.configGet(pattern); + return getResponse(BuilderFactory.STRING); + } + + public Response configSet(String parameter, String value) { + client.configSet(parameter, value); + return getResponse(BuilderFactory.STRING); + } + + public Response brpoplpush(String source, String destination, + int timeout) { + client.brpoplpush(source, destination, timeout); + return getResponse(BuilderFactory.STRING); + } + + public Response configResetStat() { + client.configResetStat(); + return getResponse(BuilderFactory.STRING); + } + + public Response save() { + client.save(); + return getResponse(BuilderFactory.STRING); + } + + public Response lastsave() { + client.lastsave(); + return getResponse(BuilderFactory.LONG); + } + + public Response publish(String channel, String message) { + client.publish(channel, message); + return getResponse(BuilderFactory.LONG); + } + + public Response publish(byte[] channel, byte[] message) { + client.publish(channel, message); + return getResponse(BuilderFactory.LONG); + } + } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Tuple.java b/src/main/java/redis/clients/jedis/Tuple.java index 6df12d2..211a9e9 100644 --- a/src/main/java/redis/clients/jedis/Tuple.java +++ b/src/main/java/redis/clients/jedis/Tuple.java @@ -4,11 +4,10 @@ import java.util.Arrays; import redis.clients.util.SafeEncoder; -public class Tuple { +public class Tuple implements Comparable { private byte[] element; private Double score; - @Override public int hashCode() { final int prime = 31; int result = 1; @@ -24,7 +23,6 @@ public class Tuple { return result; } - @Override public boolean equals(Object obj) { if (this == obj) return true; @@ -38,12 +36,16 @@ public class Tuple { return false; } else if (!Arrays.equals(element, other.element)) return false; - if (Double.doubleToLongBits(score) != Double - .doubleToLongBits(other.score)) - return false; return true; } + public int compareTo(Tuple other) { + if (Arrays.equals(this.element, other.element)) + return 0; + else + return this.score < other.getScore() ? -1 : 1; + } + public Tuple(String element, Double score) { super(); this.element = SafeEncoder.encode(element); @@ -75,4 +77,4 @@ public class Tuple { public String toString() { return '[' + Arrays.toString(element) + ',' + score + ']'; } -} +} \ No newline at end of file diff --git a/src/main/java/redis/clients/util/Hashing.java b/src/main/java/redis/clients/util/Hashing.java index 3323347..1460f03 100644 --- a/src/main/java/redis/clients/util/Hashing.java +++ b/src/main/java/redis/clients/util/Hashing.java @@ -5,23 +5,22 @@ import java.security.NoSuchAlgorithmException; public interface Hashing { public static final Hashing MURMUR_HASH = new MurmurHash(); + public ThreadLocal md5Holder = new ThreadLocal(); public static final Hashing MD5 = new Hashing() { - private MessageDigest md5 = null; // avoid recurring construction - public long hash(String key) { return hash(SafeEncoder.encode(key)); } public long hash(byte[] key) { - if (md5 == null) { - try { - md5 = MessageDigest.getInstance("MD5"); - } catch (NoSuchAlgorithmException e) { - throw new IllegalStateException( - "++++ no md5 algorythm found"); + try { + if (md5Holder.get() == null) { + md5Holder.set(MessageDigest.getInstance("MD5")); } + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("++++ no md5 algorythm found"); } + MessageDigest md5 = md5Holder.get(); md5.reset(); md5.update(key); diff --git a/src/main/java/redis/clients/util/SafeEncoder.java b/src/main/java/redis/clients/util/SafeEncoder.java index c657756..8e88c40 100644 --- a/src/main/java/redis/clients/util/SafeEncoder.java +++ b/src/main/java/redis/clients/util/SafeEncoder.java @@ -3,6 +3,7 @@ package redis.clients.util; import java.io.UnsupportedEncodingException; import redis.clients.jedis.Protocol; +import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.exceptions.JedisException; /** @@ -12,6 +13,10 @@ import redis.clients.jedis.exceptions.JedisException; public class SafeEncoder { public static byte[] encode(final String str) { try { + if (str == null) { + throw new JedisDataException( + "value sent to redis cannot be null"); + } return str.getBytes(Protocol.CHARSET); } catch (UnsupportedEncodingException e) { throw new JedisException(e); diff --git a/src/main/java/redis/clients/util/Sharded.java b/src/main/java/redis/clients/util/Sharded.java index 0f7849e..2e5502d 100644 --- a/src/main/java/redis/clients/util/Sharded.java +++ b/src/main/java/redis/clients/util/Sharded.java @@ -2,7 +2,7 @@ package redis.clients.util; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.SortedMap; @@ -15,7 +15,7 @@ public class Sharded> { public static final int DEFAULT_WEIGHT = 1; private TreeMap nodes; private final Hashing algo; - private final Map, R> resources = new HashMap, R>(); + private final Map, R> resources = new LinkedHashMap, R>(); /** * The default pattern used for extracting a key tag. The pattern must have @@ -56,7 +56,7 @@ public class Sharded> { for (int i = 0; i != shards.size(); ++i) { final S shardInfo = shards.get(i); for (int n = 0; n < 160 * shardInfo.getWeight(); n++) { - nodes.put(this.algo.hash(shardInfo.toString() + n), shardInfo); + nodes.put(this.algo.hash("SHARD-" + i + "-NODE-" + n), shardInfo); } resources.put(shardInfo, shardInfo.createResource()); } @@ -70,7 +70,7 @@ public class Sharded> { return resources.get(getShardInfo(key)); } - private S getShardInfo(byte[] key) { + public S getShardInfo(byte[] key) { SortedMap tail = nodes.tailMap(algo.hash(key)); if (tail.size() == 0) { return nodes.get(nodes.firstKey()); @@ -85,7 +85,7 @@ public class Sharded> { /** * A key tag is a special pattern inside a key that, if preset, is the only * part of the key hashed in order to select the server for this key. - * + * * @see http://code.google.com/p/redis/wiki/FAQ#I * 'm_using_some_form_of_key_hashing_for_partitioning,_but_wh * @param key @@ -107,4 +107,5 @@ public class Sharded> { public Collection getAllShards() { return Collections.unmodifiableCollection(resources.values()); } -} \ No newline at end of file +} + diff --git a/src/test/java/redis/clients/jedis/tests/BuilderFactoryTest.java b/src/test/java/redis/clients/jedis/tests/BuilderFactoryTest.java new file mode 100644 index 0000000..b759134 --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/BuilderFactoryTest.java @@ -0,0 +1,14 @@ +package redis.clients.jedis.tests; + +import org.junit.Assert; +import org.junit.Test; + +import redis.clients.jedis.BuilderFactory; + +public class BuilderFactoryTest extends Assert { + @Test + public void buildDouble() { + Double build = BuilderFactory.DOUBLE.build("1.0".getBytes()); + assertEquals(new Double(1.0), build); + } +} \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java b/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java index a303cd2..158adbf 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java @@ -84,4 +84,15 @@ public class JedisPoolTest extends Assert { newJedis.auth("foobared"); newJedis.incr("foo"); } + + @Test + public void securePool() { + JedisPoolConfig config = new JedisPoolConfig(); + config.setTestOnBorrow(true); + JedisPool pool = new JedisPool(config, hnp.host, hnp.port, 2000, "foobared"); + Jedis jedis = pool.getResource(); + jedis.set("foo", "bar"); + pool.returnResource(jedis); + pool.destroy(); + } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index d1272d2..a34ef3d 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -9,6 +9,7 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.Protocol; import redis.clients.jedis.exceptions.JedisConnectionException; +import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.tests.commands.JedisCommandTestBase; import redis.clients.util.SafeEncoder; @@ -53,4 +54,9 @@ public class JedisTest extends JedisCommandTestBase { Thread.sleep(20000); jedis.hmget("foobar", "foo"); } + + @Test(expected = JedisDataException.class) + public void failWhenSendingNullValues() { + jedis.set("foo", null); + } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/PipeliningTest.java b/src/test/java/redis/clients/jedis/tests/PipeliningTest.java index 0928553..cb82fd1 100644 --- a/src/test/java/redis/clients/jedis/tests/PipeliningTest.java +++ b/src/test/java/redis/clients/jedis/tests/PipeliningTest.java @@ -2,6 +2,8 @@ package redis.clients.jedis.tests; import java.io.UnsupportedEncodingException; import java.util.List; +import java.util.Map; +import java.util.Set; import org.junit.Assert; import org.junit.Before; @@ -10,7 +12,9 @@ import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.Pipeline; import redis.clients.jedis.PipelineBlock; -import redis.clients.jedis.Protocol; +import redis.clients.jedis.Response; +import redis.clients.jedis.Tuple; +import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort; public class PipeliningTest extends Assert { @@ -36,21 +40,78 @@ public class PipeliningTest extends Assert { }); assertEquals(2, results.size()); - assertArrayEquals("OK".getBytes(Protocol.CHARSET), (byte[]) results - .get(0)); - assertArrayEquals("bar".getBytes(Protocol.CHARSET), (byte[]) results - .get(1)); + assertEquals("OK", results.get(0)); + assertEquals("bar", results.get(1)); Pipeline p = jedis.pipelined(); p.set("foo", "bar"); p.get("foo"); - results = p.execute(); + results = p.sync(); assertEquals(2, results.size()); - assertArrayEquals("OK".getBytes(Protocol.CHARSET), (byte[]) results - .get(0)); - assertArrayEquals("bar".getBytes(Protocol.CHARSET), (byte[]) results - .get(1)); + assertEquals("OK", results.get(0)); + assertEquals("bar", results.get(1)); } -} + + @Test + public void pipelineResponse() { + jedis.set("string", "foo"); + jedis.lpush("list", "foo"); + jedis.hset("hash", "foo", "bar"); + jedis.zadd("zset", 1, "foo"); + jedis.sadd("set", "foo"); + + Pipeline p = jedis.pipelined(); + Response string = p.get("string"); + Response list = p.lpop("list"); + Response hash = p.hget("hash", "foo"); + Response> zset = p.zrange("zset", 0, -1); + Response set = p.spop("set"); + Response blist = p.exists("list"); + Response zincrby = p.zincrby("zset", 1, "foo"); + Response zcard = p.zcard("zset"); + p.lpush("list", "bar"); + Response> lrange = p.lrange("list", 0, -1); + Response> hgetAll = p.hgetAll("hash"); + p.sadd("set", "foo"); + Response> smembers = p.smembers("set"); + Response> zrangeWithScores = p.zrangeWithScores("zset", 0, + -1); + p.sync(); + + assertEquals("foo", string.get()); + assertEquals("foo", list.get()); + assertEquals("bar", hash.get()); + assertEquals("foo", zset.get().iterator().next()); + assertEquals("foo", set.get()); + assertEquals(false, blist.get()); + assertEquals(new Double(2), zincrby.get()); + assertEquals(new Long(1), zcard.get()); + assertEquals(1, lrange.get().size()); + assertNotNull(hgetAll.get().get("foo")); + assertEquals(1, smembers.get().size()); + assertEquals(1, zrangeWithScores.get().size()); + } + + @Test(expected = JedisDataException.class) + public void pipelineResponseWithinPipeline() { + jedis.set("string", "foo"); + + Pipeline p = jedis.pipelined(); + Response string = p.get("string"); + string.get(); + p.sync(); + } + + @Test + public void pipelineWithPubSub() { + Pipeline pipelined = jedis.pipelined(); + Response p1 = pipelined.publish("foo", "bar"); + Response p2 = pipelined.publish("foo".getBytes(), "bar" + .getBytes()); + pipelined.sync(); + assertEquals(0, p1.get().longValue()); + assertEquals(0, p2.get().longValue()); + } +} \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java b/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java index 4972042..3f41c00 100644 --- a/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java @@ -22,14 +22,31 @@ public class ShardedJedisTest extends Assert { private static HostAndPort redis2 = HostAndPortUtil.getRedisServers() .get(1); + private List getKeysDifferentShard(ShardedJedis jedis) { + List ret = new ArrayList (); + JedisShardInfo first = jedis.getShardInfo("a0"); + ret.add("a0"); + for (int i =1; i < 100; ++i) { + JedisShardInfo actual = jedis.getShardInfo("a" + i); + if (actual != first) { + ret.add("a" + i); + break; + + } + + } + return ret; + } + @Test public void checkSharding() { List shards = new ArrayList(); shards.add(new JedisShardInfo(redis1.host, redis1.port)); shards.add(new JedisShardInfo(redis2.host, redis2.port)); ShardedJedis jedis = new ShardedJedis(shards); - JedisShardInfo s1 = jedis.getShardInfo("a1"); - JedisShardInfo s2 = jedis.getShardInfo("b2"); + List keys = getKeysDifferentShard(jedis); + JedisShardInfo s1 = jedis.getShardInfo(keys.get(0)); + JedisShardInfo s2 = jedis.getShardInfo(keys.get(1)); assertNotSame(s1, s2); } @@ -108,8 +125,9 @@ public class ShardedJedisTest extends Assert { JedisShardInfo s2 = jedis.getShardInfo("foo{bar}"); assertSame(s1, s2); - JedisShardInfo s3 = jedis.getShardInfo("a112"); - JedisShardInfo s4 = jedis.getShardInfo("b112"); + List keys = getKeysDifferentShard(jedis); + JedisShardInfo s3 = jedis.getShardInfo(keys.get(0)); + JedisShardInfo s4 = jedis.getShardInfo(keys.get(1)); assertNotSame(s3, s4); ShardedJedis jedis2 = new ShardedJedis(shards); @@ -117,8 +135,8 @@ public class ShardedJedisTest extends Assert { assertEquals(jedis2.getKeyTag("foo"), "foo"); assertNotSame(jedis2.getKeyTag("foo{bar}"), "bar"); - JedisShardInfo s5 = jedis2.getShardInfo("foo{bar}"); - JedisShardInfo s6 = jedis2.getShardInfo("abc{bar}"); + JedisShardInfo s5 = jedis2.getShardInfo(keys.get(0)+"{bar}"); + JedisShardInfo s6 = jedis2.getShardInfo(keys.get(1)+"{bar}"); assertNotSame(s5, s6); } @@ -131,15 +149,16 @@ public class ShardedJedisTest extends Assert { shards.get(1).setPassword("foobared"); ShardedJedis jedis = new ShardedJedis(shards); - jedis.set("a112", "a"); - jedis.set("b112", "b"); + final List keys = getKeysDifferentShard(jedis); + jedis.set(keys.get(0), "a"); + jedis.set(keys.get(1), "b"); - assertNotSame(jedis.getShard("a112"), jedis.getShard("b112")); + assertNotSame(jedis.getShard(keys.get(0)), jedis.getShard(keys.get(1))); List results = jedis.pipelined(new ShardedJedisPipeline() { public void execute() { - get("a112"); - get("b112"); + get(keys.get(0)); + get(keys.get(1)); } }); @@ -221,4 +240,5 @@ public class ShardedJedisTest extends Assert { assertTrue(shard_6380 > 300 && shard_6380 < 400); assertTrue(shard_6381 > 300 && shard_6381 < 400); } -} \ No newline at end of file +} + diff --git a/src/test/java/redis/clients/jedis/tests/benchmark/SafeEncoderBenchmark.java b/src/test/java/redis/clients/jedis/tests/benchmark/SafeEncoderBenchmark.java new file mode 100644 index 0000000..bfe1fe9 --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/benchmark/SafeEncoderBenchmark.java @@ -0,0 +1,38 @@ +package redis.clients.jedis.tests.benchmark; + +import java.io.IOException; +import java.net.UnknownHostException; +import java.util.Calendar; + +import redis.clients.util.SafeEncoder; + +public class SafeEncoderBenchmark { + private static final int TOTAL_OPERATIONS = 10000000; + + public static void main(String[] args) throws UnknownHostException, + IOException { + long begin = Calendar.getInstance().getTimeInMillis(); + + for (int n = 0; n <= TOTAL_OPERATIONS; n++) { + SafeEncoder.encode("foo bar!"); + } + + long elapsed = Calendar.getInstance().getTimeInMillis() - begin; + + System.out.println(((1000 * TOTAL_OPERATIONS) / elapsed) + + " ops to build byte[]"); + + begin = Calendar.getInstance().getTimeInMillis(); + + byte[] bytes = "foo bar!".getBytes(); + for (int n = 0; n <= TOTAL_OPERATIONS; n++) { + SafeEncoder.encode(bytes); + } + + elapsed = Calendar.getInstance().getTimeInMillis() - begin; + + System.out.println(((1000 * TOTAL_OPERATIONS) / elapsed) + + " ops to build Strings"); + + } +} \ No newline at end of file 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 34f47f2..d7df713 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/AllKindOfValuesCommandsTest.java @@ -343,6 +343,13 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase { assertArrayEquals(bbar, jedis.get(bfoo)); } + @Test + public void getDB() { + assertEquals(0, jedis.getDB().longValue()); + jedis.select(1); + assertEquals(1, jedis.getDB().longValue()); + } + @Test public void move() { long status = jedis.move("foo", 1); diff --git a/src/test/java/redis/clients/jedis/tests/commands/BitCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/BitCommandsTest.java index 119fc8b..204caa8 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/BitCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/BitCommandsTest.java @@ -9,7 +9,7 @@ public class BitCommandsTest extends JedisCommandTestBase { assertEquals(false, bit); bit = jedis.getbit("foo", 0); - assertEquals(false, bit); + assertEquals(true, bit); long bbit = jedis.setbit("bfoo".getBytes(), 0, "1".getBytes()); assertEquals(0, bbit); diff --git a/src/test/java/redis/clients/jedis/tests/commands/ConnectionHandlingCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ConnectionHandlingCommandsTest.java index 13ad513..6b523fd 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ConnectionHandlingCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ConnectionHandlingCommandsTest.java @@ -2,9 +2,21 @@ package redis.clients.jedis.tests.commands; import org.junit.Test; +import redis.clients.jedis.BinaryJedis; +import redis.clients.jedis.tests.HostAndPortUtil; +import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort; + public class ConnectionHandlingCommandsTest extends JedisCommandTestBase { + protected static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); + @Test public void quit() { - jedis.quit(); + assertEquals("OK", jedis.quit()); + } + + @Test + public void binary_quit() { + BinaryJedis bj = new BinaryJedis(hnp.host); + assertEquals("OK", bj.quit()); } } \ No newline at end of file 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 423a146..c6b6698 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java @@ -5,6 +5,7 @@ import java.util.List; import org.junit.Test; import redis.clients.jedis.DebugParams; +import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisMonitor; import redis.clients.jedis.exceptions.JedisDataException; @@ -56,11 +57,32 @@ public class ControlCommandsTest extends JedisCommandTestBase { @Test public void monitor() { + new Thread(new Runnable() { + public void run() { + Jedis j = new Jedis("localhost"); + j.auth("foobared"); + for (int i = 0; i < 4; i++) { + j.incr("foobared"); + } + try { + Thread.sleep(2500); + } catch (InterruptedException e) { + } + j.incr("foobared"); + j.disconnect(); + } + }).start(); + jedis.monitor(new JedisMonitor() { - @Override + private int count = 0; + public void onCommand(String command) { - assertTrue(command.contains("OK")); - client.disconnect(); + if (command.contains("INCR")) { + count++; + } + if (count == 5) { + client.disconnect(); + } } }); } 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 8b082c3..a2e5a4b 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java @@ -585,7 +585,7 @@ public class ListCommandsTest extends JedisCommandTestBase { (new Thread(new Runnable() { public void run() { try { - Thread.sleep(100); + Thread.sleep(2000); Jedis j = createJedis(); j.lpush("foo", "a"); } catch (InterruptedException e) { diff --git a/src/test/java/redis/clients/jedis/tests/commands/PublishSubscribeCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/PublishSubscribeCommandsTest.java index 9ce0147..39f8465 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/PublishSubscribeCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/PublishSubscribeCommandsTest.java @@ -9,6 +9,7 @@ import org.junit.Test; import redis.clients.jedis.BinaryJedisPubSub; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPubSub; +import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.util.SafeEncoder; @@ -501,4 +502,29 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase { // this is OK because we are not sending AUTH command } } + + @Test(expected = JedisConnectionException.class) + public void unsubscribeWhenNotSusbscribed() throws InterruptedException { + JedisPubSub pubsub = new JedisPubSub() { + public void onMessage(String channel, String message) { + } + + public void onPMessage(String pattern, String channel, + String message) { + } + + public void onSubscribe(String channel, int subscribedChannels) { + } + + public void onUnsubscribe(String channel, int subscribedChannels) { + } + + public void onPUnsubscribe(String pattern, int subscribedChannels) { + } + + public void onPSubscribe(String pattern, int subscribedChannels) { + } + }; + pubsub.unsubscribe(); + } } \ No newline at end of file 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 2c07bd9..4135fba 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java @@ -449,6 +449,86 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { } + @Test + public void zrevrangebyscore() { + jedis.zadd("foo", 1.0d, "a"); + jedis.zadd("foo", 2.0d, "b"); + jedis.zadd("foo", 3.0d, "c"); + jedis.zadd("foo", 4.0d, "d"); + jedis.zadd("foo", 5.0d, "e"); + + Set range = jedis.zrevrangeByScore("foo", 3d, + Double.NEGATIVE_INFINITY, 0, 1); + Set expected = new LinkedHashSet(); + expected.add("c"); + + assertEquals(expected, range); + + range = jedis.zrevrangeByScore("foo", 3.5d, Double.NEGATIVE_INFINITY, + 0, 2); + expected = new LinkedHashSet(); + expected.add("c"); + expected.add("b"); + + assertEquals(expected, range); + + range = jedis.zrevrangeByScore("foo", 3.5d, Double.NEGATIVE_INFINITY, + 1, 1); + expected = new LinkedHashSet(); + expected.add("b"); + + assertEquals(expected, range); + + range = jedis.zrevrangeByScore("foo", 4d, 2d); + expected = new LinkedHashSet(); + expected.add("d"); + expected.add("c"); + expected.add("b"); + + assertEquals(expected, range); + + range = jedis.zrevrangeByScore("foo", "+inf", "(4"); + expected = new LinkedHashSet(); + expected.add("e"); + + 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.zrevrangeByScore(bfoo, 2d, 0d); + + Set bexpected = new LinkedHashSet(); + bexpected.add(bc); + bexpected.add(ba); + + assertEquals(bexpected, brange); + + brange = jedis.zrevrangeByScore(bfoo, 2d, 0d, 0, 1); + + bexpected = new LinkedHashSet(); + bexpected.add(ba); + + assertEquals(bexpected, brange); + + Set brange2 = jedis.zrevrangeByScore(bfoo, SafeEncoder + .encode("+inf"), SafeEncoder.encode("(2")); + + bexpected = new LinkedHashSet(); + bexpected.add(bb); + + assertEquals(bexpected, brange2); + + brange = jedis.zrevrangeByScore(bfoo, 2d, 0d, 1, 1); + bexpected = new LinkedHashSet(); + bexpected.add(bc); + + assertEquals(bexpected, brange); + } + @Test public void zrangebyscoreWithScores() { jedis.zadd("foo", 1d, "a"); @@ -509,6 +589,73 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { } + @Test + public void zrevrangebyscoreWithScores() { + jedis.zadd("foo", 1.0d, "a"); + jedis.zadd("foo", 2.0d, "b"); + jedis.zadd("foo", 3.0d, "c"); + jedis.zadd("foo", 4.0d, "d"); + jedis.zadd("foo", 5.0d, "e"); + + Set range = jedis.zrevrangeByScoreWithScores("foo", 3d, + Double.NEGATIVE_INFINITY, 0, 1); + Set expected = new LinkedHashSet(); + expected.add(new Tuple("c", 3.0d)); + + assertEquals(expected, range); + + range = jedis.zrevrangeByScoreWithScores("foo", 3.5d, + Double.NEGATIVE_INFINITY, 0, 2); + expected = new LinkedHashSet(); + expected.add(new Tuple("c", 3.0d)); + expected.add(new Tuple("b", 2.0d)); + + assertEquals(expected, range); + + range = jedis.zrevrangeByScoreWithScores("foo", 3.5d, + Double.NEGATIVE_INFINITY, 1, 1); + expected = new LinkedHashSet(); + expected.add(new Tuple("b", 2.0d)); + + assertEquals(expected, range); + + range = jedis.zrevrangeByScoreWithScores("foo", 4d, 2d); + expected = new LinkedHashSet(); + expected.add(new Tuple("d", 4.0d)); + expected.add(new Tuple("c", 3.0d)); + expected.add(new Tuple("b", 2.0d)); + + 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.zrevrangeByScoreWithScores(bfoo, 2d, 0d); + + Set bexpected = new LinkedHashSet(); + bexpected.add(new Tuple(bc, 0.1d)); + bexpected.add(new Tuple(ba, 2d)); + + assertEquals(bexpected, brange); + + brange = jedis.zrevrangeByScoreWithScores(bfoo, 2d, 0d, 0, 1); + + bexpected = new LinkedHashSet(); + bexpected.add(new Tuple(ba, 2d)); + + assertEquals(bexpected, brange); + + brange = jedis.zrevrangeByScoreWithScores(bfoo, 2d, 0d, 1, 1); + + bexpected = new LinkedHashSet(); + bexpected.add(new Tuple(bc, 0.1d)); + + assertEquals(bexpected, brange); + } + @Test public void zremrangeByRank() { jedis.zadd("foo", 1d, "a"); @@ -721,4 +868,14 @@ public class SortedSetCommandsTest extends JedisCommandTestBase { assertEquals(bexpected, jedis.zrangeWithScores(SafeEncoder .encode("dst"), 0, 100)); } + + @Test + public void tupleCompare() { + Tuple t1 = new Tuple("foo", 1d); + Tuple t2 = new Tuple("bar", 2d); + + assertEquals(-1, t1.compareTo(t2)); + assertEquals(1, t2.compareTo(t1)); + assertEquals(0, t2.compareTo(t2)); + } } \ No newline at end of file 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 2897942..af31b64 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java @@ -5,12 +5,13 @@ import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Set; import org.junit.Before; import org.junit.Test; import redis.clients.jedis.Jedis; -import redis.clients.jedis.Protocol; +import redis.clients.jedis.Response; import redis.clients.jedis.Transaction; import redis.clients.jedis.TransactionBlock; import redis.clients.jedis.Protocol.Keyword; @@ -152,8 +153,7 @@ public class TransactionCommandsTest extends JedisCommandTestBase { t.set("mykey", val); List resp = t.exec(); assertEquals(1, resp.size()); - assertArrayEquals(Keyword.OK.name().getBytes(Protocol.CHARSET), - (byte[]) resp.get(0)); + assertEquals("OK", resp.get(0)); // Binary jedis.watch(bmykey); @@ -171,8 +171,7 @@ public class TransactionCommandsTest extends JedisCommandTestBase { t.set(bmykey, bval); resp = t.exec(); assertEquals(1, resp.size()); - assertArrayEquals(Keyword.OK.name().getBytes(Protocol.CHARSET), - (byte[]) resp.get(0)); + assertEquals("OK", resp.get(0)); } @Test(expected = JedisDataException.class) @@ -187,4 +186,48 @@ public class TransactionCommandsTest extends JedisCommandTestBase { String status = t.discard(); assertEquals("OK", status); } + + @Test + public void transactionResponse() { + jedis.set("string", "foo"); + jedis.lpush("list", "foo"); + jedis.hset("hash", "foo", "bar"); + jedis.zadd("zset", 1, "foo"); + jedis.sadd("set", "foo"); + + Transaction t = jedis.multi(); + Response string = t.get("string"); + Response list = t.lpop("list"); + Response hash = t.hget("hash", "foo"); + Response> zset = t.zrange("zset", 0, -1); + Response set = t.spop("set"); + t.exec(); + + assertEquals("foo", string.get()); + assertEquals("foo", list.get()); + assertEquals("bar", hash.get()); + assertEquals("foo", zset.get().iterator().next()); + assertEquals("foo", set.get()); + } + + @Test(expected = JedisDataException.class) + public void transactionResponseWithinPipeline() { + jedis.set("string", "foo"); + + Transaction t = jedis.multi(); + Response string = t.get("string"); + string.get(); + t.exec(); + } + + @Test + public void lala() { + Transaction multi = jedis.multi(); + Response publish = multi.publish("foo", "bar"); + Response bpublish = multi.publish("foo".getBytes(), "bar" + .getBytes()); + multi.exec(); + assertEquals(0, publish.get().longValue()); + assertEquals(0, bpublish.get().longValue()); + } } \ No newline at end of file