Example usage for com.amazonaws.services.dynamodbv2.document.spec UpdateItemSpec withConditionExpression

List of usage examples for com.amazonaws.services.dynamodbv2.document.spec UpdateItemSpec withConditionExpression

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document.spec UpdateItemSpec withConditionExpression.

Prototype

public UpdateItemSpec withConditionExpression(String conditionExpression) 

Source Link

Usage

From source file:org.chodavarapu.jgitaws.repositories.RefRepository.java

License:Eclipse Distribution License

public Observable<Boolean> compareAndPut(String repositoryName, Ref oldRef, Ref newRef) {
    boolean isSymbolic = newRef.isSymbolic();
    boolean isPeeled = newRef.isPeeled();
    String target = newRef.isSymbolic() ? newRef.getTarget().getName() : newRef.getObjectId().name();

    logger.debug("Saving ref {} -> {} in repository {}", newRef.getName(), target, repositoryName);

    UpdateItemSpec updateSpec = new UpdateItemSpec()
            .withPrimaryKey(new PrimaryKey(new KeyAttribute(REPOSITORY_NAME_ATTRIBUTE, repositoryName),
                    new KeyAttribute(NAME_ATTRIBUTE, newRef.getName())));

    StringBuilder updateExpression = new StringBuilder(COMPARE_AND_PUT_EXPRESSION);
    ValueMap valueMap = new ValueMap().withString(":target", target).withBoolean(":isSymbolic", isSymbolic)
            .withBoolean(":isPeeled", isPeeled);

    if (isPeeled && newRef.getPeeledObjectId() != null) {
        updateExpression.append(", ");
        updateExpression.append(PEELED_TARGET_ATTRIBUTE);
        updateExpression.append(" = :peeledTarget");
        valueMap = valueMap.withString(":peeledTarget", newRef.getPeeledObjectId().name());
    }//  w w w.j a  v a  2  s.  co m

    if (oldRef != null && oldRef.getStorage() != Ref.Storage.NEW) {
        String expected = oldRef.isSymbolic() ? oldRef.getTarget().getName() : oldRef.getObjectId().name();
        updateSpec = updateSpec.withConditionExpression("#target = :expected")
                .withNameMap(new NameMap().with("#target", TARGET_ATTRIBUTE));
        valueMap = valueMap.withString(":expected", expected);
    }

    updateSpec = updateSpec.withUpdateExpression(updateExpression.toString()).withValueMap(valueMap);

    return configuration.getDynamoClient()
            .updateItem(configuration.getRefsTableName(), updateSpec, tableCreator).map(v -> true)
            .doOnNext(v -> logger.debug("Saved ref {} in repository {}", newRef.getName(), repositoryName))
            .onErrorReturn(t -> false);
}