001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Cellersion 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY CellIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.regionserver;
021
022import java.nio.ByteBuffer;
023
024import org.apache.hadoop.hbase.Cell;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.apache.hadoop.hbase.util.Bytes;
027import org.apache.hadoop.hbase.util.ByteBufferUtils;
028import org.apache.hadoop.hbase.util.ClassSize;
029
030import java.util.Comparator;
031
032
033/**
034 * CellChunkMap is an array of serialized representations of Cell
035 * (pointing to Chunks with full Cell data) and can be allocated both off-heap and on-heap.
036 *
037 * CellChunkMap is a byte array (chunk) holding all that is needed to access a Cell, which
038 * is actually saved on another deeper chunk.
039 * Per Cell we have a reference to this deeper byte array B (chunk ID, integer),
040 * offset in bytes in B (integer), length in bytes in B (integer) and seqID of the cell (long).
041 * In order to save reference to byte array we use the Chunk's ID given by ChunkCreator.
042 *
043 * The CellChunkMap memory layout on chunk A relevant to a deeper byte array B,
044 * holding the actual cell data:
045 *
046 * < header > <---------------     first Cell     -----------------> <-- second Cell ...
047 * --------------------------------------------------------------------------------------- ...
048 *  integer  | integer      | integer      | integer     | long     |
049 *  4 bytes  | 4 bytes      | 4 bytes      | 4 bytes     | 8 bytes  |
050 *  ChunkID  | chunkID of   | offset in B  | length of   | sequence |          ...
051 *  of this  | chunk B with | where Cell's | Cell's      | ID of    |
052 *  chunk A  | Cell data    | data starts  | data in B   | the Cell |
053 * --------------------------------------------------------------------------------------- ...
054 */
055@InterfaceAudience.Private
056public class CellChunkMap extends CellFlatMap {
057
058  private final Chunk[] chunks;             // the array of chunks, on which the index is based
059
060  // number of cell-representations in a chunk
061  // depends on the size of the chunks (may be index chunks or regular data chunks)
062  // each chunk starts with its own ID following the cells data
063  private final int numOfCellRepsInChunk;
064
065  /**
066   * C-tor for creating CellChunkMap from existing Chunk array, which must be ordered
067   * (decreasingly or increasingly according to parameter "descending")
068   * @param comparator a tool for comparing cells
069   * @param chunks ordered array of index chunk with cell representations
070   * @param min the index of the first cell (usually 0)
071   * @param max number of Cells or the index of the cell after the maximal cell
072   * @param descending the order of the given array
073   */
074  public CellChunkMap(Comparator<? super Cell> comparator,
075      Chunk[] chunks, int min, int max, boolean descending) {
076    super(comparator, min, max, descending);
077    this.chunks = chunks;
078    if (chunks != null && chunks.length != 0 && chunks[0] != null) {
079      this.numOfCellRepsInChunk = (chunks[0].size - ChunkCreator.SIZEOF_CHUNK_HEADER) /
080              ClassSize.CELL_CHUNK_MAP_ENTRY;
081    } else { // In case the chunks array was not allocated
082      this.numOfCellRepsInChunk = 0;
083    }
084  }
085
086  /* To be used by base (CellFlatMap) class only to create a sub-CellFlatMap
087  * Should be used only to create only CellChunkMap from CellChunkMap */
088  @Override
089  protected CellFlatMap createSubCellFlatMap(int min, int max, boolean descending) {
090    return new CellChunkMap(this.comparator(), this.chunks, min, max, descending);
091  }
092
093
094  @Override
095  protected Cell getCell(int i) {
096    // get the index of the relevant chunk inside chunk array
097    int chunkIndex = (i / numOfCellRepsInChunk);
098    ByteBuffer block = chunks[chunkIndex].getData();// get the ByteBuffer of the relevant chunk
099    int j = i - chunkIndex * numOfCellRepsInChunk; // get the index of the cell-representation
100
101    // find inside the offset inside the chunk holding the index, skip bytes for chunk id
102    int offsetInBytes = ChunkCreator.SIZEOF_CHUNK_HEADER + j* ClassSize.CELL_CHUNK_MAP_ENTRY;
103
104    // find the chunk holding the data of the cell, the chunkID is stored first
105    int chunkId = ByteBufferUtils.toInt(block, offsetInBytes);
106    Chunk chunk = ChunkCreator.getInstance().getChunk(chunkId);
107    if (chunk == null) {
108      // this should not happen
109      throw new IllegalArgumentException("In CellChunkMap, cell must be associated with chunk."
110          + ". We were looking for a cell at index " + i);
111    }
112
113    // find the offset of the data of the cell, skip integer for chunkID, offset is stored second
114    int offsetOfCell = ByteBufferUtils.toInt(block, offsetInBytes + Bytes.SIZEOF_INT);
115    // find the length of the data of the cell, skip two integers for chunkID and offset,
116    // length is stored third
117    int lengthOfCell = ByteBufferUtils.toInt(block, offsetInBytes + 2*Bytes.SIZEOF_INT);
118    // find the seqID of the cell, skip three integers for chunkID, offset, and length
119    // the seqID is plain written as part of the cell representation
120    long cellSeqID = ByteBufferUtils.toLong(block, offsetInBytes + 3*Bytes.SIZEOF_INT);
121
122    ByteBuffer buf = chunk.getData();   // get the ByteBuffer where the cell data is stored
123    if (buf == null) {
124      // this should not happen
125      throw new IllegalArgumentException("In CellChunkMap, chunk must be associated with ByteBuffer."
126          + " Chunk: " + chunk + " Chunk ID: " + chunk.getId() + ", is from pool: "
127          + chunk.isFromPool() + ". We were looking for a cell at index " + i);
128    }
129
130    return new ByteBufferChunkKeyValue(buf, offsetOfCell, lengthOfCell, cellSeqID);
131  }
132}