|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.io.hfile; |
| 19 | + |
| 20 | +import static org.apache.hadoop.hbase.io.ByteBuffAllocator.MIN_ALLOCATE_SIZE_KEY; |
| 21 | +import static org.junit.Assert.assertArrayEquals; |
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | +import static org.junit.Assert.assertTrue; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.nio.ByteBuffer; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import org.apache.hadoop.conf.Configuration; |
| 30 | +import org.apache.hadoop.fs.FileSystem; |
| 31 | +import org.apache.hadoop.fs.Path; |
| 32 | +import org.apache.hadoop.hbase.CellComparatorImpl; |
| 33 | +import org.apache.hadoop.hbase.CellUtil; |
| 34 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 35 | +import org.apache.hadoop.hbase.HBaseTestingUtil; |
| 36 | +import org.apache.hadoop.hbase.KeyValue; |
| 37 | +import org.apache.hadoop.hbase.SizeCachedNoTagsByteBufferKeyValue; |
| 38 | +import org.apache.hadoop.hbase.SizeCachedNoTagsKeyValue; |
| 39 | +import org.apache.hadoop.hbase.io.ByteBuffAllocator; |
| 40 | +import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding; |
| 41 | +import org.apache.hadoop.hbase.testclassification.IOTests; |
| 42 | +import org.apache.hadoop.hbase.testclassification.MediumTests; |
| 43 | +import org.apache.hadoop.hbase.util.Bytes; |
| 44 | +import org.junit.Before; |
| 45 | +import org.junit.ClassRule; |
| 46 | +import org.junit.Test; |
| 47 | +import org.junit.experimental.categories.Category; |
| 48 | + |
| 49 | +@Category({ IOTests.class, MediumTests.class }) |
| 50 | +public class TestRowIndexV1RoundTrip { |
| 51 | + @ClassRule |
| 52 | + public static final HBaseClassTestRule CLASS_RULE = |
| 53 | + HBaseClassTestRule.forClass(TestRowIndexV1RoundTrip.class); |
| 54 | + private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); |
| 55 | + private static final DataBlockEncoding DATA_BLOCK_ENCODING = DataBlockEncoding.ROW_INDEX_V1; |
| 56 | + private static final int ENTRY_COUNT = 100; |
| 57 | + |
| 58 | + private Configuration conf; |
| 59 | + private FileSystem fs; |
| 60 | + |
| 61 | + @Before |
| 62 | + public void setUp() throws IOException { |
| 63 | + conf = TEST_UTIL.getConfiguration(); |
| 64 | + conf.setLong(MIN_ALLOCATE_SIZE_KEY, 0); |
| 65 | + fs = FileSystem.get(conf); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testReadMyWritesOnHeap() throws IOException { |
| 70 | + Path hfilePath = new Path(TEST_UTIL.getDataTestDir(), "testHFileFormatV3"); |
| 71 | + writeDataToHFile(hfilePath, ENTRY_COUNT); |
| 72 | + readDataFromHFile(hfilePath, ENTRY_COUNT, true); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testReadMyWritesOnDirectMem() throws IOException { |
| 77 | + Path hfilePath = new Path(TEST_UTIL.getDataTestDir(), "testHFileFormatV3"); |
| 78 | + writeDataToHFile(hfilePath, ENTRY_COUNT); |
| 79 | + readDataFromHFile(hfilePath, ENTRY_COUNT, false); |
| 80 | + } |
| 81 | + |
| 82 | + private void writeDataToHFile(Path hfilePath, int entryCount) throws IOException { |
| 83 | + HFileContext context = |
| 84 | + new HFileContextBuilder().withBlockSize(1024).withDataBlockEncoding(DATA_BLOCK_ENCODING) |
| 85 | + .withCellComparator(CellComparatorImpl.COMPARATOR).build(); |
| 86 | + CacheConfig cacheConfig = new CacheConfig(conf); |
| 87 | + HFile.Writer writer = new HFile.WriterFactory(conf, cacheConfig).withPath(fs, hfilePath) |
| 88 | + .withFileContext(context).create(); |
| 89 | + |
| 90 | + List<KeyValue> keyValues = new ArrayList<>(entryCount); |
| 91 | + |
| 92 | + writeKeyValues(entryCount, writer, keyValues); |
| 93 | + } |
| 94 | + |
| 95 | + private void writeKeyValues(int entryCount, HFile.Writer writer, List<KeyValue> keyValues) |
| 96 | + throws IOException { |
| 97 | + for (int i = 0; i < entryCount; ++i) { |
| 98 | + byte[] keyBytes = intToBytes(i); |
| 99 | + |
| 100 | + byte[] valueBytes = Bytes.toBytes(String.format("value %d", i)); |
| 101 | + KeyValue keyValue = new KeyValue(keyBytes, null, null, valueBytes); |
| 102 | + |
| 103 | + writer.append(keyValue); |
| 104 | + keyValues.add(keyValue); |
| 105 | + } |
| 106 | + writer.close(); |
| 107 | + } |
| 108 | + |
| 109 | + private void readDataFromHFile(Path hfilePath, int entryCount, boolean onHeap) |
| 110 | + throws IOException { |
| 111 | + CacheConfig cacheConfig; |
| 112 | + if (onHeap) { |
| 113 | + cacheConfig = new CacheConfig(conf); |
| 114 | + } else { |
| 115 | + ByteBuffAllocator allocator = ByteBuffAllocator.create(conf, true); |
| 116 | + cacheConfig = new CacheConfig(conf, null, null, allocator); |
| 117 | + } |
| 118 | + HFile.Reader reader = HFile.createReader(fs, hfilePath, cacheConfig, false, conf); |
| 119 | + HFileScanner scanner = reader.getScanner(conf, false, false); |
| 120 | + scanner.seekTo(); |
| 121 | + int i = 1; |
| 122 | + while (scanner.next()) { |
| 123 | + byte[] keyBytes = intToBytes(i); |
| 124 | + // check row key from getKey() and getCell() separately because they use different code paths |
| 125 | + assertArrayEquals(keyBytes, CellUtil.cloneRow(scanner.getKey())); |
| 126 | + assertArrayEquals(keyBytes, CellUtil.cloneRow(scanner.getCell())); |
| 127 | + assertArrayEquals(Bytes.toBytes(String.format("value %d", i)), |
| 128 | + CellUtil.cloneValue(scanner.getCell())); |
| 129 | + if (onHeap) { |
| 130 | + assertTrue(scanner.getCell() instanceof SizeCachedNoTagsKeyValue); |
| 131 | + } else { |
| 132 | + assertTrue(scanner.getCell() instanceof SizeCachedNoTagsByteBufferKeyValue); |
| 133 | + } |
| 134 | + i += 1; |
| 135 | + } |
| 136 | + assertEquals(entryCount, i); |
| 137 | + } |
| 138 | + |
| 139 | + private byte[] intToBytes(final int i) { |
| 140 | + ByteBuffer bb = ByteBuffer.allocate(4); |
| 141 | + bb.putInt(i); |
| 142 | + return bb.array(); |
| 143 | + } |
| 144 | +} |
0 commit comments