From 7c0505511e30e490c970b03d4dad721fa4a97c36 Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Fri, 27 Aug 2010 17:22:21 -0300 Subject: [PATCH] When in multi show a nice error if not using TransactionJedis --- src/main/java/redis/clients/jedis/Client.java | 10 +- src/main/java/redis/clients/jedis/Jedis.java | 110 +++++++++++++++++- .../java/redis/clients/jedis/Transaction.java | 7 +- .../commands/TransactionCommandsTest.java | 7 ++ 4 files changed, 128 insertions(+), 6 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index 67b6729..333f830 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -5,6 +5,11 @@ import java.util.List; import java.util.Map; public class Client extends Connection { + private boolean isInMulti; + + public boolean isInMulti() { + return isInMulti; + } public Client(String host) { super(host); @@ -354,14 +359,17 @@ public class Client extends Connection { public void multi() { sendCommand("MULTI"); + isInMulti = true; } public void discard() { - sendCommand("MULTI"); + sendCommand("DISCARD"); + isInMulti = false; } public void exec() { sendCommand("EXEC"); + isInMulti = false; } public void watch(String key) { diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index d06b0ed..d21ffaf 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -27,215 +27,258 @@ public class Jedis { } public String ping() { + checkIsInMulti(); client.ping(); return client.getStatusCodeReply(); } public String set(String key, String value) { + checkIsInMulti(); client.set(key, value); return client.getStatusCodeReply(); } public String get(String key) { + checkIsInMulti(); client.sendCommand("GET", key); return client.getBulkReply(); } public void quit() { + checkIsInMulti(); client.quit(); } public int exists(String key) { + checkIsInMulti(); client.exists(key); return client.getIntegerReply(); } public int del(String... keys) { + checkIsInMulti(); client.del(keys); return client.getIntegerReply(); } public String type(String key) { + checkIsInMulti(); client.type(key); return client.getStatusCodeReply(); } public String flushDB() { + checkIsInMulti(); client.flushDB(); return client.getStatusCodeReply(); } public List keys(String pattern) { + checkIsInMulti(); client.keys(pattern); return client.getMultiBulkReply(); } public String randomKey() { + checkIsInMulti(); client.randomKey(); return client.getBulkReply(); } public String rename(String oldkey, String newkey) { + checkIsInMulti(); client.rename(oldkey, newkey); return client.getStatusCodeReply(); } public int renamenx(String oldkey, String newkey) { + checkIsInMulti(); client.renamenx(oldkey, newkey); return client.getIntegerReply(); } public int dbSize() { + checkIsInMulti(); client.dbSize(); return client.getIntegerReply(); } public int expire(String key, int seconds) { + checkIsInMulti(); client.expire(key, seconds); return client.getIntegerReply(); } public int expireAt(String key, long unixTime) { + checkIsInMulti(); client.expireAt(key, unixTime); return client.getIntegerReply(); } public int ttl(String key) { + checkIsInMulti(); client.ttl(key); return client.getIntegerReply(); } public String select(int index) { + checkIsInMulti(); client.select(index); return client.getStatusCodeReply(); } public int move(String key, int dbIndex) { + checkIsInMulti(); client.move(key, dbIndex); return client.getIntegerReply(); } public String flushAll() { + checkIsInMulti(); client.flushAll(); return client.getStatusCodeReply(); } public String getSet(String key, String value) { + checkIsInMulti(); client.getSet(key, value); return client.getBulkReply(); } public List mget(String... keys) { + checkIsInMulti(); client.mget(keys); return client.getMultiBulkReply(); } public int setnx(String key, String value) { + checkIsInMulti(); client.setnx(key, value); return client.getIntegerReply(); } public String setex(String key, int seconds, String value) { + checkIsInMulti(); client.setex(key, seconds, value); return client.getStatusCodeReply(); } public String mset(String... keysvalues) { + checkIsInMulti(); client.mset(keysvalues); return client.getStatusCodeReply(); } public int msetnx(String... keysvalues) { + checkIsInMulti(); client.msetnx(keysvalues); return client.getIntegerReply(); } public int decrBy(String key, int integer) { + checkIsInMulti(); client.decrBy(key, integer); return client.getIntegerReply(); } public int decr(String key) { + checkIsInMulti(); client.decr(key); return client.getIntegerReply(); } public int incrBy(String key, int integer) { + checkIsInMulti(); client.incrBy(key, integer); return client.getIntegerReply(); } public int incr(String key) { + checkIsInMulti(); client.incr(key); return client.getIntegerReply(); } public int append(String key, String value) { + checkIsInMulti(); client.append(key, value); return client.getIntegerReply(); } public String substr(String key, int start, int end) { + checkIsInMulti(); client.substr(key, start, end); return client.getBulkReply(); } public int hset(String key, String field, String value) { + checkIsInMulti(); client.hset(key, field, value); return client.getIntegerReply(); } public String hget(String key, String field) { + checkIsInMulti(); client.hget(key, field); return client.getBulkReply(); } public int hsetnx(String key, String field, String value) { + checkIsInMulti(); client.hsetnx(key, field, value); return client.getIntegerReply(); } public String hmset(String key, Map hash) { + checkIsInMulti(); client.hmset(key, hash); return client.getStatusCodeReply(); } public List hmget(String key, String... fields) { + checkIsInMulti(); client.hmget(key, fields); return client.getMultiBulkReply(); } public int hincrBy(String key, String field, int value) { + checkIsInMulti(); client.hincrBy(key, field, value); return client.getIntegerReply(); } public int hexists(String key, String field) { + checkIsInMulti(); client.hexists(key, field); return client.getIntegerReply(); } public int hdel(String key, String field) { + checkIsInMulti(); client.hdel(key, field); return client.getIntegerReply(); } public int hlen(String key) { + checkIsInMulti(); client.hlen(key); return client.getIntegerReply(); } public List hkeys(String key) { + checkIsInMulti(); client.hkeys(key); return client.getMultiBulkReply(); } public List hvals(String key) { + checkIsInMulti(); client.hvals(key); return client.getMultiBulkReply(); } public Map hgetAll(String key) { + checkIsInMulti(); client.hgetAll(key); List flatHash = client.getMultiBulkReply(); Map hash = new HashMap(); @@ -248,190 +291,226 @@ public class Jedis { } public int rpush(String key, String string) { + checkIsInMulti(); client.rpush(key, string); return client.getIntegerReply(); } public int lpush(String key, String string) { + checkIsInMulti(); client.lpush(key, string); return client.getIntegerReply(); } public int llen(String key) { + checkIsInMulti(); client.llen(key); return client.getIntegerReply(); } public List lrange(String key, int start, int end) { + checkIsInMulti(); client.lrange(key, start, end); return client.getMultiBulkReply(); } public String ltrim(String key, int start, int end) { + checkIsInMulti(); client.ltrim(key, start, end); return client.getStatusCodeReply(); } public String lindex(String key, int index) { + checkIsInMulti(); client.lindex(key, index); return client.getBulkReply(); } public String lset(String key, int index, String value) { + checkIsInMulti(); client.lset(key, index, value); return client.getStatusCodeReply(); } public int lrem(String key, int count, String value) { + checkIsInMulti(); client.lrem(key, count, value); return client.getIntegerReply(); } public String lpop(String key) { + checkIsInMulti(); client.lpop(key); return client.getBulkReply(); } public String rpop(String key) { + checkIsInMulti(); client.rpop(key); return client.getBulkReply(); } public String rpoplpush(String srckey, String dstkey) { + checkIsInMulti(); client.rpoplpush(srckey, dstkey); return client.getBulkReply(); } public int sadd(String key, String member) { + checkIsInMulti(); client.sadd(key, member); return client.getIntegerReply(); } public Set smembers(String key) { + checkIsInMulti(); client.smembers(key); List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } public int srem(String key, String member) { + checkIsInMulti(); client.srem(key, member); return client.getIntegerReply(); } public String spop(String key) { + checkIsInMulti(); client.spop(key); return client.getBulkReply(); } public int smove(String srckey, String dstkey, String member) { + checkIsInMulti(); client.smove(srckey, dstkey, member); return client.getIntegerReply(); } public int scard(String key) { + checkIsInMulti(); client.scard(key); return client.getIntegerReply(); } public int sismember(String key, String member) { + checkIsInMulti(); client.sismember(key, member); return client.getIntegerReply(); } public Set sinter(String... keys) { + checkIsInMulti(); client.sinter(keys); List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } public int sinterstore(String dstkey, String... keys) { + checkIsInMulti(); client.sinterstore(dstkey, keys); return client.getIntegerReply(); } public Set sunion(String... keys) { + checkIsInMulti(); client.sunion(keys); List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } public int sunionstore(String dstkey, String... keys) { + checkIsInMulti(); client.sunionstore(dstkey, keys); return client.getIntegerReply(); } public Set sdiff(String... keys) { + checkIsInMulti(); client.sdiff(keys); List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } public int sdiffstore(String dstkey, String... keys) { + checkIsInMulti(); client.sdiffstore(dstkey, keys); return client.getIntegerReply(); } public String srandmember(String key) { + checkIsInMulti(); client.srandmember(key); return client.getBulkReply(); } public int zadd(String key, double score, String member) { + checkIsInMulti(); client.zadd(key, score, member); return client.getIntegerReply(); } public Set zrange(String key, int start, int end) { + checkIsInMulti(); client.zrange(key, start, end); List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } public int zrem(String key, String member) { + checkIsInMulti(); client.zrem(key, member); return client.getIntegerReply(); } public double zincrby(String key, double score, String member) { + checkIsInMulti(); client.zincrby(key, score, member); String newscore = client.getBulkReply(); return Double.valueOf(newscore); } public int zrank(String key, String member) { + checkIsInMulti(); client.zrank(key, member); return client.getIntegerReply(); } public int zrevrank(String key, String member) { + checkIsInMulti(); client.zrevrank(key, member); return client.getIntegerReply(); } public Set zrevrange(String key, int start, int end) { + checkIsInMulti(); client.zrevrange(key, start, end); List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } public Set zrangeWithScores(String key, int start, int end) { + checkIsInMulti(); client.zrangeWithScores(key, start, end); Set set = getTupledSet(); return set; } public Set zrevrangeWithScores(String key, int start, int end) { + checkIsInMulti(); client.zrevrangeWithScores(key, start, end); Set set = getTupledSet(); return set; } public int zcard(String key) { + checkIsInMulti(); client.zcard(key); return client.getIntegerReply(); } public double zscore(String key, String member) { + checkIsInMulti(); client.zscore(key, member); String score = client.getBulkReply(); return Double.valueOf(score); @@ -444,14 +523,23 @@ public class Jedis { } public List multi(TransactionBlock jedisTransaction) { + List results = null; try { jedisTransaction.setClient(client); multi(); jedisTransaction.execute(); + results = jedisTransaction.exec(); } catch (Exception ex) { client.discard(); } - return jedisTransaction.exec(); + return results; + } + + private void checkIsInMulti() { + if (client.isInMulti()) { + throw new JedisException( + "Cannot use Jedis when in Multi. Please use JedisTransaction instead."); + } } public void connect() throws UnknownHostException, IOException { @@ -473,16 +561,19 @@ public class Jedis { } public List sort(String key) { + checkIsInMulti(); client.sort(key); return client.getMultiBulkReply(); } public List sort(String key, SortingParams sortingParameters) { + checkIsInMulti(); client.sort(key, sortingParameters); return client.getMultiBulkReply(); } public List blpop(int timeout, String... keys) { + checkIsInMulti(); List args = new ArrayList(); for (String arg : keys) { args.add(arg); @@ -497,16 +588,19 @@ public class Jedis { } public int sort(String key, SortingParams sortingParameters, String dstkey) { + checkIsInMulti(); client.sort(key, sortingParameters, dstkey); return client.getIntegerReply(); } public int sort(String key, String dstkey) { + checkIsInMulti(); client.sort(key, dstkey); return client.getIntegerReply(); } public List brpop(int timeout, String... keys) { + checkIsInMulti(); List args = new ArrayList(); for (String arg : keys) { args.add(arg); @@ -522,6 +616,7 @@ public class Jedis { } public String auth(String password) { + checkIsInMulti(); client.auth(password); return client.getStatusCodeReply(); } @@ -549,22 +644,26 @@ public class Jedis { } public int zcount(String key, double min, double max) { + checkIsInMulti(); client.zcount(key, min, max); return client.getIntegerReply(); } public Set zrangeByScore(String key, double min, double max) { + checkIsInMulti(); client.zrangeByScore(key, min, max); return new LinkedHashSet(client.getMultiBulkReply()); } public Set zrangeByScore(String key, double min, double max, int offset, int count) { + checkIsInMulti(); client.zrangeByScore(key, min, max, offset, count); return new LinkedHashSet(client.getMultiBulkReply()); } public Set zrangeByScoreWithScores(String key, double min, double max) { + checkIsInMulti(); client.zrangeByScoreWithScores(key, min, max); Set set = getTupledSet(); return set; @@ -572,12 +671,14 @@ public class Jedis { public Set zrangeByScoreWithScores(String key, double min, double max, int offset, int count) { + checkIsInMulti(); client.zrangeByScoreWithScores(key, min, max, offset, count); Set set = getTupledSet(); return set; } private Set getTupledSet() { + checkIsInMulti(); List membersWithScores = client.getMultiBulkReply(); Set set = new LinkedHashSet(); Iterator iterator = membersWithScores.iterator(); @@ -590,31 +691,37 @@ public class Jedis { } public int zremrangeByRank(String key, int start, int end) { + checkIsInMulti(); client.zremrangeByRank(key, start, end); return client.getIntegerReply(); } public int zremrangeByScore(String key, int start, int end) { + checkIsInMulti(); client.zremrangeByScore(key, start, end); return client.getIntegerReply(); } public int zunionstore(String dstkey, String... sets) { + checkIsInMulti(); client.zunionstore(dstkey, sets); return client.getIntegerReply(); } public int zunionstore(String dstkey, ZParams params, String... sets) { + checkIsInMulti(); client.zunionstore(dstkey, params, sets); return client.getIntegerReply(); } public int zinterstore(String dstkey, String... sets) { + checkIsInMulti(); client.zinterstore(dstkey, sets); return client.getIntegerReply(); } public int zinterstore(String dstkey, ZParams params, String... sets) { + checkIsInMulti(); client.zinterstore(dstkey, params, sets); return client.getIntegerReply(); } @@ -679,5 +786,4 @@ public class Jedis { client.configSet(parameter, value); return client.getStatusCodeReply(); } - } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Transaction.java b/src/main/java/redis/clients/jedis/Transaction.java index 4a9b7ac..949e691 100644 --- a/src/main/java/redis/clients/jedis/Transaction.java +++ b/src/main/java/redis/clients/jedis/Transaction.java @@ -405,10 +405,11 @@ public class Transaction { public List exec() { client.exec(); + return client.getObjectMultiBulkReply(); } - public void discard() { - client.discard(); - } + public void discard() { + client.discard(); + } } \ 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 ef6b784..41f96db 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java @@ -8,6 +8,7 @@ import java.util.List; import org.junit.Test; import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisException; import redis.clients.jedis.Transaction; import redis.clients.jedis.TransactionBlock; @@ -94,4 +95,10 @@ public class TransactionCommandsTest extends JedisCommandTestBase { expected.add("OK"); assertEquals(expected, resp); } + + @Test(expected = JedisException.class) + public void validateWhenInMulti() { + jedis.multi(); + jedis.ping(); + } } \ No newline at end of file