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 java.nio.ByteBuffer;
021import org.apache.hadoop.hbase.util.Bytes;
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * This Cell is an implementation of {@link ByteBufferExtendedCell} where the data resides in off
026 * heap/ on heap ByteBuffer
027 */
028@InterfaceAudience.Private
029public class SizeCachedByteBufferKeyValue extends ByteBufferKeyValue {
030
031  public static final int FIXED_OVERHEAD = Bytes.SIZEOF_SHORT + Bytes.SIZEOF_INT;
032  private short rowLen;
033  private int keyLen;
034
035  public SizeCachedByteBufferKeyValue(ByteBuffer buf, int offset, int length, long seqId,
036    int keyLen) {
037    super(buf, offset, length);
038    // We will read all these cached values at least once. Initialize now itself so that we can
039    // avoid uninitialized checks with every time call
040    this.rowLen = super.getRowLength();
041    this.keyLen = keyLen;
042    setSequenceId(seqId);
043  }
044
045  public SizeCachedByteBufferKeyValue(ByteBuffer buf, int offset, int length, long seqId,
046    int keyLen, short rowLen) {
047    super(buf, offset, length);
048    // We will read all these cached values at least once. Initialize now itself so that we can
049    // avoid uninitialized checks with every time call
050    this.rowLen = rowLen;
051    this.keyLen = keyLen;
052    setSequenceId(seqId);
053  }
054
055  @Override
056  public short getRowLength() {
057    return rowLen;
058  }
059
060  @Override
061  public int getKeyLength() {
062    return this.keyLen;
063  }
064
065  @Override
066  public long heapSize() {
067    return super.heapSize() + FIXED_OVERHEAD;
068  }
069
070  /**
071   * Override by just returning the length for saving cost of method dispatching. If not, it will
072   * call {@link ExtendedCell#getSerializedSize()} firstly, then forward to
073   * {@link SizeCachedKeyValue#getSerializedSize(boolean)}. (See HBASE-21657)
074   */
075  @Override
076  public int getSerializedSize() {
077    return this.length;
078  }
079
080  @Override
081  public boolean equals(Object other) {
082    return super.equals(other);
083  }
084
085  @Override
086  public int hashCode() {
087    return super.hashCode();
088  }
089}