Search in sources :

Example 1 with ComplexEvent

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

the class Window method add.

/**
 * Add the given ComplexEventChunk to the Window.
 *
 * @param complexEventChunk the event chunk to be added
 */
public void add(ComplexEventChunk complexEventChunk) {
    try {
        this.lockWrapper.lock();
        complexEventChunk.reset();
        // Convert all events to StreamEvent because StateEvents can be passed if directly received from a join
        ComplexEvent complexEvents = complexEventChunk.getFirst();
        StreamEvent firstEvent = streamEventPool.borrowEvent();
        eventConverter.convertComplexEvent(complexEvents, firstEvent);
        StreamEvent currentEvent = firstEvent;
        complexEvents = complexEvents.getNext();
        int numberOfEvents = 0;
        while (complexEvents != null) {
            numberOfEvents++;
            StreamEvent nextEvent = streamEventPool.borrowEvent();
            eventConverter.convertComplexEvent(complexEvents, nextEvent);
            currentEvent.setNext(nextEvent);
            currentEvent = nextEvent;
            complexEvents = complexEvents.getNext();
        }
        try {
            if (throughputTrackerInsert != null && siddhiAppContext.isStatsEnabled()) {
                throughputTrackerInsert.eventsIn(numberOfEvents);
                latencyTrackerInsert.markIn();
            }
            // Send to the window windowProcessor
            windowProcessor.process(new ComplexEventChunk<StreamEvent>(firstEvent, currentEvent, complexEventChunk.isBatch()));
        } finally {
            if (throughputTrackerInsert != null && siddhiAppContext.isStatsEnabled()) {
                latencyTrackerInsert.markOut();
            }
        }
    } finally {
        this.lockWrapper.unlock();
    }
}
Also used : ComplexEvent(org.ballerinalang.siddhi.core.event.ComplexEvent) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent)

Example 2 with ComplexEvent

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

the class QuerySelector method processInBatchNoGroupBy.

private ComplexEventChunk processInBatchNoGroupBy(ComplexEventChunk complexEventChunk) {
    complexEventChunk.reset();
    ComplexEvent lastEvent = null;
    synchronized (this) {
        while (complexEventChunk.hasNext()) {
            ComplexEvent event = complexEventChunk.next();
            switch(event.getType()) {
                case CURRENT:
                case EXPIRED:
                    eventPopulator.populateStateEvent(event);
                    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                        attributeProcessor.process(event);
                    }
                    if (!(havingConditionExecutor != null && !havingConditionExecutor.execute(event))) {
                        if ((event.getType() == StreamEvent.Type.CURRENT && currentOn) || (event.getType() == StreamEvent.Type.EXPIRED && expiredOn)) {
                            complexEventChunk.remove();
                            lastEvent = event;
                        }
                    }
                    break;
                case TIMER:
                    break;
                case RESET:
                    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                        attributeProcessor.process(event);
                    }
                    break;
            }
        }
    }
    if (lastEvent != null) {
        complexEventChunk.clear();
        if (limit == SiddhiConstants.UNKNOWN_STATE || limit > 0) {
            complexEventChunk.add(lastEvent);
        }
        return complexEventChunk;
    }
    return null;
}
Also used : ComplexEvent(org.ballerinalang.siddhi.core.event.ComplexEvent) GroupedComplexEvent(org.ballerinalang.siddhi.core.event.GroupedComplexEvent) AttributeProcessor(org.ballerinalang.siddhi.core.query.selector.attribute.processor.AttributeProcessor)

Example 3 with ComplexEvent

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

the class QuerySelector method orderEventChunk.

private void orderEventChunk(ComplexEventChunk complexEventChunk) {
    ComplexEventChunk orderingComplexEventChunk = new ComplexEventChunk(complexEventChunk.isBatch());
    List<ComplexEvent> eventList = new ArrayList<>();
    ComplexEvent.Type currentEventType = null;
    complexEventChunk.reset();
    if (complexEventChunk.getFirst() != null) {
        currentEventType = complexEventChunk.getFirst().getType();
        while (complexEventChunk.hasNext()) {
            ComplexEvent event = complexEventChunk.next();
            complexEventChunk.remove();
            if (currentEventType == event.getType()) {
                eventList.add(event);
            } else {
                currentEventType = event.getType();
                eventList.sort(orderByEventComparator);
                for (ComplexEvent complexEvent : eventList) {
                    orderingComplexEventChunk.add(complexEvent);
                }
                eventList.clear();
                eventList.add(event);
            }
        }
        eventList.sort(orderByEventComparator);
        for (ComplexEvent complexEvent : eventList) {
            orderingComplexEventChunk.add(complexEvent);
        }
        complexEventChunk.clear();
        complexEventChunk.add(orderingComplexEventChunk.getFirst());
    }
}
Also used : ComplexEvent(org.ballerinalang.siddhi.core.event.ComplexEvent) GroupedComplexEvent(org.ballerinalang.siddhi.core.event.GroupedComplexEvent) ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) ArrayList(java.util.ArrayList)

Example 4 with ComplexEvent

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

the class QuerySelector method limitEventChunk.

private void limitEventChunk(ComplexEventChunk complexEventChunk) {
    complexEventChunk.reset();
    int limitCount = 0;
    while (complexEventChunk.hasNext()) {
        ComplexEvent event = complexEventChunk.next();
        if (event.getType() == StreamEvent.Type.CURRENT || event.getType() == StreamEvent.Type.EXPIRED) {
            if ((limit > limitCount) && (event.getType() == StreamEvent.Type.CURRENT && currentOn) || (event.getType() == StreamEvent.Type.EXPIRED && expiredOn)) {
                limitCount++;
            } else {
                complexEventChunk.remove();
            }
        }
    }
}
Also used : ComplexEvent(org.ballerinalang.siddhi.core.event.ComplexEvent) GroupedComplexEvent(org.ballerinalang.siddhi.core.event.GroupedComplexEvent)

Example 5 with ComplexEvent

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

the class QuerySelector method processGroupBy.

private ComplexEventChunk<ComplexEvent> processGroupBy(ComplexEventChunk complexEventChunk) {
    complexEventChunk.reset();
    ComplexEventChunk<ComplexEvent> currentComplexEventChunk = new ComplexEventChunk<ComplexEvent>(complexEventChunk.isBatch());
    synchronized (this) {
        int limitCount = 0;
        while (complexEventChunk.hasNext()) {
            ComplexEvent event = complexEventChunk.next();
            switch(event.getType()) {
                case CURRENT:
                case EXPIRED:
                    eventPopulator.populateStateEvent(event);
                    String groupedByKey = groupByKeyGenerator.constructEventKey(event);
                    GroupByAggregationAttributeExecutor.getKeyThreadLocal().set(groupedByKey);
                    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                        attributeProcessor.process(event);
                    }
                    if ((event.getType() == StreamEvent.Type.CURRENT && currentOn) || (event.getType() == StreamEvent.Type.EXPIRED && expiredOn)) {
                        if (!(havingConditionExecutor != null && !havingConditionExecutor.execute(event))) {
                            complexEventChunk.remove();
                            if (limit == SiddhiConstants.UNKNOWN_STATE) {
                                currentComplexEventChunk.add(new GroupedComplexEvent(groupedByKey, event));
                            } else {
                                if (limitCount < limit) {
                                    currentComplexEventChunk.add(new GroupedComplexEvent(groupedByKey, event));
                                    limitCount++;
                                }
                            }
                        }
                    }
                    GroupByAggregationAttributeExecutor.getKeyThreadLocal().remove();
                    break;
                case TIMER:
                    break;
                case RESET:
                    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                        attributeProcessor.process(event);
                    }
                    break;
            }
        }
    }
    if (isOrderBy) {
        orderEventChunk(complexEventChunk);
    }
    if (limit != SiddhiConstants.UNKNOWN_STATE) {
        limitEventChunk(complexEventChunk);
    }
    currentComplexEventChunk.reset();
    if (currentComplexEventChunk.hasNext()) {
        return currentComplexEventChunk;
    }
    return null;
}
Also used : ComplexEvent(org.ballerinalang.siddhi.core.event.ComplexEvent) GroupedComplexEvent(org.ballerinalang.siddhi.core.event.GroupedComplexEvent) ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) GroupedComplexEvent(org.ballerinalang.siddhi.core.event.GroupedComplexEvent) AttributeProcessor(org.ballerinalang.siddhi.core.query.selector.attribute.processor.AttributeProcessor)

Aggregations

ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)58 ComplexEventChunk (org.ballerinalang.siddhi.core.event.ComplexEventChunk)29 ArrayList (java.util.ArrayList)20 GroupedComplexEvent (org.ballerinalang.siddhi.core.event.GroupedComplexEvent)17 Event (org.ballerinalang.siddhi.core.event.Event)15 SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)12 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)12 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)12 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)12 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)11 Test (org.testng.annotations.Test)11 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)7 AttributeProcessor (org.ballerinalang.siddhi.core.query.selector.attribute.processor.AttributeProcessor)4 StateEvent (org.ballerinalang.siddhi.core.event.state.StateEvent)3 Iterator (java.util.Iterator)2 Map (java.util.Map)2 LinkedHashMap (java.util.LinkedHashMap)1 Scanner (java.util.Scanner)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 StreamEventPool (org.ballerinalang.siddhi.core.event.stream.StreamEventPool)1