-
Notifications
You must be signed in to change notification settings - Fork 914
Support AutoGeneratedTimestamp and UpdateBehavior annotation in nested objects #6109
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "feature", | ||
"category": "Amazon DynamoDB Enhanced Client", | ||
"contributor": "", | ||
"description": "Added support for @DynamoDbAutoGeneratedTimestampAttribute and @DynamoDbUpdateBehavior on attributes within nested objects. The @DynamoDbUpdateBehavior annotation will only take effect for nested attributes when using IgnoreNullsMode.SCALAR_ONLY." | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,21 @@ | |
|
||
package software.amazon.awssdk.enhanced.dynamodb.extensions; | ||
|
||
import static software.amazon.awssdk.enhanced.dynamodb.extensions.utility.NestedRecordUtils.getTableSchemaForListElement; | ||
import static software.amazon.awssdk.enhanced.dynamodb.extensions.utility.NestedRecordUtils.reconstructCompositeKey; | ||
import static software.amazon.awssdk.enhanced.dynamodb.extensions.utility.NestedRecordUtils.resolveSchemasPerPath; | ||
import static software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.getNestedSchema; | ||
|
||
import java.time.Clock; | ||
import java.time.Instant; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Collectors; | ||
import software.amazon.awssdk.annotations.NotThreadSafe; | ||
import software.amazon.awssdk.annotations.SdkPublicApi; | ||
import software.amazon.awssdk.annotations.ThreadSafe; | ||
|
@@ -30,6 +38,7 @@ | |
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension; | ||
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbExtensionContext; | ||
import software.amazon.awssdk.enhanced.dynamodb.EnhancedType; | ||
import software.amazon.awssdk.enhanced.dynamodb.TableSchema; | ||
import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTag; | ||
import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableMetadata; | ||
import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
|
@@ -64,6 +73,10 @@ | |
* <p> | ||
* Every time a new update of the record is successfully written to the database, the timestamp at which it was modified will | ||
* be automatically updated. This extension applies the conversions as defined in the attribute convertor. | ||
* The implementation handles both flattened nested parameters (identified by keys separated with | ||
* {@code "_NESTED_ATTR_UPDATE_"}) and entire nested maps or lists, ensuring consistent behavior across both representations. | ||
* If a nested object or list is {@code null}, no timestamp values will be generated for any of its annotated fields. | ||
* The same timestamp value is used for both top-level attributes and all applicable nested fields. | ||
*/ | ||
@SdkPublicApi | ||
@ThreadSafe | ||
|
@@ -126,26 +139,105 @@ public static AutoGeneratedTimestampRecordExtension create() { | |
*/ | ||
@Override | ||
public WriteModification beforeWrite(DynamoDbExtensionContext.BeforeWrite context) { | ||
Map<String, AttributeValue> itemToTransform = new HashMap<>(context.items()); | ||
|
||
Map<String, AttributeValue> updatedItems = new HashMap<>(); | ||
Instant currentInstant = clock.instant(); | ||
|
||
Collection<String> customMetadataObject = context.tableMetadata() | ||
.customMetadataObject(CUSTOM_METADATA_KEY, Collection.class).orElse(null); | ||
itemToTransform.forEach((key, value) -> { | ||
if (value.hasM() && value.m() != null) { | ||
Optional<? extends TableSchema<?>> nestedSchema = getNestedSchema(context.tableSchema(), key); | ||
if (nestedSchema.isPresent()) { | ||
Map<String, AttributeValue> processed = processNestedObject(value.m(), nestedSchema.get(), currentInstant); | ||
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. Is there a possible performance degradation for deeply nested records? 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. The performance impact introduced by applying the AutogeneratedTimestamp and UpdateBehavior extensions recursively is directly proportional to the complexity and depth of the nested schema hierarchy. In other words, the more deeply nested the record structure, the more recursive processing is involved, which can marginally increase processing time. |
||
updatedItems.put(key, AttributeValue.builder().m(processed).build()); | ||
} | ||
} else if (value.hasL() && !value.l().isEmpty() && value.l().get(0).hasM()) { | ||
TableSchema<?> elementListSchema = getTableSchemaForListElement(context.tableSchema(), key); | ||
|
||
List<AttributeValue> updatedList = value.l() | ||
.stream() | ||
.map(listItem -> listItem.hasM() ? | ||
AttributeValue.builder() | ||
.m(processNestedObject(listItem.m(), | ||
elementListSchema, | ||
currentInstant)) | ||
.build() : listItem) | ||
.collect(Collectors.toList()); | ||
updatedItems.put(key, AttributeValue.builder().l(updatedList).build()); | ||
} | ||
}); | ||
|
||
Map<String, TableSchema<?>> stringTableSchemaMap = resolveSchemasPerPath(itemToTransform, context.tableSchema()); | ||
|
||
stringTableSchemaMap.forEach((path, schema) -> { | ||
Collection<String> customMetadataObject = schema.tableMetadata() | ||
.customMetadataObject(CUSTOM_METADATA_KEY, Collection.class) | ||
.orElse(null); | ||
|
||
if (customMetadataObject != null) { | ||
customMetadataObject.forEach( | ||
key -> insertTimestampInItemToTransform(updatedItems, reconstructCompositeKey(path, key), | ||
schema.converterForAttribute(key), currentInstant)); | ||
} | ||
}); | ||
|
||
if (customMetadataObject == null) { | ||
if (updatedItems.isEmpty()) { | ||
return WriteModification.builder().build(); | ||
} | ||
Map<String, AttributeValue> itemToTransform = new HashMap<>(context.items()); | ||
customMetadataObject.forEach( | ||
key -> insertTimestampInItemToTransform(itemToTransform, key, | ||
context.tableSchema().converterForAttribute(key))); | ||
|
||
itemToTransform.putAll(updatedItems); | ||
|
||
return WriteModification.builder() | ||
.transformedItem(Collections.unmodifiableMap(itemToTransform)) | ||
.build(); | ||
} | ||
|
||
private Map<String, AttributeValue> processNestedObject(Map<String, AttributeValue> nestedMap, TableSchema<?> nestedSchema, | ||
Instant currentInstant) { | ||
Map<String, AttributeValue> updatedNestedMap = new HashMap<>(nestedMap); | ||
Collection<String> customMetadataObject = nestedSchema.tableMetadata() | ||
.customMetadataObject(CUSTOM_METADATA_KEY, Collection.class).orElse(null); | ||
|
||
if (customMetadataObject != null) { | ||
customMetadataObject.forEach( | ||
key -> insertTimestampInItemToTransform(updatedNestedMap, String.valueOf(key), | ||
nestedSchema.converterForAttribute(key), currentInstant)); | ||
} | ||
|
||
nestedMap.forEach((nestedKey, nestedValue) -> { | ||
if (nestedValue.hasM()) { | ||
updatedNestedMap.put(nestedKey, | ||
AttributeValue.builder().m(processNestedObject(nestedValue.m(), nestedSchema, | ||
currentInstant)).build()); | ||
} else if (nestedValue.hasL() && !nestedValue.l().isEmpty() | ||
&& nestedValue.l().get(0).hasM()) { | ||
try { | ||
TableSchema<?> listElementSchema = TableSchema.fromClass( | ||
Class.forName(nestedSchema.converterForAttribute(nestedKey) | ||
.type().rawClassParameters().get(0).rawClass().getName())); | ||
List<AttributeValue> updatedList = nestedValue | ||
.l() | ||
.stream() | ||
.map(listItem -> listItem.hasM() ? | ||
AttributeValue.builder() | ||
.m(processNestedObject(listItem.m(), | ||
listElementSchema, | ||
currentInstant)).build() : listItem) | ||
.collect(Collectors.toList()); | ||
updatedNestedMap.put(nestedKey, AttributeValue.builder().l(updatedList).build()); | ||
} catch (ClassNotFoundException e) { | ||
throw new IllegalArgumentException("Class not found for field name: " + nestedKey, e); | ||
} | ||
} | ||
}); | ||
return updatedNestedMap; | ||
} | ||
|
||
private void insertTimestampInItemToTransform(Map<String, AttributeValue> itemToTransform, | ||
String key, | ||
AttributeConverter converter) { | ||
itemToTransform.put(key, converter.transformFrom(clock.instant())); | ||
AttributeConverter converter, | ||
Instant instant) { | ||
itemToTransform.put(key, converter.transformFrom(instant)); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.enhanced.dynamodb.extensions.utility; | ||
|
||
import static software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.getNestedSchema; | ||
import static software.amazon.awssdk.enhanced.dynamodb.internal.operations.UpdateItemOperation.NESTED_OBJECT_UPDATE; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.regex.Pattern; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.enhanced.dynamodb.TableSchema; | ||
import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
|
||
@SdkInternalApi | ||
public final class NestedRecordUtils { | ||
|
||
private static final Pattern NESTED_OBJECT_PATTERN = Pattern.compile(NESTED_OBJECT_UPDATE); | ||
|
||
private NestedRecordUtils() { | ||
} | ||
|
||
/** | ||
* Resolves and returns the {@link TableSchema} for the element type of a list attribute from the provided root schema. | ||
* <p> | ||
* This method is useful when dealing with lists of nested objects in a DynamoDB-enhanced table schema, | ||
* particularly in scenarios where the list is part of a flattened nested structure. | ||
* <p> | ||
* If the provided key contains the nested object delimiter (e.g., {@code _NESTED_ATTR_UPDATE_}), the method traverses | ||
* the nested hierarchy based on that path to locate the correct schema for the target attribute. | ||
* Otherwise, it directly resolves the list element type from the root schema using reflection. | ||
* | ||
* @param rootSchema The root {@link TableSchema} representing the top-level entity. | ||
* @param key The key representing the list attribute, either flat or nested (using a delimiter). | ||
* @return The {@link TableSchema} representing the list element type of the specified attribute. | ||
* @throws IllegalArgumentException If the list element class cannot be found via reflection. | ||
*/ | ||
public static TableSchema<?> getTableSchemaForListElement(TableSchema<?> rootSchema, String key) { | ||
TableSchema<?> listElementSchema; | ||
try { | ||
if (!key.contains(NESTED_OBJECT_UPDATE)) { | ||
listElementSchema = TableSchema.fromClass( | ||
Class.forName(rootSchema.converterForAttribute(key).type().rawClassParameters().get(0).rawClass().getName())); | ||
} else { | ||
String[] parts = NESTED_OBJECT_PATTERN.split(key); | ||
TableSchema<?> currentSchema = rootSchema; | ||
|
||
for (int i = 0; i < parts.length - 1; i++) { | ||
Optional<? extends TableSchema<?>> nestedSchema = getNestedSchema(currentSchema, parts[i]); | ||
if (nestedSchema.isPresent()) { | ||
currentSchema = nestedSchema.get(); | ||
} | ||
} | ||
String attributeName = parts[parts.length - 1]; | ||
listElementSchema = TableSchema.fromClass( | ||
Class.forName(currentSchema.converterForAttribute(attributeName) | ||
.type().rawClassParameters().get(0).rawClass().getName())); | ||
} | ||
} catch (ClassNotFoundException e) { | ||
throw new IllegalArgumentException("Class not found for field name: " + key, e); | ||
} | ||
return listElementSchema; | ||
} | ||
|
||
/** | ||
* Traverses the attribute keys representing flattened nested structures and resolves the corresponding | ||
* {@link TableSchema} for each nested path. | ||
* <p> | ||
* The method constructs a mapping between each unique nested path (represented as dot-delimited strings) | ||
* and the corresponding {@link TableSchema} object derived from the root schema. It supports resolving schemas | ||
* for arbitrarily deep nesting, using the {@code _NESTED_ATTR_UPDATE_} pattern as a path delimiter. | ||
* <p> | ||
* This is typically used in update or transformation flows where fields from nested objects are represented | ||
* as flattened keys in the attribute map (e.g., {@code parent_NESTED_ATTR_UPDATE_child}). | ||
* | ||
* @param attributesToSet A map of flattened attribute keys to values, where keys may represent paths to nested attributes. | ||
* @param rootSchema The root {@link TableSchema} of the top-level entity. | ||
* @return A map where the key is the nested path (e.g., {@code "parent.child"}) and the value is the {@link TableSchema} | ||
* corresponding to that level in the object hierarchy. | ||
*/ | ||
public static Map<String, TableSchema<?>> resolveSchemasPerPath(Map<String, AttributeValue> attributesToSet, | ||
TableSchema<?> rootSchema) { | ||
Map<String, TableSchema<?>> schemaMap = new HashMap<>(); | ||
schemaMap.put("", rootSchema); | ||
|
||
for (String key : attributesToSet.keySet()) { | ||
String[] parts = NESTED_OBJECT_PATTERN.split(key); | ||
|
||
StringBuilder pathBuilder = new StringBuilder(); | ||
TableSchema<?> currentSchema = rootSchema; | ||
|
||
for (int i = 0; i < parts.length - 1; i++) { | ||
if (pathBuilder.length() > 0) { | ||
pathBuilder.append("."); | ||
} | ||
pathBuilder.append(parts[i]); | ||
|
||
String path = pathBuilder.toString(); | ||
|
||
if (!schemaMap.containsKey(path)) { | ||
Optional<? extends TableSchema<?>> nestedSchema = getNestedSchema(currentSchema, parts[i]); | ||
if (nestedSchema.isPresent()) { | ||
schemaMap.put(path, nestedSchema.get()); | ||
currentSchema = nestedSchema.get(); | ||
} | ||
} else { | ||
currentSchema = schemaMap.get(path); | ||
} | ||
} | ||
} | ||
return schemaMap; | ||
} | ||
|
||
public static String reconstructCompositeKey(String path, String attributeName) { | ||
if (path == null || path.isEmpty()) { | ||
return attributeName; | ||
} | ||
return String.join(NESTED_OBJECT_UPDATE, path.split("\\.")) | ||
+ NESTED_OBJECT_UPDATE + attributeName; | ||
} | ||
} |
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.
Should we clarify here the difference in null value handling?