Source:
public <T extends Item> T update(final T item) { final ItemConfiguration itemConfiguration = getItemConfiguration(item.getClass()); if (item.getVersion() == null) { return create(item); } final Collection<PropertyDescriptor> updatedUniqueConstraintPropertyDescriptors = new HashSet<>(); T previousItem = null; if (!itemConfiguration.uniqueConstraints().isEmpty()) { final ItemId itemId = itemConfiguration.getItemId(item); previousItem = readWithOnlyUniqueConstraintProperties(itemId, itemConfiguration); final Collection<UniqueConstraint> updatedUniqueConstraints = getUpdatedUniqueConstraints(item, previousItem, itemConfiguration); for (final UniqueConstraint uniqueConstraint : updatedUniqueConstraints) { updatedUniqueConstraintPropertyDescriptors.add(uniqueConstraint.propertyDescriptor()); } createUniqueConstraintIndexes(item, itemConfiguration, updatedUniqueConstraintPropertyDescriptors); } final long newVersion = item.getVersion() + 1; final Map<String, AttributeValue> attributeMap = getAttributeMap(item, itemConfiguration, newVersion); final Map<String, ExpectedAttributeValue> expectedResults = new HashMap<>(); expectedResults.put(VERSION_ATTRIBUTE, new ExpectedAttributeValue(new AttributeValue().withN(String.valueOf(item.getVersion())))); final String tableName = databaseSchemaHolder.schemaName() + "." + itemConfiguration.tableName(); final PutItemRequest itemRequest = new PutItemRequest().withTableName(tableName).withItem(attributeMap) .withExpected(expectedResults); try { amazonDynamoDbClient.putItem(itemRequest); if (!updatedUniqueConstraintPropertyDescriptors.isEmpty()) { deleteUniqueConstraintIndexes(previousItem, itemConfiguration, updatedUniqueConstraintPropertyDescriptors); } } catch (final ConditionalCheckFailedException conditionalCheckFailedException) { if (!updatedUniqueConstraintPropertyDescriptors.isEmpty()) { deleteUniqueConstraintIndexes(item, itemConfiguration, updatedUniqueConstraintPropertyDescriptors); } throw new OptimisticLockException("Conflicting write detected while updating item"); } catch (final AmazonServiceException amazonServiceException) { if (!updatedUniqueConstraintPropertyDescriptors.isEmpty()) { deleteUniqueConstraintIndexes(item, itemConfiguration, updatedUniqueConstraintPropertyDescriptors); } throw new PersistenceResourceFailureException("Failure while attempting DynamoDb Put (update item)", amazonServiceException); } item.setVersion(newVersion); return item; }

Target:
public <T extends Item> T update(final T item) { final ItemConfiguration itemConfiguration = getItemConfiguration(item.getClass()); if (item.getVersion() == null) { return create(item); } final Collection<PropertyDescriptor> updatedUniqueConstraintPropertyDescriptors = new HashSet<>(); T previousItem = null; if (!itemConfiguration.uniqueConstraints().isEmpty()) { final ItemId itemId = itemConfiguration.getItemId(item); previousItem = readWithOnlyUniqueConstraintProperties(itemId, itemConfiguration); final Collection<UniqueConstraint> updatedUniqueConstraints = getUpdatedUniqueConstraints(item, previousItem, itemConfiguration); for (final UniqueConstraint uniqueConstraint : updatedUniqueConstraints) { updatedUniqueConstraintPropertyDescriptors.add(uniqueConstraint.propertyDescriptor()); } createUniqueConstraintIndexes(item, itemConfiguration, updatedUniqueConstraintPropertyDescriptors); } final long newVersion = item.getVersion() + 1; final Map<String, AttributeValue> attributeMap = getAttributeMap(item, itemConfiguration, newVersion); final Map<String, ExpectedAttributeValue> expectedResults = new HashMap<>(); expectedResults.put(VERSION_ATTRIBUTE, new ExpectedAttributeValue(new AttributeValue().withN(String.valueOf(item.getVersion())))); final String tableName = databaseSchemaHolder.schemaName() + "." + itemConfiguration.tableName(); final PutItemRequest itemRequest = new PutItemRequest().withTableName(tableName).withItem(attributeMap) .withExpected(expectedResults); boolean itemRequestSucceeded = false; try { amazonDynamoDbClient.putItem(itemRequest); itemRequestSucceeded = true; } catch (final ConditionalCheckFailedException conditionalCheckFailedException) { throw new OptimisticLockException("Conflicting write detected while updating item"); } catch (final AmazonServiceException amazonServiceException) { throw new PersistenceResourceFailureException("Failure while attempting DynamoDb Put (update item)", amazonServiceException); } finally { if (!itemRequestSucceeded) { try { deleteUniqueConstraintIndexes(item, itemConfiguration, updatedUniqueConstraintPropertyDescriptors); } catch (final Exception deleteUniqueConstraintIndexesException) { logger.error(deleteUniqueConstraintIndexesException.getMessage(), deleteUniqueConstraintIndexesException); } } } deleteUniqueConstraintIndexes(previousItem, itemConfiguration, updatedUniqueConstraintPropertyDescriptors); item.setVersion(newVersion); return item; }