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