Search in sources :

Example 21 with StreamEvent

use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class LengthBatchWindowProcessor method processFullBatchEvents.

private void processFullBatchEvents(StreamEvent streamEvent, ComplexEventChunk<StreamEvent> outputStreamEventChunk, long currentTime, WindowState state, StreamEventCloner streamEventCloner) {
    StreamEvent clonedStreamEvent = streamEventCloner.copyStreamEvent(streamEvent);
    state.currentEventQueue.add(clonedStreamEvent);
    state.count++;
    if (state.count == length) {
        if (outputExpectsExpiredEvents && state.expiredEventQueue.getFirst() != null) {
            while (state.expiredEventQueue.hasNext()) {
                StreamEvent expiredEvent = state.expiredEventQueue.next();
                expiredEvent.setTimestamp(currentTime);
            }
            outputStreamEventChunk.add(state.expiredEventQueue.getFirst());
            state.expiredEventQueue.clear();
        }
        if (state.resetEvent != null) {
            state.resetEvent.setTimestamp(currentTime);
            outputStreamEventChunk.add(state.resetEvent);
            state.resetEvent = null;
        }
        if (state.currentEventQueue.getFirst() != null) {
            if (state.expiredEventQueue != null) {
                state.currentEventQueue.reset();
                while (state.currentEventQueue.hasNext()) {
                    StreamEvent currentEvent = state.currentEventQueue.next();
                    StreamEvent toExpireEvent = streamEventCloner.copyStreamEvent(currentEvent);
                    toExpireEvent.setType(StreamEvent.Type.EXPIRED);
                    state.expiredEventQueue.add(toExpireEvent);
                }
            }
            outputStreamEventChunk.add(state.currentEventQueue.getFirst());
            state.currentEventQueue.clear();
        }
        state.count = 0;
    }
}
Also used : StreamEvent(io.siddhi.core.event.stream.StreamEvent)

Example 22 with StreamEvent

use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class CronWindowProcessor method dispatchEvents.

public void dispatchEvents() {
    Map<String, Map<String, WindowState>> allStates = stateHolder.getAllStates();
    try {
        for (Map.Entry<String, Map<String, WindowState>> allStatesEntry : allStates.entrySet()) {
            for (Map.Entry<String, WindowState> stateEntry : allStatesEntry.getValue().entrySet()) {
                WindowState windowState = stateEntry.getValue();
                ComplexEventChunk<StreamEvent> streamEventChunk = new ComplexEventChunk<StreamEvent>();
                synchronized (windowState) {
                    if (windowState.currentEventQueue.getFirst() != null) {
                        long currentTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime();
                        while (windowState.expiredEventQueue.hasNext()) {
                            StreamEvent expiredEvent = windowState.expiredEventQueue.next();
                            expiredEvent.setTimestamp(currentTime);
                        }
                        if (windowState.expiredEventQueue.getFirst() != null) {
                            streamEventChunk.add(windowState.expiredEventQueue.getFirst());
                        }
                        windowState.expiredEventQueue.clear();
                        while (windowState.currentEventQueue.hasNext()) {
                            StreamEvent currentEvent = windowState.currentEventQueue.next();
                            StreamEvent toExpireEvent = streamEventClonerHolder.getStreamEventCloner().copyStreamEvent(currentEvent);
                            toExpireEvent.setType(StreamEvent.Type.EXPIRED);
                            windowState.expiredEventQueue.add(toExpireEvent);
                        }
                        streamEventChunk.add(windowState.currentEventQueue.getFirst());
                        windowState.currentEventQueue.clear();
                    }
                }
                SiddhiAppContext.startPartitionFlow(allStatesEntry.getKey());
                SiddhiAppContext.startGroupByFlow(stateEntry.getKey());
                try {
                    if (streamEventChunk.getFirst() != null) {
                        nextProcessor.process(streamEventChunk);
                    }
                } finally {
                    SiddhiAppContext.stopGroupByFlow();
                    SiddhiAppContext.stopPartitionFlow();
                }
            }
        }
    } finally {
        stateHolder.returnAllStates(allStates);
    }
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) StreamEvent(io.siddhi.core.event.stream.StreamEvent) HashMap(java.util.HashMap) Map(java.util.Map) JobDataMap(org.quartz.JobDataMap)

Example 23 with StreamEvent

use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class InsertIntoTableCallback method send.

@Override
public void send(ComplexEventChunk complexEventChunk, int noOfEvents) {
    if (getSiddhiDebugger() != null) {
        getSiddhiDebugger().checkBreakPoint(getQueryName(), SiddhiDebugger.QueryTerminal.OUT, complexEventChunk.getFirst());
    }
    if (convertToStreamEvent) {
        ComplexEventChunk<StreamEvent> streamEventChunk = new ComplexEventChunk<StreamEvent>();
        complexEventChunk.reset();
        while (complexEventChunk.hasNext()) {
            ComplexEvent complexEvent = complexEventChunk.next();
            StreamEvent borrowEvent = streamEventFactory.newInstance();
            streamEventConverter.convertData(complexEvent.getTimestamp(), complexEvent.getOutputData(), complexEvent.getType(), borrowEvent);
            streamEventChunk.add(borrowEvent);
        }
        table.addEvents(streamEventChunk, noOfEvents);
    } else {
        table.addEvents((ComplexEventChunk<StreamEvent>) complexEventChunk, noOfEvents);
    }
}
Also used : ComplexEvent(io.siddhi.core.event.ComplexEvent) ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) StreamEvent(io.siddhi.core.event.stream.StreamEvent)

Example 24 with StreamEvent

use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class SlidingWindowProcessor method processEventChunk.

@Override
protected void processEventChunk(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner, ComplexEventPopulater complexEventPopulater, S state) {
    streamEventChunk.reset();
    while (streamEventChunk.hasNext()) {
        StreamEvent streamEvent = streamEventChunk.next();
        streamEvent.setBeforeWindowData(null);
    }
    streamEventChunk.reset();
    process(streamEventChunk, nextProcessor, streamEventCloner, state);
}
Also used : StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 25 with StreamEvent

use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class TimeLengthWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner, WindowState state) {
    synchronized (state) {
        long currentTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime();
        while (streamEventChunk.hasNext()) {
            StreamEvent streamEvent = streamEventChunk.next();
            state.expiredEventQueue.reset();
            while (state.expiredEventQueue.hasNext()) {
                StreamEvent expiredEvent = state.expiredEventQueue.next();
                long timeDiff = expiredEvent.getTimestamp() - currentTime + timeInMilliSeconds;
                if (timeDiff <= 0) {
                    state.expiredEventQueue.remove();
                    state.count--;
                    expiredEvent.setTimestamp(currentTime);
                    streamEventChunk.insertBeforeCurrent(expiredEvent);
                } else {
                    break;
                }
            }
            state.expiredEventQueue.reset();
            if (streamEvent.getType() == StreamEvent.Type.CURRENT) {
                StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
                clonedEvent.setType(StreamEvent.Type.EXPIRED);
                if (state.count < length) {
                    state.count++;
                    state.expiredEventQueue.add(clonedEvent);
                } else {
                    StreamEvent firstEvent = state.expiredEventQueue.poll();
                    if (firstEvent != null) {
                        firstEvent.setTimestamp(currentTime);
                        streamEventChunk.insertBeforeCurrent(firstEvent);
                        state.expiredEventQueue.add(clonedEvent);
                    }
                }
                scheduler.notifyAt(clonedEvent.getTimestamp() + timeInMilliSeconds);
            } else {
                streamEventChunk.remove();
            }
        }
    }
    streamEventChunk.reset();
    if (streamEventChunk.hasNext()) {
        nextProcessor.process(streamEventChunk);
    }
}
Also used : StreamEvent(io.siddhi.core.event.stream.StreamEvent)

Aggregations

StreamEvent (io.siddhi.core.event.stream.StreamEvent)203 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)92 ComplexEventChunk (io.siddhi.core.event.ComplexEventChunk)74 StateEvent (io.siddhi.core.event.state.StateEvent)52 ComplexEvent (io.siddhi.core.event.ComplexEvent)33 ArrayList (java.util.ArrayList)27 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)26 Map (java.util.Map)24 Event (io.siddhi.core.event.Event)23 Test (org.testng.annotations.Test)23 StreamEventFactory (io.siddhi.core.event.stream.StreamEventFactory)20 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)18 Attribute (io.siddhi.query.api.definition.Attribute)17 HashMap (java.util.HashMap)17 Operation (io.siddhi.core.event.stream.Operation)12 LinkedList (java.util.LinkedList)12 SnapshotableStreamEventQueue (io.siddhi.core.event.stream.holder.SnapshotableStreamEventQueue)11 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)10 HashSet (java.util.HashSet)10 StreamEventConverter (io.siddhi.core.event.stream.converter.StreamEventConverter)8