001/*
002 * Copyright (c) 2016-2018 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.local;
023
024import java.io.IOException;
025import java.util.Collection;
026import java.util.Collections;
027import java.util.Map;
028import java.util.Properties;
029
030import cascading.CascadingException;
031import cascading.flow.FlowProcess;
032import cascading.flow.FlowSession;
033import cascading.stats.local.LocalStepStats;
034import cascading.tap.Tap;
035import cascading.tuple.TupleEntryCollector;
036import cascading.tuple.TupleEntryIterator;
037
038/** Class LocalFlowProcess is the local mode implementation of {@link FlowProcess}. */
039public class LocalFlowProcess extends FlowProcess<Properties>
040  {
041  private final Properties config;
042  private LocalStepStats stepStats;
043
044  public LocalFlowProcess()
045    {
046    config = new Properties();
047    }
048
049  public LocalFlowProcess( Properties config )
050    {
051    this.config = config;
052    }
053
054  public LocalFlowProcess( FlowSession flowSession, Properties config )
055    {
056    super( flowSession );
057    this.config = config;
058    }
059
060  public LocalFlowProcess( LocalFlowProcess flowProcess, Properties properties )
061    {
062    super( flowProcess );
063    this.config = properties;
064    this.stepStats = flowProcess.stepStats;
065    }
066
067  public void setStepStats( LocalStepStats stepStats )
068    {
069    this.stepStats = stepStats;
070    }
071
072  @Override
073  public int getNumProcessSlices()
074    {
075    return 1;
076    }
077
078  @Override
079  public int getCurrentSliceNum()
080    {
081    return 0;
082    }
083
084  @Override
085  public Object getProperty( String key )
086    {
087    return config.getProperty( key );
088    }
089
090  @Override
091  public Collection<String> getPropertyKeys()
092    {
093    return Collections.unmodifiableSet( config.stringPropertyNames() );
094    }
095
096  @Override
097  public Object newInstance( String className )
098    {
099    if( className == null || className.isEmpty() )
100      return null;
101
102    try
103      {
104      Class type = LocalFlowProcess.class.getClassLoader().loadClass( className );
105
106      return type.newInstance();
107      }
108    catch( ClassNotFoundException exception )
109      {
110      throw new CascadingException( "unable to load class: " + className, exception );
111      }
112    catch( InstantiationException exception )
113      {
114      throw new CascadingException( "unable to instantiate class: " + className, exception );
115      }
116    catch( IllegalAccessException exception )
117      {
118      throw new CascadingException( "unable to access class: " + className, exception );
119      }
120    }
121
122  @Override
123  public void keepAlive()
124    {
125    }
126
127  @Override
128  public void increment( Enum counter, long amount )
129    {
130    if( stepStats != null )
131      stepStats.increment( counter, amount );
132    }
133
134  @Override
135  public void increment( String group, String counter, long amount )
136    {
137    if( stepStats != null )
138      stepStats.increment( group, counter, amount );
139    }
140
141  @Override
142  public long getCounterValue( Enum counter )
143    {
144    if( stepStats != null )
145      return stepStats.getCounterValue( counter );
146
147    return 0;
148    }
149
150  @Override
151  public long getCounterValue( String group, String counter )
152    {
153    if( stepStats != null )
154      return stepStats.getCounterValue( group, counter );
155
156    return 0;
157    }
158
159  @Override
160  public void setStatus( String status )
161    {
162
163    }
164
165  @Override
166  public boolean isCounterStatusInitialized()
167    {
168    return true;
169    }
170
171  @Override
172  public TupleEntryIterator openTapForRead( Tap tap ) throws IOException
173    {
174    return tap.openForRead( this );
175    }
176
177  @Override
178  public TupleEntryCollector openTapForWrite( Tap tap ) throws IOException
179    {
180    return tap.openForWrite( this, null ); // do not honor sinkmode as this may be opened across tasks
181    }
182
183  @Override
184  public TupleEntryCollector openTrapForWrite( Tap trap ) throws IOException
185    {
186    return trap.openForWrite( this, null ); // do not honor sinkmode as this may be opened across tasks
187    }
188
189  @Override
190  public TupleEntryCollector openSystemIntermediateForWrite() throws IOException
191    {
192    return null;
193    }
194
195  @Override
196  public FlowProcess copyWith( Properties object )
197    {
198    return new LocalFlowProcess( this, object );
199    }
200
201  @Override
202  public Properties getConfig()
203    {
204    return config;
205    }
206
207  @Override
208  public Properties getConfigCopy()
209    {
210    return new Properties( config );
211    }
212
213  @Override
214  public <C> C copyConfig( C config )
215    {
216    return (C) new Properties( (Properties) config );
217    }
218
219  @Override
220  public <C> Map<String, String> diffConfigIntoMap( C defaultConfig, C updatedConfig )
221    {
222    return null;
223    }
224
225  @Override
226  public Properties mergeMapIntoConfig( Properties defaultConfig, Map<String, String> map )
227    {
228    return null;
229    }
230  }