All Downloads are FREE. Search and download functionalities are using the official Maven repository.

redis.clients.jedis.Pipeline Maven / Gradle / Ivy

package redis.clients.jedis;

import java.io.Closeable;
import java.util.ArrayList;
import java.util.List;

import redis.clients.jedis.exceptions.JedisDataException;

public class Pipeline extends MultiKeyPipelineBase implements Closeable {

  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 < list.size(); i++) {
        Response response = responses.get(i);
        response.set(list.get(i));
        Object builtResponse;
        try {
          builtResponse = response.get();
        } catch (JedisDataException e) {
          builtResponse = e;
        }
        values.add(builtResponse);
      }
      return values;
    }

    public void setResponseDependency(Response dependency) {
      for (Response response : responses) {
        response.setDependency(dependency);
      }
    }

    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 void clear() {
    if (isInMulti()) {
      discard();
    }

    sync();
  }

  public boolean isInMulti() {
    return currentMulti != null;
  }

  /**
   * Synchronize 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() {
    if (getPipelinedResponseLength() > 0) {
      List unformatted = client.getMany(getPipelinedResponseLength());
      for (Object o : unformatted) {
        generateResponse(o);
      }
    }
  }

  /**
   * Synchronize 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() {
    if (getPipelinedResponseLength() > 0) {
      List unformatted = client.getMany(getPipelinedResponseLength());
      List formatted = new ArrayList<>();
      for (Object o : unformatted) {
        try {
          formatted.add(generateResponse(o).get());
        } catch (JedisDataException e) {
          formatted.add(e);
        }
      }
      return formatted;
    } else {
      return java.util.Collections. emptyList();
    }
  }

  /**
   * @deprecated This method will be removed in next major release.
   */
  @Deprecated
  public Response discard() {
    if (currentMulti == null) throw new JedisDataException("DISCARD without MULTI");
    client.discard();
    currentMulti = null;
    return getResponse(BuilderFactory.STRING);
  }

  /**
   * @deprecated This method will be removed in next major release.
   */
  @Deprecated
  public Response> exec() {
    if (currentMulti == null) throw new JedisDataException("EXEC without MULTI");

    client.exec();
    Response> response = super.getResponse(currentMulti);
    currentMulti.setResponseDependency(response);
    currentMulti = null;
    return response;
  }

  /**
   * @deprecated This method will be removed in next major release.
   */
  @Deprecated
  public Response multi() {
    if (currentMulti != null) throw new JedisDataException("MULTI calls can not be nested");

    client.multi();
    Response response = getResponse(BuilderFactory.STRING); // Expecting
    // OK
    currentMulti = new MultiResponseBuilder();
    return response;
  }

  @Override
  public void close() {
    clear();
  }

  public Response waitReplicas(int replicas, long timeout) {
    client.waitReplicas(replicas, timeout);
    return getResponse(BuilderFactory.LONG);
  }

}