sparqlGraphLibrary/src/main/java/com/ge/research/semtk/edc/client/ResultsClient.java
@SuppressWarnings("unchecked")
	public void execStoreTableResults(String jobId, Table table) throws ConnectException, EndpointNotFoundException, Exception {
		// chunk up the table by size and then send all the chunks. 
		// hopefully, this will avoid sending anything too large to the results service
		
		int tableRowsDone = 0;
		int totalRows     = table.getNumRows();
		
		long startTime=0, endTime=0;
		double prepSec = 0.0;
		double sendSec = 0.0;
		boolean timerFlag = false;
		
		Thread thread = null;

		// write the start of the JSON
		this.conf.setServiceEndpoint("results/storeTableResultsJsonInitialize"); 
		this.conf.setMethod(RestClientConfig.Methods.POST);
		
		this.parametersJSON.put("jobId", jobId);
		this.parametersJSON.put("jsonRenderedHeader", createNewHeaderMap(table).toJSONString());
		thread = new Thread(this, "execStoreTableResults_initialize");
		thread.start();
		
		// do formatting tasks in parallel
		formatTableElements(table);		// escapes double quotes
		
		// write the data rows to JSON, in batches
		while(tableRowsDone < totalRows){
			if (timerFlag) { startTime = System.nanoTime();}
			int tableRowsAtStart = tableRowsDone;
			// get the next few rows.
			StringBuilder resultsSoFar = new StringBuilder();

			// get the next allocation of rows. 
			for(int i = 0; i < this.BATCH_SIZE_SEND; i++){
				
				if(!(tableRowsDone < table.getNumRows())){
					break;	// processed all rows - we're done
				}
				try{

					resultsSoFar.append("[");
					ArrayList<String> row = table.getRow(tableRowsDone);
					for(int j = 0; j < row.size(); j++){
						resultsSoFar.append("\"").append(row.get(j)).append("\"");	// enclose in quotes (tried putting in thread, but resulted in worse performance)
						if(j < row.size() - 1){
							resultsSoFar.append(",");								// don't append comma to the last element of the row
						}
					}
					resultsSoFar.append("]");
					
					// each row has: 1) internal double quotes escaped 2) elements surrounded by double quotes 3) enclosing brackets

					tableRowsDone += 1;
					
					if(i < BATCH_SIZE_SEND - 1){
						resultsSoFar.append("\n");
					}
				}
				catch(IndexOutOfBoundsException eek){
					// we have run out of rows. the remaining rows were fewer than the block size. just note this and move on.
					i = this.BATCH_SIZE_SEND;
				}

			}

			// fail if tableRowsDone has not changed. this implies that even the first result was too large.
			if((tableRowsDone == tableRowsAtStart) && (tableRowsDone < totalRows)){
				this.cleanUp();
				throw new Exception("unable to write results. there is a row size which is too large. row number was " + tableRowsDone + " of a total " + totalRows + ".");
			}

			if (timerFlag) { 
				endTime = System.nanoTime();
				prepSec += ((endTime - startTime) / 1000000000.0);
				LocalLogger.logToStdErr(String.format("tot prep=%.2f sec", prepSec));
				startTime = endTime;
			}

			// wait for previous batch to finish
			waitForThreadToFinish(thread);
			
			// send the current batch  
			this.conf.setServiceEndpoint("results/storeTableResultsJsonAddIncremental"); 
			this.conf.setMethod(RestClientConfig.Methods.POST);
			this.parametersJSON.put("contents", Utility.compress(resultsSoFar.toString())); 
			this.parametersJSON.put("jobId", jobId);
			thread = new Thread(this, "execStoreTableResults_jsonIncremental_"+tableRowsDone);
			thread.start();
			
			if (timerFlag) { 
				endTime = System.nanoTime();
				sendSec += ((endTime - startTime) / 1000000000.0);
				LocalLogger.logToStdErr(String.format("tot send=%.2f sec", sendSec));
				startTime = endTime;
			}
		} // end of while loop.
		
		// wait for the final batch to finish
		waitForThreadToFinish(thread);
		
		// write the end of the JSON
		this.conf.setServiceEndpoint("results/storeTableResultsJsonFinalize"); 
		this.conf.setMethod(RestClientConfig.Methods.POST);
		this.parametersJSON.put("jobId", jobId);
		thread = new Thread(this, "execStoreTableResults_finalize");
		thread.start();
		
		// wait for the finalize run to finish
		waitForThreadToFinish(thread);

		if (timerFlag) { LocalLogger.logToStdErr(String.format("prep=%.2f sec   send=%.2f sec", prepSec, sendSec)); }
		this.cleanUp();
		return;
	}
/**
	 * Store a table (as json).  
	 * Uses 3 endpoints to initialize, incrementally add data, and finalize the result.
	 * @param contents
	 * @throws Exception
	 */
Store a table (as json).
Uses 3 endpoints to initialize, incrementally add data, and finalize the result.
@param contents
@throws Exception
Store a table ( as json ) . Uses 3 endpoints to initialize incrementally add data and finalize the result . @param contents @throws Exception
@SuppressWarnings("unchecked")
	public void execStoreTableResults(String jobId, Table table) throws ConnectException, EndpointNotFoundException, Exception {
		int tableRowsDone = 0;
		int totalRows     = table.getNumRows();
		long startTime=0, endTime=0;
		double prepSec = 0.0;
		double sendSec = 0.0;
		boolean timerFlag = false;
		Thread thread = null;
		this.conf.setServiceEndpoint("results/storeTableResultsJsonInitialize"); 
		this.conf.setMethod(RestClientConfig.Methods.POST);
		this.parametersJSON.put("jobId", jobId);
		this.parametersJSON.put("jsonRenderedHeader", createNewHeaderMap(table).toJSONString());
		thread = new Thread(this, "execStoreTableResults_initialize");
		thread.start();
		formatTableElements(table);		
		while(tableRowsDone < totalRows){
			if (timerFlag) { startTime = System.nanoTime();}
			int tableRowsAtStart = tableRowsDone;
			StringBuilder resultsSoFar = new StringBuilder();
			for(int i = 0; i < this.BATCH_SIZE_SEND; i++){
				if(!(tableRowsDone < table.getNumRows())){
					break;	
				}
				try{
					resultsSoFar.append("[");
					ArrayList<String> row = table.getRow(tableRowsDone);
					for(int j = 0; j < row.size(); j++){
						resultsSoFar.append("\"").append(row.get(j)).append("\"");	
						if(j < row.size() - 1){
							resultsSoFar.append(",");								
						}
					}
					resultsSoFar.append("]");
					tableRowsDone += 1;
					if(i < BATCH_SIZE_SEND - 1){
						resultsSoFar.append("\n");
					}
				}
				catch(IndexOutOfBoundsException eek){
					i = this.BATCH_SIZE_SEND;
				}
			}
			if((tableRowsDone == tableRowsAtStart) && (tableRowsDone < totalRows)){
				this.cleanUp();
				throw new Exception("unable to write results. there is a row size which is too large. row number was " + tableRowsDone + " of a total " + totalRows + ".");
			}
			if (timerFlag) { 
				endTime = System.nanoTime();
				prepSec += ((endTime - startTime) / 1000000000.0);
				LocalLogger.logToStdErr(String.format("tot prep=%.2f sec", prepSec));
				startTime = endTime;
			}
			waitForThreadToFinish(thread);
			this.conf.setServiceEndpoint("results/storeTableResultsJsonAddIncremental"); 
			this.conf.setMethod(RestClientConfig.Methods.POST);
			this.parametersJSON.put("contents", Utility.compress(resultsSoFar.toString())); 
			this.parametersJSON.put("jobId", jobId);
			thread = new Thread(this, "execStoreTableResults_jsonIncremental_"+tableRowsDone);
			thread.start();
			if (timerFlag) { 
				endTime = System.nanoTime();
				sendSec += ((endTime - startTime) / 1000000000.0);
				LocalLogger.logToStdErr(String.format("tot send=%.2f sec", sendSec));
				startTime = endTime;
			}
		} 
		waitForThreadToFinish(thread);
		this.conf.setServiceEndpoint("results/storeTableResultsJsonFinalize"); 
		this.conf.setMethod(RestClientConfig.Methods.POST);
		this.parametersJSON.put("jobId", jobId);
		thread = new Thread(this, "execStoreTableResults_finalize");
		thread.start();
		waitForThreadToFinish(thread);
		if (timerFlag) { LocalLogger.logToStdErr(String.format("prep=%.2f sec   send=%.2f sec", prepSec, sendSec)); }
		this.cleanUp();
		return;
	}
@ SuppressWarnings ( "unchecked" ) public void execStoreTableResults ( String jobId , Table table ) throws ConnectException , EndpointNotFoundException , Exception { int tableRowsDone = 0 ; int totalRows = table . getNumRows ( ) ; long startTime = 0 , endTime = 0 ; double prepSec = 0.0 ; double sendSec = 0.0 ; boolean timerFlag = false ; Thread thread = null ; this . conf . setServiceEndpoint ( "results/storeTableResultsJsonInitialize" ) ; this . conf . setMethod ( RestClientConfig . Methods . POST ) ; this . parametersJSON . put ( "jobId" , jobId ) ; this . parametersJSON . put ( "jsonRenderedHeader" , createNewHeaderMap ( table ) . toJSONString ( ) ) ; thread = new Thread ( this , "execStoreTableResults_initialize" ) ; thread . start ( ) ; formatTableElements ( table ) ; while ( tableRowsDone < totalRows ) { if ( timerFlag ) { startTime = System . nanoTime ( ) ; } int tableRowsAtStart = tableRowsDone ; StringBuilder resultsSoFar = new StringBuilder ( ) ; for ( int i = 0 ; i < this . BATCH_SIZE_SEND ; i ++ ) { if ( ! ( tableRowsDone < table . getNumRows ( ) ) ) { break ; } try { resultsSoFar . append ( "[" ) ; ArrayList < String > row = table . getRow ( tableRowsDone ) ; for ( int j = 0 ; j < row . size ( ) ; j ++ ) { resultsSoFar . append ( "\"" ) . append ( row . get ( j ) ) . append ( "\"" ) ; if ( j < row . size ( ) - 1 ) { resultsSoFar . append ( "," ) ; } } resultsSoFar . append ( "]" ) ; tableRowsDone += 1 ; if ( i < BATCH_SIZE_SEND - 1 ) { resultsSoFar . append ( "\n" ) ; } } catch ( IndexOutOfBoundsException eek ) { i = this . BATCH_SIZE_SEND ; } } if ( ( tableRowsDone == tableRowsAtStart ) && ( tableRowsDone < totalRows ) ) { this . cleanUp ( ) ; throw new Exception ( "unable to write results. there is a row size which is too large. row number was " + tableRowsDone + " of a total " + totalRows + "." ) ; } if ( timerFlag ) { endTime = System . nanoTime ( ) ; prepSec += ( ( endTime - startTime ) / 1000000000.0 ) ; LocalLogger . logToStdErr ( String . format ( "tot prep=%.2f sec" , prepSec ) ) ; startTime = endTime ; } waitForThreadToFinish ( thread ) ; this . conf . setServiceEndpoint ( "results/storeTableResultsJsonAddIncremental" ) ; this . conf . setMethod ( RestClientConfig . Methods . POST ) ; this . parametersJSON . put ( "contents" , Utility . compress ( resultsSoFar . toString ( ) ) ) ; this . parametersJSON . put ( "jobId" , jobId ) ; thread = new Thread ( this , "execStoreTableResults_jsonIncremental_" + tableRowsDone ) ; thread . start ( ) ; if ( timerFlag ) { endTime = System . nanoTime ( ) ; sendSec += ( ( endTime - startTime ) / 1000000000.0 ) ; LocalLogger . logToStdErr ( String . format ( "tot send=%.2f sec" , sendSec ) ) ; startTime = endTime ; } } waitForThreadToFinish ( thread ) ; this . conf . setServiceEndpoint ( "results/storeTableResultsJsonFinalize" ) ; this . conf . setMethod ( RestClientConfig . Methods . POST ) ; this . parametersJSON . put ( "jobId" , jobId ) ; thread = new Thread ( this , "execStoreTableResults_finalize" ) ; thread . start ( ) ; waitForThreadToFinish ( thread ) ; if ( timerFlag ) { LocalLogger . logToStdErr ( String . format ( "prep=%.2f sec   send=%.2f sec" , prepSec , sendSec ) ) ; } this . cleanUp ( ) ; return ; }
Store a table (as json).
Store a table ( as json ) .
// chunk up the table by size and then send all the chunks.  // hopefully, this will avoid sending anything too large to the results service // write the start of the JSON // do formatting tasks in parallel // escapes double quotes // write the data rows to JSON, in batches // get the next few rows. // get the next allocation of rows.  // processed all rows - we're done // enclose in quotes (tried putting in thread, but resulted in worse performance) // don't append comma to the last element of the row // each row has: 1) internal double quotes escaped 2) elements surrounded by double quotes 3) enclosing brackets // we have run out of rows. the remaining rows were fewer than the block size. just note this and move on. // fail if tableRowsDone has not changed. this implies that even the first result was too large. // wait for previous batch to finish // send the current batch   // end of while loop. // wait for the final batch to finish // write the end of the JSON // wait for the finalize run to finish