Added commands operating on all kind of values

This commit is contained in:
Jonathan Leibiusky
2010-06-12 00:29:46 -03:00
parent 4ccb12cd75
commit def3f38de7
10 changed files with 467 additions and 52 deletions

View File

@@ -5,6 +5,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
public class Client {
private String host;
@@ -74,12 +75,19 @@ public class Client {
return connected;
}
protected String getSingleLineReply() {
protected String getStatusCodeReply() throws JedisException {
return protocol.getSingleLineReply(inputStream);
}
public String getBulkReply() {
public String getBulkReply() throws JedisException {
return protocol.getBulkReply(inputStream);
}
public int getIntegerReply() throws JedisException {
return protocol.getIntegerReply(inputStream);
}
public List<String> getMultiBulkReply() throws JedisException {
return protocol.getMultiBulkReply(inputStream);
}
}

View File

@@ -1,21 +1,89 @@
package redis.clients.jedis;
import java.util.List;
public class Jedis extends Client {
public Jedis(String host) {
super(host);
}
public String ping() {
return sendCommand("PING").getSingleLineReply();
public String ping() throws JedisException {
return sendCommand("PING").getStatusCodeReply();
}
public String set(String key, String value) {
return sendCommand("SET", key, value).getSingleLineReply();
public String set(String key, String value) throws JedisException {
return sendCommand("SET", key, value).getStatusCodeReply();
}
public String get(String key) {
public String get(String key) throws JedisException {
return sendCommand("GET", key).getBulkReply();
}
public void quit() {
sendCommand("QUIT");
}
public int exists(String key) throws JedisException {
return sendCommand("EXISTS", key).getIntegerReply();
}
public int del(String... keys) throws JedisException {
return sendCommand("DEL", keys).getIntegerReply();
}
public String type(String key) throws JedisException {
return sendCommand("TYPE", key).getStatusCodeReply();
}
public String flushDB() throws JedisException {
return sendCommand("FLUSHDB").getStatusCodeReply();
}
public List<String> keys(String pattern) throws JedisException {
return sendCommand("KEYS", pattern).getMultiBulkReply();
}
public String randomKey() throws JedisException {
return sendCommand("RANDOMKEY").getStatusCodeReply();
}
public String rename(String oldkey, String newkey) throws JedisException {
return sendCommand("RENAME", oldkey, newkey).getStatusCodeReply();
}
public int renamenx(String oldkey, String newkey) throws JedisException {
return sendCommand("RENAMENX", oldkey, newkey).getIntegerReply();
}
public int dbSize() throws JedisException {
return sendCommand("DBSIZE").getIntegerReply();
}
public int expire(String key, int seconds) throws JedisException {
return sendCommand("EXPIRE", key, String.valueOf(seconds))
.getIntegerReply();
}
public int expireAt(String key, long unixTime) throws JedisException {
return sendCommand("EXPIREAT", key, String.valueOf(unixTime))
.getIntegerReply();
}
public int ttl(String key) throws JedisException {
return sendCommand("TTL", key).getIntegerReply();
}
public String select(int index) throws JedisException {
return sendCommand("SELECT", String.valueOf(index))
.getStatusCodeReply();
}
public int move(String key, int dbIndex) throws JedisException {
return sendCommand("MOVE", key, String.valueOf(dbIndex))
.getIntegerReply();
}
public String flushAll() throws JedisException {
return sendCommand("FLUSHALL").getStatusCodeReply();
}
}

View File

@@ -0,0 +1,12 @@
package redis.clients.jedis;
public class JedisException extends Exception {
/**
*
*/
private static final long serialVersionUID = -2946266495682282677L;
public JedisException(String message) {
super(message);
}
}

View File

@@ -10,6 +10,7 @@ public class Protocol {
public static final String DOLLAR = "$";
public static final String ASTERISK = "*";
public static final String PLUS = "+";
public static final String MINUS = "-";
public static final String COLON = ":";
public static final String COMMAND_DELIMITER = "\r\n";
public static final int DEFAULT_PORT = 6379;
@@ -17,6 +18,7 @@ public class Protocol {
public static final byte DOLLAR_BYTE = DOLLAR.getBytes()[0];
public static final byte ASTERISK_BYTE = ASTERISK.getBytes()[0];
public static final byte PLUS_BYTE = PLUS.getBytes()[0];
public static final byte MINUS_BYTE = MINUS.getBytes()[0];
public static final byte COLON_BYTE = COLON.getBytes()[0];
public void sendCommand(OutputStream os, String name, String... args) {
@@ -36,10 +38,40 @@ public class Protocol {
}
}
public String getBulkReply(InputStream is) {
public void processError(InputStream is) throws JedisException {
String message = readLine(is);
throw new JedisException(message);
}
private String readLine(InputStream is) {
byte b;
StringBuilder sb = new StringBuilder();
try {
while ((b = (byte) is.read()) != -1) {
if (b == '\r') {
b = (byte) is.read();
if (b == '\n') {
break;
}
}
sb.append((char) b);
}
} catch (IOException e) {
// TODO Dont know what to do here!
}
return sb.toString();
}
public String getBulkReply(InputStream is) throws JedisException {
String ret = null;
try {
if ((byte) is.read() == DOLLAR_BYTE) {
byte b = (byte) is.read();
if (b == MINUS_BYTE) {
processError(is);
}
if (b == DOLLAR_BYTE) {
int len = Integer.parseInt(readLine(is));
if (len == -1) {
return null;
@@ -60,26 +92,15 @@ public class Protocol {
return ret;
}
private String readLine(InputStream is) throws IOException {
byte b;
StringBuilder sb = new StringBuilder();
while ((b = (byte) is.read()) != -1) {
if (b == '\r') {
b = (byte) is.read();
if (b == '\n') {
break;
}
}
sb.append((char) b);
}
return sb.toString();
}
public String getSingleLineReply(InputStream is) {
public String getSingleLineReply(InputStream is) throws JedisException {
String ret = null;
try {
if ((byte) is.read() == PLUS_BYTE) {
byte b = (byte) is.read();
if (b == MINUS_BYTE) {
processError(is);
}
if (b == PLUS_BYTE) {
ret = readLine(is);
}
} catch (IOException e) {
@@ -89,24 +110,33 @@ public class Protocol {
return ret;
}
public int getIntegerReply(InputStream is) {
public int getIntegerReply(InputStream is) throws JedisException {
int ret = 0;
try {
if ((byte) is.read() == COLON_BYTE) {
byte b = (byte) is.read();
if (b == MINUS_BYTE) {
processError(is);
}
if (b == COLON_BYTE) {
String num = readLine(is);
ret = Integer.parseInt(num);
}
} catch (IOException e) {
// TODO Not sure that I should return 0
e.printStackTrace();
return 0;
}
return ret;
}
public List<String> getMultiBulkReply(InputStream is) {
public List<String> getMultiBulkReply(InputStream is) throws JedisException {
List<String> ret = new ArrayList<String>();
try {
if ((byte) is.read() == ASTERISK_BYTE) {
byte b = (byte) is.read();
if (b == MINUS_BYTE) {
processError(is);
}
if (b == ASTERISK_BYTE) {
int num = Integer.parseInt(readLine(is));
for (int i = 0; i < num; i++) {
ret.add(getBulkReply(is));