software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException Java Examples
The following examples show how to use
software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ApplicationsService.java From realworld-serverless-application with Apache License 2.0 | 6 votes |
@Override public Application createApplication(final CreateApplicationInput createApplicationInput) { log.info("Creating application with input {}", createApplicationInput); ApplicationRecord applicationRecord = modelMapper.map(createApplicationInput, ApplicationRecord.class); applicationRecord.setCreatedAt(Instant.now(clock)); applicationRecord.setVersion(1L); applicationRecord.setUserId(securityContext.getUserPrincipal().getName()); try { dynamodb.putItem(PutItemRequest.builder() .tableName(tableName) .item(applicationRecord.toAttributeMap()) .conditionExpression( String.format("attribute_not_exists(%s) AND attribute_not_exists(%s)", ApplicationRecord.USER_ID_ATTRIBUTE_NAME, ApplicationRecord.APPLICATION_ID_ATTRIBUTE_NAME)) .build()); } catch (ConditionalCheckFailedException e) { throw new ConflictApiException(new ConflictException() .errorCode("ApplicationAlreadyExist") .message(String.format("Application %s already exists.", createApplicationInput.getApplicationId()))); } return modelMapper.map(applicationRecord, Application.class); }
Example #2
Source File: BasicCrudTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void putWithConditionThatFails() { Record record = new Record() .setId("id-value") .setSort("sort-value") .setAttribute("one") .setAttribute2("two") .setAttribute3("three"); mappedTable.putItem(r -> r.item(record)); record.setAttribute("four"); Expression conditionExpression = Expression.builder() .expression("#key = :value OR #key1 = :value1") .putExpressionName("#key", "attribute") .putExpressionName("#key1", ATTRIBUTE_NAME_WITH_SPECIAL_CHARACTERS) .putExpressionValue(":value", stringValue("wrong")) .putExpressionValue(":value1", stringValue("wrong")) .build(); exception.expect(ConditionalCheckFailedException.class); mappedTable.putItem(PutItemEnhancedRequest.builder(Record.class) .item(record) .conditionExpression(conditionExpression).build()); }
Example #3
Source File: BasicCrudTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void deleteWithConditionThatFails() { Record record = new Record() .setId("id-value") .setSort("sort-value") .setAttribute("one") .setAttribute2("two") .setAttribute3("three"); mappedTable.putItem(r -> r.item(record)); Expression conditionExpression = Expression.builder() .expression("#key = :value OR #key1 = :value1") .putExpressionName("#key", "attribute") .putExpressionName("#key1", ATTRIBUTE_NAME_WITH_SPECIAL_CHARACTERS) .putExpressionValue(":value", stringValue("wrong")) .putExpressionValue(":value1", stringValue("wrong")) .build(); exception.expect(ConditionalCheckFailedException.class); mappedTable.deleteItem(DeleteItemEnhancedRequest.builder().key(mappedTable.keyFrom(record)) .conditionExpression(conditionExpression) .build()); }
Example #4
Source File: BasicCrudTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void updateWithConditionThatFails() { Record record = new Record() .setId("id-value") .setSort("sort-value") .setAttribute("one") .setAttribute2("two") .setAttribute3("three"); mappedTable.putItem(r -> r.item(record)); record.setAttribute("four"); Expression conditionExpression = Expression.builder() .expression("#key = :value OR #key1 = :value1") .putExpressionName("#key", "attribute") .putExpressionName("#key1", ATTRIBUTE_NAME_WITH_SPECIAL_CHARACTERS) .putExpressionValue(":value", stringValue("wrong")) .putExpressionValue(":value1", stringValue("wrong")) .build(); exception.expect(ConditionalCheckFailedException.class); mappedTable.updateItem(UpdateItemEnhancedRequest.builder(Record.class) .item(record) .conditionExpression(conditionExpression) .build()); }
Example #5
Source File: VersionedRecordTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void putExistingRecordVersionDoesNotMatchConditionExpressionMatches() { mappedTable.putItem(r -> r.item(new Record().setId("id").setAttribute("one"))); Expression conditionExpression = Expression.builder() .expression("#k = :v OR #k = :v1") .putExpressionName("#k", "attribute") .putExpressionValue(":v", stringValue("wrong")) .putExpressionValue(":v1", stringValue("one")) .build(); exception.expect(ConditionalCheckFailedException.class); mappedTable.putItem(PutItemEnhancedRequest.builder(Record.class) .item(new Record().setId("id").setAttribute("one").setVersion(2)) .conditionExpression(conditionExpression) .build()); }
Example #6
Source File: VersionedRecordTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void putExistingRecordVersionMatchesConditionExpressionDoesNotMatch() { mappedTable.putItem(r -> r.item(new Record().setId("id").setAttribute("one"))); Expression conditionExpression = Expression.builder() .expression("#k = :v OR #k = :v1") .putExpressionName("#k", "attribute") .putExpressionValue(":v", stringValue("wrong")) .putExpressionValue(":v1", stringValue("wrong2")) .build(); exception.expect(ConditionalCheckFailedException.class); mappedTable.putItem(PutItemEnhancedRequest.builder(Record.class) .item(new Record().setId("id").setAttribute("one").setVersion(1)) .conditionExpression(conditionExpression) .build()); }
Example #7
Source File: VersionedRecordTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void updateExistingRecordVersionDoesNotMatchConditionExpressionMatches() { mappedTable.putItem(r -> r.item(new Record().setId("id").setAttribute("one"))); Expression conditionExpression = Expression.builder() .expression("#k = :v OR #k = :v1") .putExpressionName("#k", "attribute") .putExpressionValue(":v", stringValue("wrong")) .putExpressionValue(":v1", stringValue("one")) .build(); exception.expect(ConditionalCheckFailedException.class); mappedTable.updateItem(UpdateItemEnhancedRequest.builder(Record.class) .item(new Record().setId("id").setAttribute("one").setVersion(2)) .conditionExpression(conditionExpression) .build()); }
Example #8
Source File: VersionedRecordTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void updateExistingRecordVersionMatchesConditionExpressionDoesNotMatch() { mappedTable.putItem(r -> r.item(new Record().setId("id").setAttribute("one"))); Expression conditionExpression = Expression.builder() .expression("#k = :v OR #k = :v1") .putExpressionName("#k", "attribute") .putExpressionValue(":v", stringValue("wrong")) .putExpressionValue(":v1", stringValue("wrong2")) .build(); exception.expect(ConditionalCheckFailedException.class); mappedTable.updateItem(UpdateItemEnhancedRequest.builder(Record.class) .item(new Record().setId("id").setAttribute("one").setVersion(1)) .conditionExpression(conditionExpression) .build()); }
Example #9
Source File: MetaStore.java From aws-dynamodb-encryption-java with Apache License 2.0 | 6 votes |
/** * This API retrieves the intermediate keys from the source region and replicates it in the target region. * * @param materialName material name of the encryption material. * @param version version of the encryption material. * @param targetMetaStore target MetaStore where the encryption material to be stored. */ public void replicate(final String materialName, final long version, final MetaStore targetMetaStore) { try { final Map<String, AttributeValue> item = getMaterialItem(materialName, version); final Map<String, AttributeValue> plainText = getPlainText(item); final Map<String, AttributeValue> encryptedText = targetMetaStore.getEncryptedText(plainText); final PutItemRequest put = PutItemRequest.builder() .tableName(targetMetaStore.tableName) .item(encryptedText) .expected(doesNotExist) .build(); targetMetaStore.ddb.putItem(put); } catch (ConditionalCheckFailedException e) { //Item already present. } }
Example #10
Source File: AsyncBasicCrudTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void deleteWithConditionThatFails() { Record record = new Record() .setId("id-value") .setSort("sort-value") .setAttribute("one") .setAttribute2("two") .setAttribute3("three"); mappedTable.putItem(r -> r.item(record)).join(); Expression conditionExpression = Expression.builder() .expression("#key = :value OR #key1 = :value1") .putExpressionName("#key", "attribute") .putExpressionName("#key1", "attribute3") .putExpressionValue(":value", stringValue("wrong")) .putExpressionValue(":value1", stringValue("wrong")) .build(); exception.expect(CompletionException.class); exception.expectCause(instanceOf(ConditionalCheckFailedException.class)); mappedTable.deleteItem(DeleteItemEnhancedRequest.builder().key(mappedTable.keyFrom(record)) .conditionExpression(conditionExpression) .build()).join(); }
Example #11
Source File: ApplicationsServiceTest.java From realworld-serverless-application with Apache License 2.0 | 5 votes |
@Test public void createApplication_alreadyExist() { String userId = UUID.randomUUID().toString(); String applicationId = UUID.randomUUID().toString(); CreateApplicationInput input = new CreateApplicationInput() .applicationId(applicationId); when(principal.getName()).thenReturn(userId); doThrow(ConditionalCheckFailedException.class).when(dynamodb).putItem(any(PutItemRequest.class)); assertThatThrownBy(() -> service.createApplication(input)) .isInstanceOf(ConflictApiException.class); }
Example #12
Source File: AWSDynamoDAO.java From para with Apache License 2.0 | 5 votes |
private boolean updateRow(String key, String appid, Map<String, AttributeValue> row) { if (StringUtils.isBlank(key) || StringUtils.isBlank(appid) || row == null || row.isEmpty()) { return false; } String table = getTableNameForAppid(appid); try { UpdateItemRequest.Builder updateRequest = UpdateItemRequest.builder(); StringBuilder updateExpression = new StringBuilder("SET "); Map<String, String> names = new HashMap<>(row.size() + 1); Map<String, AttributeValue> values = new HashMap<>(row.size() + 1); boolean isLockingEnabledForRow = false; AttributeValue version = row.remove(Config._VERSION); // ignore the version field here if (version == null || version.n() == null) { version = AttributeValue.builder().n("0").build(); } if (Long.parseLong(version.n()) > 0L) { isLockingEnabledForRow = true; } for (Entry<String, AttributeValue> attr : row.entrySet()) { String name = "#" + attr.getKey(); String value = ":" + attr.getKey(); updateExpression.append(name).append("=").append(value).append(","); names.put(name, attr.getKey()); values.put(value, attr.getValue()); } updateExpression.setLength(updateExpression.length() - 1); // remove comma at the end if (isLockingEnabledForRow) { names.put("#" + Config._VERSION, Config._VERSION); values.put(":" + Config._VERSION, version); values.put(":plusOne", AttributeValue.builder().n("1").build()); updateRequest.conditionExpression("#" + Config._VERSION + " = :" + Config._VERSION); updateExpression.append(" ADD #").append(Config._VERSION).append(" :plusOne"); } updateRequest.tableName(table); updateRequest.key(rowKey(key, appid)); updateRequest.expressionAttributeNames(names); updateRequest.expressionAttributeValues(values); updateRequest.updateExpression(updateExpression.toString()); client().updateItem(updateRequest.build()); return true; } catch (ConditionalCheckFailedException ex) { logger.warn("Item not updated - versions don't match. table={}, appid={}, key={}.", table, appid, key); } catch (Exception e) { logger.error("Could not update row in DB - table={}, appid={}, key={}:", table, appid, key, e); throwIfNecessary(e); } return false; }
Example #13
Source File: MetaStore.java From aws-dynamodb-encryption-java with Apache License 2.0 | 5 votes |
private Map<String, AttributeValue> conditionalPut(final Map<String, AttributeValue> item) { try { final PutItemRequest put = PutItemRequest.builder().tableName(tableName).item(item) .expected(doesNotExist).build(); ddb.putItem(put); return item; } catch (final ConditionalCheckFailedException ex) { final Map<String, AttributeValue> ddbKey = new HashMap<>(); ddbKey.put(DEFAULT_HASH_KEY, item.get(DEFAULT_HASH_KEY)); ddbKey.put(DEFAULT_RANGE_KEY, item.get(DEFAULT_RANGE_KEY)); return ddbGet(ddbKey); } }
Example #14
Source File: DynamoDBLockProvider.java From ShedLock with Apache License 2.0 | 5 votes |
@Override @NonNull public Optional<SimpleLock> lock(@NonNull LockConfiguration lockConfiguration) { String nowIso = toIsoString(now()); String lockUntilIso = toIsoString(lockConfiguration.getLockAtMostUntil()); Map<String, AttributeValue> key = singletonMap(ID, attr(lockConfiguration.getName())); Map<String, AttributeValue> attributeUpdates = new HashMap<>(3); attributeUpdates.put(":lockUntil", attr(lockUntilIso)); attributeUpdates.put(":lockedAt", attr(nowIso)); attributeUpdates.put(":lockedBy", attr(hostname)); UpdateItemRequest request = UpdateItemRequest.builder() .tableName(tableName) .key(key) .updateExpression(OBTAIN_LOCK_QUERY) .conditionExpression(OBTAIN_LOCK_CONDITION) .expressionAttributeValues(attributeUpdates) .returnValues(ReturnValue.UPDATED_NEW) .build(); try { // There are three possible situations: // 1. The lock document does not exist yet - it is inserted - we have the lock // 2. The lock document exists and lockUtil <= now - it is updated - we have the lock // 3. The lock document exists and lockUtil > now - ConditionalCheckFailedException is thrown dynamoDbClient.updateItem(request); return Optional.of(new DynamoDBLock(dynamoDbClient, tableName, lockConfiguration)); } catch (ConditionalCheckFailedException e) { // Condition failed. This means there was a lock with lockUntil > now. return Optional.empty(); } }
Example #15
Source File: AsyncBasicCrudTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void updateWithConditionThatFails() { Record record = new Record() .setId("id-value") .setSort("sort-value") .setAttribute("one") .setAttribute2("two") .setAttribute3("three"); mappedTable.putItem(r -> r.item(record)).join(); record.setAttribute("four"); Expression conditionExpression = Expression.builder() .expression("#key = :value OR #key1 = :value1") .putExpressionName("#key", "attribute") .putExpressionName("#key1", "attribute3") .putExpressionValue(":value", stringValue("wrong")) .putExpressionValue(":value1", stringValue("wrong")) .build(); exception.expect(CompletionException.class); exception.expectCause(instanceOf(ConditionalCheckFailedException.class)); mappedTable.updateItem(UpdateItemEnhancedRequest.builder(Record.class) .item(record) .conditionExpression(conditionExpression) .build()) .join(); }
Example #16
Source File: AsyncBasicCrudTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void putWithConditionThatFails() { Record record = new Record() .setId("id-value") .setSort("sort-value") .setAttribute("one") .setAttribute2("two") .setAttribute3("three"); mappedTable.putItem(r -> r.item(record)).join(); record.setAttribute("four"); Expression conditionExpression = Expression.builder() .expression("#key = :value OR #key1 = :value1") .putExpressionName("#key", "attribute") .putExpressionName("#key1", "attribute3") .putExpressionValue(":value", stringValue("wrong")) .putExpressionValue(":value1", stringValue("wrong")) .build(); exception.expect(CompletionException.class); exception.expectCause(instanceOf(ConditionalCheckFailedException.class)); mappedTable.putItem(PutItemEnhancedRequest.builder(Record.class) .item(record) .conditionExpression(conditionExpression) .build()) .join(); }
Example #17
Source File: VersionedRecordTest.java From aws-sdk-java-v2 with Apache License 2.0 | 4 votes |
@Test(expected = ConditionalCheckFailedException.class) public void putRecordWithWrongVersionNumber() { mappedTable.putItem(r -> r.item(new Record().setId("id").setAttribute("one"))); mappedTable.putItem(r -> r.item(new Record().setId("id").setAttribute("one").setVersion(2))); }
Example #18
Source File: VersionedRecordTest.java From aws-sdk-java-v2 with Apache License 2.0 | 4 votes |
@Test(expected = ConditionalCheckFailedException.class) public void updateNewRecordTwice() { mappedTable.updateItem(r -> r.item(new Record().setId("id").setAttribute("one"))); mappedTable.updateItem(r -> r.item(new Record().setId("id").setAttribute("one"))); }
Example #19
Source File: VersionedRecordTest.java From aws-sdk-java-v2 with Apache License 2.0 | 4 votes |
@Test(expected = ConditionalCheckFailedException.class) public void putNewRecordTwice() { mappedTable.putItem(r -> r.item(new Record().setId("id").setAttribute("one"))); mappedTable.putItem(r -> r.item(new Record().setId("id").setAttribute("one"))); }