Refactored to support object multi bulk reply
This commit is contained in:
@@ -375,5 +375,4 @@ public class Client extends Connection {
|
||||
public void exec() throws JedisException {
|
||||
sendCommand("EXEC");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -80,7 +80,7 @@ public class Connection {
|
||||
}
|
||||
|
||||
protected String getStatusCodeReply() throws JedisException {
|
||||
return protocol.getSingleLineReply(inputStream);
|
||||
return protocol.getStatusCodeReply(inputStream);
|
||||
}
|
||||
|
||||
public String getBulkReply() throws JedisException {
|
||||
@@ -91,7 +91,13 @@ public class Connection {
|
||||
return protocol.getIntegerReply(inputStream);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<String> getMultiBulkReply() throws JedisException {
|
||||
return (List<String>) (List<?>) protocol.getMultiBulkReply(inputStream);
|
||||
}
|
||||
|
||||
public List<Object> getObjectMultiBulkReply() throws JedisException {
|
||||
return protocol.getMultiBulkReply(inputStream);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -466,15 +466,16 @@ public class Jedis {
|
||||
return new Transaction(client);
|
||||
}
|
||||
|
||||
public void multi(TransactionBlock jedisTransaction) throws JedisException {
|
||||
public List<Object> multi(TransactionBlock jedisTransaction)
|
||||
throws JedisException {
|
||||
try {
|
||||
jedisTransaction.setClient(client);
|
||||
client.multi();
|
||||
client.getStatusCodeReply();
|
||||
multi();
|
||||
jedisTransaction.execute();
|
||||
} catch (Exception ex) {
|
||||
client.discard();
|
||||
}
|
||||
return jedisTransaction.exec();
|
||||
}
|
||||
|
||||
public void connect() throws UnknownHostException, IOException {
|
||||
|
||||
@@ -64,88 +64,83 @@ public class Protocol {
|
||||
}
|
||||
|
||||
public String getBulkReply(InputStream is) throws JedisException {
|
||||
String ret = null;
|
||||
try {
|
||||
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;
|
||||
}
|
||||
byte[] read = new byte[len];
|
||||
is.read(read);
|
||||
// read 2 more bytes for the command delimiter
|
||||
is.read();
|
||||
is.read();
|
||||
|
||||
ret = new String(read);
|
||||
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Not sure that I should return null
|
||||
return null;
|
||||
}
|
||||
return ret;
|
||||
Object reply = process(is);
|
||||
return (String) reply;
|
||||
}
|
||||
|
||||
public String getSingleLineReply(InputStream is) throws JedisException {
|
||||
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 String getStatusCodeReply(InputStream is) throws JedisException {
|
||||
Object reply = process(is);
|
||||
return (String) reply;
|
||||
}
|
||||
|
||||
public int getIntegerReply(InputStream is) throws JedisException {
|
||||
int ret = 0;
|
||||
Object reply = process(is);
|
||||
return (Integer) reply;
|
||||
}
|
||||
|
||||
private Object process(InputStream is) throws JedisException {
|
||||
try {
|
||||
byte b = (byte) is.read();
|
||||
if (b == MINUS_BYTE) {
|
||||
processError(is);
|
||||
}
|
||||
if (b == COLON_BYTE) {
|
||||
String num = readLine(is);
|
||||
ret = Integer.parseInt(num);
|
||||
} 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 Not sure that I should return 0
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
// TODO check what to do here
|
||||
throw new JedisException(e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
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));
|
||||
if (len == -1) {
|
||||
return null;
|
||||
}
|
||||
byte[] read = new byte[len];
|
||||
is.read(read);
|
||||
// read 2 more bytes for the command delimiter
|
||||
is.read();
|
||||
is.read();
|
||||
|
||||
ret = new String(read);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private Object processInteger(InputStream is) {
|
||||
int ret = 0;
|
||||
String num = readLine(is);
|
||||
ret = Integer.parseInt(num);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private Object processMultiBulkReply(InputStream is) throws JedisException {
|
||||
List<Object> ret = new ArrayList<Object>();
|
||||
int num = Integer.parseInt(readLine(is));
|
||||
for (int i = 0; i < num; i++) {
|
||||
ret.add(process(is));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public List<String> getMultiBulkReply(InputStream is) throws JedisException {
|
||||
List<String> ret = new ArrayList<String>();
|
||||
try {
|
||||
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));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Not sure that I should return null
|
||||
return null;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Object> getMultiBulkReply(InputStream is) throws JedisException {
|
||||
Object reply = process(is);
|
||||
List<Object> ret = (List<Object>) reply;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Transaction {
|
||||
@@ -418,7 +419,8 @@ public class Transaction {
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public void exec() throws JedisException {
|
||||
public List<Object> exec() throws JedisException {
|
||||
client.exec();
|
||||
return client.getObjectMultiBulkReply();
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,7 @@ public class ProtocolTest extends Assert {
|
||||
public void singleLineReply() throws JedisException {
|
||||
InputStream is = new ByteArrayInputStream("+OK\r\n".getBytes());
|
||||
Protocol protocol = new Protocol();
|
||||
String response = protocol.getSingleLineReply(is);
|
||||
String response = protocol.getStatusCodeReply(is);
|
||||
assertEquals("OK", response);
|
||||
}
|
||||
|
||||
@@ -70,13 +70,15 @@ public class ProtocolTest extends Assert {
|
||||
assertEquals(123, response);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void multiBulkReply() throws JedisException {
|
||||
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"
|
||||
.getBytes());
|
||||
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>();
|
||||
expected.add("foo");
|
||||
expected.add("bar");
|
||||
@@ -84,5 +86,22 @@ public class ProtocolTest extends Assert {
|
||||
expected.add("World");
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
package redis.clients.jedis.tests.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.After;
|
||||
@@ -39,14 +42,18 @@ public class TransactionCommandsTest extends Assert {
|
||||
status = trans.scard("foo");
|
||||
assertEquals("QUEUED", status);
|
||||
|
||||
trans.exec();
|
||||
|
||||
//TODO: check for exec response
|
||||
List<Object> response = trans.exec();
|
||||
|
||||
List<Object> expected = new ArrayList<Object>();
|
||||
expected.add(1);
|
||||
expected.add(1);
|
||||
expected.add(2);
|
||||
assertEquals(expected, response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiBlock() throws JedisException {
|
||||
jedis.multi(new TransactionBlock() {
|
||||
List<Object> response = jedis.multi(new TransactionBlock() {
|
||||
public void execute() throws JedisException {
|
||||
String status = sadd("foo", "a");
|
||||
assertEquals("QUEUED", status);
|
||||
@@ -58,7 +65,11 @@ public class TransactionCommandsTest extends Assert {
|
||||
assertEquals("QUEUED", status);
|
||||
}
|
||||
});
|
||||
|
||||
//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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user