private TempTable createIndexTable(List<ElementSymbol> indexColumns, boolean unique) { List<ElementSymbol> allColumns = new ArrayList<ElementSymbol>(indexColumns); for (ElementSymbol elementSymbol : columns.subList(0, tree.getKeyLength())) { if (allColumns.indexOf(elementSymbol) < 0) { allColumns.add(elementSymbol); } } TempTable indexTable = new TempTable(new TempMetadataID("idx", Collections.EMPTY_LIST), this.bm, allColumns, allColumns.size(), this.sessionID); //$NON-NLS-1$ indexTable.setPreferMemory(this.tree.isPreferMemory()); indexTable.lock = this.lock; if (unique) { indexTable.uniqueColIndex = indexColumns.size(); } if (indexTables == null) { indexTables = new LinkedHashMap<List<ElementSymbol>, TempTable>(); indexTables.put(indexColumns, indexTable); } indexTable.setUpdatable(this.updatable); return indexTable; }
@Override public List<?> updateMatViewRow(String matTableName, List<?> tuple, boolean delete) throws TeiidComponentException { TempTable tempTable = tableStore.getTempTable(matTableName); if (tempTable != null) { TempMetadataID id = tableStore.getMetadataStore().getTempGroupID(matTableName); synchronized (id) { boolean clone = tempTable.getActive().get() != 0; if (clone) { tempTable = tempTable.clone(); } List<?> result = tempTable.updateTuple(tuple, delete); if (clone) { swapTempTable(matTableName, tempTable); } return result; } } return null; }
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(); }
void readFrom(ObjectInputStream ois) throws TeiidComponentException, IOException, ClassNotFoundException { this.tree.readValuesFrom(ois); int numIdx = ois.readInt(); for (int i = 0; i < numIdx; i++) { boolean unique = ois.readBoolean(); int numCols = ois.readInt(); ArrayList<ElementSymbol> indexColumns = new ArrayList<ElementSymbol>(numCols); for (int j = 0; j < numCols; j++) { int colIndex = ois.readInt(); indexColumns.add(this.columns.get(colIndex)); } TempTable tt = this.createIndexTable(indexColumns, unique); tt.readFrom(ois); } }
insertTupleSource = new BatchCollector.BatchProducerTupleSource(qp); table.insert(insertTupleSource, allColumns, false, false, null); table.getTree().compact(); rowCount = table.getRowCount(); Determinism determinism = qp.getContext().getDeterminismLevel(); context.setDeterminismLevel(determinism); table.addIndex(columns, false); table.addIndex(columns, true); CacheHint hint = table.getCacheHint(); if (hint != null && table.getPkLength() > 0) { table.setUpdatable(hint.isUpdatable(false));
working = tempTable.clone(); intermediate = tempTable.clone(); while (working.getRowCount() > 0) { if (building) { return working; iterations++; if (maxIterations > 0 && iterations > maxIterations) { throw new TeiidProcessingException(QueryPlugin.Event.TEIID31158, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31158, maxIterations, tempTable.getMetadataId().getName())); working.truncate(true); TempTable temp = working; working = intermediate; this.working.truncate(true); this.intermediate.truncate(true); tempTable.setUpdatable(false); tempTable.setAllowImplicitIndexing(true); return tempTable;
return table.insert(ts, insert.getVariables(), true, insert.isUpsert(), context); return table.update(crit, update.getChangeList()); if (crit == null) { long rows = table.truncate(false); return CollectionTupleSource.createUpdateCountTupleSource((int)Math.min(Integer.MAX_VALUE, rows)); return table.delete(crit);
long count = this.getRowCount(); return new CollectionTupleSource(Arrays.asList(Collections.nCopies(projectedCols.size(), (int)Math.min(Integer.MAX_VALUE, count))).iterator()); if ((indexTables != null || (!this.updatable && allowImplicitIndexing && condition != null && this.getRowCount() > 2*this.getTree().getPageSize(true))) && (condition != null || orderBy != null) && ii.valueSet.size() != 1) { LogManager.logDetail(LogConstants.CTX_DQP, "Considering indexes on table", this, "for query", projectedCols, condition, orderBy); //$NON-NLS-1$ //$NON-NLS-2$ long rowCost = this.tree.getRowCount(); long bestCost = estimateCost(orderBy, ii, rowCost); if (this.indexTables != null) { for (TempTable table : this.indexTables.values()) { IndexInfo secondary = new IndexInfo(table, projectedCols, condition, orderBy, false); long cost = estimateCost(orderBy, secondary, rowCost); if (cost < bestCost) { ii = secondary; if (createImplicitIndexIfNeeded(condition)) { IndexInfo secondary = new IndexInfo(this.indexTables.values().iterator().next(), projectedCols, condition, orderBy, false); LogManager.logDetail(LogConstants.CTX_DQP, "Created an implicit index ", secondary.table); //$NON-NLS-1$ long cost = estimateCost(orderBy, secondary, rowCost); if (cost < bestCost) { ii = secondary; return ii.table.createTupleSource(projectedCols, condition, orderBy, ii, agg); primary.valueTs = ii.table.createTupleSource(pkColumns, ii.coveredCriteria, orderBy, ii, agg); primary.ordering = null; return createTupleSource(projectedCols, ii.nonCoveredCriteria, null, primary, agg); primary.valueTs = ii.table.createTupleSource(pkColumns,
if (tc != null) { TempTableSynchronization synch = getSynchronization(context); if (synch != null && synch.existingTables.contains(tempTable.getId())) { TempTable result = synch.tables.get(tempTableID); if (result == null) { throw new AssertionError("Expected active transaction"); //$NON-NLS-1$ if (!tempTable.getActive().compareAndSet(0, 1)) { throw new TeiidProcessingException(QueryPlugin.Event.TEIID30227, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30227, tempTableID)); synch.tables.put(tempTableID, tempTable.clone()); } else if (tempTable.getActive().get() != 0) { throw new TeiidProcessingException(QueryPlugin.Event.TEIID30227, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30227, tempTableID)); if (!synch.isCompleted()) { synch.tables.put(tempTableID, tempTable); result.getActive().getAndIncrement();
TempTable tt = contextStore.getTempTable(att.getTempTable()); Assertion.isNotNull(tt, "Table doesn't exist"); //$NON-NLS-1$ tt.setUpdatable(false); if (att.getIndexColumns() != null && tt.getRowCount() > 2*tt.getTree().getPageSize(true)) { for (List<ElementSymbol> cols : att.getIndexColumns()) { tt.addIndex(cols, false);
long count = this.getRowCount(); return new CollectionTupleSource(Arrays.asList(Collections.nCopies(projectedCols.size(), (int)Math.min(Integer.MAX_VALUE, count))).iterator()); LogManager.logDetail(LogConstants.CTX_DQP, "Considering indexes on table", this, "for query", projectedCols, condition, orderBy); //$NON-NLS-1$ //$NON-NLS-2$ long rowCost = this.tree.getRowCount(); long bestCost = estimateCost(orderBy, ii, rowCost); for (TempTable table : this.indexTables.values()) { IndexInfo secondary = new IndexInfo(table, projectedCols, condition, orderBy, false); long cost = estimateCost(orderBy, secondary, rowCost); if (cost < bestCost) { ii = secondary; return ii.table.createTupleSource(projectedCols, condition, orderBy, ii, agg); primary.valueTs = ii.table.createTupleSource(pkColumns, ii.coveredCriteria, orderBy, ii, agg); primary.ordering = null; return createTupleSource(projectedCols, ii.nonCoveredCriteria, null, primary, agg); pkOrderBy.addVariable(elementSymbol); primary.valueTs = ii.table.createTupleSource(pkColumns, ii.coveredCriteria, pkOrderBy, ii, agg); return createTupleSource(projectedCols, ii.nonCoveredCriteria, orderBy, primary, agg); return createTupleSource(projectedCols, condition, orderBy, ii, agg);
@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()); } };
@Override public synchronized void afterCompletion(int status) { completed = true; synchronizations.remove(id); if (transactionMode == TransactionMode.ISOLATE_READS) { for (TempTable table : tables.values()) { table.getActive().decrementAndGet(); } } else { HashSet<TempTable> current = new HashSet<TempTable>(tempTables.values()); current.retainAll(tables.values()); for (TempTable table : current) { table.getActive().set(0); table.getTree().clearClonedFlags(); } } for (TransactionCallback callback : callbacks) { if (status == Status.STATUS_COMMITTED) { callback.commit(); } else { callback.rollback(); } } callbacks.clear(); }
public TupleSource update(Criteria crit, final SetClauseList update) throws TeiidComponentException, ExpressionEvaluationException, TeiidProcessingException { final boolean primaryKeyChangePossible = canChangePrimaryKey(update); final TupleBrowser browser = createTupleBrower(crit, OrderBy.ASC); UpdateProcessor up = new UpdateProcessor(crit, browser, true) {
public TupleSource delete(Criteria crit) throws TeiidComponentException, ExpressionEvaluationException, TeiidProcessingException { final TupleBrowser browser = createTupleBrower(crit, OrderBy.ASC); UpdateProcessor up = new UpdateProcessor(crit, browser, true) { @Override protected void tuplePassed(List tuple) throws ExpressionEvaluationException, BlockedException, TeiidComponentException { browser.removed(); deleteTuple(tuple); } @Override protected void undo(List<?> tuple) throws TeiidComponentException, TeiidProcessingException { insertTuple(tuple, false, true); } }; long updateCount = up.process(); tid.setCardinality(tree.getRowCount()); tid.getTableData().dataModified(updateCount); return CollectionTupleSource.createUpdateCountTupleSource((int)Math.min(Integer.MAX_VALUE, updateCount)); }
public TempTable clone() { lock.readLock().lock(); try { TempTable clone = (TempTable) super.clone(); clone.lock = new ReentrantReadWriteLock(); if (clone.indexTables != null) { clone.indexTables = new LinkedHashMap<List<ElementSymbol>, TempTable>(clone.indexTables); for (Map.Entry<List<ElementSymbol>, TempTable> entry : clone.indexTables.entrySet()) { TempTable indexClone = entry.getValue().clone(); indexClone.lock = clone.lock; entry.setValue(indexClone); } } clone.tree = tree.clone(); clone.activeReaders = new AtomicInteger(); return clone; } catch (CloneNotSupportedException e) { throw new TeiidRuntimeException(e); } finally { lock.readLock().unlock(); } }
@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); } } return tt.createTupleSource(query.getProjectedSymbols(), query.getCriteria(), query.getOrderBy()); } };
this.addIndex(new ArrayList<>(symbols), false); return true;
final TempTable tempTable = new TempTable(id, buffer, columns, create.getPrimaryKey().size(), sessionID); if (add) { tempTables.put(tempTableName, tempTable);