All Products
Search
Document Center

Tablestore:Use the atomic counter feature

Last Updated:Jan 02, 2024

If you want to implement a counter for your online application, you can use the atomic counter feature. To use this feature, set a column as an atomic counter and perform atomic counter operations on the column.

Prerequisites

Limits

  • You can implement atomic counters only on INTEGER columns.

  • By default, if a column that is specified as an atomic counter does not exist, the value of the column is 0 before you write data. 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 increased value of an atomic counter is returned.

  • You cannot specify a column as an atomic counter and update the column in a single request. If Attribute Column A is set to an atomic counter, you cannot perform other operations such as overwrite and delete operations on the attribute column A.

  • You can perform multiple update operations on the same row by using 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 is increased. You cannot increase the value of a specified version of an atomic counter. After you update a row, a new version of data is inserted into the atomic counter in the row.

Syntax

/**
 * Update a row of data. 
 * @api
 * @param [] $request The request parameters. 
 * @return [] The response. 
 * @throws OTSClientException The exception that is thrown if a parameter error occurs or the Tablestore server returns a verification error. 
 * @throws OTSServerException The exception that is thrown if the Tablestore server returns an error. 
 */
public function updateRow(array $request);         

You can call the updateRow operation to perform operations on atomic counters. The following table describes the operations.

Operation

Description

update_of_attribute_columns

Specifies INCREMENT as the update type to increment or decrement a column value by a specific integer. Configuration format:

'update_of_attribute_columns'=> array(
    'INCREMENT' => array (     
        array('<column_name>', <value>, ColumnTypeConst::CONST_INTEGER), 
    ),
)

return_content

Returns the updated column value. You must specify the name of the column whose value you want to change and set the return_type parameter to ReturnTypeConst::CONST_AFTER_MODIFY. Configuration format:

'return_content' => array(
    'return_type' => ReturnTypeConst::CONST_AFTER_MODIFY,
    'return_column_names' => array('<column_name>')
 )

Parameters

Parameter

Description

table_name

The name of the table.

column_name

The name of the column on which the atomic counter operation is to be performed. You can specify a column only of the INTEGER type.

value

The increment or decrement in the column value.

return_content

The returned information about the column on which the atomic counter operation is performed. This parameter contains the return_type and return_column_names parameters.

  • return_type: Set this parameter to ReturnTypeConst::CONST_AFTER_MODIFY to return the updated value of the column on which the atomic counter operation is performed.

  • return_column_names: returns the name of the column on which the atomic counter operation is performed.

Example

The following sample code provides an example on how to update the value of a column of the INTEGER type when data is written and read the updated value.

$request = array (
    // Specify the name of the table. 
    'table_name' => 'MyTable',
    // Set the row existence condition to RowExistenceExpectationConst::CONST_IGNORE, which specifies that the existence of rows is not checked. 
    'condition' => RowExistenceExpectationConst::CONST_IGNORE,
    // Specify the primary key. The number and types of primary key columns that you specify must be the same as the actual number and types of primary key columns in the table. 
    'primary_key' => array ( 
        // The name of the first primary key column is PK0, the data type is INTEGER, and the column value is 123. 
        array('PK0', 123),
        // The name of the second primary key column is PK1, the data type is STRING, and the column value is 'inc'. 
        array('PK1', 'inc')
    ),
    // The attribute column whose value is to be updated. Three operation types are supported: PUT, DELETE, and DELETE_ALL. 
    'update_of_attribute_columns'=> array(
        'INCREMENT' => array (     
            // Specify the column on which the atomic counter operation is to be performed. The column name is attr0, the increment in the column value is 1, and the data type is INTEGER. 
            array('attr0', 1, ColumnTypeConst::CONST_INTEGER), 
        ),
        'PUT' => array(
            array('attr1', 1, ColumnTypeConst::CONST_INTEGER),
        )
    ),
    // Return the column information after the atomic counter operation is performed. 
    'return_content' => array(
        'return_type' => ReturnTypeConst::CONST_AFTER_MODIFY,
        'return_column_names' => array('attr0')
    )
);
$response = $otsClient->updateRow ($request);
print json_encode ($response);