Writes the results to a ChannelBuffer to return to the caller. This will iterate over all of the outputs and drop in meta data where appropriate. @throws Exception if something went pear shaped
()
| 480 | * @throws Exception if something went pear shaped |
| 481 | */ |
| 482 | private Deferred<ChannelBuffer> serialize() throws Exception { |
| 483 | final long start = System.currentTimeMillis(); |
| 484 | // buffers and an array list to stored the deferreds |
| 485 | final ChannelBuffer response = ChannelBuffers.dynamicBuffer(); |
| 486 | final OutputStream output_stream = new ChannelBufferOutputStream(response); |
| 487 | |
| 488 | final JsonGenerator json = JSON.getFactory().createGenerator(output_stream); |
| 489 | json.writeStartObject(); |
| 490 | json.writeFieldName("outputs"); |
| 491 | json.writeStartArray(); |
| 492 | |
| 493 | // We want the serializer to execute serially so we need to create a callback |
| 494 | // chain so that when one DPsResolver is finished, it triggers the next to |
| 495 | // start serializing. |
| 496 | final Deferred<Object> cb_chain = new Deferred<Object>(); |
| 497 | |
| 498 | // default to the expressions if there, or fall back to the metrics |
| 499 | final List<Output> outputs; |
| 500 | if (query.getOutputs() == null || query.getOutputs().isEmpty()) { |
| 501 | if (query.getExpressions() != null && !query.getExpressions().isEmpty()) { |
| 502 | outputs = new ArrayList<Output>(query.getExpressions().size()); |
| 503 | for (final Expression exp : query.getExpressions()) { |
| 504 | outputs.add(Output.Builder().setId(exp.getId()).build()); |
| 505 | } |
| 506 | } else if (query.getMetrics() != null && !query.getMetrics().isEmpty()) { |
| 507 | outputs = new ArrayList<Output>(query.getMetrics().size()); |
| 508 | for (final Metric metric : query.getMetrics()) { |
| 509 | outputs.add(Output.Builder().setId(metric.getId()).build()); |
| 510 | } |
| 511 | } else { |
| 512 | throw new IllegalArgumentException( |
| 513 | "How did we get here?? No metrics or expressions??"); |
| 514 | } |
| 515 | } else { |
| 516 | outputs = query.getOutputs(); |
| 517 | } |
| 518 | |
| 519 | for (final Output output : outputs) { |
| 520 | if (expressions != null) { |
| 521 | final ExpressionIterator it = expressions.get(output.getId()); |
| 522 | if (it != null) { |
| 523 | cb_chain.addCallback(new SerializeExpressionIterator(tsdb, json, |
| 524 | output, it, ts_query)); |
| 525 | continue; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if (query.getMetrics() != null && !query.getMetrics().isEmpty()) { |
| 530 | final TSSubQuery sub = sub_queries.get(output.getId()); |
| 531 | if (sub != null) { |
| 532 | final TimeSyncedIterator it = new TimeSyncedIterator(output.getId(), |
| 533 | sub.getFilterTagKs(), sub_query_results.get(output.getId())); |
| 534 | cb_chain.addCallback(new SerializeSubIterator(tsdb, json, output, it)); |
| 535 | continue; |
| 536 | } |
| 537 | } else { |
| 538 | LOG.warn("Couldn't find a variable matching: " + output.getId() + |
| 539 | " in query " + query); |
no test coverage detected