use of org.teiid.common.buffer.TupleSource in project teiid by teiid.
the class TempTable method createTupleSource.
private TupleSource createTupleSource(final List<? extends Expression> projectedCols, final Criteria condition, OrderBy orderBy, IndexInfo ii, boolean agg) throws TeiidComponentException, TeiidProcessingException {
TupleBrowser browser = ii.createTupleBrowser(bm.getOptions().getDefaultNullOrder(), true);
TupleSource ts = new QueryTupleSource(browser, columnMap, agg ? getColumns() : projectedCols, condition);
boolean usingQueryTupleSource = false;
boolean success = false;
TupleBuffer tb = null;
try {
if (ii.ordering == null && orderBy != null) {
SortUtility sort = new SortUtility(ts, orderBy.getOrderByItems(), Mode.SORT, bm, sessionID, projectedCols);
sort.setNonBlocking(true);
tb = sort.sort();
} else if (agg) {
int count = 0;
while (ts.nextTuple() != null) {
count++;
}
success = true;
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;
success = true;
return ts;
}
tb.close();
success = true;
return tb.createIndexedTupleSource(true);
} finally {
if (!success && tb != null) {
tb.remove();
}
if (!usingQueryTupleSource) {
// ensure the buffers get released
ts.closeSource();
}
}
}
use of org.teiid.common.buffer.TupleSource in project teiid by teiid.
the class TempTable method addIndex.
void addIndex(List<ElementSymbol> indexColumns, boolean unique) throws TeiidComponentException, TeiidProcessingException {
List<ElementSymbol> keyColumns = columns.subList(0, tree.getKeyLength());
if (keyColumns.equals(indexColumns) || (indexTables != null && indexTables.containsKey(indexColumns))) {
return;
}
TempTable indexTable = createIndexTable(indexColumns, unique);
// TODO: ordered insert optimization
TupleSource ts = createTupleSource(indexTable.getColumns(), null, null);
indexTable.insert(ts, indexTable.getColumns(), false, false, null);
indexTable.getTree().compact();
}
use of org.teiid.common.buffer.TupleSource in project teiid by teiid.
the class TempTableDataManager method registerQuery.
private TupleSource registerQuery(final CommandContext context, final TempTableStore contextStore, final Query query) {
final GroupSymbol group = query.getFrom().getGroups().get(0);
if (!group.isTempGroupSymbol()) {
return null;
}
final String tableName = group.getNonCorrelationName();
if (group.isGlobalTable()) {
TempMetadataID matTableId = (TempMetadataID) group.getMetadataID();
final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
final MatTableInfo info = globalStore.getMatTableInfo(tableName);
return new ProxyTupleSource() {
Future<Void> moreWork = null;
TupleSource loadingTupleSource;
DQPWorkContext newWorkContext;
@Override
protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
if (loadingTupleSource != null) {
load();
} else {
boolean load = false;
if (!info.isUpToDate()) {
boolean invalidate = shouldInvalidate(context.getVdb());
load = globalStore.needsLoading(tableName, globalStore.getAddress(), true, false, info.isValid() && invalidate);
if (load) {
load = globalStore.needsLoading(tableName, globalStore.getAddress(), false, false, info.isValid() && invalidate);
}
if (!load) {
synchronized (info) {
if (!info.isUpToDate()) {
RequestWorkItem workItem = context.getWorkItem();
info.addWaiter(workItem);
if (moreWork != null) {
moreWork.cancel(false);
}
// fail-safe - attempt again in 10 seconds
moreWork = workItem.scheduleWork(10000);
// $NON-NLS-1$
throw BlockedException.block("Blocking on mat view load", tableName);
}
}
} else {
if (!info.isValid() || executor == null) {
// TODO: we should probably do all loads using a temp session
if (info.getVdbMetaData() != null && context.getDQPWorkContext() != null && !info.getVdbMetaData().getFullName().equals(context.getDQPWorkContext().getVDB().getFullName())) {
assert executor != null;
// load with by pretending we're in the imported vdb
newWorkContext = createWorkContext(context, info.getVdbMetaData());
CommandContext newContext = context.clone();
newContext.setNewVDBState(newWorkContext);
loadingTupleSource = loadGlobalTable(newContext, group, tableName, newContext.getGlobalTableStore());
} else {
loadingTupleSource = loadGlobalTable(context, group, tableName, globalStore);
}
load();
} else {
loadViaRefresh(context, tableName, context.getDQPWorkContext().getVDB(), info);
}
}
}
}
TempTable table = globalStore.getTempTable(tableName);
context.accessedDataObject(group.getMetadataID());
if (context.isParallel() && query.getCriteria() == null && query.getOrderBy() != null && table.getRowCount() > MIN_ASYNCH_SIZE) {
return new AsyncTupleSource(new Callable<TupleSource>() {
@Override
public TupleSource call() throws Exception {
synchronized (this) {
TupleSource result = table.createTupleSource(query.getProjectedSymbols(), query.getCriteria(), query.getOrderBy());
cancelMoreWork();
return result;
}
}
}, context);
}
TupleSource result = table.createTupleSource(query.getProjectedSymbols(), query.getCriteria(), query.getOrderBy());
cancelMoreWork();
return result;
}
private void load() throws TeiidComponentException, TeiidProcessingException {
try {
if (newWorkContext != null) {
newWorkContext.runInContext(new Callable<Void>() {
@Override
public Void call() throws Exception {
loadingTupleSource.nextTuple();
return null;
}
});
} else {
loadingTupleSource.nextTuple();
}
} catch (Throwable e) {
rethrow(e);
}
}
private void cancelMoreWork() {
if (moreWork != null) {
moreWork.cancel(false);
moreWork = null;
}
}
@Override
public void closeSource() {
if (loadingTupleSource != null) {
loadingTupleSource.closeSource();
}
super.closeSource();
cancelMoreWork();
}
};
}
// it's not expected for a blocked exception to bubble up from here, so return a tuplesource to perform getOrCreateTempTable
return new ProxyTupleSource() {
@Override
protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
TempTableStore tts = contextStore;
TempTable tt = tts.getOrCreateTempTable(tableName, query, bufferManager, true, false, context, group);
if (context.getDataObjects() != null) {
Object id = RelationalPlanner.getTrackableGroup(group, context.getMetadata());
if (id != null) {
context.accessedDataObject(id);
}
}
if (context.isParallel() && query.getCriteria() == null && query.getOrderBy() != null && tt.getRowCount() > MIN_ASYNCH_SIZE) {
return new AsyncTupleSource(new Callable<TupleSource>() {
@Override
public TupleSource call() throws Exception {
synchronized (this) {
return tt.createTupleSource(query.getProjectedSymbols(), query.getCriteria(), query.getOrderBy());
}
}
}, context);
}
return tt.createTupleSource(query.getProjectedSymbols(), query.getCriteria(), query.getOrderBy());
}
};
}
use of org.teiid.common.buffer.TupleSource in project teiid by teiid.
the class TempTableDataManager method lookupCodeValue.
public Object lookupCodeValue(CommandContext context, String codeTableName, String returnElementName, String keyElementName, Object keyValue) throws BlockedException, TeiidComponentException, TeiidProcessingException {
// we are not using a resolved form of a lookup, so we canonicallize with upper case
codeTableName = codeTableName.toUpperCase();
keyElementName = keyElementName.toUpperCase();
returnElementName = returnElementName.toUpperCase();
String matTableName = CODE_PREFIX + codeTableName + ElementSymbol.SEPARATOR + keyElementName + ElementSymbol.SEPARATOR + returnElementName;
TupleSource ts = context.getCodeLookup(matTableName, keyValue);
if (ts == null) {
QueryMetadataInterface metadata = context.getMetadata();
TempMetadataID id = context.getGlobalTableStore().getCodeTableMetadataId(codeTableName, returnElementName, keyElementName, matTableName);
ElementSymbol keyElement = new ElementSymbol(keyElementName, new GroupSymbol(matTableName));
ElementSymbol returnElement = new ElementSymbol(returnElementName, new GroupSymbol(matTableName));
keyElement.setType(DataTypeManager.getDataTypeClass(metadata.getElementRuntimeTypeName(metadata.getElementID(codeTableName + ElementSymbol.SEPARATOR + keyElementName))));
returnElement.setType(DataTypeManager.getDataTypeClass(metadata.getElementRuntimeTypeName(metadata.getElementID(codeTableName + ElementSymbol.SEPARATOR + returnElementName))));
Query query = RelationalPlanner.createMatViewQuery(id, matTableName, Arrays.asList(returnElement), true);
query.setCriteria(new CompareCriteria(keyElement, CompareCriteria.EQ, new Constant(keyValue)));
ts = registerQuery(context, context.getTempTableStore(), query);
}
try {
List<?> row = ts.nextTuple();
Object result = null;
if (row != null) {
result = row.get(0);
}
ts.closeSource();
return result;
} catch (BlockedException e) {
context.putCodeLookup(matTableName, keyValue, ts);
throw e;
}
}
use of org.teiid.common.buffer.TupleSource in project teiid by teiid.
the class CommandContext method close.
public void close() {
synchronized (this.globalState) {
if (this.globalState.reservedBuffers > 0) {
long toRelease = this.globalState.reservedBuffers;
this.globalState.reservedBuffers = 0;
this.globalState.bufferManager.releaseOrphanedBuffers(toRelease);
}
if (this.globalState.reusableExecutions != null) {
for (List<ReusableExecution<?>> reusableExecutions : this.globalState.reusableExecutions.values()) {
for (ReusableExecution<?> reusableExecution : reusableExecutions) {
try {
reusableExecution.dispose();
} catch (Exception e) {
LogManager.logWarning(LogConstants.CTX_DQP, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30030));
}
}
}
this.globalState.reusableExecutions.clear();
}
if (this.globalState.commandListeners != null) {
for (CommandListener listener : this.globalState.commandListeners) {
try {
listener.commandClosed(this);
} catch (Exception e) {
LogManager.logWarning(LogConstants.CTX_DQP, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30031));
}
}
this.globalState.commandListeners.clear();
}
if (this.globalState.lookups != null) {
for (TupleSource ts : this.globalState.lookups.values()) {
ts.closeSource();
}
this.globalState.lookups = null;
}
if (this.globalState.created != null) {
for (InputStreamFactory isf : this.globalState.created) {
try {
isf.free();
} catch (IOException e) {
}
}
this.globalState.created.clear();
}
}
}
Aggregations