Use the atomic counter feature

更新时间:
复制 MD 格式

Use the atomic counter feature to increment or decrement an INTEGER column in a single, thread-safe operation — no read-modify-write cycle needed.

Prerequisites

Usage notes

  • You can implement atomic counters only on INTEGER columns.

  • If a column that is specified as an atomic counter does not exist before you write data, the default value of the column is 0. If a column that is specified as an atomic counter is not an INTEGER column, an OTSParameterInvalid error occurs.

  • You can update an atomic counter by using a positive or negative number, but you must avoid an integer overflow. If an integer overflow occurs, an OTSParameterInvalid error is returned.

  • By default, the value of an atomic counter is not returned in the response to an update row request. You can specify that the updated value of an atomic counter is returned.

  • You cannot specify a column as an atomic counter and update the column in a single update request. For example, if you set Column A to an atomic counter, you cannot perform other operations such as overwrite and delete operations on the column at the same time.

  • You can perform multiple update operations on the same row by sending a BatchWriteRow request. However, if you perform an atomic counter operation on a row, you can perform only one update operation on the row in a BatchWriteRow request.

  • Only the value of the latest version of an atomic counter can be updated. You cannot update the value of a specified version of an atomic counter. After an update operation is complete, a new version of data is inserted into the atomic counter in the row.

API operations

Atomic counter operations are exposed through the RowUpdateChange class.

Operation

Description

RowUpdateChange Increment(Column)

Increments or decrements the value of an INTEGER column by the specified amount.

List<String> ReturnColumnNames

Specifies which columns to return after the atomic counter operation.

ReturnType

Specifies the return type for the atomic counter result.

Parameters

Parameter

Description

TableName

The name of the data table.

ColumnName

The column to apply the atomic counter operation to. Only INTEGER columns are supported.

Value

The amount to add or subtract from the column value. Use a negative number to decrement.

ReturnColumnNames

The columns to return after the operation.

ReturnType

Set to ReturnType.RT_AFTER_MODIFY to return the updated column value after the operation.

Example

The following example increments an INTEGER column and returns the updated value.

public static void Increment(int incrementValue)
{
    Console.WriteLine("Start set increment column...");
    OTSClient otsClient = Config.GetClient();

    // Specify the primary key of the row. The primary key must be consistent with the primary key specified in TableMeta when you create the table. 
    PrimaryKey primaryKey = new PrimaryKey
    {
        { Pk1, new ColumnValue(0) },
        { Pk2, new ColumnValue("abc") }
    };
    RowUpdateChange rowUpdateChange = new RowUpdateChange(TableName, primaryKey); // Specify the name of the data table. 
    // Set the ReturnType parameter to ReturnType.RT_AFTER_MODIFY to return the values of the column on which atomic counter operations are performed. 
    rowUpdateChange.ReturnType = ReturnType.RT_AFTER_MODIFY;
    rowUpdateChange.ReturnColumnNames = new List<string>() { IncrementCol};
    // Specify the column on which atomic counter operations are performed. The column value starts from 0 and increases by the number specified by the incrementValue parameter. 
    rowUpdateChange.Increment(new Column(IncrementCol, new ColumnValue(incrementValue)));

    UpdateRowRequest updateRowRequest = new UpdateRowRequest(rowUpdateChange);

    var response = otsClient.UpdateRow(updateRowRequest);
    Console.WriteLine("set Increment column succeed Increment result:" + response.Row.GetColumns()[0].Value);
}