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 | import org.apache.hadoop.fs.*; |
---|
23 | import org.apache.hadoop.conf.*; |
---|
24 | import org.apache.hadoop.util.*; |
---|
25 | import org.apache.hadoop.io.SequenceFile.CompressionType; |
---|
26 | |
---|
27 | |
---|
28 | /** A dense file-based mapping from integers to values. */ |
---|
29 | public class ArrayFile extends MapFile { |
---|
30 | |
---|
31 | protected ArrayFile() {} // no public ctor |
---|
32 | |
---|
33 | /** Write a new array file. */ |
---|
34 | public static class Writer extends MapFile.Writer { |
---|
35 | private LongWritable count = new LongWritable(0); |
---|
36 | |
---|
37 | /** Create the named file for values of the named class. */ |
---|
38 | public Writer(Configuration conf, FileSystem fs, |
---|
39 | String file, Class<? extends Writable> valClass) |
---|
40 | throws IOException { |
---|
41 | super(conf, fs, file, LongWritable.class, valClass); |
---|
42 | } |
---|
43 | |
---|
44 | /** Create the named file for values of the named class. */ |
---|
45 | public Writer(Configuration conf, FileSystem fs, |
---|
46 | String file, Class<? extends Writable> valClass, |
---|
47 | CompressionType compress, Progressable progress) |
---|
48 | throws IOException { |
---|
49 | super(conf, fs, file, LongWritable.class, valClass, compress, progress); |
---|
50 | } |
---|
51 | |
---|
52 | /** Append a value to the file. */ |
---|
53 | public synchronized void append(Writable value) throws IOException { |
---|
54 | super.append(count, value); // add to map |
---|
55 | count.set(count.get()+1); // increment count |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | /** Provide access to an existing array file. */ |
---|
60 | public static class Reader extends MapFile.Reader { |
---|
61 | private LongWritable key = new LongWritable(); |
---|
62 | |
---|
63 | /** Construct an array reader for the named file.*/ |
---|
64 | public Reader(FileSystem fs, String file, Configuration conf) throws IOException { |
---|
65 | super(fs, file, conf); |
---|
66 | } |
---|
67 | |
---|
68 | /** Positions the reader before its <code>n</code>th value. */ |
---|
69 | public synchronized void seek(long n) throws IOException { |
---|
70 | key.set(n); |
---|
71 | seek(key); |
---|
72 | } |
---|
73 | |
---|
74 | /** Read and return the next value in the file. */ |
---|
75 | public synchronized Writable next(Writable value) throws IOException { |
---|
76 | return next(key, value) ? value : null; |
---|
77 | } |
---|
78 | |
---|
79 | /** Returns the key associated with the most recent call to {@link |
---|
80 | * #seek(long)}, {@link #next(Writable)}, or {@link |
---|
81 | * #get(long,Writable)}. */ |
---|
82 | public synchronized long key() throws IOException { |
---|
83 | return key.get(); |
---|
84 | } |
---|
85 | |
---|
86 | /** Return the <code>n</code>th value in the file. */ |
---|
87 | public synchronized Writable get(long n, Writable value) |
---|
88 | throws IOException { |
---|
89 | key.set(n); |
---|
90 | return get(key, value); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | } |
---|