{
// apply new data to aggregates
for (EventBean aNewData : newData)
{
eventsPerStream[0] = aNewData;
MultiKeyUntyped mk = generateGroupKey(eventsPerStream, true);
// if this is a newly encountered group, generate the remove stream event
if (groupRepsView.put(mk, new EventBean[] {aNewData}) == null)
{
if (prototype.isSelectRStream())
{
generateOutputBatched(mk, eventsPerStream, false, generateSynthetic, oldEvents, oldEventsSortKey);
}
}
aggregationService.applyEnter(eventsPerStream, mk, agentInstanceContext);
}
}
if (oldData != null)
{
// apply old data to aggregates
for (EventBean anOldData : oldData)
{
eventsPerStream[0] = anOldData;
MultiKeyUntyped mk = generateGroupKey(eventsPerStream, true);
if (groupRepsView.put(mk, new EventBean[] {anOldData}) == null)
{
if (prototype.isSelectRStream())
{
generateOutputBatched(mk, eventsPerStream, false, generateSynthetic, oldEvents, oldEventsSortKey);
}
}
aggregationService.applyLeave(eventsPerStream, mk, agentInstanceContext);
}
}
}
generateOutputBatchedArr(groupRepsView, true, generateSynthetic, newEvents, newEventsSortKey);
EventBean[] newEventsArr = (newEvents.isEmpty()) ? null : newEvents.toArray(new EventBean[newEvents.size()]);
EventBean[] oldEventsArr = null;
if (prototype.isSelectRStream())
{
oldEventsArr = (oldEvents.isEmpty()) ? null : oldEvents.toArray(new EventBean[oldEvents.size()]);
}
if (orderByProcessor != null)
{
MultiKeyUntyped[] sortKeysNew = (newEventsSortKey.isEmpty()) ? null : newEventsSortKey.toArray(new MultiKeyUntyped[newEventsSortKey.size()]);
newEventsArr = orderByProcessor.sort(newEventsArr, sortKeysNew, agentInstanceContext);
if (prototype.isSelectRStream())
{
MultiKeyUntyped[] sortKeysOld = (oldEventsSortKey.isEmpty()) ? null : oldEventsSortKey.toArray(new MultiKeyUntyped[oldEventsSortKey.size()]);
oldEventsArr = orderByProcessor.sort(oldEventsArr, sortKeysOld, agentInstanceContext);
}
}
if ((newEventsArr == null) && (oldEventsArr == null))
{
return null;
}
return new UniformPair<EventBean[]>(newEventsArr, oldEventsArr);
}
else if (outputLimitLimitType == OutputLimitLimitType.FIRST)
{
List<EventBean> newEvents = new LinkedList<EventBean>();
List<EventBean> oldEvents = null;
if (prototype.isSelectRStream())
{
oldEvents = new LinkedList<EventBean>();
}
List<MultiKeyUntyped> newEventsSortKey = null;
List<MultiKeyUntyped> oldEventsSortKey = null;
if (orderByProcessor != null)
{
newEventsSortKey = new LinkedList<MultiKeyUntyped>();
if (prototype.isSelectRStream())
{
oldEventsSortKey = new LinkedList<MultiKeyUntyped>();
}
}
if (prototype.getOptionalHavingNode() == null) {
groupRepsView.clear();
for (UniformPair<EventBean[]> pair : viewEventsList)
{
EventBean[] newData = pair.getFirst();
EventBean[] oldData = pair.getSecond();
if (newData != null)
{
// apply new data to aggregates
for (EventBean aNewData : newData)
{
EventBean[] eventsPerStream = new EventBean[] {aNewData};
MultiKeyUntyped mk = generateGroupKey(eventsPerStream, true);
OutputConditionPolled outputStateGroup = outputState.get(mk);
if (outputStateGroup == null) {
try {
outputStateGroup = OutputConditionPolledFactory.createCondition(prototype.getOutputLimitSpec(), agentInstanceContext);
}
catch (ExprValidationException e) {
log.error("Error starting output limit for group for statement '" + agentInstanceContext.getStatementContext().getStatementName() + "'");
}
outputState.put(mk, outputStateGroup);
}
boolean pass = outputStateGroup.updateOutputCondition(1, 0);
if (pass) {
// if this is a newly encountered group, generate the remove stream event
if (groupRepsView.put(mk, eventsPerStream) == null)
{
if (prototype.isSelectRStream())
{
generateOutputBatched(mk, eventsPerStream, false, generateSynthetic, oldEvents, oldEventsSortKey);
}
}
}
aggregationService.applyEnter(eventsPerStream, mk, agentInstanceContext);
}
}
if (oldData != null)
{
// apply old data to aggregates
for (EventBean anOldData : oldData)
{
EventBean[] eventsPerStream = new EventBean[] {anOldData};
MultiKeyUntyped mk = generateGroupKey(eventsPerStream, true);
OutputConditionPolled outputStateGroup = outputState.get(mk);
if (outputStateGroup == null) {
try {
outputStateGroup = OutputConditionPolledFactory.createCondition(prototype.getOutputLimitSpec(), agentInstanceContext);
}
catch (ExprValidationException e) {
log.error("Error starting output limit for group for statement '" + agentInstanceContext.getStatementContext().getStatementName() + "'");
}
outputState.put(mk, outputStateGroup);
}
boolean pass = outputStateGroup.updateOutputCondition(0, 1);
if (pass) {
if (groupRepsView.put(mk, eventsPerStream) == null)
{
if (prototype.isSelectRStream())
{
generateOutputBatched(mk, eventsPerStream, false, generateSynthetic, oldEvents, oldEventsSortKey);
}
}
}
aggregationService.applyLeave(eventsPerStream, mk, agentInstanceContext);
}
}
}
}
else { // having clause present, having clause evaluates at the level of individual posts
EventBean[] eventsPerStreamOneStream = new EventBean[1];
groupRepsView.clear();
for (UniformPair<EventBean[]> pair : viewEventsList)
{
EventBean[] newData = pair.getFirst();
EventBean[] oldData = pair.getSecond();
MultiKeyUntyped[] newDataMultiKey = generateGroupKeys(newData, true);
MultiKeyUntyped[] oldDataMultiKey = generateGroupKeys(oldData, false);
if (newData != null)
{
// apply new data to aggregates
for (int i = 0; i < newData.length; i++)
{
eventsPerStreamOneStream[0] = newData[i];
aggregationService.applyEnter(eventsPerStreamOneStream, newDataMultiKey[i], agentInstanceContext);
}
}
if (oldData != null)
{
// apply old data to aggregates
for (int i = 0; i < oldData.length; i++)
{
eventsPerStreamOneStream[0] = oldData[i];
aggregationService.applyLeave(eventsPerStreamOneStream, oldDataMultiKey[i], agentInstanceContext);
}
}
// evaluate having-clause
if (newData != null)
{
for (int i = 0; i < newData.length; i++)
{
MultiKeyUntyped mk = newDataMultiKey[i];
eventsPerStreamOneStream[0] = newData[i];
aggregationService.setCurrentAccess(mk, agentInstanceContext.getAgentInstanceIds());
// Filter the having clause
Boolean result = (Boolean) prototype.getOptionalHavingNode().evaluate(eventsPerStreamOneStream, true, agentInstanceContext);
if ((result == null) || (!result))
{
continue;
}
OutputConditionPolled outputStateGroup = outputState.get(mk);
if (outputStateGroup == null) {
try {
outputStateGroup = OutputConditionPolledFactory.createCondition(prototype.getOutputLimitSpec(), agentInstanceContext);
}
catch (ExprValidationException e) {
log.error("Error starting output limit for group for statement '" + agentInstanceContext.getStatementContext().getStatementName() + "'");
}
outputState.put(mk, outputStateGroup);
}
boolean pass = outputStateGroup.updateOutputCondition(0, 1);
if (pass) {
EventBean[] eventsPerStream = new EventBean[] {newData[i]};
if (groupRepsView.put(mk, eventsPerStream) == null)
{
if (prototype.isSelectRStream())
{
generateOutputBatched(mk, eventsPerStream, true, generateSynthetic, oldEvents, oldEventsSortKey);
}
}
}
}
}
// evaluate having-clause
if (oldData != null)
{
for (int i = 0; i < oldData.length; i++)
{
MultiKeyUntyped mk = oldDataMultiKey[i];
eventsPerStreamOneStream[0] = oldData[i];
aggregationService.setCurrentAccess(mk, agentInstanceContext.getAgentInstanceIds());
// Filter the having clause
Boolean result = (Boolean) prototype.getOptionalHavingNode().evaluate(eventsPerStreamOneStream, false, agentInstanceContext);
if ((result == null) || (!result))
{
continue;
}
OutputConditionPolled outputStateGroup = outputState.get(mk);
if (outputStateGroup == null) {
try {
outputStateGroup = OutputConditionPolledFactory.createCondition(prototype.getOutputLimitSpec(), agentInstanceContext);
}
catch (ExprValidationException e) {
log.error("Error starting output limit for group for statement '" + agentInstanceContext.getStatementContext().getStatementName() + "'");
}
outputState.put(mk, outputStateGroup);
}
boolean pass = outputStateGroup.updateOutputCondition(0, 1);
if (pass) {
EventBean[] eventsPerStream = new EventBean[] {oldData[i]};
if (groupRepsView.put(mk, eventsPerStream) == null)
{
if (prototype.isSelectRStream())
{
generateOutputBatched(mk, eventsPerStream, false, generateSynthetic, oldEvents, oldEventsSortKey);
}
}
}
}
}
}
}
generateOutputBatchedArr(groupRepsView, true, generateSynthetic, newEvents, newEventsSortKey);
EventBean[] newEventsArr = (newEvents.isEmpty()) ? null : newEvents.toArray(new EventBean[newEvents.size()]);
EventBean[] oldEventsArr = null;
if (prototype.isSelectRStream())
{
oldEventsArr = (oldEvents.isEmpty()) ? null : oldEvents.toArray(new EventBean[oldEvents.size()]);
}
if (orderByProcessor != null)
{
MultiKeyUntyped[] sortKeysNew = (newEventsSortKey.isEmpty()) ? null : newEventsSortKey.toArray(new MultiKeyUntyped[newEventsSortKey.size()]);
newEventsArr = orderByProcessor.sort(newEventsArr, sortKeysNew, agentInstanceContext);
if (prototype.isSelectRStream())
{
MultiKeyUntyped[] sortKeysOld = (oldEventsSortKey.isEmpty()) ? null : oldEventsSortKey.toArray(new MultiKeyUntyped[oldEventsSortKey.size()]);
oldEventsArr = orderByProcessor.sort(oldEventsArr, sortKeysOld, agentInstanceContext);
}
}
if ((newEventsArr == null) && (oldEventsArr == null))
{
return null;
}
return new UniformPair<EventBean[]>(newEventsArr, oldEventsArr);
}
else // (outputLimitLimitType == OutputLimitLimitType.LAST)
{
List<EventBean> newEvents = new LinkedList<EventBean>();
List<EventBean> oldEvents = null;
if (prototype.isSelectRStream())
{
oldEvents = new LinkedList<EventBean>();
}
List<MultiKeyUntyped> newEventsSortKey = null;
List<MultiKeyUntyped> oldEventsSortKey = null;
if (orderByProcessor != null)
{
newEventsSortKey = new LinkedList<MultiKeyUntyped>();
if (prototype.isSelectRStream())
{
oldEventsSortKey = new LinkedList<MultiKeyUntyped>();
}
}
groupRepsView.clear();
for (UniformPair<EventBean[]> pair : viewEventsList)
{
EventBean[] newData = pair.getFirst();
EventBean[] oldData = pair.getSecond();
if (newData != null)
{
// apply new data to aggregates
for (EventBean aNewData : newData)
{
EventBean[] eventsPerStream = new EventBean[] {aNewData};
MultiKeyUntyped mk = generateGroupKey(eventsPerStream, true);
// if this is a newly encountered group, generate the remove stream event
if (groupRepsView.put(mk, eventsPerStream) == null)
{
if (prototype.isSelectRStream())
{
generateOutputBatched(mk, eventsPerStream, false, generateSynthetic, oldEvents, oldEventsSortKey);
}
}
aggregationService.applyEnter(eventsPerStream, mk, agentInstanceContext);
}
}
if (oldData != null)
{
// apply old data to aggregates
for (EventBean anOldData : oldData)
{
EventBean[] eventsPerStream = new EventBean[] {anOldData};
MultiKeyUntyped mk = generateGroupKey(eventsPerStream, true);
if (groupRepsView.put(mk, eventsPerStream) == null)
{
if (prototype.isSelectRStream())
{