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

io.dataspray.singletable.builder.CombinedQueryBuilder Maven / Gradle / Ivy

Go to download

DynamoDB best practices encourages a single-table design that allows multiple record types to reside within the same table. The goal of this library is to improve the experience of Java developers and make it safer to define non-conflicting schema of each record, serializing and deserializing automatically and working with secondary indexes.

The newest version!
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); }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy