/**************************************************************************************
* Copyright (C) 2008 EsperTech, Inc. All rights reserved. *
* http://esper.codehaus.org *
* http://www.espertech.com *
* ---------------------------------------------------------------------------------- *
* The software in this package is published under the terms of the GPL license *
* a copy of which has been included with this distribution in the license.txt file. *
**************************************************************************************/
package com.espertech.esper.epl.agg.service;
import com.espertech.esper.client.EventBean;
import com.espertech.esper.epl.agg.access.AggregationAccessorSlotPair;
import com.espertech.esper.epl.agg.access.AggregationState;
import com.espertech.esper.epl.agg.aggregator.AggregationMethod;
import com.espertech.esper.epl.core.MethodResolutionService;
import com.espertech.esper.epl.expression.ExprEvaluator;
import com.espertech.esper.epl.expression.ExprEvaluatorContext;
import com.espertech.esper.metrics.instrumentation.InstrumentationHelper;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* Implementation for handling aggregation with grouping by group-keys.
*/
public class AggSvcGroupByMixedAccessImpl extends AggregationServiceBaseGrouped
{
private final AggregationAccessorSlotPair[] accessorsFactory;
protected final AggregationStateFactory[] accessAggregations;
protected final boolean isJoin;
// maintain for each group a row of aggregator states that the expression node canb pull the data from via index
protected Map<Object, AggregationRowPair> aggregatorsPerGroup;
// maintain a current row for random access into the aggregator state table
// (row=groups, columns=expression nodes that have aggregation functions)
private AggregationRowPair currentAggregatorRow;
private Object currentGroupKey;
private MethodResolutionService methodResolutionService;
/**
* Ctor.
* @param evaluators - evaluate the sub-expression within the aggregate function (ie. sum(4*myNum))
* @param prototypes - collect the aggregation state that evaluators evaluate to, act as prototypes for new aggregations
* aggregation states for each group
* @param methodResolutionService - factory for creating additional aggregation method instances per group key
* @param accessorsFactory accessor definitions
* @param accessAggregations access aggs
* @param isJoin true for join, false for single-stream
*/
public AggSvcGroupByMixedAccessImpl(ExprEvaluator evaluators[],
AggregationMethodFactory prototypes[],
Object groupKeyBinding,
MethodResolutionService methodResolutionService,
AggregationAccessorSlotPair[] accessorsFactory,
AggregationStateFactory[] accessAggregations,
boolean isJoin)
{
super(evaluators, prototypes, groupKeyBinding);
this.accessorsFactory = accessorsFactory;
this.accessAggregations = accessAggregations;
this.isJoin = isJoin;
this.methodResolutionService = methodResolutionService;
this.aggregatorsPerGroup = new HashMap<Object, AggregationRowPair>();
}
public void clearResults(ExprEvaluatorContext exprEvaluatorContext)
{
aggregatorsPerGroup.clear();
}
public void applyEnter(EventBean[] eventsPerStream, Object groupByKey, ExprEvaluatorContext exprEvaluatorContext)
{
if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().qAggregationGroupedApplyEnterLeave(true, aggregators.length, accessAggregations.length, groupByKey);}
AggregationRowPair groupAggregators = aggregatorsPerGroup.get(groupByKey);
// The aggregators for this group do not exist, need to create them from the prototypes
if (groupAggregators == null)
{
AggregationMethod[] methods = methodResolutionService.newAggregators(aggregators, exprEvaluatorContext.getAgentInstanceId(), groupByKey, groupKeyBinding);
AggregationState[] states = methodResolutionService.newAccesses(exprEvaluatorContext.getAgentInstanceId(), isJoin, accessAggregations, groupByKey, groupKeyBinding);
groupAggregators = new AggregationRowPair(methods, states);
aggregatorsPerGroup.put(groupByKey, groupAggregators);
}
// For this row, evaluate sub-expressions, enter result
currentAggregatorRow = groupAggregators;
AggregationMethod[] groupAggMethods = groupAggregators.getMethods();
for (int i = 0; i < evaluators.length; i++) {
if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().qAggNoAccessEnterLeave(true, i, groupAggMethods[i], aggregators[i]);}
Object columnResult = evaluators[i].evaluate(eventsPerStream, true, exprEvaluatorContext);
groupAggMethods[i].enter(columnResult);
if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().aAggNoAccessEnterLeave(true, i, groupAggMethods[i]);}
}
AggregationState[] states = currentAggregatorRow.getStates();
for (int i = 0; i < states.length; i++) {
if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().qAggAccessEnterLeave(true, i, states[i], accessAggregations[i]);}
states[i].applyEnter(eventsPerStream, exprEvaluatorContext);
if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().aAggAccessEnterLeave(true, i, states[i]);}
}
internalHandleUpdated(groupByKey, groupAggregators);
if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().aAggregationGroupedApplyEnterLeave(true);}
}
public void applyLeave(EventBean[] eventsPerStream, Object groupByKey, ExprEvaluatorContext exprEvaluatorContext)
{
if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().qAggregationGroupedApplyEnterLeave(false, aggregators.length, accessAggregations.length, groupByKey);}
AggregationRowPair groupAggregators = aggregatorsPerGroup.get(groupByKey);
// The aggregators for this group do not exist, need to create them from the prototypes
if (groupAggregators == null)
{
AggregationMethod[] methods = methodResolutionService.newAggregators(aggregators, exprEvaluatorContext.getAgentInstanceId(), groupByKey, groupKeyBinding);
AggregationState[] states = methodResolutionService.newAccesses(exprEvaluatorContext.getAgentInstanceId(), isJoin, accessAggregations, groupByKey, groupKeyBinding);
groupAggregators = new AggregationRowPair(methods, states);
aggregatorsPerGroup.put(groupByKey, groupAggregators);
}
// For this row, evaluate sub-expressions, enter result
currentAggregatorRow = groupAggregators;
AggregationMethod[] groupAggMethods = groupAggregators.getMethods();
for (int i = 0; i < evaluators.length; i++)
{
if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().qAggNoAccessEnterLeave(false, i, groupAggMethods[i], aggregators[i]);}
Object columnResult = evaluators[i].evaluate(eventsPerStream, false, exprEvaluatorContext);
groupAggMethods[i].leave(columnResult);
if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().aAggNoAccessEnterLeave(false, i, groupAggMethods[i]);}
}
AggregationState[] states = currentAggregatorRow.getStates();
for (int i = 0; i < states.length; i++) {
if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().qAggAccessEnterLeave(false, i, states[i], accessAggregations[i]);}
states[i].applyLeave(eventsPerStream, exprEvaluatorContext);
if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().aAggAccessEnterLeave(false, i, states[i]);}
}
internalHandleUpdated(groupByKey, groupAggregators);
if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().aAggregationGroupedApplyEnterLeave(false);}
}
public void setCurrentAccess(Object groupByKey, int agentInstanceId)
{
currentAggregatorRow = aggregatorsPerGroup.get(groupByKey);
this.currentGroupKey = groupByKey;
if (currentAggregatorRow == null)
{
AggregationMethod[] methods = methodResolutionService.newAggregators(aggregators, agentInstanceId, groupByKey, groupKeyBinding);
AggregationState[] states = methodResolutionService.newAccesses(agentInstanceId, isJoin, accessAggregations, groupByKey, groupKeyBinding);
currentAggregatorRow = new AggregationRowPair(methods, states);
aggregatorsPerGroup.put(groupByKey, currentAggregatorRow);
}
}
public Object getValue(int column, int agentInstanceId)
{
if (column < aggregators.length) {
return currentAggregatorRow.getMethods()[column].getValue();
}
else {
AggregationAccessorSlotPair pair = accessorsFactory[column - aggregators.length];
return pair.getAccessor().getValue(currentAggregatorRow.getStates()[pair.getSlot()]);
}
}
public Collection<EventBean> getCollection(int column, ExprEvaluatorContext context) {
if (column < aggregators.length) {
return null;
}
else {
AggregationAccessorSlotPair pair = accessorsFactory[column - aggregators.length];
return pair.getAccessor().getEnumerableEvents(currentAggregatorRow.getStates()[pair.getSlot()]);
}
}
public EventBean getEventBean(int column, ExprEvaluatorContext context) {
if (column < aggregators.length) {
return null;
}
else {
AggregationAccessorSlotPair pair = accessorsFactory[column - aggregators.length];
return pair.getAccessor().getEnumerableEvent(currentAggregatorRow.getStates()[pair.getSlot()]);
}
}
public void setRemovedCallback(AggregationRowRemovedCallback callback) {
// not applicable
}
public void internalHandleUpdated(Object groupByKey, AggregationRowPair groupAggregators) {
// no action required
}
public void accept(AggregationServiceVisitor visitor) {
visitor.visitAggregations(aggregatorsPerGroup.size(), aggregatorsPerGroup);
}
public void acceptGroupDetail(AggregationServiceVisitorWGroupDetail visitor) {
visitor.visitGrouped(aggregatorsPerGroup.size());
for (Map.Entry<Object, AggregationRowPair> entry : aggregatorsPerGroup.entrySet()) {
visitor.visitGroup(entry.getKey(), entry.getValue());
}
}
public boolean isGrouped() {
return true;
}
public Object getGroupKey(int agentInstanceId) {
return currentGroupKey;
}
public Collection<Object> getGroupKeys(ExprEvaluatorContext exprEvaluatorContext) {
return aggregatorsPerGroup.keySet();
}
}