Added pipeline support

This commit is contained in:
Jonathan Leibiusky
2010-08-05 21:45:21 -03:00
parent 01da80627d
commit 5679597495
7 changed files with 121 additions and 36 deletions

View File

@@ -6,6 +6,7 @@ import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class Connection {
@@ -16,6 +17,7 @@ public class Connection {
private Protocol protocol = new Protocol();
private DataOutputStream outputStream;
private DataInputStream inputStream;
private int pipelinedCommands = 0;
public Connection(String host) {
super();
@@ -27,6 +29,7 @@ public class Connection {
throw new JedisException("Please connect Jedis before using it.");
}
protocol.sendCommand(outputStream, name, args);
pipelinedCommands++;
return this;
}
@@ -58,7 +61,6 @@ public class Connection {
public void connect() throws UnknownHostException, IOException {
if (!connected) {
socket = new Socket(host, port);
socket.setReceiveBufferSize(256);
connected = socket.isConnected();
outputStream = new DataOutputStream(socket.getOutputStream());
inputStream = new DataInputStream(new BufferedInputStream(socket
@@ -82,24 +84,38 @@ public class Connection {
}
protected String getStatusCodeReply() {
return protocol.getStatusCodeReply(inputStream);
pipelinedCommands--;
return (String) protocol.read(inputStream);
}
public String getBulkReply() {
return protocol.getBulkReply(inputStream);
pipelinedCommands--;
return (String) protocol.read(inputStream);
}
public int getIntegerReply() {
return protocol.getIntegerReply(inputStream);
pipelinedCommands--;
return (Integer) protocol.read(inputStream);
}
@SuppressWarnings("unchecked")
public List<String> getMultiBulkReply() {
return (List<String>) (List<?>) protocol.getMultiBulkReply(inputStream);
pipelinedCommands--;
return (List<String>) protocol.read(inputStream);
}
@SuppressWarnings("unchecked")
public List<Object> getObjectMultiBulkReply() {
return protocol.getMultiBulkReply(inputStream);
pipelinedCommands--;
return (List<Object>) protocol.read(inputStream);
}
public List<Object> getAll() {
List<Object> all = new ArrayList<Object>();
while (pipelinedCommands > 0) {
all.add(protocol.read(inputStream));
pipelinedCommands--;
}
return all;
}
}

View File

@@ -527,4 +527,10 @@ public class Jedis {
client.auth(password);
return client.getStatusCodeReply();
}
public List<Object> pipelined(JedisPipeline jedisPipeline) {
jedisPipeline.setClient(client);
jedisPipeline.execute();
return client.getAll();
}
}

View File

@@ -0,0 +1,11 @@
package redis.clients.jedis;
public abstract class JedisPipeline {
protected Client client;
public void setClient(Client client) {
this.client = client;
}
public abstract void execute();
}

View File

@@ -65,21 +65,6 @@ public class Protocol {
return sb.toString();
}
public String getBulkReply(DataInputStream is) {
Object reply = process(is);
return (String) reply;
}
public String getStatusCodeReply(DataInputStream is) {
Object reply = process(is);
return (String) reply;
}
public int getIntegerReply(DataInputStream is) {
Object reply = process(is);
return (Integer) reply;
}
private Object process(DataInputStream is) {
try {
byte b = is.readByte();
@@ -94,7 +79,7 @@ public class Protocol {
} else if (b == PLUS_BYTE) {
return processStatusCodeReply(is);
} else {
throw new JedisException("Unknown reply");
throw new JedisException("Unknown reply: " + (char) b);
}
} catch (IOException e) {
throw new JedisException(e);
@@ -145,10 +130,7 @@ public class Protocol {
return ret;
}
@SuppressWarnings("unchecked")
public List<Object> getMultiBulkReply(DataInputStream is) {
Object reply = process(is);
List<Object> ret = (List<Object>) reply;
return ret;
public Object read(DataInputStream is) {
return process(is);
}
}