}
protected TupleBatch nextBatchDirect() throws BlockedException,
TeiidComponentException,
TeiidProcessingException {
TupleBatch batch = null; // Can throw BlockedException
if (limit == 0) {
this.terminateBatches();
return pullBatch();
}
// If we haven't reached the offset, then skip rows/batches
if (offsetPhase) {
while (rowCounter <= offset) {
batch = getChildren()[0].nextBatch(); // Can throw BlockedException
rowCounter += batch.getRowCount();
if (batch.getTerminationFlag()) {
break;
}
}
List[] tuples = null;
if (rowCounter > offset) {
List[] originalTuples = batch.getAllTuples();
int rowsToKeep = rowCounter - offset;
tuples = new List[rowsToKeep];
System.arraycopy(originalTuples, batch.getRowCount() - rowsToKeep, tuples, 0, tuples.length);
} else {
tuples = new List[0]; //empty batch
}
TupleBatch resultBatch = new TupleBatch(1, tuples);
resultBatch.setTerminationFlag(batch.getTerminationFlag());
batch = resultBatch;
offsetPhase = false;
rowCounter = 0;
} else {
batch = getChildren()[0].nextBatch(); // Can throw BlockedException
}
List[] tuples = null;
if (limit < 0 || rowCounter + batch.getRowCount() <= limit) {
// Passthrough
tuples = batch.getAllTuples();
} else {
// Partial batch
List[] originalTuples = batch.getAllTuples();
tuples = new List[limit - rowCounter];
System.arraycopy(originalTuples, 0, tuples, 0, tuples.length);
}
TupleBatch resultBatch = new TupleBatch(rowCounter+1, tuples);
rowCounter += resultBatch.getRowCount();
if (rowCounter == limit || batch.getTerminationFlag()) {
resultBatch.setTerminationFlag(true);
}
return resultBatch;
}