SetFile.java 3.6 KB

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