|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.enhanced.dynamodb.extensions; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.UUID; |
| 23 | +import java.util.function.Consumer; |
| 24 | +import software.amazon.awssdk.annotations.SdkPublicApi; |
| 25 | +import software.amazon.awssdk.annotations.ThreadSafe; |
| 26 | +import software.amazon.awssdk.enhanced.dynamodb.AttributeValueType; |
| 27 | +import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension; |
| 28 | +import software.amazon.awssdk.enhanced.dynamodb.DynamoDbExtensionContext; |
| 29 | +import software.amazon.awssdk.enhanced.dynamodb.EnhancedType; |
| 30 | +import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTag; |
| 31 | +import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableMetadata; |
| 32 | +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; |
| 33 | +import software.amazon.awssdk.utils.Validate; |
| 34 | + |
| 35 | +/** |
| 36 | + * This extension facilitates the automatic generation of a unique UUID for any attribute tagged with |
| 37 | + * {@code @DynamoDbAutoGeneratedKey}. The generated value uses {@link UUID#randomUUID()}. |
| 38 | + * <p> |
| 39 | + * Register this extension explicitly when building the {@link software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient}. |
| 40 | + * <p> |
| 41 | + * Example: |
| 42 | + * <pre> |
| 43 | + * DynamoDbEnhancedClient client = DynamoDbEnhancedClient.builder() |
| 44 | + * .dynamoDbClient(lowLevelClient) |
| 45 | + * .extensions(AutoGeneratedKeyExtension.create()) |
| 46 | + * .build(); |
| 47 | + * </pre> |
| 48 | + */ |
| 49 | +@SdkPublicApi |
| 50 | +@ThreadSafe |
| 51 | +public final class AutoGeneratedKeyExtension implements DynamoDbEnhancedClientExtension { |
| 52 | + |
| 53 | + private static final String CUSTOM_METADATA_KEY = |
| 54 | + "software.amazon.awssdk.enhanced.dynamodb.extensions.AutoGeneratedKeyExtension:AutoGeneratedKeyAttribute"; |
| 55 | + |
| 56 | + private static final AutoGeneratedKeyAttribute AUTO_GENERATED_KEY_ATTRIBUTE = new AutoGeneratedKeyAttribute(); |
| 57 | + |
| 58 | + private AutoGeneratedKeyExtension() { |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @return an instance of {@link AutoGeneratedKeyExtension} |
| 63 | + */ |
| 64 | + public static AutoGeneratedKeyExtension create() { |
| 65 | + return new AutoGeneratedKeyExtension(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * If this table has attributes tagged for auto-generation, insert a UUID value into the outgoing item for any such attribute |
| 70 | + * that is currently missing/empty. |
| 71 | + */ |
| 72 | + @Override |
| 73 | + public WriteModification beforeWrite(DynamoDbExtensionContext.BeforeWrite context) { |
| 74 | + Collection<String> taggedAttributes = |
| 75 | + context.tableMetadata() |
| 76 | + .customMetadataObject(CUSTOM_METADATA_KEY, Collection.class) |
| 77 | + .orElse(null); |
| 78 | + |
| 79 | + if (taggedAttributes == null || taggedAttributes.isEmpty()) { |
| 80 | + return WriteModification.builder().build(); |
| 81 | + } |
| 82 | + |
| 83 | + Map<String, AttributeValue> itemToTransform = new HashMap<>(context.items()); |
| 84 | + taggedAttributes.forEach(attr -> insertUuidIfMissing(itemToTransform, attr)); |
| 85 | + |
| 86 | + return WriteModification.builder() |
| 87 | + .transformedItem(Collections.unmodifiableMap(itemToTransform)) |
| 88 | + .build(); |
| 89 | + } |
| 90 | + |
| 91 | + private void insertUuidIfMissing(Map<String, AttributeValue> itemToTransform, String key) { |
| 92 | + AttributeValue existing = itemToTransform.get(key); |
| 93 | + boolean missing = existing == null || existing.s() == null || existing.s().isEmpty(); |
| 94 | + if (missing) { |
| 95 | + itemToTransform.put(key, AttributeValue.builder().s(UUID.randomUUID().toString()).build()); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Static helpers used by the {@code @BeanTableSchemaAttributeTag}-based annotation tag. |
| 101 | + */ |
| 102 | + public static final class AttributeTags { |
| 103 | + private AttributeTags() { |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @return a {@link StaticAttributeTag} that marks the attribute for auto-generated key behavior. |
| 108 | + */ |
| 109 | + public static StaticAttributeTag autoGeneratedKeyAttribute() { |
| 110 | + return AUTO_GENERATED_KEY_ATTRIBUTE; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Validates the Java type and writes table metadata so {@link #beforeWrite} can find the tagged attributes at runtime. |
| 116 | + */ |
| 117 | + private static final class AutoGeneratedKeyAttribute implements StaticAttributeTag { |
| 118 | + |
| 119 | + @Override |
| 120 | + public <R> void validateType(String attributeName, |
| 121 | + EnhancedType<R> type, |
| 122 | + AttributeValueType attributeValueType) { |
| 123 | + |
| 124 | + Validate.notNull(type, "type is null"); |
| 125 | + Validate.notNull(type.rawClass(), "rawClass is null"); |
| 126 | + Validate.notNull(attributeValueType, "attributeValueType is null"); |
| 127 | + |
| 128 | + if (!String.class.equals(type.rawClass())) { |
| 129 | + throw new IllegalArgumentException(String.format( |
| 130 | + "Attribute '%s' of Java type %s is not valid for @DynamoDbAutoGeneratedKey. Only String is supported.", |
| 131 | + attributeName, type.rawClass())); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public Consumer<StaticTableMetadata.Builder> modifyMetadata(String attributeName, |
| 137 | + AttributeValueType attributeValueType) { |
| 138 | + return metadata -> metadata |
| 139 | + .addCustomMetadataObject(CUSTOM_METADATA_KEY, Collections.singleton(attributeName)) |
| 140 | + .markAttributeAsKey(attributeName, attributeValueType); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments