Refine search
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); } }
/** * 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); }
@Override public int hashCode() { final int prime = 31; int hashCode = 1; hashCode = prime * hashCode + ((getValue() == null) ? 0 : getValue().hashCode()); hashCode = prime * hashCode + ((getExists() == null) ? 0 : getExists().hashCode()); hashCode = prime * hashCode + ((getComparisonOperator() == null) ? 0 : getComparisonOperator().hashCode()); hashCode = prime * hashCode + ((getAttributeValueList() == null) ? 0 : getAttributeValueList().hashCode()); return hashCode; }
final String attr = expected.getAttribute(); final Object[] values = expected.getValues(); ExpectedAttributeValue eav = new ExpectedAttributeValue(); if (values != null) { if (values.length > 0) { eav.withAttributeValueList(avs); } else { throw new IllegalStateException("Bug!"); + " must be specified"); eav.withComparisonOperator(op); expectedMap.put(attr, eav);
@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); }
/** * 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); }
AttributeValueUpdate txItemUpdate = new AttributeValueUpdate() .withAction(AttributeAction.ADD) .withValue(new AttributeValue().withBS(Arrays.asList(requestBytes))); txItemUpdates.put(AttributeName.VERSION.toString(), new AttributeValueUpdate().withAction(AttributeAction.ADD).withValue(new AttributeValue().withN("1"))); txItemUpdates.put(AttributeName.DATE.toString(), new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(txManager.getCurrentTimeAttribute())); expected.put(AttributeName.STATE.toString(), new ExpectedAttributeValue(new AttributeValue(STATE_PENDING))); expected.put(AttributeName.VERSION.toString(), new ExpectedAttributeValue(new AttributeValue().withN(Integer.toString(version))));
txAssert(! item.containsKey(AttributeName.APPLIED.toString()), txId, "The transaction has already applied this item image, it should not be saving over the item image with it"); AttributeValue existingTxId = item.put(AttributeName.TXID.toString(), new AttributeValue(txId)); if(existingTxId != null && ! txId.equals(existingTxId.getS())) { throw new TransactionException(txId, "Items in transactions may not contain the attribute named " + AttributeName.TXID.toString()); expected.put(AttributeName.IMAGE_ID.toString(), new ExpectedAttributeValue().withExists(false)); AttributeValue existingImageId = item.put(AttributeName.IMAGE_ID.toString(), new AttributeValue(txId + "#" + rid)); if(existingImageId != null) { throw new TransactionException(txId, "Items in transactions may not contain the attribute named " + AttributeName.IMAGE_ID.toString() + ", value was already " + existingImageId);
new AttributeValue().withN("" + update.getFinalValue()))); put(setAttribute, new ExpectedAttributeValue().withExists(false)); expected.put( setAttribute, new ExpectedAttributeValue().withComparisonOperator( calc.getDynamoComparisonOperator()).withValue( new AttributeValue().withN("" + update.getFinalValue()))); req.setExpected(expected);
@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); } }
/** * Inserts a new transaction item into the table. Assumes txKey is already initialized. * @return the txItem * @throws TransactionException if the transaction already exists */ private Map<String, AttributeValue> insert() { Map<String, AttributeValue> item = new HashMap<String, AttributeValue>(); item.put(AttributeName.STATE.toString(), new AttributeValue(STATE_PENDING)); item.put(AttributeName.VERSION.toString(), new AttributeValue().withN(Integer.toString(1))); item.put(AttributeName.DATE.toString(), txManager.getCurrentTimeAttribute()); item.putAll(txKey); Map<String, ExpectedAttributeValue> expectNotExists = new HashMap<String, ExpectedAttributeValue>(2); expectNotExists.put(AttributeName.TXID.toString(), new ExpectedAttributeValue(false)); expectNotExists.put(AttributeName.STATE.toString(), new ExpectedAttributeValue(false)); PutItemRequest request = new PutItemRequest() .withTableName(txManager.getTransactionTableName()) .withItem(item) .withExpected(expectNotExists); try { txManager.getClient().putItem(request); return item; } catch (ConditionalCheckFailedException e) { throw new TransactionException("Failed to create new transaction with id " + txId, e); } }
/** * 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)); } }
put("threadId", new ExpectedAttributeValue(false))
public ExpectedAttributeValue unmarshall(JsonUnmarshallerContext context) throws Exception { ExpectedAttributeValue expectedAttributeValue = new ExpectedAttributeValue(); if (context.testExpression("Value", targetDepth)) { context.nextToken(); expectedAttributeValue.setValue(AttributeValueJsonUnmarshaller.getInstance().unmarshall(context)); expectedAttributeValue.setExists(context.getUnmarshaller(Boolean.class).unmarshall(context)); expectedAttributeValue.setComparisonOperator(context.getUnmarshaller(String.class).unmarshall(context)); expectedAttributeValue.setAttributeValueList(new ListUnmarshaller<AttributeValue>(AttributeValueJsonUnmarshaller.getInstance()) .unmarshall(context));
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));
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); } }
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)); } }
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); } }
if ((entry.getValue().isExists() == null || entry.getValue().isExists() == true) || !expectedValueMatches(entry.getValue().getValue(), item.get(entry.getKey())))) { throw new ConditionalCheckFailedException( "expected attribute(s) " + expectedValues + " but found " + item); } else if (entry.getValue().isExists() != null && !entry.getValue().isExists() && item != null && item.get(entry.getKey()) != null) {