@@ -45,6 +45,7 @@ public class Mutation<T> {
4545 protected Row rowKey ;
4646 private TableQuery query ;
4747 private boolean hasSetRowKey = false ;
48+ private boolean hasSetPartitionKey = false ;
4849 protected List <String > rowKeyNames = null ;
4950 protected List <Object > rowKeyValues = null ;
5051 protected List <String > columns ;
@@ -308,6 +309,140 @@ public T setRowKeyOnly(ColumnValue... rowKey) {
308309 return (T ) this ;
309310 }
310311
312+ /*
313+ * set the Partition Key of mutation with Row
314+ */
315+ @ SuppressWarnings ("unchecked" )
316+ public T setPartitionKey (Row partitionKey ) {
317+ if (hasSetRowKey ) {
318+ throw new IllegalArgumentException ("Heap table can not set row key" );
319+ } else if (hasSetPartitionKey ) {
320+ throw new IllegalArgumentException ("Could not set partition key (scan range) twice" );
321+ } else if (null == partitionKey ) {
322+ throw new IllegalArgumentException ("Invalid null partitionKey set into Mutation" );
323+ } else if (0 == partitionKey .getMap ().size ()) {
324+ throw new IllegalArgumentException ("input partition key should not be empty" );
325+ }
326+
327+ // set partitionKey
328+ this .rowKey = partitionKey ;
329+
330+ // set row key name into client and set rowKeys
331+ this .rowKeyValues = new ArrayList <>(Arrays .asList (rowKey .getValues ()));
332+ this .rowKeyNames = new ArrayList <>(Arrays .asList (rowKey .getColumns ()));
333+
334+ // set row key in table
335+ if (null != tableName ) {
336+ ((ObTableClient ) client )
337+ .addRowKeyElement (tableName , this .rowKeyNames .toArray (new String [0 ]));
338+ }
339+
340+ // renew scan range of QueryAndMutate
341+ if (null != query ) {
342+ query .addScanRange (rowKeyValues .toArray (), rowKeyValues .toArray ());
343+ }
344+
345+ hasSetPartitionKey = true ;
346+ return (T ) this ;
347+ }
348+
349+ /*
350+ * Set the Partition Key of mutation with Row and keep scan range
351+ */
352+ @ SuppressWarnings ("unchecked" )
353+ protected T setPartitionKeyOnly (Row partitionKey ) {
354+ if (hasSetRowKey ) {
355+ throw new IllegalArgumentException ("Heap table can not set row key" );
356+ } else if (hasSetPartitionKey ) {
357+ throw new IllegalArgumentException ("Could not set partition key (scan range) twice" );
358+ } else if (null == partitionKey ) {
359+ throw new IllegalArgumentException ("Invalid null partitionKey set into Mutation" );
360+ } else if (0 == partitionKey .getMap ().size ()) {
361+ throw new IllegalArgumentException ("input partition key should not be empty" );
362+ }
363+
364+ // set partitionKey
365+ this .rowKey = partitionKey ;
366+
367+ // set row key name into client and set rowKeys
368+ this .rowKeyValues = new ArrayList <>(Arrays .asList (rowKey .getValues ()));
369+ this .rowKeyNames = new ArrayList <>(Arrays .asList (rowKey .getColumns ()));
370+
371+ // set row key in table
372+ if (null != tableName ) {
373+ ((ObTableClient ) client )
374+ .addRowKeyElement (tableName , this .rowKeyNames .toArray (new String [0 ]));
375+ }
376+
377+ hasSetPartitionKey = true ;
378+ return (T ) this ;
379+ }
380+
381+ /*
382+ * set the Partition Key of mutation with ColumnValues
383+ */
384+ @ SuppressWarnings ("unchecked" )
385+ public T setPartitionKey (ColumnValue ... partitionKey ) {
386+ if (hasSetRowKey ) {
387+ throw new IllegalArgumentException ("Heap table can not set row key" );
388+ } else if (hasSetPartitionKey ) {
389+ throw new IllegalArgumentException ("Could not set partition key (scan range) twice" );
390+ } else if (null == partitionKey ) {
391+ throw new IllegalArgumentException ("Invalid null partitionKey set into Mutation" );
392+ }
393+
394+ // set partitionKey
395+ this .rowKey = new Row (partitionKey );
396+
397+ // set row key name into client and set rowKey
398+ this .rowKeyValues = new ArrayList <>(Arrays .asList (this .rowKey .getValues ()));
399+ this .rowKeyNames = new ArrayList <>(Arrays .asList (this .rowKey .getColumns ()));
400+
401+ // set row key in table
402+ if (null != tableName ) {
403+ ((ObTableClient ) client )
404+ .addRowKeyElement (tableName , this .rowKeyNames .toArray (new String [0 ]));
405+ }
406+
407+ // renew scan range of QueryAndMutate
408+ if (null != query ) {
409+ query .addScanRange (rowKeyValues .toArray (), rowKeyValues .toArray ());
410+ }
411+
412+ hasSetPartitionKey = true ;
413+ return (T ) this ;
414+ }
415+
416+ /*
417+ * set the Partition Key of mutation with ColumnValues and keep scan range
418+ */
419+ @ SuppressWarnings ("unchecked" )
420+ public T setPartitionKeyOnly (ColumnValue ... partitionKey ) {
421+ if (hasSetRowKey ) {
422+ throw new IllegalArgumentException ("Heap table can not set row key" );
423+ } else if (hasSetPartitionKey ) {
424+ throw new IllegalArgumentException ("Could not set partition key (scan range) twice" );
425+ } else if (null == partitionKey ) {
426+ throw new IllegalArgumentException ("Invalid null partitionKey set into Mutation" );
427+ }
428+
429+ // set partitionKey
430+ this .rowKey = new Row (partitionKey );
431+
432+ // set row key name into client and set rowKey
433+ this .rowKeyValues = new ArrayList <>(Arrays .asList (this .rowKey .getValues ()));
434+ this .rowKeyNames = new ArrayList <>(Arrays .asList (this .rowKey .getColumns ()));
435+
436+ // set row key in table
437+ if (null != tableName ) {
438+ ((ObTableClient ) client )
439+ .addRowKeyElement (tableName , this .rowKeyNames .toArray (new String [0 ]));
440+ }
441+
442+ hasSetPartitionKey = true ;
443+ return (T ) this ;
444+ }
445+
311446 /*
312447 * add filter into mutation (use QueryAndMutate)
313448 */
@@ -436,12 +571,12 @@ public T addScanRange(Object[] start, boolean startEquals, Object[] end, boolean
436571 return (T ) this ;
437572 }
438573
439- static void removeRowkeyFromMutateColval (List <String > columns , List <Object > values ,
440- List <String > rowKeyNames ) {
574+ protected void removeRowkeyFromMutateColval (List <String > columns , List <Object > values ,
575+ List <String > rowKeyNames ) {
441576 if (null == columns || null == rowKeyNames || columns .size () != values .size ()) {
442577 return ;
443578 }
444- for (int i = values .size () - 1 ; i >= 0 ; --i ) {
579+ for (int i = values .size () - 1 ; i >= 0 && hasSetRowKey ; --i ) {
445580 if (rowKeyNames .contains (columns .get (i ))) {
446581 columns .remove (i );
447582 values .remove (i );
0 commit comments