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.master.region;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.List;
023import org.apache.hadoop.hbase.Cell;
024import org.apache.hadoop.hbase.client.Result;
025import org.apache.hadoop.hbase.client.ResultScanner;
026import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
027import org.apache.hadoop.hbase.regionserver.RegionScanner;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * Wrap a {@link RegionScanner} as a {@link ResultScanner}.
034 */
035@InterfaceAudience.Private
036class RegionScannerAsResultScanner implements ResultScanner {
037
038  private static final Logger LOG = LoggerFactory.getLogger(RegionScannerAsResultScanner.class);
039
040  private final RegionScanner scanner;
041
042  private boolean moreRows = true;
043
044  private final List<Cell> cells = new ArrayList<>();
045
046  RegionScannerAsResultScanner(RegionScanner scanner) {
047    this.scanner = scanner;
048  }
049
050  @Override
051  public boolean renewLease() {
052    return true;
053  }
054
055  @Override
056  public Result next() throws IOException {
057    if (!moreRows) {
058      return null;
059    }
060    for (;;) {
061      moreRows = scanner.next(cells);
062      if (cells.isEmpty()) {
063        if (!moreRows) {
064          return null;
065        } else {
066          continue;
067        }
068      }
069      Result result = Result.create(cells);
070      cells.clear();
071      return result;
072    }
073  }
074
075  @Override
076  public ScanMetrics getScanMetrics() {
077    return null;
078  }
079
080  @Override
081  public void close() {
082    try {
083      scanner.close();
084    } catch (IOException e) {
085      LOG.warn("Failed to close scanner", e);
086    }
087  }
088}