[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | package org.apache.hadoop.io; |
---|
| 20 | |
---|
| 21 | import java.io.*; |
---|
| 22 | |
---|
| 23 | import org.apache.hadoop.fs.*; |
---|
| 24 | import org.apache.hadoop.conf.*; |
---|
| 25 | |
---|
| 26 | /** A file-based set of keys. */ |
---|
| 27 | public class SetFile extends MapFile { |
---|
| 28 | |
---|
| 29 | protected SetFile() {} // no public ctor |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * Write a new set file. |
---|
| 33 | */ |
---|
| 34 | public static class Writer extends MapFile.Writer { |
---|
| 35 | |
---|
| 36 | /** Create the named set for keys of the named class. |
---|
| 37 | * @deprecated pass a Configuration too |
---|
| 38 | */ |
---|
| 39 | public Writer(FileSystem fs, String dirName, |
---|
| 40 | Class<? extends WritableComparable> keyClass) throws IOException { |
---|
| 41 | super(new Configuration(), fs, dirName, keyClass, NullWritable.class); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | /** Create a set naming the element class and compression type. */ |
---|
| 45 | public Writer(Configuration conf, FileSystem fs, String dirName, |
---|
| 46 | Class<? extends WritableComparable> keyClass, |
---|
| 47 | SequenceFile.CompressionType compress) |
---|
| 48 | throws IOException { |
---|
| 49 | this(conf, fs, dirName, WritableComparator.get(keyClass), compress); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /** Create a set naming the element comparator and compression type. */ |
---|
| 53 | public Writer(Configuration conf, FileSystem fs, String dirName, |
---|
| 54 | WritableComparator comparator, |
---|
| 55 | SequenceFile.CompressionType compress) throws IOException { |
---|
| 56 | super(conf, fs, dirName, comparator, NullWritable.class, compress); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /** Append a key to a set. The key must be strictly greater than the |
---|
| 60 | * previous key added to the set. */ |
---|
| 61 | public void append(WritableComparable key) throws IOException{ |
---|
| 62 | append(key, NullWritable.get()); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /** Provide access to an existing set file. */ |
---|
| 67 | public static class Reader extends MapFile.Reader { |
---|
| 68 | |
---|
| 69 | /** Construct a set reader for the named set.*/ |
---|
| 70 | public Reader(FileSystem fs, String dirName, Configuration conf) throws IOException { |
---|
| 71 | super(fs, dirName, conf); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /** Construct a set reader for the named set using the named comparator.*/ |
---|
| 75 | public Reader(FileSystem fs, String dirName, WritableComparator comparator, Configuration conf) |
---|
| 76 | throws IOException { |
---|
| 77 | super(fs, dirName, comparator, conf); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | // javadoc inherited |
---|
| 81 | public boolean seek(WritableComparable key) |
---|
| 82 | throws IOException { |
---|
| 83 | return super.seek(key); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** Read the next key in a set into <code>key</code>. Returns |
---|
| 87 | * true if such a key exists and false when at the end of the set. */ |
---|
| 88 | public boolean next(WritableComparable key) |
---|
| 89 | throws IOException { |
---|
| 90 | return next(key, NullWritable.get()); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /** Read the matching key from a set into <code>key</code>. |
---|
| 94 | * Returns <code>key</code>, or null if no match exists. */ |
---|
| 95 | public WritableComparable get(WritableComparable key) |
---|
| 96 | throws IOException { |
---|
| 97 | if (seek(key)) { |
---|
| 98 | next(key); |
---|
| 99 | return key; |
---|
| 100 | } else |
---|
| 101 | return null; |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | } |
---|