Refactored to support object multi bulk reply

This commit is contained in:
Jonathan Leibiusky
2010-07-07 22:55:35 -03:00
parent 81cc2cec8e
commit 20acb5bc60
7 changed files with 114 additions and 81 deletions

View File

@@ -375,5 +375,4 @@ public class Client extends Connection {
public void exec() throws JedisException { public void exec() throws JedisException {
sendCommand("EXEC"); sendCommand("EXEC");
} }
} }

View File

@@ -80,7 +80,7 @@ public class Connection {
} }
protected String getStatusCodeReply() throws JedisException { protected String getStatusCodeReply() throws JedisException {
return protocol.getSingleLineReply(inputStream); return protocol.getStatusCodeReply(inputStream);
} }
public String getBulkReply() throws JedisException { public String getBulkReply() throws JedisException {
@@ -91,7 +91,13 @@ public class Connection {
return protocol.getIntegerReply(inputStream); return protocol.getIntegerReply(inputStream);
} }
@SuppressWarnings("unchecked")
public List<String> getMultiBulkReply() throws JedisException { public List<String> getMultiBulkReply() throws JedisException {
return (List<String>) (List<?>) protocol.getMultiBulkReply(inputStream);
}
public List<Object> getObjectMultiBulkReply() throws JedisException {
return protocol.getMultiBulkReply(inputStream); return protocol.getMultiBulkReply(inputStream);
} }
} }

View File

@@ -466,15 +466,16 @@ public class Jedis {
return new Transaction(client); return new Transaction(client);
} }
public void multi(TransactionBlock jedisTransaction) throws JedisException { public List<Object> multi(TransactionBlock jedisTransaction)
throws JedisException {
try { try {
jedisTransaction.setClient(client); jedisTransaction.setClient(client);
client.multi(); multi();
client.getStatusCodeReply();
jedisTransaction.execute(); jedisTransaction.execute();
} catch (Exception ex) { } catch (Exception ex) {
client.discard(); client.discard();
} }
return jedisTransaction.exec();
} }
public void connect() throws UnknownHostException, IOException { public void connect() throws UnknownHostException, IOException {

View File

@@ -64,14 +64,49 @@ public class Protocol {
} }
public String getBulkReply(InputStream is) throws JedisException { public String getBulkReply(InputStream is) throws JedisException {
String ret = null; Object reply = process(is);
return (String) reply;
}
public String getStatusCodeReply(InputStream is) throws JedisException {
Object reply = process(is);
return (String) reply;
}
public int getIntegerReply(InputStream is) throws JedisException {
Object reply = process(is);
return (Integer) reply;
}
private Object process(InputStream is) throws JedisException {
try { try {
byte b = (byte) is.read(); byte b = (byte) is.read();
if (b == MINUS_BYTE) { if (b == MINUS_BYTE) {
processError(is); processError(is);
} else if (b == ASTERISK_BYTE) {
return processMultiBulkReply(is);
} else if (b == COLON_BYTE) {
return processInteger(is);
} else if (b == DOLLAR_BYTE) {
return processBulkReply(is);
} else {
return processStatusCodeReply(is);
}
} catch (IOException e) {
// TODO check what to do here
throw new JedisException(e.getMessage());
}
return null;
} }
if (b == DOLLAR_BYTE) { private Object processStatusCodeReply(InputStream is) {
String ret = null;
ret = readLine(is);
return ret;
}
private Object processBulkReply(InputStream is) throws IOException {
String ret = null;
int len = Integer.parseInt(readLine(is)); int len = Integer.parseInt(readLine(is));
if (len == -1) { if (len == -1) {
return null; return null;
@@ -83,69 +118,29 @@ public class Protocol {
is.read(); is.read();
ret = new String(read); ret = new String(read);
}
} catch (IOException e) {
// TODO Not sure that I should return null
return null;
}
return ret; return ret;
} }
public String getSingleLineReply(InputStream is) throws JedisException { private Object processInteger(InputStream is) {
String ret = null;
try {
byte b = (byte) is.read();
if (b == MINUS_BYTE) {
processError(is);
}
if (b == PLUS_BYTE) {
ret = readLine(is);
}
} catch (IOException e) {
// TODO Not sure that I should return null
return null;
}
return ret;
}
public int getIntegerReply(InputStream is) throws JedisException {
int ret = 0; int ret = 0;
try {
byte b = (byte) is.read();
if (b == MINUS_BYTE) {
processError(is);
}
if (b == COLON_BYTE) {
String num = readLine(is); String num = readLine(is);
ret = Integer.parseInt(num); ret = Integer.parseInt(num);
return ret;
} }
} catch (IOException e) {
// TODO Not sure that I should return 0 private Object processMultiBulkReply(InputStream is) throws JedisException {
e.printStackTrace(); List<Object> ret = new ArrayList<Object>();
return 0; int num = Integer.parseInt(readLine(is));
for (int i = 0; i < num; i++) {
ret.add(process(is));
} }
return ret; return ret;
} }
public List<String> getMultiBulkReply(InputStream is) throws JedisException { @SuppressWarnings("unchecked")
List<String> ret = new ArrayList<String>(); public List<Object> getMultiBulkReply(InputStream is) throws JedisException {
try { Object reply = process(is);
byte b = (byte) is.read(); List<Object> ret = (List<Object>) reply;
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));
}
}
} catch (IOException e) {
// TODO Not sure that I should return null
return null;
}
return ret; return ret;
} }
} }

View File

@@ -1,5 +1,6 @@
package redis.clients.jedis; package redis.clients.jedis;
import java.util.List;
import java.util.Map; import java.util.Map;
public class Transaction { public class Transaction {
@@ -418,7 +419,8 @@ public class Transaction {
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
public void exec() throws JedisException { public List<Object> exec() throws JedisException {
client.exec(); client.exec();
return client.getObjectMultiBulkReply();
} }
} }

View File

@@ -58,7 +58,7 @@ public class ProtocolTest extends Assert {
public void singleLineReply() throws JedisException { public void singleLineReply() throws JedisException {
InputStream is = new ByteArrayInputStream("+OK\r\n".getBytes()); InputStream is = new ByteArrayInputStream("+OK\r\n".getBytes());
Protocol protocol = new Protocol(); Protocol protocol = new Protocol();
String response = protocol.getSingleLineReply(is); String response = protocol.getStatusCodeReply(is);
assertEquals("OK", response); assertEquals("OK", response);
} }
@@ -70,13 +70,15 @@ public class ProtocolTest extends Assert {
assertEquals(123, response); assertEquals(123, response);
} }
@SuppressWarnings("unchecked")
@Test @Test
public void multiBulkReply() throws JedisException { public void multiBulkReply() throws JedisException {
InputStream is = new ByteArrayInputStream( InputStream is = new ByteArrayInputStream(
"*4\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$5\r\nHello\r\n$5\r\nWorld\r\n" "*4\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$5\r\nHello\r\n$5\r\nWorld\r\n"
.getBytes()); .getBytes());
Protocol protocol = new Protocol(); Protocol protocol = new Protocol();
List<String> response = protocol.getMultiBulkReply(is); List<String> response = (List<String>) (List<?>) protocol
.getMultiBulkReply(is);
List<String> expected = new ArrayList<String>(); List<String> expected = new ArrayList<String>();
expected.add("foo"); expected.add("foo");
expected.add("bar"); expected.add("bar");
@@ -84,5 +86,22 @@ public class ProtocolTest extends Assert {
expected.add("World"); expected.add("World");
assertEquals(expected, response); assertEquals(expected, response);
is = new ByteArrayInputStream(
"*4\r\n$3\r\nfoo\r\n+OK\r\n:1000\r\n*2\r\n$3\r\nfoo\r\n$3\r\nbar"
.getBytes());
protocol = new Protocol();
List<Object> response2 = protocol.getMultiBulkReply(is);
List<Object> expected2 = new ArrayList<Object>();
expected2.add("foo");
expected2.add("OK");
expected2.add(1000);
List<Object> sub = new ArrayList<Object>();
sub.add("foo");
sub.add("bar");
expected2.add(sub);
assertEquals(expected2, response2);
} }
} }

View File

@@ -1,5 +1,8 @@
package redis.clients.jedis.tests.commands; package redis.clients.jedis.tests.commands;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert; import junit.framework.Assert;
import org.junit.After; import org.junit.After;
@@ -39,14 +42,18 @@ public class TransactionCommandsTest extends Assert {
status = trans.scard("foo"); status = trans.scard("foo");
assertEquals("QUEUED", status); assertEquals("QUEUED", status);
trans.exec(); List<Object> response = trans.exec();
//TODO: check for exec response List<Object> expected = new ArrayList<Object>();
expected.add(1);
expected.add(1);
expected.add(2);
assertEquals(expected, response);
} }
@Test @Test
public void multiBlock() throws JedisException { public void multiBlock() throws JedisException {
jedis.multi(new TransactionBlock() { List<Object> response = jedis.multi(new TransactionBlock() {
public void execute() throws JedisException { public void execute() throws JedisException {
String status = sadd("foo", "a"); String status = sadd("foo", "a");
assertEquals("QUEUED", status); assertEquals("QUEUED", status);
@@ -59,6 +66,10 @@ public class TransactionCommandsTest extends Assert {
} }
}); });
//TODO: check what happens when throwind an exception List<Object> expected = new ArrayList<Object>();
expected.add(1);
expected.add(1);
expected.add(2);
assertEquals(expected, response);
} }
} }