/**
* Send results if they have been requested. This should only be called from the processing thread.
*/
protected boolean sendResultsIfNeeded(TupleBatch batch) throws TeiidComponentException {
ResultsMessage response = null;
ResultsReceiver<ResultsMessage> receiver = null;
boolean result = true;
synchronized (this) {
if (this.resultsReceiver == null
|| (this.begin > (batch != null?batch.getEndRow():this.resultsBuffer.getRowCount()) && !doneProducingBatches)
|| (this.transactionState == TransactionState.ACTIVE)) {
return result;
}
if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP, MessageLevel.DETAIL)) {
LogManager.logDetail(LogConstants.CTX_DQP, "[RequestWorkItem.sendResultsIfNeeded] requestID:", requestID, "resultsID:", this.resultsBuffer, "done:", doneProducingBatches ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
//TODO: support fetching more than 1 batch
boolean fromBuffer = false;
if (batch == null || !(batch.containsRow(this.begin) || (batch.getTerminationFlag() && batch.getEndRow() <= this.begin))) {
if (savedBatch != null && savedBatch.containsRow(this.begin)) {
batch = savedBatch;
} else {
batch = resultsBuffer.getBatch(begin);
}
savedBatch = null;
fromBuffer = true;
}
int count = this.end - this.begin + 1;
if (batch.getRowCount() > count) {
int beginRow = Math.min(this.begin, batch.getEndRow() - count + 1);
int endRow = Math.min(beginRow + count - 1, batch.getEndRow());
boolean last = false;
if (endRow == batch.getEndRow()) {
last = batch.getTerminationFlag();
} else if (fromBuffer && isForwardOnly()) {
savedBatch = batch;
}
List<List<?>> memoryRows = batch.getTuples();
batch = new TupleBatch(beginRow, memoryRows.subList(beginRow - batch.getBeginRow(), endRow - batch.getBeginRow() + 1));
batch.setTerminationFlag(last);
} else if (!fromBuffer){
result = !isForwardOnly();
}
int finalRowCount = this.resultsBuffer.isFinal()?this.resultsBuffer.getRowCount():(batch.getTerminationFlag()?batch.getEndRow():-1);
response = createResultsMessage(batch.getAllTuples(), this.originalCommand.getProjectedSymbols());
response.setFirstRow(batch.getBeginRow());
response.setLastRow(batch.getEndRow());
response.setUpdateResult(this.returnsUpdateCount);
// set final row
response.setFinalRow(finalRowCount);
// send any warnings with the response object
List<Throwable> responseWarnings = new ArrayList<Throwable>();
if (this.processor != null) {
List<Exception> currentWarnings = processor.getAndClearWarnings();
if (currentWarnings != null) {
responseWarnings.addAll(currentWarnings);
}
}
synchronized (warnings) {
responseWarnings.addAll(this.warnings);
this.warnings.clear();
}
response.setWarnings(responseWarnings);
// If it is stored procedure, set parameters
if (originalCommand instanceof StoredProcedure) {
StoredProcedure proc = (StoredProcedure)originalCommand;
if (proc.returnParameters()) {
response.setParameters(getParameterInfo(proc));
}
}
/*
* mark the results sent at this point.
* communication exceptions will be treated as non-recoverable