Skip to content

Commit 18bb268

Browse files
committed
Fixed edge case in isInitialVersion, fixed javadoc and typos
1 parent b8e0ad9 commit 18bb268

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/extensions/VersionedRecordExtension.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public Consumer<StaticTableMetadata.Builder> modifyMetadata(String attributeName
122122
Validate.isNotNegativeOrNull(startAt, "startAt");
123123

124124
if (incrementBy != null && incrementBy < 1) {
125-
throw new IllegalArgumentException("IncrementBy must be greater than 0.");
125+
throw new IllegalArgumentException("incrementBy must be greater than 0.");
126126
}
127127

128128
return metadata -> metadata.addCustomMetadataObject(CUSTOM_METADATA_KEY, attributeName)
@@ -149,11 +149,11 @@ public WriteModification beforeWrite(DynamoDbExtensionContext.BeforeWrite contex
149149

150150
AttributeValue existingVersionValue = itemToTransform.get(versionAttributeKey.get());
151151
Long versionStartAtFromAnnotation = context.tableMetadata()
152-
.customMetadataObject(VersionAttribute.START_AT_METADATA_KEY,
153-
Long.class).orElse(this.startAt);
152+
.customMetadataObject(VersionAttribute.START_AT_METADATA_KEY, Long.class)
153+
.orElse(this.startAt);
154154
Long versionIncrementByFromAnnotation = context.tableMetadata()
155-
.customMetadataObject(VersionAttribute.INCREMENT_BY_METADATA_KEY,
156-
Long.class).orElse(this.incrementBy);
155+
.customMetadataObject(VersionAttribute.INCREMENT_BY_METADATA_KEY, Long.class)
156+
.orElse(this.incrementBy);
157157

158158

159159
if (isInitialVersion(existingVersionValue, versionStartAtFromAnnotation)) {
@@ -177,8 +177,8 @@ public WriteModification beforeWrite(DynamoDbExtensionContext.BeforeWrite contex
177177
long increment = versionIncrementByFromAnnotation;
178178

179179
/*
180-
since the new incrementBy and StartAt functionality can now accept any positive number, though unlikely
181-
to happen in a rela life scenario, we should add overflow protection.
180+
Since the new incrementBy and StartAt functionality can now accept any positive number, though unlikely
181+
to happen in a real life scenario, we should add overflow protection.
182182
*/
183183
if (existingVersion > Long.MAX_VALUE - increment) {
184184
throw new IllegalStateException(
@@ -211,8 +211,9 @@ private boolean isInitialVersion(AttributeValue existingVersionValue, Long versi
211211

212212
if (existingVersionValue.n() != null) {
213213
long currentVersion = Long.parseLong(existingVersionValue.n());
214-
return (versionStartAtFromAnnotation != null && currentVersion == versionStartAtFromAnnotation)
215-
|| currentVersion == this.startAt;
214+
// If annotation value is present, use it, otherwise fall back to the extension's value
215+
Long effectiveStartAt = versionStartAtFromAnnotation != null ? versionStartAtFromAnnotation : this.startAt;
216+
return currentVersion == effectiveStartAt;
216217
}
217218

218219
return false;
@@ -230,7 +231,7 @@ private Builder() {
230231
* Sets the startAt used to compare if a record is the initial version of a record.
231232
* Default value - {@code 0}.
232233
*
233-
* @param startAt
234+
* @param startAt the starting value for version comparison, must not be negative
234235
* @return the builder instance
235236
*/
236237
public Builder startAt(Long startAt) {
@@ -242,7 +243,7 @@ public Builder startAt(Long startAt) {
242243
* Sets the amount to increment the version by with each subsequent update.
243244
* Default value - {@code 1}.
244245
*
245-
* @param incrementBy
246+
* @param incrementBy the amount to increment the version by, must be greater than 0
246247
* @return the builder instance
247248
*/
248249
public Builder incrementBy(Long incrementBy) {

services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/extensions/VersionedRecordExtensionTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,34 @@ public void versionOverflowThrowsException() {
606606
.build());
607607
}
608608

609+
@Test
610+
public void isInitialVersion_shouldPrioritizeAnnotationValueOverBuilderValue() {
611+
VersionedRecordExtension recordExtension = VersionedRecordExtension.builder()
612+
.startAt(5L)
613+
.build();
614+
615+
// FakeVersionedThroughAnnotationItem value for startAt is 3, which would conflict with builder value of 5.
616+
FakeVersionedThroughAnnotationItem item = new FakeVersionedThroughAnnotationItem();
617+
item.setId(UUID.randomUUID().toString());
618+
619+
item.setVersion(5L);
620+
621+
TableSchema<FakeVersionedThroughAnnotationItem> schema =
622+
TableSchema.fromBean(FakeVersionedThroughAnnotationItem.class);
623+
624+
Map<String, AttributeValue> inputMap = new HashMap<>(schema.itemToMap(item, true));
625+
626+
WriteModification result =
627+
recordExtension.beforeWrite(DefaultDynamoDbExtensionContext
628+
.builder()
629+
.items(inputMap)
630+
.tableMetadata(schema.tableMetadata())
631+
.operationContext(PRIMARY_CONTEXT).build());
632+
633+
assertThat(result.additionalConditionalExpression().expression(),
634+
is("#AMZN_MAPPED_version = :old_version_value"));
635+
}
636+
609637
public static Stream<Arguments> customIncrementForExistingVersionValues() {
610638
return Stream.of(
611639
Arguments.of(0L, 1L, 5L, "6"),

0 commit comments

Comments
 (0)