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

redis.clients.jedis.resps.FunctionStats Maven / Gradle / Ivy

The newest version!
package redis.clients.jedis.resps;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import redis.clients.jedis.Builder;
import redis.clients.jedis.BuilderFactory;
import redis.clients.jedis.util.KeyValue;

public class FunctionStats {

  private final Map runningScript;
  private final Map> engines;

  public FunctionStats(Map script, Map> engines) {
    this.runningScript = script;
    this.engines = engines;
  }

  public Map getRunningScript() {
    return runningScript;
  }

  public Map> getEngines() {
    return engines;
  }

  public static final Builder FUNCTION_STATS_BUILDER = new Builder() {

    @Override
    public FunctionStats build(Object data) {
      if (data == null) return null;
      List list = (List) data;
      if (list.isEmpty()) return null;

      if (list.get(0) instanceof KeyValue) {

        Map runningScriptMap = null;
        Map> enginesMap = null;

        for (KeyValue kv : (List) list) {
          switch (BuilderFactory.STRING.build(kv.getKey())) {
            case "running_script":
              runningScriptMap = BuilderFactory.ENCODED_OBJECT_MAP.build(kv.getValue());
              break;
            case "engines":
              List ilist = (List) kv.getValue();
              enginesMap = new LinkedHashMap<>(ilist.size());
              for (KeyValue ikv : (List) kv.getValue()) {
                enginesMap.put(BuilderFactory.STRING.build(ikv.getKey()),
                    BuilderFactory.ENCODED_OBJECT_MAP.build(ikv.getValue()));
              }
              break;
          }
        }

        return new FunctionStats(runningScriptMap, enginesMap);
      }

      Map runningScriptMap = list.get(1) == null ? null
          : BuilderFactory.ENCODED_OBJECT_MAP.build(list.get(1));

      List enginesList = (List) list.get(3);

      Map> enginesMap = new LinkedHashMap<>(enginesList.size() / 2);
      for (int i = 0; i < enginesList.size(); i += 2) {
        enginesMap.put(BuilderFactory.STRING.build(enginesList.get(i)),
            BuilderFactory.ENCODED_OBJECT_MAP.build(enginesList.get(i + 1)));
      }

      return new FunctionStats(runningScriptMap, enginesMap);
    }
  };

}