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 {
|
public void exec() throws JedisException {
|
||||||
sendCommand("EXEC");
|
sendCommand("EXEC");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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 {
|
||||||
|
|||||||
@@ -64,88 +64,83 @@ public class Protocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getBulkReply(InputStream is) throws JedisException {
|
public String getBulkReply(InputStream is) throws JedisException {
|
||||||
String ret = null;
|
Object reply = process(is);
|
||||||
try {
|
return (String) reply;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSingleLineReply(InputStream is) throws JedisException {
|
public String getStatusCodeReply(InputStream is) throws JedisException {
|
||||||
String ret = null;
|
Object reply = process(is);
|
||||||
try {
|
return (String) reply;
|
||||||
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 {
|
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 {
|
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) {
|
||||||
if (b == COLON_BYTE) {
|
return processMultiBulkReply(is);
|
||||||
String num = readLine(is);
|
} else if (b == COLON_BYTE) {
|
||||||
ret = Integer.parseInt(num);
|
return processInteger(is);
|
||||||
|
} else if (b == DOLLAR_BYTE) {
|
||||||
|
return processBulkReply(is);
|
||||||
|
} else {
|
||||||
|
return processStatusCodeReply(is);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// TODO Not sure that I should return 0
|
// TODO check what to do here
|
||||||
e.printStackTrace();
|
throw new JedisException(e.getMessage());
|
||||||
return 0;
|
}
|
||||||
|
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;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
@@ -58,7 +65,11 @@ public class TransactionCommandsTest extends Assert {
|
|||||||
assertEquals("QUEUED", status);
|
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