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 66f18d0..695c880 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 @@ -135,6 +135,8 @@ public class KeyValue implements Cell, HeapSize, Cloneable { 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; @@ -1289,7 +1291,7 @@ public class KeyValue implements Cell, HeapSize, Cloneable { */ @Override public int getRowOffset() { - return getKeyOffset() + Bytes.SIZEOF_SHORT; + return this.offset + ROW_KEY_OFFSET; } /** @@ -1320,7 +1322,7 @@ public class KeyValue implements Cell, HeapSize, Cloneable { * @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-common/src/main/java/org/apache/hadoop/hbase/NoTagsKeyValue.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/NoTagsKeyValue.java deleted file mode 100644 index 2c6b170..0000000 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/NoTagsKeyValue.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * 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; - -/** - * An extension of the KeyValue where the tags length is always 0 - */ -@InterfaceAudience.Private -public class NoTagsKeyValue extends KeyValue { - public NoTagsKeyValue(byte[] bytes, int offset, int length) { - super(bytes, offset, length); - } - - @Override - @Deprecated - public short getTagsLength() { - return (short) getTagsLengthUnsigned(); - } - - /** - * This returns the total length of the tag bytes - */ - @Override - @Deprecated - public int getTagsLengthUnsigned() { - return 0; - } -} \ No newline at end of file diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/ContentSizeCachedKeyValue.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/ContentSizeCachedKeyValue.java new file mode 100644 index 0000000..404701f --- /dev/null +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/ContentSizeCachedKeyValue.java @@ -0,0 +1,67 @@ +/** + * 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 rowLen and keyLen 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. + * Note: Please do not use these objects in write path and it will increase the heap space usage. + */ +@InterfaceAudience.Private +public class ContentSizeCachedKeyValue extends KeyValue { + + private static final int HEAP_SIZE_OVERHEAD = Bytes.SIZEOF_SHORT + Bytes.SIZEOF_INT; + + private short rowLen; + private int keyLen; + + 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(); + keyLen = super.getKeyLength(); + } + + @Override + public short getRowLength() { + return rowLen; + } + + @Override + public int getKeyLength() { + return this.keyLen; + } + + @Override + public long heapSize() { + return super.heapSize() + HEAP_SIZE_OVERHEAD; + } + + @Override + @Deprecated + public long heapSizeWithoutTags() { + return super.heapSizeWithoutTags() + HEAP_SIZE_OVERHEAD; + } +} \ No newline at end of file diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/ContentSizeCachedNoTagsKeyValue.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/ContentSizeCachedNoTagsKeyValue.java new file mode 100644 index 0000000..33fb267 --- /dev/null +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/ContentSizeCachedNoTagsKeyValue.java @@ -0,0 +1,45 @@ +/** + * 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; + +/** + * This class is an extension to ContentSizeCachedKeyValue where there are no tags in Cell. + * Note: Please do not use these objects in write path and it will increase the heap space usage. + */ +@InterfaceAudience.Private +public class ContentSizeCachedNoTagsKeyValue extends ContentSizeCachedKeyValue { + + public ContentSizeCachedNoTagsKeyValue(byte[] bytes, int offset, int length) { + super(bytes, offset, length); + } + + @Override + public int getTagsLengthUnsigned() { + return 0; + } + + @Override + @Deprecated + public long heapSizeWithoutTags() { + return super.heapSize(); + } +} diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderV2.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderV2.java index 933e773..ffbd4fb 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderV2.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderV2.java @@ -28,6 +28,7 @@ import org.apache.commons.logging.LogFactory; import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; +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; @@ -688,7 +689,7 @@ public class HFileReaderV2 extends AbstractHFileReader { } protected KeyValue formKeyValue() { - KeyValue ret = new KeyValue(blockBuffer.array(), blockBuffer.arrayOffset() + KeyValue ret = new ContentSizeCachedKeyValue(blockBuffer.array(), blockBuffer.arrayOffset() + blockBuffer.position(), getCellBufSize()); if (this.reader.shouldIncludeMemstoreTS()) { ret.setMvccVersion(currMemstoreTS); diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderV3.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderV3.java index fb9fd16..6c25d95 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderV3.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderV3.java @@ -26,9 +26,9 @@ import org.apache.commons.logging.LogFactory; import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hbase.ContentSizeCachedNoTagsKeyValue; import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.KeyValue; -import org.apache.hadoop.hbase.NoTagsKeyValue; import org.apache.hadoop.hbase.fs.HFileSystem; import org.apache.hadoop.hbase.io.FSDataInputStreamWrapper; import org.apache.hadoop.hbase.io.crypto.Cipher; @@ -195,7 +195,7 @@ public class HFileReaderV3 extends HFileReaderV2 { if (currTagsLen > 0) { return formKeyValue(); } else { - NoTagsKeyValue ret = new NoTagsKeyValue(blockBuffer.array(), blockBuffer.arrayOffset() + ContentSizeCachedNoTagsKeyValue ret = new ContentSizeCachedNoTagsKeyValue(blockBuffer.array(), blockBuffer.arrayOffset() + blockBuffer.position(), getCellBufSize()); if (this.reader.shouldIncludeMemstoreTS()) { ret.setMvccVersion(currMemstoreTS);