KickJava   Java API By Example, From Geeks To Geeks.

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


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 import net.nutch.fs.*;
8 import net.nutch.util.*;
9
10 /** A dense file-based mapping from integers to values. */
11 public class ArrayFile extends MapFile {
12
13   protected ArrayFile() {} // no public ctor
14

15   /** Write a new array file. */
16   public static class Writer extends MapFile.Writer {
17     private LongWritable count = new LongWritable(0);
18
19     /** Create the named file for values of the named class. */
20     public Writer(NutchFileSystem nfs, String JavaDoc file, Class JavaDoc valClass) throws IOException {
21       super(nfs, file, LongWritable.class, valClass);
22     }
23
24     /** Append a value to the file. */
25     public synchronized void append(Writable value) throws IOException {
26       super.append(count, value); // add to map
27
count.set(count.get()+1); // increment count
28
}
29   }
30
31   /** Provide access to an existing array file. */
32   public static class Reader extends MapFile.Reader {
33     private LongWritable key = new LongWritable();
34
35     /** Construct an array reader for the named file.*/
36     public Reader(NutchFileSystem nfs, String JavaDoc file) throws IOException {
37       super(nfs, file);
38     }
39
40     /** Positions the reader before its <code>n</code>th value. */
41     public synchronized void seek(long n) throws IOException {
42       key.set(n);
43       seek(key);
44     }
45
46     /** Read and return the next value in the file. */
47     public synchronized Writable next(Writable value) throws IOException {
48       return next(key, value) ? value : null;
49     }
50
51     /** Returns the key associated with the most recent call to {@link
52      * #seek(long)}, {@link #next(Writable)}, or {@link
53      * #get(long,Writable)}. */

54     public synchronized long key() throws IOException {
55       return key.get();
56     }
57
58     /** Return the <code>n</code>th value in the file. */
59     public synchronized Writable get(long n, Writable value)
60       throws IOException {
61       key.set(n);
62       return get(key, value);
63     }
64   }
65
66 }
67
Popular Tags