All Products
Search
Document Center

Tablestore:Perform conditional updates

Last Updated:Aug 19, 2024

You can use the conditional update feature to update data in a data table only if the specified conditions are met. If the conditions are not met, the update fails.

Prerequisites

Usage notes

When you call the PutRow, UpdateRow, DeleteRow, or BatchWriteRow operation to update data in a data table, you can specify row existence conditions and column-based conditions to perform conditional updates. The data in the data table is updated only if the conditions are met.

Conditional updates can be performed based on row existence conditions and column-based conditions.

  • The following row existence conditions are supported: IGNORE, EXPECT_EXIST, and EXPECT_NOT_EXIST.

    When you update a data table, Tablestore first checks whether row existence conditions are met. If the row existence conditions are not met, the update fails and an error is reported.

  • Column-based conditions include RelationalCondition and CompositeCondition, which are used to determine whether the conditions are met based on the values of one or more columns.

    Column-based conditions support the following relational operators: =, !=, >, >=, <, and <=. Column-based conditions also support the following logical operators: NOT, AND, and OR. You can specify up to 10 column-based conditions for a conditional update.

    • A RelationalCondition allows you to compare a column with a constant. The comparison between two columns or two constants is not supported.

    • A CompositeCondition consists of multiple RelationalConditions or CompositeConditions. You must specify the logical relationships among the subconditions.

You can perform conditional updates to implement optimistic locking. When you update a row, you must obtain the value of a specific column and specify a row update condition based on the column value. For example, when you update the value of Column A in a row to 2, you must obtain the value of Column A. In this example, the obtained value is 1. Then, you must specify that the row can be updated only if the value of Column A is 1. If the specified condition is met, the update is successful. If the row is updated by another client, the update fails.

Parameters

Parameter

Description

RowExistenceExpectation

When you update a data table, Tablestore first checks whether row existence conditions are met. If the row existence conditions are not met, the update fails and an error is reported.

The following row existence conditions are supported: IGNORE, EXPECT_EXIST, and EXPECT_NOT_EXIST.

  • IGNORE: The existence of the row is ignored. No row existence check is performed.

  • EXPECT_EXIST: The row is expected to exist. If the row exists, the condition is met.

  • EXPECT_NOT_EXIST: The row is expected to not exist. If the row does not exist, the condition is met.

ColumnName

The name of the column.

ColumnValue

The value to be compared with the column.

Operator

The relational operator that is used to compare values. For more information, see ComparatorType.

The following relational operators are supported: =, !=, >, >=, <, and <=.

LogicOperator

The logical operator that is used to combine multiple conditions. For more information, see LogicalOperator.

The following relational operators are supported: NOT, AND, and OR.

The number of required subconditions varies based on logical operators.

  • If the logical operator is NOT, you can add only one subcondition.

  • If the logical operator is AND or OR, you must add at least two subconditions.

PassIfMissing

Specifies whether to the conditional check is passed if a column does not exist in a row. The value of this parameter is of the Boolean data type. The default value is true, which indicates that if the column does not exist in a row, the conditional check is passed and the row meets the update conditions.

If the PassIfMissing parameter is set to false, the conditional check fails when the column does not exist in a row.

LatestVersionsOnly

Specifies whether to use only the value of the latest version when the column has multiple versions of values. The value of this parameter is of the Boolean data type. The default value is true, which indicates that if the column has multiple versions of data, only the value of the latest version is used for comparison.

If the LatestVersionsOnly parameter is set to false, the column values of all versions are used for comparison if the column has multiple versions of data. In this case, the conditional check is passed when one of the versions of values meets the condition.

Example

The following sample code shows an example on how to update data based on column-based conditions. If the value of the col0 column is equal to 5, data is updated. Otherwise, data fails to be updated

    // Specify the primary key of the row. The primary key must be the same as the primary key that is specified in TableMeta when the table is created. 
    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);

    // Call the PutRow operation when other conditions are not configured The operation is expected to succeed. 
    try
    {
        otsClient.PutRow(request);

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

    // If the value of the col0 column is not equal to 5, call the PutRow operation again to overwrite the original value. The operation is expected to succeed. 
    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);
    }

    // If the value of the col0 column is equal to 5, call the PutRow operation again to overwrite the original value. The operation is expected to succeed. 
    try
    {
        // Add a new condition that the value of the col0 column is equal to 5. 
        request.Condition.ColumnCondition = new RelationalCondition("col0",
                                            CompareOperator.EQUAL,
                                            new ColumnValue(5));
        otsClient.PutRow(request);

        Console.WriteLine("Put row succeeded.");
    }
    catch (OTSServerException)
    {
        // OTSServerException is returned 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);
    }