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.mob;
019
020import java.io.Closeable;
021import java.io.IOException;
022import org.apache.hadoop.hbase.Cell;
023import org.apache.hadoop.hbase.regionserver.StoreFileScanner;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * The MobCell will maintain a {@link Cell} and a {@link StoreFileScanner} inside. Now, the mob cell
028 * is backend by NIO ByteBuffers which are allocated from ByteBuffAllocator, so we cannot just read
029 * the cell and close the MOB file scanner because the MOB file scanner closing will deallocate the
030 * NIO ByteBuffers, which resulting memory leak.
031 * <p>
032 * Actually, the right solution is: <br>
033 * 1. Read the normal cell; <br>
034 * 2. Parse the value of normal cell and get MOB fileName,offset,length; <br>
035 * 3. Open scanner to read the mob value; <br>
036 * 4. Construct the response cell whose key is from the normal cell and value is from the mob cell.
037 * <br>
038 * 5. Ship the response cell to HBase client. <br>
039 * 6. Release both normal cell's block and mob cell's block. <br>
040 * <p>
041 * For mob cell, the block releasing just means closing the the mob scanner, so here we need to keep
042 * the {@link StoreFileScanner} inside and close only when we're ensure that the MobCell has been
043 * shipped to RPC client.
044 */
045@InterfaceAudience.Private
046public class MobCell implements Closeable {
047
048  private final Cell cell;
049  private final StoreFileScanner sfScanner;
050
051  public MobCell(Cell cell) {
052    this.cell = cell;
053    this.sfScanner = null;
054  }
055
056  public MobCell(Cell cell, StoreFileScanner sfScanner) {
057    this.cell = cell;
058    this.sfScanner = sfScanner;
059  }
060
061  public Cell getCell() {
062    return cell;
063  }
064
065  @Override
066  public void close() throws IOException {
067    if (this.sfScanner != null) {
068      this.sfScanner.close();
069    }
070  }
071}