Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Multi is fixed in Pipeline. Issue #251 is fixed #288

Merged
merged 3 commits into from

2 participants

ivowiblo Jonathan Leibiusky
ivowiblo
Collaborator

Now the Response<?> instances return the expected values when executing commands inside a Multi

Jonathan Leibiusky xetorthio merged commit d526a32 into from
Jonathan Leibiusky
Owner

Merged! Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
57 src/main/java/redis/clients/jedis/Pipeline.java
View
@@ -9,8 +9,51 @@
import redis.clients.jedis.exceptions.JedisDataException;
public class Pipeline extends Queable {
+
+ private MultiResponseBuilder currentMulti;
+
+ private class MultiResponseBuilder extends Builder<List<Object>>{
+ private List<Response<?>> responses = new ArrayList<Response<?>>();
+
+ @Override
+ public List<Object> build(Object data) {
+ @SuppressWarnings("unchecked")
+ List<Object> list = (List<Object>)data;
+ List<Object> values = new ArrayList<Object>();
+
+ if(list.size() != responses.size()){
+ throw new JedisDataException("Expected data size " + responses.size() + " but was " + list.size());
+ }
+
+ for(int i=0;i<list.size();i++){
+ Response<?> response = responses.get(i);
+ response.set(list.get(i));
+ values.add(response.get());
+ }
+ return values;
+ }
+
+ public void addResponse(Response<?> response){
+ responses.add(response);
+ }
+ }
+
+ @Override
+ protected <T> Response<T> getResponse(Builder<T> builder) {
+ if(currentMulti != null){
+ super.getResponse(BuilderFactory.STRING); //Expected QUEUED
+
+ Response<T> lr = new Response<T>(builder);
+ currentMulti.addResponse(lr);
+ return lr;
+ }
+ else{
+ return super.getResponse(builder);
+ }
+ }
+
private Client client;
-
+
public void setClient(Client client) {
this.client = client;
}
@@ -39,9 +82,10 @@ public void sync() {
public List<Object> syncAndReturnAll() {
List<Object> unformatted = client.getAll();
List<Object> formatted = new ArrayList<Object>();
+
for (Object o : unformatted) {
try {
- formatted.add(generateResponse(o).get());
+ formatted.add(generateResponse(o).get());
} catch (JedisDataException e) {
formatted.add(e);
}
@@ -1174,12 +1218,17 @@ public void sync() {
return getResponse(BuilderFactory.STRING);
}
- public void exec() {
+ public Response<List<Object>> exec() {
client.exec();
+ Response<List<Object>> response = super.getResponse(currentMulti);
+ currentMulti = null;
+ return response;
}
public void multi() {
client.multi();
+ getResponse(BuilderFactory.STRING); //Expecting OK
+ currentMulti = new MultiResponseBuilder();
}
public Response<Long> publish(String channel, String message) {
@@ -1230,5 +1279,5 @@ public void multi() {
public Response<String> select(int index){
client.select(index);
return getResponse(BuilderFactory.STRING);
- }
+ }
}
29 src/test/java/redis/clients/jedis/tests/PipeliningTest.java
View
@@ -164,4 +164,33 @@ public void piplineWithError(){
}
assertEquals(r.get(), "bar");
}
+
+ @Test
+ public void multi(){
+ Pipeline p = jedis.pipelined();
+ p.multi();
+ Response<Long> r1 = p.hincrBy("a", "f1", -1);
+ Response<Long> r2 = p.hincrBy("a", "f1", -2);
+ Response<List<Object>> r3 = p.exec();
+ List<Object> result = p.syncAndReturnAll();
+
+ assertEquals(new Long(-1), r1.get());
+ assertEquals(new Long(-3), r2.get());
+
+ assertEquals(4, result.size());
+
+ assertEquals("OK", result.get(0));
+ assertEquals("QUEUED", result.get(1));
+ assertEquals("QUEUED", result.get(2));
+
+ //4th result is a list with the results from the multi
+ @SuppressWarnings("unchecked")
+ List<Object> multiResult = (List<Object>) result.get(3);
+ assertEquals(new Long(-1), multiResult.get(0));
+ assertEquals(new Long(-3), multiResult.get(1));
+
+ assertEquals(new Long(-1), r3.get().get(0));
+ assertEquals(new Long(-3), r3.get().get(1));
+
+ }
}
Something went wrong with that request. Please try again.