/** * 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); }
/** * 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); }
@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); } }
dynamoDBClient.putItem(new PutItemRequest() .withTableName(tableConfig.getTableName()) .addExpectedEntry("id", new ExpectedAttributeValue().withExists(false)) .withItem(ImmutableMap.<String, AttributeValue>builder() .put("project", new AttributeValue(project)) dynamoDBClient.putItem(new PutItemRequest() .withTableName(tableConfig.getTableName()) .addExpectedEntry("id", new ExpectedAttributeValue().withExists(false)) .withItem(ImmutableMap.<String, AttributeValue>builder() .put("project", new AttributeValue(project))
/** * Returns a new copy of Map that can be used in a write on the item to ensure it does not exist * @param txManager * @return a map for use in an expected clause to ensure the item does not exist */ @JsonIgnore protected Map<String, ExpectedAttributeValue> getExpectNotExists(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().withExists(false)); } return expected; }
public MetaStore(final AmazonDynamoDB ddb, final String tableName, final DynamoDBEncryptor encryptor) { this.ddb = checkNotNull(ddb, "ddb must not be null"); this.tableName = checkNotNull(tableName, "tableName must not be null"); this.encryptor = checkNotNull(encryptor, "encryptor must not be null"); ddbCtx = new EncryptionContext.Builder().withTableName(this.tableName) .withHashKeyName(DEFAULT_HASH_KEY).withRangeKeyName(DEFAULT_RANGE_KEY).build(); final Map<String, ExpectedAttributeValue> tmpExpected = new HashMap<String, ExpectedAttributeValue>(); tmpExpected.put(DEFAULT_HASH_KEY, new ExpectedAttributeValue().withExists(false)); tmpExpected.put(DEFAULT_RANGE_KEY, new ExpectedAttributeValue().withExists(false)); doesNotExist = Collections.unmodifiableMap(tmpExpected); }
/** * 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); }
/** * Create a new MetaStore with specified table name and extra data supplier. * * @param ddb Interface for accessing DynamoDB. * @param tableName DynamoDB table name for this {@link MetaStore}. * @param encryptor used to perform crypto operations on the record attributes * @param extraDataSupplier provides extra data that should be stored along with the material. */ public MetaStore(final AmazonDynamoDB ddb, final String tableName, final DynamoDBEncryptor encryptor, final ExtraDataSupplier extraDataSupplier) { this.ddb = checkNotNull(ddb, "ddb must not be null"); this.tableName = checkNotNull(tableName, "tableName must not be null"); this.encryptor = checkNotNull(encryptor, "encryptor must not be null"); this.extraDataSupplier = checkNotNull(extraDataSupplier, "extraDataSupplier must not be null"); this.ddbCtx = new EncryptionContext.Builder().withTableName(this.tableName) .withHashKeyName(DEFAULT_HASH_KEY).withRangeKeyName(DEFAULT_RANGE_KEY).build(); final Map<String, ExpectedAttributeValue> tmpExpected = new HashMap<>(); tmpExpected.put(DEFAULT_HASH_KEY, new ExpectedAttributeValue().withExists(false)); tmpExpected.put(DEFAULT_RANGE_KEY, new ExpectedAttributeValue().withExists(false)); doesNotExist = Collections.unmodifiableMap(tmpExpected); this.doNotEncrypt = getSignedOnlyFields(extraDataSupplier); }
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); } }
/** * 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); }
expected.put(AttributeName.IMAGE_ID.toString(), new ExpectedAttributeValue().withExists(false));
@Override public String lock(final String name, final String label) throws IOException { final AmazonDynamoDB aws = this.table.region().aws(); String msg = ""; try { final PutItemRequest request = new PutItemRequest(); request.setTableName(this.table.name()); request.setItem( new Attributes() .with(DyLocks.HASH, this.owner) .with(DyLocks.RANGE, name) .with(DyLocks.ATTR_LABEL, label) ); request.setExpected( new ImmutableMap.Builder<String, ExpectedAttributeValue>().put( DyLocks.ATTR_LABEL, new ExpectedAttributeValue().withExists(false) ).build() ); aws.putItem(request); } catch (final ConditionalCheckFailedException ex) { msg = ex.getLocalizedMessage(); } catch (final AmazonClientException ex) { throw new IOException(ex); } finally { aws.shutdown(); } return msg; }
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));
/** * 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); }
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))));