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.regionserver;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.List;
023import java.util.NavigableSet;
024import org.apache.hadoop.hbase.Cell;
025import org.apache.hadoop.hbase.client.Scan;
026import org.apache.hadoop.hbase.mob.MobCell;
027import org.apache.hadoop.hbase.mob.MobUtils;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * ReversedMobStoreScanner extends from ReversedStoreScanner, and is used to support reversed
034 * scanning in both the memstore and the MOB store.
035 */
036@InterfaceAudience.Private
037public class ReversedMobStoreScanner extends ReversedStoreScanner {
038
039  private static final Logger LOG = LoggerFactory.getLogger(ReversedMobStoreScanner.class);
040  private boolean cacheMobBlocks = false;
041  private boolean rawMobScan = false;
042  private boolean readEmptyValueOnMobCellMiss = false;
043  private final HMobStore mobStore;
044  private final List<MobCell> referencedMobCells;
045
046  ReversedMobStoreScanner(HStore store, ScanInfo scanInfo, Scan scan, NavigableSet<byte[]> columns,
047    long readPt) throws IOException {
048    super(store, scanInfo, scan, columns, readPt);
049    cacheMobBlocks = MobUtils.isCacheMobBlocks(scan);
050    rawMobScan = MobUtils.isRawMobScan(scan);
051    readEmptyValueOnMobCellMiss = MobUtils.isReadEmptyValueOnMobCellMiss(scan);
052    if (!(store instanceof HMobStore)) {
053      throw new IllegalArgumentException("The store " + store + " is not a HMobStore");
054    }
055    mobStore = (HMobStore) store;
056    this.referencedMobCells = new ArrayList<>();
057  }
058
059  /**
060   * Firstly reads the cells from the HBase. If the cell is a reference cell (which has the
061   * reference tag), the scanner need seek this cell from the mob file, and use the cell found from
062   * the mob file as the result.
063   */
064  @Override
065  public boolean next(List<Cell> outResult, ScannerContext ctx) throws IOException {
066    boolean result = super.next(outResult, ctx);
067    if (!rawMobScan) {
068      // retrieve the mob data
069      if (outResult.isEmpty()) {
070        return result;
071      }
072      long mobKVCount = 0;
073      long mobKVSize = 0;
074      for (int i = 0; i < outResult.size(); i++) {
075        Cell cell = outResult.get(i);
076        if (MobUtils.isMobReferenceCell(cell)) {
077          MobCell mobCell =
078            mobStore.resolve(cell, cacheMobBlocks, readPt, readEmptyValueOnMobCellMiss);
079          mobKVCount++;
080          mobKVSize += mobCell.getCell().getValueLength();
081          outResult.set(i, mobCell.getCell());
082          // Keep the MobCell here unless we shipped the RPC or close the scanner.
083          referencedMobCells.add(mobCell);
084        }
085      }
086      mobStore.updateMobScanCellsCount(mobKVCount);
087      mobStore.updateMobScanCellsSize(mobKVSize);
088    }
089    return result;
090  }
091
092  private void freeAllReferencedMobCells() throws IOException {
093    for (MobCell mobCell : referencedMobCells) {
094      mobCell.close();
095    }
096    referencedMobCells.clear();
097  }
098
099  @Override
100  public void shipped() throws IOException {
101    super.shipped();
102    this.freeAllReferencedMobCells();
103  }
104
105  @Override
106  public void close() {
107    super.close();
108    try {
109      this.freeAllReferencedMobCells();
110    } catch (IOException e) {
111      LOG.warn("Failed to free referenced mob cells: ", e);
112    }
113  }
114}