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 static org.junit.Assert.assertEquals;
22  
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.hadoop.fs.FSDataOutputStream;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.HConstants;
31  import org.apache.hadoop.hbase.KeyValue;
32  import org.apache.hadoop.hbase.SmallTests;
33  import org.apache.hadoop.hbase.Tag;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  /**
39   * Test {@link HFileScanner#reseekTo(byte[])}
40   */
41  @Category(SmallTests.class)
42  public class TestReseekTo {
43  
44    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
45  
46    @Test
47    public void testReseekTo() throws Exception {
48      testReseekToInternals(TagUsage.NO_TAG);
49      testReseekToInternals(TagUsage.ONLY_TAG);
50      testReseekToInternals(TagUsage.PARTIAL_TAG);
51    }
52  
53    private void testReseekToInternals(TagUsage tagUsage) throws IOException {
54      Path ncTFile = new Path(TEST_UTIL.getDataTestDir(), "basic.hfile");
55      FSDataOutputStream fout = TEST_UTIL.getTestFileSystem().create(ncTFile);
56      if(tagUsage != TagUsage.NO_TAG){
57        TEST_UTIL.getConfiguration().setInt("hfile.format.version", 3);
58      }
59      CacheConfig cacheConf = new CacheConfig(TEST_UTIL.getConfiguration());
60      HFileContext context = new HFileContextBuilder().withBlockSize(4000).build();
61      HFile.Writer writer = HFile.getWriterFactory(
62          TEST_UTIL.getConfiguration(), cacheConf)
63              .withOutputStream(fout)
64              .withFileContext(context)
65              // NOTE: This test is dependent on this deprecated nonstandard comparator
66              .withComparator(new KeyValue.RawBytesComparator())
67              .create();
68      int numberOfKeys = 1000;
69  
70      String valueString = "Value";
71  
72      List<Integer> keyList = new ArrayList<Integer>();
73      List<String> valueList = new ArrayList<String>();
74  
75      for (int key = 0; key < numberOfKeys; key++) {
76        String value = valueString + key;
77        keyList.add(key);
78        valueList.add(value);
79        if(tagUsage == TagUsage.NO_TAG){
80          writer.append(Bytes.toBytes(key), Bytes.toBytes(value));
81        } else if (tagUsage == TagUsage.ONLY_TAG) {
82          Tag t = new Tag((byte) 1, "myTag1");
83          writer.append(Bytes.toBytes(key), Bytes.toBytes(value), t.getBuffer());
84        } else {
85          if (key % 4 == 0) {
86            Tag t = new Tag((byte) 1, "myTag1");
87            writer.append(Bytes.toBytes(key), Bytes.toBytes(value), t.getBuffer());
88          } else {
89            writer.append(Bytes.toBytes(key), Bytes.toBytes(value), HConstants.EMPTY_BYTE_ARRAY);
90          }
91        }
92      }
93      writer.close();
94      fout.close();
95  
96      HFile.Reader reader = HFile.createReader(TEST_UTIL.getTestFileSystem(),
97          ncTFile, cacheConf, TEST_UTIL.getConfiguration());
98      reader.loadFileInfo();
99      HFileScanner scanner = reader.getScanner(false, true);
100 
101     scanner.seekTo();
102     for (int i = 0; i < keyList.size(); i++) {
103       Integer key = keyList.get(i);
104       String value = valueList.get(i);
105       long start = System.nanoTime();
106       scanner.seekTo(Bytes.toBytes(key));
107       assertEquals(value, scanner.getValueString());
108     }
109 
110     scanner.seekTo();
111     for (int i = 0; i < keyList.size(); i += 10) {
112       Integer key = keyList.get(i);
113       String value = valueList.get(i);
114       long start = System.nanoTime();
115       scanner.reseekTo(Bytes.toBytes(key));
116       assertEquals("i is " + i, value, scanner.getValueString());
117     }
118 
119     reader.close();
120   }
121 
122 
123 }
124