|
| 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.document; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.LinkedHashSet; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | +import java.util.stream.Collectors; |
| 27 | +import software.amazon.awssdk.annotations.NotThreadSafe; |
| 28 | +import software.amazon.awssdk.annotations.SdkPublicApi; |
| 29 | +import software.amazon.awssdk.enhanced.dynamodb.AttributeConverter; |
| 30 | +import software.amazon.awssdk.enhanced.dynamodb.AttributeConverterProvider; |
| 31 | +import software.amazon.awssdk.enhanced.dynamodb.AttributeValueType; |
| 32 | +import software.amazon.awssdk.enhanced.dynamodb.DefaultAttributeConverterProvider; |
| 33 | +import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; |
| 34 | +import software.amazon.awssdk.enhanced.dynamodb.EnhancedType; |
| 35 | +import software.amazon.awssdk.enhanced.dynamodb.TableMetadata; |
| 36 | +import software.amazon.awssdk.enhanced.dynamodb.TableSchema; |
| 37 | +import software.amazon.awssdk.enhanced.dynamodb.internal.converter.ConverterProviderResolver; |
| 38 | +import software.amazon.awssdk.enhanced.dynamodb.internal.document.DefaultEnhancedDocument; |
| 39 | +import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticImmutableTableSchema; |
| 40 | +import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableMetadata; |
| 41 | +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; |
| 42 | + |
| 43 | + |
| 44 | +/** |
| 45 | + * Implementation of {@link TableSchema} that builds a table schema based on DynamoDB Items. |
| 46 | + * <p> |
| 47 | + * In Amazon DynamoDB, an item is a collection of attributes. Each attribute has a name and a value. An attribute value can be a |
| 48 | + * scalar, a set, or a document type |
| 49 | + * <p> |
| 50 | + * A DocumentTableSchema is used to create a {@link DynamoDbTable} which provides read and writes access to DynamoDB table as |
| 51 | + * {@link EnhancedDocument}. |
| 52 | + * <p> DocumentTableSchema specifying primaryKey, sortKey and a customAttributeConverter can be created as below |
| 53 | + * {@snippet : |
| 54 | + * DocumentTableSchema documentTableSchema = DocumentTableSchema.builder() |
| 55 | + * .primaryKey("sampleHashKey", AttributeValueType.S) |
| 56 | + * .sortKey("sampleSortKey", AttributeValueType.S) |
| 57 | + * .attributeConverterProviders(customAttributeConverter, AttributeConverterProvider.defaultProvider()) |
| 58 | + * .build(); |
| 59 | + *} |
| 60 | + * <p> DocumentTableSchema can also be created without specifying primaryKey and sortKey in which cases the |
| 61 | + * {@link TableMetadata} of DocumentTableSchema will error if we try to access attributes from metaData. Also if |
| 62 | + * attributeConverterProviders are not provided then {@link DefaultAttributeConverterProvider} will be used |
| 63 | + * {@snippet : |
| 64 | + * DocumentTableSchema documentTableSchema = DocumentTableSchema.builder().build(); |
| 65 | + *} |
| 66 | + * |
| 67 | + * @see <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html" target="_top">Working |
| 68 | + * with items and attributes</a> |
| 69 | + */ |
| 70 | +@SdkPublicApi |
| 71 | +public final class DocumentTableSchema implements TableSchema<EnhancedDocument> { |
| 72 | + private final TableMetadata tableMetadata; |
| 73 | + private final List<AttributeConverterProvider> attributeConverterProviders; |
| 74 | + |
| 75 | + private DocumentTableSchema(Builder builder) { |
| 76 | + this.attributeConverterProviders = builder.attributeConverterProviders; |
| 77 | + this.tableMetadata = builder.staticTableMetaDataBuilder.build(); |
| 78 | + } |
| 79 | + |
| 80 | + public static Builder builder() { |
| 81 | + return new Builder(); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public EnhancedDocument mapToItem(Map<String, AttributeValue> attributeMap) { |
| 86 | + if (attributeMap == null) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + DefaultEnhancedDocument.DefaultBuilder builder = |
| 90 | + (DefaultEnhancedDocument.DefaultBuilder) DefaultEnhancedDocument.builder(); |
| 91 | + attributeMap.forEach(builder::putObject); |
| 92 | + return builder.attributeConverterProviders(attributeConverterProviders) |
| 93 | + .build(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * {@inheritDoc} |
| 98 | + * |
| 99 | + * This flag does not have significance for the Document API, unlike Java objects where the default value of an undefined |
| 100 | + * Object is null.In contrast to mapped classes, where a schema is present, the DocumentSchema is unaware of the entire |
| 101 | + * schema.Therefore, if an attribute is not present, it signifies that it is null, and there is no need to handle it in a |
| 102 | + * separate way.However, if the user explicitly wants to nullify certain attributes, then the user needs to set those |
| 103 | + * attributes as null in the Document that needs to be updated. |
| 104 | + * |
| 105 | + */ |
| 106 | + @Override |
| 107 | + public Map<String, AttributeValue> itemToMap(EnhancedDocument item, boolean ignoreNulls) { |
| 108 | + if (item == null) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + List<AttributeConverterProvider> providers = mergeAttributeConverterProviders(item); |
| 112 | + return item.toBuilder().attributeConverterProviders(providers).build().toMap(); |
| 113 | + } |
| 114 | + |
| 115 | + private List<AttributeConverterProvider> mergeAttributeConverterProviders(EnhancedDocument item) { |
| 116 | + if (item.attributeConverterProviders() != null && !item.attributeConverterProviders().isEmpty()) { |
| 117 | + Set<AttributeConverterProvider> providers = new LinkedHashSet<>(); |
| 118 | + providers.addAll(item.attributeConverterProviders()); |
| 119 | + providers.addAll(attributeConverterProviders); |
| 120 | + return providers.stream().collect(Collectors.toList()); |
| 121 | + } |
| 122 | + return attributeConverterProviders; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public Map<String, AttributeValue> itemToMap(EnhancedDocument item, Collection<String> attributes) { |
| 127 | + if (item.toMap() == null) { |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + List<AttributeConverterProvider> providers = mergeAttributeConverterProviders(item); |
| 132 | + return item.toBuilder().attributeConverterProviders(providers).build().toMap().entrySet() |
| 133 | + .stream() |
| 134 | + .filter(entry -> attributes.contains(entry.getKey())) |
| 135 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public AttributeValue attributeValue(EnhancedDocument item, String attributeName) { |
| 140 | + if (item == null || item.toMap() == null) { |
| 141 | + return null; |
| 142 | + } |
| 143 | + List<AttributeConverterProvider> providers = mergeAttributeConverterProviders(item); |
| 144 | + return item.toBuilder() |
| 145 | + .attributeConverterProviders(providers) |
| 146 | + .build() |
| 147 | + .toMap() |
| 148 | + .get(attributeName); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public TableMetadata tableMetadata() { |
| 153 | + return tableMetadata; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public EnhancedType<EnhancedDocument> itemType() { |
| 158 | + return EnhancedType.of(EnhancedDocument.class); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public List<String> attributeNames() { |
| 163 | + return tableMetadata.keyAttributes().stream().map(key -> key.name()).collect(Collectors.toList()); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public boolean isAbstract() { |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + @NotThreadSafe |
| 172 | + public static final class Builder { |
| 173 | + |
| 174 | + private final StaticTableMetadata.Builder staticTableMetaDataBuilder = StaticTableMetadata.builder(); |
| 175 | + |
| 176 | + /** |
| 177 | + * By Default the defaultConverterProvider is used for converting AttributeValue to primitive types. |
| 178 | + */ |
| 179 | + private List<AttributeConverterProvider> attributeConverterProviders = |
| 180 | + Collections.singletonList(ConverterProviderResolver.defaultConverterProvider()); |
| 181 | + |
| 182 | + /** |
| 183 | + * Adds information about a partition key associated with a specific index. |
| 184 | + * |
| 185 | + * @param indexName the name of the index to associate the partition key with |
| 186 | + * @param attributeName the name of the attribute that represents the partition key |
| 187 | + * @param attributeValueType the {@link AttributeValueType} of the partition key |
| 188 | + * @throws IllegalArgumentException if a partition key has already been defined for this index |
| 189 | + */ |
| 190 | + public Builder addIndexPartitionKey(String indexName, String attributeName, AttributeValueType attributeValueType) { |
| 191 | + staticTableMetaDataBuilder.addIndexPartitionKey(indexName, attributeName, attributeValueType); |
| 192 | + return this; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Adds information about a sort key associated with a specific index. |
| 197 | + * |
| 198 | + * @param indexName the name of the index to associate the sort key with |
| 199 | + * @param attributeName the name of the attribute that represents the sort key |
| 200 | + * @param attributeValueType the {@link AttributeValueType} of the sort key |
| 201 | + * @throws IllegalArgumentException if a sort key has already been defined for this index |
| 202 | + */ |
| 203 | + public Builder addIndexSortKey(String indexName, String attributeName, AttributeValueType attributeValueType) { |
| 204 | + staticTableMetaDataBuilder.addIndexSortKey(indexName, attributeName, attributeValueType); |
| 205 | + return this; |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Specifies the {@link AttributeConverterProvider}s to use with the table schema. The list of attribute converter |
| 210 | + * providers must provide {@link AttributeConverter}s for Custom types. The attribute converter providers will be loaded |
| 211 | + * in the strict order they are supplied here. |
| 212 | + * <p> |
| 213 | + * By default, {@link DefaultAttributeConverterProvider} will be used, and it will provide standard converters for most |
| 214 | + * primitive and common Java types. Configuring this will override the default behavior, so it is recommended to always |
| 215 | + * append `DefaultAttributeConverterProvider` when you configure the custom attribute converter providers. |
| 216 | + * <p> |
| 217 | + * {@snippet : |
| 218 | + * builder.attributeConverterProviders(customAttributeConverter, AttributeConverterProvider.defaultProvider()); |
| 219 | + *} |
| 220 | + * |
| 221 | + * @param attributeConverterProviders a list of attribute converter providers to use with the table schema |
| 222 | + */ |
| 223 | + public Builder attributeConverterProviders(AttributeConverterProvider... attributeConverterProviders) { |
| 224 | + this.attributeConverterProviders = Arrays.asList(attributeConverterProviders); |
| 225 | + return this; |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Specifies the {@link AttributeConverterProvider}s to use with the table schema. The list of attribute converter |
| 230 | + * providers must provide {@link AttributeConverter}s for all types used in the schema. The attribute converter providers |
| 231 | + * will be loaded in the strict order they are supplied here. |
| 232 | + * <p> |
| 233 | + * By default, {@link DefaultAttributeConverterProvider} will be used, and it will provide standard converters for most |
| 234 | + * primitive and common Java types. Configuring this will override the default behavior, so it is recommended to always |
| 235 | + * append `DefaultAttributeConverterProvider` when you configure the custom attribute converter providers. |
| 236 | + * <p> |
| 237 | + * {@snippet : |
| 238 | + * List<AttributeConverterProvider> providers = new ArrayList<>( customAttributeConverter, |
| 239 | + * AttributeConverterProvider.defaultProvider()); |
| 240 | + * builder.attributeConverterProviders(providers); |
| 241 | + *} |
| 242 | + * |
| 243 | + * @param attributeConverterProviders a list of attribute converter providers to use with the table schema |
| 244 | + */ |
| 245 | + public Builder attributeConverterProviders(List<AttributeConverterProvider> attributeConverterProviders) { |
| 246 | + this.attributeConverterProviders = new ArrayList<>(attributeConverterProviders); |
| 247 | + return this; |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * Builds a {@link StaticImmutableTableSchema} based on the values this builder has been configured with |
| 252 | + */ |
| 253 | + public DocumentTableSchema build() { |
| 254 | + return new DocumentTableSchema(this); |
| 255 | + } |
| 256 | + } |
| 257 | +} |
0 commit comments