/** * Auto-generates the version. * @param mapping The mapping details. */ private void onVersionAttribute(DynamoDBMapperFieldModel<Object,Object> field) { if ( getLocalSaveBehavior() != SaveBehavior.CLOBBER && !internalExpectedValueAssertions.containsKey(field.name())) { // First establish the expected (current) value for the // update call // For new objects, insist that the value doesn't exist. // For existing ones, insist it has the old value. final Object current = field.get(object); if (current == null) { internalExpectedValueAssertions.put(field.name(), new ExpectedAttributeValue().withExists(false)); } else { internalExpectedValueAssertions.put(field.name(), new ExpectedAttributeValue().withExists(true).withValue(field.convert(current))); } } // Generate the new version value onAutoGenerate(field); }
/** * Deletes the given object from its DynamoDB table with the condition that * the hash and, if applicable, the range key, already exist. * @param object The object to delete. * @throws ConditionalCheckFailedException If the object does not exist. * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper#delete * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDeleteExpression * @see com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue */ public void deleteIfExists(T object) throws ConditionalCheckFailedException { final DynamoDBDeleteExpression deleteExpression = new DynamoDBDeleteExpression(); for (final DynamoDBMapperFieldModel<T,Object> key : model.keys()) { deleteExpression.withExpectedEntry(key.name(), new ExpectedAttributeValue() .withExists(true).withValue(key.convert(key.get(object)))); } mapper.delete(object, deleteExpression); }
/** * Saves the object given into DynamoDB with the condition that the hash * and, if applicable, the range key, already exist. * @param object The object to update. * @throws ConditionalCheckFailedException If the object does not exist. * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper#save * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBSaveExpression * @see com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue */ public void saveIfExists(T object) throws ConditionalCheckFailedException { final DynamoDBSaveExpression saveExpression = new DynamoDBSaveExpression(); for (final DynamoDBMapperFieldModel<T,Object> key : model.keys()) { saveExpression.withExpectedEntry(key.name(), new ExpectedAttributeValue() .withExists(true).withValue(key.convert(key.get(object)))); } mapper.<T>save(object, saveExpression); }
internalAssertions.put(field.name(), new ExpectedAttributeValue(false)); } else { internalAssertions.put(field.name(), new ExpectedAttributeValue(true).withValue(current));
@Test(expected = IllegalArgumentException.class) public void testMergeExpectedAttributeValueConditionsInvalidOperator() { Map<String, ExpectedAttributeValue> internalAssertions = new HashMap<String, ExpectedAttributeValue>(); Map<String, ExpectedAttributeValue> userProvidedConditions = new HashMap<String, ExpectedAttributeValue>(); ExpectedAttributeValue internal = new ExpectedAttributeValue() .withValue(new AttributeValue().withS("internal")); ExpectedAttributeValue user = new ExpectedAttributeValue().withValue(new AttributeValue() .withS("user")); ExpectedAttributeValue bothInterlan = new ExpectedAttributeValue() .withValue(new AttributeValue().withS("bothInterlan")); ExpectedAttributeValue bothUser = new ExpectedAttributeValue() .withValue(new AttributeValue().withS("bothUser")); internalAssertions.put("internal", internal); userProvidedConditions.put("user", user); internalAssertions.put("both", bothInterlan); userProvidedConditions.put("both", bothUser); Map<String, ExpectedAttributeValue> merged = DynamoDBMapper .mergeExpectedAttributeValueConditions( internalAssertions, userProvidedConditions, "OR"); }
@Test public void testMergeExpectedAttributeValueConditionsNoInternalAssertions() { Map<String, ExpectedAttributeValue> userProvidedConditions = new HashMap<String, ExpectedAttributeValue>(); ExpectedAttributeValue user = new ExpectedAttributeValue().withValue(new AttributeValue() .withS("user")); userProvidedConditions.put("user", user); Map<String, ExpectedAttributeValue> merged = DynamoDBMapper .mergeExpectedAttributeValueConditions( null, userProvidedConditions, "AND"); assertEquals(merged.get("user").getValue().getS(), "user"); assertEquals(merged.size(), 1); }
@Test public void testMergeExpectedAttributeValueConditionsNoUserProvidedConditions() { Map<String, ExpectedAttributeValue> internalAssertions = new HashMap<String, ExpectedAttributeValue>(); ExpectedAttributeValue internal = new ExpectedAttributeValue() .withValue(new AttributeValue().withS("internal")); internalAssertions.put("internal", internal); Map<String, ExpectedAttributeValue> merged = DynamoDBMapper .mergeExpectedAttributeValueConditions( internalAssertions, null, "AND"); assertEquals(merged.get("internal").getValue().getS(), "internal"); assertEquals(merged.size(), 1); }
@Test public void testMergeExpectedAttributeValueConditions() { Map<String, ExpectedAttributeValue> internalAssertions = new HashMap<String, ExpectedAttributeValue>(); Map<String, ExpectedAttributeValue> userProvidedConditions = new HashMap<String, ExpectedAttributeValue>(); ExpectedAttributeValue internal = new ExpectedAttributeValue() .withValue(new AttributeValue().withS("internal")); ExpectedAttributeValue user = new ExpectedAttributeValue().withValue(new AttributeValue() .withS("user")); ExpectedAttributeValue bothInterlan = new ExpectedAttributeValue() .withValue(new AttributeValue().withS("bothInterlan")); ExpectedAttributeValue bothUser = new ExpectedAttributeValue() .withValue(new AttributeValue().withS("bothUser")); internalAssertions.put("internal", internal); userProvidedConditions.put("user", user); internalAssertions.put("both", bothInterlan); userProvidedConditions.put("both", bothUser); Map<String, ExpectedAttributeValue> merged = DynamoDBMapper .mergeExpectedAttributeValueConditions( internalAssertions, userProvidedConditions, "AND"); assertEquals(merged.get("internal").getValue().getS(), "internal"); assertEquals(merged.get("user").getValue().getS(), "user"); assertEquals(merged.get("both").getValue().getS(), "bothUser"); }
/** * Returns a new copy of Map that can be used in a write on the item to ensure it exists * @param txManager * @return a map for use in an expected clause to ensure the item exists */ @JsonIgnore protected Map<String, ExpectedAttributeValue> getExpectExists(TransactionManager txManager) { Map<String, AttributeValue> key = getKey(txManager); Map<String, ExpectedAttributeValue> expected = new HashMap<String, ExpectedAttributeValue>(key.size()); for(Map.Entry<String, AttributeValue> entry : key.entrySet()) { expected.put(entry.getKey(), new ExpectedAttributeValue().withValue(entry.getValue())); } return expected; }
private void addExpectedValueIfPresent(final StaticBuffer column, final Map<String, ExpectedAttributeValue> expectedValueMap) { final String dynamoDbColumn = encodeKeyBuffer(column); if (expectedValueMap.containsKey(dynamoDbColumn)) { return; } if (transaction.contains(store, key, column)) { final StaticBuffer expectedValue = transaction.get(store, key, column); final ExpectedAttributeValue expectedAttributeValue; if (expectedValue == null) { expectedAttributeValue = new ExpectedAttributeValue().withExists(false); } else { final AttributeValue attributeValue = encodeValue(expectedValue); expectedAttributeValue = new ExpectedAttributeValue().withValue(attributeValue) .withComparisonOperator(ComparisonOperator.EQ); } expectedValueMap.put(dynamoDbColumn, expectedAttributeValue); } }
/** * Deletes the tx item, only if it was in the "finalized" state. * * @throws ConditionalCheckFailedException if the item does not exist or is not finalized */ public void delete() throws ConditionalCheckFailedException { Map<String, ExpectedAttributeValue> expected = new HashMap<String, ExpectedAttributeValue>(1); expected.put(AttributeName.FINALIZED.toString(), new ExpectedAttributeValue().withValue(new AttributeValue(Transaction.BOOLEAN_TRUE_ATTR_VAL))); DeleteItemRequest completeRequest = new DeleteItemRequest() .withTableName(txManager.getTransactionTableName()) .withKey(txKey) .withExpected(expected); txManager.getClient().deleteItem(completeRequest); }
/** * Auto-generates the version. * @param mapping The mapping details. */ private void onVersionAttribute(DynamoDBMapperFieldModel<Object,Object> field) { if ( getLocalSaveBehavior() != SaveBehavior.CLOBBER && !internalExpectedValueAssertions.containsKey(field.name())) { // First establish the expected (current) value for the // update call // For new objects, insist that the value doesn't exist. // For existing ones, insist it has the old value. final Object current = field.get(object); if (current == null) { internalExpectedValueAssertions.put(field.name(), new ExpectedAttributeValue().withExists(false)); } else { internalExpectedValueAssertions.put(field.name(), new ExpectedAttributeValue().withExists(true).withValue(field.convert(current))); } } // Generate the new version value onAutoGenerate(field); }
expected.put(AttributeName.TXID.toString(), new ExpectedAttributeValue().withValue(new AttributeValue(txId)));
protected static void releaseReadLock(String txId, TransactionManager txManager, String tableName, Map<String, AttributeValue> key) { Map<String, ExpectedAttributeValue> expected = new HashMap<String, ExpectedAttributeValue>(); expected.put(AttributeName.TXID.toString(), new ExpectedAttributeValue().withValue(new AttributeValue(txId))); expected.put(AttributeName.TRANSIENT.toString(), new ExpectedAttributeValue().withExists(false)); expected.put(AttributeName.APPLIED.toString(), new ExpectedAttributeValue().withExists(false)); } catch (ConditionalCheckFailedException e) { try { expected.put(AttributeName.TRANSIENT.toString(), new ExpectedAttributeValue().withValue(new AttributeValue().withS(BOOLEAN_TRUE_ATTR_VAL)));
/** * Deletes the given object from its DynamoDB table with the condition that * the hash and, if applicable, the range key, already exist. * @param object The object to delete. * @throws ConditionalCheckFailedException If the object does not exist. * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper#delete * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDeleteExpression * @see com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue */ public void deleteIfExists(T object) throws ConditionalCheckFailedException { final DynamoDBDeleteExpression deleteExpression = new DynamoDBDeleteExpression(); for (final DynamoDBMapperFieldModel<T,Object> key : model.keys()) { deleteExpression.withExpectedEntry(key.name(), new ExpectedAttributeValue() .withExists(true).withValue(key.convert(key.get(object)))); } mapper.delete(object, deleteExpression); }
txAssert(State.COMMITTED.equals(targetState) || State.ROLLED_BACK.equals(targetState),"Illegal state in finish(): " + targetState, "txItem", txItem); Map<String, ExpectedAttributeValue> expected = new HashMap<String, ExpectedAttributeValue>(2); expected.put(AttributeName.STATE.toString(), new ExpectedAttributeValue().withValue(new AttributeValue().withS(STATE_PENDING))); expected.put(AttributeName.FINALIZED.toString(), new ExpectedAttributeValue().withExists(false)); expected.put(AttributeName.VERSION.toString(), new ExpectedAttributeValue().withValue(new AttributeValue().withN(Integer.toString(expectedVersion))));
/** * Saves the object given into DynamoDB with the condition that the hash * and, if applicable, the range key, already exist. * @param object The object to update. * @throws ConditionalCheckFailedException If the object does not exist. * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper#save * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBSaveExpression * @see com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue */ public void saveIfExists(T object) throws ConditionalCheckFailedException { final DynamoDBSaveExpression saveExpression = new DynamoDBSaveExpression(); for (final DynamoDBMapperFieldModel<T,Object> key : model.keys()) { saveExpression.withExpectedEntry(key.name(), new ExpectedAttributeValue() .withExists(true).withValue(key.convert(key.get(object)))); } mapper.<T>save(object, saveExpression); }
try { Map<String, ExpectedAttributeValue> expected = new HashMap<String, ExpectedAttributeValue>(); expected.put(AttributeName.TXID.toString(), new ExpectedAttributeValue().withValue(new AttributeValue(txId)));
internalAssertions.put(field.name(), new ExpectedAttributeValue(false)); } else { internalAssertions.put(field.name(), new ExpectedAttributeValue(true).withValue(current));
setAttribute, new ExpectedAttributeValue().withComparisonOperator( calc.getDynamoComparisonOperator()).withValue( new AttributeValue().withN("" + update.getFinalValue()))); req.setExpected(expected);