Implemented Client.getMany(int count) to remove multiple flush while getting responses at once

This commit is contained in:
Jungtaek Lim
2014-01-20 11:36:24 +09:00
parent 5bf29b43ee
commit 2267c3318c
3 changed files with 24 additions and 34 deletions

View File

@@ -211,4 +211,17 @@ public class Connection {
flush(); flush();
return Protocol.read(inputStream); return Protocol.read(inputStream);
} }
public List<Object> getMany(int count) {
flush();
List<Object> responses = new ArrayList<Object>();
for (int i = 0 ; i < count ; i++) {
try {
responses.add(Protocol.read(inputStream));
} catch (JedisDataException e) {
responses.add(e);
}
}
return responses;
}
} }

View File

@@ -63,23 +63,16 @@ public class Pipeline extends MultiKeyPipelineBase {
return client; return client;
} }
public Object getOneWithJedisDataException() {
try {
return client.getOne();
} catch (JedisDataException e) {
return e;
}
}
/** /**
* Syncronize pipeline by reading all responses. This operation close the * Syncronize pipeline by reading all responses. This operation close the
* pipeline. In order to get return values from pipelined commands, capture * pipeline. In order to get return values from pipelined commands, capture
* the different Response<?> of the commands you execute. * the different Response<?> of the commands you execute.
*/ */
public void sync() { public void sync() {
while (hasPipelinedResponse()) { List<Object> unformatted = client.getMany(getPipelinedResponseLength());
generateResponse(getOneWithJedisDataException());
} for (Object resp : unformatted)
generateResponse(resp);
} }
/** /**
@@ -91,15 +84,12 @@ public class Pipeline extends MultiKeyPipelineBase {
* @return A list of all the responses in the order you executed them. * @return A list of all the responses in the order you executed them.
*/ */
public List<Object> syncAndReturnAll() { public List<Object> syncAndReturnAll() {
List<Object> unformatted = client.getMany(getPipelinedResponseLength());
List<Object> formatted = new ArrayList<Object>(); List<Object> formatted = new ArrayList<Object>();
while (hasPipelinedResponse()) { for (Object resp : unformatted)
try { formatted.add(generateResponse(resp).get());
formatted.add(generateResponse(getOneWithJedisDataException()).get());
} catch (JedisDataException e) {
formatted.add(e);
}
}
return formatted; return formatted;
} }

View File

@@ -30,22 +30,9 @@ public class Transaction extends MultiKeyPipelineBase {
return client; return client;
} }
public Object getOneWithJedisDataException() {
try {
return client.getOne();
} catch (JedisDataException e) {
return e;
}
}
private void consumeResponse(int count) {
for (int i = 0 ; i < count ; i++)
getOneWithJedisDataException();
}
public List<Object> exec() { public List<Object> exec() {
// Discard QUEUED or ERROR // Discard QUEUED or ERROR
consumeResponse(getPipelinedResponseLength()); client.getMany(getPipelinedResponseLength());
client.exec(); client.exec();
@@ -66,7 +53,7 @@ public class Transaction extends MultiKeyPipelineBase {
public List<Response<?>> execGetResponse() { public List<Response<?>> execGetResponse() {
// Discard QUEUED or ERROR // Discard QUEUED or ERROR
consumeResponse(getPipelinedResponseLength()); client.getMany(getPipelinedResponseLength());
client.exec(); client.exec();
@@ -82,7 +69,7 @@ public class Transaction extends MultiKeyPipelineBase {
} }
public String discard() { public String discard() {
consumeResponse(getPipelinedResponseLength()); client.getMany(getPipelinedResponseLength());
client.discard(); client.discard();
inTransaction = false; inTransaction = false;
clean(); clean();