use of org.ballerinalang.siddhi.core.event.GroupedComplexEvent in project ballerina by ballerina-lang.
the class QuerySelector method processGroupBy.
private ComplexEventChunk<ComplexEvent> processGroupBy(ComplexEventChunk complexEventChunk) {
complexEventChunk.reset();
ComplexEventChunk<ComplexEvent> currentComplexEventChunk = new ComplexEventChunk<ComplexEvent>(complexEventChunk.isBatch());
synchronized (this) {
int limitCount = 0;
while (complexEventChunk.hasNext()) {
ComplexEvent event = complexEventChunk.next();
switch(event.getType()) {
case CURRENT:
case EXPIRED:
eventPopulator.populateStateEvent(event);
String groupedByKey = groupByKeyGenerator.constructEventKey(event);
GroupByAggregationAttributeExecutor.getKeyThreadLocal().set(groupedByKey);
for (AttributeProcessor attributeProcessor : attributeProcessorList) {
attributeProcessor.process(event);
}
if ((event.getType() == StreamEvent.Type.CURRENT && currentOn) || (event.getType() == StreamEvent.Type.EXPIRED && expiredOn)) {
if (!(havingConditionExecutor != null && !havingConditionExecutor.execute(event))) {
complexEventChunk.remove();
if (limit == SiddhiConstants.UNKNOWN_STATE) {
currentComplexEventChunk.add(new GroupedComplexEvent(groupedByKey, event));
} else {
if (limitCount < limit) {
currentComplexEventChunk.add(new GroupedComplexEvent(groupedByKey, event));
limitCount++;
}
}
}
}
GroupByAggregationAttributeExecutor.getKeyThreadLocal().remove();
break;
case TIMER:
break;
case RESET:
for (AttributeProcessor attributeProcessor : attributeProcessorList) {
attributeProcessor.process(event);
}
break;
}
}
}
if (isOrderBy) {
orderEventChunk(complexEventChunk);
}
if (limit != SiddhiConstants.UNKNOWN_STATE) {
limitEventChunk(complexEventChunk);
}
currentComplexEventChunk.reset();
if (currentComplexEventChunk.hasNext()) {
return currentComplexEventChunk;
}
return null;
}
use of org.ballerinalang.siddhi.core.event.GroupedComplexEvent in project ballerina by ballerina-lang.
the class FirstGroupByPerEventOutputRateLimiter method process.
@Override
public void process(ComplexEventChunk complexEventChunk) {
complexEventChunk.reset();
ArrayList<ComplexEventChunk<ComplexEvent>> outputEventChunks = new ArrayList<ComplexEventChunk<ComplexEvent>>();
synchronized (this) {
while (complexEventChunk.hasNext()) {
ComplexEvent event = complexEventChunk.next();
if (event.getType() == ComplexEvent.Type.CURRENT || event.getType() == ComplexEvent.Type.EXPIRED) {
complexEventChunk.remove();
GroupedComplexEvent groupedComplexEvent = ((GroupedComplexEvent) event);
if (!groupByKeys.contains(groupedComplexEvent.getGroupKey())) {
groupByKeys.add(groupedComplexEvent.getGroupKey());
allComplexEventChunk.add(groupedComplexEvent.getComplexEvent());
}
if (++counter == value) {
if (allComplexEventChunk.getFirst() != null) {
ComplexEventChunk<ComplexEvent> outputEventChunk = new ComplexEventChunk<ComplexEvent>(complexEventChunk.isBatch());
outputEventChunk.add(allComplexEventChunk.getFirst());
outputEventChunks.add(outputEventChunk);
allComplexEventChunk.clear();
counter = 0;
groupByKeys.clear();
} else {
counter = 0;
groupByKeys.clear();
}
}
}
}
}
for (ComplexEventChunk eventChunk : outputEventChunks) {
sendToCallBacks(eventChunk);
}
}
use of org.ballerinalang.siddhi.core.event.GroupedComplexEvent in project ballerina by ballerina-lang.
the class LastGroupByPerEventOutputRateLimiter method process.
@Override
public void process(ComplexEventChunk complexEventChunk) {
complexEventChunk.reset();
ArrayList<ComplexEventChunk<ComplexEvent>> outputEventChunks = new ArrayList<ComplexEventChunk<ComplexEvent>>();
synchronized (this) {
while (complexEventChunk.hasNext()) {
ComplexEvent event = complexEventChunk.next();
if (event.getType() == ComplexEvent.Type.CURRENT || event.getType() == ComplexEvent.Type.EXPIRED) {
complexEventChunk.remove();
GroupedComplexEvent groupedComplexEvent = ((GroupedComplexEvent) event);
allGroupByKeyEvents.put(groupedComplexEvent.getGroupKey(), groupedComplexEvent.getComplexEvent());
if (++counter == value) {
counter = 0;
if (allGroupByKeyEvents.size() != 0) {
ComplexEventChunk<ComplexEvent> outputEventChunk = new ComplexEventChunk<ComplexEvent>(complexEventChunk.isBatch());
for (ComplexEvent complexEvent : allGroupByKeyEvents.values()) {
outputEventChunk.add(complexEvent);
}
allGroupByKeyEvents.clear();
outputEventChunks.add(outputEventChunk);
}
}
}
}
}
for (ComplexEventChunk eventChunk : outputEventChunks) {
sendToCallBacks(eventChunk);
}
}
use of org.ballerinalang.siddhi.core.event.GroupedComplexEvent in project ballerina by ballerina-lang.
the class AggregationGroupByWindowedPerSnapshotOutputRateLimiter method constructOutputChunk.
private void constructOutputChunk(List<ComplexEventChunk<ComplexEvent>> outputEventChunks) {
ComplexEventChunk<ComplexEvent> outputEventChunk = new ComplexEventChunk<ComplexEvent>(false);
for (GroupedComplexEvent originalComplexEvent : eventList) {
String currentGroupByKey = originalComplexEvent.getGroupKey();
Map<Integer, Object> currentAggregateAttributeValueMap = groupByAggregateAttributeValueMap.get(currentGroupByKey);
ComplexEvent eventCopy = cloneComplexEvent(originalComplexEvent.getComplexEvent());
for (Integer position : aggregateAttributePositionList) {
eventCopy.getOutputData()[position] = currentAggregateAttributeValueMap.get(position);
}
outputEventChunk.add(eventCopy);
}
outputEventChunks.add(outputEventChunk);
}
use of org.ballerinalang.siddhi.core.event.GroupedComplexEvent in project ballerina by ballerina-lang.
the class AggregationGroupByWindowedPerSnapshotOutputRateLimiter method process.
@Override
public void process(ComplexEventChunk complexEventChunk) {
complexEventChunk.reset();
List<ComplexEventChunk<ComplexEvent>> outputEventChunks = new ArrayList<ComplexEventChunk<ComplexEvent>>();
synchronized (this) {
complexEventChunk.reset();
String currentGroupByKey = null;
Map<Integer, Object> currentAggregateAttributeValueMap = null;
while (complexEventChunk.hasNext()) {
ComplexEvent event = complexEventChunk.next();
if (event.getType() == ComplexEvent.Type.TIMER) {
tryFlushEvents(outputEventChunks, event);
} else {
complexEventChunk.remove();
tryFlushEvents(outputEventChunks, event);
GroupedComplexEvent groupedComplexEvent = ((GroupedComplexEvent) event);
if (currentGroupByKey == null || !currentGroupByKey.equals(groupedComplexEvent.getGroupKey())) {
currentGroupByKey = groupedComplexEvent.getGroupKey();
currentAggregateAttributeValueMap = groupByAggregateAttributeValueMap.get(currentGroupByKey);
if (currentAggregateAttributeValueMap == null) {
currentAggregateAttributeValueMap = new HashMap<Integer, Object>(aggregateAttributePositionList.size());
groupByAggregateAttributeValueMap.put(currentGroupByKey, currentAggregateAttributeValueMap);
}
}
if (groupedComplexEvent.getType() == ComplexEvent.Type.CURRENT) {
eventList.add(groupedComplexEvent);
for (Integer position : aggregateAttributePositionList) {
currentAggregateAttributeValueMap.put(position, event.getOutputData()[position]);
}
} else if (groupedComplexEvent.getType() == ComplexEvent.Type.EXPIRED) {
for (Iterator<GroupedComplexEvent> iterator = eventList.iterator(); iterator.hasNext(); ) {
GroupedComplexEvent currentEvent = iterator.next();
if (comparator.compare(currentEvent.getComplexEvent(), groupedComplexEvent.getComplexEvent()) == 0) {
iterator.remove();
for (Integer position : aggregateAttributePositionList) {
currentAggregateAttributeValueMap.put(position, groupedComplexEvent.getOutputData()[position]);
}
break;
}
}
} else if (groupedComplexEvent.getType() == ComplexEvent.Type.RESET) {
eventList.clear();
groupByAggregateAttributeValueMap.clear();
}
}
}
}
for (ComplexEventChunk eventChunk : outputEventChunks) {
sendToCallBacks(eventChunk);
}
}
Aggregations