/**************************************************************************************
* 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.view;
import com.espertech.esper.client.EventBean;
import com.espertech.esper.collection.MultiKey;
import com.espertech.esper.collection.UniformPair;
import com.espertech.esper.core.service.UpdateDispatchView;
import com.espertech.esper.epl.core.ResultSetProcessor;
import com.espertech.esper.epl.expression.ExprEvaluatorContext;
import com.espertech.esper.event.EventBeanUtility;
import com.espertech.esper.util.AuditPath;
import com.espertech.esper.util.ExecutionPathDebugLog;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Iterator;
import java.util.Set;
/**
* Output process view that does not enforce any output policies and may simply
* hand over events to child views, but works with distinct and after-output policies
*/
public class OutputProcessViewDirectDistinctOrAfter extends OutputProcessViewBaseWAfter
{
private static final Log log = LogFactory.getLog(OutputProcessViewDirectDistinctOrAfter.class);
private final OutputProcessViewDirectDistinctOrAfterFactory parent;
public OutputProcessViewDirectDistinctOrAfter(ResultSetProcessor resultSetProcessor, Long afterConditionTime, Integer afterConditionNumberOfEvents, boolean afterConditionSatisfied, OutputProcessViewDirectDistinctOrAfterFactory parent) {
super(resultSetProcessor, afterConditionTime, afterConditionNumberOfEvents, afterConditionSatisfied);
this.parent = parent;
}
/**
* The update method is called if the view does not participate in a join.
* @param newData - new events
* @param oldData - old events
*/
public void update(EventBean[] newData, EventBean[] oldData)
{
if ((ExecutionPathDebugLog.isDebugEnabled) && (log.isDebugEnabled()))
{
log.debug(".update Received update, " +
" newData.length==" + ((newData == null) ? 0 : newData.length) +
" oldData.length==" + ((oldData == null) ? 0 : oldData.length));
}
boolean isGenerateSynthetic = parent.getStatementResultService().isMakeSynthetic();
boolean isGenerateNatural = parent.getStatementResultService().isMakeNatural();
UniformPair<EventBean[]> newOldEvents = resultSetProcessor.processViewResult(newData, oldData, isGenerateSynthetic);
if (!super.checkAfterCondition(newOldEvents, parent.getStatementContext()))
{
return;
}
if (parent.isDistinct() && newOldEvents != null)
{
newOldEvents.setFirst(EventBeanUtility.getDistinctByProp(newOldEvents.getFirst(), parent.getEventBeanReader()));
newOldEvents.setSecond(EventBeanUtility.getDistinctByProp(newOldEvents.getSecond(), parent.getEventBeanReader()));
}
if ((!isGenerateSynthetic) && (!isGenerateNatural))
{
if (AuditPath.isAuditEnabled) {
OutputStrategyUtil.indicateEarlyReturn(parent.getStatementContext(), newOldEvents);
}
return;
}
boolean forceOutput = false;
if ((newData == null) && (oldData == null) &&
((newOldEvents == null) || (newOldEvents.getFirst() == null && newOldEvents.getSecond() == null)))
{
forceOutput = true;
}
// Child view can be null in replay from named window
if (childView != null)
{
postProcess(forceOutput, newOldEvents, childView);
}
}
/**
* This process (update) method is for participation in a join.
* @param newEvents - new events
* @param oldEvents - old events
*/
public void process(Set<MultiKey<EventBean>> newEvents, Set<MultiKey<EventBean>> oldEvents, ExprEvaluatorContext exprEvaluatorContext)
{
if ((ExecutionPathDebugLog.isDebugEnabled) && (log.isDebugEnabled()))
{
log.debug(".process Received update, " +
" newData.length==" + ((newEvents == null) ? 0 : newEvents.size()) +
" oldData.length==" + ((oldEvents == null) ? 0 : oldEvents.size()));
}
boolean isGenerateSynthetic = parent.getStatementResultService().isMakeSynthetic();
boolean isGenerateNatural = parent.getStatementResultService().isMakeNatural();
UniformPair<EventBean[]> newOldEvents = resultSetProcessor.processJoinResult(newEvents, oldEvents, isGenerateSynthetic);
if (!checkAfterCondition(newOldEvents, parent.getStatementContext()))
{
return;
}
if (parent.isDistinct() && newOldEvents != null)
{
newOldEvents.setFirst(EventBeanUtility.getDistinctByProp(newOldEvents.getFirst(), parent.getEventBeanReader()));
newOldEvents.setSecond(EventBeanUtility.getDistinctByProp(newOldEvents.getSecond(), parent.getEventBeanReader()));
}
if ((!isGenerateSynthetic) && (!isGenerateNatural))
{
if (AuditPath.isAuditEnabled) {
OutputStrategyUtil.indicateEarlyReturn(parent.getStatementContext(), newOldEvents);
}
return;
}
if (newOldEvents == null)
{
return;
}
// Child view can be null in replay from named window
if (childView != null)
{
postProcess(false, newOldEvents, childView);
}
}
protected void postProcess(boolean force, UniformPair<EventBean[]> newOldEvents, UpdateDispatchView childView) {
OutputStrategyUtil.output(force, newOldEvents, childView);
}
public Iterator<EventBean> iterator() {
return OutputStrategyUtil.getIterator(joinExecutionStrategy, resultSetProcessor, parentView, parent.isDistinct());
}
public void terminated() {
// Not applicable
}
}