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    
019    package org.apache.hadoop.mapred.join;
020    
021    import java.io.IOException;
022    import java.util.PriorityQueue;
023    
024    import org.apache.hadoop.classification.InterfaceAudience;
025    import org.apache.hadoop.classification.InterfaceStability;
026    import org.apache.hadoop.io.Writable;
027    import org.apache.hadoop.io.WritableComparable;
028    import org.apache.hadoop.io.WritableComparator;
029    import org.apache.hadoop.io.WritableUtils;
030    import org.apache.hadoop.mapred.JobConf;
031    
032    /**
033     * Base class for Composite joins returning Tuples of arbitrary Writables.
034     * @deprecated Use 
035     * {@link org.apache.hadoop.mapreduce.lib.join.JoinRecordReader} instead
036     */
037    @Deprecated
038    @InterfaceAudience.Public
039    @InterfaceStability.Stable
040    public abstract class JoinRecordReader<K extends WritableComparable>
041        extends CompositeRecordReader<K,Writable,TupleWritable>
042        implements ComposableRecordReader<K,TupleWritable> {
043    
044      public JoinRecordReader(int id, JobConf conf, int capacity,
045          Class<? extends WritableComparator> cmpcl) throws IOException {
046        super(id, capacity, cmpcl);
047        setConf(conf);
048      }
049    
050      /**
051       * Emit the next set of key, value pairs as defined by the child
052       * RecordReaders and operation associated with this composite RR.
053       */
054      public boolean next(K key, TupleWritable value) throws IOException {
055        if (jc.flush(value)) {
056          WritableUtils.cloneInto(key, jc.key());
057          return true;
058        }
059        jc.clear();
060        K iterkey = createKey();
061        final PriorityQueue<ComposableRecordReader<K,?>> q = getRecordReaderQueue();
062        while (!q.isEmpty()) {
063          fillJoinCollector(iterkey);
064          jc.reset(iterkey);
065          if (jc.flush(value)) {
066            WritableUtils.cloneInto(key, jc.key());
067            return true;
068          }
069          jc.clear();
070        }
071        return false;
072      }
073    
074      /** {@inheritDoc} */
075      public TupleWritable createValue() {
076        return createInternalValue();
077      }
078    
079      /**
080       * Return an iterator wrapping the JoinCollector.
081       */
082      protected ResetableIterator<TupleWritable> getDelegate() {
083        return new JoinDelegationIterator();
084      }
085    
086      /**
087       * Since the JoinCollector is effecting our operation, we need only
088       * provide an iterator proxy wrapping its operation.
089       */
090      protected class JoinDelegationIterator
091          implements ResetableIterator<TupleWritable> {
092    
093        public boolean hasNext() {
094          return jc.hasNext();
095        }
096    
097        public boolean next(TupleWritable val) throws IOException {
098          return jc.flush(val);
099        }
100    
101        public boolean replay(TupleWritable val) throws IOException {
102          return jc.replay(val);
103        }
104    
105        public void reset() {
106          jc.reset(jc.key());
107        }
108    
109        public void add(TupleWritable item) throws IOException {
110          throw new UnsupportedOperationException();
111        }
112    
113        public void close() throws IOException {
114          jc.close();
115        }
116    
117        public void clear() {
118          jc.clear();
119        }
120      }
121    }