Search in sources :

Example 26 with Event

use of org.wso2.carbon.apimgt.core.models.Event in project siddhi by wso2.

the class StreamPostStateProcessor method process.

/**
 * Process the handed StreamEvent
 *
 * @param complexEventChunk event chunk to be processed
 */
@Override
public void process(ComplexEventChunk complexEventChunk) {
    complexEventChunk.reset();
    if (complexEventChunk.hasNext()) {
        // one one event will be coming
        StateEvent stateEvent = (StateEvent) complexEventChunk.next();
        process(stateEvent, complexEventChunk);
    }
    complexEventChunk.clear();
}
Also used : StateEvent(org.wso2.siddhi.core.event.state.StateEvent)

Example 27 with Event

use of org.wso2.carbon.apimgt.core.models.Event in project siddhi by wso2.

the class InsertIntoWindowCallback method send.

/**
 * Add the event into the {@link Window}
 *
 * @param complexEventChunk the event to add
 * @param noOfEvents number of events
 */
@Override
public void send(ComplexEventChunk complexEventChunk, int noOfEvents) {
    if (getSiddhiDebugger() != null) {
        getSiddhiDebugger().checkBreakPoint(getQueryName(), SiddhiDebugger.QueryTerminal.OUT, complexEventChunk.getFirst());
    }
    // If events are inserted directly from another window, expired events can arrive
    complexEventChunk.reset();
    while (complexEventChunk.hasNext()) {
        ComplexEvent complexEvent = complexEventChunk.next();
        if (complexEvent.getType() == ComplexEvent.Type.EXPIRED) {
            complexEvent.setType(ComplexEvent.Type.CURRENT);
        }
    }
    window.add(complexEventChunk);
}
Also used : ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent)

Example 28 with Event

use of org.wso2.carbon.apimgt.core.models.Event in project siddhi by wso2.

the class QueryCallback method receiveStreamEvent.

public void receiveStreamEvent(ComplexEventChunk complexEventChunk) {
    Event[] currentEvents = null;
    Event[] expiredEvents = null;
    long timestamp = -1;
    List<Event> currentEventBuffer = new ArrayList<Event>();
    List<Event> expiredEventBuffer = new ArrayList<Event>();
    complexEventChunk.reset();
    while (complexEventChunk.hasNext()) {
        ComplexEvent streamEvent = complexEventChunk.next();
        if (streamEvent.getType() == StreamEvent.Type.EXPIRED) {
            bufferEvent(streamEvent, expiredEventBuffer);
        } else if (streamEvent.getType() == StreamEvent.Type.CURRENT) {
            bufferEvent(streamEvent, currentEventBuffer);
        }
        timestamp = streamEvent.getTimestamp();
    }
    if (!currentEventBuffer.isEmpty()) {
        currentEvents = currentEventBuffer.toArray(new Event[currentEventBuffer.size()]);
        currentEventBuffer.clear();
    }
    if (!expiredEventBuffer.isEmpty()) {
        expiredEvents = expiredEventBuffer.toArray(new Event[expiredEventBuffer.size()]);
        expiredEventBuffer.clear();
    }
    send(timestamp, currentEvents, expiredEvents);
}
Also used : ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) ArrayList(java.util.ArrayList) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) Event(org.wso2.siddhi.core.event.Event)

Example 29 with Event

use of org.wso2.carbon.apimgt.core.models.Event in project siddhi by wso2.

the class FrequentWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    synchronized (this) {
        StreamEvent streamEvent = streamEventChunk.getFirst();
        streamEventChunk.clear();
        long currentTime = siddhiAppContext.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 = map.put(key, clonedEvent);
            if (oldEvent != null) {
                countMap.put(key, countMap.get(key) + 1);
                streamEventChunk.add(streamEvent);
            } else {
                // This is a new event
                if (map.size() > mostFrequentCount) {
                    List<String> keys = new ArrayList<String>(countMap.keySet());
                    for (int i = 0; i < mostFrequentCount; i++) {
                        int count = countMap.get(keys.get(i)) - 1;
                        if (count == 0) {
                            countMap.remove(keys.get(i));
                            StreamEvent expiredEvent = map.remove(keys.get(i));
                            expiredEvent.setTimestamp(currentTime);
                            streamEventChunk.add(expiredEvent);
                        } else {
                            countMap.put(keys.get(i), count);
                        }
                    }
                    // now we have tried to remove one for newly added item
                    if (map.size() > mostFrequentCount) {
                        // nothing happend by the attempt to remove one from the
                        // map so we are ignoring this event
                        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
                        countMap.put(key, 1);
                        streamEventChunk.add(streamEvent);
                    }
                } else {
                    countMap.put(generateKey(streamEvent), 1);
                    streamEventChunk.add(streamEvent);
                }
            }
            streamEvent = next;
        }
    }
    nextProcessor.process(streamEventChunk);
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList)

Example 30 with Event

use of org.wso2.carbon.apimgt.core.models.Event in project siddhi by wso2.

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.wso2.siddhi.core.event.ComplexEventChunk) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList)

Aggregations

Test (org.testng.annotations.Test)1150 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)1146 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)1145 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)1119 Event (org.wso2.siddhi.core.event.Event)855 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)587 TestUtil (org.wso2.siddhi.core.TestUtil)300 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)218 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)86 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)79 Query (org.wso2.siddhi.query.api.execution.query.Query)79 ArrayList (java.util.ArrayList)68 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)63 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)58 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)38 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)35 GroupedComplexEvent (org.wso2.siddhi.core.event.GroupedComplexEvent)16 InMemoryBroker (org.wso2.siddhi.core.util.transport.InMemoryBroker)16 HashMap (java.util.HashMap)14 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)13