Package com.espertech.esper.epl.agg.service

Source Code of com.espertech.esper.epl.agg.service.AggSvcGroupByRefcountedWAccessRollupImpl

/**************************************************************************************
* 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.*;

/**
* Implementation for handling aggregation with grouping by group-keys.
*/
public class AggSvcGroupByRefcountedWAccessRollupImpl extends AggregationServiceBaseGrouped
{
    protected final AggregationAccessorSlotPair[] accessors;
    protected final AggregationStateFactory[] accessAggregations;
    protected final boolean isJoin;
    protected final AggregationGroupByRollupDesc rollupLevelDesc;

    // maintain for each group a row of aggregator states that the expression node can pull the data from via index
    protected Map<Object, AggregationMethodPairRow>[] aggregatorsPerGroup;
    protected AggregationMethodPairRow aggregatorTopGroup;

    // maintain a current row for random access into the aggregator state table
    // (row=groups, columns=expression nodes that have aggregation functions)
    private AggregationMethod[] currentAggregatorMethods;
    private AggregationState[] currentAggregatorStates;
    private Object currentGroupKey;

    private MethodResolutionService methodResolutionService;

    protected final Object[] methodParameterValues;
    protected boolean hasRemovedKey;
    protected final 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
     * @param accessors accessor definitions
     * @param accessAggregations access aggs
     * @param isJoin true for join, false for single-stream
     */
    public AggSvcGroupByRefcountedWAccessRollupImpl(ExprEvaluator evaluators[],
                                                    AggregationMethodFactory prototypes[],
                                                    Object groupKeyBinding,
                                                    MethodResolutionService methodResolutionService,
                                                    AggregationAccessorSlotPair[] accessors,
                                                    AggregationStateFactory[] accessAggregations,
                                                    boolean isJoin,
                                                    AggregationGroupByRollupDesc rollupLevelDesc,
                                                    AggregationMethod[] topGroupAggregators,
                                                    AggregationState[] topGroupStates)
    {
        super(evaluators, prototypes, groupKeyBinding);
        this.methodResolutionService = methodResolutionService;

        this.aggregatorsPerGroup = (Map<Object, AggregationMethodPairRow>[]) new Map[rollupLevelDesc.getNumLevelsAggregation()];
        this.removedKeys = (List<Object>[]) new ArrayList[rollupLevelDesc.getNumLevelsAggregation()];
        for (int i = 0; i < rollupLevelDesc.getNumLevelsAggregation(); i++) {
            this.aggregatorsPerGroup[i] = new HashMap<Object, AggregationMethodPairRow>();
            this.removedKeys[i] = new ArrayList<Object>(2);
        }
        this.accessors = accessors;
        this.accessAggregations = accessAggregations;
        this.isJoin = isJoin;
        this.rollupLevelDesc = rollupLevelDesc;
        this.aggregatorTopGroup = new AggregationMethodPairRow(0, topGroupAggregators, topGroupStates);
        this.methodParameterValues = new Object[evaluators.length];
    }

    public void clearResults(ExprEvaluatorContext exprEvaluatorContext)
    {
        for (AggregationState state : aggregatorTopGroup.getStates()) {
            state.clear();
        }
        for (AggregationMethod aggregator : aggregatorTopGroup.getMethods()) {
            aggregator.clear();
        }
        for (int i = 0; i < rollupLevelDesc.getNumLevelsAggregation(); i++) {
            aggregatorsPerGroup[i].clear();
        }
    }

    public void applyEnter(EventBean[] eventsPerStream, Object compositeGroupKey, ExprEvaluatorContext exprEvaluatorContext)
    {
        handleRemovedKeys();

        for (int i = 0; i < evaluators.length; i++) {
            if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().qAggregationGroupedRollupEvalParam(true, methodParameterValues.length);}
            methodParameterValues[i] = evaluators[i].evaluate(eventsPerStream, true, exprEvaluatorContext);
            if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().aAggregationGroupedRollupEvalParam(methodParameterValues[i]);}
        }

        Object[] groupKeyPerLevel = (Object[]) compositeGroupKey;
        for (int i = 0; i < groupKeyPerLevel.length; i++) {
            AggregationGroupByRollupLevel level = rollupLevelDesc.getLevels()[i];
            Object groupKey = groupKeyPerLevel[i];
           
            AggregationMethodPairRow row;
            if (!level.isAggregationTop()) {
                row = aggregatorsPerGroup[level.getAggregationOffset()].get(groupKey);
            }
            else {
                row = aggregatorTopGroup;
            }

            if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().qAggregationGroupedApplyEnterLeave(true, aggregators.length, accessAggregations.length, groupKey);}

            // The aggregators for this group do not exist, need to create them from the prototypes
            AggregationMethod[] groupAggregators;
            AggregationState[] groupStates;
            if (row == null)
            {
                groupAggregators = methodResolutionService.newAggregators(aggregators, exprEvaluatorContext.getAgentInstanceId(), groupKey, groupKeyBinding, level);
                groupStates = methodResolutionService.newAccesses(exprEvaluatorContext.getAgentInstanceId(), isJoin, accessAggregations, groupKey, groupKeyBinding, level);
                row = new AggregationMethodPairRow(methodResolutionService.getCurrentRowCount(groupAggregators, groupStates) + 1, groupAggregators, groupStates);
                if (!level.isAggregationTop()) {
                    aggregatorsPerGroup[level.getAggregationOffset()].put(groupKey, row);
                }
            }
            else
            {
                groupAggregators = row.getMethods();
                groupStates = row.getStates();
                row.increaseRefcount();
            }

            // For this row, evaluate sub-expressions, enter result
            currentAggregatorMethods = groupAggregators;
            currentAggregatorStates = groupStates;
            for (int j = 0; j < evaluators.length; j++)
            {
                if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().qAggNoAccessEnterLeave(true, j, groupAggregators[j], aggregators[j]);}
                groupAggregators[j].enter(methodParameterValues[j]);
                if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().aAggNoAccessEnterLeave(true, j, groupAggregators[j]);}
            }

            for (int j = 0; j < currentAggregatorStates.length; j++) {
                if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().qAggAccessEnterLeave(true, j, currentAggregatorStates[j], accessAggregations[j]);}
                currentAggregatorStates[j].applyEnter(eventsPerStream, exprEvaluatorContext);
                if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().aAggAccessEnterLeave(true, j, currentAggregatorStates[j]);}
            }

            internalHandleGroupUpdate(groupKey, row, level);
            if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().aAggregationGroupedApplyEnterLeave(true);}
        }
    }

    public void applyLeave(EventBean[] eventsPerStream, Object compositeGroupKey, ExprEvaluatorContext exprEvaluatorContext)
    {
        for (int i = 0; i < evaluators.length; i++) {
            if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().qAggregationGroupedRollupEvalParam(false, methodParameterValues.length);}
            methodParameterValues[i] = evaluators[i].evaluate(eventsPerStream, false, exprEvaluatorContext);
            if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().aAggregationGroupedRollupEvalParam(methodParameterValues[i]);}
        }

        Object[] groupKeyPerLevel = (Object[]) compositeGroupKey;
        for (int i = 0; i < groupKeyPerLevel.length; i++) {
            AggregationGroupByRollupLevel level = rollupLevelDesc.getLevels()[i];
            Object groupKey = groupKeyPerLevel[i];

            AggregationMethodPairRow row;
            if (!level.isAggregationTop()) {
                row = aggregatorsPerGroup[level.getAggregationOffset()].get(groupKey);
            }
            else {
                row = aggregatorTopGroup;
            }

            if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().qAggregationGroupedApplyEnterLeave(false, aggregators.length, accessAggregations.length, groupKey);}

            // The aggregators for this group do not exist, need to create them from the prototypes
            AggregationMethod[] groupAggregators;
            AggregationState[] groupStates;
            if (row != null)
            {
                groupAggregators = row.getMethods();
                groupStates = row.getStates();
            }
            else
            {
                groupAggregators = methodResolutionService.newAggregators(aggregators, exprEvaluatorContext.getAgentInstanceId(), groupKey, groupKeyBinding, level);
                groupStates = methodResolutionService.newAccesses(exprEvaluatorContext.getAgentInstanceId(), isJoin, accessAggregations, groupKey, groupKeyBinding, level);
                row = new AggregationMethodPairRow(methodResolutionService.getCurrentRowCount(groupAggregators, groupStates) + 1, groupAggregators, groupStates);
                if (!level.isAggregationTop()) {
                    aggregatorsPerGroup[level.getAggregationOffset()].put(groupKey, row);
                }
            }

            // For this row, evaluate sub-expressions, enter result
            currentAggregatorMethods = groupAggregators;
            currentAggregatorStates = groupStates;
            for (int j = 0; j < evaluators.length; j++)
            {
                if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().qAggNoAccessEnterLeave(false, j, groupAggregators[j], aggregators[j]);}
                groupAggregators[j].leave(methodParameterValues[j]);
                if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().aAggNoAccessEnterLeave(false, j, groupAggregators[j]);}
            }

            for (int j = 0; j < currentAggregatorStates.length; j++) {
                if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().qAggAccessEnterLeave(false, j, currentAggregatorStates[j], accessAggregations[j]);}
                currentAggregatorStates[j].applyLeave(eventsPerStream, exprEvaluatorContext);
                if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().aAggAccessEnterLeave(false, j, currentAggregatorStates[j]);}
            }

            row.decreaseRefcount();
            if (row.getRefcount() <= 0)
            {
                hasRemovedKey = true;
                if (!level.isAggregationTop()) {
                    removedKeys[level.getAggregationOffset()].add(groupKey);
                }
                methodResolutionService.removeAggregators(exprEvaluatorContext.getAgentInstanceId(), groupKey, groupKeyBinding, level)// allow persistence to remove keys already
            }

            internalHandleGroupUpdate(groupKey, row, level);
            if (InstrumentationHelper.ENABLED) { InstrumentationHelper.get().aAggregationGroupedApplyEnterLeave(false);}
        }
    }

    public void setCurrentAccess(Object groupByKey, int agentInstanceId, AggregationGroupByRollupLevel rollupLevel)
    {
        AggregationMethodPairRow row;
        if (rollupLevel.isAggregationTop()) {
            row = aggregatorTopGroup;
        }
        else {
            row = aggregatorsPerGroup[rollupLevel.getAggregationOffset()].get(groupByKey);
        }

        if (row != null) {
            currentAggregatorMethods = row.getMethods();
            currentAggregatorStates = row.getStates();
        }
        else {
            currentAggregatorMethods = null;
        }

        if (currentAggregatorMethods == null) {
            currentAggregatorMethods = methodResolutionService.newAggregators(aggregators, agentInstanceId, groupByKey, groupKeyBinding, rollupLevel);
            currentAggregatorStates = methodResolutionService.newAccesses(agentInstanceId, isJoin, accessAggregations, groupByKey, groupKeyBinding, rollupLevel);
        }

        this.currentGroupKey = groupByKey;
    }

    public Object getValue(int column, int agentInstanceId)
    {
        if (column < aggregators.length) {
            return currentAggregatorMethods[column].getValue();
        }
        else {
            AggregationAccessorSlotPair pair = accessors[column - aggregators.length];
            return pair.getAccessor().getValue(currentAggregatorStates[pair.getSlot()]);
        }
    }

    public Collection<EventBean> getCollection(int column, ExprEvaluatorContext context) {
        if (column < aggregators.length) {
            return null;
        }
        else {
            AggregationAccessorSlotPair pair = accessors[column - aggregators.length];
            return pair.getAccessor().getEnumerableEvents(currentAggregatorStates[pair.getSlot()]);
        }
    }

    public EventBean getEventBean(int column, ExprEvaluatorContext context) {
        if (column < aggregators.length) {
            return null;
        }
        else {
            AggregationAccessorSlotPair pair = accessors[column - aggregators.length];
            return pair.getAccessor().getEnumerableEvent(currentAggregatorStates[pair.getSlot()]);
        }
    }

    public void setRemovedCallback(AggregationRowRemovedCallback callback) {
        // not applicable
    }

    public void internalHandleGroupUpdate(Object groupByKey, AggregationMethodPairRow row, AggregationGroupByRollupLevel groupByRollupLevel) {
        // no action required
    }

    public void internalHandleGroupRemove(Object groupByKey, AggregationGroupByRollupLevel groupByRollupLevel) {
        // no action required
    }

    public void accept(AggregationServiceVisitor visitor) {
        visitor.visitAggregations(getGroupKeyCount(), aggregatorsPerGroup);
    }

    public void acceptGroupDetail(AggregationServiceVisitorWGroupDetail visitor) {
        visitor.visitGrouped(getGroupKeyCount());
        for (Map<Object, AggregationMethodPairRow> anAggregatorsPerGroup : aggregatorsPerGroup) {
            for (Map.Entry<Object, AggregationMethodPairRow> entry : anAggregatorsPerGroup.entrySet()) {
                visitor.visitGroup(entry.getKey(), entry.getValue());
            }
        }
        visitor.visitGroup(new Object[0], aggregatorTopGroup);
    }

    public boolean isGrouped() {
        return true;
    }

    protected void handleRemovedKeys() {
        if (!hasRemovedKey) {
            return;
        }
        hasRemovedKey = false;
        for (int i = 0; i < removedKeys.length; i++) {
            if (removedKeys[i].isEmpty()) {
                continue;
            }
            for (Object removedKey : removedKeys[i])
            {
                aggregatorsPerGroup[i].remove(removedKey);
                internalHandleGroupRemove(removedKey, rollupLevelDesc.getLevels()[i]);
            }
            removedKeys[i].clear();
        }
    }

    public Object getGroupKey(int agentInstanceId) {
        return currentGroupKey;
    }

    public Collection<Object> getGroupKeys(ExprEvaluatorContext exprEvaluatorContext) {
        throw new UnsupportedOperationException();
    }

    private int getGroupKeyCount() {
        int count = 1;
        for (Map<Object, AggregationMethodPairRow> anAggregatorsPerGroup : aggregatorsPerGroup) {
            count += anAggregatorsPerGroup.size();
        }
        return count;
    }
}
TOP

Related Classes of com.espertech.esper.epl.agg.service.AggSvcGroupByRefcountedWAccessRollupImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.