source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/io/TestSetFile.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: 4.8 KB
Line 
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
19package org.apache.hadoop.io;
20
21import java.io.*;
22import java.util.*;
23import junit.framework.TestCase;
24
25import org.apache.commons.logging.*;
26
27import org.apache.hadoop.fs.*;
28import org.apache.hadoop.conf.*;
29import org.apache.hadoop.io.SequenceFile.CompressionType;
30
31/** Support for flat files of binary key/value pairs. */
32public class TestSetFile extends TestCase {
33  private static final Log LOG = LogFactory.getLog(TestSetFile.class);
34  private static String FILE =
35    System.getProperty("test.build.data",".") + "/test.set";
36
37  private static Configuration conf = new Configuration();
38 
39  public TestSetFile(String name) { super(name); }
40
41  public void testSetFile() throws Exception {
42    FileSystem fs = FileSystem.getLocal(conf);
43    try {
44      RandomDatum[] data = generate(10000);
45      writeTest(fs, data, FILE, CompressionType.NONE);
46      readTest(fs, data, FILE);
47
48      writeTest(fs, data, FILE, CompressionType.BLOCK);
49      readTest(fs, data, FILE);
50    } finally {
51      fs.close();
52    }
53  }
54
55  private static RandomDatum[] generate(int count) {
56    LOG.info("generating " + count + " records in memory");
57    RandomDatum[] data = new RandomDatum[count];
58    RandomDatum.Generator generator = new RandomDatum.Generator();
59    for (int i = 0; i < count; i++) {
60      generator.next();
61      data[i] = generator.getValue();
62    }
63    LOG.info("sorting " + count + " records");
64    Arrays.sort(data);
65    return data;
66  }
67
68  private static void writeTest(FileSystem fs, RandomDatum[] data,
69                                String file, CompressionType compress)
70    throws IOException {
71    MapFile.delete(fs, file);
72    LOG.info("creating with " + data.length + " records");
73    SetFile.Writer writer =
74      new SetFile.Writer(conf, fs, file,
75                         WritableComparator.get(RandomDatum.class),
76                         compress);
77    for (int i = 0; i < data.length; i++)
78      writer.append(data[i]);
79    writer.close();
80  }
81
82  private static void readTest(FileSystem fs, RandomDatum[] data, String file)
83    throws IOException {
84    RandomDatum v = new RandomDatum();
85    int sample = (int)Math.sqrt(data.length);
86    Random random = new Random();
87    LOG.info("reading " + sample + " records");
88    SetFile.Reader reader = new SetFile.Reader(fs, file, conf);
89    for (int i = 0; i < sample; i++) {
90      if (!reader.seek(data[random.nextInt(data.length)]))
91        throw new RuntimeException("wrong value at " + i);
92    }
93    reader.close();
94    LOG.info("done reading " + data.length);
95  }
96
97
98  /** For debugging and testing. */
99  public static void main(String[] args) throws Exception {
100    int count = 1024 * 1024;
101    boolean create = true;
102    boolean check = true;
103    String file = FILE;
104    String compress = "NONE";
105
106    String usage = "Usage: TestSetFile [-count N] [-nocreate] [-nocheck] [-compress type] file";
107     
108    if (args.length == 0) {
109      System.err.println(usage);
110      System.exit(-1);
111    }
112     
113    int i = 0;
114    Path fpath=null;
115    FileSystem fs = null;   
116    try {
117      for (; i < args.length; i++) {       // parse command line
118        if (args[i] == null) {
119          continue;
120        } else if (args[i].equals("-count")) {
121          count = Integer.parseInt(args[++i]);
122        } else if (args[i].equals("-nocreate")) {
123          create = false;
124        } else if (args[i].equals("-nocheck")) {
125          check = false;
126        } else if (args[i].equals("-compress")) {
127          compress = args[++i];
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("compress = " + compress);
141      LOG.info("file = " + file);
142     
143      RandomDatum[] data = generate(count);
144     
145      if (create) {
146        writeTest(fs, data, file, CompressionType.valueOf(compress));
147      }
148     
149      if (check) {
150        readTest(fs, data, file);
151      }
152 
153    } finally {
154      fs.close();
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.