private TupleSource createTupleSource(
final List<? extends SingleElementSymbol> projectedCols,
final Criteria condition, OrderBy orderBy, IndexInfo ii, boolean agg)
throws TeiidComponentException, TeiidProcessingException {
TupleBrowser browser = ii.createTupleBrowser();
TupleSource ts = new QueryTupleSource(browser, columnMap, agg?getColumns():projectedCols, condition);
boolean usingQueryTupleSource = false;
try {
TupleBuffer tb = null;
if (ii.ordering == null && orderBy != null) {
SortUtility sort = new SortUtility(ts, orderBy.getOrderByItems(), Mode.SORT, bm, sessionID, projectedCols);
tb = sort.sort();
} else if (agg) {
int count = 0;
while (ts.nextTuple() != null) {
count++;
}
return new CollectionTupleSource(Arrays.asList(Collections.nCopies(projectedCols.size(), count)).iterator());
} else if (updatable) {
tb = bm.createTupleBuffer(projectedCols, sessionID, TupleSourceType.PROCESSOR);
List<?> next = null;
while ((next = ts.nextTuple()) != null) {
tb.addTuple(next);
}
} else {
usingQueryTupleSource = true;
return ts;
}
tb.close();
return tb.createIndexedTupleSource(true);
} finally {
if (!usingQueryTupleSource) {
//ensure the buffers get released
ts.closeSource();
}
}
}