final String attr = expected.getAttribute(); final Object[] values = expected.getValues(); ExpectedAttributeValue eav = new ExpectedAttributeValue(); if (values != null) { if (values.length > 0) {
/** * Auto-generates the key. */ private void onAutoGenerateAssignableKey(DynamoDBMapperFieldModel<Object, Object> field) { // Generate the new key value first, then ensure it doesn't exist. onAutoGenerate(field); if (getLocalSaveBehavior() != SaveBehavior.CLOBBER && !internalExpectedValueAssertions.containsKey(field.name()) && field.getGenerateStrategy() != DynamoDBAutoGenerateStrategy.ALWAYS) { // Add an expect clause to make sure that the item // doesn't already exist, since it's supposed to be new internalExpectedValueAssertions.put(field.name(), new ExpectedAttributeValue().withExists(false)); } }
/** * 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); }
/** * Saves the object given into DynamoDB with the condition that the hash * and if applicable, the range key, does not already exist. * @param object The object to create. * @throws ConditionalCheckFailedException If the object exists. * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper#save * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBSaveExpression * @see com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue */ public void saveIfNotExists(T object) throws ConditionalCheckFailedException { final DynamoDBSaveExpression saveExpression = new DynamoDBSaveExpression(); for (final DynamoDBMapperFieldModel<T,Object> key : model.keys()) { saveExpression.withExpectedEntry(key.name(), new ExpectedAttributeValue() .withExists(false)); } mapper.<T>save(object, saveExpression); }
private Environment updateEnvironmentToValid(@NonNull final Environment environment) throws InternalServiceException { environment.setValidEnvironment(true); try { // update succeeds if environment revision was created in this call return updateEnvironment( environment, new DynamoDBSaveExpression() .withExpectedEntry( LATEST_ENVIRONMENT_REVISION_ID, new ExpectedAttributeValue( new AttributeValue(environment.getLatestEnvironmentRevisionId())))); } catch (final ResourceNotFoundException e) { throw new InternalServiceException( String.format( "Trying to update the environment %s to valid but the environment does not exist", environment.getEnvironmentId()), e); } }
/** * 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); }
final AttributeValue current = field.getAndConvert(object); if (current == null) { internalAssertions.put(field.name(), new ExpectedAttributeValue(false)); } else { internalAssertions.put(field.name(), new ExpectedAttributeValue(true).withValue(current));
public ExpectedAttributeValue unmarshall(JsonUnmarshallerContext context) throws Exception { ExpectedAttributeValue expectedAttributeValue = new ExpectedAttributeValue();
private void onAutoGenerateAssignableKey(Method method, String attributeName) { final AttributeValue newVersionValue = getAutoGeneratedKeyAttributeValue(converter, method); updateValues.put(attributeName, new AttributeValueUpdate().withAction("PUT").withValue(newVersionValue)); inMemoryUpdates.add(new ValueUpdate(method, newVersionValue, object, converter)); if (getLocalSaveBehavior() != SaveBehavior.CLOBBER && !internalExpectedValueAssertions.containsKey(attributeName)) { // Add an expect clause to make sure that the item // doesn't already exist, since it's supposed to be new final ExpectedAttributeValue expected = new ExpectedAttributeValue(); expected.setExists(false); internalExpectedValueAssertions.put(attributeName, expected); } }
@Override public <T> T setConfigOnce(String project, String configName, @NotNull T value) { try { dynamoDBClient.putItem(new PutItemRequest() .withTableName(tableConfig.getTableName()) .withExpected(of("id", new ExpectedAttributeValue(false), "project", new ExpectedAttributeValue(false))) .withItem(of( "project", new AttributeValue(project), "id", new AttributeValue(configName), "value", new AttributeValue(JsonHelper.encode(value))))); return value; } catch (Exception e) { return getConfig(project, configName, (Class<T>) value.getClass()); } }
@Override public void createProject(String project) { try { dynamoDBClient.putItem(new PutItemRequest() .withTableName(tableConfig.getTableName()) .addExpectedEntry("id", new ExpectedAttributeValue().withExists(false)) .withItem(ImmutableMap.<String, AttributeValue>builder() .put("project", new AttributeValue(project)) .put("id", new AttributeValue("|")).build() )); } catch (ConditionalCheckFailedException e) { throw new AlreadyExistsException("Project", HttpResponseStatus.BAD_REQUEST); } }
private void onVersionAttribute(Method method, Object getterResult, String attributeName) { if (getLocalSaveBehavior() != SaveBehavior.CLOBBER && !internalExpectedValueAssertions.containsKey(attributeName)) { // First establish the expected (current) value for the // update call final ExpectedAttributeValue expected = new ExpectedAttributeValue(); // For new objects, insist that the value doesn't exist. // For existing ones, insist it has the old value. final AttributeValue currentValue = converter.convert(method, getterResult); expected.setExists(currentValue != null); if (currentValue != null) { expected.setValue(currentValue); } internalExpectedValueAssertions.put(attributeName, expected); } final Object newVersion = incrementor.increment(method, getterResult); final AttributeValue newVersionValue = converter.convert(method, newVersion); updateValues.put(attributeName, new AttributeValueUpdate() .withAction("PUT") .withValue(newVersionValue)); inMemoryUpdates.add(new ValueUpdate(method, newVersionValue, object, converter)); } }
@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"); }
return null; ExpectedAttributeValue expectedAttributeValue = new ExpectedAttributeValue(); reader.beginObject(); while (reader.hasNext()) {
@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"); }
@Test public void testWithMethods() { DynamoDBSaveExpression se = new DynamoDBSaveExpression(); ConditionalOperator co = ConditionalOperator.AND; assertNotNull(se.withConditionalOperator(co)); assertEquals(se.getConditionalOperator(), co.toString()); assertNotNull(se.withConditionalOperator("OR")); assertEquals(se.getConditionalOperator(), ConditionalOperator.OR.toString()); Map<String, ExpectedAttributeValue> expected = new HashMap<String, ExpectedAttributeValue>(); assertNotNull(se.withExpected(expected)); assertSame(se.getExpected(), expected); ExpectedAttributeValue eav = new ExpectedAttributeValue(); assertNotNull(se.withExpectedEntry("eav", eav)); assertSame(se.getExpected().get("eav"), eav); }
assertNotNull(de.withExpected(ea)); assertEquals(de.getExpected(), ea); ExpectedAttributeValue expected = new ExpectedAttributeValue(); assertNotNull(de.withExpectedEntry("eave", expected)); assertEquals(de.getExpected().get("eave"), expected);