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

redis.clients.jedis.search.aggr.AggregationResult Maven / Gradle / Ivy

There is a newer version: 5.2.0
Show newest version
package redis.clients.jedis.search.aggr;

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.exceptions.JedisDataException;
import redis.clients.jedis.util.SafeEncoder;

public class AggregationResult {

  /**
   * @deprecated Use {@link AggregationResult#getTotalResults()}.
   */
  @Deprecated
  public final long totalResults;

  private final List> results;

  private long cursorId = -1;

  @Deprecated
  public AggregationResult(Object resp, long cursorId) {
    this(resp);
    this.cursorId = cursorId;
  }

  @Deprecated
  public AggregationResult(Object resp) {
    List list = (List) SafeEncoder.encodeObject(resp);

    // the first element is always the number of results
    totalResults = (Long) list.get(0);
    results = new ArrayList<>(list.size() - 1);

    for (int i = 1; i < list.size(); i++) {
      List mapList = (List) list.get(i);
      Map map = new HashMap<>(mapList.size() / 2, 1f);
      for (int j = 0; j < mapList.size(); j += 2) {
        Object r = mapList.get(j);
        if (r instanceof JedisDataException) {
          throw (JedisDataException) r;
        }
        map.put((String) r, mapList.get(j + 1));
      }
      results.add(map);
    }
  }

  public long getTotalResults() {
    return totalResults;
  }

  public List> getResults() {
    return results;
  }

  /**
   * @return results as {@link Row}s.
   * @see #getResults()
   */
  public List getRows() {
    return results.stream().map(Row::new).collect(Collectors.toList());
  }

  public Row getRow(int index) {
    return new Row(results.get(index));
  }

  public long getCursorId() {
    return cursorId;
  }
}