Search in sources :

Example 1 with InsertIntoWindowCallback

use of io.siddhi.core.query.output.callback.InsertIntoWindowCallback in project siddhi by wso2.

the class PartitionRuntimeImpl method addQuery.

public void addQuery(QueryRuntimeImpl metaQueryRuntime) {
    Query query = metaQueryRuntime.getQuery();
    if (query.getOutputStream() instanceof InsertIntoStream && metaQueryRuntime.getOutputCallback() instanceof InsertIntoStreamCallback) {
        InsertIntoStreamCallback insertIntoStreamCallback = (InsertIntoStreamCallback) metaQueryRuntime.getOutputCallback();
        StreamDefinition streamDefinition = insertIntoStreamCallback.getOutputStreamDefinition();
        String id = streamDefinition.getId();
        if (((InsertIntoStream) query.getOutputStream()).isInnerStream()) {
            metaQueryRuntime.setToLocalStream(true);
            localStreamDefinitionMap.putIfAbsent(id, streamDefinition);
            DefinitionParserHelper.validateOutputStream(streamDefinition, localStreamDefinitionMap.get(id));
            StreamJunction outputStreamJunction = localStreamJunctionMap.get(id);
            if (outputStreamJunction == null) {
                outputStreamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), null, siddhiAppContext);
                localStreamJunctionMap.putIfAbsent(id, outputStreamJunction);
            }
            insertIntoStreamCallback.init(localStreamJunctionMap.get(id));
        } else {
            streamDefinitionMap.putIfAbsent(id, streamDefinition);
            DefinitionParserHelper.validateOutputStream(streamDefinition, streamDefinitionMap.get(id));
            StreamJunction outputStreamJunction = streamJunctionMap.get(id);
            if (outputStreamJunction == null) {
                outputStreamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), null, siddhiAppContext);
                streamJunctionMap.putIfAbsent(id, outputStreamJunction);
            }
            insertIntoStreamCallback.init(streamJunctionMap.get(id));
        }
    } else if (query.getOutputStream() instanceof InsertIntoStream && metaQueryRuntime.getOutputCallback() instanceof InsertIntoWindowCallback) {
        InsertIntoWindowCallback insertIntoWindowCallback = (InsertIntoWindowCallback) metaQueryRuntime.getOutputCallback();
        StreamDefinition streamDefinition = insertIntoWindowCallback.getOutputStreamDefinition();
        String id = streamDefinition.getId();
        DefinitionParserHelper.validateOutputStream(streamDefinition, windowDefinitionMap.get(id));
        StreamJunction outputStreamJunction = streamJunctionMap.get(id);
        if (outputStreamJunction == null) {
            outputStreamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), null, siddhiAppContext);
            streamJunctionMap.putIfAbsent(id, outputStreamJunction);
        }
        insertIntoWindowCallback.getWindow().setPublisher(streamJunctionMap.get(insertIntoWindowCallback.getOutputStreamDefinition().getId()).constructPublisher());
    }
    if (metaQueryRuntime.isFromLocalStream()) {
        for (int i = 0; i < metaQueryRuntime.getStreamRuntime().getSingleStreamRuntimes().size(); i++) {
            String streamId = metaQueryRuntime.getStreamRuntime().getSingleStreamRuntimes().get(i).getProcessStreamReceiver().getStreamId();
            if (streamId.startsWith("#")) {
                StreamDefinition streamDefinition = (StreamDefinition) localStreamDefinitionMap.get(streamId);
                StreamJunction streamJunction = localStreamJunctionMap.get(streamId);
                if (streamJunction == null) {
                    streamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), null, siddhiAppContext);
                    localStreamJunctionMap.put(streamId, streamJunction);
                }
                streamJunction.subscribe(metaQueryRuntime.getStreamRuntime().getSingleStreamRuntimes().get(i).getProcessStreamReceiver());
            }
        }
    }
    queryRuntimeList.add(metaQueryRuntime);
}
Also used : Query(io.siddhi.query.api.execution.query.Query) StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) InsertIntoWindowCallback(io.siddhi.core.query.output.callback.InsertIntoWindowCallback) InsertIntoStream(io.siddhi.query.api.execution.query.output.stream.InsertIntoStream) StreamJunction(io.siddhi.core.stream.StreamJunction) InsertIntoStreamCallback(io.siddhi.core.query.output.callback.InsertIntoStreamCallback)

Example 2 with InsertIntoWindowCallback

use of io.siddhi.core.query.output.callback.InsertIntoWindowCallback in project siddhi by wso2.

the class SiddhiAppRuntimeBuilder method addQuery.

public String addQuery(QueryRuntimeImpl queryRuntime) {
    QueryRuntime oldQueryRuntime = queryProcessorMap.put(queryRuntime.getQueryId(), queryRuntime);
    if (oldQueryRuntime != null) {
        throw new SiddhiAppCreationException("Multiple queries with name '" + queryRuntime.getQueryId() + "' defined in Siddhi App '" + siddhiAppContext.getName() + "'", queryRuntime.getQuery().getQueryContextStartIndex(), queryRuntime.getQuery().getQueryContextEndIndex());
    }
    StreamRuntime streamRuntime = queryRuntime.getStreamRuntime();
    for (SingleStreamRuntime singleStreamRuntime : streamRuntime.getSingleStreamRuntimes()) {
        ProcessStreamReceiver processStreamReceiver = singleStreamRuntime.getProcessStreamReceiver();
        if (processStreamReceiver.toStream()) {
            StreamJunction streamJunction = streamJunctionMap.get(processStreamReceiver.getStreamId());
            if (streamJunction != null) {
                streamJunction.subscribe(processStreamReceiver);
            } else {
                throw new SiddhiAppCreationException("Expecting a stream, but provided '" + processStreamReceiver.getStreamId() + "' is not a stream");
            }
        }
    }
    OutputCallback outputCallback = queryRuntime.getOutputCallback();
    if (outputCallback != null && outputCallback instanceof InsertIntoStreamCallback) {
        InsertIntoStreamCallback insertIntoStreamCallback = (InsertIntoStreamCallback) outputCallback;
        StreamDefinition streamDefinition = insertIntoStreamCallback.getOutputStreamDefinition();
        streamDefinitionMap.putIfAbsent(streamDefinition.getId(), streamDefinition);
        DefinitionParserHelper.validateOutputStream(streamDefinition, streamDefinitionMap.get(streamDefinition.getId()));
        StreamJunction outputStreamJunction = streamJunctionMap.get(streamDefinition.getId());
        if (outputStreamJunction == null) {
            outputStreamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), null, siddhiAppContext);
            streamJunctionMap.putIfAbsent(streamDefinition.getId(), outputStreamJunction);
        }
        insertIntoStreamCallback.init(streamJunctionMap.get(insertIntoStreamCallback.getOutputStreamDefinition().getId()));
    } else if (outputCallback != null && outputCallback instanceof InsertIntoWindowCallback) {
        InsertIntoWindowCallback insertIntoWindowCallback = (InsertIntoWindowCallback) outputCallback;
        StreamDefinition streamDefinition = insertIntoWindowCallback.getOutputStreamDefinition();
        windowDefinitionMap.putIfAbsent(streamDefinition.getId(), streamDefinition);
        DefinitionParserHelper.validateOutputStream(streamDefinition, windowDefinitionMap.get(streamDefinition.getId()));
        StreamJunction outputStreamJunction = streamJunctionMap.get(streamDefinition.getId());
        if (outputStreamJunction == null) {
            outputStreamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), null, siddhiAppContext);
            streamJunctionMap.putIfAbsent(streamDefinition.getId(), outputStreamJunction);
        }
        insertIntoWindowCallback.getWindow().setPublisher(streamJunctionMap.get(insertIntoWindowCallback.getOutputStreamDefinition().getId()).constructPublisher());
    }
    return queryRuntime.getQueryId();
}
Also used : ProcessStreamReceiver(io.siddhi.core.query.input.ProcessStreamReceiver) StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) InsertIntoWindowCallback(io.siddhi.core.query.output.callback.InsertIntoWindowCallback) QueryRuntime(io.siddhi.core.query.QueryRuntime) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) SingleStreamRuntime(io.siddhi.core.query.input.stream.single.SingleStreamRuntime) StreamJunction(io.siddhi.core.stream.StreamJunction) StreamRuntime(io.siddhi.core.query.input.stream.StreamRuntime) SingleStreamRuntime(io.siddhi.core.query.input.stream.single.SingleStreamRuntime) InsertIntoStreamCallback(io.siddhi.core.query.output.callback.InsertIntoStreamCallback) OutputCallback(io.siddhi.core.query.output.callback.OutputCallback)

Example 3 with InsertIntoWindowCallback

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

InsertIntoStreamCallback (io.siddhi.core.query.output.callback.InsertIntoStreamCallback)3 InsertIntoWindowCallback (io.siddhi.core.query.output.callback.InsertIntoWindowCallback)3 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)2 StreamJunction (io.siddhi.core.stream.StreamJunction)2 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)2 InsertIntoStream (io.siddhi.query.api.execution.query.output.stream.InsertIntoStream)2 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 QueryRuntime (io.siddhi.core.query.QueryRuntime)1 ProcessStreamReceiver (io.siddhi.core.query.input.ProcessStreamReceiver)1 StreamRuntime (io.siddhi.core.query.input.stream.StreamRuntime)1 SingleStreamRuntime (io.siddhi.core.query.input.stream.single.SingleStreamRuntime)1 DeleteTableCallback (io.siddhi.core.query.output.callback.DeleteTableCallback)1 InsertIntoStreamEndPartitionCallback (io.siddhi.core.query.output.callback.InsertIntoStreamEndPartitionCallback)1 InsertIntoTableCallback (io.siddhi.core.query.output.callback.InsertIntoTableCallback)1 InsertIntoWindowEndPartitionCallback (io.siddhi.core.query.output.callback.InsertIntoWindowEndPartitionCallback)1 OutputCallback (io.siddhi.core.query.output.callback.OutputCallback)1