use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.
the class TimeBatchWindowProcessor method process.
@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
synchronized (this) {
if (nextEmitTime == -1) {
long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
if (isStartTimeEnabled) {
nextEmitTime = getNextEmitTime(currentTime);
} else {
nextEmitTime = siddhiAppContext.getTimestampGenerator().currentTime() + timeInMilliSeconds;
}
scheduler.notifyAt(nextEmitTime);
}
long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
boolean sendEvents;
if (currentTime >= nextEmitTime) {
nextEmitTime += timeInMilliSeconds;
scheduler.notifyAt(nextEmitTime);
sendEvents = true;
} else {
sendEvents = false;
}
while (streamEventChunk.hasNext()) {
StreamEvent streamEvent = streamEventChunk.next();
if (streamEvent.getType() != ComplexEvent.Type.CURRENT) {
continue;
}
StreamEvent clonedStreamEvent = streamEventCloner.copyStreamEvent(streamEvent);
currentEventChunk.add(clonedStreamEvent);
}
streamEventChunk.clear();
if (sendEvents) {
if (outputExpectsExpiredEvents) {
if (expiredEventChunk.getFirst() != null) {
while (expiredEventChunk.hasNext()) {
StreamEvent expiredEvent = expiredEventChunk.next();
expiredEvent.setTimestamp(currentTime);
}
streamEventChunk.add(expiredEventChunk.getFirst());
}
}
if (expiredEventChunk != null) {
expiredEventChunk.clear();
}
if (currentEventChunk.getFirst() != null) {
// add reset event in front of current events
streamEventChunk.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);
streamEventChunk.add(currentEventChunk.getFirst());
}
currentEventChunk.clear();
}
}
if (streamEventChunk.getFirst() != null) {
streamEventChunk.setBatch(true);
nextProcessor.process(streamEventChunk);
streamEventChunk.setBatch(false);
}
}
use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.
the class TimeBatchWindowProcessor method init.
@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, boolean outputExpectsExpiredEvents, SiddhiAppContext siddhiAppContext) {
this.outputExpectsExpiredEvents = outputExpectsExpiredEvents;
this.siddhiAppContext = siddhiAppContext;
if (outputExpectsExpiredEvents) {
this.expiredEventChunk = new ComplexEventChunk<StreamEvent>(false);
}
if (attributeExpressionExecutors.length == 1) {
if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.INT) {
timeInMilliSeconds = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
} else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.LONG) {
timeInMilliSeconds = (Long) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
} else {
throw new SiddhiAppValidationException("Time window's parameter attribute should be either " + "int or long, but found " + attributeExpressionExecutors[0].getReturnType());
}
} else {
throw new SiddhiAppValidationException("Time window should have constant parameter attribute but " + "found a dynamic attribute " + attributeExpressionExecutors[0].getClass().getCanonicalName());
}
} else if (attributeExpressionExecutors.length == 2) {
if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.INT) {
timeInMilliSeconds = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
} else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.LONG) {
timeInMilliSeconds = (Long) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
} else {
throw new SiddhiAppValidationException("Time window's parameter attribute should be either " + "int or long, but found " + attributeExpressionExecutors[0].getReturnType());
}
} else {
throw new SiddhiAppValidationException("Time window should have constant parameter attribute but " + "found a dynamic attribute " + attributeExpressionExecutors[0].getClass().getCanonicalName());
}
// start time
isStartTimeEnabled = true;
if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.INT) {
startTime = Integer.parseInt(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
} else {
startTime = Long.parseLong(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
}
} else {
throw new SiddhiAppValidationException("Time window should only have one or two parameters. " + "(<int|long|time> windowTime), but found " + attributeExpressionExecutors.length + " input " + "attributes");
}
}
use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.
the class TimeLengthWindowProcessor method process.
@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
synchronized (this) {
long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
while (streamEventChunk.hasNext()) {
StreamEvent streamEvent = streamEventChunk.next();
expiredEventChunk.reset();
while (expiredEventChunk.hasNext()) {
StreamEvent expiredEvent = expiredEventChunk.next();
long timeDiff = expiredEvent.getTimestamp() - currentTime + timeInMilliSeconds;
if (timeDiff <= 0) {
expiredEventChunk.remove();
count--;
expiredEvent.setTimestamp(currentTime);
streamEventChunk.insertBeforeCurrent(expiredEvent);
} else {
break;
}
}
expiredEventChunk.reset();
if (streamEvent.getType() == StreamEvent.Type.CURRENT) {
StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
clonedEvent.setType(StreamEvent.Type.EXPIRED);
if (count < length) {
count++;
this.expiredEventChunk.add(clonedEvent);
} else {
StreamEvent firstEvent = this.expiredEventChunk.poll();
if (firstEvent != null) {
firstEvent.setTimestamp(currentTime);
streamEventChunk.insertBeforeCurrent(firstEvent);
this.expiredEventChunk.add(clonedEvent);
}
}
scheduler.notifyAt(clonedEvent.getTimestamp() + timeInMilliSeconds);
} else {
streamEventChunk.remove();
}
}
}
streamEventChunk.reset();
if (streamEventChunk.hasNext()) {
nextProcessor.process(streamEventChunk);
}
}
use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.
the class TimeWindowProcessor method init.
@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, boolean outputExpectsExpiredEvents, SiddhiAppContext siddhiAppContext) {
this.siddhiAppContext = siddhiAppContext;
this.expiredEventChunk = new ComplexEventChunk<StreamEvent>(false);
if (attributeExpressionExecutors.length == 1) {
if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.INT) {
timeInMilliSeconds = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
} else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.LONG) {
timeInMilliSeconds = (Long) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
} else {
throw new SiddhiAppValidationException("Time window's parameter attribute should be either " + "int or long, but found " + attributeExpressionExecutors[0].getReturnType());
}
} else {
throw new SiddhiAppValidationException("Time window should have constant parameter attribute but " + "found a dynamic attribute " + attributeExpressionExecutors[0].getClass().getCanonicalName());
}
} else {
throw new SiddhiAppValidationException("Time window should only have one parameter (<int|long|time> " + "windowTime), but found " + attributeExpressionExecutors.length + " input attributes");
}
}
use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.
the class ExternalTimeBatchWindowProcessor method flushToOutputChunk.
private void flushToOutputChunk(StreamEventCloner streamEventCloner, List<ComplexEventChunk<StreamEvent>> complexEventChunks, long currentTime, boolean preserveCurrentEvents) {
ComplexEventChunk<StreamEvent> newEventChunk = new ComplexEventChunk<StreamEvent>(true);
if (outputExpectsExpiredEvents) {
if (expiredEventChunk.getFirst() != null) {
// mark the timestamp for the expiredType event
expiredEventChunk.reset();
while (expiredEventChunk.hasNext()) {
StreamEvent expiredEvent = expiredEventChunk.next();
expiredEvent.setTimestamp(currentTime);
}
// add expired event to newEventChunk.
newEventChunk.add(expiredEventChunk.getFirst());
}
}
if (expiredEventChunk != null) {
expiredEventChunk.clear();
}
if (currentEventChunk.getFirst() != null) {
// add reset event in front of current events
resetEvent.setTimestamp(currentTime);
newEventChunk.add(resetEvent);
resetEvent = null;
// move to expired events
if (preserveCurrentEvents || storeExpiredEvents) {
currentEventChunk.reset();
while (currentEventChunk.hasNext()) {
StreamEvent currentEvent = currentEventChunk.next();
StreamEvent toExpireEvent = streamEventCloner.copyStreamEvent(currentEvent);
toExpireEvent.setType(StreamEvent.Type.EXPIRED);
expiredEventChunk.add(toExpireEvent);
}
}
// add current event chunk to next processor
newEventChunk.add(currentEventChunk.getFirst());
}
currentEventChunk.clear();
if (newEventChunk.getFirst() != null) {
complexEventChunks.add(newEventChunk);
}
}
Aggregations