Conditional updates are implemented only when specific conditions are met. Conditional updates can be used for put_row, update_row, delete_row, and batch_write_row operations.

Parameters

Judgment conditions for conditional updates can be either row existence or column-based conditions.
  • Row existence conditions include:
    • IGNORE
    • EXPECT_EXIST
    • EXPECT_NOT_EXIST
    Note When you update a row, the system checks for row existence conditions. If the row existence conditions are not met, the operation fails and an error is returned.
  • Column-based conditions are used to make judgments based on the values of one or more columns. These conditions are similar to the conditions used by the Tablestore filters. Column-based conditions include:
    • SingleColumnCondition
    • CompositeColumnCondition

Conditional updates are used to implement optimistic locking. When you update a row, the system first obtains the value of the specified column. Assume that Column A has a value of 1. Set the condition of Column A = 1, and then attempt to update the row value and condition both to Column A = 2. If the update fails, the row has already been updated by another client.

Example

Update a row based on the specified primary key. The update succeeds if the following conditions are met. Otherwise, the update fails.
  • The specified row exists.
  • The value of the age column in the specified row is 20.
    primary_key = [('gid',1), ('uid',"101")]
    update_of_attribute_columns = {
        'PUT' : [('name','David'), ('address','Hongkong')],
        'DELETE' : [('address', None, 1488436949003)],
        'DELETE_ALL' : [('mobile'), ('age')],
        'INCREMENT' : [('counter', -1)]
    }
    row = Row(primary_key, update_of_attribute_columns)

    # Specify Condition: The update succeeds only when the following two conditions are met. Otherwise, the update fails.
    #...(1) The specified row exists.
    #...(2) The value of the age column in the specified row is 20. 
    condition = Condition(RowExistenceExpectation.EXPECT_EXIST, SingleColumnCondition("age", 20, ComparatorType.EQUAL)) # update row only when this row is exist
    consumed, return_row = client.update_row(table_name, row, condition)