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(TEST_UTIL.getDataTestDir(), "basic.hfile");
44      FSDataOutputStream fout = TEST_UTIL.getTestFileSystem().create(ncTFile);
45      CacheConfig cacheConf = new CacheConfig(TEST_UTIL.getConfiguration());
46      HFile.Writer writer = HFile.getWriterFactory(
47          TEST_UTIL.getConfiguration(), cacheConf).createWriter(
48              fout, 4000, "none", null);
49      int numberOfKeys = 1000;
50  
51      String valueString = "Value";
52  
53      List<Integer> keyList = new ArrayList<Integer>();
54      List<String> valueList = new ArrayList<String>();
55  
56      for (int key = 0; key < numberOfKeys; key++) {
57        String value = valueString + key;
58        keyList.add(key);
59        valueList.add(value);
60        writer.append(Bytes.toBytes(key), Bytes.toBytes(value));
61      }
62      writer.close();
63      fout.close();
64  
65      HFile.Reader reader = HFile.createReader(TEST_UTIL.getTestFileSystem(),
66          ncTFile, cacheConf);
67      reader.loadFileInfo();
68      HFileScanner scanner = reader.getScanner(false, true);
69  
70      scanner.seekTo();
71      for (int i = 0; i < keyList.size(); i++) {
72        Integer key = keyList.get(i);
73        String value = valueList.get(i);
74        long start = System.nanoTime();
75        scanner.seekTo(Bytes.toBytes(key));
76        assertEquals(value, scanner.getValueString());
77      }
78  
79      scanner.seekTo();
80      for (int i = 0; i < keyList.size(); i += 10) {
81        Integer key = keyList.get(i);
82        String value = valueList.get(i);
83        long start = System.nanoTime();
84        scanner.reseekTo(Bytes.toBytes(key));
85        assertEquals(value, scanner.getValueString());
86      }
87    }
88  
89  }