001/* Hadoop RecordReader for Ptolemy tokens.
002 * 
003 * Copyright (c) 2014 The Regents of the University of California.
004 * All rights reserved.
005 *
006 * '$Author: crawl $'
007 * '$Date: 2014-07-02 15:58:19 +0000 (Wed, 02 Jul 2014) $' 
008 * '$Revision: 32804 $'
009 * 
010 * Permission is hereby granted, without written agreement and without
011 * license or royalty fees, to use, copy, modify, and distribute this
012 * software and its documentation for any purpose, provided that the above
013 * copyright notice and the following two paragraphs appear in all copies
014 * of this software.
015 *
016 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
017 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
018 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
019 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
020 * SUCH DAMAGE.
021 *
022 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
023 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
025 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
026 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
027 * ENHANCEMENTS, OR MODIFICATIONS.
028 *
029 */
030package org.kepler.hadoop.io.input;
031
032import java.io.IOException;
033
034import org.apache.hadoop.io.NullWritable;
035import org.apache.hadoop.mapreduce.InputSplit;
036import org.apache.hadoop.mapreduce.RecordReader;
037import org.apache.hadoop.mapreduce.TaskAttemptContext;
038import org.kepler.hadoop.io.TokenWritable;
039
040/** Hadoop RecordReader for Ptolemy tokens.
041 * 
042 *  @author Daniel Crawl
043 *  @verion $Id: TokenRecordReader.java 32804 2014-07-02 15:58:19Z crawl $
044 */
045public class TokenRecordReader extends RecordReader<NullWritable, TokenWritable> {
046
047    /** Initialize the record reader with a split. */
048    @Override
049    public void initialize(InputSplit split, TaskAttemptContext context)
050            throws IOException, InterruptedException {
051
052        _split = (TokenInputSplit)split;
053    }
054
055    /** If another key value can be read, returns true. */
056    @Override
057    public boolean nextKeyValue() throws IOException, InterruptedException {
058        if (done) {
059            return false;
060        } else {
061            done = true;
062            return true;
063        }
064    }
065
066    /** Get the key. In this class returns Null. */
067    @Override
068    public NullWritable getCurrentKey() throws IOException, InterruptedException {
069        return NullWritable.get();
070    }
071
072    /** Get the token writable. */
073    @Override
074    public TokenWritable getCurrentValue() throws IOException, InterruptedException {
075        return _split.getTokenWritable();
076    }
077
078    /** Get the progress. */
079    @Override
080    public float getProgress() throws IOException, InterruptedException {
081        //System.out.println("in getProgress : " + done);
082        if (done) {
083            return 1.0f;
084        } else {
085            return 0.0f;
086        }
087    }
088
089    /** Close the reader. */
090    @Override
091    public void close() throws IOException {
092        done = true;  
093        _split = null;
094    }
095
096    /** If true, single key value has been read. */
097    private boolean done = false;
098    
099    /** The split. */
100    private TokenInputSplit _split;
101}