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.jupiter.api.Assertions.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.HBaseTestingUtil;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.KeyValue;
031import org.apache.hadoop.hbase.Tag;
032import org.apache.hadoop.hbase.testclassification.IOTests;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.apache.hadoop.hbase.util.ByteBufferUtils;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.junit.jupiter.api.Test;
037
038/**
039 * Test {@link HFileScanner#reseekTo(org.apache.hadoop.hbase.Cell)}
040 */
041@org.junit.jupiter.api.Tag(IOTests.TAG)
042@org.junit.jupiter.api.Tag(SmallTests.TAG)
043public class TestReseekTo {
044
045  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
046
047  @Test
048  public void testReseekTo() throws Exception {
049    testReseekToInternals(TagUsage.NO_TAG);
050    testReseekToInternals(TagUsage.ONLY_TAG);
051    testReseekToInternals(TagUsage.PARTIAL_TAG);
052  }
053
054  private void testReseekToInternals(TagUsage tagUsage) throws IOException {
055    Path ncTFile = new Path(TEST_UTIL.getDataTestDir(), "basic.hfile");
056    FSDataOutputStream fout = TEST_UTIL.getTestFileSystem().create(ncTFile);
057    if (tagUsage != TagUsage.NO_TAG) {
058      TEST_UTIL.getConfiguration().setInt("hfile.format.version", 3);
059    }
060    CacheConfig cacheConf = new CacheConfig(TEST_UTIL.getConfiguration());
061    HFileContext context = new HFileContextBuilder().withBlockSize(4000).build();
062    HFile.Writer writer = HFile.getWriterFactory(TEST_UTIL.getConfiguration(), cacheConf)
063      .withOutputStream(fout).withFileContext(context).create();
064    int numberOfKeys = 1000;
065
066    String valueString = "Value";
067
068    List<Integer> keyList = new ArrayList<>();
069    List<String> valueList = new ArrayList<>();
070
071    for (int key = 0; key < numberOfKeys; key++) {
072      String value = valueString + key;
073      KeyValue kv;
074      keyList.add(key);
075      valueList.add(value);
076      if (tagUsage == TagUsage.NO_TAG) {
077        kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"),
078          Bytes.toBytes(value));
079        writer.append(kv);
080      } else if (tagUsage == TagUsage.ONLY_TAG) {
081        Tag t = new ArrayBackedTag((byte) 1, "myTag1");
082        Tag[] tags = new Tag[1];
083        tags[0] = t;
084        kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"),
085          HConstants.LATEST_TIMESTAMP, Bytes.toBytes(value), tags);
086        writer.append(kv);
087      } else {
088        if (key % 4 == 0) {
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          kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"),
097            HConstants.LATEST_TIMESTAMP, Bytes.toBytes(value));
098          writer.append(kv);
099        }
100      }
101    }
102    writer.close();
103    fout.close();
104
105    HFile.Reader reader = HFile.createReader(TEST_UTIL.getTestFileSystem(), ncTFile, cacheConf,
106      true, TEST_UTIL.getConfiguration());
107    HFileScanner scanner = reader.getScanner(TEST_UTIL.getConfiguration(), false, true);
108
109    scanner.seekTo();
110    for (int i = 0; i < keyList.size(); i++) {
111      Integer key = keyList.get(i);
112      String value = valueList.get(i);
113      long start = System.nanoTime();
114      scanner.seekTo(new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"),
115        Bytes.toBytes("qual"), Bytes.toBytes(value)));
116      assertEquals(value, ByteBufferUtils.toStringBinary(scanner.getValue()));
117    }
118
119    scanner.seekTo();
120    for (int i = 0; i < keyList.size(); i += 10) {
121      Integer key = keyList.get(i);
122      String value = valueList.get(i);
123      long start = System.nanoTime();
124      scanner.reseekTo(new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"),
125        Bytes.toBytes("qual"), Bytes.toBytes(value)));
126      assertEquals(value, ByteBufferUtils.toStringBinary(scanner.getValue()), "i is " + i);
127    }
128
129    reader.close();
130  }
131
132}