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

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

Introduction

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

Prototype

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

Source Link

Document

Constructs a new PutAttributesRequest object.

Usage

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

License:Apache License

private void putAttributesVersionedInternal(String domainName, String id, List<ReplaceableAttribute> attributes,
        String expectedVersion, PersistentEntity persistentEntity, int attempt) throws DataAccessException {
    PutAttributesRequest request = new PutAttributesRequest(domainName, id, attributes,
            getOptimisticVersionCondition(expectedVersion));
    try {//from ww w.j a  v a2  s  .  c o  m
        sdb.putAttributes(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++;
            putAttributesVersionedInternal(domainName, id, attributes, expectedVersion, persistentEntity,
                    attempt);
        } else {
            throw e;
        }
    }
}

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

License:Apache License

@Override
public int put(String itemName, Optional<Integer> version, ReplaceableAttribute attribute) throws Exception {

    if (!initialised) {
        throw new IllegalStateException("The optimistic persister has not been initialised");
    }//from ww  w.  j av a  2  s .  co m

    logger.log("About to add attrbutes to simpledb item: " + itemName);

    AmazonSimpleDB client = getSimpleDBClient();

    // Check the put will not take us over the maximum number of attributes:
    // N.B. if (replace == true) then this check could be over-eager, but not
    // worth refining it, since this effectively just alters the limit by one.
    ImmutablePair<Optional<Integer>, Set<Attribute>> versionedAttributes = get(itemName);

    if (versionedAttributes.left.isPresent()) {
        logger.log("Retrieved versioned attributes(Count: " + versionedAttributes.right.size()
                + ")  have version number: " + versionedAttributes.left.get());
    } else {
        // There should be no attributes in this case.
        logger.log("Retrieved versioned attributes(Count: " + versionedAttributes.right.size()
                + ") have no version number");
    }

    Boolean tooManyAttributes = versionedAttributes.right.size() >= maxNumberOfAttributes;
    if (tooManyAttributes && !attribute.getValue().startsWith("Inactive")) {
        // We allow puts to inactivate attributes even when on the limit -
        // otherwise we could never delete when we're on the limit.
        logger.log("Cannot create attribute - the maximum number of attributes already exists ("
                + maxNumberOfAttributes
                + ") so throwing a 'Database put failed - too many attributes' exception");
        throw new Exception("Database put failed - too many attributes");
    }

    // Do a conditional put - so we don't overwrite someone else's attributes
    UpdateCondition updateCondition = new UpdateCondition();
    updateCondition.setName(versionAttributeName);
    ReplaceableAttribute versionAttribute = new ReplaceableAttribute();
    versionAttribute.setName(versionAttributeName);
    versionAttribute.setReplace(true);
    // Update will proceed unless the version number has changed
    if (version.isPresent()) {
        // A version number attribute exists - so it should be unchanged
        updateCondition.setValue(Integer.toString(version.get()));
        // Bump up our version number attribute
        versionAttribute.setValue(Integer.toString(version.get() + 1));
    } else {
        // A version number attribute did not exist - so it still should not
        updateCondition.setExists(false);
        // Set initial value for our version number attribute
        versionAttribute.setValue("0");
    }

    List<ReplaceableAttribute> replaceableAttributes = new ArrayList<>();
    replaceableAttributes.add(versionAttribute);

    // Add the new attribute
    replaceableAttributes.add(attribute);

    PutAttributesRequest simpleDBPutRequest = new PutAttributesRequest(simpleDbDomainName, itemName,
            replaceableAttributes, updateCondition);

    try {
        client.putAttributes(simpleDBPutRequest);
    } catch (AmazonServiceException ase) {
        if (ase.getErrorCode().contains("ConditionalCheckFailed")) {
            // Someone else has mutated an attribute since we read them. This is
            // likely to be rare, and a retry should almost always succeed. However,
            // we leave it to clients of this class to retry the call if they wish,
            // as the new mutation may mean they no longer want to do the put.
            logger.log("Caught AmazonServiceException for ConditionalCheckFailed whilst creating"
                    + " attribute(s) so throwing as 'Database put failed' instead");
            throw new Exception("Database put failed - conditional check failed");
        }
        throw ase;
    }

    logger.log("Created attribute(s) in simpledb");

    return Integer.parseInt(versionAttribute.getValue());
}