6
6
import com .alipay .oceanbase .rpc .ObTableClient ;
7
7
import com .alipay .oceanbase .rpc .constant .Constants ;
8
8
import com .alipay .oceanbase .rpc .exception .ObTableException ;
9
+ import com .alipay .oceanbase .rpc .exception .ObTableUnexpectedException ;
9
10
import com .alipay .oceanbase .rpc .location .model .TableEntry ;
10
11
import com .alipay .oceanbase .rpc .meta .ObTableMetaRequest ;
11
12
import com .alipay .oceanbase .rpc .meta .ObTableMetaResponse ;
@@ -116,28 +117,37 @@ private OHRegionLocator createRangePartitionLocator(
116
117
final List <Object > tableIdDict ,
117
118
final List <Object > replicaDict ,
118
119
final List <Object > partitions ) {
119
- final int partitionCount = partitions .size ();
120
- final int regionCount = partitionCount + 1 ;
121
- final byte [][] startKeys = new byte [regionCount ][];
122
- final byte [][] endKeys = new byte [regionCount ][];
123
-
124
- for (int i = 0 ; i < regionCount ; i ++) {
120
+ if ((partitions .size () % tableIdDict .size ()) != 0 ) {
121
+ throw new ObTableUnexpectedException (
122
+ "The number of partitions should be an integer multiple of the number of tables" );
123
+ }
124
+ // the size of partitions the multiple of the number of zones, the number of tablets and the number of tables
125
+ final int regionCount = partitions .size () / tableIdDict .size (); // get tablet boundaries of leaders and followers
126
+ final List <byte []> startKeysList = new ArrayList <>();
127
+ final List <byte []> endKeysList = new ArrayList <>();
128
+
129
+ for (int i = 0 ; i < regionCount ; ++i ) {
130
+ boolean isLeader = ((int ) ((List <Object >) partitions .get (i )).get (4 ) == 1 );
131
+ if (!isLeader ) { // only record leader's boundary
132
+ continue ;
133
+ }
125
134
if (i == 0 ) {
126
- startKeys [ i ] = HConstants .EMPTY_BYTE_ARRAY ;
127
- endKeys [ i ] = (( List <Object >) partitions .get (i )).get (2 ).toString ().getBytes ();
135
+ startKeysList . add ( HConstants .EMPTY_BYTE_ARRAY ) ;
136
+ endKeysList . add ((( List <Object >) partitions .get (i )).get (2 ).toString ().getBytes () );
128
137
} else if (i == regionCount - 1 ) {
129
- startKeys [ i ] = (( List <Object >) partitions .get (i - 1 )).get (2 ).toString ().getBytes ();
130
- endKeys [ i ] = HConstants .EMPTY_BYTE_ARRAY ;
138
+ startKeysList . add ((( List <Object >) partitions .get (i - 1 )).get (2 ).toString ().getBytes () );
139
+ endKeysList . add ( HConstants .EMPTY_BYTE_ARRAY ) ;
131
140
} else {
132
- startKeys [ i ] = (( List <Object >) partitions .get (i - 1 )).get (2 ).toString ().getBytes ();
133
- endKeys [ i ] = (( List <Object >) partitions .get (i )).get (2 ).toString ().getBytes ();
141
+ startKeysList . add ((( List <Object >) partitions .get (i - 1 )).get (2 ).toString ().getBytes () );
142
+ endKeysList . add ((( List <Object >) partitions .get (i )).get (2 ).toString ().getBytes () );
134
143
}
135
144
}
136
-
145
+ final byte [][] startKeys = startKeysList .toArray (new byte [0 ][]);
146
+ final byte [][] endKeys = endKeysList .toArray (new byte [0 ][]);
137
147
// Create region locations for all regions
138
148
final List <HRegionLocation > regionLocations = IntStream .range (0 , regionCount )
139
149
.mapToObj (i -> {
140
- final List <Object > partition = (List <Object >) partitions .get (Math .min (i , partitionCount - 1 ));
150
+ final List <Object > partition = (List <Object >) partitions .get (Math .min (i , regionCount - 1 ));
141
151
final int replicationIdx = (int ) partition .get (3 );
142
152
final List <Object > hostInfo = (List <Object >) replicaDict .get (replicationIdx );
143
153
@@ -155,7 +165,7 @@ private OHRegionLocator createRangePartitionLocator(
155
165
})
156
166
.collect (Collectors .toList ());
157
167
158
- return new OHRegionLocator (startKeys , endKeys , regionLocations );
168
+ return new OHRegionLocator (startKeys , endKeys , regionLocations , TableName . valueOf ( tableName ), client );
159
169
}
160
170
161
171
/**
@@ -195,7 +205,7 @@ private OHRegionLocator createHashPartitionLocator(
195
205
})
196
206
.collect (Collectors .toList ());
197
207
198
- return new OHRegionLocator (startKeys , endKeys , regionLocations );
208
+ return new OHRegionLocator (startKeys , endKeys , regionLocations , TableName . valueOf ( tableName ), client );
199
209
}
200
210
201
211
public OHRegionLocator getRegionLocator (final String tableName ) throws IOException {
0 commit comments