Search in sources :

Example 1 with ProcessStreamReceiver

use of org.wso2.siddhi.core.query.input.ProcessStreamReceiver in project siddhi by wso2.

the class SingleStreamRuntime method clone.

@Override
public StreamRuntime clone(String key) {
    ProcessStreamReceiver clonedProcessStreamReceiver = this.processStreamReceiver.clone(key);
    EntryValveProcessor entryValveProcessor = null;
    SchedulingProcessor schedulingProcessor;
    Processor clonedProcessorChain = null;
    if (processorChain != null) {
        if (!(processorChain instanceof QuerySelector || processorChain instanceof OutputRateLimiter)) {
            clonedProcessorChain = processorChain.cloneProcessor(key);
            if (clonedProcessorChain instanceof EntryValveProcessor) {
                entryValveProcessor = (EntryValveProcessor) clonedProcessorChain;
            }
        }
        Processor processor = processorChain.getNextProcessor();
        while (processor != null) {
            if (!(processor instanceof QuerySelector || processor instanceof OutputRateLimiter)) {
                Processor clonedProcessor = processor.cloneProcessor(key);
                clonedProcessorChain.setToLast(clonedProcessor);
                if (clonedProcessor instanceof EntryValveProcessor) {
                    entryValveProcessor = (EntryValveProcessor) clonedProcessor;
                } else if (clonedProcessor instanceof SchedulingProcessor) {
                    schedulingProcessor = (SchedulingProcessor) clonedProcessor;
                    schedulingProcessor.setScheduler(((SchedulingProcessor) processor).getScheduler().clone(key, entryValveProcessor));
                }
            }
            processor = processor.getNextProcessor();
        }
    }
    return new SingleStreamRuntime(clonedProcessStreamReceiver, clonedProcessorChain, metaComplexEvent);
}
Also used : ProcessStreamReceiver(org.wso2.siddhi.core.query.input.ProcessStreamReceiver) SchedulingProcessor(org.wso2.siddhi.core.query.processor.SchedulingProcessor) Processor(org.wso2.siddhi.core.query.processor.Processor) SchedulingProcessor(org.wso2.siddhi.core.query.processor.SchedulingProcessor) OutputRateLimiter(org.wso2.siddhi.core.query.output.ratelimit.OutputRateLimiter) QuerySelector(org.wso2.siddhi.core.query.selector.QuerySelector)

Example 2 with ProcessStreamReceiver

use of org.wso2.siddhi.core.query.input.ProcessStreamReceiver in project siddhi by wso2.

the class JoinInputStreamParser method parseInputStream.

public static StreamRuntime parseInputStream(JoinInputStream joinInputStream, SiddhiAppContext siddhiAppContext, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, Map<String, Window> windowMap, Map<String, AggregationRuntime> aggregationMap, List<VariableExpressionExecutor> executors, LatencyTracker latencyTracker, boolean outputExpectsExpiredEvents, String queryName) {
    try {
        ProcessStreamReceiver leftProcessStreamReceiver;
        ProcessStreamReceiver rightProcessStreamReceiver;
        MetaStreamEvent leftMetaStreamEvent = new MetaStreamEvent();
        MetaStreamEvent rightMetaStreamEvent = new MetaStreamEvent();
        String leftInputStreamId = ((SingleInputStream) joinInputStream.getLeftInputStream()).getStreamId();
        String rightInputStreamId = ((SingleInputStream) joinInputStream.getRightInputStream()).getStreamId();
        boolean leftOuterJoinProcessor = false;
        boolean rightOuterJoinProcessor = false;
        if (joinInputStream.getAllStreamIds().size() == 2) {
            setEventType(streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, leftMetaStreamEvent, leftInputStreamId);
            setEventType(streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, rightMetaStreamEvent, rightInputStreamId);
            leftProcessStreamReceiver = new ProcessStreamReceiver(leftInputStreamId, latencyTracker, queryName, siddhiAppContext);
            leftProcessStreamReceiver.setBatchProcessingAllowed(leftMetaStreamEvent.getEventType() == WINDOW);
            rightProcessStreamReceiver = new ProcessStreamReceiver(rightInputStreamId, latencyTracker, queryName, siddhiAppContext);
            rightProcessStreamReceiver.setBatchProcessingAllowed(rightMetaStreamEvent.getEventType() == WINDOW);
            if ((leftMetaStreamEvent.getEventType() == TABLE || leftMetaStreamEvent.getEventType() == AGGREGATE) && (rightMetaStreamEvent.getEventType() == TABLE || rightMetaStreamEvent.getEventType() == AGGREGATE)) {
                throw new SiddhiAppCreationException("Both inputs of join " + leftInputStreamId + " and " + rightInputStreamId + " are from static sources");
            }
            if (leftMetaStreamEvent.getEventType() != AGGREGATE && rightMetaStreamEvent.getEventType() != AGGREGATE) {
                if (joinInputStream.getPer() != null) {
                    throw new SiddhiAppCreationException("When joining " + leftInputStreamId + " and " + rightInputStreamId + " 'per' cannot be used as neither of them is an aggregation ");
                } else if (joinInputStream.getWithin() != null) {
                    throw new SiddhiAppCreationException("When joining " + leftInputStreamId + " and " + rightInputStreamId + " 'within' cannot be used as neither of them is an aggregation ");
                }
            }
        } else {
            if (windowDefinitionMap.containsKey(joinInputStream.getAllStreamIds().get(0))) {
                leftMetaStreamEvent.setEventType(WINDOW);
                rightMetaStreamEvent.setEventType(WINDOW);
                rightProcessStreamReceiver = new MultiProcessStreamReceiver(joinInputStream.getAllStreamIds().get(0), 1, latencyTracker, queryName, siddhiAppContext);
                rightProcessStreamReceiver.setBatchProcessingAllowed(true);
                leftProcessStreamReceiver = rightProcessStreamReceiver;
            } else if (streamDefinitionMap.containsKey(joinInputStream.getAllStreamIds().get(0))) {
                rightProcessStreamReceiver = new MultiProcessStreamReceiver(joinInputStream.getAllStreamIds().get(0), 2, latencyTracker, queryName, siddhiAppContext);
                leftProcessStreamReceiver = rightProcessStreamReceiver;
            } else {
                throw new SiddhiAppCreationException("Input of join is from static source " + leftInputStreamId + " and " + rightInputStreamId);
            }
        }
        SingleStreamRuntime leftStreamRuntime = SingleInputStreamParser.parseInputStream((SingleInputStream) joinInputStream.getLeftInputStream(), siddhiAppContext, executors, streamDefinitionMap, leftMetaStreamEvent.getEventType() != TABLE ? null : tableDefinitionMap, leftMetaStreamEvent.getEventType() != WINDOW ? null : windowDefinitionMap, leftMetaStreamEvent.getEventType() != AGGREGATE ? null : aggregationDefinitionMap, tableMap, leftMetaStreamEvent, leftProcessStreamReceiver, true, outputExpectsExpiredEvents, queryName);
        for (VariableExpressionExecutor variableExpressionExecutor : executors) {
            variableExpressionExecutor.getPosition()[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = 0;
        }
        int size = executors.size();
        SingleStreamRuntime rightStreamRuntime = SingleInputStreamParser.parseInputStream((SingleInputStream) joinInputStream.getRightInputStream(), siddhiAppContext, executors, streamDefinitionMap, rightMetaStreamEvent.getEventType() != TABLE ? null : tableDefinitionMap, rightMetaStreamEvent.getEventType() != WINDOW ? null : windowDefinitionMap, rightMetaStreamEvent.getEventType() != AGGREGATE ? null : aggregationDefinitionMap, tableMap, rightMetaStreamEvent, rightProcessStreamReceiver, true, outputExpectsExpiredEvents, queryName);
        for (int i = size; i < executors.size(); i++) {
            VariableExpressionExecutor variableExpressionExecutor = executors.get(i);
            variableExpressionExecutor.getPosition()[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = 1;
        }
        setStreamRuntimeProcessorChain(leftMetaStreamEvent, leftStreamRuntime, leftInputStreamId, tableMap, windowMap, aggregationMap, executors, outputExpectsExpiredEvents, queryName, joinInputStream.getWithin(), joinInputStream.getPer(), siddhiAppContext, joinInputStream.getLeftInputStream());
        setStreamRuntimeProcessorChain(rightMetaStreamEvent, rightStreamRuntime, rightInputStreamId, tableMap, windowMap, aggregationMap, executors, outputExpectsExpiredEvents, queryName, joinInputStream.getWithin(), joinInputStream.getPer(), siddhiAppContext, joinInputStream.getRightInputStream());
        MetaStateEvent metaStateEvent = new MetaStateEvent(2);
        metaStateEvent.addEvent(leftMetaStreamEvent);
        metaStateEvent.addEvent(rightMetaStreamEvent);
        switch(joinInputStream.getType()) {
            case FULL_OUTER_JOIN:
                leftOuterJoinProcessor = true;
                rightOuterJoinProcessor = true;
                break;
            case RIGHT_OUTER_JOIN:
                rightOuterJoinProcessor = true;
                break;
            case LEFT_OUTER_JOIN:
                leftOuterJoinProcessor = true;
                break;
        }
        JoinProcessor leftPreJoinProcessor = new JoinProcessor(true, true, leftOuterJoinProcessor, 0);
        JoinProcessor leftPostJoinProcessor = new JoinProcessor(true, false, leftOuterJoinProcessor, 0);
        FindableProcessor leftFindableProcessor = insertJoinProcessorsAndGetFindable(leftPreJoinProcessor, leftPostJoinProcessor, leftStreamRuntime, siddhiAppContext, outputExpectsExpiredEvents, queryName, joinInputStream.getLeftInputStream());
        JoinProcessor rightPreJoinProcessor = new JoinProcessor(false, true, rightOuterJoinProcessor, 1);
        JoinProcessor rightPostJoinProcessor = new JoinProcessor(false, false, rightOuterJoinProcessor, 1);
        FindableProcessor rightFindableProcessor = insertJoinProcessorsAndGetFindable(rightPreJoinProcessor, rightPostJoinProcessor, rightStreamRuntime, siddhiAppContext, outputExpectsExpiredEvents, queryName, joinInputStream.getRightInputStream());
        leftPreJoinProcessor.setFindableProcessor(rightFindableProcessor);
        leftPostJoinProcessor.setFindableProcessor(rightFindableProcessor);
        rightPreJoinProcessor.setFindableProcessor(leftFindableProcessor);
        rightPostJoinProcessor.setFindableProcessor(leftFindableProcessor);
        Expression compareCondition = joinInputStream.getOnCompare();
        if (compareCondition == null) {
            compareCondition = Expression.value(true);
        }
        MatchingMetaInfoHolder rightMatchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(metaStateEvent, 0, rightMetaStreamEvent.getLastInputDefinition(), UNKNOWN_STATE);
        CompiledCondition leftCompiledCondition = rightFindableProcessor.compileCondition(compareCondition, rightMatchingMetaInfoHolder, siddhiAppContext, executors, tableMap, queryName);
        MatchingMetaInfoHolder leftMatchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(metaStateEvent, 1, leftMetaStreamEvent.getLastInputDefinition(), UNKNOWN_STATE);
        CompiledCondition rightCompiledCondition = leftFindableProcessor.compileCondition(compareCondition, leftMatchingMetaInfoHolder, siddhiAppContext, executors, tableMap, queryName);
        if (joinInputStream.getTrigger() != JoinInputStream.EventTrigger.LEFT) {
            populateJoinProcessors(rightMetaStreamEvent, rightInputStreamId, rightPreJoinProcessor, rightPostJoinProcessor, rightCompiledCondition);
        }
        if (joinInputStream.getTrigger() != JoinInputStream.EventTrigger.RIGHT) {
            populateJoinProcessors(leftMetaStreamEvent, leftInputStreamId, leftPreJoinProcessor, leftPostJoinProcessor, leftCompiledCondition);
        }
        JoinStreamRuntime joinStreamRuntime = new JoinStreamRuntime(siddhiAppContext, metaStateEvent);
        joinStreamRuntime.addRuntime(leftStreamRuntime);
        joinStreamRuntime.addRuntime(rightStreamRuntime);
        return joinStreamRuntime;
    } catch (Throwable t) {
        ExceptionUtil.populateQueryContext(t, joinInputStream, siddhiAppContext);
        throw t;
    }
}
Also used : MultiProcessStreamReceiver(org.wso2.siddhi.core.query.input.MultiProcessStreamReceiver) ProcessStreamReceiver(org.wso2.siddhi.core.query.input.ProcessStreamReceiver) FindableProcessor(org.wso2.siddhi.core.query.processor.stream.window.FindableProcessor) SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException) SingleStreamRuntime(org.wso2.siddhi.core.query.input.stream.single.SingleStreamRuntime) JoinStreamRuntime(org.wso2.siddhi.core.query.input.stream.join.JoinStreamRuntime) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) MultiProcessStreamReceiver(org.wso2.siddhi.core.query.input.MultiProcessStreamReceiver) MetaStateEvent(org.wso2.siddhi.core.event.state.MetaStateEvent) CompiledCondition(org.wso2.siddhi.core.util.collection.operator.CompiledCondition) Expression(org.wso2.siddhi.query.api.expression.Expression) SingleInputStream(org.wso2.siddhi.query.api.execution.query.input.stream.SingleInputStream) MatchingMetaInfoHolder(org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) JoinProcessor(org.wso2.siddhi.core.query.input.stream.join.JoinProcessor) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent)

Example 3 with ProcessStreamReceiver

use of org.wso2.siddhi.core.query.input.ProcessStreamReceiver in project siddhi by wso2.

the class SingleInputStreamParser method parseInputStream.

/**
 * Parse single InputStream and return SingleStreamRuntime
 *
 * @param inputStream                 single input stream to be parsed
 * @param siddhiAppContext            query to be parsed
 * @param variableExpressionExecutors List to hold VariableExpressionExecutors to update after query parsing
 * @param streamDefinitionMap         Stream Definition Map
 * @param tableDefinitionMap          Table Definition Map
 * @param windowDefinitionMap         window definition map
 * @param aggregationDefinitionMap    aggregation definition map
 * @param tableMap                    Table Map
 * @param metaComplexEvent            MetaComplexEvent
 * @param processStreamReceiver       ProcessStreamReceiver
 * @param supportsBatchProcessing     supports batch processing
 * @param outputExpectsExpiredEvents  is output expects ExpiredEvents
 * @param queryName                   query name of single input stream belongs to.
 *
 * @return SingleStreamRuntime
 */
public static SingleStreamRuntime parseInputStream(SingleInputStream inputStream, SiddhiAppContext siddhiAppContext, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, MetaComplexEvent metaComplexEvent, ProcessStreamReceiver processStreamReceiver, boolean supportsBatchProcessing, boolean outputExpectsExpiredEvents, String queryName) {
    Processor processor = null;
    EntryValveProcessor entryValveProcessor = null;
    boolean first = true;
    MetaStreamEvent metaStreamEvent;
    if (metaComplexEvent instanceof MetaStateEvent) {
        metaStreamEvent = new MetaStreamEvent();
        ((MetaStateEvent) metaComplexEvent).addEvent(metaStreamEvent);
        initMetaStreamEvent(inputStream, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, metaStreamEvent);
    } else {
        metaStreamEvent = (MetaStreamEvent) metaComplexEvent;
        initMetaStreamEvent(inputStream, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, metaStreamEvent);
    }
    // A window cannot be defined for a window stream
    if (!inputStream.getStreamHandlers().isEmpty() && windowDefinitionMap != null && windowDefinitionMap.containsKey(inputStream.getStreamId())) {
        for (StreamHandler handler : inputStream.getStreamHandlers()) {
            if (handler instanceof Window) {
                throw new OperationNotSupportedException("Cannot create " + ((Window) handler).getName() + " " + "window for the window stream " + inputStream.getStreamId());
            }
        }
    }
    if (!inputStream.getStreamHandlers().isEmpty()) {
        for (StreamHandler handler : inputStream.getStreamHandlers()) {
            Processor currentProcessor = generateProcessor(handler, metaComplexEvent, variableExpressionExecutors, siddhiAppContext, tableMap, supportsBatchProcessing, outputExpectsExpiredEvents, queryName);
            if (currentProcessor instanceof SchedulingProcessor) {
                if (entryValveProcessor == null) {
                    entryValveProcessor = new EntryValveProcessor(siddhiAppContext);
                    if (first) {
                        processor = entryValveProcessor;
                        first = false;
                    } else {
                        processor.setToLast(entryValveProcessor);
                    }
                }
                Scheduler scheduler = SchedulerParser.parse(siddhiAppContext.getScheduledExecutorService(), entryValveProcessor, siddhiAppContext);
                ((SchedulingProcessor) currentProcessor).setScheduler(scheduler);
            }
            if (first) {
                processor = currentProcessor;
                first = false;
            } else {
                processor.setToLast(currentProcessor);
            }
        }
    }
    metaStreamEvent.initializeAfterWindowData();
    return new SingleStreamRuntime(processStreamReceiver, processor, metaComplexEvent);
}
Also used : Window(org.wso2.siddhi.query.api.execution.query.input.handler.Window) OperationNotSupportedException(org.wso2.siddhi.core.exception.OperationNotSupportedException) SchedulingProcessor(org.wso2.siddhi.core.query.processor.SchedulingProcessor) FilterProcessor(org.wso2.siddhi.core.query.processor.filter.FilterProcessor) StreamFunctionProcessor(org.wso2.siddhi.core.query.processor.stream.function.StreamFunctionProcessor) Processor(org.wso2.siddhi.core.query.processor.Processor) WindowProcessor(org.wso2.siddhi.core.query.processor.stream.window.WindowProcessor) EntryValveProcessor(org.wso2.siddhi.core.query.input.stream.single.EntryValveProcessor) StreamProcessor(org.wso2.siddhi.core.query.processor.stream.StreamProcessor) AbstractStreamProcessor(org.wso2.siddhi.core.query.processor.stream.AbstractStreamProcessor) SchedulingProcessor(org.wso2.siddhi.core.query.processor.SchedulingProcessor) Scheduler(org.wso2.siddhi.core.util.Scheduler) SingleStreamRuntime(org.wso2.siddhi.core.query.input.stream.single.SingleStreamRuntime) StreamHandler(org.wso2.siddhi.query.api.execution.query.input.handler.StreamHandler) EntryValveProcessor(org.wso2.siddhi.core.query.input.stream.single.EntryValveProcessor) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(org.wso2.siddhi.core.event.state.MetaStateEvent)

Example 4 with ProcessStreamReceiver

use of org.wso2.siddhi.core.query.input.ProcessStreamReceiver in project siddhi by wso2.

the class StateInputStreamParser method parse.

private static InnerStateRuntime parse(StateElement stateElement, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, MetaStateEvent metaStateEvent, SiddhiAppContext siddhiAppContext, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, ProcessStreamReceiver> processStreamReceiverMap, StreamPreStateProcessor streamPreStateProcessor, StreamPostStateProcessor streamPostStateProcessor, StateInputStream.Type stateType, ArrayList<Map.Entry<Long, Set<Integer>>> withinStates, LatencyTracker latencyTracker, String queryName) {
    if (stateElement instanceof StreamStateElement) {
        BasicSingleInputStream basicSingleInputStream = ((StreamStateElement) stateElement).getBasicSingleInputStream();
        SingleStreamRuntime singleStreamRuntime = SingleInputStreamParser.parseInputStream(basicSingleInputStream, siddhiAppContext, variableExpressionExecutors, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, processStreamReceiverMap.get(basicSingleInputStream.getUniqueStreamIds().get(0)), false, false, queryName);
        int stateIndex = metaStateEvent.getStreamEventCount() - 1;
        if (streamPreStateProcessor == null) {
            if (stateElement.getWithin() != null) {
                Set<Integer> withinStateset = new HashSet<Integer>();
                withinStateset.add(SiddhiConstants.ANY);
                withinStates.add(0, new AbstractMap.SimpleEntry<Long, Set<Integer>>(stateElement.getWithin().getValue(), withinStateset));
            }
            if (stateElement instanceof AbsentStreamStateElement) {
                AbsentStreamPreStateProcessor absentProcessor = new AbsentStreamPreStateProcessor(stateType, clonewithinStates(withinStates), ((AbsentStreamStateElement) stateElement).getWaitingTime());
                // Set the scheduler
                siddhiAppContext.addEternalReferencedHolder(absentProcessor);
                EntryValveProcessor entryValveProcessor = new EntryValveProcessor(siddhiAppContext);
                entryValveProcessor.setToLast(absentProcessor);
                Scheduler scheduler = SchedulerParser.parse(siddhiAppContext.getScheduledExecutorService(), entryValveProcessor, siddhiAppContext);
                absentProcessor.setScheduler(scheduler);
                // Assign the AbsentStreamPreStateProcessor to streamPreStateProcessor
                streamPreStateProcessor = absentProcessor;
            } else {
                streamPreStateProcessor = new StreamPreStateProcessor(stateType, clonewithinStates(withinStates));
            }
            streamPreStateProcessor.init(siddhiAppContext, queryName);
            if (stateElement.getWithin() != null) {
                withinStates.remove(0);
            }
        }
        streamPreStateProcessor.setStateId(stateIndex);
        streamPreStateProcessor.setNextProcessor(singleStreamRuntime.getProcessorChain());
        singleStreamRuntime.setProcessorChain(streamPreStateProcessor);
        if (streamPostStateProcessor == null) {
            if (stateElement instanceof AbsentStreamStateElement) {
                streamPostStateProcessor = new AbsentStreamPostStateProcessor();
            } else {
                streamPostStateProcessor = new StreamPostStateProcessor();
            }
        }
        streamPostStateProcessor.setStateId(stateIndex);
        singleStreamRuntime.getProcessorChain().setToLast(streamPostStateProcessor);
        streamPostStateProcessor.setThisStatePreProcessor(streamPreStateProcessor);
        streamPreStateProcessor.setThisStatePostProcessor(streamPostStateProcessor);
        streamPreStateProcessor.setThisLastProcessor(streamPostStateProcessor);
        StreamInnerStateRuntime innerStateRuntime = new StreamInnerStateRuntime(stateType);
        innerStateRuntime.setFirstProcessor(streamPreStateProcessor);
        innerStateRuntime.setLastProcessor(streamPostStateProcessor);
        innerStateRuntime.addStreamRuntime(singleStreamRuntime);
        return innerStateRuntime;
    } else if (stateElement instanceof NextStateElement) {
        StateElement currentElement = ((NextStateElement) stateElement).getStateElement();
        InnerStateRuntime currentInnerStateRuntime = parse(currentElement, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, siddhiAppContext, variableExpressionExecutors, processStreamReceiverMap, streamPreStateProcessor, streamPostStateProcessor, stateType, withinStates, latencyTracker, queryName);
        if (stateElement.getWithin() != null) {
            Set<Integer> withinStateSet = new HashSet<Integer>();
            withinStateSet.add(currentInnerStateRuntime.getFirstProcessor().getStateId());
            withinStateSet.add(currentInnerStateRuntime.getLastProcessor().getStateId());
            withinStates.add(0, new AbstractMap.SimpleEntry<Long, Set<Integer>>(stateElement.getWithin().getValue(), withinStateSet));
        }
        StateElement nextElement = ((NextStateElement) stateElement).getNextStateElement();
        InnerStateRuntime nextInnerStateRuntime = parse(nextElement, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, siddhiAppContext, variableExpressionExecutors, processStreamReceiverMap, streamPreStateProcessor, streamPostStateProcessor, stateType, withinStates, latencyTracker, queryName);
        if (stateElement.getWithin() != null) {
            withinStates.remove(0);
        }
        // currentInnerStateRuntime.getFirstProcessor().getStateId()
        currentInnerStateRuntime.getLastProcessor().setNextStatePreProcessor(nextInnerStateRuntime.getFirstProcessor());
        NextInnerStateRuntime nextStateRuntime = new NextInnerStateRuntime(currentInnerStateRuntime, nextInnerStateRuntime, stateType);
        nextStateRuntime.setFirstProcessor(currentInnerStateRuntime.getFirstProcessor());
        nextStateRuntime.setLastProcessor(nextInnerStateRuntime.getLastProcessor());
        for (SingleStreamRuntime singleStreamRuntime : currentInnerStateRuntime.getSingleStreamRuntimeList()) {
            nextStateRuntime.addStreamRuntime(singleStreamRuntime);
        }
        for (SingleStreamRuntime singleStreamRuntime : nextInnerStateRuntime.getSingleStreamRuntimeList()) {
            nextStateRuntime.addStreamRuntime(singleStreamRuntime);
        }
        return nextStateRuntime;
    } else if (stateElement instanceof EveryStateElement) {
        StateElement currentElement = ((EveryStateElement) stateElement).getStateElement();
        InnerStateRuntime innerStateRuntime = parse(currentElement, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, siddhiAppContext, variableExpressionExecutors, processStreamReceiverMap, streamPreStateProcessor, streamPostStateProcessor, stateType, withinStates, latencyTracker, queryName);
        EveryInnerStateRuntime everyInnerStateRuntime = new EveryInnerStateRuntime(innerStateRuntime, stateType);
        everyInnerStateRuntime.setFirstProcessor(innerStateRuntime.getFirstProcessor());
        everyInnerStateRuntime.setLastProcessor(innerStateRuntime.getLastProcessor());
        for (SingleStreamRuntime singleStreamRuntime : innerStateRuntime.getSingleStreamRuntimeList()) {
            everyInnerStateRuntime.addStreamRuntime(singleStreamRuntime);
        }
        everyInnerStateRuntime.getLastProcessor().setNextEveryStatePerProcessor(everyInnerStateRuntime.getFirstProcessor());
        return everyInnerStateRuntime;
    } else if (stateElement instanceof LogicalStateElement) {
        LogicalStateElement.Type type = ((LogicalStateElement) stateElement).getType();
        if (stateElement.getWithin() != null) {
            Set<Integer> withinStateset = new HashSet<Integer>();
            withinStateset.add(SiddhiConstants.ANY);
            withinStates.add(0, new AbstractMap.SimpleEntry<Long, Set<Integer>>(stateElement.getWithin().getValue(), withinStateset));
        }
        LogicalPreStateProcessor logicalPreStateProcessor1;
        if (((LogicalStateElement) stateElement).getStreamStateElement1() instanceof AbsentStreamStateElement) {
            logicalPreStateProcessor1 = new AbsentLogicalPreStateProcessor(type, stateType, clonewithinStates(withinStates), ((AbsentStreamStateElement) ((LogicalStateElement) stateElement).getStreamStateElement1()).getWaitingTime());
            // Set the scheduler
            siddhiAppContext.addEternalReferencedHolder((AbsentLogicalPreStateProcessor) logicalPreStateProcessor1);
            EntryValveProcessor entryValveProcessor = new EntryValveProcessor(siddhiAppContext);
            entryValveProcessor.setToLast(logicalPreStateProcessor1);
            Scheduler scheduler = SchedulerParser.parse(siddhiAppContext.getScheduledExecutorService(), entryValveProcessor, siddhiAppContext);
            ((SchedulingProcessor) logicalPreStateProcessor1).setScheduler(scheduler);
        } else {
            logicalPreStateProcessor1 = new LogicalPreStateProcessor(type, stateType, clonewithinStates(withinStates));
        }
        logicalPreStateProcessor1.init(siddhiAppContext, queryName);
        LogicalPostStateProcessor logicalPostStateProcessor1;
        if (((LogicalStateElement) stateElement).getStreamStateElement1() instanceof AbsentStreamStateElement) {
            logicalPostStateProcessor1 = new AbsentLogicalPostStateProcessor(type);
        } else {
            logicalPostStateProcessor1 = new LogicalPostStateProcessor(type);
        }
        LogicalPreStateProcessor logicalPreStateProcessor2;
        if (((LogicalStateElement) stateElement).getStreamStateElement2() instanceof AbsentStreamStateElement) {
            logicalPreStateProcessor2 = new AbsentLogicalPreStateProcessor(type, stateType, clonewithinStates(withinStates), ((AbsentStreamStateElement) ((LogicalStateElement) stateElement).getStreamStateElement2()).getWaitingTime());
            siddhiAppContext.addEternalReferencedHolder((AbsentLogicalPreStateProcessor) logicalPreStateProcessor2);
            EntryValveProcessor entryValveProcessor = new EntryValveProcessor(siddhiAppContext);
            entryValveProcessor.setToLast(logicalPreStateProcessor2);
            Scheduler scheduler = SchedulerParser.parse(siddhiAppContext.getScheduledExecutorService(), entryValveProcessor, siddhiAppContext);
            ((SchedulingProcessor) logicalPreStateProcessor2).setScheduler(scheduler);
        } else {
            logicalPreStateProcessor2 = new LogicalPreStateProcessor(type, stateType, clonewithinStates(withinStates));
        }
        logicalPreStateProcessor2.init(siddhiAppContext, queryName);
        LogicalPostStateProcessor logicalPostStateProcessor2;
        if (((LogicalStateElement) stateElement).getStreamStateElement2() instanceof AbsentStreamStateElement) {
            logicalPostStateProcessor2 = new AbsentLogicalPostStateProcessor(type);
        } else {
            logicalPostStateProcessor2 = new LogicalPostStateProcessor(type);
        }
        if (stateElement.getWithin() != null) {
            withinStates.remove(0);
        }
        logicalPostStateProcessor1.setPartnerPreStateProcessor(logicalPreStateProcessor2);
        logicalPostStateProcessor2.setPartnerPreStateProcessor(logicalPreStateProcessor1);
        logicalPostStateProcessor1.setPartnerPostStateProcessor(logicalPostStateProcessor2);
        logicalPostStateProcessor2.setPartnerPostStateProcessor(logicalPostStateProcessor1);
        logicalPreStateProcessor1.setPartnerStatePreProcessor(logicalPreStateProcessor2);
        logicalPreStateProcessor2.setPartnerStatePreProcessor(logicalPreStateProcessor1);
        StateElement stateElement2 = ((LogicalStateElement) stateElement).getStreamStateElement2();
        InnerStateRuntime innerStateRuntime2 = parse(stateElement2, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, siddhiAppContext, variableExpressionExecutors, processStreamReceiverMap, logicalPreStateProcessor2, logicalPostStateProcessor2, stateType, withinStates, latencyTracker, queryName);
        StateElement stateElement1 = ((LogicalStateElement) stateElement).getStreamStateElement1();
        InnerStateRuntime innerStateRuntime1 = parse(stateElement1, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, siddhiAppContext, variableExpressionExecutors, processStreamReceiverMap, logicalPreStateProcessor1, logicalPostStateProcessor1, stateType, withinStates, latencyTracker, queryName);
        LogicalInnerStateRuntime logicalInnerStateRuntime = new LogicalInnerStateRuntime(innerStateRuntime1, innerStateRuntime2, stateType);
        logicalInnerStateRuntime.setFirstProcessor(innerStateRuntime1.getFirstProcessor());
        logicalInnerStateRuntime.setLastProcessor(innerStateRuntime2.getLastProcessor());
        for (SingleStreamRuntime singleStreamRuntime : innerStateRuntime2.getSingleStreamRuntimeList()) {
            logicalInnerStateRuntime.addStreamRuntime(singleStreamRuntime);
        }
        for (SingleStreamRuntime singleStreamRuntime : innerStateRuntime1.getSingleStreamRuntimeList()) {
            logicalInnerStateRuntime.addStreamRuntime(singleStreamRuntime);
        }
        return logicalInnerStateRuntime;
    } else if (stateElement instanceof CountStateElement) {
        int minCount = ((CountStateElement) stateElement).getMinCount();
        int maxCount = ((CountStateElement) stateElement).getMaxCount();
        if (minCount == SiddhiConstants.ANY) {
            minCount = 0;
        }
        if (maxCount == SiddhiConstants.ANY) {
            maxCount = Integer.MAX_VALUE;
        }
        if (stateElement.getWithin() != null) {
            Set<Integer> withinStateset = new HashSet<Integer>();
            withinStateset.add(SiddhiConstants.ANY);
            withinStates.add(0, new AbstractMap.SimpleEntry<Long, Set<Integer>>(stateElement.getWithin().getValue(), withinStateset));
        }
        CountPreStateProcessor countPreStateProcessor = new CountPreStateProcessor(minCount, maxCount, stateType, withinStates);
        countPreStateProcessor.init(siddhiAppContext, queryName);
        CountPostStateProcessor countPostStateProcessor = new CountPostStateProcessor(minCount, maxCount);
        if (stateElement.getWithin() != null) {
            withinStates.remove(0);
        }
        countPreStateProcessor.setCountPostStateProcessor(countPostStateProcessor);
        StateElement currentElement = ((CountStateElement) stateElement).getStreamStateElement();
        InnerStateRuntime innerStateRuntime = parse(currentElement, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, siddhiAppContext, variableExpressionExecutors, processStreamReceiverMap, countPreStateProcessor, countPostStateProcessor, stateType, withinStates, latencyTracker, queryName);
        return new CountInnerStateRuntime((StreamInnerStateRuntime) innerStateRuntime);
    } else {
        throw new OperationNotSupportedException();
    }
}
Also used : CountPreStateProcessor(org.wso2.siddhi.core.query.input.stream.state.CountPreStateProcessor) Set(java.util.Set) HashSet(java.util.HashSet) NextStateElement(org.wso2.siddhi.query.api.execution.query.input.state.NextStateElement) Scheduler(org.wso2.siddhi.core.util.Scheduler) LogicalStateElement(org.wso2.siddhi.query.api.execution.query.input.state.LogicalStateElement) EveryStateElement(org.wso2.siddhi.query.api.execution.query.input.state.EveryStateElement) StreamStateElement(org.wso2.siddhi.query.api.execution.query.input.state.StreamStateElement) NextStateElement(org.wso2.siddhi.query.api.execution.query.input.state.NextStateElement) StateElement(org.wso2.siddhi.query.api.execution.query.input.state.StateElement) CountStateElement(org.wso2.siddhi.query.api.execution.query.input.state.CountStateElement) AbsentStreamStateElement(org.wso2.siddhi.query.api.execution.query.input.state.AbsentStreamStateElement) StreamPreStateProcessor(org.wso2.siddhi.core.query.input.stream.state.StreamPreStateProcessor) AbsentStreamPreStateProcessor(org.wso2.siddhi.core.query.input.stream.state.AbsentStreamPreStateProcessor) LogicalPostStateProcessor(org.wso2.siddhi.core.query.input.stream.state.LogicalPostStateProcessor) AbsentLogicalPostStateProcessor(org.wso2.siddhi.core.query.input.stream.state.AbsentLogicalPostStateProcessor) EveryInnerStateRuntime(org.wso2.siddhi.core.query.input.stream.state.runtime.EveryInnerStateRuntime) EveryStateElement(org.wso2.siddhi.query.api.execution.query.input.state.EveryStateElement) AbsentLogicalPostStateProcessor(org.wso2.siddhi.core.query.input.stream.state.AbsentLogicalPostStateProcessor) AbstractMap(java.util.AbstractMap) StreamInnerStateRuntime(org.wso2.siddhi.core.query.input.stream.state.runtime.StreamInnerStateRuntime) NextInnerStateRuntime(org.wso2.siddhi.core.query.input.stream.state.runtime.NextInnerStateRuntime) LogicalInnerStateRuntime(org.wso2.siddhi.core.query.input.stream.state.runtime.LogicalInnerStateRuntime) InnerStateRuntime(org.wso2.siddhi.core.query.input.stream.state.runtime.InnerStateRuntime) EveryInnerStateRuntime(org.wso2.siddhi.core.query.input.stream.state.runtime.EveryInnerStateRuntime) CountInnerStateRuntime(org.wso2.siddhi.core.query.input.stream.state.runtime.CountInnerStateRuntime) StreamPostStateProcessor(org.wso2.siddhi.core.query.input.stream.state.StreamPostStateProcessor) AbsentStreamPostStateProcessor(org.wso2.siddhi.core.query.input.stream.state.AbsentStreamPostStateProcessor) CountStateElement(org.wso2.siddhi.query.api.execution.query.input.state.CountStateElement) NextInnerStateRuntime(org.wso2.siddhi.core.query.input.stream.state.runtime.NextInnerStateRuntime) LogicalInnerStateRuntime(org.wso2.siddhi.core.query.input.stream.state.runtime.LogicalInnerStateRuntime) LogicalStateElement(org.wso2.siddhi.query.api.execution.query.input.state.LogicalStateElement) StreamInnerStateRuntime(org.wso2.siddhi.core.query.input.stream.state.runtime.StreamInnerStateRuntime) HashSet(java.util.HashSet) OperationNotSupportedException(org.wso2.siddhi.core.exception.OperationNotSupportedException) CountPostStateProcessor(org.wso2.siddhi.core.query.input.stream.state.CountPostStateProcessor) SingleStreamRuntime(org.wso2.siddhi.core.query.input.stream.single.SingleStreamRuntime) CountInnerStateRuntime(org.wso2.siddhi.core.query.input.stream.state.runtime.CountInnerStateRuntime) AbsentStreamStateElement(org.wso2.siddhi.query.api.execution.query.input.state.AbsentStreamStateElement) LogicalPreStateProcessor(org.wso2.siddhi.core.query.input.stream.state.LogicalPreStateProcessor) AbsentLogicalPreStateProcessor(org.wso2.siddhi.core.query.input.stream.state.AbsentLogicalPreStateProcessor) AbsentLogicalPreStateProcessor(org.wso2.siddhi.core.query.input.stream.state.AbsentLogicalPreStateProcessor) AbsentStreamPreStateProcessor(org.wso2.siddhi.core.query.input.stream.state.AbsentStreamPreStateProcessor) StreamStateElement(org.wso2.siddhi.query.api.execution.query.input.state.StreamStateElement) AbsentStreamStateElement(org.wso2.siddhi.query.api.execution.query.input.state.AbsentStreamStateElement) BasicSingleInputStream(org.wso2.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream) EntryValveProcessor(org.wso2.siddhi.core.query.input.stream.single.EntryValveProcessor) AbsentStreamPostStateProcessor(org.wso2.siddhi.core.query.input.stream.state.AbsentStreamPostStateProcessor)

Example 5 with ProcessStreamReceiver

use of org.wso2.siddhi.core.query.input.ProcessStreamReceiver in project siddhi by wso2.

the class InputStreamParser method parse.

/**
 * Parse an InputStream returning corresponding StreamRuntime
 *
 * @param inputStream                input stream to be parsed
 * @param siddhiAppContext           associated siddhi siddhiAppContext
 * @param streamDefinitionMap        map containing user given stream definitions
 * @param tableDefinitionMap         table definition map
 * @param windowDefinitionMap        window definition map
 * @param aggregationDefinitionMap   aggregation definition map
 * @param tableMap                   Table Map
 * @param windowMap                  event window map
 * @param aggregationMap             aggregator map
 * @param executors                  List to hold VariableExpressionExecutors to update after query parsing
 * @param latencyTracker             latency tracker
 * @param outputExpectsExpiredEvents is output expects ExpiredEvents
 * @param queryName                  query name of input stream belongs to.
 * @return StreamRuntime
 */
public static StreamRuntime parse(InputStream inputStream, SiddhiAppContext siddhiAppContext, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, Map<String, Window> windowMap, Map<String, AggregationRuntime> aggregationMap, List<VariableExpressionExecutor> executors, LatencyTracker latencyTracker, boolean outputExpectsExpiredEvents, String queryName) {
    if (inputStream instanceof BasicSingleInputStream || inputStream instanceof SingleInputStream) {
        SingleInputStream singleInputStream = (SingleInputStream) inputStream;
        Window window = windowMap.get(singleInputStream.getStreamId());
        // If stream is from window, allow batch
        boolean batchProcessingAllowed = window != null;
        // processing
        ProcessStreamReceiver processStreamReceiver = new ProcessStreamReceiver(singleInputStream.getStreamId(), latencyTracker, queryName, siddhiAppContext);
        processStreamReceiver.setBatchProcessingAllowed(batchProcessingAllowed);
        return SingleInputStreamParser.parseInputStream((SingleInputStream) inputStream, siddhiAppContext, executors, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, new MetaStreamEvent(), processStreamReceiver, true, outputExpectsExpiredEvents, queryName);
    } else if (inputStream instanceof JoinInputStream) {
        return JoinInputStreamParser.parseInputStream(((JoinInputStream) inputStream), siddhiAppContext, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, windowMap, aggregationMap, executors, latencyTracker, outputExpectsExpiredEvents, queryName);
    } else if (inputStream instanceof StateInputStream) {
        MetaStateEvent metaStateEvent = new MetaStateEvent(inputStream.getAllStreamIds().size());
        return StateInputStreamParser.parseInputStream(((StateInputStream) inputStream), siddhiAppContext, metaStateEvent, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, executors, latencyTracker, queryName);
    } else {
        throw new OperationNotSupportedException();
    }
}
Also used : Window(org.wso2.siddhi.core.window.Window) OperationNotSupportedException(org.wso2.siddhi.core.exception.OperationNotSupportedException) ProcessStreamReceiver(org.wso2.siddhi.core.query.input.ProcessStreamReceiver) SingleInputStream(org.wso2.siddhi.query.api.execution.query.input.stream.SingleInputStream) BasicSingleInputStream(org.wso2.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream) JoinInputStream(org.wso2.siddhi.query.api.execution.query.input.stream.JoinInputStream) BasicSingleInputStream(org.wso2.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream) StateInputStream(org.wso2.siddhi.query.api.execution.query.input.stream.StateInputStream) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(org.wso2.siddhi.core.event.state.MetaStateEvent)

Aggregations

ProcessStreamReceiver (org.wso2.siddhi.core.query.input.ProcessStreamReceiver)8 SingleStreamRuntime (org.wso2.siddhi.core.query.input.stream.single.SingleStreamRuntime)5 MetaStateEvent (org.wso2.siddhi.core.event.state.MetaStateEvent)4 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)4 OperationNotSupportedException (org.wso2.siddhi.core.exception.OperationNotSupportedException)3 StreamPreStateProcessor (org.wso2.siddhi.core.query.input.stream.state.StreamPreStateProcessor)3 Processor (org.wso2.siddhi.core.query.processor.Processor)3 SchedulingProcessor (org.wso2.siddhi.core.query.processor.SchedulingProcessor)3 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)2 JoinProcessor (org.wso2.siddhi.core.query.input.stream.join.JoinProcessor)2 EntryValveProcessor (org.wso2.siddhi.core.query.input.stream.single.EntryValveProcessor)2 AbsentStreamPreStateProcessor (org.wso2.siddhi.core.query.input.stream.state.AbsentStreamPreStateProcessor)2 SequenceMultiProcessStreamReceiver (org.wso2.siddhi.core.query.input.stream.state.receiver.SequenceMultiProcessStreamReceiver)2 SequenceSingleProcessStreamReceiver (org.wso2.siddhi.core.query.input.stream.state.receiver.SequenceSingleProcessStreamReceiver)2 CountInnerStateRuntime (org.wso2.siddhi.core.query.input.stream.state.runtime.CountInnerStateRuntime)2 EveryInnerStateRuntime (org.wso2.siddhi.core.query.input.stream.state.runtime.EveryInnerStateRuntime)2 InnerStateRuntime (org.wso2.siddhi.core.query.input.stream.state.runtime.InnerStateRuntime)2 LogicalInnerStateRuntime (org.wso2.siddhi.core.query.input.stream.state.runtime.LogicalInnerStateRuntime)2 NextInnerStateRuntime (org.wso2.siddhi.core.query.input.stream.state.runtime.NextInnerStateRuntime)2 StreamInnerStateRuntime (org.wso2.siddhi.core.query.input.stream.state.runtime.StreamInnerStateRuntime)2