001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.hfile;
019
020import static org.junit.Assert.assertEquals;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.List;
025import org.apache.hadoop.fs.FSDataOutputStream;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.ArrayBackedTag;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.KeyValue;
032import org.apache.hadoop.hbase.Tag;
033import org.apache.hadoop.hbase.testclassification.IOTests;
034import org.apache.hadoop.hbase.testclassification.SmallTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039
040/**
041 * Test {@link HFileScanner#reseekTo(org.apache.hadoop.hbase.Cell)}
042 */
043@Category({IOTests.class, SmallTests.class})
044public class TestReseekTo {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048      HBaseClassTestRule.forClass(TestReseekTo.class);
049
050  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
051
052  @Test
053  public void testReseekTo() throws Exception {
054    testReseekToInternals(TagUsage.NO_TAG);
055    testReseekToInternals(TagUsage.ONLY_TAG);
056    testReseekToInternals(TagUsage.PARTIAL_TAG);
057  }
058
059  private void testReseekToInternals(TagUsage tagUsage) throws IOException {
060    Path ncTFile = new Path(TEST_UTIL.getDataTestDir(), "basic.hfile");
061    FSDataOutputStream fout = TEST_UTIL.getTestFileSystem().create(ncTFile);
062    if(tagUsage != TagUsage.NO_TAG){
063      TEST_UTIL.getConfiguration().setInt("hfile.format.version", 3);
064    }
065    CacheConfig cacheConf = new CacheConfig(TEST_UTIL.getConfiguration());
066    HFileContext context = new HFileContextBuilder().withBlockSize(4000).build();
067    HFile.Writer writer = HFile.getWriterFactory(
068        TEST_UTIL.getConfiguration(), cacheConf)
069            .withOutputStream(fout)
070            .withFileContext(context)
071            .create();
072    int numberOfKeys = 1000;
073
074    String valueString = "Value";
075
076    List<Integer> keyList = new ArrayList<>();
077    List<String> valueList = new ArrayList<>();
078
079    for (int key = 0; key < numberOfKeys; key++) {
080      String value = valueString + key;
081      KeyValue kv;
082      keyList.add(key);
083      valueList.add(value);
084      if(tagUsage == TagUsage.NO_TAG){
085        kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"),
086            Bytes.toBytes(value));
087        writer.append(kv);
088      } else if (tagUsage == TagUsage.ONLY_TAG) {
089        Tag t = new ArrayBackedTag((byte) 1, "myTag1");
090        Tag[] tags = new Tag[1];
091        tags[0] = t;
092        kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"),
093            HConstants.LATEST_TIMESTAMP, Bytes.toBytes(value), tags);
094        writer.append(kv);
095      } else {
096        if (key % 4 == 0) {
097          Tag t = new ArrayBackedTag((byte) 1, "myTag1");
098          Tag[] tags = new Tag[1];
099          tags[0] = t;
100          kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"),
101              HConstants.LATEST_TIMESTAMP, Bytes.toBytes(value), tags);
102          writer.append(kv);
103        } else {
104          kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"),
105              HConstants.LATEST_TIMESTAMP, Bytes.toBytes(value));
106          writer.append(kv);
107        }
108      }
109    }
110    writer.close();
111    fout.close();
112
113    HFile.Reader reader = HFile.createReader(TEST_UTIL.getTestFileSystem(), ncTFile, cacheConf,
114      true, TEST_UTIL.getConfiguration());
115    HFileScanner scanner = reader.getScanner(false, true);
116
117    scanner.seekTo();
118    for (int i = 0; i < keyList.size(); i++) {
119      Integer key = keyList.get(i);
120      String value = valueList.get(i);
121      long start = System.nanoTime();
122      scanner.seekTo(new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes
123          .toBytes("qual"), Bytes.toBytes(value)));
124      assertEquals(value, scanner.getValueString());
125    }
126
127    scanner.seekTo();
128    for (int i = 0; i < keyList.size(); i += 10) {
129      Integer key = keyList.get(i);
130      String value = valueList.get(i);
131      long start = System.nanoTime();
132      scanner.reseekTo(new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes
133          .toBytes("qual"), Bytes.toBytes(value)));
134      assertEquals("i is " + i, value, scanner.getValueString());
135    }
136
137    reader.close();
138  }
139
140
141}
142