Search in sources :

Example 61 with ComplexEventChunk

use of io.siddhi.core.event.ComplexEventChunk in project siddhi by wso2.

the class AbsentStreamPreStateProcessor method processAndReturn.

@Override
public ComplexEventChunk<StateEvent> processAndReturn(ComplexEventChunk complexEventChunk) {
    LogicalStreamPreState state = (LogicalStreamPreState) stateHolder.getState();
    try {
        if (!state.active) {
            return new ComplexEventChunk<>();
        }
        ComplexEventChunk<StateEvent> event = super.processAndReturn(complexEventChunk);
        StateEvent firstEvent = event.getFirst();
        if (firstEvent != null) {
            event = new ComplexEventChunk<>();
        }
        // Always return an empty event
        return event;
    } finally {
        stateHolder.returnState(state);
    }
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) StateEvent(io.siddhi.core.event.state.StateEvent)

Example 62 with ComplexEventChunk

use of io.siddhi.core.event.ComplexEventChunk in project siddhi by wso2.

the class AbsentStreamPreStateProcessor method process.

@Override
public void process(List<ComplexEventChunk> complexEventChunks) {
    ComplexEventChunk complexEventChunk = new ComplexEventChunk();
    for (ComplexEventChunk streamEventChunk : complexEventChunks) {
        complexEventChunk.addAll(streamEventChunk);
    }
    process(complexEventChunk);
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk)

Example 63 with ComplexEventChunk

use of io.siddhi.core.event.ComplexEventChunk in project siddhi by wso2.

the class AbsentStreamPreStateProcessor method process.

@Override
public void process(ComplexEventChunk complexEventChunk) {
    LogicalStreamPreState state = (LogicalStreamPreState) stateHolder.getState();
    try {
        if (!state.active) {
            // Every keyword is not used and already a pattern is processed
            return;
        }
        boolean notProcessed = true;
        long currentTime = complexEventChunk.getFirst().getTimestamp();
        ComplexEventChunk<StateEvent> retEventChunk = new ComplexEventChunk<>();
        lock.lock();
        try {
            // If the process method is called, it is guaranteed that the waitingTime is passed
            boolean initialize = isStartState && state.getNewAndEveryStateEventList().isEmpty() && state.getPendingStateEventList().isEmpty();
            if (initialize && stateType == StateInputStream.Type.SEQUENCE && thisStatePostProcessor.nextEveryStatePreProcessor == null && state.lastScheduledTime > 0) {
                // Sequence with no every but an event arrived
                initialize = false;
            }
            if (initialize) {
                // This is the first processor and no events received so far
                StateEvent stateEvent = stateEventFactory.newInstance();
                addState(stateEvent);
            } else if (stateType == StateInputStream.Type.SEQUENCE && !state.getNewAndEveryStateEventList().isEmpty()) {
                this.resetState();
            }
            this.updateState();
            Iterator<StateEvent> iterator = state.getPendingStateEventList().iterator();
            while (iterator.hasNext()) {
                StateEvent event = iterator.next();
                // Remove expired events based on within
                if (isExpired(event, currentTime)) {
                    iterator.remove();
                    if (withinEveryPreStateProcessor != null && thisStatePostProcessor.nextEveryStatePreProcessor != this) {
                        thisStatePostProcessor.nextEveryStatePreProcessor.addEveryState(event);
                    }
                    continue;
                }
                // Collect the events that came before the waiting time
                if (event.getTimestamp() == -1 && currentTime >= state.lastScheduledTime || event.getTimestamp() != -1 && currentTime >= event.getTimestamp() + waitingTime) {
                    iterator.remove();
                    event.setTimestamp(currentTime);
                    retEventChunk.add(event);
                }
            }
            if (withinEveryPreStateProcessor != null) {
                withinEveryPreStateProcessor.updateState();
            }
        } finally {
            lock.unlock();
        }
        notProcessed = retEventChunk.getFirst() == null;
        while (retEventChunk.hasNext()) {
            StateEvent stateEvent = retEventChunk.next();
            retEventChunk.remove();
            sendEvent(stateEvent, state);
        }
        long actualCurrentTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime();
        if (actualCurrentTime > waitingTime + currentTime) {
            state.lastScheduledTime = actualCurrentTime + waitingTime;
        }
        if (notProcessed && state.lastScheduledTime < currentTime) {
            state.lastScheduledTime = currentTime + waitingTime;
            this.scheduler.notifyAt(state.lastScheduledTime);
        }
    } finally {
        stateHolder.returnState(state);
    }
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) StateEvent(io.siddhi.core.event.state.StateEvent)

Example 64 with ComplexEventChunk

use of io.siddhi.core.event.ComplexEventChunk in project siddhi by wso2.

the class OnDemandQueryRuntime method execute.

/**
 * This method initiates the execution of on-demand Query.
 *
 * @return an array of Events.
 */
public Event[] execute() {
    try {
        StateEvent stateEvent = new StateEvent(1, outputAttributes.length);
        StreamEvent streamEvent = new StreamEvent(metaStreamEvent.getBeforeWindowData().size(), metaStreamEvent.getOnAfterWindowData().size(), metaStreamEvent.getOutputData().size());
        stateEvent.addEvent(0, streamEvent);
        ComplexEventChunk complexEventChunk = new ComplexEventChunk(stateEvent, stateEvent);
        if (eventType == MetaStreamEvent.EventType.TABLE) {
            selector.process(complexEventChunk);
        } else {
            throw new OnDemandQueryRuntimeException("DELETE, INSERT, UPDATE and UPDATE OR INSERT on-demand Query " + "operations consume only stream events of type \"TABLE\".");
        }
        return new Event[] {};
    } catch (Throwable t) {
        throw new OnDemandQueryRuntimeException("Error executing '" + queryName + "', " + t.getMessage(), t);
    }
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) Event(io.siddhi.core.event.Event) StateEvent(io.siddhi.core.event.state.StateEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) StateEvent(io.siddhi.core.event.state.StateEvent) OnDemandQueryRuntimeException(io.siddhi.core.exception.OnDemandQueryRuntimeException)

Example 65 with ComplexEventChunk

use of io.siddhi.core.event.ComplexEventChunk in project siddhi by wso2.

the class PartitionStreamReceiver method receive.

@Override
public void receive(ComplexEvent complexEvent) {
    if (partitionExecutors.size() == 0) {
        ComplexEventChunk<ComplexEvent> outputEventChunk = new ComplexEventChunk<ComplexEvent>();
        ComplexEvent aComplexEvent = complexEvent;
        while (aComplexEvent != null) {
            StreamEvent newEvent = streamEventFactory.newInstance();
            streamEventConverter.convertComplexEvent(aComplexEvent, newEvent);
            outputEventChunk.add(newEvent);
            aComplexEvent = aComplexEvent.getNext();
        }
        send(outputEventChunk.getFirst());
    } else {
        if (complexEvent.getNext() == null) {
            for (PartitionExecutor partitionExecutor : partitionExecutors) {
                StreamEvent newEvent = streamEventFactory.newInstance();
                streamEventConverter.convertComplexEvent(complexEvent, newEvent);
                String key = partitionExecutor.execute(newEvent);
                send(key, newEvent);
            }
        } else {
            ComplexEventChunk<ComplexEvent> complexEventChunk = new ComplexEventChunk<ComplexEvent>();
            complexEventChunk.add(complexEvent);
            ComplexEventChunk<ComplexEvent> outputEventChunk = new ComplexEventChunk<ComplexEvent>();
            String currentKey = null;
            while (complexEventChunk.hasNext()) {
                ComplexEvent aEvent = complexEventChunk.next();
                complexEventChunk.remove();
                StreamEvent newEvent = streamEventFactory.newInstance();
                streamEventConverter.convertComplexEvent(aEvent, newEvent);
                boolean currentEventMatchedPrevPartitionExecutor = false;
                for (PartitionExecutor partitionExecutor : partitionExecutors) {
                    String key = partitionExecutor.execute(newEvent);
                    if (key != null) {
                        if (currentKey == null) {
                            currentKey = key;
                        } else if (!currentKey.equals(key)) {
                            if (!currentEventMatchedPrevPartitionExecutor) {
                                ComplexEvent firstEvent = outputEventChunk.getFirst();
                                send(currentKey, firstEvent);
                                currentKey = key;
                                outputEventChunk.clear();
                            } else {
                                ComplexEvent firstEvent = outputEventChunk.getFirst();
                                send(currentKey, firstEvent);
                                currentKey = key;
                                outputEventChunk.clear();
                                StreamEvent cloneEvent = streamEventFactory.newInstance();
                                streamEventConverter.convertComplexEvent(aEvent, cloneEvent);
                                outputEventChunk.add(cloneEvent);
                            }
                        }
                        if (!currentEventMatchedPrevPartitionExecutor) {
                            outputEventChunk.add(newEvent);
                        }
                        currentEventMatchedPrevPartitionExecutor = true;
                    }
                }
            }
            send(currentKey, outputEventChunk.getFirst());
            outputEventChunk.clear();
        }
    }
}
Also used : ComplexEvent(io.siddhi.core.event.ComplexEvent) ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) PartitionExecutor(io.siddhi.core.partition.executor.PartitionExecutor) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Aggregations

ComplexEventChunk (io.siddhi.core.event.ComplexEventChunk)114 StreamEvent (io.siddhi.core.event.stream.StreamEvent)75 ComplexEvent (io.siddhi.core.event.ComplexEvent)41 StateEvent (io.siddhi.core.event.state.StateEvent)38 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)34 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)14 GroupedComplexEvent (io.siddhi.core.event.GroupedComplexEvent)13 LinkedList (java.util.LinkedList)12 Map (java.util.Map)12 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)10 ArrayList (java.util.ArrayList)10 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)7 HashMap (java.util.HashMap)7 Attribute (io.siddhi.query.api.definition.Attribute)5 Event (io.siddhi.core.event.Event)4 TimePeriod (io.siddhi.query.api.aggregation.TimePeriod)4 SnapshotableStreamEventQueue (io.siddhi.core.event.stream.holder.SnapshotableStreamEventQueue)3 SiddhiAppRuntimeException (io.siddhi.core.exception.SiddhiAppRuntimeException)3 Table (io.siddhi.core.table.Table)3 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)2