Use atomic counters
Use the Tablestore SDK for Go to atomically increment or decrement an INTEGER attribute column at the row level and return the updated value in the same request.
Prerequisites
Install the Tablestore SDK for Go and initialize the client.
Description
Call IncrementColumn to perform an atomic counter operation on an INTEGER attribute column. A positive value increments the column, and a negative value decrements it. The server guarantees row-level atomicity and writes a new data version. To return the updated value in the same request, call AppendIncrementColumnToReturn to specify the column and call SetReturnIncrementValue to set the return type.
func (rowchange *UpdateRowChange) IncrementColumn(columnName string, value int64)
func (rowchange *UpdateRowChange) AppendIncrementColumnToReturn(name string)
func (rowchange *UpdateRowChange) SetReturnIncrementValue()
The following sample increments the score column of row1 in the example_table table by 10 and returns the updated value.
primaryKey := &tablestore.PrimaryKey{}
primaryKey.AddPrimaryKeyColumn("id", "row1")
change := &tablestore.UpdateRowChange{
TableName: "example_table",
PrimaryKey: primaryKey,
}
change.IncrementColumn("score", int64(10))
change.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
change.AppendIncrementColumnToReturn("score")
change.SetReturnIncrementValue()
response, err := client.UpdateRow(&tablestore.UpdateRowRequest{UpdateRowChange: change})
if err != nil {
log.Fatal(err)
}
fmt.Println(response.Columns[0].Value)
Parameters
UpdateRowChange contains the following parameters related to atomic counters.
|
Name |
Type |
Description |
|
TableName (required) |
|
The table name. |
|
PrimaryKey (required) |
|
The complete primary key of the target row. |
|
Columns (required) |
|
The attribute column operations. Call |
|
Condition (required) |
|
The update condition. For more information, see Use conditional updates. |
|
ReturnType (optional) |
|
The return type. Calling |
|
ColumnNamesToReturn (optional) |
|
The atomic counter columns whose updated values are returned. Call |
|
TransactionId (optional) |
|
The local transaction ID. Specify this parameter only for an atomic counter operation in a local transaction. |
Response
UpdateRowResponse contains the following business field related to atomic counters.
|
Field |
Type |
Description |
|
|
|
When |
Limits
Only INTEGER attribute columns are supported. If the target column does not exist, the initial value is 0. If the column exists but is not an INTEGER column, a parameter error is returned.
The increment can be positive or negative, but the result must not overflow.
An atomic counter operates only on the latest version and does not support a specified timestamp. The operation writes a new version.
In the same request, an atomic counter operation and another operation, such as an overwrite or deletion, cannot be performed on the same column.
In a
BatchWriteRowrequest, a row that contains an atomic counter operation can appear only once.
A network timeout or system error may cause the client to retry and execute an atomic counter operation more than once. If you must precisely control the final column value, use conditional updates to implement optimistic locking.