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