Reputation: 263
I need to execute a conditional update if and only if the value of in-memory counter is greater than that stored in DynamoDB.
Example - Let's say the value of in-memory counter is 30. The value of the counter stored in DynamoDB is 25. In 1 conditional operation, I want to set DynamoDB value to 30 (because 25 is an old value)
Documentation has the following -
Expected allows you to provide an attribute name, and whether or not Amazon DynamoDB should check to see if the attribute value already exists; or if the attribute value exists and has a particular value before changing it.
It clearly states that unless you know the old value, u cannot perform the conditional operation.
Is there a workaround using which I can perform the conditional update on 'greater than'?
Any help is appreciated.
Thanks.
Upvotes: 2
Views: 3905
Reputation: 3614
http://aws.amazon.com/releasenotes/Amazon-DynamoDB/4503109122817643
What you described is natively supported in the release on Apr 24 2014 called "Improved Conditional Expressions". So you don't need workaround anymore.
Upvotes: 3
Reputation: 29
DynamoDB does support other operators for conditional updates. For the case you're talking about here, you would specify a conditional operator on the attribute field you want to evaluate, and the condition "LT" (less than) the value of your in-memory counter. In your case, attribute "myCounterName" is less than 30.
In Java (pseudocode):
ExpectedAttributeValue expectedValue = new ExpectedAttributeValue()
.withComparisonOperator(ComparisonOperator.LT)
.withAttributeValue(new AttributeValue().withN("30");
UpdateItemRequest request = new UpdateItemRequest(...);
request.addExpectedEntry("myCounterName", expectedValue);
dbClient.updateItem(request);
This code would only update that counter if the current counter in DynamoDB is less than the value you specified.
Upvotes: 0
Reputation: 10052
You can't do it with an "atomic operation" as stated in the documentation.
If you don't need full atomic nature, you might implement your own 'transnational' approach or use the one from amazon (Java)
Upvotes: 0