item.put(this.partitionKeyName, AttributeValue.builder().s(key).build()); item.put(OWNER_NAME, AttributeValue.builder().s(this.ownerName).build()); item.put(LEASE_DURATION, AttributeValue.builder().s(String.valueOf(this.leaseDurationInMilliseconds)).build()); final String recordVersionNumber = this.generateRecordVersionNumber(); item.put(RECORD_VERSION_NUMBER, AttributeValue.builder().s(String.valueOf(recordVersionNumber)).build()); sortKeyName.ifPresent(sortKeyName -> item.put(sortKeyName, AttributeValue.builder().s(sortKey.get()).build())); newLockData.ifPresent(byteBuffer -> item.put(DATA, AttributeValue.builder().b(SdkBytes.fromByteBuffer(byteBuffer)).build()));
expressionAttributeValues.put(RVN_VALUE_EXPRESSION_VARIABLE, AttributeValue.builder().s(lockItem.getRecordVersionNumber()).build()); expressionAttributeValues.put(OWNER_NAME_VALUE_EXPRESSION_VARIABLE, AttributeValue.builder().s(lockItem.getOwnerName()).build()); final Map<String, String> expressionAttributeNames = new HashMap<>(); expressionAttributeNames.put(PK_PATH_EXPRESSION_VARIABLE, partitionKeyName); expressionAttributeValues.put(NEW_RVN_VALUE_EXPRESSION_VARIABLE, AttributeValue.builder().s(recordVersionNumber).build()); expressionAttributeValues.put(LEASE_DURATION_VALUE_EXPRESSION_VARIABLE, AttributeValue.builder().s(String.valueOf(leaseDurationToEnsureInMilliseconds)).build()); if (deleteData) { expressionAttributeNames.put(DATA_PATH_EXPRESSION_VARIABLE, DATA); } else if (options.getData().isPresent()) { expressionAttributeNames.put(DATA_PATH_EXPRESSION_VARIABLE, DATA); expressionAttributeValues.put(DATA_VALUE_EXPRESSION_VARIABLE, AttributeValue.builder().b(SdkBytes.fromByteBuffer(options.getData().get())).build()); updateExpression = UPDATE_LEASE_DURATION_AND_RVN_AND_DATA; } else {
expressionAttributeValues.put(RVN_VALUE_EXPRESSION_VARIABLE, AttributeValue.builder().s(lockItem.getRecordVersionNumber()).build()); expressionAttributeValues.put(OWNER_NAME_VALUE_EXPRESSION_VARIABLE, AttributeValue.builder().s(lockItem.getOwnerName()).build()); final Map<String, String> expressionAttributeNames = new HashMap<>(); expressionAttributeNames.put(PK_PATH_EXPRESSION_VARIABLE, partitionKeyName); updateExpression = UPDATE_IS_RELEASED_AND_DATA; expressionAttributeNames.put(DATA_PATH_EXPRESSION_VARIABLE, DATA); expressionAttributeValues.put(DATA_VALUE_EXPRESSION_VARIABLE, AttributeValue.builder().b(SdkBytes.fromByteBuffer(data.get())).build()); } else { updateExpression = UPDATE_IS_RELEASED;
AttributeValue.builder().s(validateCustomerId(order.getCustomerId())).build()); AttributeValue.builder().n(order.getPreTaxAmount().toString()).build()); } catch (NullPointerException e) { throw new IllegalArgumentException(PRE_TAX_AMOUNT_WAS_NULL); AttributeValue.builder().n(order.getPostTaxAmount().toString()).build()); } catch (NullPointerException e) { throw new IllegalArgumentException(POST_TAX_AMOUNT_WAS_NULL); expressionAttributeValues.put(":o", AttributeValue.builder().n("1").build()); try { expressionAttributeValues.put(":v", AttributeValue.builder().n(order.getVersion().toString()).build()); } catch (NullPointerException e) { throw new IllegalArgumentException(VERSION_WAS_NULL); .tableName(tableName) .key(Collections.singletonMap(ORDER_ID, AttributeValue.builder().s(order.getOrderId()).build())) .returnValues(ReturnValue.ALL_NEW) .updateExpression(UPDATE_EXPRESSION)
final Map<String, AttributeValue> expressionAttributeValues = new HashMap<>(); final boolean updateExistingLockRecord = options.getUpdateExistingLockRecord(); expressionAttributeValues.put(RVN_VALUE_EXPRESSION_VARIABLE, AttributeValue.builder().s(existingLock.get().getRecordVersionNumber()).build()); final Map<String, String> expressionAttributeNames = new HashMap<>(); expressionAttributeNames.put(PK_PATH_EXPRESSION_VARIABLE, partitionKeyName);
/** * Deletes an order. * @param orderId order id of order to delete * @return the deleted order */ public Order deleteOrder(final String orderId) { final DeleteItemResponse result; try { return Optional.ofNullable(dynamoDb.deleteItem(DeleteItemRequest.builder() .tableName(tableName) .key(Collections.singletonMap(ORDER_ID, AttributeValue.builder().s(orderId).build())) .conditionExpression("attribute_exists(orderId)") .returnValues(ReturnValue.ALL_OLD) .build())) .map(DeleteItemResponse::attributes) .map(this::convert) .orElseThrow(() -> new IllegalStateException( "Condition passed but deleted item was null")); } catch (ConditionalCheckFailedException e) { throw new UnableToDeleteException( "A competing request changed the order while processing this request"); } catch (ResourceNotFoundException e) { throw new TableDoesNotExistException("Order table " + tableName + " does not exist and was deleted after reading the order"); } }
/** * <p> * Represents the data for the expected attribute. * </p> * <p> * Each attribute value is described as a name-value pair. The name is the data type, and the value is the data * itself. * </p> * <p> * For more information, see <a href= * "http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes" * >Data Types</a> in the <i>Amazon DynamoDB Developer Guide</i>. * </p> * This is a convenience that creates an instance of the {@link AttributeValue.Builder} avoiding the need to * create one manually via {@link AttributeValue#builder()}. * * When the {@link Consumer} completes, {@link AttributeValue.Builder#build()} is called immediately and its * result is passed to {@link #value(AttributeValue)}. * * @param value * a consumer that will call methods on {@link AttributeValue.Builder} * @return Returns a reference to this object so that method calls can be chained together. * @see #value(AttributeValue) */ default Builder value(Consumer<AttributeValue.Builder> value) { return value(AttributeValue.builder().applyMutation(value).build()); }
/** * <p> * Represents the data for an attribute. * </p> * <p> * Each attribute value is described as a name-value pair. The name is the data type, and the value is the data * itself. * </p> * <p> * For more information, see <a href= * "http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes" * >Data Types</a> in the <i>Amazon DynamoDB Developer Guide</i>. * </p> * This is a convenience that creates an instance of the {@link AttributeValue.Builder} avoiding the need to * create one manually via {@link AttributeValue#builder()}. * * When the {@link Consumer} completes, {@link AttributeValue.Builder#build()} is called immediately and its * result is passed to {@link #value(AttributeValue)}. * * @param value * a consumer that will call methods on {@link AttributeValue.Builder} * @return Returns a reference to this object so that method calls can be chained together. * @see #value(AttributeValue) */ default Builder value(Consumer<AttributeValue.Builder> value) { return value(AttributeValue.builder().applyMutation(value).build()); }
/** * Returns an order or throws if the order does not exist. * @param orderId id of order to get * @return the order if it exists * @throws OrderDoesNotExistException if the order does not exist */ public Order getOrder(final String orderId) { try { return Optional.ofNullable( dynamoDb.getItem(GetItemRequest.builder() .tableName(tableName) .key(Collections.singletonMap(ORDER_ID, AttributeValue.builder().s(orderId).build())) .build())) .map(GetItemResponse::item) .map(this::convert) .orElseThrow(() -> new OrderDoesNotExistException("Order " + orderId + " does not exist")); } catch (ResourceNotFoundException e) { throw new TableDoesNotExistException("Order table " + tableName + " does not exist"); } }
private Map<String, AttributeValue> createOrderItem(final CreateOrderRequest order) { Map<String, AttributeValue> item = new HashMap<>(); item.put(ORDER_ID, AttributeValue.builder().s(UUID.randomUUID().toString()).build()); item.put("version", AttributeValue.builder().n("1").build()); item.put("customerId", AttributeValue.builder().s(validateCustomerId(order.getCustomerId())).build()); try { item.put("preTaxAmount", AttributeValue.builder().n(order.getPreTaxAmount().toString()).build()); } catch (NullPointerException e) { throw new IllegalArgumentException(PRE_TAX_AMOUNT_WAS_NULL); } try { item.put("postTaxAmount", AttributeValue.builder().n(order.getPostTaxAmount().toString()).build()); } catch (NullPointerException e) { throw new IllegalArgumentException(POST_TAX_AMOUNT_WAS_NULL); } return item; }
private GetItemResponse readFromDynamoDB(final String key, final Optional<String> sortKey) { final Map<String, AttributeValue> dynamoDBKey = new HashMap<>(); dynamoDBKey.put(this.partitionKeyName, AttributeValue.builder().s(key).build()); if (this.sortKeyName.isPresent()) { dynamoDBKey.put(this.sortKeyName.get(), AttributeValue.builder().s(sortKey.get()).build()); } final GetItemRequest getItemRequest = GetItemRequest.builder().tableName(tableName).key(dynamoDBKey) .consistentRead(true) .build(); return this.dynamoDB.getItem(getItemRequest); }
private Map<String, AttributeValue> getKeys(String partitionKey, Optional<String> sortKey) { final Map<String, AttributeValue> key = new HashMap<>(); key.put(this.partitionKeyName, AttributeValue.builder().s(partitionKey).build()); if (sortKey.isPresent()) { key.put(this.sortKeyName.get(), AttributeValue.builder().s(sortKey.get()).build()); } return key; }