All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.amazonaws.services.dynamodbv2.document.internal.QueryImpl Maven / Gradle / Ivy

Go to download

The AWS SDK for Java with support for OSGi. The AWS SDK for Java provides Java APIs for building software on AWS' cost-effective, scalable, and reliable infrastructure products. The AWS Java SDK allows developers to code against APIs for all of Amazon's infrastructure web services (Amazon S3, Amazon EC2, Amazon SQS, Amazon Relational Database Service, Amazon AutoScaling, etc).

There is a newer version: 1.11.60
Show newest version
/*
 * Copyright 2014-2016 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.amazonaws.services.dynamodbv2.document.internal;

import java.util.Collection;
import java.util.Map;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.document.ItemCollection;
import com.amazonaws.services.dynamodbv2.document.KeyAttribute;
import com.amazonaws.services.dynamodbv2.document.KeyConditions;
import com.amazonaws.services.dynamodbv2.document.QueryFilter;
import com.amazonaws.services.dynamodbv2.document.QueryOutcome;
import com.amazonaws.services.dynamodbv2.document.RangeKeyCondition;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.api.QueryApi;
import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.amazonaws.services.dynamodbv2.model.QueryRequest;

/**
 * The implementation for QueryApi of a table.
 */
public class QueryImpl extends AbstractImpl implements QueryApi {
    public QueryImpl(AmazonDynamoDB client, Table table) {
        super(client, table);
    }

    @Override
    public ItemCollection query(String hashKeyName, Object hashKey) {
        return doQuery(new QuerySpec()
            .withHashKey(new KeyAttribute(hashKeyName, hashKey)));
    }

    @Override
    public ItemCollection query(KeyAttribute hashKey) {
        return doQuery(new QuerySpec().withHashKey(hashKey));
    }

    @Override
    public ItemCollection query(KeyAttribute hashKey,
            RangeKeyCondition rangeKeyCondition) {
        return doQuery(new QuerySpec().withHashKey(hashKey)
                .withRangeKeyCondition(rangeKeyCondition));
    }

    @Override
    public ItemCollection query(KeyAttribute hashKey,
            RangeKeyCondition rangeKeyCondition, QueryFilter... queryFilters) {
        return doQuery(new QuerySpec().withHashKey(hashKey)
                .withRangeKeyCondition(rangeKeyCondition)
                .withQueryFilters(queryFilters));
    }

    @Override
    public ItemCollection query(KeyAttribute hashKey,
            RangeKeyCondition rangeKeyCondition, String filterExpression,
            Map nameMap, Map valueMap) {
        return doQuery(new QuerySpec().withHashKey(hashKey)
                .withRangeKeyCondition(rangeKeyCondition)
                .withFilterExpression(filterExpression)
                .withNameMap(nameMap)
                .withValueMap(valueMap));
    }

    @Override
    public ItemCollection query(KeyAttribute hashKey,
            RangeKeyCondition rangeKeyCondition, String filterExpression,
            String projectionExpression, Map nameMap,
            Map valueMap) {
        return doQuery(new QuerySpec().withHashKey(hashKey)
                .withRangeKeyCondition(rangeKeyCondition)
                .withFilterExpression(filterExpression)
                .withProjectionExpression(projectionExpression)
                .withNameMap(nameMap)
                .withValueMap(valueMap));
    }

    @Override
    public ItemCollection query(QuerySpec spec) {
        return doQuery(spec);
    }

    protected ItemCollection doQuery(QuerySpec spec) {
        // set the table name
        String tableName = getTable().getTableName();
        QueryRequest req = spec.getRequest().withTableName(tableName);
        // hash key
        final KeyAttribute hashKey = spec.getHashKey();
        if (hashKey != null) {
            req.addKeyConditionsEntry(hashKey.getName(),
                    new Condition()
                    .withComparisonOperator(ComparisonOperator.EQ)
                    .withAttributeValueList(InternalUtils.toAttributeValue(hashKey.getValue()))
            );
        }
        // range key condition
        RangeKeyCondition rangeKeyCond = spec.getRangeKeyCondition();
        if (rangeKeyCond != null) {
            KeyConditions keyCond = rangeKeyCond.getKeyCondition();
            if (keyCond == null)
                throw new IllegalArgumentException("key condition not specified in range key condition");
            Object[] values = rangeKeyCond.getValues();
            if (values == null)
                throw new IllegalArgumentException("key condition values not specified in range key condition");
            req.addKeyConditionsEntry(rangeKeyCond.getAttrName(),
                    new Condition()
                    .withComparisonOperator(keyCond.toComparisonOperator())
                    .withAttributeValueList(InternalUtils.toAttributeValues(values))
            );
        }
        // query filters;
        Collection filters = spec.getQueryFilters();
        if (filters != null) {
            req.setQueryFilter(InternalUtils.toAttributeConditionMap(filters));
        }

        // set up the start key, if any
        Collection startKey = spec.getExclusiveStartKey();
        if (startKey != null)
            req.setExclusiveStartKey(InternalUtils.toAttributeValueMap(startKey));

        // set up the value map, if any (when expression API is used)
        final Map attrValMap = InternalUtils.fromSimpleMap(spec.getValueMap());
        // set up expressions, if any
        req.withExpressionAttributeNames(spec.getNameMap())
           .withExpressionAttributeValues(attrValMap)
           ;
        return new QueryCollection(getClient(), spec);
    }

    @Override
    public ItemCollection query(String hashKeyName,
            Object hashKeyValue, RangeKeyCondition rangeKeyCondition) {
        return query(new KeyAttribute(hashKeyName, hashKeyValue), rangeKeyCondition);
    }

    @Override
    public ItemCollection query(String hashKeyName,
            Object hashKeyValue, RangeKeyCondition rangeKeyCondition,
            QueryFilter... queryFilters) {
        return query(new KeyAttribute(hashKeyName, hashKeyValue),
                rangeKeyCondition, queryFilters);
    }

    @Override
    public ItemCollection query(String hashKeyName,
            Object hashKeyValue, RangeKeyCondition rangeKeyCondition,
            String filterExpression, Map nameMap,
            Map valueMap) {
        return query(new KeyAttribute(hashKeyName, hashKeyValue),
                rangeKeyCondition, filterExpression, nameMap, valueMap);
    }

    @Override
    public ItemCollection query(String hashKeyName,
            Object hashKeyValue, RangeKeyCondition rangeKeyCondition,
            String filterExpression, String projectionExpression,
            Map nameMap, Map valueMap) {
        return query(new KeyAttribute(hashKeyName, hashKeyValue),
                rangeKeyCondition, filterExpression, projectionExpression,
                nameMap, valueMap);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy