1   /**
2    * Copyright 2010 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.hbase.io.hfile;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.hadoop.fs.FSDataOutputStream;
26  import org.apache.hadoop.fs.Path;
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.junit.Test;
30  
31  import static org.junit.Assert.*;
32  
33  /**
34   * Test {@link HFileScanner#reseekTo(byte[])}
35   */
36  public class TestReseekTo {
37  
38    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
39  
40    @Test
41    public void testReseekTo() throws Exception {
42  
43      Path ncTFile = new Path(HBaseTestingUtility.getTestDir(), "basic.hfile");
44      FSDataOutputStream fout = TEST_UTIL.getTestFileSystem().create(ncTFile);
45      HFile.Writer writer = new HFile.Writer(fout, 4000, "none", null);
46      int numberOfKeys = 1000;
47  
48      String valueString = "Value";
49  
50      List<Integer> keyList = new ArrayList<Integer>();
51      List<String> valueList = new ArrayList<String>();
52  
53      for (int key = 0; key < numberOfKeys; key++) {
54        String value = valueString + key;
55        keyList.add(key);
56        valueList.add(value);
57        writer.append(Bytes.toBytes(key), Bytes.toBytes(value));
58      }
59      writer.close();
60      fout.close();
61  
62      HFile.Reader reader = new HFile.Reader(TEST_UTIL.getTestFileSystem(),
63          ncTFile, null, false);
64      reader.loadFileInfo();
65      HFileScanner scanner = reader.getScanner(false, true);
66  
67      scanner.seekTo();
68      for (int i = 0; i < keyList.size(); i++) {
69        Integer key = keyList.get(i);
70        String value = valueList.get(i);
71        long start = System.nanoTime();
72        scanner.seekTo(Bytes.toBytes(key));
73        System.out.println("Seek Finished in: " + (System.nanoTime() - start)/1000 + " micro s");
74        assertEquals(value, scanner.getValueString());
75      }
76  
77      scanner.seekTo();
78      for (int i = 0; i < keyList.size(); i += 10) {
79        Integer key = keyList.get(i);
80        String value = valueList.get(i);
81        long start = System.nanoTime();
82        scanner.reseekTo(Bytes.toBytes(key));
83        System.out.println("Reseek Finished in: " + (System.nanoTime() - start)/1000 + " micro s");
84        assertEquals(value, scanner.getValueString());
85      }
86    }
87  
88  }