-
Notifications
You must be signed in to change notification settings - Fork 910
DDB Enhanced: Allow custom versioning #6019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
099236c
f0ccb3e
086b769
de4a4f5
9a66e86
6195af7
7cbf420
b787406
b7b79c2
4de6ae4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "feature", | ||
"category": "DynamoDB Enhanced Client", | ||
"contributor": "akiesler", | ||
"description": "Support for Version Starting at 0 with Configurable Increment" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
import software.amazon.awssdk.annotations.SdkPublicApi; | ||
import software.amazon.awssdk.annotations.ThreadSafe; | ||
import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
import software.amazon.awssdk.utils.ToString; | ||
|
||
/** | ||
* High-level representation of a DynamoDB 'expression' that can be used in various situations where the API requires | ||
|
@@ -311,6 +312,20 @@ public int hashCode() { | |
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
// return "Expression{" + | ||
// "expression='" + expression + '\'' + | ||
// ", expressionValues=" + expressionValues + | ||
// ", expressionNames=" + expressionNames + | ||
// '}'; | ||
Comment on lines
+317
to
+321
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete |
||
return ToString.builder("Expression") | ||
.add("expression", expression) | ||
.add("expressionValues", expressionValues) | ||
.add("expressionNames", expressionNames) | ||
.build(); | ||
} | ||
|
||
/** | ||
* A builder for {@link Expression} | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTag; | ||
import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableMetadata; | ||
import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
import software.amazon.awssdk.utils.Validate; | ||
|
||
/** | ||
* This extension implements optimistic locking on record writes by means of a 'record version number' that is used | ||
|
@@ -61,7 +62,18 @@ public final class VersionedRecordExtension implements DynamoDbEnhancedClientExt | |
private static final String CUSTOM_METADATA_KEY = "VersionedRecordExtension:VersionAttribute"; | ||
private static final VersionAttribute VERSION_ATTRIBUTE = new VersionAttribute(); | ||
|
||
private VersionedRecordExtension() { | ||
private final long startAt; | ||
private final long incrementBy; | ||
|
||
private VersionedRecordExtension(Long startAt, Long incrementBy) { | ||
Validate.isNotNegativeOrNull(startAt, "startAt"); | ||
|
||
if (incrementBy != null && incrementBy < 1) { | ||
throw new IllegalArgumentException("incrementBy must be greater than 0."); | ||
} | ||
|
||
this.startAt = startAt != null ? startAt : 0L; | ||
this.incrementBy = incrementBy != null ? incrementBy : 1L; | ||
} | ||
|
||
public static Builder builder() { | ||
|
@@ -75,19 +87,47 @@ private AttributeTags() { | |
public static StaticAttributeTag versionAttribute() { | ||
return VERSION_ATTRIBUTE; | ||
} | ||
|
||
public static StaticAttributeTag versionAttribute(Long startAt, Long incrementBy) { | ||
return new VersionAttribute(startAt, incrementBy); | ||
} | ||
} | ||
|
||
private static class VersionAttribute implements StaticAttributeTag { | ||
private static final class VersionAttribute implements StaticAttributeTag { | ||
private static final String START_AT_METADATA_KEY = "VersionedRecordExtension:StartAt"; | ||
private static final String INCREMENT_BY_METADATA_KEY = "VersionedRecordExtension:IncrementBy"; | ||
|
||
private final Long startAt; | ||
private final Long incrementBy; | ||
|
||
private VersionAttribute() { | ||
this.startAt = null; | ||
this.incrementBy = null; | ||
} | ||
|
||
private VersionAttribute(Long startAt, Long incrementBy) { | ||
this.startAt = startAt; | ||
this.incrementBy = incrementBy; | ||
} | ||
|
||
@Override | ||
public Consumer<StaticTableMetadata.Builder> modifyMetadata(String attributeName, | ||
AttributeValueType attributeValueType) { | ||
if (attributeValueType != AttributeValueType.N) { | ||
throw new IllegalArgumentException(String.format( | ||
"Attribute '%s' of type %s is not a suitable type to be used as a version attribute. Only type 'N' " + | ||
"is supported.", attributeName, attributeValueType.name())); | ||
"is supported.", attributeName, attributeValueType.name())); | ||
} | ||
|
||
Validate.isNotNegativeOrNull(startAt, "startAt"); | ||
|
||
if (incrementBy != null && incrementBy < 1) { | ||
throw new IllegalArgumentException("IncrementBy must be greater than 0."); | ||
} | ||
|
||
return metadata -> metadata.addCustomMetadataObject(CUSTOM_METADATA_KEY, attributeName) | ||
.addCustomMetadataObject(START_AT_METADATA_KEY, startAt) | ||
.addCustomMetadataObject(INCREMENT_BY_METADATA_KEY, incrementBy) | ||
.markAttributeAsKey(attributeName, attributeValueType); | ||
} | ||
} | ||
|
@@ -109,9 +149,18 @@ public WriteModification beforeWrite(DynamoDbExtensionContext.BeforeWrite contex | |
Optional<AttributeValue> existingVersionValue = | ||
Optional.ofNullable(itemToTransform.get(versionAttributeKey.get())); | ||
|
||
if (!existingVersionValue.isPresent() || isNullAttributeValue(existingVersionValue.get())) { | ||
// First version of the record | ||
newVersionValue = AttributeValue.builder().n("1").build(); | ||
Optional<Long> versionStartAtFromAnnotation = context.tableMetadata() | ||
.customMetadataObject(VersionAttribute.START_AT_METADATA_KEY, | ||
Long.class); | ||
|
||
Optional<Long> versionIncrementByFromAnnotation = context.tableMetadata() | ||
.customMetadataObject(VersionAttribute.INCREMENT_BY_METADATA_KEY, | ||
Long.class); | ||
if (isInitialVersion(existingVersionValue, versionStartAtFromAnnotation)) { | ||
long startValue = versionStartAtFromAnnotation.orElse(this.startAt); | ||
long increment = versionIncrementByFromAnnotation.orElse(this.incrementBy); | ||
|
||
newVersionValue = AttributeValue.builder().n(Long.toString(startValue + increment)).build(); | ||
condition = Expression.builder() | ||
.expression(String.format("attribute_not_exists(%s)", attributeKeyRef)) | ||
.expressionNames(Collections.singletonMap(attributeKeyRef, versionAttributeKey.get())) | ||
|
@@ -123,9 +172,12 @@ public WriteModification beforeWrite(DynamoDbExtensionContext.BeforeWrite contex | |
throw new IllegalArgumentException("Version attribute appears to be the wrong type. N is required."); | ||
} | ||
|
||
int existingVersion = Integer.parseInt(existingVersionValue.get().n()); | ||
long existingVersion = Long.parseLong(existingVersionValue.get().n()); | ||
String existingVersionValueKey = VERSIONED_RECORD_EXPRESSION_VALUE_KEY_MAPPER.apply(versionAttributeKey.get()); | ||
newVersionValue = AttributeValue.builder().n(Integer.toString(existingVersion + 1)).build(); | ||
|
||
long increment = versionIncrementByFromAnnotation.orElse(this.incrementBy); | ||
newVersionValue = AttributeValue.builder().n(Long.toString(existingVersion + increment)).build(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't look like the original version handled this, but how do we deal with integer overflow? Originally Can we check how v1's mapper handles this (if at all)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't seem like v1 had any sort of integer overflow protection? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for looking! I'd still like us to add protection for 2.x if possible There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see, I completely missed this. So just to double check, in order to make it backwards compatible, the new behavior should be like the following:
Is that correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zoewangg yes you are correct. |
||
|
||
condition = Expression.builder() | ||
.expression(String.format("%s = %s", attributeKeyRef, existingVersionValueKey)) | ||
.expressionNames(Collections.singletonMap(attributeKeyRef, versionAttributeKey.get())) | ||
|
@@ -142,13 +194,55 @@ public WriteModification beforeWrite(DynamoDbExtensionContext.BeforeWrite contex | |
.build(); | ||
} | ||
|
||
private boolean isInitialVersion(Optional<AttributeValue> existingVersionValue, Optional<Long> versionStartAtFromAnnotation) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use of Optional as method parameter is a bit code smell. https://github.com/aws/aws-sdk-java-v2/blob/master/docs/design/UseOfOptional.md |
||
if (!existingVersionValue.isPresent() || isNullAttributeValue(existingVersionValue.get())) { | ||
return true; | ||
} | ||
|
||
AttributeValue value = existingVersionValue.get(); | ||
if (value.n() != null) { | ||
long currentVersion = Long.parseLong(value.n()); | ||
return (versionStartAtFromAnnotation.isPresent() && currentVersion == versionStartAtFromAnnotation.get()) | ||
|| currentVersion == this.startAt; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@NotThreadSafe | ||
public static final class Builder { | ||
private Long startAt; | ||
private Long incrementBy; | ||
|
||
private Builder() { | ||
} | ||
|
||
/** | ||
* Sets the startAt used to compare if a record is the initial version of a record. | ||
* Default value - {@code 0}. | ||
* | ||
* @param startAt | ||
* @return the builder instance | ||
*/ | ||
public Builder startAt(Long startAt) { | ||
this.startAt = startAt; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the amount to increment the version by with each subsequent update. | ||
* Default value - {@code 1}. | ||
* | ||
* @param incrementBy | ||
* @return the builder instance | ||
*/ | ||
public Builder incrementBy(Long incrementBy) { | ||
this.incrementBy = incrementBy; | ||
return this; | ||
} | ||
|
||
public VersionedRecordExtension build() { | ||
return new VersionedRecordExtension(); | ||
return new VersionedRecordExtension(this.startAt, this.incrementBy); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.