-
Notifications
You must be signed in to change notification settings - Fork 977
Eliminate lambda indirection overhead in DynamoDB Enhanced Client #6591
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
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 |
|---|---|---|
|
|
@@ -27,33 +27,40 @@ | |
| import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
|
|
||
| @SdkInternalApi | ||
| public final class ResolvedImmutableAttribute<T, B> { | ||
| public final class ResolvedImmutableAttribute<T, B, R> { | ||
| private final String attributeName; | ||
| private final Function<T, AttributeValue> getAttributeMethod; | ||
| private final BiConsumer<B, AttributeValue> updateBuilderMethod; | ||
| private final StaticTableMetadata tableMetadata; | ||
| private final AttributeConverter attributeConverter; | ||
| private final Function<T, R> getter; | ||
| private final AttributeType<R> attributeType; | ||
|
|
||
|
|
||
| private ResolvedImmutableAttribute(String attributeName, | ||
| Function<T, AttributeValue> getAttributeMethod, | ||
| BiConsumer<B, AttributeValue> updateBuilderMethod, | ||
| StaticTableMetadata tableMetadata, | ||
| AttributeConverter attributeConverter) { | ||
| AttributeConverter attributeConverter, | ||
|
Check warning on line 42 in services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/internal/mapper/ResolvedImmutableAttribute.java
|
||
| Function<T, R> getter, | ||
| AttributeType<R> attributeType) { | ||
| this.attributeName = attributeName; | ||
| this.getAttributeMethod = getAttributeMethod; | ||
| this.updateBuilderMethod = updateBuilderMethod; | ||
| this.tableMetadata = tableMetadata; | ||
| this.attributeConverter = attributeConverter; | ||
| this.getter = getter; | ||
| this.attributeType = attributeType; | ||
| } | ||
|
|
||
| AttributeValue getAttributeValue(T item) { | ||
|
Contributor
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. Should this be private? |
||
| R value = getter.apply(item); | ||
| return value == null ? nullAttributeValue() | ||
| : attributeType.objectToAttributeValue(value); | ||
| } | ||
|
|
||
| public static <T, B, R> ResolvedImmutableAttribute<T, B> create(ImmutableAttribute<T, B, R> immutableAttribute, | ||
| public static <T, B, R> ResolvedImmutableAttribute<T, B, R> create(ImmutableAttribute<T, B, R> immutableAttribute, | ||
| AttributeConverter<R> attributeConverter) { | ||
|
|
||
| AttributeType<R> attributeType = StaticAttributeType.create(attributeConverter); | ||
| Function<T, AttributeValue> getAttributeValueWithTransform = item -> { | ||
| R value = immutableAttribute.getter().apply(item); | ||
| return value == null ? nullAttributeValue() : attributeType.objectToAttributeValue(value); | ||
| }; | ||
| Function<T, R> getter = immutableAttribute.getter(); | ||
|
|
||
| // When setting a value on the java object, do not explicitly set nulls as this can cause an NPE to be thrown | ||
| // if the target attribute type is a primitive. | ||
|
|
@@ -78,35 +85,35 @@ | |
| tag.modifyMetadata(immutableAttribute.name(), attributeType.attributeValueType()).accept(tableMetadataBuilder); | ||
| }); | ||
| return new ResolvedImmutableAttribute<>(immutableAttribute.name(), | ||
| getAttributeValueWithTransform, | ||
| updateBuilderWithTransform, | ||
| tableMetadataBuilder.build(), | ||
| attributeConverter); | ||
| attributeConverter, | ||
| getter, | ||
| attributeType); | ||
| } | ||
|
|
||
| public <T1, B1> ResolvedImmutableAttribute<T1, B1> transform( | ||
| public <T1, B1> ResolvedImmutableAttribute<T1, B1, R> transform( | ||
| Function<T1, T> transformItem, | ||
| Function<B1, B> transformBuilder) { | ||
|
|
||
| return new ResolvedImmutableAttribute<>( | ||
| attributeName, | ||
| (item, value) -> updateBuilderMethod.accept(transformBuilder.apply(item), value), | ||
| tableMetadata, | ||
| attributeConverter, | ||
| item -> { | ||
| T otherItem = transformItem.apply(item); | ||
|
|
||
| // If the containing object is null don't attempt to read attributes from it | ||
| return otherItem == null ? | ||
| nullAttributeValue() : getAttributeMethod.apply(otherItem); | ||
| return otherItem == null ? null : getter.apply(otherItem); | ||
| }, | ||
| (item, value) -> updateBuilderMethod.accept(transformBuilder.apply(item), value), | ||
| tableMetadata, attributeConverter); | ||
| attributeType); | ||
| } | ||
|
|
||
| public String attributeName() { | ||
| return attributeName; | ||
| } | ||
|
|
||
| public Function<T, AttributeValue> attributeGetterMethod() { | ||
| return getAttributeMethod; | ||
| return this::getAttributeValue; | ||
| } | ||
|
|
||
| public BiConsumer<B, AttributeValue> updateItemMethod() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Three type parameters starts to feel like a lot to keep track of with just single letter type names. We should at least documment in comments what these are.