private static JsonObject processSamplingQuery()

in src/main/java/org/apache/datasketches/server/DataQueryHandler.java [324:375]


  private static JsonObject processSamplingQuery(final JsonObject result, final JsonObject query, final Family type, final Object sketch) {
    if (query == null || type == null || sketch == null) {
      return null;
    }

    // check if we need a summary
    final boolean addSummary = checkSummaryFlag(query);
    String summary = null;

    final long streamWeight;
    final int k;
    final JsonArray itemArray = new JsonArray();

    switch (type) {
      case RESERVOIR:
        final ReservoirItemsSketch<String> ris = (ReservoirItemsSketch<String>) sketch;
        for (final String item : ris.getSamples()) {
          itemArray.add(item);
        }
        streamWeight = ris.getN();
        k = ris.getK();
        if (addSummary)
          summary = ris.toString();
        break;

      case VAROPT:
        final VarOptItemsSketch<String> vis = (VarOptItemsSketch<String>) sketch;
        for (final VarOptItemsSamples<String>.WeightedSample ws : vis.getSketchSamples()) {
          final JsonObject item = new JsonObject();
          item.addProperty(RESPONSE_ITEM_VALUE, ws.getItem());
          item.addProperty(RESPONSE_ITEM_WEIGHT, ws.getWeight());
          itemArray.add(item);
        }
        streamWeight = vis.getN();
        k = vis.getK();
        if (addSummary)
          summary = vis.toString();
        break;

      default:
        throw new SketchesException("processSamplingQuery() received a non-sampling sketch: " + type);
    }

    //final JsonObject result = new JsonObject();
    result.addProperty(RESPONSE_SKETCH_K, k);
    result.addProperty(RESPONSE_STREAM_WEIGHT, streamWeight);
    result.add(RESPONSE_ITEMS_ARRAY, itemArray);
    if (addSummary)
      result.addProperty(RESPONSE_SUMMARY_FIELD, summary);

    return result;
  }