Format the results from a timeseries data query @param data_query The TSQuery object used to fetch the results @param results The data fetched from storage @param globals An optional list of global annotation objects @return A Deferred object to pass on to the caller @throws IOExcepti
(final TSQuery data_query,
final List<DataPoints[]> results, final List<Annotation> globals)
| 639 | * @since 2.2 |
| 640 | */ |
| 641 | public Deferred<ChannelBuffer> formatQueryAsyncV1(final TSQuery data_query, |
| 642 | final List<DataPoints[]> results, final List<Annotation> globals) |
| 643 | throws IOException { |
| 644 | |
| 645 | final long start = DateTime.currentTimeMillis(); |
| 646 | final boolean as_arrays = this.query.hasQueryStringParam("arrays"); |
| 647 | final String jsonp = this.query.getQueryStringParam("jsonp"); |
| 648 | |
| 649 | // buffers and an array list to stored the deferreds |
| 650 | final ChannelBuffer response = ChannelBuffers.dynamicBuffer(); |
| 651 | final OutputStream output = new ChannelBufferOutputStream(response); |
| 652 | // too bad an inner class can't modify a primitive. This is a work around |
| 653 | final List<Boolean> timeout_flag = new ArrayList<Boolean>(1); |
| 654 | timeout_flag.add(false); |
| 655 | |
| 656 | // start with JSONp if we're told to |
| 657 | if (jsonp != null && !jsonp.isEmpty()) { |
| 658 | output.write((jsonp + "(").getBytes(query.getCharset())); |
| 659 | } |
| 660 | |
| 661 | // start the JSON generator and write the opening array |
| 662 | final JsonGenerator json = JSON.getFactory().createGenerator(output); |
| 663 | json.writeStartArray(); |
| 664 | |
| 665 | /** |
| 666 | * Every individual data point set (the result of a query and possibly a |
| 667 | * group by) will initiate an asynchronous metric/tag UID to name resolution |
| 668 | * and then print to the buffer. |
| 669 | * NOTE that because this is asynchronous, the order of results is |
| 670 | * indeterminate. |
| 671 | */ |
| 672 | class DPsResolver implements Callback<Deferred<Object>, Object> { |
| 673 | /** Has to be final to be shared with the nested classes */ |
| 674 | final StringBuilder metric = new StringBuilder(256); |
| 675 | /** Resolved tags */ |
| 676 | final Map<String, String> tags = new HashMap<String, String>(); |
| 677 | /** Resolved aggregated tags */ |
| 678 | final List<String> agg_tags = new ArrayList<String>(); |
| 679 | /** A list storing the metric and tag resolve calls */ |
| 680 | final List<Deferred<Object>> resolve_deferreds = |
| 681 | new ArrayList<Deferred<Object>>(); |
| 682 | /** The data points to serialize */ |
| 683 | final DataPoints dps; |
| 684 | /** Starting time in nanos when we sent the UID resolution queries off */ |
| 685 | long uid_start; |
| 686 | |
| 687 | public DPsResolver(final DataPoints dps) { |
| 688 | this.dps = dps; |
| 689 | } |
| 690 | |
| 691 | /** Resolves the metric UID to a name*/ |
| 692 | class MetricResolver implements Callback<Object, String> { |
| 693 | public Object call(final String metric) throws Exception { |
| 694 | DPsResolver.this.metric.append(metric); |
| 695 | return null; |
| 696 | } |
| 697 | } |
| 698 |