Search in sources :

Example 1 with InsertIntoWindowEndPartitionCallback

use of io.siddhi.core.query.output.callback.InsertIntoWindowEndPartitionCallback 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

StateEventFactory (io.siddhi.core.event.state.StateEventFactory)1 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)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 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 UpdateOrInsertTableCallback (io.siddhi.core.query.output.callback.UpdateOrInsertTableCallback)1 UpdateTableCallback (io.siddhi.core.query.output.callback.UpdateTableCallback)1 CompiledUpdateSet (io.siddhi.core.table.CompiledUpdateSet)1 Table (io.siddhi.core.table.Table)1 CompiledCondition (io.siddhi.core.util.collection.operator.CompiledCondition)1 MatchingMetaInfoHolder (io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)1 Window (io.siddhi.core.window.Window)1 Attribute (io.siddhi.query.api.definition.Attribute)1