Search in sources :

Example 1 with KeyDescription

use of com.amazonaws.mobileconnectors.dynamodbv2.document.internal.KeyDescription in project aws-sdk-android by aws-amplify.

the class Table method loadTableDescription.

/**
 * Load table description.
 */
public void loadTableDescription() {
    clear();
    final DescribeTableRequest request = Table.appendDynamoDBDocumentUserAgentString(new DescribeTableRequest(this.tableName));
    final DescribeTableResult result = client.describeTable(request);
    this.tableDescription = result.getTable();
    for (final KeySchemaElement element : this.tableDescription.getKeySchema()) {
        final String keyName = element.getAttributeName();
        for (final AttributeDefinition ad : this.tableDescription.getAttributeDefinitions()) {
            if (ad.getAttributeName().equals(keyName)) {
                final KeyDescription kd = new KeyDescription();
                kd.setHash("HASH".equalsIgnoreCase(element.getKeyType()));
                kd.setType(getType(ad.getAttributeType()));
                if (kd.isHash()) {
                    this.hashKeys.add(keyName);
                } else {
                    this.rangeKeys.add(keyName);
                }
                this.keys.put(keyName, kd);
                break;
            }
        }
    }
    if (this.tableDescription.getLocalSecondaryIndexes() != null) {
        for (final LocalSecondaryIndexDescription index : this.tableDescription.getLocalSecondaryIndexes()) {
            this.localSecondaryIndexes.put(index.getIndexName(), index);
            this.localSecondaryIndexNames.add(index.getIndexName());
        }
    }
    if (this.tableDescription.getGlobalSecondaryIndexes() != null) {
        for (final GlobalSecondaryIndexDescription index : this.tableDescription.getGlobalSecondaryIndexes()) {
            this.globalSecondaryIndexes.put(index.getIndexName(), index);
            this.globalSecondaryIndexNames.add(index.getIndexName());
        }
    }
    for (final AttributeDefinition ad : this.tableDescription.getAttributeDefinitions()) {
        this.attributes.add(ad);
    }
}
Also used : AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) GlobalSecondaryIndexDescription(com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndexDescription) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) DescribeTableResult(com.amazonaws.services.dynamodbv2.model.DescribeTableResult) KeyDescription(com.amazonaws.mobileconnectors.dynamodbv2.document.internal.KeyDescription) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) LocalSecondaryIndexDescription(com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndexDescription)

Example 2 with KeyDescription

use of com.amazonaws.mobileconnectors.dynamodbv2.document.internal.KeyDescription in project aws-sdk-android by aws-amplify.

the class Table method makeKey.

private Key makeKey(Primitive hashKey, Primitive rangeKey) {
    final Key key = new Key();
    if (this.hashKeys.size() != 1) {
        throw new IllegalStateException("no hashkeys in table");
    }
    final String hashKeyName = this.hashKeys.get(0);
    final KeyDescription keyDescription = this.keys.get(hashKeyName);
    if (keyDescription.getType() != hashKey.getType()) {
        throw new IllegalStateException("hash key type does not match the one in table definition");
    }
    final AttributeValue av = hashKey.convertToAttributeValue();
    key.put(hashKeyName, av);
    if (rangeKey == null && this.rangeKeys.size() > 0) {
        throw new IllegalStateException("range key not specificed for a table with range keys");
    } else if (rangeKey != null) {
        final String rangeKeyName = this.rangeKeys.get(0);
        final KeyDescription kd = this.keys.get(rangeKeyName);
        if (kd.getType() != rangeKey.getType()) {
            throw new IllegalStateException("range key type does not match that of table definition");
        }
        final AttributeValue rangeKeyAttributeValue = rangeKey.convertToAttributeValue();
        key.put(rangeKeyName, rangeKeyAttributeValue);
    }
    return key;
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) KeyDescription(com.amazonaws.mobileconnectors.dynamodbv2.document.internal.KeyDescription) Key(com.amazonaws.mobileconnectors.dynamodbv2.document.internal.Key)

Example 3 with KeyDescription

use of com.amazonaws.mobileconnectors.dynamodbv2.document.internal.KeyDescription in project aws-sdk-android by aws-amplify.

the class Table method makeKey.

private Key makeKey(Document doc) {
    final Key key = new Key();
    for (final Entry<String, KeyDescription> kvp : this.keys.entrySet()) {
        final String keyName = kvp.getKey();
        final KeyDescription description = kvp.getValue();
        final DynamoDBEntry value = doc.get(keyName);
        if (value == null) {
            throw new IllegalStateException("no value for key " + keyName);
        }
        final Primitive primitive = value.asPrimitive();
        if (primitive == null) {
            throw new IllegalStateException("Key attribute " + keyName + " must be a Primitive type");
        }
        if (primitive.getType() != description.getType()) {
            throw new IllegalStateException("Key attribute " + keyName + " must be of type " + description.getType());
        }
        key.put(keyName, primitive.convertToAttributeValue());
    }
    return key;
}
Also used : DynamoDBEntry(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBEntry) Primitive(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Primitive) KeyDescription(com.amazonaws.mobileconnectors.dynamodbv2.document.internal.KeyDescription) Key(com.amazonaws.mobileconnectors.dynamodbv2.document.internal.Key)

Aggregations

KeyDescription (com.amazonaws.mobileconnectors.dynamodbv2.document.internal.KeyDescription)3 Key (com.amazonaws.mobileconnectors.dynamodbv2.document.internal.Key)2 DynamoDBEntry (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBEntry)1 Primitive (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Primitive)1 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)1 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)1 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)1 DescribeTableResult (com.amazonaws.services.dynamodbv2.model.DescribeTableResult)1 GlobalSecondaryIndexDescription (com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndexDescription)1 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)1 LocalSecondaryIndexDescription (com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndexDescription)1