diff --git a/README.md b/README.md index bb2a7e0..9b29539 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,10 @@ Jedis is a WORK IN PROGRESS. ## What's still missing? - Sorting -- Transactions - Publish/Subscribe - Persistence control commands - Remote server control commands -- The AUTH, SORT, BLPOP, BRPOP, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE commands +- The WATCH, UNWATCH, AUTH, SORT, BLPOP, BRPOP, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE commands But stay close because things are going fast and all this will be implemented soon! @@ -24,6 +23,7 @@ But stay close because things are going fast and all this will be implemented so - Commands operating on lists (not SORT, BLPOP, BRPOP) - Commands operating on sets - Commands operating on sorted sets (not SORT, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE) +- Transactions ## How do I use it? diff --git a/pom.xml b/pom.xml index 84b1b9f..80fe26f 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 redis.clients jedis - 0.0.7 + 0.0.8 junit diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index 6b0bc9a..1b3783f 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -1,97 +1,379 @@ package redis.clients.jedis; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.Socket; -import java.net.UnknownHostException; +import java.util.ArrayList; import java.util.List; +import java.util.Map; -public class Client { - private String host; - private int port = Protocol.DEFAULT_PORT; - private Socket socket; - private boolean connected = false; - private Protocol protocol = new Protocol(); - private OutputStream outputStream; - private InputStream inputStream; +public class Client extends Connection { public Client(String host) { - super(); - this.host = host; - } - - protected Client sendCommand(String name, String... args) - throws JedisException { - if (!isConnected()) { - throw new JedisException("Please connect Jedis before using it."); - } - protocol.sendCommand(outputStream, name, args); - return this; + super(host); } public Client(String host, int port) { - super(); - this.host = host; - this.port = port; + super(host, port); } - public String getHost() { - return host; + public void ping() throws JedisException { + sendCommand("PING"); } - public void setHost(String host) { - this.host = host; + public void set(String key, String value) throws JedisException { + sendCommand("SET", key, value); } - public int getPort() { - return port; + public void get(String key) throws JedisException { + sendCommand("GET", key); } - public void setPort(int port) { - this.port = port; + public void quit() throws JedisException { + sendCommand("QUIT"); } - public Client() { + public void exists(String key) throws JedisException { + sendCommand("EXISTS", key); } - public void connect() throws UnknownHostException, IOException { - if (!connected) { - socket = new Socket(host, port); - connected = socket.isConnected(); - outputStream = socket.getOutputStream(); - inputStream = socket.getInputStream(); + public void del(String... keys) throws JedisException { + sendCommand("DEL", keys); + } + + public void type(String key) throws JedisException { + sendCommand("TYPE", key); + } + + public void flushDB() throws JedisException { + sendCommand("FLUSHDB"); + } + + public void keys(String pattern) throws JedisException { + sendCommand("KEYS", pattern); + } + + public void randomKey() throws JedisException { + sendCommand("RANDOMKEY"); + } + + public void rename(String oldkey, String newkey) throws JedisException { + sendCommand("RENAME", oldkey, newkey); + } + + public void renamenx(String oldkey, String newkey) throws JedisException { + sendCommand("RENAMENX", oldkey, newkey); + } + + public void dbSize() throws JedisException { + sendCommand("DBSIZE"); + } + + public void expire(String key, int seconds) throws JedisException { + sendCommand("EXPIRE", key, String.valueOf(seconds)); + } + + public void expireAt(String key, long unixTime) throws JedisException { + sendCommand("EXPIREAT", key, String.valueOf(unixTime)); + } + + public void ttl(String key) throws JedisException { + sendCommand("TTL", key); + } + + public void select(int index) throws JedisException { + sendCommand("SELECT", String.valueOf(index)); + } + + public void move(String key, int dbIndex) throws JedisException { + sendCommand("MOVE", key, String.valueOf(dbIndex)); + } + + public void flushAll() throws JedisException { + sendCommand("FLUSHALL"); + } + + public void getSet(String key, String value) throws JedisException { + sendCommand("GETSET", key, value); + } + + public void mget(String... keys) throws JedisException { + sendCommand("MGET", keys); + } + + public void setnx(String key, String value) throws JedisException { + sendCommand("SETNX", key, value); + } + + public void setex(String key, int seconds, String value) + throws JedisException { + sendCommand("SETEX", key, String.valueOf(seconds), value); + } + + public void mset(String... keysvalues) throws JedisException { + sendCommand("MSET", keysvalues); + } + + public void msetnx(String... keysvalues) throws JedisException { + sendCommand("MSETNX", keysvalues); + } + + public void decrBy(String key, int integer) throws JedisException { + sendCommand("DECRBY", key, String.valueOf(integer)); + } + + public void decr(String key) throws JedisException { + sendCommand("DECR", key); + } + + public void incrBy(String key, int integer) throws JedisException { + sendCommand("INCRBY", key, String.valueOf(integer)); + } + + public void incr(String key) throws JedisException { + sendCommand("INCR", key); + } + + public void append(String key, String value) throws JedisException { + sendCommand("APPEND", key, value); + } + + public void substr(String key, int start, int end) throws JedisException { + sendCommand("SUBSTR", key, String.valueOf(start), String.valueOf(end)); + } + + public void hset(String key, String field, String value) + throws JedisException { + sendCommand("HSET", key, field, value); + } + + public void hget(String key, String field) throws JedisException { + sendCommand("HGET", key, field); + } + + public void hsetnx(String key, String field, String value) + throws JedisException { + sendCommand("HSETNX", key, field, value); + } + + public void hmset(String key, Map hash) + throws JedisException { + List params = new ArrayList(); + params.add(key); + + for (String field : hash.keySet()) { + params.add(field); + params.add(hash.get(field)); } + sendCommand("HMSET", params.toArray(new String[params.size()])); } - public void disconnect() throws IOException { - if (connected) { - inputStream.close(); - outputStream.close(); - if (!socket.isClosed()) { - socket.close(); - } - connected = false; - } + public void hmget(String key, String... fields) throws JedisException { + String[] params = new String[fields.length + 1]; + params[0] = key; + System.arraycopy(fields, 0, params, 1, fields.length); + sendCommand("HMGET", params); } - public boolean isConnected() { - return connected; + public void hincrBy(String key, String field, int value) + throws JedisException { + sendCommand("HINCRBY", key, field, String.valueOf(value)); } - protected String getStatusCodeReply() throws JedisException { - return protocol.getSingleLineReply(inputStream); + public void hexists(String key, String field) throws JedisException { + sendCommand("HEXISTS", key, field); } - public String getBulkReply() throws JedisException { - return protocol.getBulkReply(inputStream); + public void hdel(String key, String field) throws JedisException { + sendCommand("HDEL", key, field); } - public int getIntegerReply() throws JedisException { - return protocol.getIntegerReply(inputStream); + public void hlen(String key) throws JedisException { + sendCommand("HLEN", key); } - public List getMultiBulkReply() throws JedisException { - return protocol.getMultiBulkReply(inputStream); + public void hkeys(String key) throws JedisException { + sendCommand("HKEYS", key); } + + public void hvals(String key) throws JedisException { + sendCommand("HVALS", key); + } + + public void hgetAll(String key) throws JedisException { + sendCommand("HGETALL", key); + } + + public void rpush(String key, String string) throws JedisException { + sendCommand("RPUSH", key, string); + } + + public void lpush(String key, String string) throws JedisException { + sendCommand("LPUSH", key, string); + } + + public void llen(String key) throws JedisException { + sendCommand("LLEN", key); + } + + public void lrange(String key, int start, int end) throws JedisException { + sendCommand("LRANGE", key, String.valueOf(start), String.valueOf(end)); + } + + public void ltrim(String key, int start, int end) throws JedisException { + sendCommand("LTRIM", key, String.valueOf(start), String.valueOf(end)); + } + + public void lindex(String key, int index) throws JedisException { + sendCommand("LINDEX", key, String.valueOf(index)); + } + + public void lset(String key, int index, String value) throws JedisException { + sendCommand("LSET", key, String.valueOf(index), value); + } + + public void lrem(String key, int count, String value) throws JedisException { + sendCommand("LREM", key, String.valueOf(count), value); + } + + public void lpop(String key) throws JedisException { + sendCommand("LPOP", key); + } + + public void rpop(String key) throws JedisException { + sendCommand("RPOP", key); + } + + public void rpoplpush(String srckey, String dstkey) throws JedisException { + sendCommand("RPOPLPUSH", srckey, dstkey); + } + + public void sadd(String key, String member) throws JedisException { + sendCommand("SADD", key, member); + } + + public void smembers(String key) throws JedisException { + sendCommand("SMEMBERS", key); + } + + public void srem(String key, String member) throws JedisException { + sendCommand("SREM", key, member); + } + + public void spop(String key) throws JedisException { + sendCommand("SPOP", key); + } + + public void smove(String srckey, String dstkey, String member) + throws JedisException { + sendCommand("SMOVE", srckey, dstkey, member); + } + + public void scard(String key) throws JedisException { + sendCommand("SCARD", key); + } + + public void sismember(String key, String member) throws JedisException { + sendCommand("SISMEMBER", key, member); + } + + public void sinter(String... keys) throws JedisException { + sendCommand("SINTER", keys); + } + + public void sinterstore(String dstkey, String... keys) + throws JedisException { + String[] params = new String[keys.length + 1]; + params[0] = dstkey; + System.arraycopy(keys, 0, params, 1, keys.length); + sendCommand("SINTERSTORE", params); + } + + public void sunion(String... keys) throws JedisException { + sendCommand("SUNION", keys); + } + + public void sunionstore(String dstkey, String... keys) + throws JedisException { + String[] params = new String[keys.length + 1]; + params[0] = dstkey; + System.arraycopy(keys, 0, params, 1, keys.length); + sendCommand("SUNIONSTORE", params); + } + + public void sdiff(String... keys) throws JedisException { + sendCommand("SDIFF", keys); + } + + public void sdiffstore(String dstkey, String... keys) throws JedisException { + String[] params = new String[keys.length + 1]; + params[0] = dstkey; + System.arraycopy(keys, 0, params, 1, keys.length); + sendCommand("SDIFFSTORE", params); + } + + public void srandmember(String key) throws JedisException { + sendCommand("SRANDMEMBER", key); + } + + public void zadd(String key, double score, String member) + throws JedisException { + sendCommand("ZADD", key, String.valueOf(score), member); + } + + public void zrange(String key, int start, int end) throws JedisException { + sendCommand("ZRANGE", key, String.valueOf(start), String.valueOf(end)); + } + + public void zrem(String key, String member) throws JedisException { + sendCommand("ZREM", key, member); + } + + public void zincrby(String key, double score, String member) + throws JedisException { + sendCommand("ZINCRBY", key, String.valueOf(score), member); + } + + public void zrank(String key, String member) throws JedisException { + sendCommand("ZRANK", key, member); + } + + public void zrevrank(String key, String member) throws JedisException { + sendCommand("ZREVRANK", key, member); + } + + public void zrevrange(String key, int start, int end) throws JedisException { + sendCommand("ZREVRANGE", key, String.valueOf(start), String + .valueOf(end)); + } + + public void zrangeWithScores(String key, int start, int end) + throws JedisException { + sendCommand("ZRANGE", key, String.valueOf(start), String.valueOf(end), + "WITHSCORES"); + } + + public void zrevrangeWithScores(String key, int start, int end) + throws JedisException { + sendCommand("ZREVRANGE", key, String.valueOf(start), String + .valueOf(end), "WITHSCORES"); + } + + public void zcard(String key) throws JedisException { + sendCommand("ZCARD", key); + } + + public void zscore(String key, String member) throws JedisException { + sendCommand("ZSCORE", key, member); + } + + public void multi() throws JedisException { + sendCommand("MULTI"); + } + + public void discard() throws JedisException { + sendCommand("MULTI"); + } + + public void exec() throws JedisException { + sendCommand("EXEC"); + } + } \ 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 new file mode 100644 index 0000000..828054e --- /dev/null +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -0,0 +1,97 @@ +package redis.clients.jedis; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; +import java.net.UnknownHostException; +import java.util.List; + +public class Connection { + private String host; + private int port = Protocol.DEFAULT_PORT; + private Socket socket; + private boolean connected = false; + private Protocol protocol = new Protocol(); + private OutputStream outputStream; + private InputStream inputStream; + + public Connection(String host) { + super(); + this.host = host; + } + + protected Connection sendCommand(String name, String... args) + throws JedisException { + if (!isConnected()) { + throw new JedisException("Please connect Jedis before using it."); + } + protocol.sendCommand(outputStream, name, args); + return this; + } + + public Connection(String host, int port) { + super(); + this.host = host; + this.port = port; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public Connection() { + } + + public void connect() throws UnknownHostException, IOException { + if (!connected) { + socket = new Socket(host, port); + connected = socket.isConnected(); + outputStream = socket.getOutputStream(); + inputStream = socket.getInputStream(); + } + } + + public void disconnect() throws IOException { + if (connected) { + inputStream.close(); + outputStream.close(); + if (!socket.isClosed()) { + socket.close(); + } + connected = false; + } + } + + public boolean isConnected() { + return connected; + } + + protected String getStatusCodeReply() throws JedisException { + return protocol.getSingleLineReply(inputStream); + } + + public String getBulkReply() throws JedisException { + return protocol.getBulkReply(inputStream); + } + + public int getIntegerReply() throws JedisException { + return protocol.getIntegerReply(inputStream); + } + + public List getMultiBulkReply() throws JedisException { + return protocol.getMultiBulkReply(inputStream); + } +} \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index 82a2353..6c5657b 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -1,6 +1,7 @@ package redis.clients.jedis; -import java.util.ArrayList; +import java.io.IOException; +import java.net.UnknownHostException; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashSet; @@ -8,207 +9,235 @@ import java.util.List; import java.util.Map; import java.util.Set; -public class Jedis extends Client { +public class Jedis { + private Client client = null; + public Jedis(String host) { - super(host); + client = new Client(host); + } + + public Jedis(String host, int port) { + client = new Client(host, port); } public String ping() throws JedisException { - return sendCommand("PING").getStatusCodeReply(); + client.ping(); + return client.getStatusCodeReply(); } public String set(String key, String value) throws JedisException { - return sendCommand("SET", key, value).getStatusCodeReply(); + client.set(key, value); + return client.getStatusCodeReply(); } public String get(String key) throws JedisException { - return sendCommand("GET", key).getBulkReply(); + client.sendCommand("GET", key); + return client.getBulkReply(); } public void quit() throws JedisException { - sendCommand("QUIT"); + client.quit(); } public int exists(String key) throws JedisException { - return sendCommand("EXISTS", key).getIntegerReply(); + client.exists(key); + return client.getIntegerReply(); } public int del(String... keys) throws JedisException { - return sendCommand("DEL", keys).getIntegerReply(); + client.del(keys); + return client.getIntegerReply(); } public String type(String key) throws JedisException { - return sendCommand("TYPE", key).getStatusCodeReply(); + client.type(key); + return client.getStatusCodeReply(); } public String flushDB() throws JedisException { - return sendCommand("FLUSHDB").getStatusCodeReply(); + client.flushDB(); + return client.getStatusCodeReply(); } public List keys(String pattern) throws JedisException { - return sendCommand("KEYS", pattern).getMultiBulkReply(); + client.keys(pattern); + return client.getMultiBulkReply(); } public String randomKey() throws JedisException { - return sendCommand("RANDOMKEY").getBulkReply(); + client.randomKey(); + return client.getBulkReply(); } public String rename(String oldkey, String newkey) throws JedisException { - return sendCommand("RENAME", oldkey, newkey).getStatusCodeReply(); + client.rename(oldkey, newkey); + return client.getStatusCodeReply(); } public int renamenx(String oldkey, String newkey) throws JedisException { - return sendCommand("RENAMENX", oldkey, newkey).getIntegerReply(); + client.renamenx(oldkey, newkey); + return client.getIntegerReply(); } public int dbSize() throws JedisException { - return sendCommand("DBSIZE").getIntegerReply(); + client.dbSize(); + return client.getIntegerReply(); } public int expire(String key, int seconds) throws JedisException { - return sendCommand("EXPIRE", key, String.valueOf(seconds)) - .getIntegerReply(); + client.expire(key, seconds); + return client.getIntegerReply(); } public int expireAt(String key, long unixTime) throws JedisException { - return sendCommand("EXPIREAT", key, String.valueOf(unixTime)) - .getIntegerReply(); + client.expireAt(key, unixTime); + return client.getIntegerReply(); } public int ttl(String key) throws JedisException { - return sendCommand("TTL", key).getIntegerReply(); + client.ttl(key); + return client.getIntegerReply(); } public String select(int index) throws JedisException { - return sendCommand("SELECT", String.valueOf(index)) - .getStatusCodeReply(); + client.select(index); + return client.getStatusCodeReply(); } public int move(String key, int dbIndex) throws JedisException { - return sendCommand("MOVE", key, String.valueOf(dbIndex)) - .getIntegerReply(); + client.move(key, dbIndex); + return client.getIntegerReply(); } public String flushAll() throws JedisException { - return sendCommand("FLUSHALL").getStatusCodeReply(); + client.flushAll(); + return client.getStatusCodeReply(); } public String getSet(String key, String value) throws JedisException { - return sendCommand("GETSET", key, value).getBulkReply(); + client.getSet(key, value); + return client.getBulkReply(); } public List mget(String... keys) throws JedisException { - return sendCommand("MGET", keys).getMultiBulkReply(); + client.mget(keys); + return client.getMultiBulkReply(); } public int setnx(String key, String value) throws JedisException { - return sendCommand("SETNX", key, value).getIntegerReply(); + client.setnx(key, value); + return client.getIntegerReply(); } public String setex(String key, int seconds, String value) throws JedisException { - return sendCommand("SETEX", key, String.valueOf(seconds), value) - .getStatusCodeReply(); + client.setex(key, seconds, value); + return client.getStatusCodeReply(); } public String mset(String... keysvalues) throws JedisException { - return sendCommand("MSET", keysvalues).getStatusCodeReply(); + client.mset(keysvalues); + return client.getStatusCodeReply(); } public int msetnx(String... keysvalues) throws JedisException { - return sendCommand("MSETNX", keysvalues).getIntegerReply(); + client.msetnx(keysvalues); + return client.getIntegerReply(); } public int decrBy(String key, int integer) throws JedisException { - return sendCommand("DECRBY", key, String.valueOf(integer)) - .getIntegerReply(); + client.decrBy(key, integer); + return client.getIntegerReply(); } public int decr(String key) throws JedisException { - return sendCommand("DECR", key).getIntegerReply(); + client.decr(key); + return client.getIntegerReply(); } public int incrBy(String key, int integer) throws JedisException { - return sendCommand("INCRBY", key, String.valueOf(integer)) - .getIntegerReply(); + client.incrBy(key, integer); + return client.getIntegerReply(); } public int incr(String key) throws JedisException { - return sendCommand("INCR", key).getIntegerReply(); + client.incr(key); + return client.getIntegerReply(); } public int append(String key, String value) throws JedisException { - return sendCommand("APPEND", key, value).getIntegerReply(); + client.append(key, value); + return client.getIntegerReply(); } public String substr(String key, int start, int end) throws JedisException { - return sendCommand("SUBSTR", key, String.valueOf(start), - String.valueOf(end)).getBulkReply(); + client.substr(key, start, end); + return client.getBulkReply(); } public int hset(String key, String field, String value) throws JedisException { - return sendCommand("HSET", key, field, value).getIntegerReply(); + client.hset(key, field, value); + return client.getIntegerReply(); } public String hget(String key, String field) throws JedisException { - return sendCommand("HGET", key, field).getBulkReply(); + client.hget(key, field); + return client.getBulkReply(); } public int hsetnx(String key, String field, String value) throws JedisException { - return sendCommand("HSETNX", key, field, value).getIntegerReply(); + client.hsetnx(key, field, value); + return client.getIntegerReply(); } public String hmset(String key, Map hash) throws JedisException { - List params = new ArrayList(); - params.add(key); - - for (String field : hash.keySet()) { - params.add(field); - params.add(hash.get(field)); - } - return sendCommand("HMSET", params.toArray(new String[params.size()])) - .getStatusCodeReply(); + client.hmset(key, hash); + return client.getStatusCodeReply(); } public List hmget(String key, String... fields) throws JedisException { - String[] params = new String[fields.length + 1]; - params[0] = key; - System.arraycopy(fields, 0, params, 1, fields.length); - return sendCommand("HMGET", params).getMultiBulkReply(); + client.hmget(key, fields); + return client.getMultiBulkReply(); } public int hincrBy(String key, String field, int value) throws JedisException { - return sendCommand("HINCRBY", key, field, String.valueOf(value)) - .getIntegerReply(); + client.hincrBy(key, field, value); + return client.getIntegerReply(); } public int hexists(String key, String field) throws JedisException { - return sendCommand("HEXISTS", key, field).getIntegerReply(); + client.hexists(key, field); + return client.getIntegerReply(); } public int hdel(String key, String field) throws JedisException { - return sendCommand("HDEL", key, field).getIntegerReply(); + client.hdel(key, field); + return client.getIntegerReply(); } public int hlen(String key) throws JedisException { - return sendCommand("HLEN", key).getIntegerReply(); + client.hlen(key); + return client.getIntegerReply(); } public List hkeys(String key) throws JedisException { - return sendCommand("HKEYS", key).getMultiBulkReply(); + client.hkeys(key); + return client.getMultiBulkReply(); } public List hvals(String key) throws JedisException { - return sendCommand("HVALS", key).getMultiBulkReply(); + client.hvals(key); + return client.getMultiBulkReply(); } public Map hgetAll(String key) throws JedisException { - List flatHash = sendCommand("HGETALL", key).getMultiBulkReply(); + client.hgetAll(key); + List flatHash = client.getMultiBulkReply(); Map hash = new HashMap(); Iterator iterator = flatHash.iterator(); while (iterator.hasNext()) { @@ -219,169 +248,183 @@ public class Jedis extends Client { } public int rpush(String key, String string) throws JedisException { - return sendCommand("RPUSH", key, string).getIntegerReply(); + client.rpush(key, string); + return client.getIntegerReply(); } public int lpush(String key, String string) throws JedisException { - return sendCommand("LPUSH", key, string).getIntegerReply(); + client.lpush(key, string); + return client.getIntegerReply(); } public int llen(String key) throws JedisException { - return sendCommand("LLEN", key).getIntegerReply(); + client.llen(key); + return client.getIntegerReply(); } public List lrange(String key, int start, int end) throws JedisException { - return sendCommand("LRANGE", key, String.valueOf(start), - String.valueOf(end)).getMultiBulkReply(); + client.lrange(key, start, end); + return client.getMultiBulkReply(); } public String ltrim(String key, int start, int end) throws JedisException { - return sendCommand("LTRIM", key, String.valueOf(start), - String.valueOf(end)).getStatusCodeReply(); + client.ltrim(key, start, end); + return client.getStatusCodeReply(); } public String lindex(String key, int index) throws JedisException { - return sendCommand("LINDEX", key, String.valueOf(index)).getBulkReply(); + client.lindex(key, index); + return client.getBulkReply(); } public String lset(String key, int index, String value) throws JedisException { - return sendCommand("LSET", key, String.valueOf(index), value) - .getStatusCodeReply(); + client.lset(key, index, value); + return client.getStatusCodeReply(); } public int lrem(String key, int count, String value) throws JedisException { - return sendCommand("LREM", key, String.valueOf(count), value) - .getIntegerReply(); + client.lrem(key, count, value); + return client.getIntegerReply(); } public String lpop(String key) throws JedisException { - return sendCommand("LPOP", key).getBulkReply(); + client.lpop(key); + return client.getBulkReply(); } public String rpop(String key) throws JedisException { - return sendCommand("RPOP", key).getBulkReply(); + client.rpop(key); + return client.getBulkReply(); } public String rpoplpush(String srckey, String dstkey) throws JedisException { - return sendCommand("RPOPLPUSH", srckey, dstkey).getBulkReply(); + client.rpoplpush(srckey, dstkey); + return client.getBulkReply(); } public int sadd(String key, String member) throws JedisException { - return sendCommand("SADD", key, member).getIntegerReply(); + client.sadd(key, member); + return client.getIntegerReply(); } public Set smembers(String key) throws JedisException { - List members = sendCommand("SMEMBERS", key).getMultiBulkReply(); + client.smembers(key); + List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } public int srem(String key, String member) throws JedisException { - return sendCommand("SREM", key, member).getIntegerReply(); + client.srem(key, member); + return client.getIntegerReply(); } public String spop(String key) throws JedisException { - return sendCommand("SPOP", key).getBulkReply(); + client.spop(key); + return client.getBulkReply(); } public int smove(String srckey, String dstkey, String member) throws JedisException { - return sendCommand("SMOVE", srckey, dstkey, member).getIntegerReply(); + client.smove(srckey, dstkey, member); + return client.getIntegerReply(); } public int scard(String key) throws JedisException { - return sendCommand("SCARD", key).getIntegerReply(); + client.scard(key); + return client.getIntegerReply(); } public int sismember(String key, String member) throws JedisException { - return sendCommand("SISMEMBER", key, member).getIntegerReply(); + client.sismember(key, member); + return client.getIntegerReply(); } public Set sinter(String... keys) throws JedisException { - List members = sendCommand("SINTER", keys).getMultiBulkReply(); + client.sinter(keys); + List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } public int sinterstore(String dstkey, String... keys) throws JedisException { - String[] params = new String[keys.length + 1]; - params[0] = dstkey; - System.arraycopy(keys, 0, params, 1, keys.length); - return sendCommand("SINTERSTORE", params).getIntegerReply(); + client.sinterstore(dstkey, keys); + return client.getIntegerReply(); } public Set sunion(String... keys) throws JedisException { - List members = sendCommand("SUNION", keys).getMultiBulkReply(); + client.sunion(keys); + List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } public int sunionstore(String dstkey, String... keys) throws JedisException { - String[] params = new String[keys.length + 1]; - params[0] = dstkey; - System.arraycopy(keys, 0, params, 1, keys.length); - return sendCommand("SUNIONSTORE", params).getIntegerReply(); + client.sunionstore(dstkey, keys); + return client.getIntegerReply(); } public Set sdiff(String... keys) throws JedisException { - List members = sendCommand("SDIFF", keys).getMultiBulkReply(); + client.sdiff(keys); + List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } public int sdiffstore(String dstkey, String... keys) throws JedisException { - String[] params = new String[keys.length + 1]; - params[0] = dstkey; - System.arraycopy(keys, 0, params, 1, keys.length); - return sendCommand("SDIFFSTORE", params).getIntegerReply(); + client.sdiffstore(dstkey, keys); + return client.getIntegerReply(); } public String srandmember(String key) throws JedisException { - return sendCommand("SRANDMEMBER", key).getBulkReply(); + client.srandmember(key); + return client.getBulkReply(); } public int zadd(String key, double score, String member) throws JedisException { - return sendCommand("ZADD", key, String.valueOf(score), member) - .getIntegerReply(); + client.zadd(key, score, member); + return client.getIntegerReply(); } public Set zrange(String key, int start, int end) throws JedisException { - List members = sendCommand("ZRANGE", key, - String.valueOf(start), String.valueOf(end)).getMultiBulkReply(); + client.zrange(key, start, end); + List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } public int zrem(String key, String member) throws JedisException { - return sendCommand("ZREM", key, member).getIntegerReply(); + client.zrem(key, member); + return client.getIntegerReply(); } public double zincrby(String key, double score, String member) throws JedisException { - String newscore = sendCommand("ZINCRBY", key, String.valueOf(score), - member).getBulkReply(); + client.zincrby(key, score, member); + String newscore = client.getBulkReply(); return Double.valueOf(newscore); } public int zrank(String key, String member) throws JedisException { - return sendCommand("ZRANK", key, member).getIntegerReply(); + client.zrank(key, member); + return client.getIntegerReply(); } public int zrevrank(String key, String member) throws JedisException { - return sendCommand("ZREVRANK", key, member).getIntegerReply(); + client.zrevrank(key, member); + return client.getIntegerReply(); } public Set zrevrange(String key, int start, int end) throws JedisException { - List members = sendCommand("ZREVRANGE", key, - String.valueOf(start), String.valueOf(end)).getMultiBulkReply(); + client.zrevrange(key, start, end); + List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } public Set zrangeWithScores(String key, int start, int end) throws JedisException { - List membersWithScores = sendCommand("ZRANGE", key, - String.valueOf(start), String.valueOf(end), "WITHSCORES") - .getMultiBulkReply(); + client.zrangeWithScores(key, start, end); + List membersWithScores = client.getMultiBulkReply(); Set set = new LinkedHashSet(); Iterator iterator = membersWithScores.iterator(); while (iterator.hasNext()) { @@ -394,9 +437,8 @@ public class Jedis extends Client { public Set zrevrangeWithScores(String key, int start, int end) throws JedisException { - List membersWithScores = sendCommand("ZREVRANGE", key, - String.valueOf(start), String.valueOf(end), "WITHSCORES") - .getMultiBulkReply(); + client.zrevrangeWithScores(key, start, end); + List membersWithScores = client.getMultiBulkReply(); Set set = new LinkedHashSet(); Iterator iterator = membersWithScores.iterator(); while (iterator.hasNext()) { @@ -408,11 +450,39 @@ public class Jedis extends Client { } public int zcard(String key) throws JedisException { - return sendCommand("ZCARD", key).getIntegerReply(); + client.zcard(key); + return client.getIntegerReply(); } public double zscore(String key, String member) throws JedisException { - String score = sendCommand("ZSCORE", key, member).getBulkReply(); + client.zscore(key, member); + String score = client.getBulkReply(); return Double.valueOf(score); } + + public Transaction multi() throws JedisException { + client.multi(); + client.getStatusCodeReply(); + return new Transaction(client); + } + + public void multi(TransactionBlock jedisTransaction) throws JedisException { + try { + jedisTransaction.setClient(client); + client.multi(); + client.getStatusCodeReply(); + jedisTransaction.execute(); + } catch (Exception ex) { + client.discard(); + } + } + + public void connect() throws UnknownHostException, IOException { + client.connect(); + } + + public void disconnect() throws IOException { + client.disconnect(); + } + } diff --git a/src/main/java/redis/clients/jedis/Transaction.java b/src/main/java/redis/clients/jedis/Transaction.java new file mode 100644 index 0000000..d8b6d42 --- /dev/null +++ b/src/main/java/redis/clients/jedis/Transaction.java @@ -0,0 +1,424 @@ +package redis.clients.jedis; + +import java.util.Map; + +public class Transaction { + protected Client client = null; + + public Transaction() { + } + + public Transaction(Client client) { + this.client = client; + } + + public String ping() throws JedisException { + client.ping(); + return client.getStatusCodeReply(); + } + + public String set(String key, String value) throws JedisException { + client.set(key, value); + return client.getStatusCodeReply(); + } + + public String get(String key) throws JedisException { + client.sendCommand("GET", key); + return client.getStatusCodeReply(); + } + + public String exists(String key) throws JedisException { + client.exists(key); + return client.getStatusCodeReply(); + } + + public String del(String... keys) throws JedisException { + client.del(keys); + return client.getStatusCodeReply(); + } + + public String type(String key) throws JedisException { + client.type(key); + return client.getStatusCodeReply(); + } + + public String flushDB() throws JedisException { + client.flushDB(); + return client.getStatusCodeReply(); + } + + public String keys(String pattern) throws JedisException { + client.keys(pattern); + return client.getStatusCodeReply(); + } + + public String randomKey() throws JedisException { + client.randomKey(); + return client.getStatusCodeReply(); + } + + public String rename(String oldkey, String newkey) throws JedisException { + client.rename(oldkey, newkey); + return client.getStatusCodeReply(); + } + + public String renamenx(String oldkey, String newkey) throws JedisException { + client.renamenx(oldkey, newkey); + return client.getStatusCodeReply(); + } + + public String dbSize() throws JedisException { + client.dbSize(); + return client.getStatusCodeReply(); + } + + public String expire(String key, int seconds) throws JedisException { + client.expire(key, seconds); + return client.getStatusCodeReply(); + } + + public String expireAt(String key, long unixTime) throws JedisException { + client.expireAt(key, unixTime); + return client.getStatusCodeReply(); + } + + public String ttl(String key) throws JedisException { + client.ttl(key); + return client.getStatusCodeReply(); + } + + public String select(int index) throws JedisException { + client.select(index); + return client.getStatusCodeReply(); + } + + public String move(String key, int dbIndex) throws JedisException { + client.move(key, dbIndex); + return client.getStatusCodeReply(); + } + + public String flushAll() throws JedisException { + client.flushAll(); + return client.getStatusCodeReply(); + } + + public String getSet(String key, String value) throws JedisException { + client.getSet(key, value); + return client.getStatusCodeReply(); + } + + public String mget(String... keys) throws JedisException { + client.mget(keys); + return client.getStatusCodeReply(); + } + + public String setnx(String key, String value) throws JedisException { + client.setnx(key, value); + return client.getStatusCodeReply(); + } + + public String setex(String key, int seconds, String value) + throws JedisException { + client.setex(key, seconds, value); + return client.getStatusCodeReply(); + } + + public String mset(String... keysvalues) throws JedisException { + client.mset(keysvalues); + return client.getStatusCodeReply(); + } + + public String msetnx(String... keysvalues) throws JedisException { + client.msetnx(keysvalues); + return client.getStatusCodeReply(); + } + + public String decrBy(String key, int integer) throws JedisException { + client.decrBy(key, integer); + return client.getStatusCodeReply(); + } + + public String decr(String key) throws JedisException { + client.decr(key); + return client.getStatusCodeReply(); + } + + public String incrBy(String key, int integer) throws JedisException { + client.incrBy(key, integer); + return client.getStatusCodeReply(); + } + + public String incr(String key) throws JedisException { + client.incr(key); + return client.getStatusCodeReply(); + } + + public String append(String key, String value) throws JedisException { + client.append(key, value); + return client.getStatusCodeReply(); + } + + public String substr(String key, int start, int end) throws JedisException { + client.substr(key, start, end); + return client.getStatusCodeReply(); + } + + public String hset(String key, String field, String value) + throws JedisException { + client.hset(key, field, value); + return client.getStatusCodeReply(); + } + + public String hget(String key, String field) throws JedisException { + client.hget(key, field); + return client.getStatusCodeReply(); + } + + public String hsetnx(String key, String field, String value) + throws JedisException { + client.hsetnx(key, field, value); + return client.getStatusCodeReply(); + } + + public String hmset(String key, Map hash) + throws JedisException { + client.hmset(key, hash); + return client.getStatusCodeReply(); + } + + public String hmget(String key, String... fields) throws JedisException { + client.hmget(key, fields); + return client.getStatusCodeReply(); + } + + public String hincrBy(String key, String field, int value) + throws JedisException { + client.hincrBy(key, field, value); + return client.getStatusCodeReply(); + } + + public String hexists(String key, String field) throws JedisException { + client.hexists(key, field); + return client.getStatusCodeReply(); + } + + public String hdel(String key, String field) throws JedisException { + client.hdel(key, field); + return client.getStatusCodeReply(); + } + + public String hlen(String key) throws JedisException { + client.hlen(key); + return client.getStatusCodeReply(); + } + + public String hkeys(String key) throws JedisException { + client.hkeys(key); + return client.getStatusCodeReply(); + } + + public String hvals(String key) throws JedisException { + client.hvals(key); + return client.getStatusCodeReply(); + } + + public String hgetAll(String key) throws JedisException { + client.hgetAll(key); + return client.getStatusCodeReply(); + } + + public String rpush(String key, String string) throws JedisException { + client.rpush(key, string); + return client.getStatusCodeReply(); + } + + public String lpush(String key, String string) throws JedisException { + client.lpush(key, string); + return client.getStatusCodeReply(); + } + + public String llen(String key) throws JedisException { + client.llen(key); + return client.getStatusCodeReply(); + } + + public String lrange(String key, int start, int end) throws JedisException { + client.lrange(key, start, end); + return client.getStatusCodeReply(); + } + + public String ltrim(String key, int start, int end) throws JedisException { + client.ltrim(key, start, end); + return client.getStatusCodeReply(); + } + + public String lindex(String key, int index) throws JedisException { + client.lindex(key, index); + return client.getStatusCodeReply(); + } + + public String lset(String key, int index, String value) + throws JedisException { + client.lset(key, index, value); + return client.getStatusCodeReply(); + } + + public String lrem(String key, int count, String value) + throws JedisException { + client.lrem(key, count, value); + return client.getStatusCodeReply(); + } + + public String lpop(String key) throws JedisException { + client.lpop(key); + return client.getStatusCodeReply(); + } + + public String rpop(String key) throws JedisException { + client.rpop(key); + return client.getStatusCodeReply(); + } + + public String rpoplpush(String srckey, String dstkey) throws JedisException { + client.rpoplpush(srckey, dstkey); + return client.getStatusCodeReply(); + } + + public String sadd(String key, String member) throws JedisException { + client.sadd(key, member); + return client.getStatusCodeReply(); + } + + public String smembers(String key) throws JedisException { + client.smembers(key); + return client.getStatusCodeReply(); + } + + public String srem(String key, String member) throws JedisException { + client.srem(key, member); + return client.getStatusCodeReply(); + } + + public String spop(String key) throws JedisException { + client.spop(key); + return client.getStatusCodeReply(); + } + + public String smove(String srckey, String dstkey, String member) + throws JedisException { + client.smove(srckey, dstkey, member); + return client.getStatusCodeReply(); + } + + public String scard(String key) throws JedisException { + client.scard(key); + return client.getStatusCodeReply(); + } + + public String sismember(String key, String member) throws JedisException { + client.sismember(key, member); + return client.getStatusCodeReply(); + } + + public String sinter(String... keys) throws JedisException { + client.sinter(keys); + return client.getStatusCodeReply(); + } + + public String sinterstore(String dstkey, String... keys) + throws JedisException { + client.sinterstore(dstkey, keys); + return client.getStatusCodeReply(); + } + + public String sunion(String... keys) throws JedisException { + client.sunion(keys); + return client.getStatusCodeReply(); + } + + public String sunionstore(String dstkey, String... keys) + throws JedisException { + client.sunionstore(dstkey, keys); + return client.getStatusCodeReply(); + } + + public String sdiff(String... keys) throws JedisException { + client.sdiff(keys); + return client.getStatusCodeReply(); + } + + public String sdiffstore(String dstkey, String... keys) + throws JedisException { + client.sdiffstore(dstkey, keys); + return client.getStatusCodeReply(); + } + + public String srandmember(String key) throws JedisException { + client.srandmember(key); + return client.getStatusCodeReply(); + } + + public String zadd(String key, double score, String member) + throws JedisException { + client.zadd(key, score, member); + return client.getStatusCodeReply(); + } + + public String zrange(String key, int start, int end) throws JedisException { + client.zrange(key, start, end); + return client.getStatusCodeReply(); + } + + public String zrem(String key, String member) throws JedisException { + client.zrem(key, member); + return client.getStatusCodeReply(); + } + + public String zincrby(String key, double score, String member) + throws JedisException { + client.zincrby(key, score, member); + return client.getStatusCodeReply(); + } + + public String zrank(String key, String member) throws JedisException { + client.zrank(key, member); + return client.getStatusCodeReply(); + } + + public String zrevrank(String key, String member) throws JedisException { + client.zrevrank(key, member); + return client.getStatusCodeReply(); + } + + public String zrevrange(String key, int start, int end) + throws JedisException { + client.zrevrange(key, start, end); + return client.getStatusCodeReply(); + } + + public String zrangeWithScores(String key, int start, int end) + throws JedisException { + client.zrangeWithScores(key, start, end); + return client.getStatusCodeReply(); + } + + public String zrevrangeWithScores(String key, int start, int end) + throws JedisException { + client.zrevrangeWithScores(key, start, end); + return client.getStatusCodeReply(); + } + + public String zcard(String key) throws JedisException { + client.zcard(key); + return client.getStatusCodeReply(); + } + + public String zscore(String key, String member) throws JedisException { + client.zscore(key, member); + return client.getStatusCodeReply(); + } + + public void exec() throws JedisException { + client.exec(); + } +} \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/TransactionBlock.java b/src/main/java/redis/clients/jedis/TransactionBlock.java new file mode 100644 index 0000000..4e59726 --- /dev/null +++ b/src/main/java/redis/clients/jedis/TransactionBlock.java @@ -0,0 +1,16 @@ +package redis.clients.jedis; + +public abstract class TransactionBlock extends Transaction { + public TransactionBlock(Client client) { + super(client); + } + + public TransactionBlock() { + } + + public abstract void execute() throws JedisException; + + public void setClient(Client client) { + this.client = client; + } +} diff --git a/src/main/java/redis/clients/jedis/Tuple.java b/src/main/java/redis/clients/jedis/Tuple.java new file mode 100644 index 0000000..2008595 --- /dev/null +++ b/src/main/java/redis/clients/jedis/Tuple.java @@ -0,0 +1,51 @@ +package redis.clients.jedis; + +public class Tuple { + private String element; + private double score; + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((element == null) ? 0 : element.hashCode()); + long temp; + temp = Double.doubleToLongBits(score); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Tuple other = (Tuple) obj; + if (element == null) { + if (other.element != null) + return false; + } else if (!element.equals(other.element)) + return false; + if (Double.doubleToLongBits(score) != Double + .doubleToLongBits(other.score)) + return false; + return true; + } + + public Tuple(String element, double score) { + super(); + this.element = element; + this.score = score; + } + + public String getElement() { + return element; + } + + public double getScore() { + return score; + } +} diff --git a/src/test/java/redis/clients/jedis/tests/ClientTest.java b/src/test/java/redis/clients/jedis/tests/ConnectionTest.java similarity index 83% rename from src/test/java/redis/clients/jedis/tests/ClientTest.java rename to src/test/java/redis/clients/jedis/tests/ConnectionTest.java index 806fc45..94d0073 100644 --- a/src/test/java/redis/clients/jedis/tests/ClientTest.java +++ b/src/test/java/redis/clients/jedis/tests/ConnectionTest.java @@ -8,14 +8,14 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import redis.clients.jedis.Client; +import redis.clients.jedis.Connection; -public class ClientTest extends Assert { - private Client client; +public class ConnectionTest extends Assert { + private Connection client; @Before public void setUp() throws Exception { - client = new Client(); + client = new Connection(); } @After 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 d45dab0..f1bd068 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java @@ -207,7 +207,7 @@ public class ListCommandsTest extends Assert { String element = jedis.rpoplpush("foo", "dst"); assertEquals("c", element); - + List srcExpected = new ArrayList(); srcExpected.add("a"); srcExpected.add("b"); diff --git a/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java new file mode 100644 index 0000000..dfc06a7 --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java @@ -0,0 +1,64 @@ +package redis.clients.jedis.tests.commands; + +import junit.framework.Assert; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisException; +import redis.clients.jedis.Transaction; +import redis.clients.jedis.TransactionBlock; + +public class TransactionCommandsTest extends Assert { + private Jedis jedis; + + @Before + public void setUp() throws Exception { + jedis = new Jedis("localhost"); + jedis.connect(); + } + + @After + public void tearDown() throws Exception { + jedis.flushDB(); + jedis.disconnect(); + } + + @Test + public void multi() throws JedisException { + Transaction trans = jedis.multi(); + + String status = trans.sadd("foo", "a"); + assertEquals("QUEUED", status); + + status = trans.sadd("foo", "b"); + assertEquals("QUEUED", status); + + status = trans.scard("foo"); + assertEquals("QUEUED", status); + + trans.exec(); + + //TODO: check for exec response + } + + @Test + public void multiBlock() throws JedisException { + jedis.multi(new TransactionBlock() { + public void execute() throws JedisException { + String status = sadd("foo", "a"); + assertEquals("QUEUED", status); + + status = sadd("foo", "b"); + assertEquals("QUEUED", status); + + status = scard("foo"); + assertEquals("QUEUED", status); + } + }); + + //TODO: check what happens when throwind an exception + } +} \ No newline at end of file