avoid creating pipeline responses and do it lazy

This commit is contained in:
Jonathan Leibiusky
2011-05-30 13:43:21 -03:00
parent 44c3eef60e
commit d690833ed6
5 changed files with 51 additions and 16 deletions

View File

@@ -2015,10 +2015,19 @@ public class BinaryJedis implements BinaryJedisCommands {
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
/**
* Starts a pipeline, which is a very efficient way to send lots of command
* and read all the responses when you finish sending them. Try to avoid
* this version and use pipelined() when possible as it will give better
* performance.
*
* @param jedisPipeline
* @return The results of the command in the same order you've run them.
*/
public List<Object> pipelined(final PipelineBlock jedisPipeline) { public List<Object> pipelined(final PipelineBlock jedisPipeline) {
jedisPipeline.setClient(client); jedisPipeline.setClient(client);
jedisPipeline.execute(); jedisPipeline.execute();
return jedisPipeline.sync(); return jedisPipeline.syncAndReturnAll();
} }
public Pipeline pipelined() { public Pipeline pipelined() {

View File

@@ -14,7 +14,28 @@ public class Pipeline extends Queable {
this.client = client; this.client = client;
} }
public List<Object> sync() { /**
* Syncronize pipeline by reading all responses. This operation close the
* pipeline. In order to get return values from pipelined commands, capture
* the different Response<?> of the commands you execute.
*/
public void sync() {
List<Object> unformatted = client.getAll();
for (Object o : unformatted) {
generateResponse(o);
}
}
/**
* Syncronize pipeline by reading all responses. This operation close the
* pipeline. Whenever possible try to avoid using this version and use
* Pipeline.sync() as it won't go through all the responses and generate the
* right response type (usually it is a waste of time).
*
* @return A list of all the responses in the order you executed them.
* @see sync
*/
public List<Object> syncAndReturnAll() {
List<Object> unformatted = client.getAll(); List<Object> unformatted = client.getAll();
List<Object> formatted = new ArrayList<Object>(); List<Object> formatted = new ArrayList<Object>();
for (Object o : unformatted) { for (Object o : unformatted) {

View File

@@ -5,22 +5,29 @@ import redis.clients.jedis.exceptions.JedisDataException;
public class Response<T> { public class Response<T> {
protected T response = null; protected T response = null;
private boolean built = false; private boolean built = false;
private boolean set = false;
private Builder<T> builder; private Builder<T> builder;
private Object data;
public Response(Builder<T> b) { public Response(Builder<T> b) {
this.builder = b; this.builder = b;
} }
public void set(Object data) { public void set(Object data) {
response = builder.build(data); this.data = data;
built = true; set = true;
} }
public T get() { public T get() {
if (!built) { if (!set) {
throw new JedisDataException( throw new JedisDataException(
"Please close pipeline or multi block before calling this method."); "Please close pipeline or multi block before calling this method.");
} }
if (!built) {
response = builder.build(data);
this.data = null;
built = true;
}
return response; return response;
} }

View File

@@ -46,7 +46,7 @@ public class PipeliningTest extends Assert {
Pipeline p = jedis.pipelined(); Pipeline p = jedis.pipelined();
p.set("foo", "bar"); p.set("foo", "bar");
p.get("foo"); p.get("foo");
results = p.sync(); results = p.syncAndReturnAll();
assertEquals(2, results.size()); assertEquals(2, results.size());
assertEquals("OK", results.get(0)); assertEquals("OK", results.get(0));

View File

@@ -5,7 +5,7 @@ import java.net.UnknownHostException;
import java.util.Calendar; import java.util.Calendar;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.PipelineBlock; import redis.clients.jedis.Pipeline;
import redis.clients.jedis.tests.HostAndPortUtil; import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort; import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
@@ -22,15 +22,13 @@ public class PipelinedGetSetBenchmark {
long begin = Calendar.getInstance().getTimeInMillis(); long begin = Calendar.getInstance().getTimeInMillis();
jedis.pipelined(new PipelineBlock() { Pipeline p = jedis.pipelined();
public void execute() { for (int n = 0; n <= TOTAL_OPERATIONS; n++) {
for (int n = 0; n <= TOTAL_OPERATIONS; n++) { String key = "foo" + n;
String key = "foo" + n; p.set(key, "bar" + n);
set(key, "bar" + n); p.get(key);
get(key); }
} p.sync();
}
});
long elapsed = Calendar.getInstance().getTimeInMillis() - begin; long elapsed = Calendar.getInstance().getTimeInMillis() - begin;