edit-icon download-icon

Condition update

Last Updated: Apr 08, 2018

The conditional update function updates table data only when specified conditions are met. If the conditions are not met, the update fails. Conditional update is applicable to PutRow, UpdateRow, DeleteRow, and BatchWriteRow.

Judgment conditions include the row existence condition and column-based condition.

  • Row existence condition: This condition is divided into three types, IGNORE, EXPECT_EXIST, and EXPECT_NOT_EXIST. When a table needs to be updated, the system first checks the row existence condition. If the row existence condition is not met, the update fails and the system throws an error.

  • Column-based condition: This condition currently supports SingleColumnValueCondition and CompositeColumnValueCondition. These are used to make condition-based judgments based on the value(s) of one or more columns, similar to the conditions used by the Table Store filters.

Conditional update can be used to implement optimistic locking. When a row needs to be updated, the system first obtains the value of a column. For example, we assume Column A has a value of 1. Set the condition Column A = 1, update the row, and set Column A = 2. If the update fails, this means the row has been updated by another client.

Example 1

Construct a SingleColumnValueCondition.

  1. // Set the condition: Col0==0.
  2. SingleColumnValueCondition singleColumnValueCondition = new SingleColumnValueCondition("Col0",
  3. SingleColumnValueCondition.CompareOperator.EQUAL, ColumnValue.fromLong(0));
  4. // If the column Col0 does not exist, the condition is not met.
  5. singleColumnValueCondition.setPassIfMissing(false);
  6. // Only judge the latest version.
  7. singleColumnValueCondition.setLatestVersionsOnly(true);

Example 2

Construct a CompositeColumnValueCondition.

  1. // The condition composite1 is: (Col0 == 0) AND (Col1 > 100).
  2. CompositeColumnValueCondition composite1 = new CompositeColumnValueCondition(CompositeColumnValueCondition.LogicOperator.AND);
  3. SingleColumnValueCondition single1 = new SingleColumnValueCondition("Col0",
  4. SingleColumnValueCondition.CompareOperator.EQUAL, ColumnValue.fromLong(0));
  5. SingleColumnValueCondition single2 = new SingleColumnValueCondition("Col1",
  6. SingleColumnValueCondition.CompareOperator.GREATER_THAN, ColumnValue.fromLong(100));
  7. composite1.addCondition(single1);
  8. composite1.addCondition(single2);
  9. // The condition composite2 is: ( (Col0 == 0) AND (Col1 > 100) ) OR (Col2 <= 10).
  10. CompositeColumnValueCondition composite2 = new CompositeColumnValueCondition(CompositeColumnValueCondition.LogicOperator.OR);
  11. SingleColumnValueCondition single3 = new SingleColumnValueCondition("Col2",
  12. SingleColumnValueCondition.CompareOperator.LESS_EQUAL, ColumnValue.fromLong(10));
  13. composite2.addCondition(composite1);
  14. composite2.addCondition(single3);

Example 3

You can use Condition to implement the optimistic locking feature. The following example shows how to increment a column using Condition.

  1. private static void updateRowWithCondition(SyncClient client, String pkValue) {
  2. // Build primary key.
  3. PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
  4. primaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME, PrimaryKeyValue.fromString(pkValue));
  5. PrimaryKey primaryKey = primaryKeyBuilder.build();
  6. // Read a row.
  7. SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(TABLE_NAME, primaryKey);
  8. criteria.setMaxVersions(1);
  9. GetRowResponse getRowResponse = client.getRow(new GetRowRequest(criteria));
  10. Row row = getRowResponse.getRow();
  11. long col0Value = row.getLatestColumn("Col0").getValue().asLong();
  12. // Set conditional update for Col0, and use column value+1.
  13. RowUpdateChange rowUpdateChange = new RowUpdateChange(TABLE_NAME, primaryKey);
  14. Condition condition = new Condition(RowExistenceExpectation.EXPECT_EXIST);
  15. ColumnCondition columnCondition = new SingleColumnValueCondition("Col0", SingleColumnValueCondition.CompareOperator.EQUAL, ColumnValue.fromLong(col0Value));
  16. condition.setColumnCondition(columnCondition);
  17. rowUpdateChange.setCondition(condition);
  18. rowUpdateChange.put(new Column("Col0", ColumnValue.fromLong(col0Value + 1)));
  19. try {
  20. client.updateRow(new UpdateRowRequest(rowUpdateChange));
  21. } catch (TableStoreException ex) {
  22. System.out.println(ex.toString());
  23. }
  24. }
Thank you! We've received your feedback.