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.CellComparatorImpl; 029import org.apache.hadoop.hbase.HBaseClassTestRule; 030import org.apache.hadoop.hbase.HBaseTestingUtility; 031import org.apache.hadoop.hbase.HConstants; 032import org.apache.hadoop.hbase.KeyValue; 033import org.apache.hadoop.hbase.Tag; 034import org.apache.hadoop.hbase.testclassification.IOTests; 035import org.apache.hadoop.hbase.testclassification.SmallTests; 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 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 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( 069 TEST_UTIL.getConfiguration(), cacheConf) 070 .withOutputStream(fout) 071 .withFileContext(context) 072 // NOTE: This test is dependent on this deprecated nonstandard comparator 073 .withComparator(CellComparatorImpl.COMPARATOR) 074 .create(); 075 int numberOfKeys = 1000; 076 077 String valueString = "Value"; 078 079 List<Integer> keyList = new ArrayList<>(); 080 List<String> valueList = new ArrayList<>(); 081 082 for (int key = 0; key < numberOfKeys; key++) { 083 String value = valueString + key; 084 KeyValue kv; 085 keyList.add(key); 086 valueList.add(value); 087 if(tagUsage == TagUsage.NO_TAG){ 088 kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"), 089 Bytes.toBytes(value)); 090 writer.append(kv); 091 } else if (tagUsage == TagUsage.ONLY_TAG) { 092 Tag t = new ArrayBackedTag((byte) 1, "myTag1"); 093 Tag[] tags = new Tag[1]; 094 tags[0] = t; 095 kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"), 096 HConstants.LATEST_TIMESTAMP, Bytes.toBytes(value), tags); 097 writer.append(kv); 098 } else { 099 if (key % 4 == 0) { 100 Tag t = new ArrayBackedTag((byte) 1, "myTag1"); 101 Tag[] tags = new Tag[1]; 102 tags[0] = t; 103 kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"), 104 HConstants.LATEST_TIMESTAMP, Bytes.toBytes(value), tags); 105 writer.append(kv); 106 } else { 107 kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"), 108 HConstants.LATEST_TIMESTAMP, Bytes.toBytes(value)); 109 writer.append(kv); 110 } 111 } 112 } 113 writer.close(); 114 fout.close(); 115 116 HFile.Reader reader = HFile.createReader(TEST_UTIL.getTestFileSystem(), ncTFile, cacheConf, 117 true, TEST_UTIL.getConfiguration()); 118 reader.loadFileInfo(); 119 HFileScanner scanner = reader.getScanner(false, true); 120 121 scanner.seekTo(); 122 for (int i = 0; i < keyList.size(); i++) { 123 Integer key = keyList.get(i); 124 String value = valueList.get(i); 125 long start = System.nanoTime(); 126 scanner.seekTo(new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes 127 .toBytes("qual"), Bytes.toBytes(value))); 128 assertEquals(value, scanner.getValueString()); 129 } 130 131 scanner.seekTo(); 132 for (int i = 0; i < keyList.size(); i += 10) { 133 Integer key = keyList.get(i); 134 String value = valueList.get(i); 135 long start = System.nanoTime(); 136 scanner.reseekTo(new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes 137 .toBytes("qual"), Bytes.toBytes(value))); 138 assertEquals("i is " + i, value, scanner.getValueString()); 139 } 140 141 reader.close(); 142 } 143 144 145} 146