Search in sources :

Example 6 with StreamRuntime

use of org.ballerinalang.siddhi.core.query.input.stream.StreamRuntime in project ballerina by ballerina-lang.

the class SiddhiAppRuntimeBuilder method addQuery.

public String addQuery(QueryRuntime 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 streamJuction = streamJunctionMap.get(processStreamReceiver.getStreamId());
            if (streamJuction != null) {
                streamJuction.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(), 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(), siddhiAppContext);
            streamJunctionMap.putIfAbsent(streamDefinition.getId(), outputStreamJunction);
        }
        insertIntoWindowCallback.getWindow().setPublisher(streamJunctionMap.get(insertIntoWindowCallback.getOutputStreamDefinition().getId()).constructPublisher());
    }
    return queryRuntime.getQueryId();
}
Also used : ProcessStreamReceiver(org.ballerinalang.siddhi.core.query.input.ProcessStreamReceiver) StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) InsertIntoWindowCallback(org.ballerinalang.siddhi.core.query.output.callback.InsertIntoWindowCallback) QueryRuntime(org.ballerinalang.siddhi.core.query.QueryRuntime) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) SingleStreamRuntime(org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime) StreamJunction(org.ballerinalang.siddhi.core.stream.StreamJunction) StreamRuntime(org.ballerinalang.siddhi.core.query.input.stream.StreamRuntime) SingleStreamRuntime(org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime) InsertIntoStreamCallback(org.ballerinalang.siddhi.core.query.output.callback.InsertIntoStreamCallback) OutputCallback(org.ballerinalang.siddhi.core.query.output.callback.OutputCallback)

Aggregations

StreamRuntime (org.ballerinalang.siddhi.core.query.input.stream.StreamRuntime)6 QueryRuntime (org.ballerinalang.siddhi.core.query.QueryRuntime)4 SingleStreamRuntime (org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime)4 OutputCallback (org.ballerinalang.siddhi.core.query.output.callback.OutputCallback)4 ArrayList (java.util.ArrayList)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 LockWrapper (org.ballerinalang.siddhi.core.util.lock.LockWrapper)3 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)2 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)2 VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)2 OutputRateLimiter (org.ballerinalang.siddhi.core.query.output.ratelimit.OutputRateLimiter)2 QuerySelector (org.ballerinalang.siddhi.core.query.selector.QuerySelector)2 StreamJunction (org.ballerinalang.siddhi.core.stream.StreamJunction)2 LatencyTracker (org.ballerinalang.siddhi.core.util.statistics.LatencyTracker)2 Element (org.ballerinalang.siddhi.query.api.annotation.Element)2 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)2 AggregationRuntime (org.ballerinalang.siddhi.core.aggregation.AggregationRuntime)1 IncrementalAggregationProcessor (org.ballerinalang.siddhi.core.aggregation.IncrementalAggregationProcessor)1 IncrementalExecutor (org.ballerinalang.siddhi.core.aggregation.IncrementalExecutor)1 RecreateInMemoryData (org.ballerinalang.siddhi.core.aggregation.RecreateInMemoryData)1