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, int keyLen) {
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    this.rowLen = super.getRowLength();
047    this.keyLen = keyLen;
048    setSequenceId(seqId);
049  }
050
051  public SizeCachedKeyValue(byte[] bytes, int offset, int length, long seqId, int keyLen,
052      short rowLen) {
053    super(bytes, offset, length);
054    // We will read all these cached values at least once. Initialize now itself so that we can
055    // avoid uninitialized checks with every time call
056    this.rowLen = rowLen;
057    this.keyLen = keyLen;
058    setSequenceId(seqId);
059  }
060
061  @Override
062  public short getRowLength() {
063    return rowLen;
064  }
065
066  @Override
067  public int getKeyLength() {
068    return this.keyLen;
069  }
070
071  @Override
072  public long heapSize() {
073    return super.heapSize() + FIXED_OVERHEAD;
074  }
075
076  /**
077   * Override by just returning the length for saving cost of method dispatching. If not, it will
078   * call {@link ExtendedCell#getSerializedSize()} firstly, then forward to
079   * {@link SizeCachedKeyValue#getSerializedSize(boolean)}. (See HBASE-21657)
080   */
081  @Override
082  public int getSerializedSize() {
083    return this.length;
084  }
085}