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