KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > io > SetFile


1 /* Copyright (c) 2003 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.io;
5
6 import java.io.*;
7
8 import net.nutch.fs.*;
9 import net.nutch.util.*;
10
11 /** A file-based set of keys. */
12 public class SetFile extends MapFile {
13
14   protected SetFile() {} // no public ctor
15

16   /** Write a new set file. */
17   public static class Writer extends MapFile.Writer {
18
19     /** Create the named set for keys of the named class. */
20     public Writer(NutchFileSystem nfs, String JavaDoc dirName, Class JavaDoc keyClass) throws IOException {
21       super(nfs, dirName, keyClass, NullWritable.class);
22     }
23
24     /** Create the named set using the named key comparator. */
25     public Writer(NutchFileSystem nfs, String JavaDoc dirName, WritableComparator comparator)
26       throws IOException {
27       super(nfs, dirName, comparator, NullWritable.class);
28     }
29
30     /** Append a key to a set. The key must be strictly greater than the
31      * previous key added to the set. */

32     public void append(WritableComparable key) throws IOException{
33       append(key, NullWritable.get());
34     }
35   }
36
37   /** Provide access to an existing set file. */
38   public static class Reader extends MapFile.Reader {
39
40     /** Construct a set reader for the named set.*/
41     public Reader(NutchFileSystem nfs, String JavaDoc dirName) throws IOException {
42       super(nfs, dirName);
43     }
44
45     /** Construct a set reader for the named set using the named comparator.*/
46     public Reader(NutchFileSystem nfs, String JavaDoc dirName, WritableComparator comparator)
47       throws IOException {
48       super(nfs, dirName, comparator);
49     }
50
51     // javadoc inherited
52
public boolean seek(WritableComparable key)
53       throws IOException {
54       return super.seek(key);
55     }
56
57     /** Read the next key in a set into <code>key</code>. Returns
58      * true if such a key exists and false when at the end of the set. */

59     public boolean next(WritableComparable key)
60       throws IOException {
61       return next(key, NullWritable.get());
62     }
63
64     /** Read the matching key from a set into <code>key</code>.
65      * Returns <code>key</code>, or null if no match exists. */

66     public WritableComparable get(WritableComparable key)
67       throws IOException {
68       if (seek(key)) {
69         next(key);
70         return key;
71       } else
72         return null;
73     }
74   }
75
76 }
77
Popular Tags