001/**
002 * Copyright The Apache Software Foundation
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020package org.apache.hadoop.hbase;
021
022import org.apache.hadoop.hbase.util.Bytes;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * This class is an extension to KeyValue where rowLen and keyLen are cached.
027 * Parsing the backing byte[] every time to get these values will affect the performance.
028 * In read path, we tend to read these values many times in Comparator, SQM etc.
029 * Note: Please do not use these objects in write path as it will increase the heap space usage.
030 * See https://issues.apache.org/jira/browse/HBASE-13448
031 */
032@InterfaceAudience.Private
033@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="EQ_DOESNT_OVERRIDE_EQUALS")
034public class SizeCachedKeyValue extends KeyValue {
035  // Overhead in this class alone. Parent's overhead will be considered in usage places by calls to
036  // super. methods
037  private static final int FIXED_OVERHEAD = Bytes.SIZEOF_SHORT + Bytes.SIZEOF_INT;
038
039  private short rowLen;
040  private int keyLen;
041
042  public SizeCachedKeyValue(byte[] bytes, int offset, int length, long seqId) {
043    super(bytes, offset, length);
044    // We will read all these cached values at least once. Initialize now itself so that we can
045    // avoid uninitialized checks with every time call
046    rowLen = super.getRowLength();
047    keyLen = super.getKeyLength();
048    setSequenceId(seqId);
049  }
050
051  @Override
052  public short getRowLength() {
053    return rowLen;
054  }
055
056  @Override
057  public int getKeyLength() {
058    return this.keyLen;
059  }
060
061  @Override
062  public long heapSize() {
063    return super.heapSize() + FIXED_OVERHEAD;
064  }
065}