Class AtomicCounterExtension
java.lang.Object
software.amazon.awssdk.enhanced.dynamodb.extensions.AtomicCounterExtension
- All Implemented Interfaces:
DynamoDbEnhancedClientExtension
This extension enables atomic counter attributes to be changed in DynamoDb by creating instructions for modifying
an existing value or setting a start value. The extension is loaded by default when you instantiate a
DynamoDbEnhancedClient
and only needs to be added to the client if you are adding custom extensions to the client.
To utilize atomic counters, first create a field in your model that will be used to store the counter.
This class field should of type Long
and you need to tag it as an atomic counter:
- If you are using the
BeanTableSchema
, you should annotate withDynamoDbAtomicCounter
- If you are using the
StaticTableSchema
, use theStaticAttributeTags.atomicCounter()
static attribute tag.
Every time a new update of the record is successfully written to the database, the counter will be updated automatically.
By default, the counter starts at 0 and increments by 1 for each update. The tags provide the capability of adjusting
the counter start and increment/decrement values such as described in DynamoDbAtomicCounter
.
Example 1: Using a bean based table schema
@DynamoDbBean
public class CounterRecord {
@DynamoDbAtomicCounter(delta = 5, startValue = 10)
public Long getCustomCounter() {
return customCounter;
}
}
Example 2: Using a static table schema
private static final StaticTableSchema<AtomicCounterItem> ITEM_MAPPER =
StaticTableSchema.builder(AtomicCounterItem.class)
.newItemSupplier(AtomicCounterItem::new)
.addAttribute(Long.class, a -> a.name("defaultCounter")
.getter(AtomicCounterItem::getDefaultCounter)
.setter(AtomicCounterItem::setDefaultCounter)
.addTag(StaticAttributeTags.atomicCounter()))
.build();
NOTES:
- When using putItem, the counter will be reset to its start value.
- The extension will remove any existing occurrences of the atomic counter attributes from the record during an updateItem operation. Manually editing attributes marked as atomic counters will have NO EFFECT.
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionThis hook is called just before an operation is going to write data to the database.builder()
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension
afterRead
-
Method Details
-
builder
-
beforeWrite
Description copied from interface:DynamoDbEnhancedClientExtension
This hook is called just before an operation is going to write data to the database. The extension that implements this method can choose to transform the item itself, or add a condition to the write operation or both.- Specified by:
beforeWrite
in interfaceDynamoDbEnhancedClientExtension
- Parameters:
context
- TheDynamoDbExtensionContext.BeforeWrite
context containing the state of the execution.- Returns:
- WriteModification contains an update expression representing the counters.
-