1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.DataInput;
23 import java.io.DataInputStream;
24 import java.io.DataOutput;
25 import java.io.EOFException;
26 import java.io.IOException;
27 import java.io.SequenceInputStream;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31
32 import org.apache.hadoop.hbase.util.ByteStringer;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.hadoop.hbase.classification.InterfaceAudience;
36 import org.apache.hadoop.hbase.classification.InterfaceStability;
37 import org.apache.hadoop.hbase.KeyValue.KVComparator;
38 import org.apache.hadoop.hbase.client.RegionReplicaUtil;
39 import org.apache.hadoop.hbase.client.Result;
40 import org.apache.hadoop.hbase.exceptions.DeserializationException;
41 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
42 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
43 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionInfo;
44 import org.apache.hadoop.hbase.util.Bytes;
45 import org.apache.hadoop.hbase.util.JenkinsHash;
46 import org.apache.hadoop.hbase.util.MD5Hash;
47 import org.apache.hadoop.hbase.util.Pair;
48 import org.apache.hadoop.hbase.util.PairOfSameType;
49 import org.apache.hadoop.io.DataInputBuffer;
50
51 import com.google.protobuf.InvalidProtocolBufferException;
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.Evolving
81 public class HRegionInfo implements Comparable<HRegionInfo> {
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 @Deprecated
106 public static final byte VERSION = 1;
107 private static final Log LOG = LogFactory.getLog(HRegionInfo.class);
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 private static final int ENC_SEPARATOR = '.';
136 public static final int MD5_HEX_LENGTH = 32;
137
138
139 public static final String ENCODED_REGION_NAME_REGEX = "(?:[a-f0-9]+)";
140
141
142
143 public static final String REPLICA_ID_FORMAT = "%04X";
144
145 public static final byte REPLICA_ID_DELIMITER = (byte)'_';
146
147 private static final int MAX_REPLICA_ID = 0xFFFF;
148 public static final int DEFAULT_REPLICA_ID = 0;
149
150
151
152
153
154
155 private static boolean hasEncodedName(final byte[] regionName) {
156
157 if ((regionName.length >= 1)
158 && (regionName[regionName.length - 1] == ENC_SEPARATOR)) {
159
160 return true;
161 }
162 return false;
163 }
164
165
166
167
168
169 public static String encodeRegionName(final byte [] regionName) {
170 String encodedName;
171 if (hasEncodedName(regionName)) {
172
173
174 encodedName = Bytes.toString(regionName,
175 regionName.length - MD5_HEX_LENGTH - 1,
176 MD5_HEX_LENGTH);
177 } else {
178
179
180 int hashVal = Math.abs(JenkinsHash.getInstance().hash(regionName,
181 regionName.length, 0));
182 encodedName = String.valueOf(hashVal);
183 }
184 return encodedName;
185 }
186
187
188
189
190 public String getShortNameToLog() {
191 return prettyPrint(this.getEncodedName());
192 }
193
194
195
196
197
198
199
200 public static String prettyPrint(final String encodedRegionName) {
201 if (encodedRegionName.equals("1028785192")) {
202 return encodedRegionName + "/hbase:meta";
203 }
204 return encodedRegionName;
205 }
206
207 private byte [] endKey = HConstants.EMPTY_BYTE_ARRAY;
208
209
210
211 private boolean offLine = false;
212 private long regionId = -1;
213 private transient byte [] regionName = HConstants.EMPTY_BYTE_ARRAY;
214 private boolean split = false;
215 private byte [] startKey = HConstants.EMPTY_BYTE_ARRAY;
216 private int hashCode = -1;
217
218 public static final String NO_HASH = null;
219 private String encodedName = null;
220 private byte [] encodedNameAsBytes = null;
221 private int replicaId = DEFAULT_REPLICA_ID;
222
223
224 private TableName tableName = null;
225
226
227 public static final HRegionInfo FIRST_META_REGIONINFO =
228 new HRegionInfo(1L, TableName.META_TABLE_NAME);
229
230 private void setHashCode() {
231 int result = Arrays.hashCode(this.regionName);
232 result ^= this.regionId;
233 result ^= Arrays.hashCode(this.startKey);
234 result ^= Arrays.hashCode(this.endKey);
235 result ^= Boolean.valueOf(this.offLine).hashCode();
236 result ^= Arrays.hashCode(this.tableName.getName());
237 result ^= this.replicaId;
238 this.hashCode = result;
239 }
240
241
242
243
244
245
246 private HRegionInfo(long regionId, TableName tableName) {
247 this(regionId, tableName, DEFAULT_REPLICA_ID);
248 }
249
250 public HRegionInfo(long regionId, TableName tableName, int replicaId) {
251 super();
252 this.regionId = regionId;
253 this.tableName = tableName;
254 this.replicaId = replicaId;
255
256 this.regionName = createRegionName(tableName, null, regionId, replicaId, false);
257 setHashCode();
258 }
259
260
261
262
263
264
265
266 @Deprecated
267 public HRegionInfo() {
268 super();
269 }
270
271 public HRegionInfo(final TableName tableName) {
272 this(tableName, null, null);
273 }
274
275
276
277
278
279
280
281
282
283 public HRegionInfo(final TableName tableName, final byte[] startKey, final byte[] endKey)
284 throws IllegalArgumentException {
285 this(tableName, startKey, endKey, false);
286 }
287
288
289
290
291
292
293
294
295
296
297
298 public HRegionInfo(final TableName tableName, final byte[] startKey, final byte[] endKey,
299 final boolean split)
300 throws IllegalArgumentException {
301 this(tableName, startKey, endKey, split, System.currentTimeMillis());
302 }
303
304
305
306
307
308
309
310
311
312
313
314
315 public HRegionInfo(final TableName tableName, final byte[] startKey,
316 final byte[] endKey, final boolean split, final long regionid)
317 throws IllegalArgumentException {
318 this(tableName, startKey, endKey, split, regionid, DEFAULT_REPLICA_ID);
319 }
320
321
322
323
324
325
326
327
328
329
330
331
332
333 public HRegionInfo(final TableName tableName, final byte[] startKey,
334 final byte[] endKey, final boolean split, final long regionid,
335 final int replicaId)
336 throws IllegalArgumentException {
337 super();
338 if (tableName == null) {
339 throw new IllegalArgumentException("TableName cannot be null");
340 }
341 this.tableName = tableName;
342 this.offLine = false;
343 this.regionId = regionid;
344 this.replicaId = replicaId;
345 if (this.replicaId > MAX_REPLICA_ID) {
346 throw new IllegalArgumentException("ReplicaId cannot be greater than" + MAX_REPLICA_ID);
347 }
348
349 this.regionName = createRegionName(this.tableName, startKey, regionId, replicaId, true);
350
351 this.split = split;
352 this.endKey = endKey == null? HConstants.EMPTY_END_ROW: endKey.clone();
353 this.startKey = startKey == null?
354 HConstants.EMPTY_START_ROW: startKey.clone();
355 this.tableName = tableName;
356 setHashCode();
357 }
358
359
360
361
362
363
364 public HRegionInfo(HRegionInfo other) {
365 super();
366 this.endKey = other.getEndKey();
367 this.offLine = other.isOffline();
368 this.regionId = other.getRegionId();
369 this.regionName = other.getRegionName();
370 this.split = other.isSplit();
371 this.startKey = other.getStartKey();
372 this.hashCode = other.hashCode();
373 this.encodedName = other.getEncodedName();
374 this.tableName = other.tableName;
375 this.replicaId = other.replicaId;
376 }
377
378 public HRegionInfo(HRegionInfo other, int replicaId) {
379 this(other);
380 this.replicaId = replicaId;
381 this.setHashCode();
382 }
383
384
385
386
387
388
389
390
391
392
393 public static byte [] createRegionName(final TableName tableName,
394 final byte [] startKey, final long regionid, boolean newFormat) {
395 return createRegionName(tableName, startKey, Long.toString(regionid), newFormat);
396 }
397
398
399
400
401
402
403
404
405
406
407 public static byte [] createRegionName(final TableName tableName,
408 final byte [] startKey, final String id, boolean newFormat) {
409 return createRegionName(tableName, startKey, Bytes.toBytes(id), newFormat);
410 }
411
412
413
414
415
416
417
418
419
420
421
422 public static byte [] createRegionName(final TableName tableName,
423 final byte [] startKey, final long regionid, int replicaId, boolean newFormat) {
424 return createRegionName(tableName, startKey, Bytes.toBytes(Long.toString(regionid)),
425 replicaId, newFormat);
426 }
427
428
429
430
431
432
433
434
435
436
437 public static byte [] createRegionName(final TableName tableName,
438 final byte [] startKey, final byte [] id, boolean newFormat) {
439 return createRegionName(tableName, startKey, id, DEFAULT_REPLICA_ID, newFormat);
440 }
441
442
443
444
445
446
447
448
449
450 public static byte [] createRegionName(final TableName tableName,
451 final byte [] startKey, final byte [] id, final int replicaId, boolean newFormat) {
452 int len = tableName.getName().length + 2 + id.length +
453 (startKey == null? 0: startKey.length);
454 if (newFormat) {
455 len += MD5_HEX_LENGTH + 2;
456 }
457 byte[] replicaIdBytes = null;
458
459
460
461 if (replicaId > 0) {
462
463 replicaIdBytes = Bytes.toBytes(String.format(REPLICA_ID_FORMAT, replicaId));
464 len += 1 + replicaIdBytes.length;
465 }
466
467 byte [] b = new byte [len];
468
469 int offset = tableName.getName().length;
470 System.arraycopy(tableName.getName(), 0, b, 0, offset);
471 b[offset++] = HConstants.DELIMITER;
472 if (startKey != null && startKey.length > 0) {
473 System.arraycopy(startKey, 0, b, offset, startKey.length);
474 offset += startKey.length;
475 }
476 b[offset++] = HConstants.DELIMITER;
477 System.arraycopy(id, 0, b, offset, id.length);
478 offset += id.length;
479
480 if (replicaIdBytes != null) {
481 b[offset++] = REPLICA_ID_DELIMITER;
482 System.arraycopy(replicaIdBytes, 0, b, offset, replicaIdBytes.length);
483 offset += replicaIdBytes.length;
484 }
485
486 if (newFormat) {
487
488
489
490
491
492
493
494 String md5Hash = MD5Hash.getMD5AsHex(b, 0, offset);
495 byte [] md5HashBytes = Bytes.toBytes(md5Hash);
496
497 if (md5HashBytes.length != MD5_HEX_LENGTH) {
498 LOG.error("MD5-hash length mismatch: Expected=" + MD5_HEX_LENGTH +
499 "; Got=" + md5HashBytes.length);
500 }
501
502
503 b[offset++] = ENC_SEPARATOR;
504 System.arraycopy(md5HashBytes, 0, b, offset, MD5_HEX_LENGTH);
505 offset += MD5_HEX_LENGTH;
506 b[offset++] = ENC_SEPARATOR;
507 }
508
509 return b;
510 }
511
512
513
514
515
516
517
518
519
520 @Deprecated
521 public static byte [] getTableName(byte[] regionName) {
522 int offset = -1;
523 for (int i = 0; i < regionName.length; i++) {
524 if (regionName[i] == HConstants.DELIMITER) {
525 offset = i;
526 break;
527 }
528 }
529 byte[] buff = new byte[offset];
530 System.arraycopy(regionName, 0, buff, 0, offset);
531 return buff;
532 }
533
534
535
536
537
538
539
540
541
542 public static TableName getTable(final byte [] regionName) {
543 return TableName.valueOf(getTableName(regionName));
544 }
545
546
547
548
549
550
551 public static byte[] getStartKey(final byte[] regionName) throws IOException {
552 return parseRegionName(regionName)[1];
553 }
554
555
556
557
558
559
560
561 public static byte [][] parseRegionName(final byte [] regionName)
562 throws IOException {
563
564
565
566
567
568 int offset = -1;
569 for (int i = 0; i < regionName.length; i++) {
570 if (regionName[i] == HConstants.DELIMITER) {
571 offset = i;
572 break;
573 }
574 }
575 if (offset == -1) {
576 throw new IOException("Invalid regionName format: " + Bytes.toStringBinary(regionName));
577 }
578 byte[] tableName = new byte[offset];
579 System.arraycopy(regionName, 0, tableName, 0, offset);
580 offset = -1;
581
582 int endOffset = regionName.length;
583
584 if (regionName.length > MD5_HEX_LENGTH + 2
585 && regionName[regionName.length-1] == ENC_SEPARATOR
586 && regionName[regionName.length-MD5_HEX_LENGTH-2] == ENC_SEPARATOR) {
587 endOffset = endOffset - MD5_HEX_LENGTH - 2;
588 }
589
590
591 byte[] replicaId = null;
592 int idEndOffset = endOffset;
593 for (int i = endOffset - 1; i > 0; i--) {
594 if (regionName[i] == REPLICA_ID_DELIMITER) {
595 replicaId = new byte[endOffset - i - 1];
596 System.arraycopy(regionName, i + 1, replicaId, 0,
597 endOffset - i - 1);
598 idEndOffset = i;
599
600 }
601 if (regionName[i] == HConstants.DELIMITER) {
602 offset = i;
603 break;
604 }
605 }
606 if (offset == -1) {
607 throw new IOException("Invalid regionName format: " + Bytes.toStringBinary(regionName));
608 }
609 byte [] startKey = HConstants.EMPTY_BYTE_ARRAY;
610 if(offset != tableName.length + 1) {
611 startKey = new byte[offset - tableName.length - 1];
612 System.arraycopy(regionName, tableName.length + 1, startKey, 0,
613 offset - tableName.length - 1);
614 }
615 byte [] id = new byte[idEndOffset - offset - 1];
616 System.arraycopy(regionName, offset + 1, id, 0,
617 idEndOffset - offset - 1);
618 byte [][] elements = new byte[replicaId == null ? 3 : 4][];
619 elements[0] = tableName;
620 elements[1] = startKey;
621 elements[2] = id;
622 if (replicaId != null) {
623 elements[3] = replicaId;
624 }
625
626 return elements;
627 }
628
629
630 public long getRegionId(){
631 return regionId;
632 }
633
634
635
636
637
638 public byte [] getRegionName(){
639 return regionName;
640 }
641
642
643
644
645 public String getRegionNameAsString() {
646 if (hasEncodedName(this.regionName)) {
647
648 return Bytes.toStringBinary(this.regionName);
649 }
650
651
652
653
654 return Bytes.toStringBinary(this.regionName) + "." + this.getEncodedName();
655 }
656
657
658 public synchronized String getEncodedName() {
659 if (this.encodedName == null) {
660 this.encodedName = encodeRegionName(this.regionName);
661 }
662 return this.encodedName;
663 }
664
665 public synchronized byte [] getEncodedNameAsBytes() {
666 if (this.encodedNameAsBytes == null) {
667 this.encodedNameAsBytes = Bytes.toBytes(getEncodedName());
668 }
669 return this.encodedNameAsBytes;
670 }
671
672
673 public byte [] getStartKey(){
674 return startKey;
675 }
676
677
678 public byte [] getEndKey(){
679 return endKey;
680 }
681
682
683
684
685
686
687
688
689 @Deprecated
690 public byte [] getTableName() {
691 return getTable().toBytes();
692 }
693
694
695
696
697
698
699 public TableName getTable() {
700
701
702
703 if (tableName == null || tableName.getName().length == 0) {
704 tableName = getTable(getRegionName());
705 }
706 return this.tableName;
707 }
708
709
710
711
712
713
714
715
716 public boolean containsRange(byte[] rangeStartKey, byte[] rangeEndKey) {
717 if (Bytes.compareTo(rangeStartKey, rangeEndKey) > 0) {
718 throw new IllegalArgumentException(
719 "Invalid range: " + Bytes.toStringBinary(rangeStartKey) +
720 " > " + Bytes.toStringBinary(rangeEndKey));
721 }
722
723 boolean firstKeyInRange = Bytes.compareTo(rangeStartKey, startKey) >= 0;
724 boolean lastKeyInRange =
725 Bytes.compareTo(rangeEndKey, endKey) < 0 ||
726 Bytes.equals(endKey, HConstants.EMPTY_BYTE_ARRAY);
727 return firstKeyInRange && lastKeyInRange;
728 }
729
730
731
732
733 public boolean containsRow(byte[] row) {
734 return Bytes.compareTo(row, startKey) >= 0 &&
735 (Bytes.compareTo(row, endKey) < 0 ||
736 Bytes.equals(endKey, HConstants.EMPTY_BYTE_ARRAY));
737 }
738
739
740
741
742 public boolean isMetaTable() {
743 return isMetaRegion();
744 }
745
746
747 public boolean isMetaRegion() {
748 return tableName.equals(HRegionInfo.FIRST_META_REGIONINFO.getTable());
749 }
750
751
752
753
754 public boolean isSystemTable() {
755 return tableName.isSystemTable();
756 }
757
758
759
760
761 public boolean isSplit() {
762 return this.split;
763 }
764
765
766
767
768 public void setSplit(boolean split) {
769 this.split = split;
770 }
771
772
773
774
775 public boolean isOffline() {
776 return this.offLine;
777 }
778
779
780
781
782
783
784 public void setOffline(boolean offLine) {
785 this.offLine = offLine;
786 }
787
788
789
790
791 public boolean isSplitParent() {
792 if (!isSplit()) return false;
793 if (!isOffline()) {
794 LOG.warn("Region is split but NOT offline: " + getRegionNameAsString());
795 }
796 return true;
797 }
798
799
800
801
802
803 public int getReplicaId() {
804 return replicaId;
805 }
806
807
808
809
810 @Override
811 public String toString() {
812 return "{ENCODED => " + getEncodedName() + ", " +
813 HConstants.NAME + " => '" + Bytes.toStringBinary(this.regionName)
814 + "', STARTKEY => '" +
815 Bytes.toStringBinary(this.startKey) + "', ENDKEY => '" +
816 Bytes.toStringBinary(this.endKey) + "'" +
817 (isOffline()? ", OFFLINE => true": "") +
818 (isSplit()? ", SPLIT => true": "") +
819 ((replicaId > 0)? ", REPLICA_ID => " + replicaId : "") + "}";
820 }
821
822
823
824
825 @Override
826 public boolean equals(Object o) {
827 if (this == o) {
828 return true;
829 }
830 if (o == null) {
831 return false;
832 }
833 if (!(o instanceof HRegionInfo)) {
834 return false;
835 }
836 return this.compareTo((HRegionInfo)o) == 0;
837 }
838
839
840
841
842 @Override
843 public int hashCode() {
844 return this.hashCode;
845 }
846
847
848
849 @Deprecated
850 public byte getVersion() {
851 return VERSION;
852 }
853
854
855
856
857
858 @Deprecated
859 public void write(DataOutput out) throws IOException {
860 out.writeByte(getVersion());
861 Bytes.writeByteArray(out, endKey);
862 out.writeBoolean(offLine);
863 out.writeLong(regionId);
864 Bytes.writeByteArray(out, regionName);
865 out.writeBoolean(split);
866 Bytes.writeByteArray(out, startKey);
867 Bytes.writeByteArray(out, tableName.getName());
868 out.writeInt(hashCode);
869 }
870
871
872
873
874
875 @Deprecated
876 public void readFields(DataInput in) throws IOException {
877
878
879
880 byte version = in.readByte();
881 if (version == 0) {
882
883
884 this.endKey = Bytes.readByteArray(in);
885 this.offLine = in.readBoolean();
886 this.regionId = in.readLong();
887 this.regionName = Bytes.readByteArray(in);
888 this.split = in.readBoolean();
889 this.startKey = Bytes.readByteArray(in);
890 try {
891 HTableDescriptor htd = new HTableDescriptor();
892 htd.readFields(in);
893 this.tableName = htd.getTableName();
894 } catch(EOFException eofe) {
895 throw new IOException("HTD not found in input buffer", eofe);
896 }
897 this.hashCode = in.readInt();
898 } else if (getVersion() == version) {
899 this.endKey = Bytes.readByteArray(in);
900 this.offLine = in.readBoolean();
901 this.regionId = in.readLong();
902 this.regionName = Bytes.readByteArray(in);
903 this.split = in.readBoolean();
904 this.startKey = Bytes.readByteArray(in);
905 this.tableName = TableName.valueOf(Bytes.readByteArray(in));
906 this.hashCode = in.readInt();
907 } else {
908 throw new IOException("Non-migratable/unknown version=" + getVersion());
909 }
910 }
911
912 @Deprecated
913 private void readFields(byte[] bytes, int offset, int len) throws IOException {
914 if (bytes == null || len <= 0) {
915 throw new IllegalArgumentException("Can't build a writable with empty " +
916 "bytes array");
917 }
918 DataInputBuffer in = new DataInputBuffer();
919 try {
920 in.reset(bytes, offset, len);
921 this.readFields(in);
922 } finally {
923 in.close();
924 }
925 }
926
927
928
929
930
931 @Override
932 public int compareTo(HRegionInfo o) {
933 if (o == null) {
934 return 1;
935 }
936
937
938 int result = this.tableName.compareTo(o.tableName);
939 if (result != 0) {
940 return result;
941 }
942
943
944 result = Bytes.compareTo(this.startKey, o.startKey);
945 if (result != 0) {
946 return result;
947 }
948
949
950 result = Bytes.compareTo(this.endKey, o.endKey);
951
952 if (result != 0) {
953 if (this.getStartKey().length != 0
954 && this.getEndKey().length == 0) {
955 return 1;
956 }
957 if (o.getStartKey().length != 0
958 && o.getEndKey().length == 0) {
959 return -1;
960 }
961 return result;
962 }
963
964
965
966 if (this.regionId > o.regionId) {
967 return 1;
968 } else if (this.regionId < o.regionId) {
969 return -1;
970 }
971
972 int replicaDiff = this.getReplicaId() - o.getReplicaId();
973 if (replicaDiff != 0) return replicaDiff;
974
975 if (this.offLine == o.offLine)
976 return 0;
977 if (this.offLine == true) return -1;
978
979 return 1;
980 }
981
982
983
984
985 public KVComparator getComparator() {
986 return isMetaRegion()?
987 KeyValue.META_COMPARATOR: KeyValue.COMPARATOR;
988 }
989
990
991
992
993
994
995 RegionInfo convert() {
996 return convert(this);
997 }
998
999
1000
1001
1002
1003
1004
1005 public static RegionInfo convert(final HRegionInfo info) {
1006 if (info == null) return null;
1007 RegionInfo.Builder builder = RegionInfo.newBuilder();
1008 builder.setTableName(ProtobufUtil.toProtoTableName(info.getTable()));
1009 builder.setRegionId(info.getRegionId());
1010 if (info.getStartKey() != null) {
1011 builder.setStartKey(ByteStringer.wrap(info.getStartKey()));
1012 }
1013 if (info.getEndKey() != null) {
1014 builder.setEndKey(ByteStringer.wrap(info.getEndKey()));
1015 }
1016 builder.setOffline(info.isOffline());
1017 builder.setSplit(info.isSplit());
1018 builder.setReplicaId(info.getReplicaId());
1019 return builder.build();
1020 }
1021
1022
1023
1024
1025
1026
1027
1028 public static HRegionInfo convert(final RegionInfo proto) {
1029 if (proto == null) return null;
1030 TableName tableName =
1031 ProtobufUtil.toTableName(proto.getTableName());
1032 if (tableName.equals(TableName.META_TABLE_NAME)) {
1033 return RegionReplicaUtil.getRegionInfoForReplica(FIRST_META_REGIONINFO,
1034 proto.getReplicaId());
1035 }
1036 long regionId = proto.getRegionId();
1037 int replicaId = proto.hasReplicaId() ? proto.getReplicaId() : DEFAULT_REPLICA_ID;
1038 byte[] startKey = null;
1039 byte[] endKey = null;
1040 if (proto.hasStartKey()) {
1041 startKey = proto.getStartKey().toByteArray();
1042 }
1043 if (proto.hasEndKey()) {
1044 endKey = proto.getEndKey().toByteArray();
1045 }
1046 boolean split = false;
1047 if (proto.hasSplit()) {
1048 split = proto.getSplit();
1049 }
1050 HRegionInfo hri = new HRegionInfo(
1051 tableName,
1052 startKey,
1053 endKey, split, regionId, replicaId);
1054 if (proto.hasOffline()) {
1055 hri.setOffline(proto.getOffline());
1056 }
1057 return hri;
1058 }
1059
1060
1061
1062
1063
1064 public byte [] toByteArray() {
1065 byte [] bytes = convert().toByteArray();
1066 return ProtobufUtil.prependPBMagic(bytes);
1067 }
1068
1069
1070
1071
1072
1073
1074 public static HRegionInfo parseFromOrNull(final byte [] bytes) {
1075 if (bytes == null) return null;
1076 return parseFromOrNull(bytes, 0, bytes.length);
1077 }
1078
1079
1080
1081
1082
1083
1084 public static HRegionInfo parseFromOrNull(final byte [] bytes, int offset, int len) {
1085 if (bytes == null || len <= 0) return null;
1086 try {
1087 return parseFrom(bytes, offset, len);
1088 } catch (DeserializationException e) {
1089 return null;
1090 }
1091 }
1092
1093
1094
1095
1096
1097
1098
1099 public static HRegionInfo parseFrom(final byte [] bytes) throws DeserializationException {
1100 if (bytes == null) return null;
1101 return parseFrom(bytes, 0, bytes.length);
1102 }
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112 public static HRegionInfo parseFrom(final byte [] bytes, int offset, int len)
1113 throws DeserializationException {
1114 if (ProtobufUtil.isPBMagicPrefix(bytes, offset, len)) {
1115 int pblen = ProtobufUtil.lengthOfPBMagic();
1116 try {
1117 HBaseProtos.RegionInfo ri =
1118 HBaseProtos.RegionInfo.newBuilder().
1119 mergeFrom(bytes, pblen + offset, len - pblen).build();
1120 return convert(ri);
1121 } catch (InvalidProtocolBufferException e) {
1122 throw new DeserializationException(e);
1123 }
1124 } else {
1125 try {
1126 HRegionInfo hri = new HRegionInfo();
1127 hri.readFields(bytes, offset, len);
1128 return hri;
1129 } catch (IOException e) {
1130 throw new DeserializationException(e);
1131 }
1132 }
1133 }
1134
1135
1136
1137
1138
1139
1140
1141
1142 public byte [] toDelimitedByteArray() throws IOException {
1143 return ProtobufUtil.toDelimitedByteArray(convert());
1144 }
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154 @Deprecated
1155 public static Pair<HRegionInfo, ServerName> getHRegionInfoAndServerName(final Result r) {
1156 HRegionInfo info =
1157 getHRegionInfo(r, HConstants.REGIONINFO_QUALIFIER);
1158 ServerName sn = getServerName(r);
1159 return new Pair<HRegionInfo, ServerName>(info, sn);
1160 }
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170 @Deprecated
1171 public static HRegionInfo getHRegionInfo(Result data) {
1172 return getHRegionInfo(data, HConstants.REGIONINFO_QUALIFIER);
1173 }
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183 @Deprecated
1184 public static PairOfSameType<HRegionInfo> getDaughterRegions(Result data) throws IOException {
1185 HRegionInfo splitA = getHRegionInfo(data, HConstants.SPLITA_QUALIFIER);
1186 HRegionInfo splitB = getHRegionInfo(data, HConstants.SPLITB_QUALIFIER);
1187
1188 return new PairOfSameType<HRegionInfo>(splitA, splitB);
1189 }
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199 @Deprecated
1200 public static PairOfSameType<HRegionInfo> getMergeRegions(Result data) throws IOException {
1201 HRegionInfo mergeA = getHRegionInfo(data, HConstants.MERGEA_QUALIFIER);
1202 HRegionInfo mergeB = getHRegionInfo(data, HConstants.MERGEB_QUALIFIER);
1203
1204 return new PairOfSameType<HRegionInfo>(mergeA, mergeB);
1205 }
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217 @Deprecated
1218 public static HRegionInfo getHRegionInfo(final Result r, byte [] qualifier) {
1219 Cell cell = r.getColumnLatestCell(
1220 HConstants.CATALOG_FAMILY, qualifier);
1221 if (cell == null) return null;
1222 return parseFromOrNull(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
1223 }
1224
1225
1226
1227
1228 @Deprecated
1229 public static ServerName getServerName(final Result r) {
1230 Cell cell = r.getColumnLatestCell(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER);
1231 if (cell == null || cell.getValueLength() == 0) return null;
1232 String hostAndPort = Bytes.toString(
1233 cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
1234 cell = r.getColumnLatestCell(HConstants.CATALOG_FAMILY,
1235 HConstants.STARTCODE_QUALIFIER);
1236 if (cell == null || cell.getValueLength() == 0) return null;
1237 try {
1238 return ServerName.valueOf(hostAndPort,
1239 Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
1240 } catch (IllegalArgumentException e) {
1241 LOG.error("Ignoring invalid region for server " + hostAndPort + "; cell=" + cell, e);
1242 return null;
1243 }
1244 }
1245
1246
1247
1248
1249
1250
1251
1252
1253 @Deprecated
1254 public static long getSeqNumDuringOpen(final Result r) {
1255 Cell cell = r.getColumnLatestCell(HConstants.CATALOG_FAMILY, HConstants.SEQNUM_QUALIFIER);
1256 if (cell == null || cell.getValueLength() == 0) return HConstants.NO_SEQNUM;
1257 return Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
1258 }
1259
1260
1261
1262
1263
1264
1265
1266
1267 public static HRegionInfo parseFrom(final DataInputStream in) throws IOException {
1268
1269
1270 int pblen = ProtobufUtil.lengthOfPBMagic();
1271 byte [] pbuf = new byte[pblen];
1272 if (in.markSupported()) {
1273 in.mark(pblen);
1274 }
1275
1276
1277 int read = in.read(pbuf);
1278 if (read != pblen) throw new IOException("read=" + read + ", wanted=" + pblen);
1279 if (ProtobufUtil.isPBMagicPrefix(pbuf)) {
1280 return convert(HBaseProtos.RegionInfo.parseDelimitedFrom(in));
1281 } else {
1282
1283 if (in.markSupported()) {
1284 in.reset();
1285 HRegionInfo hri = new HRegionInfo();
1286 hri.readFields(in);
1287 return hri;
1288 } else {
1289
1290 ByteArrayInputStream bais = new ByteArrayInputStream(pbuf);
1291 SequenceInputStream sis = new SequenceInputStream(bais, in);
1292 HRegionInfo hri = new HRegionInfo();
1293 hri.readFields(new DataInputStream(sis));
1294 return hri;
1295 }
1296 }
1297 }
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309 public static byte[] toDelimitedByteArray(HRegionInfo... infos) throws IOException {
1310 byte[][] bytes = new byte[infos.length][];
1311 int size = 0;
1312 for (int i = 0; i < infos.length; i++) {
1313 bytes[i] = infos[i].toDelimitedByteArray();
1314 size += bytes[i].length;
1315 }
1316
1317 byte[] result = new byte[size];
1318 int offset = 0;
1319 for (byte[] b : bytes) {
1320 System.arraycopy(b, 0, result, offset, b.length);
1321 offset += b.length;
1322 }
1323 return result;
1324 }
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334 public static List<HRegionInfo> parseDelimitedFrom(final byte[] bytes, final int offset,
1335 final int length) throws IOException {
1336 if (bytes == null) {
1337 throw new IllegalArgumentException("Can't build an object with empty bytes array");
1338 }
1339 DataInputBuffer in = new DataInputBuffer();
1340 List<HRegionInfo> hris = new ArrayList<HRegionInfo>();
1341 try {
1342 in.reset(bytes, offset, length);
1343 while (in.available() > 0) {
1344 HRegionInfo hri = parseFrom(in);
1345 hris.add(hri);
1346 }
1347 } finally {
1348 in.close();
1349 }
1350 return hris;
1351 }
1352
1353
1354
1355
1356
1357
1358
1359 public static boolean areAdjacent(HRegionInfo regionA, HRegionInfo regionB) {
1360 if (regionA == null || regionB == null) {
1361 throw new IllegalArgumentException(
1362 "Can't check whether adjacent for null region");
1363 }
1364 HRegionInfo a = regionA;
1365 HRegionInfo b = regionB;
1366 if (Bytes.compareTo(a.getStartKey(), b.getStartKey()) > 0) {
1367 a = regionB;
1368 b = regionA;
1369 }
1370 if (Bytes.compareTo(a.getEndKey(), b.getStartKey()) == 0) {
1371 return true;
1372 }
1373 return false;
1374 }
1375 }