Example usage for com.amazonaws.services.simpledb.model GetAttributesRequest setConsistentRead

List of usage examples for com.amazonaws.services.simpledb.model GetAttributesRequest setConsistentRead

Introduction

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

Prototype


public void setConsistentRead(Boolean consistentRead) 

Source Link

Document

Determines whether or not strong consistency should be enforced when data is read from SimpleDB.

Usage

From source file:com.aipo.aws.simpledb.SimpleDB.java

License:Open Source License

private static Integer counterJob(String domain) {
    AmazonSimpleDB client = getClient();
    GetAttributesRequest getAttributesRequest = new GetAttributesRequest();
    getAttributesRequest.setDomainName(DEFAULT_COUNTER_DOMAIN);
    getAttributesRequest.setItemName(domain);
    getAttributesRequest.setConsistentRead(true);
    Integer count = null;// w  w w.  j  av a 2  s.c  om
    try {
        GetAttributesResult attributes = client.getAttributes(getAttributesRequest);
        List<Attribute> list = attributes.getAttributes();
        for (Attribute item : list) {
            if ("c".equals(item.getName())) {
                try {
                    count = Integer.valueOf(item.getValue());
                } catch (Throwable ignore) {

                }
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }

    if (count == null) {
        CreateDomainRequest createDomainRequest = new CreateDomainRequest(DEFAULT_COUNTER_DOMAIN);
        client.createDomain(createDomainRequest);
        count = 0;
    }

    int next = count + 1;
    PutAttributesRequest putAttributesRequest = new PutAttributesRequest();
    putAttributesRequest.setDomainName(DEFAULT_COUNTER_DOMAIN);
    putAttributesRequest.setItemName(domain);
    List<ReplaceableAttribute> attr = new ArrayList<ReplaceableAttribute>();
    attr.add(new ReplaceableAttribute("c", String.valueOf(next), true));
    putAttributesRequest.setAttributes(attr);
    UpdateCondition updateCondition = new UpdateCondition();
    if (next == 1) {
        updateCondition.setExists(false);
        updateCondition.setName("c");
    } else {
        updateCondition.setExists(true);
        updateCondition.setName("c");
        updateCondition.setValue(String.valueOf(count));
    }
    putAttributesRequest.setExpected(updateCondition);

    client.putAttributes(putAttributesRequest);

    return next;
}

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

License:Apache License

private Item getConsistentInternal(String domainName, String id, int attempt) {
    //        String selectExpression = "select * from `" + domainName + "` where id = '"+id+"'"; //todo

    //todo - handle exceptions and retries

    GetAttributesRequest request = new GetAttributesRequest(domainName, id);
    request.setConsistentRead(true);
    try {/*from www  .j  a  v a 2  s .c o  m*/
        List<Attribute> attributes = sdb.getAttributes(request).getAttributes();
        if (attributes.isEmpty()) {
            return null;
        }

        return new Item(id, attributes);
    } catch (AmazonServiceException e) {
        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++;
            return getConsistentInternal(domainName, id, attempt);
        } else {
            throw e;
        }
    }
}

From source file:siena.sdb.SdbPersistenceManager.java

License:Apache License

public void get(Object obj) {
    Class<?> clazz = obj.getClass();
    ClassInfo info = ClassInfo.getClassInfo(obj.getClass());

    String domain = SdbMappingUtils.getDomainName(clazz, prefix);
    try {// w  w w.  ja v a  2  s  .c o  m
        checkDomain(domain);
        GetAttributesRequest req = SdbMappingUtils.createGetRequest(domain, clazz, obj);
        // sets consistent read to true when reading one single object
        req.setConsistentRead(isReadConsistent());
        GetAttributesResult res = sdb.getAttributes(req);
        if (res.getAttributes().size() == 0) {
            throw new SienaException(req.getItemName() + " not found in domain " + req.getDomainName());
        }

        SdbMappingUtils.fillModel(req.getItemName(), res, clazz, obj);

        // join management
        if (!info.joinFields.isEmpty()) {
            mapJoins(obj);
        }
    } catch (AmazonClientException ex) {
        throw new SienaException(ex);
    }
}

From source file:siena.sdb.SdbPersistenceManager.java

License:Apache License

public <T> T getByKey(Class<T> clazz, Object key) {
    String domain = SdbMappingUtils.getDomainName(clazz, prefix);
    try {/*w ww .jav  a 2s .c  om*/
        checkDomain(domain);
        GetAttributesRequest req = SdbMappingUtils.createGetRequestFromKey(domain, clazz, key);
        // sets consistent read to true when reading one single object
        req.setConsistentRead(isReadConsistent());
        GetAttributesResult res = sdb.getAttributes(req);
        if (res.getAttributes().size() == 0) {
            throw new SienaException(req.getItemName() + " not found in domain " + req.getDomainName());
        }

        T obj = Util.createObjectInstance(clazz);

        SdbMappingUtils.fillModel(req.getItemName(), res, clazz, obj);

        // join management
        if (!ClassInfo.getClassInfo(clazz).joinFields.isEmpty()) {
            mapJoins(obj);
        }

        return obj;
    } catch (AmazonClientException ex) {
        throw new SienaException(ex);
    }
}

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

License:Apache License

@Override
public ImmutablePair<Optional<Integer>, Set<Attribute>> get(String itemName) {

    if (!initialised) {
        throw new IllegalStateException("The optimistic persister has not been initialised");
    }/*from  w  w  w. j a  v  a 2s.c o  m*/

    logger.log("About to get all active attributes from simpledb item: " + itemName);

    AmazonSimpleDB client = getSimpleDBClient();

    // Do a consistent read - to ensure we get correct version number
    GetAttributesRequest simpleDBRequest = new GetAttributesRequest(simpleDbDomainName, itemName);
    logger.log("Using simpleDB domain: " + simpleDbDomainName);

    simpleDBRequest.setConsistentRead(true);
    GetAttributesResult result = client.getAttributes(simpleDBRequest);
    List<Attribute> attributes = result.getAttributes();

    // Get the version number and other attributes.
    Optional<Integer> version = Optional.empty();
    Set<Attribute> nonVersionAttributes = new HashSet<>();
    if (attributes.size() > 0) {
        // If we have any attributes, we'll always have a version number
        Attribute versionNumberAttribute = attributes.stream()
                .filter(attribute -> attribute.getName().equals(versionAttributeName)).findFirst().get();
        version = Optional.of(Integer.parseInt(versionNumberAttribute.getValue()));
        logger.log("Retrieved version number: " + versionNumberAttribute.getValue());
        attributes.remove(versionNumberAttribute);

        // Add all active attributes (i.e. those not pending deletion)
        nonVersionAttributes.addAll(attributes.stream()
                .filter(attribute -> !attribute.getValue().startsWith("Inactive")).collect(Collectors.toSet()));
    }
    logger.log("Got all attributes from simpledb");

    return new ImmutablePair<>(version, nonVersionAttributes);
}