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
021
022import java.io.IOException;
023
024import org.apache.hadoop.classification.InterfaceAudience;
025import org.apache.hadoop.classification.InterfaceStability;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.fs.FileSystem;
028import org.apache.hadoop.fs.Path;
029
030/** A file-based set of keys. */
031@InterfaceAudience.Public
032@InterfaceStability.Stable
033public class SetFile extends MapFile {
034
035  protected SetFile() {}                            // no public ctor
036
037  /** 
038   * Write a new set file.
039   */
040  public static class Writer extends MapFile.Writer {
041
042    /** Create the named set for keys of the named class. 
043     *  @deprecated pass a Configuration too
044     */
045    public Writer(FileSystem fs, String dirName,
046        Class<? extends WritableComparable> keyClass) throws IOException {
047      super(new Configuration(), fs, dirName, keyClass, NullWritable.class);
048    }
049
050    /** Create a set naming the element class and compression type. */
051    public Writer(Configuration conf, FileSystem fs, String dirName,
052                  Class<? extends WritableComparable> keyClass,
053                  SequenceFile.CompressionType compress)
054      throws IOException {
055      this(conf, fs, dirName, WritableComparator.get(keyClass), compress);
056    }
057
058    /** Create a set naming the element comparator and compression type. */
059    public Writer(Configuration conf, FileSystem fs, String dirName,
060                  WritableComparator comparator,
061                  SequenceFile.CompressionType compress) throws IOException {
062      super(conf, new Path(dirName), 
063            comparator(comparator), 
064            valueClass(NullWritable.class), 
065            compression(compress));
066    }
067
068    /** Append a key to a set.  The key must be strictly greater than the
069     * previous key added to the set. */
070    public void append(WritableComparable key) throws IOException{
071      append(key, NullWritable.get());
072    }
073  }
074
075  /** Provide access to an existing set file. */
076  public static class Reader extends MapFile.Reader {
077
078    /** Construct a set reader for the named set.*/
079    public Reader(FileSystem fs, String dirName, Configuration conf) throws IOException {
080      super(fs, dirName, conf);
081    }
082
083    /** Construct a set reader for the named set using the named comparator.*/
084    public Reader(FileSystem fs, String dirName, WritableComparator comparator, Configuration conf)
085      throws IOException {
086      super(new Path(dirName), conf, comparator(comparator));
087    }
088
089    // javadoc inherited
090    public boolean seek(WritableComparable key)
091      throws IOException {
092      return super.seek(key);
093    }
094
095    /** Read the next key in a set into <code>key</code>.  Returns
096     * true if such a key exists and false when at the end of the set. */
097    public boolean next(WritableComparable key)
098      throws IOException {
099      return next(key, NullWritable.get());
100    }
101
102    /** Read the matching key from a set into <code>key</code>.
103     * Returns <code>key</code>, or null if no match exists. */
104    public WritableComparable get(WritableComparable key)
105      throws IOException {
106      if (seek(key)) {
107        next(key);
108        return key;
109      } else
110        return null;
111    }
112  }
113
114}