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 */
019
020package org.apache.hadoop.hbase.client;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028import java.util.NavigableSet;
029import java.util.TreeMap;
030import java.util.TreeSet;
031
032import org.apache.hadoop.hbase.HConstants;
033import org.apache.yetus.audience.InterfaceAudience;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
037import org.apache.hadoop.hbase.filter.Filter;
038import org.apache.hadoop.hbase.filter.IncompatibleFilterException;
039import org.apache.hadoop.hbase.io.TimeRange;
040import org.apache.hadoop.hbase.security.access.Permission;
041import org.apache.hadoop.hbase.security.visibility.Authorizations;
042import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
043import org.apache.hadoop.hbase.util.Bytes;
044
045/**
046 * Used to perform Scan operations.
047 * <p>
048 * All operations are identical to {@link Get} with the exception of instantiation. Rather than
049 * specifying a single row, an optional startRow and stopRow may be defined. If rows are not
050 * specified, the Scanner will iterate over all rows.
051 * <p>
052 * To get all columns from all rows of a Table, create an instance with no constraints; use the
053 * {@link #Scan()} constructor. To constrain the scan to specific column families, call
054 * {@link #addFamily(byte[]) addFamily} for each family to retrieve on your Scan instance.
055 * <p>
056 * To get specific columns, call {@link #addColumn(byte[], byte[]) addColumn} for each column to
057 * retrieve.
058 * <p>
059 * To only retrieve columns within a specific range of version timestamps, call
060 * {@link #setTimeRange(long, long) setTimeRange}.
061 * <p>
062 * To only retrieve columns with a specific timestamp, call {@link #setTimestamp(long) setTimestamp}
063 * .
064 * <p>
065 * To limit the number of versions of each column to be returned, call {@link #setMaxVersions(int)
066 * setMaxVersions}.
067 * <p>
068 * To limit the maximum number of values returned for each call to next(), call
069 * {@link #setBatch(int) setBatch}.
070 * <p>
071 * To add a filter, call {@link #setFilter(org.apache.hadoop.hbase.filter.Filter) setFilter}.
072 * <p>
073 * For small scan, it is deprecated in 2.0.0. Now we have a {@link #setLimit(int)} method in Scan
074 * object which is used to tell RS how many rows we want. If the rows return reaches the limit, the
075 * RS will close the RegionScanner automatically. And we will also fetch data when openScanner in
076 * the new implementation, this means we can also finish a scan operation in one rpc call. And we
077 * have also introduced a {@link #setReadType(ReadType)} method. You can use this method to tell RS
078 * to use pread explicitly.
079 * <p>
080 * Expert: To explicitly disable server-side block caching for this scan, execute
081 * {@link #setCacheBlocks(boolean)}.
082 * <p>
083 * <em>Note:</em> Usage alters Scan instances. Internally, attributes are updated as the Scan runs
084 * and if enabled, metrics accumulate in the Scan instance. Be aware this is the case when you go to
085 * clone a Scan instance or if you go to reuse a created Scan instance; safer is create a Scan
086 * instance per usage.
087 */
088@InterfaceAudience.Public
089public class Scan extends Query {
090  private static final Logger LOG = LoggerFactory.getLogger(Scan.class);
091
092  private static final String RAW_ATTR = "_raw_";
093
094  private byte[] startRow = HConstants.EMPTY_START_ROW;
095  private boolean includeStartRow = true;
096  private byte[] stopRow  = HConstants.EMPTY_END_ROW;
097  private boolean includeStopRow = false;
098  private int maxVersions = 1;
099  private int batch = -1;
100
101  /**
102   * Partial {@link Result}s are {@link Result}s must be combined to form a complete {@link Result}.
103   * The {@link Result}s had to be returned in fragments (i.e. as partials) because the size of the
104   * cells in the row exceeded max result size on the server. Typically partial results will be
105   * combined client side into complete results before being delivered to the caller. However, if
106   * this flag is set, the caller is indicating that they do not mind seeing partial results (i.e.
107   * they understand that the results returned from the Scanner may only represent part of a
108   * particular row). In such a case, any attempt to combine the partials into a complete result on
109   * the client side will be skipped, and the caller will be able to see the exact results returned
110   * from the server.
111   */
112  private boolean allowPartialResults = false;
113
114  private int storeLimit = -1;
115  private int storeOffset = 0;
116
117  /**
118   * @deprecated since 1.0.0. Use {@link #setScanMetricsEnabled(boolean)}
119   */
120  // Make private or remove.
121  @Deprecated
122  static public final String SCAN_ATTRIBUTES_METRICS_ENABLE = "scan.attributes.metrics.enable";
123
124  /**
125   * Use {@link #getScanMetrics()}
126   */
127  // Make this private or remove.
128  @Deprecated
129  static public final String SCAN_ATTRIBUTES_METRICS_DATA = "scan.attributes.metrics.data";
130
131  // If an application wants to use multiple scans over different tables each scan must
132  // define this attribute with the appropriate table name by calling
133  // scan.setAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME, Bytes.toBytes(tableName))
134  static public final String SCAN_ATTRIBUTES_TABLE_NAME = "scan.attributes.table.name";
135
136  /**
137   * -1 means no caching specified and the value of {@link HConstants#HBASE_CLIENT_SCANNER_CACHING}
138   * (default to {@link HConstants#DEFAULT_HBASE_CLIENT_SCANNER_CACHING}) will be used
139   */
140  private int caching = -1;
141  private long maxResultSize = -1;
142  private boolean cacheBlocks = true;
143  private boolean reversed = false;
144  private TimeRange tr = TimeRange.allTime();
145  private Map<byte [], NavigableSet<byte []>> familyMap =
146    new TreeMap<byte [], NavigableSet<byte []>>(Bytes.BYTES_COMPARATOR);
147  private Boolean asyncPrefetch = null;
148
149  /**
150   * Parameter name for client scanner sync/async prefetch toggle.
151   * When using async scanner, prefetching data from the server is done at the background.
152   * The parameter currently won't have any effect in the case that the user has set
153   * Scan#setSmall or Scan#setReversed
154   */
155  public static final String HBASE_CLIENT_SCANNER_ASYNC_PREFETCH =
156      "hbase.client.scanner.async.prefetch";
157
158  /**
159   * Default value of {@link #HBASE_CLIENT_SCANNER_ASYNC_PREFETCH}.
160   */
161  public static final boolean DEFAULT_HBASE_CLIENT_SCANNER_ASYNC_PREFETCH = false;
162
163  /**
164   * Set it true for small scan to get better performance Small scan should use pread and big scan
165   * can use seek + read seek + read is fast but can cause two problem (1) resource contention (2)
166   * cause too much network io [89-fb] Using pread for non-compaction read request
167   * https://issues.apache.org/jira/browse/HBASE-7266 On the other hand, if setting it true, we
168   * would do openScanner,next,closeScanner in one RPC call. It means the better performance for
169   * small scan. [HBASE-9488]. Generally, if the scan range is within one data block(64KB), it could
170   * be considered as a small scan.
171   */
172  private boolean small = false;
173
174  /**
175   * The mvcc read point to use when open a scanner. Remember to clear it after switching regions as
176   * the mvcc is only valid within region scope.
177   */
178  private long mvccReadPoint = -1L;
179
180  /**
181   * The number of rows we want for this scan. We will terminate the scan if the number of return
182   * rows reaches this value.
183   */
184  private int limit = -1;
185
186  /**
187   * Control whether to use pread at server side.
188   */
189  private ReadType readType = ReadType.DEFAULT;
190
191  private boolean needCursorResult = false;
192
193  /**
194   * Create a Scan operation across all rows.
195   */
196  public Scan() {}
197
198  /**
199   * @deprecated use {@code new Scan().withStartRow(startRow).setFilter(filter)} instead.
200   */
201  @Deprecated
202  public Scan(byte[] startRow, Filter filter) {
203    this(startRow);
204    this.filter = filter;
205  }
206
207  /**
208   * Create a Scan operation starting at the specified row.
209   * <p>
210   * If the specified row does not exist, the Scanner will start from the next closest row after the
211   * specified row.
212   * @param startRow row to start scanner at or after
213   * @deprecated use {@code new Scan().withStartRow(startRow)} instead.
214   */
215  @Deprecated
216  public Scan(byte[] startRow) {
217    setStartRow(startRow);
218  }
219
220  /**
221   * Create a Scan operation for the range of rows specified.
222   * @param startRow row to start scanner at or after (inclusive)
223   * @param stopRow row to stop scanner before (exclusive)
224   * @deprecated use {@code new Scan().withStartRow(startRow).withStopRow(stopRow)} instead.
225   */
226  @Deprecated
227  public Scan(byte[] startRow, byte[] stopRow) {
228    setStartRow(startRow);
229    setStopRow(stopRow);
230  }
231
232  /**
233   * Creates a new instance of this class while copying all values.
234   *
235   * @param scan  The scan instance to copy from.
236   * @throws IOException When copying the values fails.
237   */
238  public Scan(Scan scan) throws IOException {
239    startRow = scan.getStartRow();
240    includeStartRow = scan.includeStartRow();
241    stopRow  = scan.getStopRow();
242    includeStopRow = scan.includeStopRow();
243    maxVersions = scan.getMaxVersions();
244    batch = scan.getBatch();
245    storeLimit = scan.getMaxResultsPerColumnFamily();
246    storeOffset = scan.getRowOffsetPerColumnFamily();
247    caching = scan.getCaching();
248    maxResultSize = scan.getMaxResultSize();
249    cacheBlocks = scan.getCacheBlocks();
250    filter = scan.getFilter(); // clone?
251    loadColumnFamiliesOnDemand = scan.getLoadColumnFamiliesOnDemandValue();
252    consistency = scan.getConsistency();
253    this.setIsolationLevel(scan.getIsolationLevel());
254    reversed = scan.isReversed();
255    asyncPrefetch = scan.isAsyncPrefetch();
256    small = scan.isSmall();
257    allowPartialResults = scan.getAllowPartialResults();
258    tr = scan.getTimeRange(); // TimeRange is immutable
259    Map<byte[], NavigableSet<byte[]>> fams = scan.getFamilyMap();
260    for (Map.Entry<byte[],NavigableSet<byte[]>> entry : fams.entrySet()) {
261      byte [] fam = entry.getKey();
262      NavigableSet<byte[]> cols = entry.getValue();
263      if (cols != null && cols.size() > 0) {
264        for (byte[] col : cols) {
265          addColumn(fam, col);
266        }
267      } else {
268        addFamily(fam);
269      }
270    }
271    for (Map.Entry<String, byte[]> attr : scan.getAttributesMap().entrySet()) {
272      setAttribute(attr.getKey(), attr.getValue());
273    }
274    for (Map.Entry<byte[], TimeRange> entry : scan.getColumnFamilyTimeRange().entrySet()) {
275      TimeRange tr = entry.getValue();
276      setColumnFamilyTimeRange(entry.getKey(), tr.getMin(), tr.getMax());
277    }
278    this.mvccReadPoint = scan.getMvccReadPoint();
279    this.limit = scan.getLimit();
280    this.needCursorResult = scan.isNeedCursorResult();
281    setPriority(scan.getPriority());
282  }
283
284  /**
285   * Builds a scan object with the same specs as get.
286   * @param get get to model scan after
287   */
288  public Scan(Get get) {
289    this.startRow = get.getRow();
290    this.includeStartRow = true;
291    this.stopRow = get.getRow();
292    this.includeStopRow = true;
293    this.filter = get.getFilter();
294    this.cacheBlocks = get.getCacheBlocks();
295    this.maxVersions = get.getMaxVersions();
296    this.storeLimit = get.getMaxResultsPerColumnFamily();
297    this.storeOffset = get.getRowOffsetPerColumnFamily();
298    this.tr = get.getTimeRange();
299    this.familyMap = get.getFamilyMap();
300    this.asyncPrefetch = false;
301    this.consistency = get.getConsistency();
302    this.setIsolationLevel(get.getIsolationLevel());
303    this.loadColumnFamiliesOnDemand = get.getLoadColumnFamiliesOnDemandValue();
304    for (Map.Entry<String, byte[]> attr : get.getAttributesMap().entrySet()) {
305      setAttribute(attr.getKey(), attr.getValue());
306    }
307    for (Map.Entry<byte[], TimeRange> entry : get.getColumnFamilyTimeRange().entrySet()) {
308      TimeRange tr = entry.getValue();
309      setColumnFamilyTimeRange(entry.getKey(), tr.getMin(), tr.getMax());
310    }
311    this.mvccReadPoint = -1L;
312    setPriority(get.getPriority());
313  }
314
315  public boolean isGetScan() {
316    return includeStartRow && includeStopRow
317        && ClientUtil.areScanStartRowAndStopRowEqual(this.startRow, this.stopRow);
318  }
319
320  /**
321   * Get all columns from the specified family.
322   * <p>
323   * Overrides previous calls to addColumn for this family.
324   * @param family family name
325   * @return this
326   */
327  public Scan addFamily(byte [] family) {
328    familyMap.remove(family);
329    familyMap.put(family, null);
330    return this;
331  }
332
333  /**
334   * Get the column from the specified family with the specified qualifier.
335   * <p>
336   * Overrides previous calls to addFamily for this family.
337   * @param family family name
338   * @param qualifier column qualifier
339   * @return this
340   */
341  public Scan addColumn(byte [] family, byte [] qualifier) {
342    NavigableSet<byte []> set = familyMap.get(family);
343    if(set == null) {
344      set = new TreeSet<>(Bytes.BYTES_COMPARATOR);
345      familyMap.put(family, set);
346    }
347    if (qualifier == null) {
348      qualifier = HConstants.EMPTY_BYTE_ARRAY;
349    }
350    set.add(qualifier);
351    return this;
352  }
353
354  /**
355   * Get versions of columns only within the specified timestamp range,
356   * [minStamp, maxStamp).  Note, default maximum versions to return is 1.  If
357   * your time range spans more than one version and you want all versions
358   * returned, up the number of versions beyond the default.
359   * @param minStamp minimum timestamp value, inclusive
360   * @param maxStamp maximum timestamp value, exclusive
361   * @see #setMaxVersions()
362   * @see #setMaxVersions(int)
363   * @return this
364   */
365  public Scan setTimeRange(long minStamp, long maxStamp) throws IOException {
366    tr = new TimeRange(minStamp, maxStamp);
367    return this;
368  }
369
370  /**
371   * Get versions of columns with the specified timestamp. Note, default maximum
372   * versions to return is 1.  If your time range spans more than one version
373   * and you want all versions returned, up the number of versions beyond the
374   * defaut.
375   * @param timestamp version timestamp
376   * @see #setMaxVersions()
377   * @see #setMaxVersions(int)
378   * @return this
379   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0.
380   *             Use {@link #setTimestamp(long)} instead
381   */
382  @Deprecated
383  public Scan setTimeStamp(long timestamp)
384  throws IOException {
385    return this.setTimestamp(timestamp);
386  }
387
388  /**
389   * Get versions of columns with the specified timestamp. Note, default maximum
390   * versions to return is 1.  If your time range spans more than one version
391   * and you want all versions returned, up the number of versions beyond the
392   * defaut.
393   * @param timestamp version timestamp
394   * @see #setMaxVersions()
395   * @see #setMaxVersions(int)
396   * @return this
397   */
398  public Scan setTimestamp(long timestamp) {
399    try {
400      tr = new TimeRange(timestamp, timestamp + 1);
401    } catch(Exception e) {
402      // This should never happen, unless integer overflow or something extremely wrong...
403      LOG.error("TimeRange failed, likely caused by integer overflow. ", e);
404      throw e;
405    }
406
407    return this;
408  }
409
410  @Override public Scan setColumnFamilyTimeRange(byte[] cf, long minStamp, long maxStamp) {
411    return (Scan) super.setColumnFamilyTimeRange(cf, minStamp, maxStamp);
412  }
413
414  /**
415   * Set the start row of the scan.
416   * <p>
417   * If the specified row does not exist, the Scanner will start from the next closest row after the
418   * specified row.
419   * @param startRow row to start scanner at or after
420   * @return this
421   * @throws IllegalArgumentException if startRow does not meet criteria for a row key (when length
422   *           exceeds {@link HConstants#MAX_ROW_LENGTH})
423   * @deprecated use {@link #withStartRow(byte[])} instead. This method may change the inclusive of
424   *             the stop row to keep compatible with the old behavior.
425   */
426  @Deprecated
427  public Scan setStartRow(byte[] startRow) {
428    withStartRow(startRow);
429    if (ClientUtil.areScanStartRowAndStopRowEqual(this.startRow, this.stopRow)) {
430      // for keeping the old behavior that a scan with the same start and stop row is a get scan.
431      this.includeStopRow = true;
432    }
433    return this;
434  }
435
436  /**
437   * Set the start row of the scan.
438   * <p>
439   * If the specified row does not exist, the Scanner will start from the next closest row after the
440   * specified row.
441   * @param startRow row to start scanner at or after
442   * @return this
443   * @throws IllegalArgumentException if startRow does not meet criteria for a row key (when length
444   *           exceeds {@link HConstants#MAX_ROW_LENGTH})
445   */
446  public Scan withStartRow(byte[] startRow) {
447    return withStartRow(startRow, true);
448  }
449
450  /**
451   * Set the start row of the scan.
452   * <p>
453   * If the specified row does not exist, or the {@code inclusive} is {@code false}, the Scanner
454   * will start from the next closest row after the specified row.
455   * @param startRow row to start scanner at or after
456   * @param inclusive whether we should include the start row when scan
457   * @return this
458   * @throws IllegalArgumentException if startRow does not meet criteria for a row key (when length
459   *           exceeds {@link HConstants#MAX_ROW_LENGTH})
460   */
461  public Scan withStartRow(byte[] startRow, boolean inclusive) {
462    if (Bytes.len(startRow) > HConstants.MAX_ROW_LENGTH) {
463      throw new IllegalArgumentException("startRow's length must be less than or equal to "
464          + HConstants.MAX_ROW_LENGTH + " to meet the criteria" + " for a row key.");
465    }
466    this.startRow = startRow;
467    this.includeStartRow = inclusive;
468    return this;
469  }
470
471  /**
472   * Set the stop row of the scan.
473   * <p>
474   * The scan will include rows that are lexicographically less than the provided stopRow.
475   * <p>
476   * <b>Note:</b> When doing a filter for a rowKey <u>Prefix</u> use
477   * {@link #setRowPrefixFilter(byte[])}. The 'trailing 0' will not yield the desired result.
478   * </p>
479   * @param stopRow row to end at (exclusive)
480   * @return this
481   * @throws IllegalArgumentException if stopRow does not meet criteria for a row key (when length
482   *           exceeds {@link HConstants#MAX_ROW_LENGTH})
483   * @deprecated use {@link #withStartRow(byte[])} instead. This method may change the inclusive of
484   *             the stop row to keep compatible with the old behavior.
485   */
486  @Deprecated
487  public Scan setStopRow(byte[] stopRow) {
488    withStopRow(stopRow);
489    if (ClientUtil.areScanStartRowAndStopRowEqual(this.startRow, this.stopRow)) {
490      // for keeping the old behavior that a scan with the same start and stop row is a get scan.
491      this.includeStopRow = true;
492    }
493    return this;
494  }
495
496  /**
497   * Set the stop row of the scan.
498   * <p>
499   * The scan will include rows that are lexicographically less than the provided stopRow.
500   * <p>
501   * <b>Note:</b> When doing a filter for a rowKey <u>Prefix</u> use
502   * {@link #setRowPrefixFilter(byte[])}. The 'trailing 0' will not yield the desired result.
503   * </p>
504   * @param stopRow row to end at (exclusive)
505   * @return this
506   * @throws IllegalArgumentException if stopRow does not meet criteria for a row key (when length
507   *           exceeds {@link HConstants#MAX_ROW_LENGTH})
508   */
509  public Scan withStopRow(byte[] stopRow) {
510    return withStopRow(stopRow, false);
511  }
512
513  /**
514   * Set the stop row of the scan.
515   * <p>
516   * The scan will include rows that are lexicographically less than (or equal to if
517   * {@code inclusive} is {@code true}) the provided stopRow.
518   * @param stopRow row to end at
519   * @param inclusive whether we should include the stop row when scan
520   * @return this
521   * @throws IllegalArgumentException if stopRow does not meet criteria for a row key (when length
522   *           exceeds {@link HConstants#MAX_ROW_LENGTH})
523   */
524  public Scan withStopRow(byte[] stopRow, boolean inclusive) {
525    if (Bytes.len(stopRow) > HConstants.MAX_ROW_LENGTH) {
526      throw new IllegalArgumentException("stopRow's length must be less than or equal to "
527          + HConstants.MAX_ROW_LENGTH + " to meet the criteria" + " for a row key.");
528    }
529    this.stopRow = stopRow;
530    this.includeStopRow = inclusive;
531    return this;
532  }
533
534  /**
535   * <p>Set a filter (using stopRow and startRow) so the result set only contains rows where the
536   * rowKey starts with the specified prefix.</p>
537   * <p>This is a utility method that converts the desired rowPrefix into the appropriate values
538   * for the startRow and stopRow to achieve the desired result.</p>
539   * <p>This can safely be used in combination with setFilter.</p>
540   * <p><b>NOTE: Doing a {@link #setStartRow(byte[])} and/or {@link #setStopRow(byte[])}
541   * after this method will yield undefined results.</b></p>
542   * @param rowPrefix the prefix all rows must start with. (Set <i>null</i> to remove the filter.)
543   * @return this
544   */
545  public Scan setRowPrefixFilter(byte[] rowPrefix) {
546    if (rowPrefix == null) {
547      setStartRow(HConstants.EMPTY_START_ROW);
548      setStopRow(HConstants.EMPTY_END_ROW);
549    } else {
550      this.setStartRow(rowPrefix);
551      this.setStopRow(calculateTheClosestNextRowKeyForPrefix(rowPrefix));
552    }
553    return this;
554  }
555
556  /**
557   * <p>When scanning for a prefix the scan should stop immediately after the the last row that
558   * has the specified prefix. This method calculates the closest next rowKey immediately following
559   * the given rowKeyPrefix.</p>
560   * <p><b>IMPORTANT: This converts a rowKey<u>Prefix</u> into a rowKey</b>.</p>
561   * <p>If the prefix is an 'ASCII' string put into a byte[] then this is easy because you can
562   * simply increment the last byte of the array.
563   * But if your application uses real binary rowids you may run into the scenario that your
564   * prefix is something like:</p>
565   * &nbsp;&nbsp;&nbsp;<b>{ 0x12, 0x23, 0xFF, 0xFF }</b><br/>
566   * Then this stopRow needs to be fed into the actual scan<br/>
567   * &nbsp;&nbsp;&nbsp;<b>{ 0x12, 0x24 }</b> (Notice that it is shorter now)<br/>
568   * This method calculates the correct stop row value for this usecase.
569   *
570   * @param rowKeyPrefix the rowKey<u>Prefix</u>.
571   * @return the closest next rowKey immediately following the given rowKeyPrefix.
572   */
573  private byte[] calculateTheClosestNextRowKeyForPrefix(byte[] rowKeyPrefix) {
574    // Essentially we are treating it like an 'unsigned very very long' and doing +1 manually.
575    // Search for the place where the trailing 0xFFs start
576    int offset = rowKeyPrefix.length;
577    while (offset > 0) {
578      if (rowKeyPrefix[offset - 1] != (byte) 0xFF) {
579        break;
580      }
581      offset--;
582    }
583
584    if (offset == 0) {
585      // We got an 0xFFFF... (only FFs) stopRow value which is
586      // the last possible prefix before the end of the table.
587      // So set it to stop at the 'end of the table'
588      return HConstants.EMPTY_END_ROW;
589    }
590
591    // Copy the right length of the original
592    byte[] newStopRow = Arrays.copyOfRange(rowKeyPrefix, 0, offset);
593    // And increment the last one
594    newStopRow[newStopRow.length - 1]++;
595    return newStopRow;
596  }
597
598  /**
599   * Get all available versions.
600   * @return this
601   * @deprecated It is easy to misunderstand with column family's max versions, so use
602   *             {@link #readAllVersions()} instead.
603   */
604  @Deprecated
605  public Scan setMaxVersions() {
606    return readAllVersions();
607  }
608
609  /**
610   * Get up to the specified number of versions of each column.
611   * @param maxVersions maximum versions for each column
612   * @return this
613   * @deprecated It is easy to misunderstand with column family's max versions, so use
614   *             {@link #readVersions(int)} instead.
615   */
616  @Deprecated
617  public Scan setMaxVersions(int maxVersions) {
618    return readVersions(maxVersions);
619  }
620
621  /**
622   * Get all available versions.
623   * @return this
624   */
625  public Scan readAllVersions() {
626    this.maxVersions = Integer.MAX_VALUE;
627    return this;
628  }
629
630  /**
631   * Get up to the specified number of versions of each column.
632   * @param versions specified number of versions for each column
633   * @return this
634   */
635  public Scan readVersions(int versions) {
636    this.maxVersions = versions;
637    return this;
638  }
639
640  /**
641   * Set the maximum number of cells to return for each call to next(). Callers should be aware
642   * that this is not equivalent to calling {@link #setAllowPartialResults(boolean)}.
643   * If you don't allow partial results, the number of cells in each Result must equal to your
644   * batch setting unless it is the last Result for current row. So this method is helpful in paging
645   * queries. If you just want to prevent OOM at client, use setAllowPartialResults(true) is better.
646   * @param batch the maximum number of values
647   * @see Result#mayHaveMoreCellsInRow()
648   */
649  public Scan setBatch(int batch) {
650    if (this.hasFilter() && this.filter.hasFilterRow()) {
651      throw new IncompatibleFilterException(
652        "Cannot set batch on a scan using a filter" +
653        " that returns true for filter.hasFilterRow");
654    }
655    this.batch = batch;
656    return this;
657  }
658
659  /**
660   * Set the maximum number of values to return per row per Column Family
661   * @param limit the maximum number of values returned / row / CF
662   */
663  public Scan setMaxResultsPerColumnFamily(int limit) {
664    this.storeLimit = limit;
665    return this;
666  }
667
668  /**
669   * Set offset for the row per Column Family.
670   * @param offset is the number of kvs that will be skipped.
671   */
672  public Scan setRowOffsetPerColumnFamily(int offset) {
673    this.storeOffset = offset;
674    return this;
675  }
676
677  /**
678   * Set the number of rows for caching that will be passed to scanners.
679   * If not set, the Configuration setting {@link HConstants#HBASE_CLIENT_SCANNER_CACHING} will
680   * apply.
681   * Higher caching values will enable faster scanners but will use more memory.
682   * @param caching the number of rows for caching
683   */
684  public Scan setCaching(int caching) {
685    this.caching = caching;
686    return this;
687  }
688
689  /**
690   * @return the maximum result size in bytes. See {@link #setMaxResultSize(long)}
691   */
692  public long getMaxResultSize() {
693    return maxResultSize;
694  }
695
696  /**
697   * Set the maximum result size. The default is -1; this means that no specific
698   * maximum result size will be set for this scan, and the global configured
699   * value will be used instead. (Defaults to unlimited).
700   *
701   * @param maxResultSize The maximum result size in bytes.
702   */
703  public Scan setMaxResultSize(long maxResultSize) {
704    this.maxResultSize = maxResultSize;
705    return this;
706  }
707
708  @Override
709  public Scan setFilter(Filter filter) {
710    super.setFilter(filter);
711    return this;
712  }
713
714  /**
715   * Setting the familyMap
716   * @param familyMap map of family to qualifier
717   * @return this
718   */
719  public Scan setFamilyMap(Map<byte [], NavigableSet<byte []>> familyMap) {
720    this.familyMap = familyMap;
721    return this;
722  }
723
724  /**
725   * Getting the familyMap
726   * @return familyMap
727   */
728  public Map<byte [], NavigableSet<byte []>> getFamilyMap() {
729    return this.familyMap;
730  }
731
732  /**
733   * @return the number of families in familyMap
734   */
735  public int numFamilies() {
736    if(hasFamilies()) {
737      return this.familyMap.size();
738    }
739    return 0;
740  }
741
742  /**
743   * @return true if familyMap is non empty, false otherwise
744   */
745  public boolean hasFamilies() {
746    return !this.familyMap.isEmpty();
747  }
748
749  /**
750   * @return the keys of the familyMap
751   */
752  public byte[][] getFamilies() {
753    if(hasFamilies()) {
754      return this.familyMap.keySet().toArray(new byte[0][0]);
755    }
756    return null;
757  }
758
759  /**
760   * @return the startrow
761   */
762  public byte [] getStartRow() {
763    return this.startRow;
764  }
765
766  /**
767   * @return if we should include start row when scan
768   */
769  public boolean includeStartRow() {
770    return includeStartRow;
771  }
772
773  /**
774   * @return the stoprow
775   */
776  public byte[] getStopRow() {
777    return this.stopRow;
778  }
779
780  /**
781   * @return if we should include stop row when scan
782   */
783  public boolean includeStopRow() {
784    return includeStopRow;
785  }
786
787  /**
788   * @return the max number of versions to fetch
789   */
790  public int getMaxVersions() {
791    return this.maxVersions;
792  }
793
794  /**
795   * @return maximum number of values to return for a single call to next()
796   */
797  public int getBatch() {
798    return this.batch;
799  }
800
801  /**
802   * @return maximum number of values to return per row per CF
803   */
804  public int getMaxResultsPerColumnFamily() {
805    return this.storeLimit;
806  }
807
808  /**
809   * Method for retrieving the scan's offset per row per column
810   * family (#kvs to be skipped)
811   * @return row offset
812   */
813  public int getRowOffsetPerColumnFamily() {
814    return this.storeOffset;
815  }
816
817  /**
818   * @return caching the number of rows fetched when calling next on a scanner
819   */
820  public int getCaching() {
821    return this.caching;
822  }
823
824  /**
825   * @return TimeRange
826   */
827  public TimeRange getTimeRange() {
828    return this.tr;
829  }
830
831  /**
832   * @return RowFilter
833   */
834  @Override
835  public Filter getFilter() {
836    return filter;
837  }
838
839  /**
840   * @return true is a filter has been specified, false if not
841   */
842  public boolean hasFilter() {
843    return filter != null;
844  }
845
846  /**
847   * Set whether blocks should be cached for this Scan.
848   * <p>
849   * This is true by default.  When true, default settings of the table and
850   * family are used (this will never override caching blocks if the block
851   * cache is disabled for that family or entirely).
852   *
853   * @param cacheBlocks if false, default settings are overridden and blocks
854   * will not be cached
855   */
856  public Scan setCacheBlocks(boolean cacheBlocks) {
857    this.cacheBlocks = cacheBlocks;
858    return this;
859  }
860
861  /**
862   * Get whether blocks should be cached for this Scan.
863   * @return true if default caching should be used, false if blocks should not
864   * be cached
865   */
866  public boolean getCacheBlocks() {
867    return cacheBlocks;
868  }
869
870  /**
871   * Set whether this scan is a reversed one
872   * <p>
873   * This is false by default which means forward(normal) scan.
874   *
875   * @param reversed if true, scan will be backward order
876   * @return this
877   */
878  public Scan setReversed(boolean reversed) {
879    this.reversed = reversed;
880    return this;
881  }
882
883  /**
884   * Get whether this scan is a reversed one.
885   * @return true if backward scan, false if forward(default) scan
886   */
887  public boolean isReversed() {
888    return reversed;
889  }
890
891  /**
892   * Setting whether the caller wants to see the partial results when server returns
893   * less-than-expected cells. It is helpful while scanning a huge row to prevent OOM at client.
894   * By default this value is false and the complete results will be assembled client side
895   * before being delivered to the caller.
896   * @param allowPartialResults
897   * @return this
898   * @see Result#mayHaveMoreCellsInRow()
899   * @see #setBatch(int)
900   */
901  public Scan setAllowPartialResults(final boolean allowPartialResults) {
902    this.allowPartialResults = allowPartialResults;
903    return this;
904  }
905
906  /**
907   * @return true when the constructor of this scan understands that the results they will see may
908   *         only represent a partial portion of a row. The entire row would be retrieved by
909   *         subsequent calls to {@link ResultScanner#next()}
910   */
911  public boolean getAllowPartialResults() {
912    return allowPartialResults;
913  }
914
915  @Override
916  public Scan setLoadColumnFamiliesOnDemand(boolean value) {
917    return (Scan) super.setLoadColumnFamiliesOnDemand(value);
918  }
919
920  /**
921   * Compile the table and column family (i.e. schema) information
922   * into a String. Useful for parsing and aggregation by debugging,
923   * logging, and administration tools.
924   * @return Map
925   */
926  @Override
927  public Map<String, Object> getFingerprint() {
928    Map<String, Object> map = new HashMap<>();
929    List<String> families = new ArrayList<>();
930    if(this.familyMap.isEmpty()) {
931      map.put("families", "ALL");
932      return map;
933    } else {
934      map.put("families", families);
935    }
936    for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
937        this.familyMap.entrySet()) {
938      families.add(Bytes.toStringBinary(entry.getKey()));
939    }
940    return map;
941  }
942
943  /**
944   * Compile the details beyond the scope of getFingerprint (row, columns,
945   * timestamps, etc.) into a Map along with the fingerprinted information.
946   * Useful for debugging, logging, and administration tools.
947   * @param maxCols a limit on the number of columns output prior to truncation
948   * @return Map
949   */
950  @Override
951  public Map<String, Object> toMap(int maxCols) {
952    // start with the fingerpring map and build on top of it
953    Map<String, Object> map = getFingerprint();
954    // map from families to column list replaces fingerprint's list of families
955    Map<String, List<String>> familyColumns = new HashMap<>();
956    map.put("families", familyColumns);
957    // add scalar information first
958    map.put("startRow", Bytes.toStringBinary(this.startRow));
959    map.put("stopRow", Bytes.toStringBinary(this.stopRow));
960    map.put("maxVersions", this.maxVersions);
961    map.put("batch", this.batch);
962    map.put("caching", this.caching);
963    map.put("maxResultSize", this.maxResultSize);
964    map.put("cacheBlocks", this.cacheBlocks);
965    map.put("loadColumnFamiliesOnDemand", this.loadColumnFamiliesOnDemand);
966    List<Long> timeRange = new ArrayList<>(2);
967    timeRange.add(this.tr.getMin());
968    timeRange.add(this.tr.getMax());
969    map.put("timeRange", timeRange);
970    int colCount = 0;
971    // iterate through affected families and list out up to maxCols columns
972    for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
973      this.familyMap.entrySet()) {
974      List<String> columns = new ArrayList<>();
975      familyColumns.put(Bytes.toStringBinary(entry.getKey()), columns);
976      if(entry.getValue() == null) {
977        colCount++;
978        --maxCols;
979        columns.add("ALL");
980      } else {
981        colCount += entry.getValue().size();
982        if (maxCols <= 0) {
983          continue;
984        }
985        for (byte [] column : entry.getValue()) {
986          if (--maxCols <= 0) {
987            continue;
988          }
989          columns.add(Bytes.toStringBinary(column));
990        }
991      }
992    }
993    map.put("totalColumns", colCount);
994    if (this.filter != null) {
995      map.put("filter", this.filter.toString());
996    }
997    // add the id if set
998    if (getId() != null) {
999      map.put("id", getId());
1000    }
1001    return map;
1002  }
1003
1004  /**
1005   * Enable/disable "raw" mode for this scan.
1006   * If "raw" is enabled the scan will return all
1007   * delete marker and deleted rows that have not
1008   * been collected, yet.
1009   * This is mostly useful for Scan on column families
1010   * that have KEEP_DELETED_ROWS enabled.
1011   * It is an error to specify any column when "raw" is set.
1012   * @param raw True/False to enable/disable "raw" mode.
1013   */
1014  public Scan setRaw(boolean raw) {
1015    setAttribute(RAW_ATTR, Bytes.toBytes(raw));
1016    return this;
1017  }
1018
1019  /**
1020   * @return True if this Scan is in "raw" mode.
1021   */
1022  public boolean isRaw() {
1023    byte[] attr = getAttribute(RAW_ATTR);
1024    return attr == null ? false : Bytes.toBoolean(attr);
1025  }
1026
1027  /**
1028   * Set whether this scan is a small scan
1029   * <p>
1030   * Small scan should use pread and big scan can use seek + read seek + read is fast but can cause
1031   * two problem (1) resource contention (2) cause too much network io [89-fb] Using pread for
1032   * non-compaction read request https://issues.apache.org/jira/browse/HBASE-7266 On the other hand,
1033   * if setting it true, we would do openScanner,next,closeScanner in one RPC call. It means the
1034   * better performance for small scan. [HBASE-9488]. Generally, if the scan range is within one
1035   * data block(64KB), it could be considered as a small scan.
1036   * @param small
1037   * @deprecated since 2.0.0. Use {@link #setLimit(int)} and {@link #setReadType(ReadType)} instead.
1038   *             And for the one rpc optimization, now we will also fetch data when openScanner, and
1039   *             if the number of rows reaches the limit then we will close the scanner
1040   *             automatically which means we will fall back to one rpc.
1041   * @see #setLimit(int)
1042   * @see #setReadType(ReadType)
1043   */
1044  @Deprecated
1045  public Scan setSmall(boolean small) {
1046    this.small = small;
1047    this.readType = ReadType.PREAD;
1048    return this;
1049  }
1050
1051  /**
1052   * Get whether this scan is a small scan
1053   * @return true if small scan
1054   * @deprecated since 2.0.0. See the comment of {@link #setSmall(boolean)}
1055   */
1056  @Deprecated
1057  public boolean isSmall() {
1058    return small;
1059  }
1060
1061  @Override
1062  public Scan setAttribute(String name, byte[] value) {
1063    return (Scan) super.setAttribute(name, value);
1064  }
1065
1066  @Override
1067  public Scan setId(String id) {
1068    return (Scan) super.setId(id);
1069  }
1070
1071  @Override
1072  public Scan setAuthorizations(Authorizations authorizations) {
1073    return (Scan) super.setAuthorizations(authorizations);
1074  }
1075
1076  @Override
1077  public Scan setACL(Map<String, Permission> perms) {
1078    return (Scan) super.setACL(perms);
1079  }
1080
1081  @Override
1082  public Scan setACL(String user, Permission perms) {
1083    return (Scan) super.setACL(user, perms);
1084  }
1085
1086  @Override
1087  public Scan setConsistency(Consistency consistency) {
1088    return (Scan) super.setConsistency(consistency);
1089  }
1090
1091  @Override
1092  public Scan setReplicaId(int Id) {
1093    return (Scan) super.setReplicaId(Id);
1094  }
1095
1096  @Override
1097  public Scan setIsolationLevel(IsolationLevel level) {
1098    return (Scan) super.setIsolationLevel(level);
1099  }
1100
1101  @Override
1102  public Scan setPriority(int priority) {
1103    return (Scan) super.setPriority(priority);
1104  }
1105
1106  /**
1107   * Enable collection of {@link ScanMetrics}. For advanced users.
1108   * @param enabled Set to true to enable accumulating scan metrics
1109   */
1110  public Scan setScanMetricsEnabled(final boolean enabled) {
1111    setAttribute(Scan.SCAN_ATTRIBUTES_METRICS_ENABLE, Bytes.toBytes(Boolean.valueOf(enabled)));
1112    return this;
1113  }
1114
1115  /**
1116   * @return True if collection of scan metrics is enabled. For advanced users.
1117   */
1118  public boolean isScanMetricsEnabled() {
1119    byte[] attr = getAttribute(Scan.SCAN_ATTRIBUTES_METRICS_ENABLE);
1120    return attr == null ? false : Bytes.toBoolean(attr);
1121  }
1122
1123  /**
1124   * @return Metrics on this Scan, if metrics were enabled.
1125   * @see #setScanMetricsEnabled(boolean)
1126   * @deprecated Use {@link ResultScanner#getScanMetrics()} instead. And notice that, please do not
1127   *             use this method and {@link ResultScanner#getScanMetrics()} together, the metrics
1128   *             will be messed up.
1129   */
1130  @Deprecated
1131  public ScanMetrics getScanMetrics() {
1132    byte[] bytes = getAttribute(Scan.SCAN_ATTRIBUTES_METRICS_DATA);
1133    if (bytes == null) return null;
1134    return ProtobufUtil.toScanMetrics(bytes);
1135  }
1136
1137  public Boolean isAsyncPrefetch() {
1138    return asyncPrefetch;
1139  }
1140
1141  public Scan setAsyncPrefetch(boolean asyncPrefetch) {
1142    this.asyncPrefetch = asyncPrefetch;
1143    return this;
1144  }
1145
1146  /**
1147   * @return the limit of rows for this scan
1148   */
1149  public int getLimit() {
1150    return limit;
1151  }
1152
1153  /**
1154   * Set the limit of rows for this scan. We will terminate the scan if the number of returned rows
1155   * reaches this value.
1156   * <p>
1157   * This condition will be tested at last, after all other conditions such as stopRow, filter, etc.
1158   * @param limit the limit of rows for this scan
1159   * @return this
1160   */
1161  public Scan setLimit(int limit) {
1162    this.limit = limit;
1163    return this;
1164  }
1165
1166  /**
1167   * Call this when you only want to get one row. It will set {@code limit} to {@code 1}, and also
1168   * set {@code readType} to {@link ReadType#PREAD}.
1169   * @return this
1170   */
1171  public Scan setOneRowLimit() {
1172    return setLimit(1).setReadType(ReadType.PREAD);
1173  }
1174
1175  @InterfaceAudience.Public
1176  public enum ReadType {
1177    DEFAULT, STREAM, PREAD
1178  }
1179
1180  /**
1181   * @return the read type for this scan
1182   */
1183  public ReadType getReadType() {
1184    return readType;
1185  }
1186
1187  /**
1188   * Set the read type for this scan.
1189   * <p>
1190   * Notice that we may choose to use pread even if you specific {@link ReadType#STREAM} here. For
1191   * example, we will always use pread if this is a get scan.
1192   * @return this
1193   */
1194  public Scan setReadType(ReadType readType) {
1195    this.readType = readType;
1196    return this;
1197  }
1198
1199  /**
1200   * Get the mvcc read point used to open a scanner.
1201   */
1202  long getMvccReadPoint() {
1203    return mvccReadPoint;
1204  }
1205
1206  /**
1207   * Set the mvcc read point used to open a scanner.
1208   */
1209  Scan setMvccReadPoint(long mvccReadPoint) {
1210    this.mvccReadPoint = mvccReadPoint;
1211    return this;
1212  }
1213
1214  /**
1215   * Set the mvcc read point to -1 which means do not use it.
1216   */
1217  Scan resetMvccReadPoint() {
1218    return setMvccReadPoint(-1L);
1219  }
1220
1221  /**
1222   * When the server is slow or we scan a table with many deleted data or we use a sparse filter,
1223   * the server will response heartbeat to prevent timeout. However the scanner will return a Result
1224   * only when client can do it. So if there are many heartbeats, the blocking time on
1225   * ResultScanner#next() may be very long, which is not friendly to online services.
1226   *
1227   * Set this to true then you can get a special Result whose #isCursor() returns true and is not
1228   * contains any real data. It only tells you where the server has scanned. You can call next
1229   * to continue scanning or open a new scanner with this row key as start row whenever you want.
1230   *
1231   * Users can get a cursor when and only when there is a response from the server but we can not
1232   * return a Result to users, for example, this response is a heartbeat or there are partial cells
1233   * but users do not allow partial result.
1234   *
1235   * Now the cursor is in row level which means the special Result will only contains a row key.
1236   * {@link Result#isCursor()}
1237   * {@link Result#getCursor()}
1238   * {@link Cursor}
1239   */
1240  public Scan setNeedCursorResult(boolean needCursorResult) {
1241    this.needCursorResult = needCursorResult;
1242    return this;
1243  }
1244
1245  public boolean isNeedCursorResult() {
1246    return needCursorResult;
1247  }
1248
1249  /**
1250   * Create a new Scan with a cursor. It only set the position information like start row key.
1251   * The others (like cfs, stop row, limit) should still be filled in by the user.
1252   * {@link Result#isCursor()}
1253   * {@link Result#getCursor()}
1254   * {@link Cursor}
1255   */
1256  public static Scan createScanFromCursor(Cursor cursor) {
1257    return new Scan().withStartRow(cursor.getRow());
1258  }
1259}