use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.
the class LengthWindowProcessor method process.
@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner, WindowState state) {
synchronized (state) {
long currentTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime();
while (streamEventChunk.hasNext()) {
StreamEvent streamEvent = streamEventChunk.next();
StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
clonedEvent.setType(StreamEvent.Type.EXPIRED);
if (state.count < length) {
state.count++;
state.expiredEventQueue.add(clonedEvent);
} else {
StreamEvent firstEvent = state.expiredEventQueue.poll();
if (firstEvent != null) {
firstEvent.setTimestamp(currentTime);
streamEventChunk.insertBeforeCurrent(firstEvent);
state.expiredEventQueue.add(clonedEvent);
} else {
StreamEvent resetEvent = streamEventCloner.copyStreamEvent(streamEvent);
resetEvent.setType(ComplexEvent.Type.RESET);
// adding resetEvent and clonedEvent event to the streamEventChunk
// since we are using insertAfterCurrent(), the final order will be
// currentEvent > clonedEvent (or expiredEvent) > resetEvent
streamEventChunk.insertAfterCurrent(resetEvent);
streamEventChunk.insertAfterCurrent(clonedEvent);
// since we manually added resetEvent and clonedEvent in earlier step
// we have to skip those two events from getting processed in the next
// iteration. Hence, calling next() twice.
streamEventChunk.next();
streamEventChunk.next();
}
}
}
}
nextProcessor.process(streamEventChunk);
}
use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.
the class SortWindowProcessor method process.
@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner, WindowState state) {
synchronized (state) {
long currentTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime();
StreamEvent streamEvent = streamEventChunk.getFirst();
streamEventChunk.clear();
while (streamEvent != null) {
StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
clonedEvent.setType(StreamEvent.Type.EXPIRED);
StreamEvent next = streamEvent.getNext();
streamEvent.setNext(null);
streamEventChunk.add(streamEvent);
state.sortedWindow.add(clonedEvent);
if (state.sortedWindow.size() > lengthToKeep) {
Collections.sort(state.sortedWindow, eventComparator);
StreamEvent expiredEvent = state.sortedWindow.remove(state.sortedWindow.size() - 1);
expiredEvent.setTimestamp(currentTime);
streamEventChunk.add(expiredEvent);
}
streamEvent = next;
}
}
nextProcessor.process(streamEventChunk);
}
use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.
the class TimeBatchWindowProcessor method process.
@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner, WindowState state) {
synchronized (state) {
if (nextEmitTime == -1) {
long currentTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime();
if (isStartTimeEnabled) {
nextEmitTime = getNextEmitTime(currentTime);
} else {
nextEmitTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime() + timeInMilliSeconds;
}
scheduler.notifyAt(nextEmitTime);
}
long currentTime = siddhiQueryContext.getSiddhiAppContext().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);
if (state.resetEvent == null) {
state.resetEvent = streamEventCloner.copyStreamEvent(streamEvent);
state.resetEvent.setType(ComplexEvent.Type.RESET);
}
if (!isStreamCurrentEvents) {
state.currentEventQueue.add(clonedStreamEvent);
} else if (state.expiredEventQueue != null) {
clonedStreamEvent.setType(StreamEvent.Type.EXPIRED);
state.expiredEventQueue.add(clonedStreamEvent);
}
}
if (!isStreamCurrentEvents) {
streamEventChunk.clear();
}
if (sendEvents) {
if (outputExpectsExpiredEvents && state.expiredEventQueue.getFirst() != null) {
while (state.expiredEventQueue.hasNext()) {
StreamEvent expiredEvent = state.expiredEventQueue.next();
expiredEvent.setTimestamp(currentTime);
}
streamEventChunk.add(state.expiredEventQueue.getFirst());
state.expiredEventQueue.clear();
}
if (state.resetEvent != null) {
streamEventChunk.add(state.resetEvent);
state.resetEvent = null;
}
if (state.currentEventQueue != null && state.currentEventQueue.getFirst() != null) {
if (state.expiredEventQueue != null) {
state.currentEventQueue.reset();
while (state.currentEventQueue.hasNext()) {
StreamEvent currentEvent = state.currentEventQueue.next();
StreamEvent toExpireEvent = streamEventCloner.copyStreamEvent(currentEvent);
toExpireEvent.setType(StreamEvent.Type.EXPIRED);
state.expiredEventQueue.add(toExpireEvent);
}
}
streamEventChunk.add(state.currentEventQueue.getFirst());
state.currentEventQueue.clear();
}
}
}
if (streamEventChunk.getFirst() != null) {
nextProcessor.process(streamEventChunk);
}
}
use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.
the class TimeWindowProcessor method process.
@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner, WindowState state) {
synchronized (state) {
SnapshotableStreamEventQueue expiredEventQueue = state.expiredEventQueue;
while (streamEventChunk.hasNext()) {
StreamEvent streamEvent = streamEventChunk.next();
long currentTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime();
expiredEventQueue.reset();
while (expiredEventQueue.hasNext()) {
StreamEvent expiredEvent = expiredEventQueue.next();
long timeDiff = expiredEvent.getTimestamp() - currentTime + timeInMilliSeconds;
if (timeDiff <= 0) {
expiredEventQueue.remove();
expiredEvent.setTimestamp(currentTime);
streamEventChunk.insertBeforeCurrent(expiredEvent);
} else {
break;
}
}
if (streamEvent.getType() == StreamEvent.Type.CURRENT) {
StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
clonedEvent.setType(StreamEvent.Type.EXPIRED);
expiredEventQueue.add(clonedEvent);
if (state.lastTimestamp < clonedEvent.getTimestamp()) {
scheduler.notifyAt(clonedEvent.getTimestamp() + timeInMilliSeconds);
state.lastTimestamp = clonedEvent.getTimestamp();
}
} else {
streamEventChunk.remove();
}
}
expiredEventQueue.reset();
}
nextProcessor.process(streamEventChunk);
}
use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.
the class SnapshotableEventQueueTestCase method incrementalPersistenceTest3.
@Test
public void incrementalPersistenceTest3() throws InterruptedException, IOException, ClassNotFoundException {
MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
metaStreamEvent.addOutputData(new Attribute("symbol", Attribute.Type.STRING));
metaStreamEvent.addOutputData(new Attribute("price", Attribute.Type.FLOAT));
metaStreamEvent.addOutputData(new Attribute("volume", Attribute.Type.LONG));
StreamEventCloner streamEventCloner = new StreamEventCloner(metaStreamEvent, new StreamEventFactory(metaStreamEvent));
SnapshotableStreamEventQueue snapshotableStreamEventQueue = new SnapshotableStreamEventQueue(new StreamEventClonerHolder(streamEventCloner));
StreamEvent streamEvent = new StreamEvent(metaStreamEvent.getBeforeWindowData().size(), metaStreamEvent.getOnAfterWindowData().size(), metaStreamEvent.getOutputData().size());
streamEvent.setOutputData(new Object[] { "IBM", 500.6f, 1 });
for (int i = 0; i < 10; i++) {
streamEvent.getOutputData()[2] = i;
snapshotableStreamEventQueue.add(streamEventCloner.copyStreamEvent(streamEvent));
}
HashMap<Long, String> snapshots = new HashMap<>();
Snapshot snapshot1 = snapshotableStreamEventQueue.getSnapshot();
StreamEvent streamEvents = (StreamEvent) snapshot1.getState();
Assert.assertTrue(streamEvents != null);
snapshots.put(3L, toString(snapshot1));
snapshotableStreamEventQueue.next();
snapshotableStreamEventQueue.next();
snapshotableStreamEventQueue.next();
for (int i = 7; i < 10; i++) {
snapshotableStreamEventQueue.next();
snapshotableStreamEventQueue.remove();
}
Snapshot snapshot2 = snapshotableStreamEventQueue.getSnapshot();
ArrayList<Operation> operationLog = (ArrayList<Operation>) snapshot2.getState();
Assert.assertTrue(operationLog != null);
snapshots.put(4L, toString(snapshot2));
for (int i = 10; i < 15; i++) {
streamEvent.getOutputData()[2] = i;
snapshotableStreamEventQueue.add(streamEventCloner.copyStreamEvent(streamEvent));
}
Snapshot snapshot3 = snapshotableStreamEventQueue.getSnapshot();
operationLog = (ArrayList<Operation>) snapshot3.getState();
Assert.assertTrue(operationLog != null);
snapshots.put(5L, toString(snapshot3));
SnapshotableStreamEventQueue snapshotableStreamEventQueue2 = new SnapshotableStreamEventQueue(new StreamEventClonerHolder(streamEventCloner));
SnapshotStateList snapshotStateList = new SnapshotStateList();
for (Map.Entry<Long, String> entry : snapshots.entrySet()) {
snapshotStateList.putSnapshotState(entry.getKey(), (Snapshot) fromString(entry.getValue()));
}
snapshotableStreamEventQueue2.restore(snapshotStateList);
Assert.assertEquals(snapshotableStreamEventQueue, snapshotableStreamEventQueue2);
}
Aggregations