Search in sources :

Example 1 with CompiledUpdateSet

use of io.siddhi.core.table.CompiledUpdateSet in project siddhi by wso2.

the class TestStoreContainingInMemoryTable method update.

@Override
protected void update(CompiledCondition updateCondition, List<Map<String, Object>> updateConditionParameterMaps, Map<String, CompiledExpression> updateSetExpressions, List<Map<String, Object>> updateSetParameterMaps) throws ConnectionUnavailableException {
    MetaStreamEvent tableMetaStreamEvent = new MetaStreamEvent();
    tableMetaStreamEvent.setEventType(MetaStreamEvent.EventType.TABLE);
    TableDefinition matchingTableDefinition = TableDefinition.id("");
    for (Attribute attribute : inMemoryTable.getTableDefinition().getAttributeList()) {
        tableMetaStreamEvent.addOutputData(attribute);
        matchingTableDefinition.attribute(attribute.getName(), attribute.getType());
    }
    tableMetaStreamEvent.addInputDefinition(matchingTableDefinition);
    MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, inMemoryTable.getTableDefinition(), 0);
    UpdateSet updateSet = new UpdateSet();
    for (Attribute attribute : matchingMetaInfoHolder.getMatchingStreamDefinition().getAttributeList()) {
        updateSet.set(new Variable(attribute.getName()), new Variable(attribute.getName()));
    }
    CompiledUpdateSet compiledUpdateSet = inMemoryTable.compileUpdateSet(updateSet, matchingMetaInfoHolder, null, tableMap, null);
    inMemoryTable.update(convForUpdate(updateSetParameterMaps), updateCondition, compiledUpdateSet);
}
Also used : Variable(io.siddhi.query.api.expression.Variable) Attribute(io.siddhi.query.api.definition.Attribute) MatchingMetaInfoHolder(io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) TableDefinition(io.siddhi.query.api.definition.TableDefinition) CompiledUpdateSet(io.siddhi.core.table.CompiledUpdateSet) UpdateSet(io.siddhi.query.api.execution.query.output.stream.UpdateSet) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) CompiledUpdateSet(io.siddhi.core.table.CompiledUpdateSet)

Example 2 with CompiledUpdateSet

use of io.siddhi.core.table.CompiledUpdateSet in project siddhi by wso2.

the class TestStoreContainingInMemoryTable method updateOrAdd.

@Override
protected void updateOrAdd(CompiledCondition updateCondition, List<Map<String, Object>> updateConditionParameterMaps, Map<String, CompiledExpression> updateSetExpressions, List<Map<String, Object>> updateSetParameterMaps, List<Object[]> addingRecords) throws ConnectionUnavailableException {
    MetaStreamEvent tableMetaStreamEvent = new MetaStreamEvent();
    tableMetaStreamEvent.setEventType(MetaStreamEvent.EventType.TABLE);
    TableDefinition matchingTableDefinition = TableDefinition.id("");
    for (Attribute attribute : inMemoryTable.getTableDefinition().getAttributeList()) {
        tableMetaStreamEvent.addOutputData(attribute);
        matchingTableDefinition.attribute(attribute.getName(), attribute.getType());
    }
    tableMetaStreamEvent.addInputDefinition(matchingTableDefinition);
    MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, inMemoryTable.getTableDefinition(), 0);
    UpdateSet updateSet = new UpdateSet();
    for (Attribute attribute : matchingMetaInfoHolder.getMatchingStreamDefinition().getAttributeList()) {
        updateSet.set(new Variable(attribute.getName()), new Variable(attribute.getName()));
    }
    CompiledUpdateSet compiledUpdateSet = inMemoryTable.compileUpdateSet(updateSet, matchingMetaInfoHolder, null, tableMap, null);
    inMemoryTable.updateOrAdd(convForUpdate(updateSetParameterMaps), updateCondition, compiledUpdateSet, new AddingStreamEventExtractor(matchingMetaInfoHolder.getMatchingStreamEventIndex()));
}
Also used : Variable(io.siddhi.query.api.expression.Variable) AddingStreamEventExtractor(io.siddhi.core.util.collection.AddingStreamEventExtractor) Attribute(io.siddhi.query.api.definition.Attribute) MatchingMetaInfoHolder(io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) TableDefinition(io.siddhi.query.api.definition.TableDefinition) CompiledUpdateSet(io.siddhi.core.table.CompiledUpdateSet) UpdateSet(io.siddhi.query.api.execution.query.output.stream.UpdateSet) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) CompiledUpdateSet(io.siddhi.core.table.CompiledUpdateSet)

Example 3 with CompiledUpdateSet

use of io.siddhi.core.table.CompiledUpdateSet in project siddhi by wso2.

the class AbstractRecordTable method compileUpdateSet.

public CompiledUpdateSet compileUpdateSet(UpdateSet updateSet, MatchingMetaInfoHolder matchingMetaInfoHolder, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, SiddhiQueryContext siddhiQueryContext) {
    RecordTableCompiledUpdateSet recordTableCompiledUpdateSet = new RecordTableCompiledUpdateSet();
    Map<String, ExpressionExecutor> parentExecutorMap = new HashMap<>();
    for (UpdateSet.SetAttribute setAttribute : updateSet.getSetAttributeList()) {
        ExpressionExecutor inMemoryAssignmentExecutor = ExpressionParser.parseExpression(setAttribute.getAssignmentExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        ExpressionBuilder expressionBuilder = new ExpressionBuilder(setAttribute.getAssignmentExpression(), matchingMetaInfoHolder, variableExpressionExecutors, tableMap, null, inMemoryAssignmentExecutor, siddhiQueryContext);
        CompiledExpression compiledExpression = compileSetAttribute(expressionBuilder);
        recordTableCompiledUpdateSet.put(setAttribute.getTableVariable().getAttributeName(), compiledExpression);
        Map<String, ExpressionExecutor> expressionExecutorMap = expressionBuilder.getVariableExpressionExecutorMap();
        parentExecutorMap.putAll(expressionExecutorMap);
    }
    recordTableCompiledUpdateSet.setExpressionExecutorMap(parentExecutorMap);
    return recordTableCompiledUpdateSet;
}
Also used : VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) HashMap(java.util.HashMap) CompiledUpdateSet(io.siddhi.core.table.CompiledUpdateSet) UpdateSet(io.siddhi.query.api.execution.query.output.stream.UpdateSet) CompiledExpression(io.siddhi.core.util.collection.operator.CompiledExpression)

Example 4 with CompiledUpdateSet

use of io.siddhi.core.table.CompiledUpdateSet in project siddhi by wso2.

the class OutputParser method constructOutputCallback.

public static OutputCallback constructOutputCallback(final OutputStream outStream, StreamDefinition outputStreamDefinition, Map<String, Table> tableMap, Map<String, Window> eventWindowMap, boolean convertToStreamEvent, SiddhiQueryContext siddhiQueryContext) {
    String id = outStream.getId();
    Table table = null;
    Window window = null;
    if (id != null) {
        table = tableMap.get(id);
        window = eventWindowMap.get(id);
    }
    StreamEventFactory streamEventFactory = null;
    StreamEventConverter streamEventConverter = null;
    MetaStreamEvent tableMetaStreamEvent = null;
    if (table != null) {
        tableMetaStreamEvent = new MetaStreamEvent();
        tableMetaStreamEvent.setEventType(MetaStreamEvent.EventType.TABLE);
        TableDefinition matchingTableDefinition = TableDefinition.id("");
        for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
            tableMetaStreamEvent.addOutputData(attribute);
            matchingTableDefinition.attribute(attribute.getName(), attribute.getType());
        }
        matchingTableDefinition.setQueryContextStartIndex(outStream.getQueryContextStartIndex());
        matchingTableDefinition.setQueryContextEndIndex(outStream.getQueryContextEndIndex());
        tableMetaStreamEvent.addInputDefinition(matchingTableDefinition);
        streamEventFactory = new StreamEventFactory(tableMetaStreamEvent);
        streamEventConverter = new ZeroStreamEventConverter();
    }
    // Construct CallBack
    if (outStream instanceof InsertIntoStream || outStream instanceof ReturnStream) {
        if (window != null) {
            if (!siddhiQueryContext.isPartitioned()) {
                return new InsertIntoWindowCallback(window, outputStreamDefinition, siddhiQueryContext.getName());
            } else {
                return new InsertIntoWindowEndPartitionCallback(window, outputStreamDefinition, siddhiQueryContext.getName());
            }
        } else if (table != null) {
            DefinitionParserHelper.validateOutputStream(outputStreamDefinition, table.getTableDefinition());
            return new InsertIntoTableCallback(table, outputStreamDefinition, convertToStreamEvent, streamEventFactory, streamEventConverter, siddhiQueryContext.getName());
        } else {
            if (!siddhiQueryContext.isPartitioned() || outputStreamDefinition.getId().startsWith("#")) {
                return new InsertIntoStreamCallback(outputStreamDefinition, siddhiQueryContext.getName());
            } else {
                return new InsertIntoStreamEndPartitionCallback(outputStreamDefinition, siddhiQueryContext.getName());
            }
        }
    } else if (outStream instanceof DeleteStream || outStream instanceof UpdateStream || outStream instanceof UpdateOrInsertStream) {
        if (table != null) {
            if (outStream instanceof UpdateStream) {
                if (((UpdateStream) outStream).getUpdateSet() == null) {
                    TableDefinition tableDefinition = table.getTableDefinition();
                    for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
                        if (!tableDefinition.getAttributeList().contains(attribute)) {
                            throw new SiddhiAppCreationException("Attribute " + attribute + " does not exist on " + "Event Table " + tableDefinition, outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
                        }
                    }
                }
            }
            if (outStream instanceof UpdateOrInsertStream) {
                TableDefinition tableDefinition = table.getTableDefinition();
                for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
                    if (!tableDefinition.getAttributeList().contains(attribute)) {
                        throw new SiddhiAppCreationException("Attribute " + attribute + " does not exist on " + "Event Table " + tableDefinition, outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
                    }
                }
            }
            if (outStream instanceof DeleteStream) {
                try {
                    MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
                    CompiledCondition compiledCondition = table.compileCondition((((DeleteStream) outStream).getOnDeleteExpression()), matchingMetaInfoHolder, null, tableMap, siddhiQueryContext);
                    StateEventFactory stateEventFactory = new StateEventFactory(matchingMetaInfoHolder.getMetaStateEvent());
                    return new DeleteTableCallback(table, compiledCondition, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventFactory, streamEventFactory, streamEventConverter, siddhiQueryContext.getName());
                } catch (SiddhiAppValidationException e) {
                    throw new SiddhiAppCreationException("Cannot create delete for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiQueryContext.getName(), siddhiQueryContext.getSiddhiAppContext().getSiddhiAppString());
                }
            } else if (outStream instanceof UpdateStream) {
                try {
                    MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
                    CompiledCondition compiledCondition = table.compileCondition((((UpdateStream) outStream).getOnUpdateExpression()), matchingMetaInfoHolder, null, tableMap, siddhiQueryContext);
                    UpdateSet updateSet = ((UpdateStream) outStream).getUpdateSet();
                    if (updateSet == null) {
                        updateSet = new UpdateSet();
                        for (Attribute attribute : matchingMetaInfoHolder.getMatchingStreamDefinition().getAttributeList()) {
                            updateSet.set(new Variable(attribute.getName()), new Variable(attribute.getName()));
                        }
                    }
                    CompiledUpdateSet compiledUpdateSet = table.compileUpdateSet(updateSet, matchingMetaInfoHolder, null, tableMap, siddhiQueryContext);
                    StateEventFactory stateEventFactory = new StateEventFactory(matchingMetaInfoHolder.getMetaStateEvent());
                    return new UpdateTableCallback(table, compiledCondition, compiledUpdateSet, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventFactory, streamEventFactory, streamEventConverter, siddhiQueryContext.getName());
                } catch (SiddhiAppValidationException e) {
                    throw new SiddhiAppCreationException("Cannot create update for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiQueryContext.getSiddhiAppContext());
                }
            } else {
                DefinitionParserHelper.validateOutputStream(outputStreamDefinition, table.getTableDefinition());
                try {
                    MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
                    CompiledCondition compiledCondition = table.compileCondition((((UpdateOrInsertStream) outStream).getOnUpdateExpression()), matchingMetaInfoHolder, null, tableMap, siddhiQueryContext);
                    UpdateSet updateSet = ((UpdateOrInsertStream) outStream).getUpdateSet();
                    if (updateSet == null) {
                        updateSet = new UpdateSet();
                        for (Attribute attribute : matchingMetaInfoHolder.getMatchingStreamDefinition().getAttributeList()) {
                            updateSet.set(new Variable(attribute.getName()), new Variable(attribute.getName()));
                        }
                    }
                    CompiledUpdateSet compiledUpdateSet = table.compileUpdateSet(updateSet, matchingMetaInfoHolder, null, tableMap, siddhiQueryContext);
                    StateEventFactory stateEventFactory = new StateEventFactory(matchingMetaInfoHolder.getMetaStateEvent());
                    return new UpdateOrInsertTableCallback(table, compiledCondition, compiledUpdateSet, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventFactory, streamEventFactory, streamEventConverter, siddhiQueryContext.getName());
                } catch (SiddhiAppValidationException e) {
                    throw new SiddhiAppCreationException("Cannot create update or insert into for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiQueryContext.getSiddhiAppContext());
                }
            }
        } else {
            throw new SiddhiAppCreationException("Event table with id :" + id + " does not exist", outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
        }
    } else {
        throw new SiddhiAppCreationException(outStream.getClass().getName() + " not supported", outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
    }
}
Also used : InsertIntoWindowEndPartitionCallback(io.siddhi.core.query.output.callback.InsertIntoWindowEndPartitionCallback) Variable(io.siddhi.query.api.expression.Variable) Attribute(io.siddhi.query.api.definition.Attribute) InsertIntoWindowCallback(io.siddhi.core.query.output.callback.InsertIntoWindowCallback) UpdateStream(io.siddhi.query.api.execution.query.output.stream.UpdateStream) ZeroStreamEventConverter(io.siddhi.core.event.stream.converter.ZeroStreamEventConverter) UpdateTableCallback(io.siddhi.core.query.output.callback.UpdateTableCallback) DeleteStream(io.siddhi.query.api.execution.query.output.stream.DeleteStream) InsertIntoStreamEndPartitionCallback(io.siddhi.core.query.output.callback.InsertIntoStreamEndPartitionCallback) UpdateOrInsertTableCallback(io.siddhi.core.query.output.callback.UpdateOrInsertTableCallback) TableDefinition(io.siddhi.query.api.definition.TableDefinition) InsertIntoTableCallback(io.siddhi.core.query.output.callback.InsertIntoTableCallback) Window(io.siddhi.core.window.Window) Table(io.siddhi.core.table.Table) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) StreamEventFactory(io.siddhi.core.event.stream.StreamEventFactory) StreamEventConverter(io.siddhi.core.event.stream.converter.StreamEventConverter) ZeroStreamEventConverter(io.siddhi.core.event.stream.converter.ZeroStreamEventConverter) InsertIntoStream(io.siddhi.query.api.execution.query.output.stream.InsertIntoStream) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) ReturnStream(io.siddhi.query.api.execution.query.output.stream.ReturnStream) InsertIntoStreamCallback(io.siddhi.core.query.output.callback.InsertIntoStreamCallback) CompiledUpdateSet(io.siddhi.core.table.CompiledUpdateSet) UpdateOrInsertStream(io.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream) CompiledCondition(io.siddhi.core.util.collection.operator.CompiledCondition) MatchingMetaInfoHolder(io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) DeleteTableCallback(io.siddhi.core.query.output.callback.DeleteTableCallback) StateEventFactory(io.siddhi.core.event.state.StateEventFactory) UpdateSet(io.siddhi.query.api.execution.query.output.stream.UpdateSet) CompiledUpdateSet(io.siddhi.core.table.CompiledUpdateSet) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Aggregations

CompiledUpdateSet (io.siddhi.core.table.CompiledUpdateSet)4 UpdateSet (io.siddhi.query.api.execution.query.output.stream.UpdateSet)4 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)3 MatchingMetaInfoHolder (io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)3 Attribute (io.siddhi.query.api.definition.Attribute)3 TableDefinition (io.siddhi.query.api.definition.TableDefinition)3 Variable (io.siddhi.query.api.expression.Variable)3 StateEventFactory (io.siddhi.core.event.state.StateEventFactory)1 StreamEventFactory (io.siddhi.core.event.stream.StreamEventFactory)1 StreamEventConverter (io.siddhi.core.event.stream.converter.StreamEventConverter)1 ZeroStreamEventConverter (io.siddhi.core.event.stream.converter.ZeroStreamEventConverter)1 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)1 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)1 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)1 DeleteTableCallback (io.siddhi.core.query.output.callback.DeleteTableCallback)1 InsertIntoStreamCallback (io.siddhi.core.query.output.callback.InsertIntoStreamCallback)1 InsertIntoStreamEndPartitionCallback (io.siddhi.core.query.output.callback.InsertIntoStreamEndPartitionCallback)1 InsertIntoTableCallback (io.siddhi.core.query.output.callback.InsertIntoTableCallback)1 InsertIntoWindowCallback (io.siddhi.core.query.output.callback.InsertIntoWindowCallback)1 InsertIntoWindowEndPartitionCallback (io.siddhi.core.query.output.callback.InsertIntoWindowEndPartitionCallback)1