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 junit.framework.TestCase; |
---|
23 | |
---|
24 | import org.apache.commons.logging.*; |
---|
25 | |
---|
26 | import org.apache.hadoop.fs.*; |
---|
27 | import org.apache.hadoop.conf.*; |
---|
28 | |
---|
29 | /** Support for flat files of binary key/value pairs. */ |
---|
30 | public class TestArrayFile extends TestCase { |
---|
31 | private static final Log LOG = LogFactory.getLog(TestArrayFile.class); |
---|
32 | private static String FILE = |
---|
33 | System.getProperty("test.build.data",".") + "/test.array"; |
---|
34 | |
---|
35 | public TestArrayFile(String name) { |
---|
36 | super(name); |
---|
37 | } |
---|
38 | |
---|
39 | public void testArrayFile() throws Exception { |
---|
40 | Configuration conf = new Configuration(); |
---|
41 | FileSystem fs = FileSystem.getLocal(conf); |
---|
42 | RandomDatum[] data = generate(10000); |
---|
43 | writeTest(fs, data, FILE); |
---|
44 | readTest(fs, data, FILE, conf); |
---|
45 | } |
---|
46 | |
---|
47 | public void testEmptyFile() throws Exception { |
---|
48 | Configuration conf = new Configuration(); |
---|
49 | FileSystem fs = FileSystem.getLocal(conf); |
---|
50 | writeTest(fs, new RandomDatum[0], FILE); |
---|
51 | ArrayFile.Reader reader = new ArrayFile.Reader(fs, FILE, conf); |
---|
52 | assertNull(reader.get(0, new RandomDatum())); |
---|
53 | reader.close(); |
---|
54 | } |
---|
55 | |
---|
56 | private static RandomDatum[] generate(int count) { |
---|
57 | LOG.debug("generating " + count + " records in debug"); |
---|
58 | RandomDatum[] data = new RandomDatum[count]; |
---|
59 | RandomDatum.Generator generator = new RandomDatum.Generator(); |
---|
60 | for (int i = 0; i < count; i++) { |
---|
61 | generator.next(); |
---|
62 | data[i] = generator.getValue(); |
---|
63 | } |
---|
64 | return data; |
---|
65 | } |
---|
66 | |
---|
67 | private static void writeTest(FileSystem fs, RandomDatum[] data, String file) |
---|
68 | throws IOException { |
---|
69 | Configuration conf = new Configuration(); |
---|
70 | MapFile.delete(fs, file); |
---|
71 | LOG.debug("creating with " + data.length + " debug"); |
---|
72 | ArrayFile.Writer writer = new ArrayFile.Writer(conf, fs, file, RandomDatum.class); |
---|
73 | writer.setIndexInterval(100); |
---|
74 | for (int i = 0; i < data.length; i++) |
---|
75 | writer.append(data[i]); |
---|
76 | writer.close(); |
---|
77 | } |
---|
78 | |
---|
79 | private static void readTest(FileSystem fs, RandomDatum[] data, String file, Configuration conf) |
---|
80 | throws IOException { |
---|
81 | RandomDatum v = new RandomDatum(); |
---|
82 | LOG.debug("reading " + data.length + " debug"); |
---|
83 | ArrayFile.Reader reader = new ArrayFile.Reader(fs, file, conf); |
---|
84 | for (int i = 0; i < data.length; i++) { // try forwards |
---|
85 | reader.get(i, v); |
---|
86 | if (!v.equals(data[i])) { |
---|
87 | throw new RuntimeException("wrong value at " + i); |
---|
88 | } |
---|
89 | } |
---|
90 | for (int i = data.length-1; i >= 0; i--) { // then backwards |
---|
91 | reader.get(i, v); |
---|
92 | if (!v.equals(data[i])) { |
---|
93 | throw new RuntimeException("wrong value at " + i); |
---|
94 | } |
---|
95 | } |
---|
96 | reader.close(); |
---|
97 | LOG.debug("done reading " + data.length + " debug"); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | /** For debugging and testing. */ |
---|
102 | public static void main(String[] args) throws Exception { |
---|
103 | int count = 1024 * 1024; |
---|
104 | boolean create = true; |
---|
105 | boolean check = true; |
---|
106 | String file = FILE; |
---|
107 | String usage = "Usage: TestArrayFile [-count N] [-nocreate] [-nocheck] file"; |
---|
108 | |
---|
109 | if (args.length == 0) { |
---|
110 | System.err.println(usage); |
---|
111 | System.exit(-1); |
---|
112 | } |
---|
113 | |
---|
114 | Configuration conf = new Configuration(); |
---|
115 | int i = 0; |
---|
116 | Path fpath = null; |
---|
117 | FileSystem fs = null; |
---|
118 | try { |
---|
119 | for (; i < args.length; i++) { // parse command line |
---|
120 | if (args[i] == null) { |
---|
121 | continue; |
---|
122 | } else if (args[i].equals("-count")) { |
---|
123 | count = Integer.parseInt(args[++i]); |
---|
124 | } else if (args[i].equals("-nocreate")) { |
---|
125 | create = false; |
---|
126 | } else if (args[i].equals("-nocheck")) { |
---|
127 | check = false; |
---|
128 | } else { |
---|
129 | // file is required parameter |
---|
130 | file = args[i]; |
---|
131 | fpath=new Path(file); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | fs = fpath.getFileSystem(conf); |
---|
136 | |
---|
137 | LOG.info("count = " + count); |
---|
138 | LOG.info("create = " + create); |
---|
139 | LOG.info("check = " + check); |
---|
140 | LOG.info("file = " + file); |
---|
141 | |
---|
142 | RandomDatum[] data = generate(count); |
---|
143 | |
---|
144 | if (create) { |
---|
145 | writeTest(fs, data, file); |
---|
146 | } |
---|
147 | |
---|
148 | if (check) { |
---|
149 | readTest(fs, data, file, conf); |
---|
150 | } |
---|
151 | } finally { |
---|
152 | fs.close(); |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|