/** * Converts a list of <code>AttributeUpdate</code> into the low-level * representation; or null if the input is null. */ public static Map<String, AttributeValueUpdate> toAttributeValueUpdate( List<AttributeUpdate> attributesToUpdate) { if (attributesToUpdate == null) return null; Map<String, AttributeValueUpdate> result = new LinkedHashMap<String, AttributeValueUpdate>(); for (AttributeUpdate attribute : attributesToUpdate) { AttributeValueUpdate attributeToUpdate = new AttributeValueUpdate() .withAction(attribute.getAction()); if (attribute.getValue() != null) { attributeToUpdate.withValue(toAttributeValue(attribute .getValue())); } else if (attribute.getAttributeValues() != null) { attributeToUpdate.withValue(toAttributeValue(attribute .getAttributeValues())); } result.put(attribute.getAttributeName(), attributeToUpdate); } return result; }
@Override public String putIfAbsent(String key, String value) { Assert.hasText(key, "'key' must not be empty."); Assert.hasText(value, "'value' must not be empty."); awaitForActive(); UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey(KEY, key) .withAttributeUpdate( new AttributeUpdate(VALUE) .put(value)) .withExpected( new Expected(KEY) .notExist()); if (this.timeToLive != null && this.timeToLive > 0) { updateItemSpec = updateItemSpec.addAttributeUpdate( new AttributeUpdate(TTL) .put((System.currentTimeMillis() + this.timeToLive) / 1000)); } try { this.table.updateItem(updateItemSpec); return null; } catch (ConditionalCheckFailedException e) { return get(key); } }
@Override public String putIfAbsent(String key, String value) { Assert.hasText(key, "'key' must not be empty."); Assert.hasText(value, "'value' must not be empty."); awaitForActive(); UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey(KEY, key) .withAttributeUpdate( new AttributeUpdate(VALUE) .put(value)) .withExpected( new Expected(KEY) .notExist()); if (this.timeToLive != null && this.timeToLive > 0) { updateItemSpec = updateItemSpec.addAttributeUpdate( new AttributeUpdate(TTL) .put((System.currentTimeMillis() + this.timeToLive) / 1000)); } try { this.table.updateItem(updateItemSpec); return null; } catch (ConditionalCheckFailedException e) { return get(key); } }
@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; } }
/** * Converts a list of <code>AttributeUpdate</code> into the low-level * representation; or null if the input is null. */ public static Map<String, AttributeValueUpdate> toAttributeValueUpdate( List<AttributeUpdate> attributesToUpdate) { if (attributesToUpdate == null) return null; Map<String, AttributeValueUpdate> result = new LinkedHashMap<String, AttributeValueUpdate>(); for (AttributeUpdate attribute : attributesToUpdate) { AttributeValueUpdate attributeToUpdate = new AttributeValueUpdate() .withAction(attribute.getAction()); if (attribute.getValue() != null) { attributeToUpdate.withValue(toAttributeValue(attribute .getValue())); } else if (attribute.getAttributeValues() != null) { attributeToUpdate.withValue(toAttributeValue(attribute .getAttributeValues())); } result.put(attribute.getAttributeName(), attributeToUpdate); } return result; }
@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; } }