1717
1818package com .alipay .oceanbase .rpc .location .model .partition ;
1919
20+ import com .alipay .oceanbase .rpc .exception .ObTableException ;
2021import com .alipay .oceanbase .rpc .exception .ObTablePartitionConsistentException ;
2122import com .alipay .oceanbase .rpc .mutation .Row ;
2223import com .alipay .oceanbase .rpc .protocol .payload .impl .ObColumn ;
2324import com .alipay .oceanbase .rpc .protocol .payload .impl .ObObj ;
2425import com .alipay .oceanbase .rpc .protocol .payload .impl .ObObjType ;
2526import com .alipay .oceanbase .rpc .util .RandomUtil ;
2627import com .alipay .oceanbase .rpc .util .TableClientLoggerFactory ;
28+ import com .alipay .oceanbase .rpc .mutation .Row ;
2729import org .apache .commons .lang .builder .ToStringBuilder ;
2830import org .slf4j .Logger ;
2931
3032import java .util .ArrayList ;
33+ import java .util .Arrays ;
3134import java .util .Collections ;
3235import java .util .List ;
3336
@@ -98,105 +101,49 @@ public void prepare() {
98101 * Get part ids.
99102 */
100103 @ Override
101- public List <Long > getPartIds (Object [] start , boolean startInclusive , Object [] end ,
104+ public List <Long > getPartIds (Object startRowObj , boolean startInclusive , Object endRowObj ,
102105 boolean endInclusive ) {
103106 // close set
104107 try {
108+ // verify the type of parameters and convert to Row
109+ if (!(startRowObj instanceof Row ) || !(endRowObj instanceof Row )) {
110+ throw new ObTableException ("invalid format of rowObj: " + startRowObj + ", "
111+ + endRowObj );
112+ }
113+ Row startRow = (Row ) startRowObj , endRow = (Row ) endRowObj ;
105114 // pre-check start and end
106115 // should remove after remove addRowkeyElement
107- if (start . length != end . length ) {
116+ if (startRow . size () != endRow . size () ) {
108117 throw new IllegalArgumentException ("length of start key and end key is not equal" );
109118 }
110119
120+ if (startRow .size () == 1 && startRow .getValues ()[0 ] instanceof ObObj && ((ObObj ) startRow .getValues ()[0 ]).isMinObj () &&
121+ endRow .size () == 1 && endRow .getValues ()[0 ] instanceof ObObj && ((ObObj ) endRow .getValues ()[0 ]).isMaxObj ()) {
122+ return completeWorks ;
123+ }
124+
111125 // check whether partition key is Min or Max, should refactor after remove addRowkeyElement
112- for (ObPair <ObColumn , List <Integer >> pair : orderedPartRefColumnRowKeyRelations ) {
113- for (int refIdx : pair .getRight ()) {
114- if (start .length <= refIdx ) {
115- throw new IllegalArgumentException ("rowkey length is " + start .length
126+ for (ObColumn curObcolumn : partColumns ) {
127+ for (int refIdx = 0 ; refIdx < curObcolumn .getRefColumnNames ().size (); ++refIdx ) {
128+ String curObRefColumnName = curObcolumn .getRefColumnNames ().get (refIdx );
129+ if (startRow .size () <= refIdx ) {
130+ throw new IllegalArgumentException ("rowkey length is " + startRow .size ()
116131 + ", which is shortest than " + refIdx );
117132 }
118- if (start [refIdx ] instanceof ObObj
119- && (((ObObj ) start [refIdx ]).isMinObj () || ((ObObj ) start [refIdx ])
120- .isMaxObj ())) {
121- return completeWorks ;
133+ Object startValue = startRow .get (curObRefColumnName );
134+ if (startValue == null ) {
135+ throw new IllegalArgumentException ("Please include all partition key in start range. Currently missing key: { " + curObRefColumnName + " }" );
122136 }
123- if (end [ refIdx ] instanceof ObObj
124- && (((ObObj ) end [ refIdx ] ).isMinObj () || ((ObObj ) end [ refIdx ] ).isMaxObj ())) {
137+ if (startValue instanceof ObObj
138+ && (((ObObj ) startValue ).isMinObj () || ((ObObj ) startValue ).isMaxObj ())) {
125139 return completeWorks ;
126140 }
127- }
128- }
129-
130- // eval partition key
131- List <Object > startValues = evalRowKeyValues (start );
132- Object startValue = startValues .get (0 );
133- List <Object > endValues = evalRowKeyValues (end );
134- Object endValue = endValues .get (0 );
135-
136- Long startLongValue = ObObjType .parseToLongOrNull (startValue );
137- Long endLongValue = ObObjType .parseToLongOrNull (endValue );
138-
139- if (startLongValue == null || endLongValue == null ) {
140- throw new NumberFormatException ("can not parseToComparable start value ["
141- + startValue + "] or end value [" + endValue
142- + "] to long" );
143- }
144- long startHashValue = startLongValue - (startInclusive ? 0 : -1 );
145- long endHashValue = endLongValue - (endInclusive ? 0 : 1 );
146-
147- if (endHashValue - startHashValue + 1 >= partNum ) {
148- return completeWorks ;
149- } else {
150- List <Long > partIds = new ArrayList <Long >();
151- for (long i = startHashValue ; i <= endHashValue ; i ++) {
152- partIds .add (innerHash (i ));
153- }
154- return partIds ;
155- }
156- } catch (IllegalArgumentException e ) {
157- logger .error (LCD .convert ("01-00002" ), e );
158- throw new IllegalArgumentException (
159- "ObHashPartDesc get part id come across illegal params" , e );
160- }
161- }
162-
163- @ Override
164- public List <Long > getPartIds (List <String > scanRangeColumns , Object [] start , boolean startInclusive ,
165- Object [] end , boolean endInclusive ) throws IllegalArgumentException {
166- try {
167- if (start .length != end .length ) {
168- throw new IllegalArgumentException ("length of start key and end key in range is not equal, " +
169- "the start key: " + start + ", the end key: " + end );
170- }
171-
172- if (start .length == 1 && start [0 ] instanceof ObObj && ((ObObj ) start [0 ]).isMinObj () &&
173- end .length == 1 && end [0 ] instanceof ObObj && ((ObObj ) end [0 ]).isMaxObj ()) {
174- return completeWorks ;
175- }
176-
177- if (scanRangeColumns .size () != start .length ) {
178- throw new IllegalArgumentException ("length of start key in range and scan range columns is not equal," +
179- "the start key: " + start + ", the scan range columns: " + scanRangeColumns );
180- }
181-
182- Row startRow = new Row ();
183- Row endRow = new Row ();
184- for (int i = 0 ; i < scanRangeColumns .size (); i ++) {
185- startRow .add (scanRangeColumns .get (i ), start [i ]);
186- endRow .add (scanRangeColumns .get (i ), end [i ]);
187- }
188-
189- // check whether partition key is Min or Max, should refactor after remove addRowkeyElement
190- for (ObColumn partColumn : partColumns ) {
191- List <String > refColumns = partColumn .getRefColumnNames ();
192- for (String column : refColumns ) {
193- if (startRow .get (column ) instanceof ObObj
194- && (((ObObj ) startRow .get (column )).isMinObj () || ((ObObj ) startRow .get (column ))
195- .isMaxObj ())) {
196- return completeWorks ;
141+ Object endValue = endRow .get (curObRefColumnName );
142+ if (endValue == null ) {
143+ throw new IllegalArgumentException ("Please include all partition key in end range. Currently missing key: { " + curObRefColumnName + " }" );
197144 }
198- if (endRow . get ( column ) instanceof ObObj
199- && (((ObObj ) endRow . get ( column )) .isMinObj () || ((ObObj ) endRow . get ( column ) ).isMaxObj ())) {
145+ if (endValue instanceof ObObj
146+ && (((ObObj ) endValue ) .isMinObj () || ((ObObj ) endValue ).isMaxObj ())) {
200147 return completeWorks ;
201148 }
202149 }
@@ -213,8 +160,8 @@ public List<Long> getPartIds(List<String> scanRangeColumns, Object[] start, bool
213160
214161 if (startLongValue == null || endLongValue == null ) {
215162 throw new NumberFormatException ("can not parseToComparable start value ["
216- + startValue + "] or end value [" + endValue
217- + "] to long" );
163+ + startValue + "] or end value [" + endValue
164+ + "] to long" );
218165 }
219166 long startHashValue = startLongValue - (startInclusive ? 0 : -1 );
220167 long endHashValue = endLongValue - (endInclusive ? 0 : 1 );
@@ -231,7 +178,7 @@ public List<Long> getPartIds(List<String> scanRangeColumns, Object[] start, bool
231178 } catch (IllegalArgumentException e ) {
232179 logger .error (LCD .convert ("01-00002" ), e );
233180 throw new IllegalArgumentException (
234- "ObHashPartDesc get part id come across illegal params" , e );
181+ "ObHashPartDesc get part id come across illegal params" , e );
235182 }
236183 }
237184
@@ -247,26 +194,30 @@ public Long getRandomPartId() {
247194 * Get part id.
248195 */
249196 @ Override
250- public Long getPartId (Object ... rowKey ) {
251- List <Object []> rowKeys = new ArrayList <Object [] >();
252- rowKeys . add ( rowKey );
253- return this .getPartId (rowKeys , false );
197+ public Long getPartId (Object ... row ) {
198+ List <Object > rows = new ArrayList <Object >();
199+ rows . addAll ( Arrays . asList ( row ) );
200+ return this .getPartId (rows , false );
254201 }
255202
256203 /*
257204 * Get part id.
258205 */
259206 @ Override
260- public Long getPartId (List <Object []> rowKeys , boolean consistency ) {
207+ public Long getPartId (List <Object > rows , boolean consistency ) {
261208
262- if (rowKeys == null || rowKeys .size () == 0 ) {
263- throw new IllegalArgumentException ("invalid row keys :" + rowKeys );
209+ if (rows == null || rows .size () == 0 ) {
210+ throw new IllegalArgumentException ("invalid row keys :" + rows );
264211 }
265212
266213 Long partId = null ;
267214 try {
268- for (Object [] rowKey : rowKeys ) {
269- List <Object > evalValues = evalRowKeyValues (rowKey );
215+ for (Object rowObj : rows ) {
216+ if (!(rowObj instanceof Row )) {
217+ throw new ObTableException ("invalid format of rowObj: " + rowObj );
218+ }
219+ Row row = (Row ) rowObj ;
220+ List <Object > evalValues = evalRowKeyValues (row );
270221 Object value = evalValues .get (0 );// the partition type of hash has one param at most
271222 Long longValue = ObObjType .parseToLongOrNull (value );
272223
@@ -285,7 +236,7 @@ public Long getPartId(List<Object[]> rowKeys, boolean consistency) {
285236
286237 if (!partId .equals (currentPartId )) {
287238 throw new ObTablePartitionConsistentException (
288- "across partition operation may cause consistent problem " + rowKeys );
239+ "across partition operation may cause consistent problem " + rows );
289240 }
290241 }
291242 } catch (IllegalArgumentException e ) {
0 commit comments