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, Version 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 KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.client; 020 021import java.io.Closeable; 022import java.io.IOException; 023import java.io.UncheckedIOException; 024import java.util.ArrayList; 025import java.util.Iterator; 026import java.util.List; 027 028import org.apache.yetus.audience.InterfaceAudience; 029import org.apache.hadoop.hbase.client.metrics.ScanMetrics; 030 031/** 032 * Interface for client-side scanning. Go to {@link Table} to obtain instances. 033 */ 034@InterfaceAudience.Public 035public interface ResultScanner extends Closeable, Iterable<Result> { 036 037 @Override 038 default Iterator<Result> iterator() { 039 return new Iterator<Result>() { 040 // The next RowResult, possibly pre-read 041 Result next = null; 042 043 // return true if there is another item pending, false if there isn't. 044 // this method is where the actual advancing takes place, but you need 045 // to call next() to consume it. hasNext() will only advance if there 046 // isn't a pending next(). 047 @Override 048 public boolean hasNext() { 049 if (next != null) { 050 return true; 051 } 052 try { 053 return (next = ResultScanner.this.next()) != null; 054 } catch (IOException e) { 055 throw new UncheckedIOException(e); 056 } 057 } 058 059 // get the pending next item and advance the iterator. returns null if 060 // there is no next item. 061 @Override 062 public Result next() { 063 // since hasNext() does the real advancing, we call this to determine 064 // if there is a next before proceeding. 065 if (!hasNext()) { 066 return null; 067 } 068 069 // if we get to here, then hasNext() has given us an item to return. 070 // we want to return the item and then null out the next pointer, so 071 // we use a temporary variable. 072 Result temp = next; 073 next = null; 074 return temp; 075 } 076 }; 077 } 078 079 /** 080 * Grab the next row's worth of values. The scanner will return a Result. 081 * @return Result object if there is another row, null if the scanner is exhausted. 082 * @throws IOException e 083 */ 084 Result next() throws IOException; 085 086 /** 087 * Get nbRows rows. How many RPCs are made is determined by the {@link Scan#setCaching(int)} 088 * setting (or hbase.client.scanner.caching in hbase-site.xml). 089 * @param nbRows number of rows to return 090 * @return Between zero and nbRows rowResults. Scan is done if returned array is of zero-length 091 * (We never return null). 092 * @throws IOException 093 */ 094 default Result[] next(int nbRows) throws IOException { 095 List<Result> resultSets = new ArrayList<>(nbRows); 096 for (int i = 0; i < nbRows; i++) { 097 Result next = next(); 098 if (next != null) { 099 resultSets.add(next); 100 } else { 101 break; 102 } 103 } 104 return resultSets.toArray(new Result[0]); 105 } 106 107 /** 108 * Closes the scanner and releases any resources it has allocated 109 */ 110 @Override 111 void close(); 112 113 /** 114 * Allow the client to renew the scanner's lease on the server. 115 * @return true if the lease was successfully renewed, false otherwise. 116 */ 117 boolean renewLease(); 118 119 /** 120 * @return the scan metrics, or {@code null} if we do not enable metrics. 121 */ 122 ScanMetrics getScanMetrics(); 123}