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