Search in sources :

Example 1 with MatTableInfo

use of org.teiid.query.tempdata.GlobalTableStoreImpl.MatTableInfo in project teiid by teiid.

the class TestMaterialization method testReadWrite.

@Test
public void testReadWrite() throws Exception {
    execute("SELECT * from vgroup3 where x = 'one'", Arrays.asList("one", "zne"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    String matTableName = RelationalPlanner.MAT_PREFIX + "MATVIEW.VGROUP3";
    this.globalStore.getState(matTableName, baos);
    MatTableInfo matTableInfo = this.globalStore.getMatTableInfo(matTableName);
    long time = matTableInfo.getUpdateTime();
    this.globalStore.failedLoad(matTableName);
    this.globalStore.setState(matTableName, new ByteArrayInputStream(baos.toByteArray()));
    assertEquals(time, matTableInfo.getUpdateTime());
    execute("SELECT * from vgroup3 where x = 'one'", Arrays.asList("one", "zne"));
    execute("select lookup('mattable.info', 'e1', 'e2', 5)", Arrays.asList((String) null));
    baos = new ByteArrayOutputStream();
    String codeTableName = "#CODE_MATTABLE.INFO.E2.E1";
    this.globalStore.getState(codeTableName, baos);
    this.globalStore.setState(codeTableName, new ByteArrayInputStream(baos.toByteArray()));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MatTableInfo(org.teiid.query.tempdata.GlobalTableStoreImpl.MatTableInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 2 with MatTableInfo

use of org.teiid.query.tempdata.GlobalTableStoreImpl.MatTableInfo 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());
        }
    };
}
Also used : DQPWorkContext(org.teiid.dqp.internal.process.DQPWorkContext) RequestWorkItem(org.teiid.dqp.internal.process.RequestWorkItem) CommandContext(org.teiid.query.util.CommandContext) TempMetadataID(org.teiid.query.metadata.TempMetadataID) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException) TeiidComponentException(org.teiid.core.TeiidComponentException) TransformationException(org.teiid.core.types.TransformationException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) BlockedException(org.teiid.common.buffer.BlockedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleSource(org.teiid.common.buffer.TupleSource) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) MatTableInfo(org.teiid.query.tempdata.GlobalTableStoreImpl.MatTableInfo) Future(java.util.concurrent.Future)

Example 3 with MatTableInfo

use of org.teiid.query.tempdata.GlobalTableStoreImpl.MatTableInfo in project teiid by teiid.

the class TempTableDataManager method updateMatviewRows.

private TupleSource updateMatviewRows(final CommandContext context, final QueryMetadataInterface metadata, final Object groupID, final GlobalTableStore globalStore, final String matViewName, List<?> ids, Object[][] params) throws QueryProcessingException, TeiidComponentException, QueryMetadataException, TransformationException {
    final String matTableName = RelationalPlanner.MAT_PREFIX + matViewName.toUpperCase();
    MatTableInfo info = globalStore.getMatTableInfo(matTableName);
    if (!info.isValid()) {
        return CollectionTupleSource.createUpdateCountTupleSource(-1);
    }
    TempTable tempTable = globalStore.getTempTable(matTableName);
    if (!tempTable.isUpdatable()) {
        throw new QueryProcessingException(QueryPlugin.Event.TEIID30232, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30232, matViewName));
    }
    List<Object[]> converted = new ArrayList<Object[]>();
    for (Object[] param : params) {
        if (param == null || ids.size() != param.length) {
            throw new QueryProcessingException(QueryPlugin.Event.TEIID30231, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30231, matViewName, ids.size(), param == null ? 0 : param.length));
        }
        final Object[] vals = new Object[param.length];
        for (int i = 0; i < ids.size(); i++) {
            Object value = param[i];
            String targetTypeName = metadata.getElementRuntimeTypeName(ids.get(i));
            value = DataTypeManager.transformValue(value, DataTypeManager.getDataTypeClass(targetTypeName));
            vals[i] = value;
        }
        converted.add(vals);
    }
    final Iterator<Object[]> paramIter = converted.iterator();
    Iterator<?> iter = ids.iterator();
    StringBuilder criteria = new StringBuilder();
    for (int i = 0; i < ids.size(); i++) {
        Object id = iter.next();
        if (i != 0) {
            // $NON-NLS-1$
            criteria.append(" AND ");
        }
        // $NON-NLS-1$
        criteria.append(metadata.getFullName(id)).append(" = ?");
    }
    final String queryString = // $NON-NLS-1$
    Reserved.SELECT + " * " + Reserved.FROM + ' ' + matViewName + ' ' + Reserved.WHERE + ' ' + criteria.toString() + ' ' + Reserved.OPTION + ' ' + Reserved.NOCACHE;
    return new ProxyTupleSource() {

        private QueryProcessor qp;

        private TupleSource ts;

        private Object[] params;

        private int count;

        @Override
        protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
            while (true) {
                if (qp == null) {
                    params = paramIter.next();
                    LogManager.logInfo(LogConstants.CTX_MATVIEWS, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30012, matViewName, Arrays.toString(params)));
                    qp = context.getQueryProcessorFactory().createQueryProcessor(queryString, matViewName.toUpperCase(), context, params);
                    ts = new BatchCollector.BatchProducerTupleSource(qp);
                }
                List<?> tuple = ts.nextTuple();
                boolean delete = false;
                if (tuple == null) {
                    delete = true;
                    tuple = Arrays.asList(params);
                } else {
                    // ensure the list is serializable
                    tuple = new ArrayList<Object>(tuple);
                }
                List<?> result = globalStore.updateMatViewRow(matTableName, tuple, delete);
                if (result != null) {
                    count++;
                }
                if (eventDistributor != null) {
                    eventDistributor.updateMatViewRow(context.getVdbName(), context.getVdbVersion(), metadata.getName(metadata.getModelID(groupID)), metadata.getName(groupID), tuple, delete);
                }
                qp.closeProcessing();
                qp = null;
                ts = null;
                if (!paramIter.hasNext()) {
                    break;
                }
            }
            return CollectionTupleSource.createUpdateCountTupleSource(count);
        }

        @Override
        public void closeSource() {
            super.closeSource();
            if (qp != null) {
                qp.closeProcessing();
            }
        }
    };
}
Also used : ArrayList(java.util.ArrayList) QueryProcessor(org.teiid.query.processor.QueryProcessor) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleSource(org.teiid.common.buffer.TupleSource) MatTableInfo(org.teiid.query.tempdata.GlobalTableStoreImpl.MatTableInfo) BatchCollector(org.teiid.query.processor.BatchCollector) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException)

Aggregations

MatTableInfo (org.teiid.query.tempdata.GlobalTableStoreImpl.MatTableInfo)3 QueryProcessingException (org.teiid.api.exception.query.QueryProcessingException)2 TupleSource (org.teiid.common.buffer.TupleSource)2 CollectionTupleSource (org.teiid.query.processor.CollectionTupleSource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 Future (java.util.concurrent.Future)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 Test (org.junit.Test)1 ExpressionEvaluationException (org.teiid.api.exception.query.ExpressionEvaluationException)1 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)1 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)1 QueryValidatorException (org.teiid.api.exception.query.QueryValidatorException)1 BlockedException (org.teiid.common.buffer.BlockedException)1 TeiidComponentException (org.teiid.core.TeiidComponentException)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)1 TransformationException (org.teiid.core.types.TransformationException)1 DQPWorkContext (org.teiid.dqp.internal.process.DQPWorkContext)1