Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
the specific language governing permissions and limitations under the License.
*/
package com.amazon.ask.attributes.persistence.impl;
import com.amazon.ask.attributes.persistence.PersistenceAdapter;
import com.amazon.ask.exception.PersistenceException;
import com.amazon.ask.model.RequestEnvelope;
import com.amazon.ask.util.ValidationUtils;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.ItemUtils;
import com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.DeleteItemRequest;
import com.amazonaws.services.dynamodbv2.model.GetItemRequest;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.PutItemRequest;
import com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
import com.amazonaws.services.dynamodbv2.util.TableUtils;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
/**
* Persistence adapter for storing skill persistence attributes in Amazon DynamoDB.
*/
public final class DynamoDbPersistenceAdapter implements PersistenceAdapter {
/**
* Amazon DynamoDb client.
*/
private final AmazonDynamoDB dynamoDb;
/**
* Table name to be used/created.
*/
private final String tableName;
/**
* A simple primary key, composed of one attribute known as the partition key. DynamoDB uses the partition key's
* value as input to an internal hash function. The output from the hash function determines the partition
* (physical storage internal to DynamoDB) in which the item will be stored.
*/
private final String partitionKeyName;
/**
* Referred to as a composite primary key, this type of key is composed of two attributes.
* The first attribute is the partition key, and the second attribute is the sort key.
*/
private final String attributesKeyName;
/**
* Partition key generator.
*/
private final Function partitionKeyGenerator;
/**
* When set to true, creates table if table doesn't exist.
*/
private boolean autoCreateTable;
/**
* Default partition key name.
*/
private static final String DEFAULT_PARTITION_KEY_NAME = "id";
/**
* Default attributes key name.
*/
private static final String DEFAULT_ATTRIBUTES_KEY_NAME = "attributes";
/**
* Default value for auto create table.
*/
private static final boolean DEFAULT_AUTO_CREATE_TABLE = false;
/**
* Default partition key generator.
*/
private static final Function DEFAULT_PARTITION_KEY_GENERATOR = PartitionKeyGenerators.userId();
/**
* Read capacity unit represents one strongly consistent read per second, or two eventually consistent reads per second,
* for items up to 4 KB in size. If you need to read an item that is larger than 4 KB, DynamoDB will need to consume additional
* read capacity units.
*/
private static final Long DEFAULT_READ_CAPACITY_UNITS = 5L;
/**
* Private constructor to build an instance of {@link DynamoDbPersistenceAdapter}.
* @param builder instance of {@link Builder}.
*/
private DynamoDbPersistenceAdapter(final Builder builder) {
this.tableName = ValidationUtils.assertStringNotEmpty(builder.tableName, "table name");
this.dynamoDb = builder.dynamoDb != null ? builder.dynamoDb : AmazonDynamoDBClientBuilder.standard().build();
this.partitionKeyName = builder.partitionKeyName;
this.attributesKeyName = builder.attributesKeyName;
this.partitionKeyGenerator = builder.partitionKeyGenerator;
this.autoCreateTable = builder.autoCreateTable;
autoCreateTableIfNotExists();
}
/**
* Static method to build an instance of Builder.
* @return {@link Builder}.
*/
public static Builder builder() {
return new Builder();
}
/**
* Gets attributes from DynamoDB table.
* @param envelope instance of {@link RequestEnvelope}.
* @throws PersistenceException if table doesn't exist or attributes retrieval fails.
* @return {@link Map} of String, Object if attributes exist, or an empty {@link Optional} if not.
*/
@Override
public Optional