Search in sources :

Example 86 with StreamEvent

use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.

the class LengthBatchWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    List<ComplexEventChunk<StreamEvent>> streamEventChunks = new ArrayList<ComplexEventChunk<StreamEvent>>();
    synchronized (this) {
        ComplexEventChunk<StreamEvent> outputStreamEventChunk = new ComplexEventChunk<StreamEvent>(true);
        long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
        while (streamEventChunk.hasNext()) {
            StreamEvent streamEvent = streamEventChunk.next();
            StreamEvent clonedStreamEvent = streamEventCloner.copyStreamEvent(streamEvent);
            currentEventChunk.add(clonedStreamEvent);
            count++;
            if (count == length) {
                if (outputExpectsExpiredEvents) {
                    if (expiredEventChunk.getFirst() != null) {
                        while (expiredEventChunk.hasNext()) {
                            StreamEvent expiredEvent = expiredEventChunk.next();
                            expiredEvent.setTimestamp(currentTime);
                        }
                        outputStreamEventChunk.add(expiredEventChunk.getFirst());
                    }
                }
                if (expiredEventChunk != null) {
                    expiredEventChunk.clear();
                }
                if (currentEventChunk.getFirst() != null) {
                    // add reset event in front of current events
                    outputStreamEventChunk.add(resetEvent);
                    resetEvent = null;
                    if (expiredEventChunk != null) {
                        currentEventChunk.reset();
                        while (currentEventChunk.hasNext()) {
                            StreamEvent currentEvent = currentEventChunk.next();
                            StreamEvent toExpireEvent = streamEventCloner.copyStreamEvent(currentEvent);
                            toExpireEvent.setType(StreamEvent.Type.EXPIRED);
                            expiredEventChunk.add(toExpireEvent);
                        }
                    }
                    resetEvent = streamEventCloner.copyStreamEvent(currentEventChunk.getFirst());
                    resetEvent.setType(ComplexEvent.Type.RESET);
                    outputStreamEventChunk.add(currentEventChunk.getFirst());
                }
                currentEventChunk.clear();
                count = 0;
                if (outputStreamEventChunk.getFirst() != null) {
                    streamEventChunks.add(outputStreamEventChunk);
                }
            }
        }
    }
    for (ComplexEventChunk<StreamEvent> outputStreamEventChunk : streamEventChunks) {
        nextProcessor.process(outputStreamEventChunk);
    }
}
Also used : ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList)

Example 87 with StreamEvent

use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.

the class LengthWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    synchronized (this) {
        long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
        while (streamEventChunk.hasNext()) {
            StreamEvent streamEvent = streamEventChunk.next();
            StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
            clonedEvent.setType(StreamEvent.Type.EXPIRED);
            if (count < length) {
                count++;
                this.expiredEventChunk.add(clonedEvent);
            } else {
                StreamEvent firstEvent = this.expiredEventChunk.poll();
                if (firstEvent != null) {
                    firstEvent.setTimestamp(currentTime);
                    streamEventChunk.insertBeforeCurrent(firstEvent);
                    this.expiredEventChunk.add(clonedEvent);
                } else {
                    StreamEvent resetEvent = streamEventCloner.copyStreamEvent(streamEvent);
                    resetEvent.setType(ComplexEvent.Type.RESET);
                    // adding resetEvent and clonedEvent event to the streamEventChunk
                    // since we are using insertAfterCurrent(), the final order will be
                    // currentEvent > clonedEvent (or expiredEvent) > resetEvent
                    streamEventChunk.insertAfterCurrent(resetEvent);
                    streamEventChunk.insertAfterCurrent(clonedEvent);
                    // since we manually added resetEvent and clonedEvent in earlier step
                    // we have to skip those two events from getting processed in the next
                    // iteration. Hence, calling next() twice.
                    streamEventChunk.next();
                    streamEventChunk.next();
                }
            }
        }
    }
    nextProcessor.process(streamEventChunk);
}
Also used : StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent)

Example 88 with StreamEvent

use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.

the class SortWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    synchronized (this) {
        long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
        StreamEvent streamEvent = streamEventChunk.getFirst();
        streamEventChunk.clear();
        while (streamEvent != null) {
            StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
            clonedEvent.setType(StreamEvent.Type.EXPIRED);
            StreamEvent next = streamEvent.getNext();
            streamEvent.setNext(null);
            streamEventChunk.add(streamEvent);
            sortedWindow.add(clonedEvent);
            if (sortedWindow.size() > lengthToKeep) {
                Collections.sort(sortedWindow, eventComparator);
                StreamEvent expiredEvent = sortedWindow.remove(sortedWindow.size() - 1);
                expiredEvent.setTimestamp(currentTime);
                streamEventChunk.add(expiredEvent);
            }
            streamEvent = next;
        }
    }
    nextProcessor.process(streamEventChunk);
}
Also used : StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent)

Example 89 with StreamEvent

use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.

the class CronWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    synchronized (this) {
        while (streamEventChunk.hasNext()) {
            StreamEvent streamEvent = streamEventChunk.next();
            StreamEvent clonedStreamEvent = streamEventCloner.copyStreamEvent(streamEvent);
            currentEventChunk.add(clonedStreamEvent);
            streamEventChunk.remove();
        }
    }
}
Also used : StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent)

Example 90 with StreamEvent

use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.

the class CronWindowProcessor method dispatchEvents.

public void dispatchEvents() {
    ComplexEventChunk<StreamEvent> streamEventChunk = new ComplexEventChunk<StreamEvent>(false);
    synchronized (this) {
        if (currentEventChunk.getFirst() != null) {
            long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
            while (expiredEventChunk.hasNext()) {
                StreamEvent expiredEvent = expiredEventChunk.next();
                expiredEvent.setTimestamp(currentTime);
            }
            if (expiredEventChunk.getFirst() != null) {
                streamEventChunk.add(expiredEventChunk.getFirst());
            }
            expiredEventChunk.clear();
            while (currentEventChunk.hasNext()) {
                StreamEvent currentEvent = currentEventChunk.next();
                StreamEvent toExpireEvent = streamEventCloner.copyStreamEvent(currentEvent);
                toExpireEvent.setType(StreamEvent.Type.EXPIRED);
                expiredEventChunk.add(toExpireEvent);
            }
            streamEventChunk.add(currentEventChunk.getFirst());
            currentEventChunk.clear();
        }
    }
    if (streamEventChunk.getFirst() != null) {
        nextProcessor.process(streamEventChunk);
    }
}
Also used : ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent)

Aggregations

StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)117 ComplexEventChunk (org.ballerinalang.siddhi.core.event.ComplexEventChunk)41 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)40 StateEvent (org.ballerinalang.siddhi.core.event.state.StateEvent)19 Test (org.testng.annotations.Test)18 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)17 StreamEventPool (org.ballerinalang.siddhi.core.event.stream.StreamEventPool)16 Event (org.ballerinalang.siddhi.core.event.Event)15 ArrayList (java.util.ArrayList)11 ExpressionExecutor (org.ballerinalang.siddhi.core.executor.ExpressionExecutor)10 Map (java.util.Map)9 HashSet (java.util.HashSet)8 StreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.StreamEventConverter)8 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)8 ConstantExpressionExecutor (org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor)7 ConversionStreamEventChunk (org.ballerinalang.siddhi.core.event.stream.converter.ConversionStreamEventChunk)6 HashMap (java.util.HashMap)5 Set (java.util.Set)5 VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)5 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)5