|
@@ -0,0 +1,84 @@
|
|
|
+/**
|
|
|
+ * Copyright 2007 The Apache Software Foundation
|
|
|
+ *
|
|
|
+ * Licensed to the Apache Software Foundation (ASF) under one
|
|
|
+ * or more contributor license agreements. See the NOTICE file
|
|
|
+ * distributed with this work for additional information
|
|
|
+ * regarding copyright ownership. The ASF licenses this file
|
|
|
+ * to you under the Apache License, Version 2.0 (the
|
|
|
+ * "License"); you may not use this file except in compliance
|
|
|
+ * with the License. You may obtain a copy of the License at
|
|
|
+ *
|
|
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
+ *
|
|
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
+ * See the License for the specific language governing permissions and
|
|
|
+ * limitations under the License.
|
|
|
+ */
|
|
|
+package org.apache.hadoop.io;
|
|
|
+
|
|
|
+import org.apache.hadoop.conf.Configuration;
|
|
|
+import org.apache.hadoop.fs.FileSystem;
|
|
|
+import org.apache.hadoop.fs.Path;
|
|
|
+
|
|
|
+import junit.framework.TestCase;
|
|
|
+
|
|
|
+public class TestMapFile extends TestCase {
|
|
|
+ private static Configuration conf = new Configuration();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test getClosest feature.
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void testGetClosest() throws Exception {
|
|
|
+ // Write a mapfile of simple data: keys are
|
|
|
+ Path dirName = new Path(System.getProperty("test.build.data",".") +
|
|
|
+ getName() + ".mapfile");
|
|
|
+ FileSystem fs = FileSystem.getLocal(conf);
|
|
|
+ Path qualifiedDirName = fs.makeQualified(dirName);
|
|
|
+ MapFile.Writer writer = new MapFile.Writer(conf, fs,
|
|
|
+ qualifiedDirName.toString(), Text.class, Text.class);
|
|
|
+ // Make an index entry for each insertion.
|
|
|
+ writer.setIndexInterval(1);
|
|
|
+ // Add entries up to 100 in intervals of ten.
|
|
|
+ final int FIRST_KEY = 10;
|
|
|
+ for (int i = FIRST_KEY; i < 100; i += 10) {
|
|
|
+ String iStr = Integer.toString(i);
|
|
|
+ Text t = new Text("00".substring(iStr.length()) + iStr);
|
|
|
+ writer.append(t, t);
|
|
|
+ }
|
|
|
+ writer.close();
|
|
|
+ // Now do getClosest on created mapfile.
|
|
|
+ MapFile.Reader reader = new MapFile.Reader(fs, qualifiedDirName.toString(),
|
|
|
+ conf);
|
|
|
+ Text key = new Text("55");
|
|
|
+ Text value = new Text();
|
|
|
+ Text closest = (Text)reader.getClosest(key, value);
|
|
|
+ // Assert that closest after 55 is 60
|
|
|
+ assertTrue(closest.equals(new Text("60")));
|
|
|
+ // Get closest that falls before the passed key: 50
|
|
|
+ closest = (Text)reader.getClosest(key, value, true);
|
|
|
+ assertTrue(closest.equals(new Text("50")));
|
|
|
+ // Test get closest when we pass explicit key
|
|
|
+ final Text TWENTY = new Text("20");
|
|
|
+ closest = (Text)reader.getClosest(TWENTY, value);
|
|
|
+ assertTrue(closest.equals(new Text(TWENTY)));
|
|
|
+ closest = (Text)reader.getClosest(TWENTY, value, true);
|
|
|
+ assertTrue(closest.equals(new Text(TWENTY)));
|
|
|
+ // Test what happens at boundaries. Assert if searching a key that is
|
|
|
+ // less than first key in the mapfile, that the first key is returned.
|
|
|
+ key = new Text("00");
|
|
|
+ closest = (Text)reader.getClosest(key, value);
|
|
|
+ assertEquals(Integer.parseInt(closest.toString()), FIRST_KEY);
|
|
|
+ closest = (Text)reader.getClosest(key, value, true);
|
|
|
+ assertEquals(Integer.parseInt(closest.toString()), FIRST_KEY);
|
|
|
+ // Assert that null is returned if key is > last entry in mapfile.
|
|
|
+ key = new Text("99");
|
|
|
+ closest = (Text)reader.getClosest(key, value);
|
|
|
+ assertNull(closest);
|
|
|
+ closest = (Text)reader.getClosest(key, value, true);
|
|
|
+ assertNull(closest);
|
|
|
+ }
|
|
|
+}
|