    001/*
    002 * Copyright (c) 2007-2016 Concurrent, Inc. All Rights Reserved.
    003 *
    004 * Project and contact information: http://www.cascading.org/
    005 *
    006 * This file is part of the Cascading project.
    007 *
    008 * Licensed under the Apache License, Version 2.0 (the "License");
    009 * you may not use this file except in compliance with the License.
    010 * You may obtain a copy of the License at
    011 *
    012 *     http://www.apache.org/licenses/LICENSE-2.0
    013 *
    014 * Unless required by applicable law or agreed to in writing, software
    015 * distributed under the License is distributed on an "AS IS" BASIS,
    016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    017 * See the License for the specific language governing permissions and
    018 * limitations under the License.
    019 */
    020
    021package cascading.flow.stream.element;
    022
    023import cascading.CascadingException;
    024import cascading.flow.FlowProcess;
    025import cascading.flow.stream.duct.Duct;
    026import cascading.flow.stream.duct.Reducing;
    027import cascading.operation.GroupAssertion;
    028import cascading.pipe.Every;
    029import cascading.pipe.OperatorException;
    030import cascading.tuple.Fields;
    031import cascading.tuple.TupleEntry;
    032
    033/**
    034 *
    035 */
    036public class GroupAssertionEveryStage extends EveryStage<TupleEntry> implements Reducing<TupleEntry, TupleEntry>
    037  {
    038  private GroupAssertion groupAssertion;
    039  private Reducing reducing;
    040
    041  public GroupAssertionEveryStage( FlowProcess flowProcess, Every every )
    042    {
    043    super( flowProcess, every );
    044    }
    045
    046  @Override
    047  protected Fields getIncomingPassThroughFields()
    048    {
    049    return incomingScopes.get( 0 ).getIncomingAggregatorPassThroughFields();
    050    }
    051
    052  @Override
    053  protected Fields getIncomingArgumentsFields()
    054    {
    055    return incomingScopes.get( 0 ).getIncomingAggregatorArgumentFields();
    056    }
    057
    058  @Override
    059  protected Fields getOutgoingSelector()
    060    {
    061    return outgoingScopes.get( 0 ).getOutGroupingSelector();
    062    }
    063
    064  @Override
    065  public void initialize()
    066    {
    067    super.initialize();
    068
    069    groupAssertion = every.getGroupAssertion();
    070
    071    reducing = (Reducing) getNext();
    072    }
    073
    074  @Override
    075  public void startGroup( Duct previous, TupleEntry groupEntry )
    076    {
    077    operationCall.setGroup( groupEntry );
    078    operationCall.setArguments( null );  // zero it out
    079    operationCall.setOutputCollector( null ); // zero it out
    080
    081    try
    082      {
    083      groupAssertion.start( flowProcess, operationCall );
    084      }
    085    catch( CascadingException exception )
    086      {
    087      handleException( exception, groupEntry );
    088      }
    089    catch( Throwable throwable )
    090      {
    091      handleException( new OperatorException( every, "operator Every failed starting operation: " + every.getOperation(), throwable ), groupEntry );
    092      }
    093
    094    reducing.startGroup( this, groupEntry );
    095    }
    096
    097  @Override
    098  public void receive( Duct previous, TupleEntry tupleEntry )
    099    {
    100    try
    101      {
    102      argumentsEntry.setTuple( argumentsBuilder.makeResult( tupleEntry.getTuple(), null ) );
    103      operationCall.setArguments( argumentsEntry );
    104
    105      groupAssertion.aggregate( flowProcess, operationCall );
    106      }
    107    catch( CascadingException exception )
    108      {
    109      handleException( exception, argumentsEntry );
    110      }
    111    catch( Throwable throwable )
    112      {
    113      handleException( new OperatorException( every, "operator Every failed executing operation: " + every.getOperation(), throwable ), argumentsEntry );
    114      }
    115
    116    next.receive( this, tupleEntry );
    117    }
    118
    119  @Override
    120  public void completeGroup( Duct previous, TupleEntry incomingEntry )
    121    {
    122    this.incomingEntry = incomingEntry;
    123    operationCall.setArguments( null );
    124
    125    try
    126      {
    127      groupAssertion.doAssert( flowProcess, operationCall ); // collector calls next
    128
    129      reducing.completeGroup( this, incomingEntry );
    130      }
    131    catch( CascadingException exception )
    132      {
    133      handleException( exception, incomingEntry );
    134      }
    135    catch( Throwable throwable )
    136      {
    137      handleException( new OperatorException( every, "operator Every failed completing operation: " + every.getOperation(), throwable ), incomingEntry );
    138      }
    139    }
    140  }