Search in sources :

Example 16 with StreamEvent

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

the class ExternalTimeWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner, WindowState state) {
    synchronized (state) {
        while (streamEventChunk.hasNext()) {
            StreamEvent streamEvent = streamEventChunk.next();
            long currentTime = (Long) timeStampVariableExpressionExecutor.execute(streamEvent);
            StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
            clonedEvent.setType(StreamEvent.Type.EXPIRED);
            // reset expiredEventQueue to make sure all of the expired events get removed,
            // otherwise lastReturned.next will always return null and here while check is always false
            state.expiredEventQueue.reset();
            while (state.expiredEventQueue.hasNext()) {
                StreamEvent expiredEvent = state.expiredEventQueue.next();
                long expiredEventTime = (Long) timeStampVariableExpressionExecutor.execute(expiredEvent);
                long timeDiff = expiredEventTime - currentTime + timeToKeep;
                if (timeDiff <= 0) {
                    state.expiredEventQueue.remove();
                    expiredEvent.setTimestamp(currentTime);
                    streamEventChunk.insertBeforeCurrent(expiredEvent);
                } else {
                    state.expiredEventQueue.reset();
                    break;
                }
            }
            if (streamEvent.getType() == StreamEvent.Type.CURRENT) {
                state.expiredEventQueue.add(clonedEvent);
            }
            state.expiredEventQueue.reset();
        }
    }
    nextProcessor.process(streamEventChunk);
}
Also used : StreamEvent(io.siddhi.core.event.stream.StreamEvent)

Example 17 with StreamEvent

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

the class FrequentWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner, WindowState state) {
    synchronized (this) {
        StreamEvent streamEvent = streamEventChunk.getFirst();
        streamEventChunk.clear();
        long currentTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime();
        while (streamEvent != null) {
            StreamEvent next = streamEvent.getNext();
            streamEvent.setNext(null);
            StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
            clonedEvent.setType(StreamEvent.Type.EXPIRED);
            String key = generateKey(streamEvent);
            StreamEvent oldEvent = state.map.put(key, clonedEvent);
            if (oldEvent != null) {
                state.countMap.put(key, state.countMap.get(key) + 1);
                streamEventChunk.add(streamEvent);
            } else {
                // This is a new event
                if (state.map.size() > mostFrequentCount) {
                    List<String> keys = new ArrayList<String>(state.countMap.keySet());
                    for (int i = 0; i < mostFrequentCount; i++) {
                        int count = state.countMap.get(keys.get(i)) - 1;
                        if (count == 0) {
                            state.countMap.remove(keys.get(i));
                            StreamEvent expiredEvent = state.map.remove(keys.get(i));
                            expiredEvent.setTimestamp(currentTime);
                            streamEventChunk.add(expiredEvent);
                        } else {
                            state.countMap.put(keys.get(i), count);
                        }
                    }
                    // now we have tried to remove one for newly added item
                    if (state.map.size() > mostFrequentCount) {
                        // nothing happend by the attempt to remove one from the
                        // map so we are ignoring this event
                        state.map.remove(key);
                    // Here we do nothing just drop the message
                    } else {
                        // we got some space, event is already there in map object
                        // we just have to add it to the countMap
                        state.countMap.put(key, 1);
                        streamEventChunk.add(streamEvent);
                    }
                } else {
                    state.countMap.put(generateKey(streamEvent), 1);
                    streamEventChunk.add(streamEvent);
                }
            }
            streamEvent = next;
        }
    }
    nextProcessor.process(streamEventChunk);
}
Also used : StreamEvent(io.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList)

Example 18 with StreamEvent

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

the class GroupingWindowProcessor method processEventChunk.

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();
    processEventChunk(streamEventChunk, nextProcessor, streamEventCloner, groupingKeyPopulator, state);
}
Also used : StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 19 with StreamEvent

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

the class LengthBatchWindowProcessor method processStreamCurrentEvents.

private void processStreamCurrentEvents(StreamEvent streamEvent, ComplexEventChunk<StreamEvent> outputStreamEventChunk, long currentTime, WindowState state, StreamEventCloner streamEventCloner) {
    state.count++;
    if (state.count == length + 1) {
        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;
        }
        state.count = 1;
    }
    outputStreamEventChunk.add(streamEvent);
    if (state.expiredEventQueue != null) {
        StreamEvent clonedStreamEvent = streamEventCloner.copyStreamEvent(streamEvent);
        clonedStreamEvent.setType(StreamEvent.Type.EXPIRED);
        state.expiredEventQueue.add(clonedStreamEvent);
    }
}
Also used : StreamEvent(io.siddhi.core.event.stream.StreamEvent)

Example 20 with StreamEvent

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

the class LengthBatchWindowProcessor method processLengthZeroBatch.

private void processLengthZeroBatch(StreamEvent streamEvent, ComplexEventChunk<StreamEvent> outputStreamEventChunk, long currentTime, StreamEventCloner streamEventCloner) {
    outputStreamEventChunk.add(streamEvent);
    if (outputExpectsExpiredEvents) {
        StreamEvent expiredEvent = streamEventCloner.copyStreamEvent(streamEvent);
        expiredEvent.setType(ComplexEvent.Type.EXPIRED);
        expiredEvent.setTimestamp(currentTime);
        outputStreamEventChunk.add(expiredEvent);
    }
    StreamEvent resetEvent = streamEventCloner.copyStreamEvent(streamEvent);
    resetEvent.setType(ComplexEvent.Type.RESET);
    resetEvent.setTimestamp(currentTime);
    outputStreamEventChunk.add(resetEvent);
}
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