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.io;
020
021import java.io.*;
022import org.apache.hadoop.fs.*;
023import org.apache.hadoop.classification.InterfaceAudience;
024import org.apache.hadoop.classification.InterfaceStability;
025import org.apache.hadoop.conf.*;
026import org.apache.hadoop.util.*;
027import org.apache.hadoop.io.SequenceFile.CompressionType;
028
029
030/** A dense file-based mapping from integers to values. */
031@InterfaceAudience.Public
032@InterfaceStability.Stable
033public class ArrayFile extends MapFile {
034
035  protected ArrayFile() {}                            // no public ctor
036
037  /** Write a new array file. */
038  public static class Writer extends MapFile.Writer {
039    private LongWritable count = new LongWritable(0);
040
041    /** Create the named file for values of the named class. */
042    public Writer(Configuration conf, FileSystem fs,
043                  String file, Class<? extends Writable> valClass)
044      throws IOException {
045      super(conf, new Path(file), keyClass(LongWritable.class), 
046            valueClass(valClass));
047    }
048
049    /** Create the named file for values of the named class. */
050    public Writer(Configuration conf, FileSystem fs,
051                  String file, Class<? extends Writable> valClass,
052                  CompressionType compress, Progressable progress)
053      throws IOException {
054      super(conf, new Path(file), 
055            keyClass(LongWritable.class), 
056            valueClass(valClass), 
057            compression(compress), 
058            progressable(progress));
059    }
060
061    /** Append a value to the file. */
062    public synchronized void append(Writable value) throws IOException {
063      super.append(count, value);                 // add to map
064      count.set(count.get()+1);                   // increment count
065    }
066  }
067
068  /** Provide access to an existing array file. */
069  public static class Reader extends MapFile.Reader {
070    private LongWritable key = new LongWritable();
071
072    /** Construct an array reader for the named file.*/
073    public Reader(FileSystem fs, String file, 
074                  Configuration conf) throws IOException {
075      super(new Path(file), conf);
076    }
077
078    /** Positions the reader before its <code>n</code>th value. */
079    public synchronized void seek(long n) throws IOException {
080      key.set(n);
081      seek(key);
082    }
083
084    /** Read and return the next value in the file. */
085    public synchronized Writable next(Writable value) throws IOException {
086      return next(key, value) ? value : null;
087    }
088
089    /** Returns the key associated with the most recent call to {@link
090     * #seek(long)}, {@link #next(Writable)}, or {@link
091     * #get(long,Writable)}. */
092    public synchronized long key() throws IOException {
093      return key.get();
094    }
095
096    /** Return the <code>n</code>th value in the file. */
097    public synchronized Writable get(long n, Writable value)
098      throws IOException {
099      key.set(n);
100      return get(key, value);
101    }
102  }
103
104}