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 row existence and column-based conditions.
  • Row existence conditions include:
    • IGNORE
    • EXPECT_EXIST
    • EXPECT_NOT_EXIST
    Note When you update a row, the system first 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. Example: 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.

Examples

  • Example 1
    Update a row based on the specified primary key. If the specified row exists, the update succeeds. Otherwise, the update fails.
        updateRowRequest := new(tablestore.UpdateRowRequest)
        updateRowChange := new(tablestore.UpdateRowChange)
        updateRowChange.TableName = tableName
        updatePk := new(tablestore.PrimaryKey)
        updatePk.AddPrimaryKeyColumn("pk1", "pk1value1")
        updatePk.AddPrimaryKeyColumn("pk2", int64(2))
        updatePk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
        updateRowChange.PrimaryKey = updatePk
        updateRowChange.DeleteColumn("col1")            // Delete col1.
        updateRowChange.PutColumn("col2", int64(77))    // Add col2 with a value of 77.
        updateRowChange.PutColumn("col4", "newcol3")    // Add col4 with a value of newcol3.
        // The specified row is expected to exist.
        updateRowChange.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
        updateRowRequest.UpdateRowChange = updateRowChange
        _, err := client.UpdateRow(updateRowRequest)
  • Example 2
    Delete a row based on the specified primary key. If the specified row exists and the value of col2 is 3, the update succeeds. Otherwise, the update fails.
        deleteRowReq := new(tablestore.DeleteRowRequest)
        deleteRowReq.DeleteRowChange = new(tablestore.DeleteRowChange)
        deleteRowReq.DeleteRowChange.TableName = tableName
        deletePk := new(tablestore.PrimaryKey)
        deletePk.AddPrimaryKeyColumn("pk1", "pk1value1")
        deletePk.AddPrimaryKeyColumn("pk2", int64(2))
        deletePk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
        deleteRowReq.DeleteRowChange.PrimaryKey = deletePk
    
        // The row is expected to exist.
        deleteRowReq.DeleteRowChange.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
        // The value of col2 is expected to be 3.
        clCondition1 := tablestore.NewSingleColumnCondition("col2", tablestore.CT_EQUAL, int64(3))
        deleteRowReq.DeleteRowChange.SetColumnCondition(clCondition1)
        _, err := client.DeleteRow(deleteRowReq)