package redis.clients.jedis; import redis.clients.jedis.exceptions.JedisDataException; import java.util.ArrayList; import java.util.List; public class Pipeline extends MultiKeyPipelineBase { private MultiResponseBuilder currentMulti; private class MultiResponseBuilder extends Builder>{ private List> responses = new ArrayList>(); @Override public List build(Object data) { @SuppressWarnings("unchecked") List list = (List)data; List values = new ArrayList(); if(list.size() != responses.size()){ throw new JedisDataException("Expected data size " + responses.size() + " but was " + list.size()); } for(int i=0;i 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 Response getResponse(Builder builder) { if(currentMulti != null){ super.getResponse(BuilderFactory.STRING); //Expected QUEUED Response lr = new Response(builder); currentMulti.addResponse(lr); return lr; } else{ return super.getResponse(builder); } } public void setClient(Client client) { this.client = client; } @Override protected Client getClient(byte[] key) { return client; } @Override protected Client getClient(String key) { return client; } public Object getOneWithJedisDataException() { try { return client.getOne(); } catch (JedisDataException e) { return e; } } /** * 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() { while (hasPipelinedResponse()) { generateResponse(getOneWithJedisDataException()); } } /** * 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. */ public List syncAndReturnAll() { List formatted = new ArrayList(); while (hasPipelinedResponse()) { try { formatted.add(generateResponse(getOneWithJedisDataException()).get()); } catch (JedisDataException e) { formatted.add(e); } } return formatted; } public Response discard() { client.discard(); currentMulti = null; return getResponse(BuilderFactory.STRING); } public Response> exec() { client.exec(); Response> response = super.getResponse(currentMulti); currentMulti = null; return response; } public Response multi() { client.multi(); Response response = getResponse(BuilderFactory.STRING); //Expecting OK currentMulti = new MultiResponseBuilder(); return response; } }