/**************************************************************************************
* 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.aggregator.AggregationMethod;
import com.espertech.esper.epl.core.MethodResolutionService;
import com.espertech.esper.epl.expression.ExprEvaluator;
import com.espertech.esper.epl.expression.ExprEvaluatorContext;
import java.util.*;
/**
* Implementation for handling aggregation with grouping by group-keys.
*/
public class AggSvcGroupByRefcountedNoAccessImpl extends AggregationServiceBaseGrouped
{
// maintain for each group a row of aggregator states that the expression node canb pull the data from via index
protected Map<Object, AggregationMethodRow> aggregatorsPerGroup;
// maintain a current row for random access into the aggregator state table
// (row=groups, columns=expression nodes that have aggregation functions)
private AggregationMethod[] currentAggregatorRow;
private MethodResolutionService methodResolutionService;
private List<Object> removedKeys;
/**
* 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
*/
public AggSvcGroupByRefcountedNoAccessImpl(ExprEvaluator evaluators[],
AggregationMethodFactory prototypes[],
MethodResolutionService methodResolutionService)
{
super(evaluators, prototypes);
this.methodResolutionService = methodResolutionService;
this.aggregatorsPerGroup = new HashMap<Object, AggregationMethodRow>();
removedKeys = new ArrayList<Object>();
}
public void clearResults(ExprEvaluatorContext exprEvaluatorContext)
{
aggregatorsPerGroup.clear();
}
public void applyEnter(EventBean[] eventsPerStream, Object groupByKey, ExprEvaluatorContext exprEvaluatorContext)
{
handleRemovedKeys();
AggregationMethodRow row = aggregatorsPerGroup.get(groupByKey);
// The aggregators for this group do not exist, need to create them from the prototypes
AggregationMethod[] groupAggregators;
if (row == null)
{
groupAggregators = methodResolutionService.newAggregators(aggregators, exprEvaluatorContext.getAgentInstanceId(), groupByKey);
row = new AggregationMethodRow(methodResolutionService.getCurrentRowCount(groupAggregators, null) + 1, groupAggregators);
aggregatorsPerGroup.put(groupByKey, row);
}
else
{
groupAggregators = row.getMethods();
row.increaseRefcount();
}
currentAggregatorRow = groupAggregators;
// For this row, evaluate sub-expressions, enter result
for (int j = 0; j < evaluators.length; j++)
{
Object columnResult = evaluators[j].evaluate(eventsPerStream, true, exprEvaluatorContext);
groupAggregators[j].enter(columnResult);
}
}
public void applyLeave(EventBean[] eventsPerStream, Object groupByKey, ExprEvaluatorContext exprEvaluatorContext)
{
AggregationMethodRow row = aggregatorsPerGroup.get(groupByKey);
// The aggregators for this group do not exist, need to create them from the prototypes
AggregationMethod[] groupAggregators;
if (row != null)
{
groupAggregators = row.getMethods();
}
else
{
groupAggregators = methodResolutionService.newAggregators(aggregators, exprEvaluatorContext.getAgentInstanceId(), groupByKey);
row = new AggregationMethodRow(methodResolutionService.getCurrentRowCount(groupAggregators, null) + 1, groupAggregators);
aggregatorsPerGroup.put(groupByKey, row);
}
currentAggregatorRow = groupAggregators;
// For this row, evaluate sub-expressions, enter result
for (int j = 0; j < evaluators.length; j++)
{
Object columnResult = evaluators[j].evaluate(eventsPerStream, false, exprEvaluatorContext);
groupAggregators[j].leave(columnResult);
}
row.decreaseRefcount();
if (row.getRefcount() <= 0)
{
removedKeys.add(groupByKey);
methodResolutionService.removeAggregators(exprEvaluatorContext.getAgentInstanceId(), groupByKey); // allow persistence to remove keys already
}
}
public void setCurrentAccess(Object groupByKey, int agentInstanceId)
{
AggregationMethodRow row = aggregatorsPerGroup.get(groupByKey);
if (row != null)
{
currentAggregatorRow = row.getMethods();
}
else
{
currentAggregatorRow = null;
}
if (currentAggregatorRow == null)
{
currentAggregatorRow = methodResolutionService.newAggregators(aggregators, agentInstanceId, groupByKey);
}
}
public Object getValue(int column, int agentInstanceId)
{
return currentAggregatorRow[column].getValue();
}
public Collection<EventBean> getCollection(int column, ExprEvaluatorContext context) {
return null;
}
public EventBean getEventBean(int column, ExprEvaluatorContext context) {
return null;
}
public void setRemovedCallback(AggregationRowRemovedCallback callback) {
// not applicable
}
protected void handleRemovedKeys() {
if (!removedKeys.isEmpty()) // we collect removed keys lazily on the next enter to reduce the chance of empty-group queries creating empty aggregators temporarily
{
for (Object removedKey : removedKeys)
{
aggregatorsPerGroup.remove(removedKey);
}
removedKeys.clear();
}
}
}