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);
}
}
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);
}
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);
}
}
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);
}
}
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();
}
}
}
Aggregations