|
15 | 15 |
|
16 | 16 | package software.amazon.awssdk.enhanced.dynamodb.internal.operations;
|
17 | 17 |
|
18 |
| -import static software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.isNullAttributeValue; |
19 | 18 | import static software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.readAndTransformSingleItem;
|
20 | 19 | import static software.amazon.awssdk.enhanced.dynamodb.internal.update.UpdateExpressionUtils.operationExpression;
|
21 | 20 | import static software.amazon.awssdk.utils.CollectionUtils.filterMap;
|
22 | 21 |
|
23 | 22 | import java.util.Collection;
|
24 |
| -import java.util.HashMap; |
25 | 23 | import java.util.List;
|
26 | 24 | import java.util.Map;
|
27 | 25 | import java.util.Optional;
|
|
55 | 53 | public class UpdateItemOperation<T>
|
56 | 54 | implements TableOperation<T, UpdateItemRequest, UpdateItemResponse, UpdateItemEnhancedResponse<T>>,
|
57 | 55 | TransactableWriteOperation<T> {
|
58 |
| - |
59 |
| - public static final String NESTED_OBJECT_UPDATE = "_NESTED_ATTR_UPDATE_"; |
| 56 | + |
60 | 57 | private final Either<UpdateItemEnhancedRequest<T>, TransactUpdateItemEnhancedRequest<T>> request;
|
61 | 58 |
|
62 | 59 | private UpdateItemOperation(UpdateItemEnhancedRequest<T> request) {
|
@@ -92,14 +89,8 @@ public UpdateItemRequest generateRequest(TableSchema<T> tableSchema,
|
92 | 89 | Boolean ignoreNulls = request.map(r -> Optional.ofNullable(r.ignoreNulls()),
|
93 | 90 | r -> Optional.ofNullable(r.ignoreNulls()))
|
94 | 91 | .orElse(null);
|
95 |
| - |
96 |
| - Map<String, AttributeValue> itemMapImmutable = tableSchema.itemToMap(item, Boolean.TRUE.equals(ignoreNulls)); |
97 |
| - |
98 |
| - // If ignoreNulls is set to true, check for nested params to be updated |
99 |
| - // If needed, Transform itemMap for it to be able to handle them. |
100 |
| - Map<String, AttributeValue> itemMap = Boolean.TRUE.equals(ignoreNulls) ? |
101 |
| - transformItemToMapForUpdateExpression(itemMapImmutable) : itemMapImmutable; |
102 |
| - |
| 92 | + |
| 93 | + Map<String, AttributeValue> itemMap = tableSchema.itemToMap(item, Boolean.TRUE.equals(ignoreNulls)); |
103 | 94 | TableMetadata tableMetadata = tableSchema.tableMetadata();
|
104 | 95 |
|
105 | 96 | WriteModification transformation =
|
@@ -150,58 +141,6 @@ public UpdateItemRequest generateRequest(TableSchema<T> tableSchema,
|
150 | 141 |
|
151 | 142 | return requestBuilder.build();
|
152 | 143 | }
|
153 |
| - |
154 |
| - /** |
155 |
| - * Method checks if a nested object parameter requires an update |
156 |
| - * If so flattens out nested params separated by "_NESTED_ATTR_UPDATE_" |
157 |
| - * this is consumed by @link EnhancedClientUtils to form the appropriate UpdateExpression |
158 |
| - */ |
159 |
| - public Map<String, AttributeValue> transformItemToMapForUpdateExpression(Map<String, AttributeValue> itemToMap) { |
160 |
| - |
161 |
| - Map<String, AttributeValue> nestedAttributes = new HashMap<>(); |
162 |
| - |
163 |
| - itemToMap.forEach((key, value) -> { |
164 |
| - if (value.hasM() && isNotEmptyMap(value.m())) { |
165 |
| - nestedAttributes.put(key, value); |
166 |
| - } |
167 |
| - }); |
168 |
| - |
169 |
| - if (!nestedAttributes.isEmpty()) { |
170 |
| - Map<String, AttributeValue> itemToMapMutable = new HashMap<>(itemToMap); |
171 |
| - nestedAttributes.forEach((key, value) -> { |
172 |
| - itemToMapMutable.remove(key); |
173 |
| - nestedItemToMap(itemToMapMutable, key, value); |
174 |
| - }); |
175 |
| - return itemToMapMutable; |
176 |
| - } |
177 |
| - |
178 |
| - return itemToMap; |
179 |
| - } |
180 |
| - |
181 |
| - private Map<String, AttributeValue> nestedItemToMap(Map<String, AttributeValue> itemToMap, |
182 |
| - String key, |
183 |
| - AttributeValue attributeValue) { |
184 |
| - attributeValue.m().forEach((mapKey, mapValue) -> { |
185 |
| - String nestedAttributeKey = key + NESTED_OBJECT_UPDATE + mapKey; |
186 |
| - if (attributeValueNonNullOrShouldWriteNull(mapValue)) { |
187 |
| - if (mapValue.hasM()) { |
188 |
| - nestedItemToMap(itemToMap, nestedAttributeKey, mapValue); |
189 |
| - } else { |
190 |
| - itemToMap.put(nestedAttributeKey, mapValue); |
191 |
| - } |
192 |
| - } |
193 |
| - }); |
194 |
| - return itemToMap; |
195 |
| - } |
196 |
| - |
197 |
| - private boolean isNotEmptyMap(Map<String, AttributeValue> map) { |
198 |
| - return !map.isEmpty() && map.values().stream() |
199 |
| - .anyMatch(this::attributeValueNonNullOrShouldWriteNull); |
200 |
| - } |
201 |
| - |
202 |
| - private boolean attributeValueNonNullOrShouldWriteNull(AttributeValue attributeValue) { |
203 |
| - return !isNullAttributeValue(attributeValue); |
204 |
| - } |
205 | 144 |
|
206 | 145 | @Override
|
207 | 146 | public UpdateItemEnhancedResponse<T> transformResponse(UpdateItemResponse response,
|
|
0 commit comments