001package com.nimbusds.infinispan.persistence.dynamodb.query;
002
003
004import java.util.function.Consumer;
005
006import com.amazonaws.services.dynamodbv2.document.Index;
007import com.amazonaws.services.dynamodbv2.document.ItemCollection;
008import com.amazonaws.services.dynamodbv2.document.QueryOutcome;
009import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
010import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
011import com.nimbusds.infinispan.persistence.common.InfinispanEntry;
012import com.nimbusds.infinispan.persistence.common.query.Query;
013import com.nimbusds.infinispan.persistence.common.query.SimpleMatchQuery;
014import com.nimbusds.infinispan.persistence.common.query.UnsupportedQueryException;
015import net.jcip.annotations.ThreadSafe;
016
017
018/**
019 * Simple match DynamoDB query executor. Accepts queries of type
020 * {@link SimpleMatchQuery} where both key name and value must be of type
021 * {@link String}.
022 */
023@ThreadSafe
024public class SimpleMatchQueryExecutor<K, V> implements DynamoDBQueryExecutor<K, V> {
025        
026        
027        /**
028         * The initialisation context.
029         */
030        private DynamoDBQueryExecutorInitContext<K, V> initCtx;
031        
032        
033        @Override
034        public void init(final DynamoDBQueryExecutorInitContext<K, V> initCtx) {
035                this.initCtx = initCtx;
036        }
037        
038        
039        /**
040         * Returns the DynamoDB query spec for the specified simple match
041         * query.
042         *
043         * @param simpleMatchQuery The simple match query. Must not be
044         *                         {@code null}.
045         *
046         * @return The DynamoDB query spec.
047         */
048        private static QuerySpec toQuerySpec(final SimpleMatchQuery<String, String> simpleMatchQuery) {
049                
050                return new QuerySpec()
051                        .withKeyConditionExpression(simpleMatchQuery.getKey() + " = :value")
052                        .withValueMap(new ValueMap()
053                                .withString(":value", simpleMatchQuery.getValue()));
054        }
055        
056        
057        @Override
058        @SuppressWarnings("unchecked")
059        public void executeQuery(final Query query, final Consumer<InfinispanEntry<K, V>> consumer) {
060                
061                if (! (query instanceof SimpleMatchQuery)) {
062                        throw new UnsupportedQueryException(query);
063                }
064                
065                SimpleMatchQuery<String, String> simpleMatchQuery = (SimpleMatchQuery<String, String>)query;
066                
067                QuerySpec querySpec = toQuerySpec(simpleMatchQuery);
068                
069                Index index = initCtx.getDynamoDBIndex(simpleMatchQuery.getKey());
070                
071                ItemCollection<QueryOutcome> items = index.query(querySpec);
072                
073                items.forEach(item -> consumer.accept(initCtx.getDynamoDBItemTransformer().toInfinispanEntry(item)));
074        }
075}