Exemplo n.º 1
0
  /**
   * A recursive method to construct a new <code>PivotFacetField</code> object from the contents of
   * the {@link NamedList}s provided by the specified shard, relative to a parent value (if this is
   * not the top field in the pivot hierarchy)
   *
   * <p>The associated child {@link PivotFacetValue}s will be recursively built as well.
   *
   * @see PivotFacetValue#createFromNamedList
   * @param shardNumber the id of the shard that provided this data
   * @param rb The response builder of the current request
   * @param owner the parent value in the current pivot (may be null)
   * @param pivotValues the data from the specified shard for this pivot field, may be null or empty
   * @return the new PivotFacetField, null if pivotValues is null or empty.
   */
  public static PivotFacetField createFromListOfNamedLists(
      int shardNumber,
      ResponseBuilder rb,
      PivotFacetValue owner,
      List<NamedList<Object>> pivotValues) {

    if (null == pivotValues || pivotValues.size() <= 0) return null;

    NamedList<Object> firstValue = pivotValues.get(0);
    PivotFacetField createdPivotFacetField =
        new PivotFacetField(rb, owner, PivotFacetHelper.getField(firstValue));

    int lowestCount = Integer.MAX_VALUE;

    for (NamedList<Object> pivotValue : pivotValues) {

      lowestCount = Math.min(lowestCount, PivotFacetHelper.getCount(pivotValue));

      PivotFacetValue newValue =
          PivotFacetValue.createFromNamedList(shardNumber, rb, createdPivotFacetField, pivotValue);
      createdPivotFacetField.valueCollection.add(newValue);
    }

    createdPivotFacetField.shardLowestCount.put(shardNumber, lowestCount);
    createdPivotFacetField.numberOfValuesContributedByShard.put(shardNumber, pivotValues.size());

    return createdPivotFacetField;
  }
Exemplo n.º 2
0
  /**
   * Merges in the count contributions from the specified shard for each. This method is recursive
   * if the shard data includes sub-pivots
   *
   * @see PivotFacetField#contributeFromShard
   * @see PivotFacetField#createFromListOfNamedLists
   */
  public void mergeContributionFromShard(
      int shardNumber, ResponseBuilder rb, NamedList<Object> value) {
    assert null != value : "can't merge in null data";

    if (!shardHasContributed(shardNumber)) {
      sourceShards.set(shardNumber);
      count += PivotFacetHelper.getCount(value);
      NamedList<NamedList<NamedList<?>>> stats = PivotFacetHelper.getStats(value);
      if (stats != null) {
        statsValues = PivotFacetHelper.mergeStats(statsValues, stats, rb._statsInfo);
      }
      NamedList<Number> shardQueryCounts = PivotFacetHelper.getQueryCounts(value);
      if (shardQueryCounts != null) {
        queryCounts = PivotFacetHelper.mergeQueryCounts(queryCounts, shardQueryCounts);
      }
      SimpleOrderedMap<SimpleOrderedMap<Object>> shardRanges = PivotFacetHelper.getRanges(value);
      if (shardRanges != null) {
        if (rangeCounts == null) {
          rangeCounts = new LinkedHashMap<>(shardRanges.size() / 2);
        }
        RangeFacetRequest.DistribRangeFacet.mergeFacetRangesFromShardResponse(
            rangeCounts, shardRanges);
      }
    }

    List<NamedList<Object>> shardChildPivots = PivotFacetHelper.getPivots(value);
    // sub pivot -- we may not have seen this yet depending on refinement
    if (null == childPivot) {
      childPivot =
          PivotFacetField.createFromListOfNamedLists(shardNumber, rb, this, shardChildPivots);
    } else {
      childPivot.contributeFromShard(shardNumber, rb, shardChildPivots);
    }
  }
Exemplo n.º 3
0
  private void contributeValueFromShard(
      int shardNumber, ResponseBuilder rb, NamedList<Object> shardValue) {

    incrementShardValueCount(shardNumber);

    Comparable value = PivotFacetHelper.getValue(shardValue);
    int count = PivotFacetHelper.getCount(shardValue);

    // We're changing values so we most mark the collection as dirty
    valueCollection.markDirty();

    if ((!shardLowestCount.containsKey(shardNumber)) || shardLowestCount.get(shardNumber) > count) {
      shardLowestCount.put(shardNumber, count);
    }

    PivotFacetValue facetValue = valueCollection.get(value);
    if (null == facetValue) {
      // never seen before, we need to create it from scratch
      facetValue = PivotFacetValue.createFromNamedList(shardNumber, rb, this, shardValue);
      this.valueCollection.add(facetValue);
    } else {
      facetValue.mergeContributionFromShard(shardNumber, rb, shardValue);
    }
  }
Exemplo n.º 4
0
  /**
   * A recursive method to construct a new <code>PivotFacetValue</code> object from the contents of
   * the {@link NamedList} provided by the specified shard, relative to the specified field.
   *
   * <p>If the <code>NamedList</code> contains data for a child {@link PivotFacetField} that will be
   * recursively built as well.
   *
   * @see PivotFacetField#createFromListOfNamedLists
   * @param shardNumber the id of the shard that provided this data
   * @param rb The response builder of the current request
   * @param parentField the parent field in the current pivot associated with this value
   * @param pivotData the data from the specified shard for this pivot value
   */
  @SuppressWarnings("unchecked")
  public static PivotFacetValue createFromNamedList(
      int shardNumber,
      ResponseBuilder rb,
      PivotFacetField parentField,
      NamedList<Object> pivotData) {

    Comparable pivotVal = null;
    int pivotCount = 0;
    List<NamedList<Object>> childPivotData = null;
    NamedList<NamedList<NamedList<?>>> statsValues = null;
    NamedList<Number> queryCounts = null;
    SimpleOrderedMap<SimpleOrderedMap<Object>> ranges = null;

    for (int i = 0; i < pivotData.size(); i++) {
      String key = pivotData.getName(i);
      Object value = pivotData.getVal(i);
      PivotListEntry entry = PivotListEntry.get(key);

      switch (entry) {
        case VALUE:
          pivotVal = (Comparable) value;
          break;
        case FIELD:
          assert parentField.field.equals(value)
              : "Parent Field mismatch: " + parentField.field + "!=" + value;
          break;
        case COUNT:
          pivotCount = (Integer) value;
          break;
        case PIVOT:
          childPivotData = (List<NamedList<Object>>) value;
          break;
        case STATS:
          statsValues = (NamedList<NamedList<NamedList<?>>>) value;
          break;
        case QUERIES:
          queryCounts = (NamedList<Number>) value;
          break;
        case RANGES:
          ranges = (SimpleOrderedMap<SimpleOrderedMap<Object>>) value;
          break;
        default:
          throw new RuntimeException("PivotListEntry contains unaccounted for item: " + entry);
      }
    }

    PivotFacetValue newPivotFacet = new PivotFacetValue(parentField, pivotVal);
    newPivotFacet.count = pivotCount;
    newPivotFacet.sourceShards.set(shardNumber);
    if (statsValues != null) {
      newPivotFacet.statsValues = PivotFacetHelper.mergeStats(null, statsValues, rb._statsInfo);
    }
    if (queryCounts != null) {
      newPivotFacet.queryCounts = PivotFacetHelper.mergeQueryCounts(null, queryCounts);
    }
    if (ranges != null) {
      newPivotFacet.rangeCounts = new LinkedHashMap<>();
      RangeFacetRequest.DistribRangeFacet.mergeFacetRangesFromShardResponse(
          newPivotFacet.rangeCounts, ranges);
    }

    newPivotFacet.childPivot =
        PivotFacetField.createFromListOfNamedLists(shardNumber, rb, newPivotFacet, childPivotData);

    return newPivotFacet;
  }