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