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
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
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 this.loadColumnFamiliesOnDemand = get.getLoadColumnFamiliesOnDemandValue();
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
287
288
289
290
291
292 public Scan addFamily(byte [] family) {
293 familyMap.remove(family);
294 familyMap.put(family, null);
295 return this;
296 }
297
298
299
300
301
302
303
304
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
321
322
323
324
325
326
327
328
329
330 public Scan setTimeRange(long minStamp, long maxStamp) 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(Exception e) {
350
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
363
364
365
366
367 public Scan setStartRow(byte [] startRow) {
368 this.startRow = startRow;
369 return this;
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
474
475
476
477
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
491
492
493 public Scan setMaxResultsPerColumnFamily(int limit) {
494 this.storeLimit = limit;
495 return this;
496 }
497
498
499
500
501
502 public Scan setRowOffsetPerColumnFamily(int offset) {
503 this.storeOffset = offset;
504 return this;
505 }
506
507
508
509
510
511
512
513
514 public Scan setCaching(int caching) {
515 this.caching = caching;
516 return this;
517 }
518
519
520
521
522 public long getMaxResultSize() {
523 return maxResultSize;
524 }
525
526
527
528
529
530
531
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
546
547
548
549 public Scan setFamilyMap(Map<byte [], NavigableSet<byte []>> familyMap) {
550 this.familyMap = familyMap;
551 return this;
552 }
553
554
555
556
557
558 public Map<byte [], NavigableSet<byte []>> getFamilyMap() {
559 return this.familyMap;
560 }
561
562
563
564
565 public int numFamilies() {
566 if(hasFamilies()) {
567 return this.familyMap.size();
568 }
569 return 0;
570 }
571
572
573
574
575 public boolean hasFamilies() {
576 return !this.familyMap.isEmpty();
577 }
578
579
580
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
591
592 public byte [] getStartRow() {
593 return this.startRow;
594 }
595
596
597
598
599 public byte [] getStopRow() {
600 return this.stopRow;
601 }
602
603
604
605
606 public int getMaxVersions() {
607 return this.maxVersions;
608 }
609
610
611
612
613 public int getBatch() {
614 return this.batch;
615 }
616
617
618
619
620 public int getMaxResultsPerColumnFamily() {
621 return this.storeLimit;
622 }
623
624
625
626
627
628
629 public int getRowOffsetPerColumnFamily() {
630 return this.storeOffset;
631 }
632
633
634
635
636 public int getCaching() {
637 return this.caching;
638 }
639
640
641
642
643 public TimeRange getTimeRange() {
644 return this.tr;
645 }
646
647
648
649
650 @Override
651 public Filter getFilter() {
652 return filter;
653 }
654
655
656
657
658 public boolean hasFilter() {
659 return filter != null;
660 }
661
662
663
664
665
666
667
668
669
670
671
672 public Scan setCacheBlocks(boolean cacheBlocks) {
673 this.cacheBlocks = cacheBlocks;
674 return this;
675 }
676
677
678
679
680
681
682 public boolean getCacheBlocks() {
683 return cacheBlocks;
684 }
685
686
687
688
689
690
691
692
693
694 public Scan setReversed(boolean reversed) {
695 this.reversed = reversed;
696 return this;
697 }
698
699
700
701
702
703 public boolean isReversed() {
704 return reversed;
705 }
706
707
708
709
710
711
712
713
714 public Scan setAllowPartialResults(final boolean allowPartialResults) {
715 this.allowPartialResults = allowPartialResults;
716 return this;
717 }
718
719
720
721
722
723
724 public boolean getAllowPartialResults() {
725 return allowPartialResults;
726 }
727
728 public Scan setLoadColumnFamiliesOnDemand(boolean value) {
729 return (Scan) super.setLoadColumnFamiliesOnDemand(value);
730 }
731
732
733
734
735
736
737
738 @Override
739 public Map<String, Object> getFingerprint() {
740 Map<String, Object> map = new HashMap<String, Object>();
741 List<String> families = new ArrayList<String>();
742 if(this.familyMap.size() == 0) {
743 map.put("families", "ALL");
744 return map;
745 } else {
746 map.put("families", families);
747 }
748 for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
749 this.familyMap.entrySet()) {
750 families.add(Bytes.toStringBinary(entry.getKey()));
751 }
752 return map;
753 }
754
755
756
757
758
759
760
761
762 @Override
763 public Map<String, Object> toMap(int maxCols) {
764
765 Map<String, Object> map = getFingerprint();
766
767 Map<String, List<String>> familyColumns =
768 new HashMap<String, List<String>>();
769 map.put("families", familyColumns);
770
771 map.put("startRow", Bytes.toStringBinary(this.startRow));
772 map.put("stopRow", Bytes.toStringBinary(this.stopRow));
773 map.put("maxVersions", this.maxVersions);
774 map.put("batch", this.batch);
775 map.put("caching", this.caching);
776 map.put("maxResultSize", this.maxResultSize);
777 map.put("cacheBlocks", this.cacheBlocks);
778 map.put("loadColumnFamiliesOnDemand", this.loadColumnFamiliesOnDemand);
779 List<Long> timeRange = new ArrayList<Long>();
780 timeRange.add(this.tr.getMin());
781 timeRange.add(this.tr.getMax());
782 map.put("timeRange", timeRange);
783 int colCount = 0;
784
785 for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
786 this.familyMap.entrySet()) {
787 List<String> columns = new ArrayList<String>();
788 familyColumns.put(Bytes.toStringBinary(entry.getKey()), columns);
789 if(entry.getValue() == null) {
790 colCount++;
791 --maxCols;
792 columns.add("ALL");
793 } else {
794 colCount += entry.getValue().size();
795 if (maxCols <= 0) {
796 continue;
797 }
798 for (byte [] column : entry.getValue()) {
799 if (--maxCols <= 0) {
800 continue;
801 }
802 columns.add(Bytes.toStringBinary(column));
803 }
804 }
805 }
806 map.put("totalColumns", colCount);
807 if (this.filter != null) {
808 map.put("filter", this.filter.toString());
809 }
810
811 if (getId() != null) {
812 map.put("id", getId());
813 }
814 return map;
815 }
816
817
818
819
820
821
822
823
824
825
826
827 public Scan setRaw(boolean raw) {
828 setAttribute(RAW_ATTR, Bytes.toBytes(raw));
829 return this;
830 }
831
832
833
834
835 public boolean isRaw() {
836 byte[] attr = getAttribute(RAW_ATTR);
837 return attr == null ? false : Bytes.toBoolean(attr);
838 }
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862 public Scan setSmall(boolean small) {
863 this.small = small;
864 return this;
865 }
866
867
868
869
870
871 public boolean isSmall() {
872 return small;
873 }
874
875 @Override
876 public Scan setAttribute(String name, byte[] value) {
877 return (Scan) super.setAttribute(name, value);
878 }
879
880 @Override
881 public Scan setId(String id) {
882 return (Scan) super.setId(id);
883 }
884
885 @Override
886 public Scan setAuthorizations(Authorizations authorizations) {
887 return (Scan) super.setAuthorizations(authorizations);
888 }
889
890 @Override
891 public Scan setACL(Map<String, Permission> perms) {
892 return (Scan) super.setACL(perms);
893 }
894
895 @Override
896 public Scan setACL(String user, Permission perms) {
897 return (Scan) super.setACL(user, perms);
898 }
899
900 @Override
901 public Scan setConsistency(Consistency consistency) {
902 return (Scan) super.setConsistency(consistency);
903 }
904
905 @Override
906 public Scan setReplicaId(int Id) {
907 return (Scan) super.setReplicaId(Id);
908 }
909
910 @Override
911 public Scan setIsolationLevel(IsolationLevel level) {
912 return (Scan) super.setIsolationLevel(level);
913 }
914
915
916
917
918
919
920
921
922
923 static Scan createGetClosestRowOrBeforeReverseScan(byte[] row) {
924
925
926 Scan scan = new Scan(row);
927 scan.setSmall(true);
928 scan.setReversed(true);
929 scan.setCaching(1);
930 return scan;
931 }
932
933
934
935
936
937 public Scan setScanMetricsEnabled(final boolean enabled) {
938 setAttribute(Scan.SCAN_ATTRIBUTES_METRICS_ENABLE, Bytes.toBytes(Boolean.valueOf(enabled)));
939 return this;
940 }
941
942
943
944
945 public boolean isScanMetricsEnabled() {
946 byte[] attr = getAttribute(Scan.SCAN_ATTRIBUTES_METRICS_ENABLE);
947 return attr == null ? false : Bytes.toBoolean(attr);
948 }
949
950
951
952
953
954 public ScanMetrics getScanMetrics() {
955 byte [] bytes = getAttribute(Scan.SCAN_ATTRIBUTES_METRICS_DATA);
956 if (bytes == null) return null;
957 return ProtobufUtil.toScanMetrics(bytes);
958 }
959 }