diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/ContentSizeCachedKeyValue.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/ContentSizeCachedKeyValue.java new file mode 100644 index 0000000..9ce1b6f --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/ContentSizeCachedKeyValue.java @@ -0,0 +1,98 @@ +/** + * Copyright The Apache Software Foundation + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase; + +import org.apache.hadoop.hbase.classification.InterfaceAudience; +import org.apache.hadoop.hbase.util.Bytes; + +/** + * This class is an extension to KeyValue where most of the individual component (RowKey, Family, + * Qualifier etc) offsets and lengths are cached. Parsing the backing byte[] every time to get these + * values will affect the performance. In read path, we tend to read these values many times in + * Comparator, SQM etc. + */ +@InterfaceAudience.Private +public class ContentSizeCachedKeyValue extends KeyValue { + private short rowLen; + private int famOffset; + private byte famLen; + private int qualLen; + private int tagsLen; + private byte type; + private long ts; + + public ContentSizeCachedKeyValue(byte[] bytes, int offset, int length) { + super(bytes, offset, length); + // We will read all these cached values at least once. Initialize now itself so that we can + // avoid uninitialized checks with every time call + rowLen = super.getRowLength(); + famOffset = super.getFamilyOffset(); + famLen = super.getFamilyLength(); + qualLen = super.getQualifierLength(); + tagsLen = super.getTagsLength(); + type = super.getTypeByte(); + ts = super.getTimestamp(); + } + + public short getRowLength() { + return rowLen; + } + + @Override + public int getFamilyOffset() { + return famOffset; + } + + @Override + public byte getFamilyLength() { + return famLen; + } + + @Override + public int getQualifierOffset() { + return famOffset + famLen; + } + + @Override + public int getQualifierLength() { + return qualLen; + } + + @Override + public int getTagsLength() { + return tagsLen; + } + + @Override + public byte getTypeByte() { + return type; + } + + @Override + public long getTimestamp() { + return ts; + } + + @Override + public long heapSize() { + return super.heapSize() + Bytes.SIZEOF_LONG + (3 * Bytes.SIZEOF_INT) + Bytes.SIZEOF_SHORT + + (2 * Bytes.SIZEOF_BYTE); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java index 7de1f54..65a9997 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java @@ -136,6 +136,8 @@ public class KeyValue implements Cell, HeapSize, Cloneable, SettableSequenceId, Bytes.SIZEOF_INT /*keylength*/ + Bytes.SIZEOF_INT /*valuelength*/; + public static final int ROW_KEY_OFFSET = ROW_OFFSET + ROW_LENGTH_SIZE; + // Size of the length ints in a KeyValue datastructure. public static final int KEYVALUE_INFRASTRUCTURE_SIZE = ROW_OFFSET; @@ -1301,7 +1303,7 @@ public class KeyValue implements Cell, HeapSize, Cloneable, SettableSequenceId, */ @Override public int getRowOffset() { - return getKeyOffset() + Bytes.SIZEOF_SHORT; + return this.offset + ROW_KEY_OFFSET; } /** @@ -1332,7 +1334,7 @@ public class KeyValue implements Cell, HeapSize, Cloneable, SettableSequenceId, * @return Family offset */ private int getFamilyOffset(int rlength) { - return this.offset + ROW_OFFSET + Bytes.SIZEOF_SHORT + rlength + Bytes.SIZEOF_BYTE; + return this.offset + ROW_KEY_OFFSET + rlength + Bytes.SIZEOF_BYTE; } /** diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java index 1e84e6a..3fdeee3 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java @@ -34,6 +34,7 @@ import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; +import org.apache.hadoop.hbase.ContentSizeCachedKeyValue; import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.KeyValue.KVComparator; @@ -772,7 +773,7 @@ public class HFileReaderImpl implements HFile.Reader, Configurable { if (!isSeeked()) return null; - KeyValue ret = new KeyValue(blockBuffer.array(), blockBuffer.arrayOffset() + KeyValue ret = new ContentSizeCachedKeyValue(blockBuffer.array(), blockBuffer.arrayOffset() + blockBuffer.position(), getCellBufSize()); if (this.reader.shouldIncludeMemstoreTS()) { ret.setSequenceId(currMemstoreTS);