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.yetus.audience.InterfaceAudience;
022
023/**
024 * This class is a server side extension to the {@link Cell} interface. It is used when the Cell is
025 * backed by a {@link ByteBuffer}: i.e. <code>cell instanceof ByteBufferedCell</code>.
026 * <p>
027 * This class has getters for the row, column family, column qualifier, value and tags hosting
028 * ByteBuffers. It also has getters of the *position* within a ByteBuffer where these field bytes
029 * begin. These are needed because a single ByteBuffer may back one or many Cell instances -- it
030 * depends on the implementation -- so the ByteBuffer position as returned by
031 * {@link ByteBuffer#arrayOffset()} cannot be relied upon. Also, do not confuse these position
032 * methods with the getXXXOffset methods from the super Interface, {@link Cell}; dependent up on
033 * implementation, the Cell getXXXOffset methods can return the same value as a call to its
034 * equivalent position method from below BUT they can also stray; if a ByteBufferedCell, use the
035 * below position methods to find where a field begins.
036 * <p>
037 * Use the getXXXLength methods from Cell to find a fields length.
038 * <p>
039 * A Cell object can be of this type only on the server side.
040 * <p>
041 * WARNING: If a Cell is backed by an offheap ByteBuffer, any call to getXXXArray() will result in a
042 * temporary byte array creation and a bytes copy. Avoid these allocations by using the appropriate
043 * Cell access server-side: i.e. ByteBufferedCell when backed by a ByteBuffer and Cell when it is
044 * not.
045 */
046/*
047 * Even though all the methods are abstract, ByteBufferExtendedCell is not made to be an interface
048 * with intent. In CellComparator compare method, we have instance of check to decide whether to use
049 * getXXXArray() or getXXXByteBuffer(). This is a very hot method in read and write paths. if (left
050 * instanceof ByteBufferExtendedCell && right instanceof ByteBufferExtendedCell) { .... } if (left
051 * instanceof ByteBufferExtendedCell) { .... } if (right instanceof ByteBufferExtendedCell) { .... }
052 * return Bytes.compareTo(left.getRowArray(), left.getRowOffset(), left.getRowLength(),
053 * right.getRowArray(), right.getRowOffset(), right.getRowLength()); We did JMH micro benchmark
054 * tests with both left and right cells as ByteBufferExtendedCell, one only ByteBufferExtendedCell
055 * and both as Cells. This is compared against JMH results on compare logic with out any instance of
056 * checks. We noticed that if ByteBufferExtendedCell is an interface, the benchmark result seems to
057 * be very bad for case of both right and left are Cell only (Not ByteBufferExtendedCell). When
058 * ByteBufferExtendedCell is an abstract class all 4 possible cases giving almost similar
059 * performance number compared with compare logic with no instance of checks.
060 */
061@InterfaceAudience.Private
062public abstract class ByteBufferExtendedCell implements ExtendedCell {
063  /** Returns The {@link ByteBuffer} containing the row bytes. */
064  public abstract ByteBuffer getRowByteBuffer();
065
066  /** Returns Position in the {@link ByteBuffer} where row bytes start */
067  public abstract int getRowPosition();
068
069  /** Returns The {@link ByteBuffer} containing the column family bytes. */
070  public abstract ByteBuffer getFamilyByteBuffer();
071
072  /** Returns Position in the {@link ByteBuffer} where column family bytes start */
073  public abstract int getFamilyPosition();
074
075  /** Returns The {@link ByteBuffer} containing the column qualifier bytes. */
076  public abstract ByteBuffer getQualifierByteBuffer();
077
078  /** Returns Position in the {@link ByteBuffer} where column qualifier bytes start */
079  public abstract int getQualifierPosition();
080
081  /** Returns The {@link ByteBuffer} containing the value bytes. */
082  public abstract ByteBuffer getValueByteBuffer();
083
084  /** Returns Position in the {@link ByteBuffer} where value bytes start */
085  public abstract int getValuePosition();
086
087  /** Returns The {@link ByteBuffer} containing the tag bytes. */
088  public abstract ByteBuffer getTagsByteBuffer();
089
090  /** Returns Position in the {@link ByteBuffer} where tag bytes start */
091  public abstract int getTagsPosition();
092}