source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/io/file/tfile/TestTFile.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 14.1 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with this
4 * work for additional information regarding copyright ownership. The ASF
5 * licenses this file to you under the Apache License, Version 2.0 (the
6 * "License"); you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17
18package org.apache.hadoop.io.file.tfile;
19
20import java.io.DataInputStream;
21import java.io.DataOutputStream;
22import java.io.IOException;
23import java.util.Arrays;
24
25import junit.framework.TestCase;
26
27import org.apache.hadoop.conf.Configuration;
28import org.apache.hadoop.fs.FSDataInputStream;
29import org.apache.hadoop.fs.FSDataOutputStream;
30import org.apache.hadoop.fs.FileSystem;
31import org.apache.hadoop.fs.Path;
32import org.apache.hadoop.io.file.tfile.TFile.Reader;
33import org.apache.hadoop.io.file.tfile.TFile.Writer;
34import org.apache.hadoop.io.file.tfile.TFile.Reader.Scanner;
35
36/**
37 * test tfile features.
38 *
39 */
40public class TestTFile extends TestCase {
41  private static String ROOT =
42      System.getProperty("test.build.data", "/tmp/tfile-test");
43  private FileSystem fs;
44  private Configuration conf;
45  private final int minBlockSize = 512;
46  private final int largeVal = 3 * 1024 * 1024;
47  private static String localFormatter = "%010d";
48
49  @Override
50  public void setUp() throws IOException {
51    conf = new Configuration();
52    fs = FileSystem.get(conf);
53  }
54
55  @Override
56  public void tearDown() throws IOException {
57    // do nothing
58  }
59
60  // read a key from the scanner
61  public byte[] readKey(Scanner scanner) throws IOException {
62    int keylen = scanner.entry().getKeyLength();
63    byte[] read = new byte[keylen];
64    scanner.entry().getKey(read);
65    return read;
66  }
67
68  // read a value from the scanner
69  public byte[] readValue(Scanner scanner) throws IOException {
70    int valueLen = scanner.entry().getValueLength();
71    byte[] read = new byte[valueLen];
72    scanner.entry().getValue(read);
73    return read;
74  }
75
76  // read a long value from the scanner
77  public byte[] readLongValue(Scanner scanner, int len) throws IOException {
78    DataInputStream din = scanner.entry().getValueStream();
79    byte[] b = new byte[len];
80    din.readFully(b);
81    din.close();
82    return b;
83  }
84
85  // write some records into the tfile
86  // write them twice
87  private int writeSomeRecords(Writer writer, int start, int n)
88      throws IOException {
89    String value = "value";
90    for (int i = start; i < (start + n); i++) {
91      String key = String.format(localFormatter, i);
92      writer.append(key.getBytes(), (value + key).getBytes());
93      writer.append(key.getBytes(), (value + key).getBytes());
94    }
95    return (start + n);
96  }
97
98  // read the records and check
99  private int readAndCheckbytes(Scanner scanner, int start, int n)
100      throws IOException {
101    String value = "value";
102    for (int i = start; i < (start + n); i++) {
103      byte[] key = readKey(scanner);
104      byte[] val = readValue(scanner);
105      String keyStr = String.format(localFormatter, i);
106      String valStr = value + keyStr;
107      assertTrue("btyes for keys do not match " + keyStr + " "
108          + new String(key), Arrays.equals(keyStr.getBytes(), key));
109      assertTrue("bytes for vals do not match " + valStr + " "
110          + new String(val), Arrays.equals(
111          valStr.getBytes(), val));
112      assertTrue(scanner.advance());
113      key = readKey(scanner);
114      val = readValue(scanner);
115      assertTrue("btyes for keys do not match", Arrays.equals(
116          keyStr.getBytes(), key));
117      assertTrue("bytes for vals do not match", Arrays.equals(
118          valStr.getBytes(), val));
119      assertTrue(scanner.advance());
120    }
121    return (start + n);
122  }
123
124  // write some large records
125  // write them twice
126  private int writeLargeRecords(Writer writer, int start, int n)
127      throws IOException {
128    byte[] value = new byte[largeVal];
129    for (int i = start; i < (start + n); i++) {
130      String key = String.format(localFormatter, i);
131      writer.append(key.getBytes(), value);
132      writer.append(key.getBytes(), value);
133    }
134    return (start + n);
135  }
136
137  // read large records
138  // read them twice since its duplicated
139  private int readLargeRecords(Scanner scanner, int start, int n)
140      throws IOException {
141    for (int i = start; i < (start + n); i++) {
142      byte[] key = readKey(scanner);
143      String keyStr = String.format(localFormatter, i);
144      assertTrue("btyes for keys do not match", Arrays.equals(
145          keyStr.getBytes(), key));
146      scanner.advance();
147      key = readKey(scanner);
148      assertTrue("btyes for keys do not match", Arrays.equals(
149          keyStr.getBytes(), key));
150      scanner.advance();
151    }
152    return (start + n);
153  }
154
155  // write empty keys and values
156  private void writeEmptyRecords(Writer writer, int n) throws IOException {
157    byte[] key = new byte[0];
158    byte[] value = new byte[0];
159    for (int i = 0; i < n; i++) {
160      writer.append(key, value);
161    }
162  }
163
164  // read empty keys and values
165  private void readEmptyRecords(Scanner scanner, int n) throws IOException {
166    byte[] key = new byte[0];
167    byte[] value = new byte[0];
168    byte[] readKey = null;
169    byte[] readValue = null;
170    for (int i = 0; i < n; i++) {
171      readKey = readKey(scanner);
172      readValue = readValue(scanner);
173      assertTrue("failed to match keys", Arrays.equals(readKey, key));
174      assertTrue("failed to match values", Arrays.equals(readValue, value));
175      assertTrue("failed to advance cursor", scanner.advance());
176    }
177  }
178
179  private int writePrepWithKnownLength(Writer writer, int start, int n)
180      throws IOException {
181    // get the length of the key
182    String key = String.format(localFormatter, start);
183    int keyLen = key.getBytes().length;
184    String value = "value" + key;
185    int valueLen = value.getBytes().length;
186    for (int i = start; i < (start + n); i++) {
187      DataOutputStream out = writer.prepareAppendKey(keyLen);
188      String localKey = String.format(localFormatter, i);
189      out.write(localKey.getBytes());
190      out.close();
191      out = writer.prepareAppendValue(valueLen);
192      String localValue = "value" + localKey;
193      out.write(localValue.getBytes());
194      out.close();
195    }
196    return (start + n);
197  }
198
199  private int readPrepWithKnownLength(Scanner scanner, int start, int n)
200      throws IOException {
201    for (int i = start; i < (start + n); i++) {
202      String key = String.format(localFormatter, i);
203      byte[] read = readKey(scanner);
204      assertTrue("keys not equal", Arrays.equals(key.getBytes(), read));
205      String value = "value" + key;
206      read = readValue(scanner);
207      assertTrue("values not equal", Arrays.equals(value.getBytes(), read));
208      scanner.advance();
209    }
210    return (start + n);
211  }
212
213  private int writePrepWithUnkownLength(Writer writer, int start, int n)
214      throws IOException {
215    for (int i = start; i < (start + n); i++) {
216      DataOutputStream out = writer.prepareAppendKey(-1);
217      String localKey = String.format(localFormatter, i);
218      out.write(localKey.getBytes());
219      out.close();
220      String value = "value" + localKey;
221      out = writer.prepareAppendValue(-1);
222      out.write(value.getBytes());
223      out.close();
224    }
225    return (start + n);
226  }
227
228  private int readPrepWithUnknownLength(Scanner scanner, int start, int n)
229      throws IOException {
230    for (int i = start; i < start; i++) {
231      String key = String.format(localFormatter, i);
232      byte[] read = readKey(scanner);
233      assertTrue("keys not equal", Arrays.equals(key.getBytes(), read));
234      try {
235        read = readValue(scanner);
236        assertTrue(false);
237      }
238      catch (IOException ie) {
239        // should have thrown exception
240      }
241      String value = "value" + key;
242      read = readLongValue(scanner, value.getBytes().length);
243      assertTrue("values nto equal", Arrays.equals(read, value.getBytes()));
244      scanner.advance();
245    }
246    return (start + n);
247  }
248
249  private byte[] getSomeKey(int rowId) {
250    return String.format(localFormatter, rowId).getBytes();
251  }
252
253  private void writeRecords(Writer writer) throws IOException {
254    writeEmptyRecords(writer, 10);
255    int ret = writeSomeRecords(writer, 0, 100);
256    ret = writeLargeRecords(writer, ret, 1);
257    ret = writePrepWithKnownLength(writer, ret, 40);
258    ret = writePrepWithUnkownLength(writer, ret, 50);
259    writer.close();
260  }
261
262  private void readAllRecords(Scanner scanner) throws IOException {
263    readEmptyRecords(scanner, 10);
264    int ret = readAndCheckbytes(scanner, 0, 100);
265    ret = readLargeRecords(scanner, ret, 1);
266    ret = readPrepWithKnownLength(scanner, ret, 40);
267    ret = readPrepWithUnknownLength(scanner, ret, 50);
268  }
269
270  private FSDataOutputStream createFSOutput(Path name) throws IOException {
271    if (fs.exists(name)) fs.delete(name, true);
272    FSDataOutputStream fout = fs.create(name);
273    return fout;
274  }
275
276  /**
277   * test none codecs
278   */
279  void basicWithSomeCodec(String codec) throws IOException {
280    Path ncTFile = new Path(ROOT, "basic.tfile");
281    FSDataOutputStream fout = createFSOutput(ncTFile);
282    Writer writer = new Writer(fout, minBlockSize, codec, "memcmp", conf);
283    writeRecords(writer);
284    fout.close();
285    FSDataInputStream fin = fs.open(ncTFile);
286    Reader reader =
287        new Reader(fs.open(ncTFile), fs.getFileStatus(ncTFile).getLen(), conf);
288
289    Scanner scanner = reader.createScanner();
290    readAllRecords(scanner);
291    scanner.seekTo(getSomeKey(50));
292    assertTrue("location lookup failed", scanner.seekTo(getSomeKey(50)));
293    // read the key and see if it matches
294    byte[] readKey = readKey(scanner);
295    assertTrue("seeked key does not match", Arrays.equals(getSomeKey(50),
296        readKey));
297
298    scanner.seekTo(new byte[0]);
299    byte[] val1 = readValue(scanner);
300    scanner.seekTo(new byte[0]);
301    byte[] val2 = readValue(scanner);
302    assertTrue(Arrays.equals(val1, val2));
303   
304    // check for lowerBound
305    scanner.lowerBound(getSomeKey(50));
306    assertTrue("locaton lookup failed", scanner.currentLocation
307        .compareTo(reader.end()) < 0);
308    readKey = readKey(scanner);
309    assertTrue("seeked key does not match", Arrays.equals(readKey,
310        getSomeKey(50)));
311
312    // check for upper bound
313    scanner.upperBound(getSomeKey(50));
314    assertTrue("location lookup failed", scanner.currentLocation
315        .compareTo(reader.end()) < 0);
316    readKey = readKey(scanner);
317    assertTrue("seeked key does not match", Arrays.equals(readKey,
318        getSomeKey(51)));
319
320    scanner.close();
321    // test for a range of scanner
322    scanner = reader.createScanner(getSomeKey(10), getSomeKey(60));
323    readAndCheckbytes(scanner, 10, 50);
324    assertFalse(scanner.advance());
325    scanner.close();
326    reader.close();
327    fin.close();
328    fs.delete(ncTFile, true);
329  }
330
331  // unsorted with some codec
332  void unsortedWithSomeCodec(String codec) throws IOException {
333    Path uTfile = new Path(ROOT, "unsorted.tfile");
334    FSDataOutputStream fout = createFSOutput(uTfile);
335    Writer writer = new Writer(fout, minBlockSize, codec, null, conf);
336    writeRecords(writer);
337    writer.close();
338    fout.close();
339    FSDataInputStream fin = fs.open(uTfile);
340    Reader reader =
341        new Reader(fs.open(uTfile), fs.getFileStatus(uTfile).getLen(), conf);
342
343    Scanner scanner = reader.createScanner();
344    readAllRecords(scanner);
345    scanner.close();
346    reader.close();
347    fin.close();
348    fs.delete(uTfile, true);
349  }
350
351  public void testTFileFeatures() throws IOException {
352    basicWithSomeCodec("none");
353    basicWithSomeCodec("gz");
354  }
355
356  // test unsorted t files.
357  public void testUnsortedTFileFeatures() throws IOException {
358    unsortedWithSomeCodec("none");
359    unsortedWithSomeCodec("gz");
360  }
361
362  private void writeNumMetablocks(Writer writer, String compression, int n)
363      throws IOException {
364    for (int i = 0; i < n; i++) {
365      DataOutputStream dout =
366          writer.prepareMetaBlock("TfileMeta" + i, compression);
367      byte[] b = ("something to test" + i).getBytes();
368      dout.write(b);
369      dout.close();
370    }
371  }
372
373  private void someTestingWithMetaBlock(Writer writer, String compression)
374      throws IOException {
375    DataOutputStream dout = null;
376    writeNumMetablocks(writer, compression, 10);
377    try {
378      dout = writer.prepareMetaBlock("TfileMeta1", compression);
379      assertTrue(false);
380    }
381    catch (MetaBlockAlreadyExists me) {
382      // avoid this exception
383    }
384    dout = writer.prepareMetaBlock("TFileMeta100", compression);
385    dout.close();
386  }
387
388  private void readNumMetablocks(Reader reader, int n) throws IOException {
389    int len = ("something to test" + 0).getBytes().length;
390    for (int i = 0; i < n; i++) {
391      DataInputStream din = reader.getMetaBlock("TfileMeta" + i);
392      byte b[] = new byte[len];
393      din.readFully(b);
394      assertTrue("faield to match metadata", Arrays.equals(
395          ("something to test" + i).getBytes(), b));
396      din.close();
397    }
398  }
399
400  private void someReadingWithMetaBlock(Reader reader) throws IOException {
401    DataInputStream din = null;
402    readNumMetablocks(reader, 10);
403    try {
404      din = reader.getMetaBlock("NO ONE");
405      assertTrue(false);
406    }
407    catch (MetaBlockDoesNotExist me) {
408      // should catch
409    }
410    din = reader.getMetaBlock("TFileMeta100");
411    int read = din.read();
412    assertTrue("check for status", (read == -1));
413    din.close();
414  }
415
416  // test meta blocks for tfiles
417  public void testMetaBlocks() throws IOException {
418    Path mFile = new Path(ROOT, "meta.tfile");
419    FSDataOutputStream fout = createFSOutput(mFile);
420    Writer writer = new Writer(fout, minBlockSize, "none", null, conf);
421    someTestingWithMetaBlock(writer, "none");
422    writer.close();
423    fout.close();
424    FSDataInputStream fin = fs.open(mFile);
425    Reader reader = new Reader(fin, fs.getFileStatus(mFile).getLen(), conf);
426    someReadingWithMetaBlock(reader);
427    fs.delete(mFile, true);
428    reader.close();
429    fin.close();
430  }
431}
Note: See TracBrowser for help on using the repository browser.