/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.mapred.lib;

import org.apache.hadoop.io.DataOutputBuffer;
import org.apache.hadoop.io.Stringifier;
import org.apache.hadoop.io.DefaultStringifier;
import org.apache.hadoop.io.serializer.Deserializer;
import org.apache.hadoop.io.serializer.Serialization;
import org.apache.hadoop.io.serializer.SerializationFactory;
import org.apache.hadoop.io.serializer.Serializer;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.ReflectionUtils;
import org.apache.hadoop.util.GenericsUtil;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * The Chain class provides all the common functionality for the
 * {@link ChainMapper} and the {@link ChainReducer} classes.
 */
class Chain {
  private static final String CHAIN_MAPPER = "chain.mapper";
  private static final String CHAIN_REDUCER = "chain.reducer";

  private static final String CHAIN_MAPPER_SIZE = ".size";
  private static final String CHAIN_MAPPER_CLASS = ".mapper.class.";
  private static final String CHAIN_MAPPER_CONFIG = ".mapper.config.";
  private static final String CHAIN_REDUCER_CLASS = ".reducer.class";
  private static final String CHAIN_REDUCER_CONFIG = ".reducer.config";

  private static final String MAPPER_BY_VALUE = "chain.mapper.byValue";
  private static final String REDUCER_BY_VALUE = "chain.reducer.byValue";

  private static final String MAPPER_INPUT_KEY_CLASS =
    "chain.mapper.input.key.class";
  private static final String MAPPER_INPUT_VALUE_CLASS =
    "chain.mapper.input.value.class";
  private static final String MAPPER_OUTPUT_KEY_CLASS =
    "chain.mapper.output.key.class";
  private static final String MAPPER_OUTPUT_VALUE_CLASS =
    "chain.mapper.output.value.class";
  private static final String REDUCER_INPUT_KEY_CLASS =
    "chain.reducer.input.key.class";
  private static final String REDUCER_INPUT_VALUE_CLASS =
    "chain.reducer.input.value.class";
  private static final String REDUCER_OUTPUT_KEY_CLASS =
    "chain.reducer.output.key.class";
  private static final String REDUCER_OUTPUT_VALUE_CLASS =
    "chain.reducer.output.value.class";

  private boolean isMap;

  private JobConf chainJobConf;

  private List<Mapper> mappers = new ArrayList<Mapper>();
  private Reducer reducer;

  // to cache the key/value output class serializations for each chain element
  // to avoid everytime lookup.
  private List<Serialization> mappersKeySerialization =
    new ArrayList<Serialization>();
  private List<Serialization> mappersValueSerialization =
    new ArrayList<Serialization>();
  private Serialization reducerKeySerialization;
  private Serialization reducerValueSerialization;

  /**
   * Creates a Chain instance configured for a Mapper or a Reducer.
   *
   * @param isMap TRUE indicates the chain is for a Mapper, FALSE that is for a
   *              Reducer.
   */
  Chain(boolean isMap) {
    this.isMap = isMap;
  }

  /**
   * Returns the prefix to use for the configuration of the chain depending
   * if it is for a Mapper or a Reducer.
   *
   * @param isMap TRUE for Mapper, FALSE for Reducer.
   * @return the prefix to use.
   */
  private static String getPrefix(boolean isMap) {
    return (isMap) ? CHAIN_MAPPER : CHAIN_REDUCER;
  }

  /**
   * Creates a {@link JobConf} for one of the Maps or Reduce in the chain.
   * <p/>
   * It creates a new JobConf using the chain job's JobConf as base and adds to
   * it the configuration properties for the chain element. The keys of the
   * chain element jobConf have precedence over the given JobConf.
   *
   * @param jobConf the chain job's JobConf.
   * @param confKey the key for chain element configuration serialized in the
   *                chain job's JobConf.
   * @return a new JobConf aggregating the chain job's JobConf with the chain
   *         element configuration properties.
   */
  private static JobConf getChainElementConf(JobConf jobConf, String confKey) {
    JobConf conf;
    try {
      Stringifier<JobConf> stringifier =
        new DefaultStringifier<JobConf>(jobConf, JobConf.class);
      conf = stringifier.fromString(jobConf.get(confKey, null));
    } catch (IOException ioex) {
      throw new RuntimeException(ioex);
    }
    // we have to do this because the Writable desearialization clears all
    // values set in the conf making not possible do do a new JobConf(jobConf)
    // in the creation of the conf above
    jobConf = new JobConf(jobConf);

    for(Map.Entry<String, String> entry : conf) {
      jobConf.set(entry.getKey(), entry.getValue());
    }
    return jobConf;
  }

  /**
   * Adds a Mapper class to the chain job's JobConf.
   * <p/>
   * The configuration properties of the chain job have precedence over the
   * configuration properties of the Mapper.
   *
   * @param isMap            indicates if the Chain is for a Mapper or for a
   * Reducer.
   * @param jobConf              chain job's JobConf to add the Mapper class.
   * @param klass            the Mapper class to add.
   * @param inputKeyClass    mapper input key class.
   * @param inputValueClass  mapper input value class.
   * @param outputKeyCl