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(TEST_UTIL.getConfiguration(), cacheConf)
068      .withOutputStream(fout).withFileContext(context).create();
069    int numberOfKeys = 1000;
070
071    String valueString = "Value";
072
073    List<Integer> keyList = new ArrayList<>();
074    List<String> valueList = new ArrayList<>();
075
076    for (int key = 0; key < numberOfKeys; key++) {
077      String value = valueString + key;
078      KeyValue kv;
079      keyList.add(key);
080      valueList.add(value);
081      if (tagUsage == TagUsage.NO_TAG) {
082        kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"),
083          Bytes.toBytes(value));
084        writer.append(kv);
085      } else if (tagUsage == TagUsage.ONLY_TAG) {
086        Tag t = new ArrayBackedTag((byte) 1, "myTag1");
087        Tag[] tags = new Tag[1];
088        tags[0] = t;
089        kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"),
090          HConstants.LATEST_TIMESTAMP, Bytes.toBytes(value), tags);
091        writer.append(kv);
092      } else {
093        if (key % 4 == 0) {
094          Tag t = new ArrayBackedTag((byte) 1, "myTag1");
095          Tag[] tags = new Tag[1];
096          tags[0] = t;
097          kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"),
098            HConstants.LATEST_TIMESTAMP, Bytes.toBytes(value), tags);
099          writer.append(kv);
100        } else {
101          kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"),
102            HConstants.LATEST_TIMESTAMP, Bytes.toBytes(value));
103          writer.append(kv);
104        }
105      }
106    }
107    writer.close();
108    fout.close();
109
110    HFile.Reader reader = HFile.createReader(TEST_UTIL.getTestFileSystem(), ncTFile, cacheConf,
111      true, TEST_UTIL.getConfiguration());
112    HFileScanner scanner = reader.getScanner(TEST_UTIL.getConfiguration(), false, true);
113
114    scanner.seekTo();
115    for (int i = 0; i < keyList.size(); i++) {
116      Integer key = keyList.get(i);
117      String value = valueList.get(i);
118      long start = System.nanoTime();
119      scanner.seekTo(new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"),
120        Bytes.toBytes("qual"), Bytes.toBytes(value)));
121      assertEquals(value, scanner.getValueString());
122    }
123
124    scanner.seekTo();
125    for (int i = 0; i < keyList.size(); i += 10) {
126      Integer key = keyList.get(i);
127      String value = valueList.get(i);
128      long start = System.nanoTime();
129      scanner.reseekTo(new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"),
130        Bytes.toBytes("qual"), Bytes.toBytes(value)));
131      assertEquals("i is " + i, value, scanner.getValueString());
132    }
133
134    reader.close();
135  }
136
137}