View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.io.hfile;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import junit.framework.Assert;
25  
26  import org.apache.hadoop.fs.FSDataOutputStream;
27  import org.apache.hadoop.fs.Path;
28  import org.apache.hadoop.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.SmallTests;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  import static org.junit.Assert.*;
35  
36  /**
37   * Test {@link HFileScanner#reseekTo(byte[])}
38   */
39  @Category(SmallTests.class)
40  public class TestReseekTo {
41  
42    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
43  
44    @Test
45    public void testReseekTo() throws Exception {
46  
47      Path ncTFile = new Path(TEST_UTIL.getDataTestDir(), "basic.hfile");
48      FSDataOutputStream fout = TEST_UTIL.getTestFileSystem().create(ncTFile);
49      CacheConfig cacheConf = new CacheConfig(TEST_UTIL.getConfiguration());
50      HFile.Writer writer = HFile.getWriterFactory(
51          TEST_UTIL.getConfiguration(), cacheConf)
52              .withOutputStream(fout)
53              .withBlockSize(4000)
54              .create();
55      int numberOfKeys = 1000;
56  
57      String valueString = "Value";
58  
59      List<Integer> keyList = new ArrayList<Integer>();
60      List<String> valueList = new ArrayList<String>();
61  
62      for (int key = 0; key < numberOfKeys; key++) {
63        String value = valueString + key;
64        keyList.add(key);
65        valueList.add(value);
66        writer.append(Bytes.toBytes(key), Bytes.toBytes(value));
67      }
68      writer.close();
69      fout.close();
70  
71      HFile.Reader reader = HFile.createReader(TEST_UTIL.getTestFileSystem(),
72          ncTFile, cacheConf);
73      reader.loadFileInfo();
74      HFileScanner scanner = reader.getScanner(false, true);
75  
76      scanner.seekTo();
77      for (int i = 0; i < keyList.size(); i++) {
78        Integer key = keyList.get(i);
79        String value = valueList.get(i);
80        long start = System.nanoTime();
81        scanner.seekTo(Bytes.toBytes(key));
82        assertEquals(value, scanner.getValueString());
83      }
84  
85      scanner.seekTo();
86      for (int i = 0; i < keyList.size(); i += 10) {
87        Integer key = keyList.get(i);
88        String value = valueList.get(i);
89        long start = System.nanoTime();
90        scanner.reseekTo(Bytes.toBytes(key));
91        assertEquals("i is " + i, value, scanner.getValueString());
92      }
93  
94      reader.close();
95    }
96  
97  
98  }
99