private UpdateItemOutcome doUpdateItem(UpdateItemSpec spec) { final UpdateItemRequest request = spec.getRequest(); request.setKey(InternalUtils.toAttributeValueMap(spec.getKeyComponents())); request.setTableName(table.getTableName()); final Collection<Expected> expected = spec.getExpected(); final Map<String, ExpectedAttributeValue> expectedMap = InternalUtils.toExpectedAttributeValueMap(expected); request.setExpected(expectedMap); request.setAttributeUpdates( InternalUtils.toAttributeValueUpdate(spec.getAttributeUpdate())); request.setExpressionAttributeNames(spec.getNameMap()); request.setExpressionAttributeValues( InternalUtils.fromSimpleMap(spec.getValueMap())); return new UpdateItemOutcome(client.updateItem(request)); }
private static void updateAddNewAttribute() { Table table = dynamoDB.getTable(tableName); try { Map<String, String> expressionAttributeNames = new HashMap<String, String>(); expressionAttributeNames.put("#na", "NewAttribute"); UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey("Id", 121) .withUpdateExpression("set #na = :val1") .withNameMap(new NameMap() .with("#na", "NewAttribute")) .withValueMap(new ValueMap() .withString(":val1", "Some value")) .withReturnValues(ReturnValue.ALL_NEW); UpdateItemOutcome outcome = table.updateItem(updateItemSpec); // Check the response. System.out.println("Printing item after adding new attribute..."); System.out.println(outcome.getItem().toJSONPretty()); } catch (Exception e) { System.err.println("Failed to add new attribute in " + tableName); System.err.println(e.getMessage()); } }
private static void updateMultipleAttributes() { Table table = dynamoDB.getTable(tableName); try { UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey("Id", 120) .withUpdateExpression("add #a :val1 set #na=:val2") .withNameMap(new NameMap() .with("#a", "Authors") .with("#na", "NewAttribute")) .withValueMap(new ValueMap() .withStringSet(":val1", "Author YY", "Author ZZ") .withString(":val2", "someValue")) .withReturnValues(ReturnValue.ALL_NEW); UpdateItemOutcome outcome = table.updateItem(updateItemSpec); // Check the response. System.out .println("Printing item after multiple attribute update..."); System.out.println(outcome.getItem().toJSONPretty()); } catch (Exception e) { System.err.println("Failed to update multiple attributes in " + tableName); System.err.println(e.getMessage()); } }
private static void updateExistingAttributeConditionally() { Table table = dynamoDB.getTable(tableName); try { // Specify the desired price (25.00) and also the condition (price = // 20.00) UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey("Id", 120) .withReturnValues(ReturnValue.ALL_NEW) .withUpdateExpression("set #p = :val1") .withConditionExpression("#p = :val2") .withNameMap(new NameMap() .with("#p", "Price")) .withValueMap(new ValueMap() .withNumber(":val1", 25) .withNumber(":val2", 20)); UpdateItemOutcome outcome = table.updateItem(updateItemSpec); // Check the response. System.out .println("Printing item after conditional update to new attribute..."); System.out.println(outcome.getItem().toJSONPretty()); } catch (Exception e) { System.err.println("Error updating item in " + tableName); System.err.println(e.getMessage()); } }
private UpdateItemOutcome doUpdateItem(UpdateItemSpec spec) { final UpdateItemRequest request = spec.getRequest(); request.setKey(InternalUtils.toAttributeValueMap(spec.getKeyComponents())); request.setTableName(table.getTableName()); final Collection<Expected> expected = spec.getExpected(); final Map<String, ExpectedAttributeValue> expectedMap = InternalUtils.toExpectedAttributeValueMap(expected); request.setExpected(expectedMap); request.setAttributeUpdates( InternalUtils.toAttributeValueUpdate(spec.getAttributeUpdate())); request.setExpressionAttributeNames(spec.getNameMap()); request.setExpressionAttributeValues( InternalUtils.fromSimpleMap(spec.getValueMap())); return new UpdateItemOutcome(client.updateItem(request)); }
@Override public boolean replace(String key, String oldValue, String newValue) { Assert.hasText(key, "'key' must not be empty."); Assert.hasText(oldValue, "'value' must not be empty."); Assert.hasText(newValue, "'newValue' must not be empty."); awaitForActive(); UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey(KEY, key) .withAttributeUpdate( new AttributeUpdate(VALUE) .put(newValue)) .withExpected( new Expected(VALUE) .eq(oldValue)) .withReturnValues(ReturnValue.UPDATED_NEW); if (this.timeToLive != null && this.timeToLive > 0) { updateItemSpec = updateItemSpec.addAttributeUpdate( new AttributeUpdate(TTL) .put((System.currentTimeMillis() + this.timeToLive) / 1000)); } try { return this.table.updateItem(updateItemSpec) .getItem() != null; } catch (ConditionalCheckFailedException e) { return false; } }
@Override public boolean replace(String key, String oldValue, String newValue) { Assert.hasText(key, "'key' must not be empty."); Assert.hasText(oldValue, "'value' must not be empty."); Assert.hasText(newValue, "'newValue' must not be empty."); awaitForActive(); UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey(KEY, key) .withAttributeUpdate( new AttributeUpdate(VALUE) .put(newValue)) .withExpected( new Expected(VALUE) .eq(oldValue)) .withReturnValues(ReturnValue.UPDATED_NEW); if (this.timeToLive != null && this.timeToLive > 0) { updateItemSpec = updateItemSpec.addAttributeUpdate( new AttributeUpdate(TTL) .put((System.currentTimeMillis() + this.timeToLive) / 1000)); } try { return this.table.updateItem(updateItemSpec) .getItem() != null; } catch (ConditionalCheckFailedException e) { return false; } }
@Override public void unlock() { // Set lockUntil to now or lockAtLeastUntil whichever is later String unlockTimeIso = toIsoString(lockConfiguration.getUnlockTime()); UpdateItemSpec request = new UpdateItemSpec() .withPrimaryKey(ID, lockConfiguration.getName()) .withUpdateExpression(RELEASE_LOCK_QUERY) .withValueMap(new ValueMap() .withString(":lockUntil", unlockTimeIso) ) .withReturnValues(ReturnValue.UPDATED_NEW); UpdateItemOutcome updated = table.updateItem(request); assert unlockTimeIso.equals(updated.getItem().getString(LOCK_UNTIL)); } }
@Override public Optional<SimpleLock> lock(LockConfiguration lockConfiguration) { String nowIso = toIsoString(now()); String lockUntilIso = toIsoString(lockConfiguration.getLockAtMostUntil()); UpdateItemSpec request = new UpdateItemSpec() .withPrimaryKey(ID, lockConfiguration.getName()) .withUpdateExpression(OBTAIN_LOCK_QUERY) .withConditionExpression(OBTAIN_LOCK_CONDITION) .withValueMap(new ValueMap() .withString(":lockUntil", lockUntilIso) .withString(":lockedAt", nowIso) .withString(":lockedBy", hostname) ) .withReturnValues(ReturnValue.UPDATED_NEW); 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 UpdateItemOutcome updated = table.updateItem(request); assert lockUntilIso.equals(updated.getItem().getString(LOCK_UNTIL)); return Optional.of(new DynamoDBLock(lockConfiguration)); } catch (ConditionalCheckFailedException e) { // Condition failed. This means there was a lock with lockUntil > now. return Optional.empty(); } }