Use the ResultReader API in the query translator to read the result and write it out to the response.

git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix_stabilization_result_distribution@1137 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/aql/translator/AqlTranslator.java b/asterix-app/src/main/java/edu/uci/ics/asterix/aql/translator/AqlTranslator.java
index 22f5895..eef619e 100644
--- a/asterix-app/src/main/java/edu/uci/ics/asterix/aql/translator/AqlTranslator.java
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/aql/translator/AqlTranslator.java
@@ -82,6 +82,7 @@
 import edu.uci.ics.asterix.om.types.ATypeTag;
 import edu.uci.ics.asterix.om.types.IAType;
 import edu.uci.ics.asterix.om.types.TypeSignature;
+import edu.uci.ics.asterix.result.ResultReader;
 import edu.uci.ics.asterix.transaction.management.exception.ACIDException;
 import edu.uci.ics.asterix.transaction.management.service.transaction.TransactionIDFactory;
 import edu.uci.ics.asterix.translator.AbstractAqlTranslator;
@@ -142,11 +143,16 @@
 
     /**
      * Compiles and submits for execution a list of AQL statements.
-     * @param hcc AHyracks client connection that is used to submit a jobspec to Hyracks.
+     * @param hcc
+     *            A Hyracks client connection that is used to submit a jobspec to Hyracks.
+     * @param resultReader
+     *            A Result Reading client API object from which the query results will be read.
      * @return A List<QueryResult> containing a QueryResult instance corresponding to each submitted query.
      * @throws Exception
      */
-    public List<QueryResult> compileAndExecute(IHyracksClientConnection hcc) throws Exception {
+    public List<QueryResult> compileAndExecute(IHyracksClientConnection hcc, ResultReader resultReader)
+            throws Exception {
+        int resultSetIdCounter = 0;
         List<QueryResult> executionResult = new ArrayList<QueryResult>();
         FileSplit outputFile = null;
         IAWriterFactory writerFactory = PrinterBasedWriterFactory.INSTANCE;
@@ -250,6 +256,7 @@
                     }
 
                     case QUERY: {
+                        metadataProvider.setResultSetId(new ResultSetId(resultSetIdCounter++));
                         executionResult.add(handleQuery(metadataProvider, (Query) stmt, hcc, jobsToExecute));
                         break;
                     }
@@ -272,7 +279,12 @@
             }
             // Following jobs are run under a separate transaction, that is committed/aborted by the JobEventListener
             for (JobSpecification jobspec : jobsToExecute) {
-                runJob(hcc, jobspec);
+                JobId jobId = runJob(hcc, jobspec);
+                if (stmt.getKind() == Kind.QUERY) {
+                    resultReader.setFormat(metadataProvider.getFormat());
+                    resultReader.notifyJobStart(jobId, metadataProvider.getResultSetId());
+                }
+                hcc.waitForCompletion(jobId);
             }
         }
         return executionResult;
@@ -762,7 +774,8 @@
             GlobalConfig.ASTERIX_LOGGER.info(compiled.toJSON().toString(1));
             jobsToExecute.add(compiled);
         }
-        return new QueryResult(query, compiled.second.getLocalFile().getFile().getAbsolutePath());
+
+        return new QueryResult(query, metadataProvider.getResultSetId());
     }
 
     private void runCreateIndexJob(IHyracksClientConnection hcc, CreateIndexStatement stmtCreateIndex,