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.nio.BufferOverflowException;
24 import java.nio.ByteBuffer;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Comparator;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.NavigableMap;
31 import java.util.TreeMap;
32
33 import org.apache.hadoop.hbase.Cell;
34 import org.apache.hadoop.hbase.CellScannable;
35 import org.apache.hadoop.hbase.CellScanner;
36 import org.apache.hadoop.hbase.CellUtil;
37 import org.apache.hadoop.hbase.HConstants;
38 import org.apache.hadoop.hbase.KeyValue;
39 import org.apache.hadoop.hbase.KeyValueUtil;
40 import org.apache.hadoop.hbase.classification.InterfaceAudience;
41 import org.apache.hadoop.hbase.classification.InterfaceStability;
42 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
43 import org.apache.hadoop.hbase.util.Bytes;
44
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 @InterfaceAudience.Public
79 @InterfaceStability.Stable
80 public class Result implements CellScannable, CellScanner {
81 private Cell[] cells;
82 private Boolean exists;
83 private boolean stale = false;
84
85
86
87
88
89
90
91
92
93
94 private boolean partial = false;
95
96
97 private transient byte [] row = null;
98
99 private transient NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> familyMap = null;
100
101 private static ThreadLocal<byte[]> localBuffer = new ThreadLocal<byte[]>();
102 private static final int PAD_WIDTH = 128;
103 public static final Result EMPTY_RESULT = new Result(true);
104
105 private final static int INITIAL_CELLSCANNER_INDEX = -1;
106
107
108
109
110 private int cellScannerIndex = INITIAL_CELLSCANNER_INDEX;
111 private ClientProtos.RegionLoadStats stats;
112
113 private final boolean readonly;
114
115
116
117
118
119
120
121 public Result() {
122 this(false);
123 }
124
125
126
127
128
129
130 private Result(boolean readonly) {
131 this.readonly = readonly;
132 }
133
134
135
136
137 @Deprecated
138 public Result(KeyValue [] cells) {
139 this(cells, null, false, false);
140 }
141
142
143
144
145 @Deprecated
146 public Result(List<KeyValue> kvs) {
147
148 this(kvs.toArray(new Cell[kvs.size()]), null, false, false);
149 }
150
151
152
153
154
155
156 public static Result create(List<Cell> cells) {
157 return create(cells, null);
158 }
159
160 public static Result create(List<Cell> cells, Boolean exists) {
161 return create(cells, exists, false);
162 }
163
164 public static Result create(List<Cell> cells, Boolean exists, boolean stale) {
165 return create(cells, exists, stale, false);
166 }
167
168 public static Result create(List<Cell> cells, Boolean exists, boolean stale, boolean partial) {
169 if (exists != null){
170 return new Result(null, exists, stale, partial);
171 }
172 return new Result(cells.toArray(new Cell[cells.size()]), null, stale, partial);
173 }
174
175
176
177
178
179
180 public static Result create(Cell[] cells) {
181 return create(cells, null, false);
182 }
183
184 public static Result create(Cell[] cells, Boolean exists, boolean stale) {
185 return create(cells, exists, stale, false);
186 }
187
188 public static Result create(Cell[] cells, Boolean exists, boolean stale, boolean partial) {
189 if (exists != null){
190 return new Result(null, exists, stale, partial);
191 }
192 return new Result(cells, null, stale, partial);
193 }
194
195
196 private Result(Cell[] cells, Boolean exists, boolean stale, boolean partial) {
197 this.cells = cells;
198 this.exists = exists;
199 this.stale = stale;
200 this.partial = partial;
201 this.readonly = false;
202 }
203
204
205
206
207
208
209 public byte [] getRow() {
210 if (this.row == null) {
211 this.row = this.cells == null || this.cells.length == 0? null: CellUtil.cloneRow(this.cells[0]);
212 }
213 return this.row;
214 }
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 public Cell[] rawCells() {
237 return cells;
238 }
239
240
241
242
243
244
245
246
247
248
249
250 @Deprecated
251 public KeyValue[] raw() {
252 KeyValue[] kvs = new KeyValue[cells.length];
253 for (int i = 0 ; i < kvs.length; i++) {
254 kvs[i] = KeyValueUtil.ensureKeyValue(cells[i]);
255 }
256 return kvs;
257 }
258
259
260
261
262
263
264
265
266 public List<Cell> listCells() {
267 return isEmpty()? null: Arrays.asList(rawCells());
268 }
269
270
271
272
273
274
275
276
277
278
279
280 @Deprecated
281 public List<KeyValue> list() {
282 return isEmpty() ? null : Arrays.asList(raw());
283 }
284
285
286
287
288 @Deprecated
289 public List<KeyValue> getColumn(byte [] family, byte [] qualifier) {
290 return KeyValueUtil.ensureKeyValues(getColumnCells(family, qualifier));
291 }
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308 public List<Cell> getColumnCells(byte [] family, byte [] qualifier) {
309 List<Cell> result = new ArrayList<Cell>();
310
311 Cell [] kvs = rawCells();
312
313 if (kvs == null || kvs.length == 0) {
314 return result;
315 }
316 int pos = binarySearch(kvs, family, qualifier);
317 if (pos == -1) {
318 return result;
319 }
320
321 for (int i = pos ; i < kvs.length ; i++ ) {
322 if (CellUtil.matchingColumn(kvs[i], family,qualifier)) {
323 result.add(kvs[i]);
324 } else {
325 break;
326 }
327 }
328
329 return result;
330 }
331
332 protected int binarySearch(final Cell [] kvs,
333 final byte [] family,
334 final byte [] qualifier) {
335 Cell searchTerm =
336 KeyValueUtil.createFirstOnRow(CellUtil.cloneRow(kvs[0]),
337 family, qualifier);
338
339
340 int pos = Arrays.binarySearch(kvs, searchTerm, KeyValue.COMPARATOR);
341
342 if (pos < 0) {
343 pos = (pos+1) * -1;
344
345 }
346 if (pos == kvs.length) {
347 return -1;
348 }
349 return pos;
350 }
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365 protected int binarySearch(final Cell [] kvs,
366 final byte [] family, final int foffset, final int flength,
367 final byte [] qualifier, final int qoffset, final int qlength) {
368
369 double keyValueSize = (double)
370 KeyValue.getKeyValueDataStructureSize(kvs[0].getRowLength(), flength, qlength, 0);
371
372 byte[] buffer = localBuffer.get();
373 if (buffer == null || keyValueSize > buffer.length) {
374
375 buffer = new byte[(int) Math.ceil(keyValueSize / PAD_WIDTH) * PAD_WIDTH];
376 localBuffer.set(buffer);
377 }
378
379 Cell searchTerm = KeyValueUtil.createFirstOnRow(buffer, 0,
380 kvs[0].getRowArray(), kvs[0].getRowOffset(), kvs[0].getRowLength(),
381 family, foffset, flength,
382 qualifier, qoffset, qlength);
383
384
385 int pos = Arrays.binarySearch(kvs, searchTerm, KeyValue.COMPARATOR);
386
387 if (pos < 0) {
388 pos = (pos+1) * -1;
389
390 }
391 if (pos == kvs.length) {
392 return -1;
393 }
394 return pos;
395 }
396
397
398
399
400 @Deprecated
401 public KeyValue getColumnLatest(byte [] family, byte [] qualifier) {
402 return KeyValueUtil.ensureKeyValue(getColumnLatestCell(family, qualifier));
403 }
404
405
406
407
408
409
410
411
412
413
414 public Cell getColumnLatestCell(byte [] family, byte [] qualifier) {
415 Cell [] kvs = rawCells();
416 if (kvs == null || kvs.length == 0) {
417 return null;
418 }
419 int pos = binarySearch(kvs, family, qualifier);
420 if (pos == -1) {
421 return null;
422 }
423 if (CellUtil.matchingColumn(kvs[pos], family, qualifier)) {
424 return kvs[pos];
425 }
426 return null;
427 }
428
429
430
431
432 @Deprecated
433 public KeyValue getColumnLatest(byte [] family, int foffset, int flength,
434 byte [] qualifier, int qoffset, int qlength) {
435 return KeyValueUtil.ensureKeyValue(
436 getColumnLatestCell(family, foffset, flength, qualifier, qoffset, qlength));
437 }
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452 public Cell getColumnLatestCell(byte [] family, int foffset, int flength,
453 byte [] qualifier, int qoffset, int qlength) {
454
455 Cell [] kvs = rawCells();
456 if (kvs == null || kvs.length == 0) {
457 return null;
458 }
459 int pos = binarySearch(kvs, family, foffset, flength, qualifier, qoffset, qlength);
460 if (pos == -1) {
461 return null;
462 }
463 if (CellUtil.matchingColumn(kvs[pos], family, foffset, flength, qualifier, qoffset, qlength)) {
464 return kvs[pos];
465 }
466 return null;
467 }
468
469
470
471
472
473
474
475
476
477
478 public byte[] getValue(byte [] family, byte [] qualifier) {
479 Cell kv = getColumnLatestCell(family, qualifier);
480 if (kv == null) {
481 return null;
482 }
483 return CellUtil.cloneValue(kv);
484 }
485
486
487
488
489
490
491
492
493
494 public ByteBuffer getValueAsByteBuffer(byte [] family, byte [] qualifier) {
495
496 Cell kv = getColumnLatestCell(family, 0, family.length, qualifier, 0, qualifier.length);
497
498 if (kv == null) {
499 return null;
500 }
501 return ByteBuffer.wrap(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength()).
502 asReadOnlyBuffer();
503 }
504
505
506
507
508
509
510
511
512
513
514
515
516
517 public ByteBuffer getValueAsByteBuffer(byte [] family, int foffset, int flength,
518 byte [] qualifier, int qoffset, int qlength) {
519
520 Cell kv = getColumnLatestCell(family, foffset, flength, qualifier, qoffset, qlength);
521
522 if (kv == null) {
523 return null;
524 }
525 return ByteBuffer.wrap(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength()).
526 asReadOnlyBuffer();
527 }
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542 public boolean loadValue(byte [] family, byte [] qualifier, ByteBuffer dst)
543 throws BufferOverflowException {
544 return loadValue(family, 0, family.length, qualifier, 0, qualifier.length, dst);
545 }
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564 public boolean loadValue(byte [] family, int foffset, int flength,
565 byte [] qualifier, int qoffset, int qlength, ByteBuffer dst)
566 throws BufferOverflowException {
567 Cell kv = getColumnLatestCell(family, foffset, flength, qualifier, qoffset, qlength);
568
569 if (kv == null) {
570 return false;
571 }
572 dst.put(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength());
573 return true;
574 }
575
576
577
578
579
580
581
582
583
584 public boolean containsNonEmptyColumn(byte [] family, byte [] qualifier) {
585
586 return containsNonEmptyColumn(family, 0, family.length, qualifier, 0, qualifier.length);
587 }
588
589
590
591
592
593
594
595
596
597
598
599
600
601 public boolean containsNonEmptyColumn(byte [] family, int foffset, int flength,
602 byte [] qualifier, int qoffset, int qlength) {
603
604 Cell kv = getColumnLatestCell(family, foffset, flength, qualifier, qoffset, qlength);
605
606 return (kv != null) && (kv.getValueLength() > 0);
607 }
608
609
610
611
612
613
614
615
616
617 public boolean containsEmptyColumn(byte [] family, byte [] qualifier) {
618
619 return containsEmptyColumn(family, 0, family.length, qualifier, 0, qualifier.length);
620 }
621
622
623
624
625
626
627
628
629
630
631
632
633
634 public boolean containsEmptyColumn(byte [] family, int foffset, int flength,
635 byte [] qualifier, int qoffset, int qlength) {
636 Cell kv = getColumnLatestCell(family, foffset, flength, qualifier, qoffset, qlength);
637
638 return (kv != null) && (kv.getValueLength() == 0);
639 }
640
641
642
643
644
645
646
647
648
649 public boolean containsColumn(byte [] family, byte [] qualifier) {
650 Cell kv = getColumnLatestCell(family, qualifier);
651 return kv != null;
652 }
653
654
655
656
657
658
659
660
661
662
663
664
665
666 public boolean containsColumn(byte [] family, int foffset, int flength,
667 byte [] qualifier, int qoffset, int qlength) {
668
669 return getColumnLatestCell(family, foffset, flength, qualifier, qoffset, qlength) != null;
670 }
671
672
673
674
675
676
677
678
679
680
681 public NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> getMap() {
682 if (this.familyMap != null) {
683 return this.familyMap;
684 }
685 if(isEmpty()) {
686 return null;
687 }
688 this.familyMap = new TreeMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>>(Bytes.BYTES_COMPARATOR);
689 for(Cell kv : this.cells) {
690 byte [] family = CellUtil.cloneFamily(kv);
691 NavigableMap<byte[], NavigableMap<Long, byte[]>> columnMap =
692 familyMap.get(family);
693 if(columnMap == null) {
694 columnMap = new TreeMap<byte[], NavigableMap<Long, byte[]>>
695 (Bytes.BYTES_COMPARATOR);
696 familyMap.put(family, columnMap);
697 }
698 byte [] qualifier = CellUtil.cloneQualifier(kv);
699 NavigableMap<Long, byte[]> versionMap = columnMap.get(qualifier);
700 if(versionMap == null) {
701 versionMap = new TreeMap<Long, byte[]>(new Comparator<Long>() {
702 @Override
703 public int compare(Long l1, Long l2) {
704 return l2.compareTo(l1);
705 }
706 });
707 columnMap.put(qualifier, versionMap);
708 }
709 Long timestamp = kv.getTimestamp();
710 byte [] value = CellUtil.cloneValue(kv);
711
712 versionMap.put(timestamp, value);
713 }
714 return this.familyMap;
715 }
716
717
718
719
720
721
722
723
724
725 public NavigableMap<byte[], NavigableMap<byte[], byte[]>> getNoVersionMap() {
726 if(this.familyMap == null) {
727 getMap();
728 }
729 if(isEmpty()) {
730 return null;
731 }
732 NavigableMap<byte[], NavigableMap<byte[], byte[]>> returnMap =
733 new TreeMap<byte[], NavigableMap<byte[], byte[]>>(Bytes.BYTES_COMPARATOR);
734 for(Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>>
735 familyEntry : familyMap.entrySet()) {
736 NavigableMap<byte[], byte[]> qualifierMap =
737 new TreeMap<byte[], byte[]>(Bytes.BYTES_COMPARATOR);
738 for(Map.Entry<byte[], NavigableMap<Long, byte[]>> qualifierEntry :
739 familyEntry.getValue().entrySet()) {
740 byte [] value =
741 qualifierEntry.getValue().get(qualifierEntry.getValue().firstKey());
742 qualifierMap.put(qualifierEntry.getKey(), value);
743 }
744 returnMap.put(familyEntry.getKey(), qualifierMap);
745 }
746 return returnMap;
747 }
748
749
750
751
752
753
754
755
756 public NavigableMap<byte[], byte[]> getFamilyMap(byte [] family) {
757 if(this.familyMap == null) {
758 getMap();
759 }
760 if(isEmpty()) {
761 return null;
762 }
763 NavigableMap<byte[], byte[]> returnMap =
764 new TreeMap<byte[], byte[]>(Bytes.BYTES_COMPARATOR);
765 NavigableMap<byte[], NavigableMap<Long, byte[]>> qualifierMap =
766 familyMap.get(family);
767 if(qualifierMap == null) {
768 return returnMap;
769 }
770 for(Map.Entry<byte[], NavigableMap<Long, byte[]>> entry :
771 qualifierMap.entrySet()) {
772 byte [] value =
773 entry.getValue().get(entry.getValue().firstKey());
774 returnMap.put(entry.getKey(), value);
775 }
776 return returnMap;
777 }
778
779
780
781
782
783 public byte [] value() {
784 if (isEmpty()) {
785 return null;
786 }
787 return CellUtil.cloneValue(cells[0]);
788 }
789
790
791
792
793
794 public boolean isEmpty() {
795 return this.cells == null || this.cells.length == 0;
796 }
797
798
799
800
801 public int size() {
802 return this.cells == null? 0: this.cells.length;
803 }
804
805
806
807
808 @Override
809 public String toString() {
810 StringBuilder sb = new StringBuilder();
811 sb.append("keyvalues=");
812 if(isEmpty()) {
813 sb.append("NONE");
814 return sb.toString();
815 }
816 sb.append("{");
817 boolean moreThanOne = false;
818 for(Cell kv : this.cells) {
819 if(moreThanOne) {
820 sb.append(", ");
821 } else {
822 moreThanOne = true;
823 }
824 sb.append(kv.toString());
825 }
826 sb.append("}");
827 return sb.toString();
828 }
829
830
831
832
833
834
835
836 public static void compareResults(Result res1, Result res2)
837 throws Exception {
838 if (res2 == null) {
839 throw new Exception("There wasn't enough rows, we stopped at "
840 + Bytes.toStringBinary(res1.getRow()));
841 }
842 if (res1.size() != res2.size()) {
843 throw new Exception("This row doesn't have the same number of KVs: "
844 + res1.toString() + " compared to " + res2.toString());
845 }
846 Cell[] ourKVs = res1.rawCells();
847 Cell[] replicatedKVs = res2.rawCells();
848 for (int i = 0; i < res1.size(); i++) {
849 if (!ourKVs[i].equals(replicatedKVs[i]) ||
850 !Bytes.equals(CellUtil.cloneValue(ourKVs[i]), CellUtil.cloneValue(replicatedKVs[i]))) {
851 throw new Exception("This result was different: "
852 + res1.toString() + " compared to " + res2.toString());
853 }
854 }
855 }
856
857
858
859
860
861
862
863
864
865 public static Result createCompleteResult(List<Result> partialResults)
866 throws IOException {
867 List<Cell> cells = new ArrayList<Cell>();
868 boolean stale = false;
869 byte[] prevRow = null;
870 byte[] currentRow = null;
871
872 if (partialResults != null && !partialResults.isEmpty()) {
873 for (int i = 0; i < partialResults.size(); i++) {
874 Result r = partialResults.get(i);
875 currentRow = r.getRow();
876 if (prevRow != null && !Bytes.equals(prevRow, currentRow)) {
877 throw new IOException(
878 "Cannot form complete result. Rows of partial results do not match." +
879 " Partial Results: " + partialResults);
880 }
881
882
883
884
885
886
887
888
889
890
891
892
893 if (i != (partialResults.size() - 1) && !r.isPartial()) {
894 throw new IOException(
895 "Cannot form complete result. Result is missing partial flag. " +
896 "Partial Results: " + partialResults);
897 }
898 prevRow = currentRow;
899 stale = stale || r.isStale();
900 for (Cell c : r.rawCells()) {
901 cells.add(c);
902 }
903 }
904 }
905
906 return Result.create(cells, null, stale);
907 }
908
909
910
911
912
913
914 public static long getTotalSizeOfCells(Result result) {
915 long size = 0;
916 for (Cell c : result.rawCells()) {
917 size += CellUtil.estimatedHeapSizeOf(c);
918 }
919 return size;
920 }
921
922
923
924
925
926
927
928 public void copyFrom(Result other) {
929 checkReadonly();
930 this.row = null;
931 this.familyMap = null;
932 this.cells = other.cells;
933 }
934
935 @Override
936 public CellScanner cellScanner() {
937
938 this.cellScannerIndex = INITIAL_CELLSCANNER_INDEX;
939 return this;
940 }
941
942 @Override
943 public Cell current() {
944 if (cells == null) return null;
945 return (cellScannerIndex < 0)? null: this.cells[cellScannerIndex];
946 }
947
948 @Override
949 public boolean advance() {
950 if (cells == null) return false;
951 return ++cellScannerIndex < this.cells.length;
952 }
953
954 public Boolean getExists() {
955 return exists;
956 }
957
958 public void setExists(Boolean exists) {
959 checkReadonly();
960 this.exists = exists;
961 }
962
963
964
965
966
967
968 public boolean isStale() {
969 return stale;
970 }
971
972
973
974
975
976
977
978 public boolean isPartial() {
979 return partial;
980 }
981
982
983
984
985
986
987
988
989 @InterfaceAudience.Private
990 @Deprecated
991 public void addResults(ClientProtos.RegionLoadStats loadStats) {
992 checkReadonly();
993 this.stats = loadStats;
994 }
995
996
997
998
999
1000 @InterfaceAudience.Private
1001 public void setStatistics(ClientProtos.RegionLoadStats loadStats) {
1002 this.stats = loadStats;
1003 }
1004
1005
1006
1007
1008
1009 public ClientProtos.RegionLoadStats getStats() {
1010 return stats;
1011 }
1012
1013
1014
1015
1016
1017 private void checkReadonly() {
1018 if (readonly == true) {
1019 throw new UnsupportedOperationException("Attempting to modify readonly EMPTY_RESULT!");
1020 }
1021 }
1022 }