
package org.apache.hadoop.hbase.client;

import static org.apache.hadoop.hbase.client.ConnectionUtils.createClosestRowAfter;
import static org.apache.hadoop.hbase.client.ConnectionUtils.isEmptyStartRow;
import static org.apache.hadoop.hbase.client.ConnectionUtils.noMoreResultsForScan;

import java.io.IOException;
import java.util.concurrent.ExecutorService;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.ipc.RpcControllerFactory;

/**
 * ClientSimpleScanner implements a sync scanner behaviour.
 * The cache is a simple list.
 * The prefetch is invoked only when the application finished processing the entire cache.
 */
@InterfaceAudience.Private
public class ClientSimpleScanner extends ClientScanner {
  public ClientSimpleScanner(Configuration configuration, Scan scan, TableName name,
      ClusterConnection connection, RpcRetryingCallerFactory rpcCallerFactory,
      RpcControllerFactory rpcControllerFactory, ExecutorService pool,
      int replicaCallTimeoutMicroSecondScan) throws IOException {
    super(configuration, scan, name, connection, rpcCallerFactory, rpcControllerFactory, pool,
        replicaCallTimeoutMicroSecondScan);
  }

  @Override
  protected boolean setNewStartKey() {
    if (noMoreResultsForScan(scan, currentRegion)) {
      return false;
    }
    scan.withStartRow(currentRegion.getEndKey(), true);
    return true;
  }

  @Override
  protected ScannerCallable createScannerCallable() {
    if (!scan.includeStartRow() && !isEmptyStartRow(scan.getStartRow())) {
      // we have not implemented locate to next row for sync client yet, so here we change the
      // inclusive of start row to true.
      scan.withStartRow(createClosestRowAfter(scan.getStartRow()), true);
    }
    return new ScannerCallable(getConnection(), getTable(), scan, this.scanMetrics,
        this.rpcControllerFactory);
  }
}