use of org.teiid.common.buffer.TupleBatch in project teiid by teiid.
the class ProcedurePlan method nextBatchDirect.
/**
* @see ProcessorPlan#nextBatch()
*/
public TupleBatch nextBatchDirect() throws TeiidComponentException, TeiidProcessingException, BlockedException {
// Already returned results?
if (done) {
// Already returned all results
TupleBatch emptyTerminationBatch = new TupleBatch(beginBatch, new List[0]);
emptyTerminationBatch.setTerminationFlag(true);
return emptyTerminationBatch;
}
// First attempt to process
if (this.finalTupleSource == null) {
// Still need to process - this should either
// throw a BlockedException or return a finalTupleSource
this.finalTupleSource = processProcedure();
}
// Next, attempt to return batches if processing completed
while (!isBatchFull()) {
// May throw BlockedException and exit here
List<?> tuple = this.finalTupleSource.nextTuple();
if (tuple == null) {
if (outParams != null) {
VariableContext vc = getCurrentVariableContext();
List<Object> paramTuple = Arrays.asList(new Object[this.getOutputElements().size()]);
int i = this.getOutputElements().size() - this.outParams.size();
for (ElementSymbol param : outParams) {
Object value = vc.getValue(param);
checkNotNull(param, value);
paramTuple.set(i++, value);
}
addBatchRow(paramTuple, true);
}
terminateBatches();
done = true;
break;
}
addBatchRow(tuple, false);
}
return pullBatch();
}
use of org.teiid.common.buffer.TupleBatch in project teiid by teiid.
the class BatchCollector method collectTuples.
public TupleBuffer collectTuples(boolean singleBatch) throws TeiidComponentException, TeiidProcessingException {
TupleBatch batch = null;
while (!done) {
if (this.hasFinalBuffer) {
if (this.buffer == null) {
TupleBuffer finalBuffer = this.sourceNode.getBuffer(rowLimit);
Assertion.isNotNull(finalBuffer);
this.buffer = finalBuffer;
}
if (this.buffer.isFinal()) {
this.buffer.setForwardOnly(forwardOnly);
done = true;
break;
}
}
batch = sourceNode.nextBatch();
if (rowLimit > 0 && rowLimit <= batch.getEndRow()) {
if (!done) {
this.sourceNode.close();
}
List<?> lastTuple = null;
if (saveLastRow) {
if (batch.getTerminationFlag()) {
lastTuple = batch.getTuples().get(batch.getTuples().size() - 1);
} else if (rowLimit < batch.getBeginRow()) {
// skip until end
continue;
}
}
boolean modified = false;
if (rowLimit < batch.getEndRow()) {
// we know row limit must be smaller than max int, so an int cast is safe here
int firstRow = (int) Math.min(rowLimit + 1, batch.getBeginRow());
List<List<?>> tuples = batch.getTuples().subList(0, rowLimit - firstRow + 1);
batch = new TupleBatch(firstRow, tuples);
modified = true;
}
if (lastTuple != null) {
if (!modified) {
batch = new TupleBatch(batch.getBeginRow(), batch.getTuples());
}
batch.getTuples().add(lastTuple);
}
batch.setTerminationFlag(true);
}
flushBatch(batch);
// Check for termination condition
if (batch.getTerminationFlag()) {
done = true;
if (!this.sourceNode.hasBuffer()) {
buffer.close();
}
break;
}
if (singleBatch) {
return null;
}
}
return buffer;
}
use of org.teiid.common.buffer.TupleBatch in project teiid by teiid.
the class BatchIterator method readAhead.
public void readAhead(long limit) throws TeiidComponentException, TeiidProcessingException {
if (buffer == null || done) {
return;
}
if (this.buffer.getManagedRowCount() >= limit) {
return;
}
if (this.batch != null && this.buffer.getRowCount() < this.batch.getEndRow() && !this.buffer.isForwardOnly()) {
// haven't saved already
this.buffer.addTupleBatch(this.batch, true);
}
TupleBatch tb = source.nextBatch();
done = tb.getTerminationFlag();
this.buffer.addTupleBatch(tb, true);
if (done) {
this.buffer.close();
}
}
use of org.teiid.common.buffer.TupleBatch in project teiid by teiid.
the class DdlPlan method nextBatch.
@Override
public TupleBatch nextBatch() throws BlockedException, TeiidComponentException, TeiidProcessingException {
TupleBatch tupleBatch = new TupleBatch(1, new List[] { Arrays.asList(0) });
tupleBatch.setTerminationFlag(true);
return tupleBatch;
}
use of org.teiid.common.buffer.TupleBatch in project teiid by teiid.
the class QueryProcessor method nextBatchDirect.
private TupleBatch nextBatchDirect() throws BlockedException, TeiidProcessingException, TeiidComponentException {
boolean done = false;
TupleBatch result = null;
try {
init();
long currentTime = System.currentTimeMillis();
Assertion.assertTrue(!processorClosed);
while (currentTime < context.getTimeSliceEnd() || context.isNonBlocking()) {
if (requestCanceled) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30160, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30160, this.context.getRequestId()));
}
if (currentTime > context.getTimeoutEnd()) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30161, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30161));
}
result = processPlan.nextBatch();
if (continuous) {
result.setRowOffset(rowOffset);
if (result.getTerminationFlag()) {
result.setTermination(TupleBatch.ITERATION_TERMINATED);
List<Object> terminationTuple = Arrays.asList(new Object[this.getOutputElements().size()]);
result.getTuples().add(terminationTuple);
this.context.getTupleSourceCache().close();
this.processPlan.close();
this.processPlan.reset();
this.context.incrementReuseCount();
this.open = false;
}
rowOffset = result.getEndRow() + 1;
}
if (result.getTermination() != TupleBatch.NOT_TERMINATED) {
if (result.getTerminationFlag()) {
done = true;
}
break;
}
if (result.getRowCount() > 0) {
break;
}
}
} catch (BlockedException e) {
throw e;
} catch (TeiidException e) {
closeProcessing();
if (e instanceof TeiidProcessingException) {
throw (TeiidProcessingException) e;
}
if (e instanceof TeiidComponentException) {
throw (TeiidComponentException) e;
}
throw new TeiidComponentException(QueryPlugin.Event.TEIID30162, e);
}
if (done) {
closeProcessing();
}
if (result == null) {
RequestWorkItem workItem = this.getContext().getWorkItem();
if (workItem != null) {
// if we have a workitem (non-test scenario) then before
// throwing exprired time slice we need to indicate there's more work
workItem.moreWork();
}
throw EXPIRED_TIME_SLICE;
}
return result;
}
Aggregations