Example usage for com.amazonaws.services.simpledb.model ReplaceableAttribute getName

List of usage examples for com.amazonaws.services.simpledb.model ReplaceableAttribute getName

Introduction

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

Prototype


public String getName() 

Source Link

Document

The name of the replaceable attribute.

Usage

From source file:org.grails.datastore.mapping.simpledb.engine.SimpleDBEntityPersister.java

License:Apache License

@Override
public void updateEntry(final PersistentEntity persistentEntity, final EntityAccess ea, final Object key,
        final SimpleDBNativeItem entry) {

    String id = key.toString();//from  w ww .  ja  v  a 2 s  .  com
    String domain = domainResolver.resolveDomain(id);

    //for non-null values we should call putAttributes, for nulls we should call delete attributes
    List<ReplaceableAttribute> allAttributes = entry.createReplaceableItem().getAttributes();

    List<ReplaceableAttribute> puts = new LinkedList<ReplaceableAttribute>();
    List<Attribute> deletes = new LinkedList<Attribute>();

    //we have to put *new* (incremented) version as part of the 'version' value and use the old version value in the conditional update.
    //if the update fails we have to restore the version to the old value
    Object currentVersion = null;
    String stringCurrentVersion = null;
    if (isVersioned(ea)) {
        currentVersion = ea.getProperty("version");
        stringCurrentVersion = convertVersionToString(currentVersion);
        incrementVersion(ea); //increment version now before we save it
    }

    for (ReplaceableAttribute attribute : allAttributes) {
        if ("version".equals(attribute.getName())) {
            //ignore it, it will be explicitly added later right before the insert by taking incrementing and taking new one
        } else {
            if (attribute.getValue() != null) {
                puts.add(attribute);
            } else {
                deletes.add(new Attribute(attribute.getName(), null));
            }
        }
    }

    if (isVersioned(ea)) {
        puts.add(createAttributeForVersion(ea)); //update the version
        try {
            simpleDBTemplate.deleteAttributesVersioned(domain, id, deletes, stringCurrentVersion,
                    persistentEntity);
            simpleDBTemplate.putAttributesVersioned(domain, id, puts, stringCurrentVersion, persistentEntity);
        } catch (DataAccessException e) {
            //we need to restore version to what it was before the attempt to update
            ea.setProperty("version", currentVersion);
            throw e;
        }
    } else {
        simpleDBTemplate.deleteAttributes(domain, id, deletes);
        simpleDBTemplate.putAttributes(domain, id, puts);
    }
}

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");
    }//from w  w  w  . ja  v  a2 s.c  o  m

    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);
}