1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
96
97
98
99
100
101
102
103
104
105 private boolean allowPartialResults = false;
106
107 private int storeLimit = -1;
108 private int storeOffset = 0;
109 private boolean getScan;
110
111
112
113
114
115 @Deprecated
116 static public final String SCAN_ATTRIBUTES_METRICS_ENABLE = "scan.attributes.metrics.enable";
117
118
119
120
121
122 @Deprecated
123 static public final String SCAN_ATTRIBUTES_METRICS_DATA = "scan.attributes.metrics.data";
124
125
126
127
128 static public final String SCAN_ATTRIBUTES_TABLE_NAME = "scan.attributes.table.name";
129
130
131
132
133 private int caching = -1;
134 private long maxResultSize = -1;
135 private boolean cacheBlocks = true;
136 private boolean reversed = false;
137 private TimeRange tr = new TimeRange();
138 private Map<byte [], NavigableSet<byte []>> familyMap =
139 new TreeMap<byte [], NavigableSet<byte []>>(Bytes.BYTES_COMPARATOR);
140 private Boolean loadColumnFamiliesOnDemand = null;
141 private Boolean asyncPrefetch = null;
142
143
144
145
146
147
148
149 public static final String HBASE_CLIENT_SCANNER_ASYNC_PREFETCH =
150 "hbase.client.scanner.async.prefetch";
151
152
153
154
155 public static final boolean DEFAULT_HBASE_CLIENT_SCANNER_ASYNC_PREFETCH = false;
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 private boolean small = false;
176
177
178
179
180 public Scan() {}
181
182 public Scan(byte [] startRow, Filter filter) {
183 this(startRow);
184 this.filter = filter;
185 }
186
187
188
189
190
191
192
193
194 public Scan(byte [] startRow) {
195 this.startRow = startRow;
196 }
197
198
199
200
201
202
203 public Scan(byte [] startRow, byte [] stopRow) {
204 this.startRow = startRow;
205 this.stopRow = stopRow;
206
207 this.getScan = isStartRowAndEqualsStopRow();
208 }
209
210
211
212
213
214
215
216 public Scan(Scan scan) throws IOException {
217 startRow = scan.getStartRow();
218 stopRow = scan.getStopRow();
219 maxVersions = scan.getMaxVersions();
220 batch = scan.getBatch();
221 storeLimit = scan.getMaxResultsPerColumnFamily();
222 storeOffset = scan.getRowOffsetPerColumnFamily();
223 caching = scan.getCaching();
224 maxResultSize = scan.getMaxResultSize();
225 cacheBlocks = scan.getCacheBlocks();
226 getScan = scan.isGetScan();
227 filter = scan.getFilter();
228 loadColumnFamiliesOnDemand = scan.getLoadColumnFamiliesOnDemandValue();
229 consistency = scan.getConsistency();
230 reversed = scan.isReversed();
231 asyncPrefetch = scan.isAsyncPrefetch();
232 small = scan.isSmall();
233 allowPartialResults = scan.getAllowPartialResults();
234 TimeRange ctr = scan.getTimeRange();
235 tr = new TimeRange(ctr.getMin(), ctr.getMax());
236 Map<byte[], NavigableSet<byte[]>> fams = scan.getFamilyMap();
237 for (Map.Entry<byte[],NavigableSet<byte[]>> entry : fams.entrySet()) {
238 byte [] fam = entry.getKey();
239 NavigableSet<byte[]> cols = entry.getValue();
240 if (cols != null && cols.size() > 0) {
241 for (byte[] col : cols) {
242 addColumn(fam, col);
243 }
244 } else {
245 addFamily(fam);
246 }
247 }
248 for (Map.Entry<String, byte[]> attr : scan.getAttributesMap().entrySet()) {
249 setAttribute(attr.getKey(), attr.getValue());
250 }
251 }
252
253
254
255
256
257 public Scan(Get get) {
258 this.startRow = get.getRow();
259 this.stopRow = get.getRow();
260 this.filter = get.getFilter();
261 this.cacheBlocks = get.getCacheBlocks();
262 this.maxVersions = get.getMaxVersions();
263 this.storeLimit = get.getMaxResultsPerColumnFamily();
264 this.storeOffset = get.getRowOffsetPerColumnFamily();
265 this.tr = get.getTimeRange();
266 this.familyMap = get.getFamilyMap();
267 this.getScan = true;
268 this.asyncPrefetch = false;
269 this.consistency = get.getConsistency();
270 for (Map.Entry<String, byte[]> attr : get.getAttributesMap().entrySet()) {
271 setAttribute(attr.getKey(), attr.getValue());
272 }
273 }
274
275 public boolean isGetScan() {
276 return this.getScan || isStartRowAndEqualsStopRow();
277 }
278
279 private boolean isStartRowAndEqualsStopRow() {
280 return this.startRow != null && this.startRow.length > 0 &&
281 Bytes.equals(this.startRow, this.stopRow);
282 }
283
284
285
286
287
288
289
290 public Scan addFamily(byte [] family) {
291 familyMap.remove(family);
292 familyMap.put(family, null);
293 return this;
294 }
295
296
297
298
299
300
301
302
303
304 public Scan addColumn(byte [] family, byte [] qualifier) {
305 NavigableSet<byte []> set = familyMap.get(family);
306 if(set == null) {
307 set = new TreeSet<byte []>(Bytes.BYTES_COMPARATOR);
308 }
309 if (qualifier == null) {
310 qualifier = HConstants.EMPTY_BYTE_ARRAY;
311 }
312 set.add(qualifier);
313 familyMap.put(family, set);
314 return this;
315 }
316
317
318
319
320
321
322
323
324
325
326
327
328
329 public Scan setTimeRange(long minStamp, long maxStamp)
330 throws IOException {
331 tr = new TimeRange(minStamp, maxStamp);
332 return this;
333 }
334
335
336
337
338
339
340
341
342
343
344
345 public Scan setTimeStamp(long timestamp)
346 throws IOException {
347 try {
348 tr = new TimeRange(timestamp, timestamp+1);
349 } catch(IOException e) {
350
351 LOG.error("TimeRange failed, likely caused by integer overflow. ", e);
352 throw e;
353 }
354 return this;
355 }
356
357
358
359
360
361
362
363
364
365 public Scan setStartRow(byte [] startRow) {
366 this.startRow = startRow;
367 return this;
368 }
369
370
371
372
373
374
375
376
377
378
379
380
381 public Scan setStopRow(byte [] stopRow) {
382 this.stopRow = stopRow;
383 return this;
384 }
385
386
387
388
389
390
391
392
393
394
395
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425 private byte[] calculateTheClosestNextRowKeyForPrefix(byte[] rowKeyPrefix) {
426
427
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
438
439
440 return HConstants.EMPTY_END_ROW;
441 }
442
443
444 byte[] newStopRow = Arrays.copyOfRange(rowKeyPrefix, 0, offset);
445
446 newStopRow[newStopRow.length - 1]++;
447 return newStopRow;
448 }
449
450
451
452
453
454 public Scan setMaxVersions() {
455 this.maxVersions = Integer.MAX_VALUE;
456 return this;
457 }
458
459
460
461
462
463
464 public Scan setMaxVersions(int maxVersions) {
465 this.maxVersions = maxVersions;
466 return this;
467 }
468
469
470
471
472
473 public Scan setBatch(int batch) {
474 if (this.hasFilter() && this.filter.hasFilterRow()) {
475 throw new IncompatibleFilterException(
476 "Cannot set batch on a scan using a filter" +
477 " that returns true for filter.hasFilterRow");
478 }
479 this.batch = batch;
480 return this;
481 }
482
483
484
485
486
487 public Scan setMaxResultsPerColumnFamily(int limit) {
488 this.storeLimit = limit;
489 return this;
490 }
491
492
493
494
495
496 public Scan setRowOffsetPerColumnFamily(int offset) {
497 this.storeOffset = offset;
498 return this;
499 }
500
501
502
503
504
505
506
507
508 public Scan setCaching(int caching) {
509 this.caching = caching;
510 return this;
511 }
512
513
514
515
516 public long getMaxResultSize() {
517 return maxResultSize;
518 }
519
520
521
522
523
524
525
526
527 public Scan setMaxResultSize(long maxResultSize) {
528 this.maxResultSize = maxResultSize;
529 return this;
530 }
531
532 @Override
533 public Scan setFilter(Filter filter) {
534 super.setFilter(filter);
535 return this;
536 }
537
538
539
540
541
542
543 public Scan setFamilyMap(Map<byte [], NavigableSet<byte []>> familyMap) {
544 this.familyMap = familyMap;
545 return this;
546 }
547
548
549
550
551
552 public Map<byte [], NavigableSet<byte []>> getFamilyMap() {
553 return this.familyMap;
554 }
555
556
557
558
559 public int numFamilies() {
560 if(hasFamilies()) {
561 return this.familyMap.size();
562 }
563 return 0;
564 }
565
566
567
568
569 public boolean hasFamilies() {
570 return !this.familyMap.isEmpty();
571 }
572
573
574
575
576 public byte[][] getFamilies() {
577 if(hasFamilies()) {
578 return this.familyMap.keySet().toArray(new byte[0][0]);
579 }
580 return null;
581 }
582
583
584
585
586 public byte [] getStartRow() {
587 return this.startRow;
588 }
589
590
591
592
593 public byte [] getStopRow() {
594 return this.stopRow;
595 }
596
597
598
599
600 public int getMaxVersions() {
601 return this.maxVersions;
602 }
603
604
605
606
607 public int getBatch() {
608 return this.batch;
609 }
610
611
612
613
614 public int getMaxResultsPerColumnFamily() {
615 return this.storeLimit;
616 }
617
618
619
620
621
622
623 public int getRowOffsetPerColumnFamily() {
624 return this.storeOffset;
625 }
626
627
628
629
630 public int getCaching() {
631 return this.caching;
632 }
633
634
635
636
637 public TimeRange getTimeRange() {
638 return this.tr;
639 }
640
641
642
643
644 @Override
645 public Filter getFilter() {
646 return filter;
647 }
648
649
650
651
652 public boolean hasFilter() {
653 return filter != null;
654 }
655
656
657
658
659
660
661
662
663
664
665
666 public Scan setCacheBlocks(boolean cacheBlocks) {
667 this.cacheBlocks = cacheBlocks;
668 return this;
669 }
670
671
672
673
674
675
676 public boolean getCacheBlocks() {
677 return cacheBlocks;
678 }
679
680
681
682
683
684
685
686
687
688 public Scan setReversed(boolean reversed) {
689 this.reversed = reversed;
690 return this;
691 }
692
693
694
695
696
697 public boolean isReversed() {
698 return reversed;
699 }
700
701
702
703
704
705
706
707
708 public Scan setAllowPartialResults(final boolean allowPartialResults) {
709 this.allowPartialResults = allowPartialResults;
710 return this;
711 }
712
713
714
715
716
717
718 public boolean getAllowPartialResults() {
719 return allowPartialResults;
720 }
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738 public Scan setLoadColumnFamiliesOnDemand(boolean value) {
739 this.loadColumnFamiliesOnDemand = value;
740 return this;
741 }
742
743
744
745
746 public Boolean getLoadColumnFamiliesOnDemandValue() {
747 return this.loadColumnFamiliesOnDemand;
748 }
749
750
751
752
753 public boolean doLoadColumnFamiliesOnDemand() {
754 return (this.loadColumnFamiliesOnDemand != null)
755 && this.loadColumnFamiliesOnDemand.booleanValue();
756 }
757
758
759
760
761
762
763
764 @Override
765 public Map<String, Object> getFingerprint() {
766 Map<String, Object> map = new HashMap<String, Object>();
767 List<String> families = new ArrayList<String>();
768 if(this.familyMap.size() == 0) {
769 map.put("families", "ALL");
770 return map;
771 } else {
772 map.put("families", families);
773 }
774 for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
775 this.familyMap.entrySet()) {
776 families.add(Bytes.toStringBinary(entry.getKey()));
777 }
778 return map;
779 }
780
781
782
783
784
785
786
787
788 @Override
789 public Map<String, Object> toMap(int maxCols) {
790
791 Map<String, Object> map = getFingerprint();
792
793 Map<String, List<String>> familyColumns =
794 new HashMap<String, List<String>>();
795 map.put("families", familyColumns);
796
797 map.put("startRow", Bytes.toStringBinary(this.startRow));
798 map.put("stopRow", Bytes.toStringBinary(this.stopRow));
799 map.put("maxVersions", this.maxVersions);
800 map.put("batch", this.batch);
801 map.put("caching", this.caching);
802 map.put("maxResultSize", this.maxResultSize);
803 map.put("cacheBlocks", this.cacheBlocks);
804 map.put("loadColumnFamiliesOnDemand", this.loadColumnFamiliesOnDemand);
805 List<Long> timeRange = new ArrayList<Long>();
806 timeRange.add(this.tr.getMin());
807 timeRange.add(this.tr.getMax());
808 map.put("timeRange", timeRange);
809 int colCount = 0;
810
811 for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
812 this.familyMap.entrySet()) {
813 List<String> columns = new ArrayList<String>();
814 familyColumns.put(Bytes.toStringBinary(entry.getKey()), columns);
815 if(entry.getValue() == null) {
816 colCount++;
817 --maxCols;
818 columns.add("ALL");
819 } else {
820 colCount += entry.getValue().size();
821 if (maxCols <= 0) {
822 continue;
823 }
824 for (byte [] column : entry.getValue()) {
825 if (--maxCols <= 0) {
826 continue;
827 }
828 columns.add(Bytes.toStringBinary(column));
829 }
830 }
831 }
832 map.put("totalColumns", colCount);
833 if (this.filter != null) {
834 map.put("filter", this.filter.toString());
835 }
836
837 if (getId() != null) {
838 map.put("id", getId());
839 }
840 return map;
841 }
842
843
844
845
846
847
848
849
850
851
852
853 public Scan setRaw(boolean raw) {
854 setAttribute(RAW_ATTR, Bytes.toBytes(raw));
855 return this;
856 }
857
858
859
860
861 public boolean isRaw() {
862 byte[] attr = getAttribute(RAW_ATTR);
863 return attr == null ? false : Bytes.toBoolean(attr);
864 }
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888 public Scan setSmall(boolean small) {
889 this.small = small;
890 return this;
891 }
892
893
894
895
896
897 public boolean isSmall() {
898 return small;
899 }
900
901 @Override
902 public Scan setAttribute(String name, byte[] value) {
903 return (Scan) super.setAttribute(name, value);
904 }
905
906 @Override
907 public Scan setId(String id) {
908 return (Scan) super.setId(id);
909 }
910
911 @Override
912 public Scan setAuthorizations(Authorizations authorizations) {
913 return (Scan) super.setAuthorizations(authorizations);
914 }
915
916 @Override
917 public Scan setACL(Map<String, Permission> perms) {
918 return (Scan) super.setACL(perms);
919 }
920
921 @Override
922 public Scan setACL(String user, Permission perms) {
923 return (Scan) super.setACL(user, perms);
924 }
925
926 @Override
927 public Scan setConsistency(Consistency consistency) {
928 return (Scan) super.setConsistency(consistency);
929 }
930
931 @Override
932 public Scan setReplicaId(int Id) {
933 return (Scan) super.setReplicaId(Id);
934 }
935
936 @Override
937 public Scan setIsolationLevel(IsolationLevel level) {
938 return (Scan) super.setIsolationLevel(level);
939 }
940
941
942
943
944
945 public Scan setScanMetricsEnabled(final boolean enabled) {
946 setAttribute(Scan.SCAN_ATTRIBUTES_METRICS_ENABLE, Bytes.toBytes(Boolean.valueOf(enabled)));
947 return this;
948 }
949
950
951
952
953 public boolean isScanMetricsEnabled() {
954 byte[] attr = getAttribute(Scan.SCAN_ATTRIBUTES_METRICS_ENABLE);
955 return attr == null ? false : Bytes.toBoolean(attr);
956 }
957
958
959
960
961
962 public ScanMetrics getScanMetrics() {
963 byte [] bytes = getAttribute(Scan.SCAN_ATTRIBUTES_METRICS_DATA);
964 if (bytes == null) return null;
965 return ProtobufUtil.toScanMetrics(bytes);
966 }
967
968 public Boolean isAsyncPrefetch() {
969 return asyncPrefetch;
970 }
971
972 public Scan setAsyncPrefetch(boolean asyncPrefetch) {
973 this.asyncPrefetch = asyncPrefetch;
974 return this;
975 }
976 }