List of usage examples for com.amazonaws.services.dynamodbv2.document Expected Expected
public Expected(String attrName)
From source file:com.clicktravel.infrastructure.persistence.aws.dynamodb.DynamoDocumentStoreTemplate.java
License:Apache License
@Override public <T extends Item> T update(final T item, final PersistenceExceptionHandler<?>... persistenceExceptionHandlers) { final ItemConfiguration itemConfiguration = getItemConfiguration(item.getClass()); if (item.getVersion() == null) { return create(item); }/* w w w . java 2 s. c o m*/ final Expected expectedCondition = new Expected(VERSION_ATTRIBUTE).eq(item.getVersion()); final Long newVersion = item.getVersion() + 1l; item.setVersion(newVersion); final String tableName = databaseSchemaHolder.schemaName() + "." + itemConfiguration.tableName(); final String itemJson = itemToString(item); final PrimaryKey primaryKey = new PrimaryKey(); final ItemId itemId = itemConfiguration.getItemId(item); final PrimaryKeyDefinition primaryKeyDefinition = itemConfiguration.primaryKeyDefinition(); primaryKey.addComponent(primaryKeyDefinition.propertyName(), itemId.value()); if (primaryKeyDefinition instanceof CompoundPrimaryKeyDefinition) { primaryKey.addComponent(((CompoundPrimaryKeyDefinition) primaryKeyDefinition).supportingPropertyName(), itemId.supportingValue()); } final Table table = dynamoDBClient.getTable(tableName); final com.amazonaws.services.dynamodbv2.document.Item previousAwsItem = table.getItem(primaryKey); final String previousItemJson = previousAwsItem.toJSON(); final String mergedJson = mergeJSONObjects(itemJson, previousItemJson); final com.amazonaws.services.dynamodbv2.document.Item awsItem = com.amazonaws.services.dynamodbv2.document.Item .fromJSON(mergedJson); final PutItemSpec putItemSpec = new PutItemSpec().withItem(awsItem).withExpected(expectedCondition); try { table.putItem(putItemSpec); } catch (final ConditionalCheckFailedException e) { throw new OptimisticLockException("Conflicting write detected while updating item"); } return item; }
From source file:org.springframework.integration.aws.metadata.DynamoDbMetaDataStore.java
License:Apache License
@Override public String putIfAbsent(String key, String value) { Assert.hasText(key, "'key' must not be empty."); Assert.hasText(value, "'value' must not be empty."); awaitForActive();//from w w w . j ava 2 s . c o m try { this.table.updateItem(new UpdateItemSpec().withPrimaryKey(KEY, key) .withAttributeUpdate(new AttributeUpdate(VALUE).put(value)) .withExpected(new Expected(KEY).notExist())); return null; } catch (ConditionalCheckFailedException e) { return get(key); } }
From source file:org.springframework.integration.aws.metadata.DynamoDbMetaDataStore.java
License:Apache License
@Override public boolean replace(String key, String oldValue, String newValue) { Assert.hasText(key, "'key' must not be empty."); Assert.hasText(oldValue, "'value' must not be empty."); Assert.hasText(newValue, "'newValue' must not be empty."); awaitForActive();// ww w .j a v a 2s.c o m try { return this.table.updateItem(new UpdateItemSpec().withPrimaryKey(KEY, key) .withAttributeUpdate(new AttributeUpdate(VALUE).put(newValue)) .withExpected(new Expected(VALUE).eq(oldValue)).withReturnValues(ReturnValue.UPDATED_NEW)) .getItem() != null; } catch (ConditionalCheckFailedException e) { return false; } }
From source file:tr.com.serkanozal.samba.cache.impl.SambaGlobalCache.java
License:Open Source License
@Override public boolean replace(String key, Object oldValue, Object newValue) { boolean replaced = false; if (oldValue == null && newValue != null) { byte[] newData = serialize(newValue); Item item = new Item().withPrimaryKey("id", key).withBinary("data", newData).with("source", UUID); try {//from ww w. j a v a 2s . co m DYNAMO_DB_TABLE.putItem(item, new Expected("id").notExist()); replaced = true; } catch (ConditionalCheckFailedException e) { } } else if (oldValue != null && newValue == null) { byte[] oldData = serialize(oldValue); try { DYNAMO_DB_TABLE.deleteItem("id", key, new Expected("data").eq(oldData)); replaced = true; } catch (ConditionalCheckFailedException e) { } } else if (oldValue != null && newValue != null) { byte[] oldData = serialize(oldValue); byte[] newData = serialize(newValue); Item item = new Item().withPrimaryKey("id", key).withBinary("data", newData).with("source", UUID); try { DYNAMO_DB_TABLE.putItem(item, new Expected("data").eq(oldData)); replaced = true; } catch (ConditionalCheckFailedException e) { } } if (replaced && LOGGER.isDebugEnabled()) { LOGGER.debug(String.format("Old value %s has been replaced with new value %s " + "assigned to key %s", oldValue, newValue, key)); } return replaced; }