View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.client;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.NavigableSet;
29  import java.util.TreeMap;
30  import java.util.TreeSet;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.hadoop.hbase.HConstants;
35  import org.apache.hadoop.hbase.classification.InterfaceAudience;
36  import org.apache.hadoop.hbase.classification.InterfaceStability;
37  import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
38  import org.apache.hadoop.hbase.filter.Filter;
39  import org.apache.hadoop.hbase.filter.IncompatibleFilterException;
40  import org.apache.hadoop.hbase.io.TimeRange;
41  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
42  import org.apache.hadoop.hbase.security.access.Permission;
43  import org.apache.hadoop.hbase.security.visibility.Authorizations;
44  import org.apache.hadoop.hbase.util.Bytes;
45  
46  /**
47   * Used to perform Scan operations.
48   * <p>
49   * All operations are identical to {@link Get} with the exception of
50   * instantiation.  Rather than specifying a single row, an optional startRow
51   * and stopRow may be defined.  If rows are not specified, the Scanner will
52   * iterate over all rows.
53   * <p>
54   * To scan everything for each row, instantiate a Scan object.
55   * <p>
56   * To modify scanner caching for just this scan, use {@link #setCaching(int) setCaching}.
57   * If caching is NOT set, we will use the caching value of the hosting {@link Table}.
58   * In addition to row caching, it is possible to specify a
59   * maximum result size, using {@link #setMaxResultSize(long)}. When both are used,
60   * single server requests are limited by either number of rows or maximum result size, whichever
61   * limit comes first.
62   * <p>
63   * To further define the scope of what to get when scanning, perform additional
64   * methods as outlined below.
65   * <p>
66   * To get all columns from specific families, execute {@link #addFamily(byte[]) addFamily}
67   * for each family to retrieve.
68   * <p>
69   * To get specific columns, execute {@link #addColumn(byte[], byte[]) addColumn}
70   * for each column to retrieve.
71   * <p>
72   * To only retrieve columns within a specific range of version timestamps,
73   * execute {@link #setTimeRange(long, long) setTimeRange}.
74   * <p>
75   * To only retrieve columns with a specific timestamp, execute
76   * {@link #setTimeStamp(long) setTimestamp}.
77   * <p>
78   * To limit the number of versions of each column to be returned, execute
79   * {@link #setMaxVersions(int) setMaxVersions}.
80   * <p>
81   * To limit the maximum number of values returned for each call to next(),
82   * execute {@link #setBatch(int) setBatch}.
83   * <p>
84   * To add a filter, execute {@link #setFilter(org.apache.hadoop.hbase.filter.Filter) setFilter}.
85   * <p>
86   * Expert: To explicitly disable server-side block caching for this scan,
87   * execute {@link #setCacheBlocks(boolean)}.
88   */
89  @InterfaceAudience.Public
90  @InterfaceStability.Stable
91  public class Scan extends Query {
92    private static final Log LOG = LogFactory.getLog(Scan.class);
93  
94    private static final String RAW_ATTR = "_raw_";
95  
96    private byte [] startRow = HConstants.EMPTY_START_ROW;
97    private byte [] stopRow  = HConstants.EMPTY_END_ROW;
98    private int maxVersions = 1;
99    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   private boolean getScan;
117 
118   /**
119    * @deprecated since 1.0.0. Use {@link #setScanMetricsEnabled(boolean)}
120    */
121   // Make private or remove.
122   @Deprecated
123   static public final String SCAN_ATTRIBUTES_METRICS_ENABLE = "scan.attributes.metrics.enable";
124 
125   /**
126    * Use {@link #getScanMetrics()}
127    */
128   // Make this private or remove.
129   @Deprecated
130   static public final String SCAN_ATTRIBUTES_METRICS_DATA = "scan.attributes.metrics.data";
131 
132   // If an application wants to use multiple scans over different tables each scan must
133   // define this attribute with the appropriate table name by calling
134   // scan.setAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME, Bytes.toBytes(tableName))
135   static public final String SCAN_ATTRIBUTES_TABLE_NAME = "scan.attributes.table.name";
136 
137   /*
138    * -1 means no caching
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 = new TimeRange();
145   private Map<byte [], NavigableSet<byte []>> familyMap =
146     new TreeMap<byte [], NavigableSet<byte []>>(Bytes.BYTES_COMPARATOR);
147   private Boolean loadColumnFamiliesOnDemand = null;
148 
149   /**
150    * Set it true for small scan to get better performance
151    *
152    * Small scan should use pread and big scan can use seek + read
153    *
154    * seek + read is fast but can cause two problem (1) resource contention (2)
155    * cause too much network io
156    *
157    * [89-fb] Using pread for non-compaction read request
158    * https://issues.apache.org/jira/browse/HBASE-7266
159    *
160    * On the other hand, if setting it true, we would do
161    * openScanner,next,closeScanner in one RPC call. It means the better
162    * performance for small scan. [HBASE-9488].
163    *
164    * Generally, if the scan range is within one data block(64KB), it could be
165    * considered as a small scan.
166    */
167   private boolean small = false;
168 
169   /**
170    * Create a Scan operation across all rows.
171    */
172   public Scan() {}
173 
174   public Scan(byte [] startRow, Filter filter) {
175     this(startRow);
176     this.filter = filter;
177   }
178 
179   /**
180    * Create a Scan operation starting at the specified row.
181    * <p>
182    * If the specified row does not exist, the Scanner will start from the
183    * next closest row after the specified row.
184    * @param startRow row to start scanner at or after
185    */
186   public Scan(byte [] startRow) {
187     this.startRow = startRow;
188   }
189 
190   /**
191    * Create a Scan operation for the range of rows specified.
192    * @param startRow row to start scanner at or after (inclusive)
193    * @param stopRow row to stop scanner before (exclusive)
194    */
195   public Scan(byte [] startRow, byte [] stopRow) {
196     this.startRow = startRow;
197     this.stopRow = stopRow;
198     //if the startRow and stopRow both are empty, it is not a Get
199     this.getScan = isStartRowAndEqualsStopRow();
200   }
201 
202   /**
203    * Creates a new instance of this class while copying all values.
204    *
205    * @param scan  The scan instance to copy from.
206    * @throws IOException When copying the values fails.
207    */
208   public Scan(Scan scan) throws IOException {
209     startRow = scan.getStartRow();
210     stopRow  = scan.getStopRow();
211     maxVersions = scan.getMaxVersions();
212     batch = scan.getBatch();
213     storeLimit = scan.getMaxResultsPerColumnFamily();
214     storeOffset = scan.getRowOffsetPerColumnFamily();
215     caching = scan.getCaching();
216     maxResultSize = scan.getMaxResultSize();
217     cacheBlocks = scan.getCacheBlocks();
218     getScan = scan.isGetScan();
219     filter = scan.getFilter(); // clone?
220     loadColumnFamiliesOnDemand = scan.getLoadColumnFamiliesOnDemandValue();
221     consistency = scan.getConsistency();
222     reversed = scan.isReversed();
223     small = scan.isSmall();
224     TimeRange ctr = scan.getTimeRange();
225     tr = new TimeRange(ctr.getMin(), ctr.getMax());
226     Map<byte[], NavigableSet<byte[]>> fams = scan.getFamilyMap();
227     for (Map.Entry<byte[],NavigableSet<byte[]>> entry : fams.entrySet()) {
228       byte [] fam = entry.getKey();
229       NavigableSet<byte[]> cols = entry.getValue();
230       if (cols != null && cols.size() > 0) {
231         for (byte[] col : cols) {
232           addColumn(fam, col);
233         }
234       } else {
235         addFamily(fam);
236       }
237     }
238     for (Map.Entry<String, byte[]> attr : scan.getAttributesMap().entrySet()) {
239       setAttribute(attr.getKey(), attr.getValue());
240     }
241   }
242 
243   /**
244    * Builds a scan object with the same specs as get.
245    * @param get get to model scan after
246    */
247   public Scan(Get get) {
248     this.startRow = get.getRow();
249     this.stopRow = get.getRow();
250     this.filter = get.getFilter();
251     this.cacheBlocks = get.getCacheBlocks();
252     this.maxVersions = get.getMaxVersions();
253     this.storeLimit = get.getMaxResultsPerColumnFamily();
254     this.storeOffset = get.getRowOffsetPerColumnFamily();
255     this.tr = get.getTimeRange();
256     this.familyMap = get.getFamilyMap();
257     this.getScan = true;
258     this.consistency = get.getConsistency();
259     for (Map.Entry<String, byte[]> attr : get.getAttributesMap().entrySet()) {
260       setAttribute(attr.getKey(), attr.getValue());
261     }
262   }
263 
264   public boolean isGetScan() {
265     return this.getScan || isStartRowAndEqualsStopRow();
266   }
267 
268   private boolean isStartRowAndEqualsStopRow() {
269     return this.startRow != null && this.startRow.length > 0 &&
270         Bytes.equals(this.startRow, this.stopRow);
271   }
272   /**
273    * Get all columns from the specified family.
274    * <p>
275    * Overrides previous calls to addColumn for this family.
276    * @param family family name
277    * @return this
278    */
279   public Scan addFamily(byte [] family) {
280     familyMap.remove(family);
281     familyMap.put(family, null);
282     return this;
283   }
284 
285   /**
286    * Get the column from the specified family with the specified qualifier.
287    * <p>
288    * Overrides previous calls to addFamily for this family.
289    * @param family family name
290    * @param qualifier column qualifier
291    * @return this
292    */
293   public Scan addColumn(byte [] family, byte [] qualifier) {
294     NavigableSet<byte []> set = familyMap.get(family);
295     if(set == null) {
296       set = new TreeSet<byte []>(Bytes.BYTES_COMPARATOR);
297     }
298     if (qualifier == null) {
299       qualifier = HConstants.EMPTY_BYTE_ARRAY;
300     }
301     set.add(qualifier);
302     familyMap.put(family, set);
303     return this;
304   }
305 
306   /**
307    * Get versions of columns only within the specified timestamp range,
308    * [minStamp, maxStamp).  Note, default maximum versions to return is 1.  If
309    * your time range spans more than one version and you want all versions
310    * returned, up the number of versions beyond the default.
311    * @param minStamp minimum timestamp value, inclusive
312    * @param maxStamp maximum timestamp value, exclusive
313    * @throws IOException if invalid time range
314    * @see #setMaxVersions()
315    * @see #setMaxVersions(int)
316    * @return this
317    */
318   public Scan setTimeRange(long minStamp, long maxStamp)
319   throws IOException {
320     tr = new TimeRange(minStamp, maxStamp);
321     return this;
322   }
323 
324   /**
325    * Get versions of columns with the specified timestamp. Note, default maximum
326    * versions to return is 1.  If your time range spans more than one version
327    * and you want all versions returned, up the number of versions beyond the
328    * defaut.
329    * @param timestamp version timestamp
330    * @see #setMaxVersions()
331    * @see #setMaxVersions(int)
332    * @return this
333    */
334   public Scan setTimeStamp(long timestamp)
335   throws IOException {
336     try {
337       tr = new TimeRange(timestamp, timestamp+1);
338     } catch(IOException e) {
339       // This should never happen, unless integer overflow or something extremely wrong...
340       LOG.error("TimeRange failed, likely caused by integer overflow. ", e);
341       throw e;
342     }
343     return this;
344   }
345 
346   /**
347    * Set the start row of the scan.
348    * <p>
349    * If the specified row does not exist, the Scanner will start from the
350    * next closest row after the specified row.
351    * @param startRow row to start scanner at or after
352    * @return this
353    */
354   public Scan setStartRow(byte [] startRow) {
355     this.startRow = startRow;
356     return this;
357   }
358 
359   /**
360    * Set the stop row of the scan.
361    * @param stopRow row to end at (exclusive)
362    * <p>
363    * The scan will include rows that are lexicographically less than
364    * the provided stopRow.
365    * <p><b>Note:</b> When doing a filter for a rowKey <u>Prefix</u>
366    * use {@link #setRowPrefixFilter(byte[])}.
367    * The 'trailing 0' will not yield the desired result.</p>
368    * @return this
369    */
370   public Scan setStopRow(byte [] stopRow) {
371     this.stopRow = stopRow;
372     return this;
373   }
374 
375   /**
376    * <p>Set a filter (using stopRow and startRow) so the result set only contains rows where the
377    * rowKey starts with the specified prefix.</p>
378    * <p>This is a utility method that converts the desired rowPrefix into the appropriate values
379    * for the startRow and stopRow to achieve the desired result.</p>
380    * <p>This can safely be used in combination with setFilter.</p>
381    * <p><b>NOTE: Doing a {@link #setStartRow(byte[])} and/or {@link #setStopRow(byte[])}
382    * after this method will yield undefined results.</b></p>
383    * @param rowPrefix the prefix all rows must start with. (Set <i>null</i> to remove the filter.)
384    * @return this
385    */
386   public Scan setRowPrefixFilter(byte[] rowPrefix) {
387     if (rowPrefix == null) {
388       setStartRow(HConstants.EMPTY_START_ROW);
389       setStopRow(HConstants.EMPTY_END_ROW);
390     } else {
391       this.setStartRow(rowPrefix);
392       this.setStopRow(calculateTheClosestNextRowKeyForPrefix(rowPrefix));
393     }
394     return this;
395   }
396 
397   /**
398    * <p>When scanning for a prefix the scan should stop immediately after the the last row that
399    * has the specified prefix. This method calculates the closest next rowKey immediately following
400    * the given rowKeyPrefix.</p>
401    * <p><b>IMPORTANT: This converts a rowKey<u>Prefix</u> into a rowKey</b>.</p>
402    * <p>If the prefix is an 'ASCII' string put into a byte[] then this is easy because you can
403    * simply increment the last byte of the array.
404    * But if your application uses real binary rowids you may run into the scenario that your
405    * prefix is something like:</p>
406    * &nbsp;&nbsp;&nbsp;<b>{ 0x12, 0x23, 0xFF, 0xFF }</b><br/>
407    * Then this stopRow needs to be fed into the actual scan<br/>
408    * &nbsp;&nbsp;&nbsp;<b>{ 0x12, 0x24 }</b> (Notice that it is shorter now)<br/>
409    * This method calculates the correct stop row value for this usecase.
410    *
411    * @param rowKeyPrefix the rowKey<u>Prefix</u>.
412    * @return the closest next rowKey immediately following the given rowKeyPrefix.
413    */
414   private byte[] calculateTheClosestNextRowKeyForPrefix(byte[] rowKeyPrefix) {
415     // Essentially we are treating it like an 'unsigned very very long' and doing +1 manually.
416     // Search for the place where the trailing 0xFFs start
417     int offset = rowKeyPrefix.length;
418     while (offset > 0) {
419       if (rowKeyPrefix[offset - 1] != (byte) 0xFF) {
420         break;
421       }
422       offset--;
423     }
424 
425     if (offset == 0) {
426       // We got an 0xFFFF... (only FFs) stopRow value which is
427       // the last possible prefix before the end of the table.
428       // So set it to stop at the 'end of the table'
429       return HConstants.EMPTY_END_ROW;
430     }
431 
432     // Copy the right length of the original
433     byte[] newStopRow = Arrays.copyOfRange(rowKeyPrefix, 0, offset);
434     // And increment the last one
435     newStopRow[newStopRow.length - 1]++;
436     return newStopRow;
437   }
438 
439   /**
440    * Get all available versions.
441    * @return this
442    */
443   public Scan setMaxVersions() {
444     this.maxVersions = Integer.MAX_VALUE;
445     return this;
446   }
447 
448   /**
449    * Get up to the specified number of versions of each column.
450    * @param maxVersions maximum versions for each column
451    * @return this
452    */
453   public Scan setMaxVersions(int maxVersions) {
454     this.maxVersions = maxVersions;
455     return this;
456   }
457 
458   /**
459    * Set the maximum number of values to return for each call to next()
460    * @param batch the maximum number of values
461    */
462   public Scan setBatch(int batch) {
463     if (this.hasFilter() && this.filter.hasFilterRow()) {
464       throw new IncompatibleFilterException(
465         "Cannot set batch on a scan using a filter" +
466         " that returns true for filter.hasFilterRow");
467     }
468     this.batch = batch;
469     return this;
470   }
471 
472   /**
473    * Set the maximum number of values to return per row per Column Family
474    * @param limit the maximum number of values returned / row / CF
475    */
476   public Scan setMaxResultsPerColumnFamily(int limit) {
477     this.storeLimit = limit;
478     return this;
479   }
480 
481   /**
482    * Set offset for the row per Column Family.
483    * @param offset is the number of kvs that will be skipped.
484    */
485   public Scan setRowOffsetPerColumnFamily(int offset) {
486     this.storeOffset = offset;
487     return this;
488   }
489 
490   /**
491    * Set the number of rows for caching that will be passed to scanners.
492    * If not set, the Configuration setting {@link HConstants#HBASE_CLIENT_SCANNER_CACHING} will
493    * apply.
494    * Higher caching values will enable faster scanners but will use more memory.
495    * @param caching the number of rows for caching
496    */
497   public Scan setCaching(int caching) {
498     this.caching = caching;
499     return this;
500   }
501 
502   /**
503    * @return the maximum result size in bytes. See {@link #setMaxResultSize(long)}
504    */
505   public long getMaxResultSize() {
506     return maxResultSize;
507   }
508 
509   /**
510    * Set the maximum result size. The default is -1; this means that no specific
511    * maximum result size will be set for this scan, and the global configured
512    * value will be used instead. (Defaults to unlimited).
513    *
514    * @param maxResultSize The maximum result size in bytes.
515    */
516   public Scan setMaxResultSize(long maxResultSize) {
517     this.maxResultSize = maxResultSize;
518     return this;
519   }
520 
521   @Override
522   public Scan setFilter(Filter filter) {
523     super.setFilter(filter);
524     return this;
525   }
526 
527   /**
528    * Setting the familyMap
529    * @param familyMap map of family to qualifier
530    * @return this
531    */
532   public Scan setFamilyMap(Map<byte [], NavigableSet<byte []>> familyMap) {
533     this.familyMap = familyMap;
534     return this;
535   }
536 
537   /**
538    * Getting the familyMap
539    * @return familyMap
540    */
541   public Map<byte [], NavigableSet<byte []>> getFamilyMap() {
542     return this.familyMap;
543   }
544 
545   /**
546    * @return the number of families in familyMap
547    */
548   public int numFamilies() {
549     if(hasFamilies()) {
550       return this.familyMap.size();
551     }
552     return 0;
553   }
554 
555   /**
556    * @return true if familyMap is non empty, false otherwise
557    */
558   public boolean hasFamilies() {
559     return !this.familyMap.isEmpty();
560   }
561 
562   /**
563    * @return the keys of the familyMap
564    */
565   public byte[][] getFamilies() {
566     if(hasFamilies()) {
567       return this.familyMap.keySet().toArray(new byte[0][0]);
568     }
569     return null;
570   }
571 
572   /**
573    * @return the startrow
574    */
575   public byte [] getStartRow() {
576     return this.startRow;
577   }
578 
579   /**
580    * @return the stoprow
581    */
582   public byte [] getStopRow() {
583     return this.stopRow;
584   }
585 
586   /**
587    * @return the max number of versions to fetch
588    */
589   public int getMaxVersions() {
590     return this.maxVersions;
591   }
592 
593   /**
594    * @return maximum number of values to return for a single call to next()
595    */
596   public int getBatch() {
597     return this.batch;
598   }
599 
600   /**
601    * @return maximum number of values to return per row per CF
602    */
603   public int getMaxResultsPerColumnFamily() {
604     return this.storeLimit;
605   }
606 
607   /**
608    * Method for retrieving the scan's offset per row per column
609    * family (#kvs to be skipped)
610    * @return row offset
611    */
612   public int getRowOffsetPerColumnFamily() {
613     return this.storeOffset;
614   }
615 
616   /**
617    * @return caching the number of rows fetched when calling next on a scanner
618    */
619   public int getCaching() {
620     return this.caching;
621   }
622 
623   /**
624    * @return TimeRange
625    */
626   public TimeRange getTimeRange() {
627     return this.tr;
628   }
629 
630   /**
631    * @return RowFilter
632    */
633   @Override
634   public Filter getFilter() {
635     return filter;
636   }
637 
638   /**
639    * @return true is a filter has been specified, false if not
640    */
641   public boolean hasFilter() {
642     return filter != null;
643   }
644 
645   /**
646    * Set whether blocks should be cached for this Scan.
647    * <p>
648    * This is true by default.  When true, default settings of the table and
649    * family are used (this will never override caching blocks if the block
650    * cache is disabled for that family or entirely).
651    *
652    * @param cacheBlocks if false, default settings are overridden and blocks
653    * will not be cached
654    */
655   public Scan setCacheBlocks(boolean cacheBlocks) {
656     this.cacheBlocks = cacheBlocks;
657     return this;
658   }
659 
660   /**
661    * Get whether blocks should be cached for this Scan.
662    * @return true if default caching should be used, false if blocks should not
663    * be cached
664    */
665   public boolean getCacheBlocks() {
666     return cacheBlocks;
667   }
668 
669   /**
670    * Set whether this scan is a reversed one
671    * <p>
672    * This is false by default which means forward(normal) scan.
673    *
674    * @param reversed if true, scan will be backward order
675    * @return this
676    */
677   public Scan setReversed(boolean reversed) {
678     this.reversed = reversed;
679     return this;
680   }
681 
682   /**
683    * Get whether this scan is a reversed one.
684    * @return true if backward scan, false if forward(default) scan
685    */
686   public boolean isReversed() {
687     return reversed;
688   }
689 
690   /**
691    * Setting whether the caller wants to see the partial results that may be returned from the
692    * server. By default this value is false and the complete results will be assembled client side
693    * before being delivered to the caller.
694    * @param allowPartialResults
695    * @return this
696    */
697   public Scan setAllowPartialResults(final boolean allowPartialResults) {
698     this.allowPartialResults = allowPartialResults;
699     return this;
700   }
701 
702   /**
703    * @return true when the constructor of this scan understands that the results they will see may
704    *         only represent a partial portion of a row. The entire row would be retrieved by
705    *         subsequent calls to {@link ResultScanner#next()}
706    */
707   public boolean getAllowPartialResults() {
708     return allowPartialResults;
709   }
710 
711   /**
712    * Set the value indicating whether loading CFs on demand should be allowed (cluster
713    * default is false). On-demand CF loading doesn't load column families until necessary, e.g.
714    * if you filter on one column, the other column family data will be loaded only for the rows
715    * that are included in result, not all rows like in normal case.
716    * With column-specific filters, like SingleColumnValueFilter w/filterIfMissing == true,
717    * this can deliver huge perf gains when there's a cf with lots of data; however, it can
718    * also lead to some inconsistent results, as follows:
719    * - if someone does a concurrent update to both column families in question you may get a row
720    *   that never existed, e.g. for { rowKey = 5, { cat_videos => 1 }, { video => "my cat" } }
721    *   someone puts rowKey 5 with { cat_videos => 0 }, { video => "my dog" }, concurrent scan
722    *   filtering on "cat_videos == 1" can get { rowKey = 5, { cat_videos => 1 },
723    *   { video => "my dog" } }.
724    * - if there's a concurrent split and you have more than 2 column families, some rows may be
725    *   missing some column families.
726    */
727   public Scan setLoadColumnFamiliesOnDemand(boolean value) {
728     this.loadColumnFamiliesOnDemand = value;
729     return this;
730   }
731 
732   /**
733    * Get the raw loadColumnFamiliesOnDemand setting; if it's not set, can be null.
734    */
735   public Boolean getLoadColumnFamiliesOnDemandValue() {
736     return this.loadColumnFamiliesOnDemand;
737   }
738 
739   /**
740    * Get the logical value indicating whether on-demand CF loading should be allowed.
741    */
742   public boolean doLoadColumnFamiliesOnDemand() {
743     return (this.loadColumnFamiliesOnDemand != null)
744       && this.loadColumnFamiliesOnDemand.booleanValue();
745   }
746 
747   /**
748    * Compile the table and column family (i.e. schema) information
749    * into a String. Useful for parsing and aggregation by debugging,
750    * logging, and administration tools.
751    * @return Map
752    */
753   @Override
754   public Map<String, Object> getFingerprint() {
755     Map<String, Object> map = new HashMap<String, Object>();
756     List<String> families = new ArrayList<String>();
757     if(this.familyMap.size() == 0) {
758       map.put("families", "ALL");
759       return map;
760     } else {
761       map.put("families", families);
762     }
763     for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
764         this.familyMap.entrySet()) {
765       families.add(Bytes.toStringBinary(entry.getKey()));
766     }
767     return map;
768   }
769 
770   /**
771    * Compile the details beyond the scope of getFingerprint (row, columns,
772    * timestamps, etc.) into a Map along with the fingerprinted information.
773    * Useful for debugging, logging, and administration tools.
774    * @param maxCols a limit on the number of columns output prior to truncation
775    * @return Map
776    */
777   @Override
778   public Map<String, Object> toMap(int maxCols) {
779     // start with the fingerpring map and build on top of it
780     Map<String, Object> map = getFingerprint();
781     // map from families to column list replaces fingerprint's list of families
782     Map<String, List<String>> familyColumns =
783       new HashMap<String, List<String>>();
784     map.put("families", familyColumns);
785     // add scalar information first
786     map.put("startRow", Bytes.toStringBinary(this.startRow));
787     map.put("stopRow", Bytes.toStringBinary(this.stopRow));
788     map.put("maxVersions", this.maxVersions);
789     map.put("batch", this.batch);
790     map.put("caching", this.caching);
791     map.put("maxResultSize", this.maxResultSize);
792     map.put("cacheBlocks", this.cacheBlocks);
793     map.put("loadColumnFamiliesOnDemand", this.loadColumnFamiliesOnDemand);
794     List<Long> timeRange = new ArrayList<Long>();
795     timeRange.add(this.tr.getMin());
796     timeRange.add(this.tr.getMax());
797     map.put("timeRange", timeRange);
798     int colCount = 0;
799     // iterate through affected families and list out up to maxCols columns
800     for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
801       this.familyMap.entrySet()) {
802       List<String> columns = new ArrayList<String>();
803       familyColumns.put(Bytes.toStringBinary(entry.getKey()), columns);
804       if(entry.getValue() == null) {
805         colCount++;
806         --maxCols;
807         columns.add("ALL");
808       } else {
809         colCount += entry.getValue().size();
810         if (maxCols <= 0) {
811           continue;
812         }
813         for (byte [] column : entry.getValue()) {
814           if (--maxCols <= 0) {
815             continue;
816           }
817           columns.add(Bytes.toStringBinary(column));
818         }
819       }
820     }
821     map.put("totalColumns", colCount);
822     if (this.filter != null) {
823       map.put("filter", this.filter.toString());
824     }
825     // add the id if set
826     if (getId() != null) {
827       map.put("id", getId());
828     }
829     return map;
830   }
831 
832   /**
833    * Enable/disable "raw" mode for this scan.
834    * If "raw" is enabled the scan will return all
835    * delete marker and deleted rows that have not
836    * been collected, yet.
837    * This is mostly useful for Scan on column families
838    * that have KEEP_DELETED_ROWS enabled.
839    * It is an error to specify any column when "raw" is set.
840    * @param raw True/False to enable/disable "raw" mode.
841    */
842   public Scan setRaw(boolean raw) {
843     setAttribute(RAW_ATTR, Bytes.toBytes(raw));
844     return this;
845   }
846 
847   /**
848    * @return True if this Scan is in "raw" mode.
849    */
850   public boolean isRaw() {
851     byte[] attr = getAttribute(RAW_ATTR);
852     return attr == null ? false : Bytes.toBoolean(attr);
853   }
854 
855 
856 
857   /**
858    * Set whether this scan is a small scan
859    * <p>
860    * Small scan should use pread and big scan can use seek + read
861    *
862    * seek + read is fast but can cause two problem (1) resource contention (2)
863    * cause too much network io
864    *
865    * [89-fb] Using pread for non-compaction read request
866    * https://issues.apache.org/jira/browse/HBASE-7266
867    *
868    * On the other hand, if setting it true, we would do
869    * openScanner,next,closeScanner in one RPC call. It means the better
870    * performance for small scan. [HBASE-9488].
871    *
872    * Generally, if the scan range is within one data block(64KB), it could be
873    * considered as a small scan.
874    *
875    * @param small
876    */
877   public Scan setSmall(boolean small) {
878     this.small = small;
879     return this;
880   }
881 
882   /**
883    * Get whether this scan is a small scan
884    * @return true if small scan
885    */
886   public boolean isSmall() {
887     return small;
888   }
889 
890   @Override
891   public Scan setAttribute(String name, byte[] value) {
892     return (Scan) super.setAttribute(name, value);
893   }
894 
895   @Override
896   public Scan setId(String id) {
897     return (Scan) super.setId(id);
898   }
899 
900   @Override
901   public Scan setAuthorizations(Authorizations authorizations) {
902     return (Scan) super.setAuthorizations(authorizations);
903   }
904 
905   @Override
906   public Scan setACL(Map<String, Permission> perms) {
907     return (Scan) super.setACL(perms);
908   }
909 
910   @Override
911   public Scan setACL(String user, Permission perms) {
912     return (Scan) super.setACL(user, perms);
913   }
914 
915   @Override
916   public Scan setConsistency(Consistency consistency) {
917     return (Scan) super.setConsistency(consistency);
918   }
919 
920   @Override
921   public Scan setReplicaId(int Id) {
922     return (Scan) super.setReplicaId(Id);
923   }
924 
925   @Override
926   public Scan setIsolationLevel(IsolationLevel level) {
927     return (Scan) super.setIsolationLevel(level);
928   }
929 
930   /**
931    * Utility that creates a Scan that will do a  small scan in reverse from passed row
932    * looking for next closest row.
933    * @param row
934    * @param family
935    * @return An instance of Scan primed with passed <code>row</code> and <code>family</code> to
936    * scan in reverse for one row only.
937    */
938   static Scan createGetClosestRowOrBeforeReverseScan(byte[] row) {
939     // Below does not work if you add in family; need to add the family qualifier that is highest
940     // possible family qualifier.  Do we have such a notion?  Would have to be magic.
941     Scan scan = new Scan(row);
942     scan.setSmall(true);
943     scan.setReversed(true);
944     scan.setCaching(1);
945     return scan;
946   }
947 
948   /**
949    * Enable collection of {@link ScanMetrics}. For advanced users.
950    * @param enabled Set to true to enable accumulating scan metrics
951    */
952   public Scan setScanMetricsEnabled(final boolean enabled) {
953     setAttribute(Scan.SCAN_ATTRIBUTES_METRICS_ENABLE, Bytes.toBytes(Boolean.valueOf(enabled)));
954     return this;
955   }
956 
957   /**
958    * @return True if collection of scan metrics is enabled. For advanced users.
959    */
960   public boolean isScanMetricsEnabled() {
961     byte[] attr = getAttribute(Scan.SCAN_ATTRIBUTES_METRICS_ENABLE);
962     return attr == null ? false : Bytes.toBoolean(attr);
963   }
964 
965   /**
966    * @return Metrics on this Scan, if metrics were enabled.
967    * @see #setScanMetricsEnabled(boolean)
968    */
969   public ScanMetrics getScanMetrics() {
970     byte [] bytes = getAttribute(Scan.SCAN_ATTRIBUTES_METRICS_DATA);
971     if (bytes == null) return null;
972     return ProtobufUtil.toScanMetrics(bytes);
973   }
974 }