1 | /** |
---|
2 | * Copyright 2007 The Apache Software Foundation |
---|
3 | * |
---|
4 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
5 | * or more contributor license agreements. See the NOTICE file |
---|
6 | * distributed with this work for additional information |
---|
7 | * regarding copyright ownership. The ASF licenses this file |
---|
8 | * to you under the Apache License, Version 2.0 (the |
---|
9 | * "License"); you may not use this file except in compliance |
---|
10 | * with the License. You may obtain a copy of the License at |
---|
11 | * |
---|
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
13 | * |
---|
14 | * Unless required by applicable law or agreed to in writing, software |
---|
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
17 | * See the License for the specific language governing permissions and |
---|
18 | * limitations under the License. |
---|
19 | */ |
---|
20 | package org.apache.hadoop.io; |
---|
21 | |
---|
22 | import org.apache.hadoop.conf.Configuration; |
---|
23 | import org.apache.hadoop.fs.FileSystem; |
---|
24 | import org.apache.hadoop.fs.Path; |
---|
25 | |
---|
26 | import junit.framework.TestCase; |
---|
27 | |
---|
28 | public class TestMapFile extends TestCase { |
---|
29 | private static Configuration conf = new Configuration(); |
---|
30 | |
---|
31 | /** |
---|
32 | * Test getClosest feature. |
---|
33 | * @throws Exception |
---|
34 | */ |
---|
35 | public void testGetClosest() throws Exception { |
---|
36 | // Write a mapfile of simple data: keys are |
---|
37 | Path dirName = new Path(System.getProperty("test.build.data",".") + |
---|
38 | getName() + ".mapfile"); |
---|
39 | FileSystem fs = FileSystem.getLocal(conf); |
---|
40 | Path qualifiedDirName = fs.makeQualified(dirName); |
---|
41 | // Make an index entry for every third insertion. |
---|
42 | MapFile.Writer.setIndexInterval(conf, 3); |
---|
43 | MapFile.Writer writer = new MapFile.Writer(conf, fs, |
---|
44 | qualifiedDirName.toString(), Text.class, Text.class); |
---|
45 | // Assert that the index interval is 1 |
---|
46 | assertEquals(3, writer.getIndexInterval()); |
---|
47 | // Add entries up to 100 in intervals of ten. |
---|
48 | final int FIRST_KEY = 10; |
---|
49 | for (int i = FIRST_KEY; i < 100; i += 10) { |
---|
50 | String iStr = Integer.toString(i); |
---|
51 | Text t = new Text("00".substring(iStr.length()) + iStr); |
---|
52 | writer.append(t, t); |
---|
53 | } |
---|
54 | writer.close(); |
---|
55 | // Now do getClosest on created mapfile. |
---|
56 | MapFile.Reader reader = new MapFile.Reader(fs, qualifiedDirName.toString(), |
---|
57 | conf); |
---|
58 | Text key = new Text("55"); |
---|
59 | Text value = new Text(); |
---|
60 | Text closest = (Text)reader.getClosest(key, value); |
---|
61 | // Assert that closest after 55 is 60 |
---|
62 | assertEquals(new Text("60"), closest); |
---|
63 | // Get closest that falls before the passed key: 50 |
---|
64 | closest = (Text)reader.getClosest(key, value, true); |
---|
65 | assertEquals(new Text("50"), closest); |
---|
66 | // Test get closest when we pass explicit key |
---|
67 | final Text TWENTY = new Text("20"); |
---|
68 | closest = (Text)reader.getClosest(TWENTY, value); |
---|
69 | assertEquals(TWENTY, closest); |
---|
70 | closest = (Text)reader.getClosest(TWENTY, value, true); |
---|
71 | assertEquals(TWENTY, closest); |
---|
72 | // Test what happens at boundaries. Assert if searching a key that is |
---|
73 | // less than first key in the mapfile, that the first key is returned. |
---|
74 | key = new Text("00"); |
---|
75 | closest = (Text)reader.getClosest(key, value); |
---|
76 | assertEquals(FIRST_KEY, Integer.parseInt(closest.toString())); |
---|
77 | |
---|
78 | // If we're looking for the first key before, and we pass in a key before |
---|
79 | // the first key in the file, we should get null |
---|
80 | closest = (Text)reader.getClosest(key, value, true); |
---|
81 | assertNull(closest); |
---|
82 | |
---|
83 | // Assert that null is returned if key is > last entry in mapfile. |
---|
84 | key = new Text("99"); |
---|
85 | closest = (Text)reader.getClosest(key, value); |
---|
86 | assertNull(closest); |
---|
87 | |
---|
88 | // If we were looking for the key before, we should get the last key |
---|
89 | closest = (Text)reader.getClosest(key, value, true); |
---|
90 | assertEquals(new Text("90"), closest); |
---|
91 | } |
---|
92 | } |
---|