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

redis.clients.jedis.timeseries.TSInfo Maven / Gradle / Ivy

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import redis.clients.jedis.Builder;
import redis.clients.jedis.BuilderFactory;
import redis.clients.jedis.util.DoublePrecision;
import redis.clients.jedis.util.KeyValue;
import redis.clients.jedis.util.SafeEncoder;

public class TSInfo {

  private static final String DUPLICATE_POLICY_PROPERTY = "duplicatePolicy";
  private static final String LABELS_PROPERTY = "labels";
  private static final String RULES_PROPERTY = "rules";
  private static final String CHUNKS_PROPERTY = "Chunks";
  private static final String CHUNKS_BYTES_PER_SAMPLE_PROPERTY = "bytesPerSample";

  private final Map properties;
  private final Map labels;
  private final Map rules;
  private final List> chunks;

  private TSInfo(Map properties, Map labels, Map rules, List> chunks) {
    this.properties = properties;
    this.labels = labels;
    this.rules = rules;
    this.chunks = chunks;
  }

  public Map getProperties() {
    return properties;
  }

  public Object getProperty(String property) {
    return properties.get(property);
  }

  public Long getIntegerProperty(String property) {
    return (Long) properties.get(property);
  }

  public Map getLabels() {
    return labels;
  }

  public String getLabel(String label) {
    return labels.get(label);
  }

  public Map getRules() {
    return rules;
  }

  public Rule getRule(String rule) {
    return rules.get(rule);
  }

  public List> getChunks() {
    return chunks;
  }

  public static Builder TIMESERIES_INFO = new Builder() {
    @Override
    public TSInfo build(Object data) {
      List list = (List) data;
      Map properties = new HashMap<>();
      Map labels = null;
      Map rules = null;
      List> chunks = null;

      for (int i = 0; i < list.size(); i += 2) {
        String prop = SafeEncoder.encode((byte[]) list.get(i));
        Object value = list.get(i + 1);
        if (value instanceof List) {
          switch (prop) {
            case LABELS_PROPERTY:
              labels = BuilderFactory.STRING_MAP_FROM_PAIRS.build(value);
              value = labels;
              break;
            case RULES_PROPERTY:
              List rulesDataList = (List) value;
              List> rulesValueList = new ArrayList<>(rulesDataList.size());
              rules = new HashMap<>(rulesDataList.size());
              for (Object ruleData : rulesDataList) {
                List encodedRule = (List) SafeEncoder.encodeObject(ruleData);
                rulesValueList.add(encodedRule);
                rules.put((String) encodedRule.get(0), new Rule((String) encodedRule.get(0), (Long) encodedRule.get(1),
                    AggregationType.safeValueOf((String) encodedRule.get(2)), (Long) encodedRule.get(3)));
              }
              value = rulesValueList;
              break;
            case CHUNKS_PROPERTY:
              List chunksDataList = (List) value;
              List> chunksValueList = new ArrayList<>(chunksDataList.size());
              chunks = new ArrayList<>(chunksDataList.size());
              for (Object chunkData : chunksDataList) {
                Map chunk = BuilderFactory.ENCODED_OBJECT_MAP.build(chunkData);
                chunksValueList.add(new HashMap<>(chunk));
                if (chunk.containsKey(CHUNKS_BYTES_PER_SAMPLE_PROPERTY)) {
                  chunk.put(CHUNKS_BYTES_PER_SAMPLE_PROPERTY,
                      DoublePrecision.parseEncodedFloatingPointNumber(chunk.get(CHUNKS_BYTES_PER_SAMPLE_PROPERTY)));
                }
                chunks.add(chunk);
              }
              value = chunksValueList;
              break;
            default:
              value = SafeEncoder.encodeObject(value);
              break;
          }
        } else if (value instanceof byte[]) {
          value = SafeEncoder.encode((byte[]) value);
          if (DUPLICATE_POLICY_PROPERTY.equals(prop)) {
            try {
              value = DuplicatePolicy.valueOf(((String) value).toUpperCase());
            } catch (Exception e) { }
          }
        }
        properties.put(prop, value);
      }

      return new TSInfo(properties, labels, rules, chunks);
    }
  };

  public static Builder TIMESERIES_INFO_RESP3 = new Builder() {
    @Override
    public TSInfo build(Object data) {
      List list = (List) data;
      Map properties = new HashMap<>();
      Map labels = null;
      Map rules = null;
      List> chunks = null;

      for (KeyValue propertyValue : list) {
        String prop = BuilderFactory.STRING.build(propertyValue.getKey());
        Object value = propertyValue.getValue();
        if (value instanceof List) {
          switch (prop) {
            case LABELS_PROPERTY:
              labels = BuilderFactory.STRING_MAP.build(value);
              value = labels;
              break;
            case RULES_PROPERTY:
              List rulesDataList = (List) value;
              Map> rulesValueMap = new HashMap<>(rulesDataList.size(), 1f);
              rules = new HashMap<>(rulesDataList.size());
              for (KeyValue rkv : rulesDataList) {
                String ruleName = BuilderFactory.STRING.build(rkv.getKey());
                List ruleValueList = BuilderFactory.ENCODED_OBJECT_LIST.build(rkv.getValue());
                rulesValueMap.put(ruleName, ruleValueList);
                rules.put(ruleName, new Rule(ruleName, ruleValueList));
              }
              value = rulesValueMap;
              break;
            case CHUNKS_PROPERTY:
              List> chunksDataList = (List>) value;
              List> chunksValueList = new ArrayList<>(chunksDataList.size());
              chunks = new ArrayList<>(chunksDataList.size());
              for (List chunkDataAsList : chunksDataList) {
                Map chunk = chunkDataAsList.stream()
                    .collect(Collectors.toMap(kv -> BuilderFactory.STRING.build(kv.getKey()),
                        kv -> BuilderFactory.ENCODED_OBJECT.build(kv.getValue())));
                chunksValueList.add(chunk);
                chunks.add(chunk);
              }
              value = chunksValueList;
              break;
            default:
              value = SafeEncoder.encodeObject(value);
              break;
          }
        } else if (value instanceof byte[]) {
          value = BuilderFactory.STRING.build(value);
          if (DUPLICATE_POLICY_PROPERTY.equals(prop)) {
            try {
              value = DuplicatePolicy.valueOf(((String) value).toUpperCase());
            } catch (Exception e) { }
          }
        }
        properties.put(prop, value);
      }

      return new TSInfo(properties, labels, rules, chunks);
    }
  };

  public static class Rule {

    private final String compactionKey;
    private final long bucketDuration;
    private final AggregationType aggregator;
    private final long alignmentTimestamp;

    private Rule(String compaction, List encodedValues) {
      this(compaction, (Long) encodedValues.get(0),
          AggregationType.safeValueOf((String) encodedValues.get(1)),
          (Long) encodedValues.get(2));
    }

    private Rule(String compaction, long bucket, AggregationType aggregation, long alignment) {
      this.compactionKey = compaction;
      this.bucketDuration = bucket;
      this.aggregator = aggregation;
      this.alignmentTimestamp = alignment;
    }

    public String getCompactionKey() {
      return compactionKey;
    }

    public long getBucketDuration() {
      return bucketDuration;
    }

    public AggregationType getAggregator() {
      return aggregator;
    }

    public long getAlignmentTimestamp() {
      return alignmentTimestamp;
    }
  }
}