io.dataspray.singletable.builder.CombinedQueryBuilder Maven / Gradle / Ivy
Show all versions of single-table Show documentation
package io.dataspray.singletable.builder;
import com.google.common.collect.ImmutableSet;
import io.dataspray.singletable.ExpressionBuilder;
import io.dataspray.singletable.Schema;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.QueryRequest;
import software.amazon.awssdk.services.dynamodb.model.QueryResponse;
import java.util.Optional;
import java.util.stream.Stream;
import static com.google.common.base.Preconditions.checkState;
public abstract class CombinedQueryBuilder extends ExpressionBuilder implements FilterExpressionBuilder, ConditionExpressionBuilder
{
protected final Optional indexNameOpt;
public CombinedQueryBuilder(Schema schema, Optional indexNameOpt) {
super(schema);
this.indexNameOpt = indexNameOpt;
}
public QueryRequest.Builder builder() {
Expression expression = buildExpression();
QueryRequest.Builder builder = QueryRequest.builder();
builder.tableName(schema.tableName());
indexNameOpt.ifPresent(builder::indexName);
checkState(expression.updateExpression().isEmpty(), "Query does not support update expression");
expression.filterExpression().ifPresent(builder::filterExpression);
expression.expressionAttributeNames().ifPresent(builder::expressionAttributeNames);
expression.expressionAttributeValues().ifPresent(builder::expressionAttributeValues);
expression.builderAdjustments().forEach(c -> c.accept(builder));
return builder;
}
public QueryRequest build() {
return builder().build();
}
public QueryResponse execute(DynamoDbClient dynamo) {
return dynamo.query(build());
}
public Stream executeStream(DynamoDbClient dynamo) {
return executeStreamBatch(dynamo).flatMap(ImmutableSet::stream);
}
public abstract Stream> executeStreamBatch(DynamoDbClient dynamo);
}