From 05a47fe8ea3e0bdd789a2bc8a333bf609bdcf37a Mon Sep 17 00:00:00 2001 From: chenglei Date: Mon, 3 Jul 2023 21:09:35 +0800 Subject: [PATCH] HBASE-27954 Eliminate duplicate code for getNonRootIndexedKey in HFileBlockIndex (#5312) Signed-off-by: Duo Zhang --- .../hbase/io/hfile/HFileBlockIndex.java | 12 ++--- .../hbase/io/hfile/NoOpIndexBlockEncoder.java | 47 ++----------------- 2 files changed, 8 insertions(+), 51 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileBlockIndex.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileBlockIndex.java index 12ef197af439..592c19c866cf 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileBlockIndex.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileBlockIndex.java @@ -399,14 +399,8 @@ public Cell midkey(CachingBlockReader cachingBlockReader) throws IOException { HFileBlock midLeafBlock = cachingBlockReader.readBlock(midLeafBlockOffset, midLeafBlockOnDiskSize, true, true, false, true, BlockType.LEAF_INDEX, null); try { - ByteBuff b = midLeafBlock.getBufferWithoutHeader(); - int numDataBlocks = b.getIntAfterPosition(0); - int keyRelOffset = b.getIntAfterPosition(Bytes.SIZEOF_INT * (midKeyEntry + 1)); - int keyLen = b.getIntAfterPosition(Bytes.SIZEOF_INT * (midKeyEntry + 2)) - keyRelOffset - - SECONDARY_INDEX_ENTRY_OVERHEAD; - int keyOffset = - Bytes.SIZEOF_INT * (numDataBlocks + 2) + keyRelOffset + SECONDARY_INDEX_ENTRY_OVERHEAD; - byte[] bytes = b.toBytes(keyOffset, keyLen); + byte[] bytes = getNonRootIndexedKey(midLeafBlock.getBufferWithoutHeader(), midKeyEntry); + assert bytes != null; targetMidKey = new KeyValue.KeyOnlyKeyValue(bytes, 0, bytes.length); } finally { midLeafBlock.release(); @@ -699,7 +693,7 @@ public int rootBlockContainingKey(final byte[] key, int offset, int length) { * @param i the ith position * @return The indexed key at the ith position in the nonRootIndex. */ - protected byte[] getNonRootIndexedKey(ByteBuff nonRootIndex, int i) { + static byte[] getNonRootIndexedKey(ByteBuff nonRootIndex, int i) { int numEntries = nonRootIndex.getInt(0); if (i < 0 || i >= numEntries) { return null; diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/NoOpIndexBlockEncoder.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/NoOpIndexBlockEncoder.java index 3115a5153c21..4162fca6afe5 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/NoOpIndexBlockEncoder.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/NoOpIndexBlockEncoder.java @@ -18,7 +18,6 @@ package org.apache.hadoop.hbase.io.hfile; import static org.apache.hadoop.hbase.io.hfile.HFileBlockIndex.MID_KEY_METADATA_SIZE; -import static org.apache.hadoop.hbase.io.hfile.HFileBlockIndex.SECONDARY_INDEX_ENTRY_OVERHEAD; import java.io.DataInput; import java.io.DataInputStream; @@ -269,14 +268,9 @@ public Cell midkey(HFile.CachingBlockReader cachingBlockReader) throws IOExcepti HFileBlock midLeafBlock = cachingBlockReader.readBlock(midLeafBlockOffset, midLeafBlockOnDiskSize, true, true, false, true, BlockType.LEAF_INDEX, null); try { - ByteBuff b = midLeafBlock.getBufferWithoutHeader(); - int numDataBlocks = b.getIntAfterPosition(0); - int keyRelOffset = b.getIntAfterPosition(Bytes.SIZEOF_INT * (midKeyEntry + 1)); - int keyLen = b.getIntAfterPosition(Bytes.SIZEOF_INT * (midKeyEntry + 2)) - keyRelOffset - - SECONDARY_INDEX_ENTRY_OVERHEAD; - int keyOffset = - Bytes.SIZEOF_INT * (numDataBlocks + 2) + keyRelOffset + SECONDARY_INDEX_ENTRY_OVERHEAD; - byte[] bytes = b.toBytes(keyOffset, keyLen); + byte[] bytes = HFileBlockIndex.BlockIndexReader + .getNonRootIndexedKey(midLeafBlock.getBufferWithoutHeader(), midKeyEntry); + assert bytes != null; targetMidKey = new KeyValue.KeyOnlyKeyValue(bytes, 0, bytes.length); } finally { midLeafBlock.release(); @@ -379,7 +373,8 @@ public BlockWithScanInfo loadDataBlockWithScanInfo(Cell key, HFileBlock currentB currentOnDiskSize = buffer.getInt(); // Only update next indexed key if there is a next indexed key in the current level - byte[] nonRootIndexedKey = getNonRootIndexedKey(buffer, index + 1); + byte[] nonRootIndexedKey = + HFileBlockIndex.BlockIndexReader.getNonRootIndexedKey(buffer, index + 1); if (nonRootIndexedKey != null) { tmpNextIndexKV.setKey(nonRootIndexedKey, 0, nonRootIndexedKey.length); nextIndexedKey = tmpNextIndexKV; @@ -441,37 +436,5 @@ public String toString() { } return sb.toString(); } - - /** - * The indexed key at the ith position in the nonRootIndex. The position starts at 0. - * @param i the ith position - * @return The indexed key at the ith position in the nonRootIndex. - */ - protected byte[] getNonRootIndexedKey(ByteBuff nonRootIndex, int i) { - int numEntries = nonRootIndex.getInt(0); - if (i < 0 || i >= numEntries) { - return null; - } - - // Entries start after the number of entries and the secondary index. - // The secondary index takes numEntries + 1 ints. - int entriesOffset = Bytes.SIZEOF_INT * (numEntries + 2); - // Targetkey's offset relative to the end of secondary index - int targetKeyRelOffset = nonRootIndex.getInt(Bytes.SIZEOF_INT * (i + 1)); - - // The offset of the target key in the blockIndex buffer - int targetKeyOffset = entriesOffset // Skip secondary index - + targetKeyRelOffset // Skip all entries until mid - + SECONDARY_INDEX_ENTRY_OVERHEAD; // Skip offset and on-disk-size - - // We subtract the two consecutive secondary index elements, which - // gives us the size of the whole (offset, onDiskSize, key) tuple. We - // then need to subtract the overhead of offset and onDiskSize. - int targetKeyLength = nonRootIndex.getInt(Bytes.SIZEOF_INT * (i + 2)) - targetKeyRelOffset - - SECONDARY_INDEX_ENTRY_OVERHEAD; - - // TODO check whether we can make BB backed Cell here? So can avoid bytes copy. - return nonRootIndex.toBytes(targetKeyOffset, targetKeyLength); - } } }