1
- /**
1
+ /*
2
2
*
3
3
* Licensed to the Apache Software Foundation (ASF) under one
4
4
* or more contributor license agreements. See the NOTICE file
70
70
*/
71
71
@ InterfaceAudience .Public
72
72
public interface RegionInfo {
73
- public static final RegionInfo UNDEFINED =
74
- RegionInfoBuilder .newBuilder (TableName .valueOf ("__UNDEFINED__" )).build ();
73
+ RegionInfo UNDEFINED = RegionInfoBuilder .newBuilder (TableName .valueOf ("__UNDEFINED__" )).build ();
75
74
/**
76
75
* Separator used to demarcate the encodedName in a region name
77
76
* in the new format. See description on new format above.
@@ -141,11 +140,16 @@ public interface RegionInfo {
141
140
}
142
141
143
142
int replicaDiff = lhs .getReplicaId () - rhs .getReplicaId ();
144
- if (replicaDiff != 0 ) return replicaDiff ;
143
+ if (replicaDiff != 0 ) {
144
+ return replicaDiff ;
145
+ }
145
146
146
- if (lhs .isOffline () == rhs .isOffline ())
147
+ if (lhs .isOffline () == rhs .isOffline ()) {
147
148
return 0 ;
148
- if (lhs .isOffline () == true ) return -1 ;
149
+ }
150
+ if (lhs .isOffline ()) {
151
+ return -1 ;
152
+ }
149
153
150
154
return 1 ;
151
155
};
@@ -224,8 +228,6 @@ public interface RegionInfo {
224
228
boolean isMetaRegion ();
225
229
226
230
/**
227
- * @param rangeStartKey
228
- * @param rangeEndKey
229
231
* @return true if the given inclusive range of rows is fully contained
230
232
* by this region. For example, if the region is foo,a,g and this is
231
233
* passed ["b","c"] or ["a","c"] it will return true, but if this is passed
@@ -235,7 +237,6 @@ public interface RegionInfo {
235
237
boolean containsRange (byte [] rangeStartKey , byte [] rangeEndKey );
236
238
237
239
/**
238
- * @param row
239
240
* @return true if the given row falls in this region.
240
241
*/
241
242
boolean containsRow (byte [] row );
@@ -339,9 +340,7 @@ static TableName getTable(final byte [] regionName) {
339
340
340
341
/**
341
342
* Gets the start key from the specified region name.
342
- * @param regionName
343
343
* @return Start key.
344
- * @throws java.io.IOException
345
344
*/
346
345
static byte [] getStartKey (final byte [] regionName ) throws IOException {
347
346
return parseRegionName (regionName )[1 ];
@@ -362,7 +361,6 @@ static boolean isEncodedRegionName(byte[] regionName) throws IOException {
362
361
}
363
362
364
363
/**
365
- * @param bytes
366
364
* @return A deserialized {@link RegionInfo}
367
365
* or null if we failed deserialize or passed bytes null
368
366
*/
@@ -373,9 +371,6 @@ static RegionInfo parseFromOrNull(final byte [] bytes) {
373
371
}
374
372
375
373
/**
376
- * @param bytes
377
- * @param offset
378
- * @param len
379
374
* @return A deserialized {@link RegionInfo} or null
380
375
* if we failed deserialize or passed bytes null
381
376
*/
@@ -392,7 +387,6 @@ static RegionInfo parseFromOrNull(final byte [] bytes, int offset, int len) {
392
387
/**
393
388
* @param bytes A pb RegionInfo serialized with a pb magic prefix.
394
389
* @return A deserialized {@link RegionInfo}
395
- * @throws DeserializationException
396
390
*/
397
391
@ InterfaceAudience .Private
398
392
static RegionInfo parseFrom (final byte [] bytes ) throws DeserializationException {
@@ -405,7 +399,6 @@ static RegionInfo parseFrom(final byte [] bytes) throws DeserializationException
405
399
* @param offset starting point in the byte array
406
400
* @param len length to read on the byte array
407
401
* @return A deserialized {@link RegionInfo}
408
- * @throws DeserializationException
409
402
*/
410
403
@ InterfaceAudience .Private
411
404
static RegionInfo parseFrom (final byte [] bytes , int offset , int len )
@@ -426,30 +419,28 @@ static RegionInfo parseFrom(final byte [] bytes, int offset, int len)
426
419
}
427
420
428
421
/**
429
- * Check whether two regions are adjacent
430
- * @param regionA
431
- * @param regionB
422
+ * Check whether two regions are adjacent; i.e. lies just before or just
423
+ * after in a table.
432
424
* @return true if two regions are adjacent
433
425
*/
434
426
static boolean areAdjacent (RegionInfo regionA , RegionInfo regionB ) {
435
427
if (regionA == null || regionB == null ) {
436
428
throw new IllegalArgumentException (
437
429
"Can't check whether adjacent for null region" );
438
430
}
431
+ if (!regionA .getTable ().equals (regionB .getTable ())) {
432
+ return false ;
433
+ }
439
434
RegionInfo a = regionA ;
440
435
RegionInfo b = regionB ;
441
436
if (Bytes .compareTo (a .getStartKey (), b .getStartKey ()) > 0 ) {
442
437
a = regionB ;
443
438
b = regionA ;
444
439
}
445
- if (Bytes .compareTo (a .getEndKey (), b .getStartKey ()) == 0 ) {
446
- return true ;
447
- }
448
- return false ;
440
+ return Bytes .equals (a .getEndKey (), b .getStartKey ());
449
441
}
450
442
451
443
/**
452
- * @param ri
453
444
* @return This instance serialized as protobuf w/ a magic pb prefix.
454
445
* @see #parseFrom(byte[])
455
446
*/
@@ -473,7 +464,6 @@ static String prettyPrint(final String encodedRegionName) {
473
464
474
465
/**
475
466
* Make a region name of passed parameters.
476
- * @param tableName
477
467
* @param startKey Can be null
478
468
* @param regionid Region id (Usually timestamp from when region was created).
479
469
* @param newFormat should we create the region name in the new format
@@ -487,7 +477,6 @@ static String prettyPrint(final String encodedRegionName) {
487
477
488
478
/**
489
479
* Make a region name of passed parameters.
490
- * @param tableName
491
480
* @param startKey Can be null
492
481
* @param id Region id (Usually timestamp from when region was created).
493
482
* @param newFormat should we create the region name in the new format
@@ -501,10 +490,8 @@ static String prettyPrint(final String encodedRegionName) {
501
490
502
491
/**
503
492
* Make a region name of passed parameters.
504
- * @param tableName
505
493
* @param startKey Can be null
506
494
* @param regionid Region id (Usually timestamp from when region was created).
507
- * @param replicaId
508
495
* @param newFormat should we create the region name in the new format
509
496
* (such that it contains its encoded name?).
510
497
* @return Region name made of passed tableName, startKey, id and replicaId
@@ -517,7 +504,6 @@ static String prettyPrint(final String encodedRegionName) {
517
504
518
505
/**
519
506
* Make a region name of passed parameters.
520
- * @param tableName
521
507
* @param startKey Can be null
522
508
* @param id Region id (Usually timestamp from when region was created).
523
509
* @param newFormat should we create the region name in the new format
@@ -531,10 +517,8 @@ static String prettyPrint(final String encodedRegionName) {
531
517
532
518
/**
533
519
* Make a region name of passed parameters.
534
- * @param tableName
535
520
* @param startKey Can be null
536
521
* @param id Region id (Usually timestamp from when region was created).
537
- * @param replicaId
538
522
* @param newFormat should we create the region name in the new format
539
523
* @return Region name made of passed tableName, startKey, id and replicaId
540
524
*/
@@ -593,7 +577,7 @@ static String prettyPrint(final String encodedRegionName) {
593
577
b [offset ++] = ENC_SEPARATOR ;
594
578
System .arraycopy (md5HashBytes , 0 , b , offset , MD5_HEX_LENGTH );
595
579
offset += MD5_HEX_LENGTH ;
596
- b [offset ++ ] = ENC_SEPARATOR ;
580
+ b [offset ] = ENC_SEPARATOR ;
597
581
}
598
582
599
583
return b ;
@@ -612,9 +596,7 @@ static RegionInfo createMobRegionInfo(TableName tableName) {
612
596
613
597
/**
614
598
* Separate elements of a regionName.
615
- * @param regionName
616
599
* @return Array of byte[] containing tableName, startKey and id
617
- * @throws IOException
618
600
*/
619
601
static byte [][] parseRegionName (final byte [] regionName )
620
602
throws IOException {
@@ -693,7 +675,6 @@ static RegionInfo createMobRegionInfo(TableName tableName) {
693
675
* be used to read back the instances.
694
676
* @param infos RegionInfo objects to serialize
695
677
* @return This instance serialized as a delimited protobuf w/ a magic pb prefix.
696
- * @throws IOException
697
678
*/
698
679
static byte [] toDelimitedByteArray (RegionInfo ... infos ) throws IOException {
699
680
byte [][] bytes = new byte [infos .length ][];
@@ -715,9 +696,7 @@ static byte[] toDelimitedByteArray(RegionInfo... infos) throws IOException {
715
696
/**
716
697
* Use this instead of {@link RegionInfo#toByteArray(RegionInfo)} when writing to a stream and you want to use
717
698
* the pb mergeDelimitedFrom (w/o the delimiter, pb reads to EOF which may not be what you want).
718
- * @param ri
719
699
* @return This instance serialized as a delimied protobuf w/ a magic pb prefix.
720
- * @throws IOException
721
700
*/
722
701
static byte [] toDelimitedByteArray (RegionInfo ri ) throws IOException {
723
702
return ProtobufUtil .toDelimitedByteArray (ProtobufUtil .toRegionInfo (ri ));
@@ -727,9 +706,7 @@ static byte[] toDelimitedByteArray(RegionInfo... infos) throws IOException {
727
706
* Parses an RegionInfo instance from the passed in stream.
728
707
* Presumes the RegionInfo was serialized to the stream with
729
708
* {@link #toDelimitedByteArray(RegionInfo)}.
730
- * @param in
731
709
* @return An instance of RegionInfo.
732
- * @throws IOException
733
710
*/
734
711
static RegionInfo parseFrom (final DataInputStream in ) throws IOException {
735
712
// I need to be able to move back in the stream if this is not a pb
@@ -757,28 +734,23 @@ static RegionInfo parseFrom(final DataInputStream in) throws IOException {
757
734
* @param offset the start offset into the byte[] buffer
758
735
* @param length how far we should read into the byte[] buffer
759
736
* @return All the RegionInfos that are in the byte array. Keeps reading till we hit the end.
760
- * @throws IOException
761
737
*/
762
738
static List <RegionInfo > parseDelimitedFrom (final byte [] bytes , final int offset ,
763
739
final int length ) throws IOException {
764
740
if (bytes == null ) {
765
741
throw new IllegalArgumentException ("Can't build an object with empty bytes array" );
766
742
}
767
- DataInputBuffer in = new DataInputBuffer ();
768
743
List <RegionInfo > ris = new ArrayList <>();
769
- try {
744
+ try ( DataInputBuffer in = new DataInputBuffer ()) {
770
745
in .reset (bytes , offset , length );
771
746
while (in .available () > 0 ) {
772
747
RegionInfo ri = parseFrom (in );
773
748
ris .add (ri );
774
749
}
775
- } finally {
776
- in .close ();
777
750
}
778
751
return ris ;
779
752
}
780
753
781
-
782
754
/**
783
755
* @return True if this is first Region in Table
784
756
*/
@@ -794,10 +766,20 @@ default boolean isLast() {
794
766
}
795
767
796
768
/**
797
- * @return True if regions are adjacent, if 'after' next. Does not do tablename compare.
769
+ * @return True if region is next, adjacent but 'after' this one.
770
+ * @see #isAdjacent(RegionInfo)
771
+ * @see #areAdjacent(RegionInfo, RegionInfo)
798
772
*/
799
773
default boolean isNext (RegionInfo after ) {
800
- return Bytes .equals (getEndKey (), after .getStartKey ());
774
+ return getTable ().equals (after .getTable ()) && Bytes .equals (getEndKey (), after .getStartKey ());
775
+ }
776
+
777
+ /**
778
+ * @return True if region is adjacent, either just before or just after this one.
779
+ * @see #isNext(RegionInfo)
780
+ */
781
+ default boolean isAdjacent (RegionInfo other ) {
782
+ return getTable ().equals (other .getTable ()) && areAdjacent (this , other );
801
783
}
802
784
803
785
/**
@@ -808,11 +790,13 @@ default boolean isDegenerate() {
808
790
}
809
791
810
792
/**
811
- * @return True if an overlap in region range. Does not do tablename compare.
812
- * Does not check if <code>other</code> has degenerate range.
793
+ * @return True if an overlap in region range.
813
794
* @see #isDegenerate()
814
795
*/
815
796
default boolean isOverlap (RegionInfo other ) {
797
+ if (!getTable ().equals (other .getTable ())) {
798
+ return false ;
799
+ }
816
800
int startKeyCompare = Bytes .compareTo (getStartKey (), other .getStartKey ());
817
801
if (startKeyCompare == 0 ) {
818
802
return true ;
0 commit comments