Example usage for com.amazonaws.services.simpledb.model DeleteAttributesRequest DeleteAttributesRequest

List of usage examples for com.amazonaws.services.simpledb.model DeleteAttributesRequest DeleteAttributesRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.simpledb.model DeleteAttributesRequest DeleteAttributesRequest.

Prototype

public DeleteAttributesRequest(String domainName, String itemName, java.util.List<Attribute> attributes,
        UpdateCondition expected) 

Source Link

Document

Constructs a new DeleteAttributesRequest object.

Usage

From source file:org.grails.datastore.mapping.simpledb.util.SimpleDBTemplateImpl.java

License:Apache License

private void deleteAttributesVersionedInternal(String domainName, String id, List<Attribute> attributes,
        String expectedVersion, PersistentEntity persistentEntity, int attempt) throws DataAccessException {
    // If attribute list is empty AWS api will erase the whole item.
    // Do not do that, otherwise all the callers will have to check for empty list before calling
    if (!attributes.isEmpty()) {
        DeleteAttributesRequest request = new DeleteAttributesRequest(domainName, id, attributes,
                getOptimisticVersionCondition(expectedVersion));
        try {//from ww  w  .j a  v a2s  . c om
            sdb.deleteAttributes(request);
        } catch (AmazonServiceException e) {
            if (SimpleDBUtil.AWS_ERR_CODE_CONDITIONAL_CHECK_FAILED.equals(e.getErrorCode())) {
                throw new OptimisticLockingException(persistentEntity, id);
            } else if (SimpleDBUtil.AWS_ERR_CODE_NO_SUCH_DOMAIN.equals(e.getErrorCode())) {
                throw new IllegalArgumentException("no such domain: " + domainName, e);
            } else if (SimpleDBUtil.AWS_ERR_CODE_SERVICE_UNAVAILABLE.equals(e.getErrorCode())) {
                //retry after a small pause
                SimpleDBUtil.sleepBeforeRetry(attempt);
                attempt++;
                deleteAttributesVersionedInternal(domainName, id, attributes, expectedVersion, persistentEntity,
                        attempt);
            } else {
                throw e;
            }
        }
    }
}

From source file:squash.booking.lambdas.core.OptimisticPersister.java

License:Apache License

@Override
public void delete(String itemName, Attribute attribute) throws Exception {

    if (!initialised) {
        throw new IllegalStateException("The optimistic persister has not been initialised");
    }//  ww w  . ja  v a2 s .com

    logger.log("About to delete attribute from simpledb item: " + itemName);

    AmazonSimpleDB client = getSimpleDBClient();

    // We retry the delete if necessary if we get a
    // ConditionalCheckFailed exception, i.e. if someone else modifies the
    // database between us reading and writing it.
    RetryHelper.DoWithRetries(() -> {
        try {
            // Get existing attributes (and version number), via consistent
            // read:
            ImmutablePair<Optional<Integer>, Set<Attribute>> versionedAttributes = get(itemName);

            if (!versionedAttributes.left.isPresent()) {
                logger.log(
                        "A version number attribute did not exist - this means no attributes exist, so we have nothing to delete.");
                return null;
            }
            if (!versionedAttributes.right.contains(attribute)) {
                logger.log("The attribute did not exist - so we have nothing to delete.");
                return null;
            }

            // Since it seems impossible to update the version number while
            // deleting an attribute, we first mark the attribute as inactive,
            // and then delete it.
            ReplaceableAttribute inactiveAttribute = new ReplaceableAttribute();
            inactiveAttribute.setName(attribute.getName());
            inactiveAttribute.setValue("Inactive" + attribute.getValue());
            inactiveAttribute.setReplace(true);
            put(itemName, versionedAttributes.left, inactiveAttribute);

            // Now we can safely delete the attribute, as other readers will now
            // ignore it.
            UpdateCondition updateCondition = new UpdateCondition();
            updateCondition.setName(inactiveAttribute.getName());
            updateCondition.setValue(inactiveAttribute.getValue());
            // Update will proceed unless the attribute no longer exists
            updateCondition.setExists(true);

            Attribute attributeToDelete = new Attribute();
            attributeToDelete.setName(inactiveAttribute.getName());
            attributeToDelete.setValue(inactiveAttribute.getValue());
            List<Attribute> attributesToDelete = new ArrayList<>();
            attributesToDelete.add(attributeToDelete);
            DeleteAttributesRequest simpleDBDeleteRequest = new DeleteAttributesRequest(simpleDbDomainName,
                    itemName, attributesToDelete, updateCondition);
            client.deleteAttributes(simpleDBDeleteRequest);
            logger.log("Deleted attribute from simpledb");
            return null;
        } catch (AmazonServiceException ase) {
            if (ase.getErrorCode().contains("AttributeDoesNotExist")) {
                // Case of trying to delete an attribute that no longer exists -
                // that's ok - it probably just means more than one person was
                // trying to delete the attribute at once. So swallow this
                // exception
                logger.log(
                        "Caught AmazonServiceException for AttributeDoesNotExist whilst deleting attribute so"
                                + " swallowing and continuing");
                return null;
            } else {
                throw ase;
            }
        }
    }, Exception.class, Optional.of("Database put failed - conditional check failed"), logger);
}