001/* 002 * Copyright (c) 2016 Chris K Wensel <chris@wensel.net>. All Rights Reserved. 003 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved. 004 * 005 * Project and contact information: http://www.cascading.org/ 006 * 007 * This file is part of the Cascading project. 008 * 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 */ 021 022package cascading.flow.stream.element; 023 024import java.io.IOException; 025 026import cascading.CascadingException; 027import cascading.flow.FlowProcess; 028import cascading.flow.stream.duct.Duct; 029import cascading.operation.Function; 030import cascading.pipe.Each; 031import cascading.pipe.OperatorException; 032import cascading.tuple.Fields; 033import cascading.tuple.Tuple; 034import cascading.tuple.TupleEntry; 035import cascading.tuple.TupleEntryCollector; 036import cascading.tuple.Tuples; 037 038/** 039 * 040 */ 041public class FunctionEachStage extends EachStage 042 { 043 private Function function; 044 045 public FunctionEachStage( FlowProcess flowProcess, Each each ) 046 { 047 super( flowProcess, each ); 048 } 049 050 @Override 051 protected Fields getIncomingPassThroughFields() 052 { 053 return incomingScopes.get( 0 ).getIncomingFunctionPassThroughFields(); 054 } 055 056 @Override 057 protected Fields getIncomingArgumentsFields() 058 { 059 return incomingScopes.get( 0 ).getIncomingFunctionArgumentFields(); 060 } 061 062 @Override 063 public void initialize() 064 { 065 super.initialize(); 066 067 function = each.getFunction(); 068 069 operationCall.setArguments( argumentsEntry ); 070 071 operationCall.setOutputCollector( new TupleEntryCollector( getOperationDeclaredFields() ) 072 { 073 @Override 074 protected void collect( TupleEntry input ) throws IOException 075 { 076 Tuple outgoing = outgoingBuilder.makeResult( incomingEntry.getTuple(), input.getTuple() ); 077 078 outgoingEntry.setTuple( outgoing ); 079 080 try 081 { 082 next.receive( FunctionEachStage.this, 0, outgoingEntry ); 083 } 084 finally 085 { 086 Tuples.asModifiable( outgoing ); 087 } 088 } 089 } ); 090 } 091 092 @Override 093 public void receive( Duct previous, int ordinal, TupleEntry incomingEntry ) 094 { 095 this.incomingEntry = incomingEntry; 096 097 argumentsEntry.setTuple( argumentsBuilder.makeResult( incomingEntry.getTuple(), null ) ); 098 099 try 100 { 101 function.operate( flowProcess, operationCall ); // adds results to collector 102 } 103 catch( CascadingException exception ) 104 { 105 handleException( exception, argumentsEntry ); 106 } 107 catch( Throwable throwable ) 108 { 109 handleException( new OperatorException( each, "operator Each failed executing operation", throwable ), argumentsEntry ); 110 } 111 } 112 }