Search in sources :

Example 36 with StateEvent

use of io.siddhi.core.event.state.StateEvent 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 37 with StateEvent

use of io.siddhi.core.event.state.StateEvent in project siddhi by wso2.

the class AbsentStreamPreStateProcessor method addEveryState.

@Override
public void addEveryState(StateEvent stateEvent) {
    LogicalStreamPreState state = (LogicalStreamPreState) stateHolder.getState();
    lock.lock();
    try {
        StateEvent clonedEvent = stateEventCloner.copyStateEvent(stateEvent);
        clonedEvent.setType(ComplexEvent.Type.CURRENT);
        for (int i = stateId; i < clonedEvent.getStreamEvents().length; i++) {
            clonedEvent.setEvent(i, null);
        }
        state.getNewAndEveryStateEventList().add(clonedEvent);
        // Start the scheduler
        state.lastScheduledTime = stateEvent.getTimestamp() + waitingTime;
        scheduler.notifyAt(state.lastScheduledTime);
    } finally {
        lock.unlock();
        stateHolder.returnState(state);
    }
}
Also used : StateEvent(io.siddhi.core.event.state.StateEvent)

Example 38 with StateEvent

use of io.siddhi.core.event.state.StateEvent 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 39 with StateEvent

use of io.siddhi.core.event.state.StateEvent in project siddhi by wso2.

the class MultiValueVariableFunctionExecutor method execute.

public Object execute(ComplexEvent event) {
    List values = new LinkedList();
    StreamEvent aEvent = ((StateEvent) event).getStreamEvent(position);
    while (aEvent != null) {
        values.add(aEvent.getAttribute(position));
        aEvent = aEvent.getNext();
    }
    return values;
}
Also used : StreamEvent(io.siddhi.core.event.stream.StreamEvent) List(java.util.List) LinkedList(java.util.LinkedList) StateEvent(io.siddhi.core.event.state.StateEvent) LinkedList(java.util.LinkedList)

Example 40 with StateEvent

use of io.siddhi.core.event.state.StateEvent in project siddhi by wso2.

the class AbstractQueryableRecordTable method updateStoreTableSize.

private void updateStoreTableSize() throws ConnectionUnavailableException {
    if (cacheEnabled && !queryStoreWithoutCheckingCache.get()) {
        readWriteLock.writeLock().lock();
        try {
            // check if we need to check the size of store
            if (storeTableSize == -1 || (!cacheExpiryEnabled && storeSizeLastCheckedTime < siddhiAppContext.getTimestampGenerator().currentTime() - storeSizeCheckInterval)) {
                StateEvent stateEventForCaching = new StateEvent(1, 0);
                queryStoreWithoutCheckingCache.set(Boolean.TRUE);
                try {
                    StreamEvent preLoadedData = query(stateEventForCaching, compiledConditionForCaching, compiledSelectionForCaching, outputAttributesForCaching);
                    storeTableSize = findEventChunkSize(preLoadedData);
                    storeSizeLastCheckedTime = siddhiAppContext.getTimestampGenerator().currentTime();
                } finally {
                    queryStoreWithoutCheckingCache.set(Boolean.FALSE);
                }
            }
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }
}
Also used : OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent(io.siddhi.core.util.OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) StateEvent(io.siddhi.core.event.state.StateEvent)

Aggregations

StateEvent (io.siddhi.core.event.state.StateEvent)70 StreamEvent (io.siddhi.core.event.stream.StreamEvent)48 ComplexEventChunk (io.siddhi.core.event.ComplexEventChunk)36 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)26 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)19 ComplexEvent (io.siddhi.core.event.ComplexEvent)13 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)11 Map (java.util.Map)10 ArrayList (java.util.ArrayList)7 LinkedList (java.util.LinkedList)6 ConnectionUnavailableException (io.siddhi.core.exception.ConnectionUnavailableException)5 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)5 HashMap (java.util.HashMap)5 Attribute (io.siddhi.query.api.definition.Attribute)4 Event (io.siddhi.core.event.Event)3 SnapshotableStreamEventQueue (io.siddhi.core.event.stream.holder.SnapshotableStreamEventQueue)3 DatabaseRuntimeException (io.siddhi.core.exception.DatabaseRuntimeException)3 OnDemandQueryRuntimeException (io.siddhi.core.exception.OnDemandQueryRuntimeException)3 OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent (io.siddhi.core.util.OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent)3 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)2