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

com.nimbusds.infinispan.persistence.dynamodb.query.SimpleMatchQueryExecutor Maven / Gradle / Ivy

package com.nimbusds.infinispan.persistence.dynamodb.query;


import java.util.function.Consumer;

import com.amazonaws.services.dynamodbv2.document.Index;
import com.amazonaws.services.dynamodbv2.document.ItemCollection;
import com.amazonaws.services.dynamodbv2.document.QueryOutcome;
import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
import com.amazonaws.services.dynamodbv2.document.utils.NameMap;
import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
import com.nimbusds.infinispan.persistence.common.InfinispanEntry;
import com.nimbusds.infinispan.persistence.common.query.Query;
import com.nimbusds.infinispan.persistence.common.query.SimpleMatchQuery;
import com.nimbusds.infinispan.persistence.common.query.UnsupportedQueryException;
import net.jcip.annotations.ThreadSafe;


/**
 * Simple match DynamoDB query executor. Accepts queries of type
 * {@link SimpleMatchQuery} where both key name and value must be of type
 * {@link String}.
 */
@ThreadSafe
public class SimpleMatchQueryExecutor implements DynamoDBQueryExecutor {
	
	
	/**
	 * The initialisation context.
	 */
	private DynamoDBQueryExecutorInitContext initCtx;
	
	
	@Override
	public void init(final DynamoDBQueryExecutorInitContext initCtx) {
		this.initCtx = initCtx;
	}
	
	
	/**
	 * Returns the DynamoDB query spec for the specified simple match
	 * query.
	 *
	 * @param simpleMatchQuery The simple match query. Must not be
	 *                         {@code null}.
	 *
	 * @return The DynamoDB query spec.
	 */
	private static QuerySpec toQuerySpec(final SimpleMatchQuery simpleMatchQuery) {
		
		return new QuerySpec()
			.withKeyConditionExpression("#k = :value")
			.withNameMap(new NameMap()
				.with("#k", simpleMatchQuery.getKey()))
			.withValueMap(new ValueMap()
				.withString(":value", simpleMatchQuery.getValue()));
	}
	
	
	@Override
	@SuppressWarnings("unchecked")
	public void executeQuery(final Query query, final Consumer> consumer) {
		
		if (! (query instanceof SimpleMatchQuery)) {
			throw new UnsupportedQueryException(query);
		}
		
		SimpleMatchQuery simpleMatchQuery = (SimpleMatchQuery)query;
		
		QuerySpec querySpec = toQuerySpec(simpleMatchQuery);
		
		Index index = initCtx.getDynamoDBIndex(simpleMatchQuery.getKey());
		
		ItemCollection items = index.query(querySpec);
		
		items.forEach(item -> consumer.accept(initCtx.getDynamoDBItemTransformer().toInfinispanEntry(item)));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy