001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.mapreduce.lib.join;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.PriorityQueue;
024
025import org.apache.hadoop.classification.InterfaceAudience;
026import org.apache.hadoop.classification.InterfaceStability;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.io.NullWritable;
029import org.apache.hadoop.io.Writable;
030import org.apache.hadoop.io.WritableComparable;
031import org.apache.hadoop.io.WritableComparator;
032import org.apache.hadoop.util.ReflectionUtils;
033
034/**
035 * Prefer the "rightmost" data source for this key.
036 * For example, <tt>override(S1,S2,S3)</tt> will prefer values
037 * from S3 over S2, and values from S2 over S1 for all keys
038 * emitted from all sources.
039 */
040@InterfaceAudience.Public
041@InterfaceStability.Stable
042public class OverrideRecordReader<K extends WritableComparable<?>,
043                                  V extends Writable>
044    extends MultiFilterRecordReader<K,V> {
045
046  OverrideRecordReader(int id, Configuration conf, int capacity,
047      Class<? extends WritableComparator> cmpcl) throws IOException {
048    super(id, conf, capacity, cmpcl);
049  }
050  private Class<? extends Writable> valueclass = null;
051
052  /**
053   * Emit the value with the highest position in the tuple.
054   */
055  @SuppressWarnings("unchecked") // No static typeinfo on Tuples
056  protected V emit(TupleWritable dst) {
057    return (V) dst.iterator().next();
058  }
059
060  @SuppressWarnings("unchecked") // Explicit check for value class agreement
061  public V createValue() {
062    if (null == valueclass) {
063      Class<?> cls = kids[kids.length -1].createValue().getClass();
064      for (int i = kids.length -1; cls.equals(NullWritable.class); i--) {
065        cls = kids[i].createValue().getClass();
066      }
067      valueclass = cls.asSubclass(Writable.class);
068    }
069    if (valueclass.equals(NullWritable.class)) {
070      return (V) NullWritable.get();
071    }
072    return (V) ReflectionUtils.newInstance(valueclass, null);
073  }
074
075  /**
076   * Instead of filling the JoinCollector with iterators from all
077   * data sources, fill only the rightmost for this key.
078   * This not only saves space by discarding the other sources, but
079   * it also emits the number of key-value pairs in the preferred
080   * RecordReader instead of repeating that stream n times, where
081   * n is the cardinality of the cross product of the discarded
082   * streams for the given key.
083   */
084  protected void fillJoinCollector(K iterkey) 
085      throws IOException, InterruptedException {
086    final PriorityQueue<ComposableRecordReader<K,?>> q = 
087      getRecordReaderQueue();
088    if (q != null && !q.isEmpty()) {
089      int highpos = -1;
090      ArrayList<ComposableRecordReader<K,?>> list =
091        new ArrayList<ComposableRecordReader<K,?>>(kids.length);
092      q.peek().key(iterkey);
093      final WritableComparator cmp = getComparator();
094      while (0 == cmp.compare(q.peek().key(), iterkey)) {
095        ComposableRecordReader<K,?> t = q.poll();
096        if (-1 == highpos || list.get(highpos).id() < t.id()) {
097          highpos = list.size();
098        }
099        list.add(t);
100        if (q.isEmpty())
101          break;
102      }
103      ComposableRecordReader<K,?> t = list.remove(highpos);
104      t.accept(jc, iterkey);
105      for (ComposableRecordReader<K,?> rr : list) {
106        rr.skip(iterkey);
107      }
108      list.add(t);
109      for (ComposableRecordReader<K,?> rr : list) {
110        if (rr.hasNext()) {
111          q.add(rr);
112        }
113      }
114    }
115  }
116
117}