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