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 include row existence conditions and column-based conditions.
  • Row existence conditions include:
    • IGNORE
    • EXPECT_EXIST
    • EXPECT_NOT_EXIST
    Note When you update a row, the system first checks the 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 Table Store filters. Column-based conditions include:
    • SingleColumnCondition
    • CompositeColumnCondition

Conditional updates can be used to implement optimistic locking. When you update a row, the system first obtains the value of a column. Assume that Column A has a value of 1. Use a conditional update with the condition of Column A = 1 and attempt to update the row and to change the condition to Column A = 2. If the update fails, the row has been updated by another client.

Examples

    // Specify the primary keys of the row. These primary keys must be consistent with the primary keys specified in TableMeta during table creation.
    PrimaryKey primaryKey = new PrimaryKey();
    primaryKey.Add("pk0", new ColumnValue(0));
    primaryKey.Add("pk1", new ColumnValue("abc"));

    // Specify the attribute columns of the row.
    AttributeColumns attribute = new AttributeColumns();
    attribute.Add("col0", new ColumnValue(0));
    attribute.Add("col1", new ColumnValue("a"));
    attribute.Add("col2", new ColumnValue(true));

    PutRowRequest request = new PutRowRequest(tableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);

    // Implement PutRow without specifying conditions. The operation succeeds.
    try
    {
        otsClient.PutRow(request);

        Console.WriteLine("Put row succeeded.");
    } catch (Exception ex)
    {
        Console.WriteLine("Put row failed. error:{0}", ex.Message);
    }

    // When the value of col0 is not 5, implement PutRow again to overwrite the original value. The operation succeeds.
    try
    {
        request.Condition.ColumnCondition = new RelationalCondition("col0",
                                            CompareOperator.NOT_EQUAL,
                                            new ColumnValue(5));
        otsClient.PutRow(request);

        Console.WriteLine("Put row succeeded.");
    } catch (Exception ex)
    {
        Console.WriteLine("Put row failed. error:{0}", ex.Message);
    }

    // When the value of col0 is 5, implement PutRow again to overwrite the original value. The operation fails.
    try
    {
        // A new condition: The value of col0 is 5.
        request.Condition.ColumnCondition = new RelationalCondition("col0",
                                            CompareOperator.EQUAL,
                                            new ColumnValue(5));
        otsClient.PutRow(request);

        Console.WriteLine("Put row succeeded.");
    }
    catch (OTSServerException)
    {
        // OTSServerException is thrown because the condition is not met.
        Console.WriteLine("Put row failed  because condition check failed. but expected");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Put row failed. error:{0}", ex.Message);
    }