Skip to content

Commit

Permalink
added ScanIndexForward to QueryConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriesen committed Jul 31, 2023
1 parent 5a590bc commit e61cf72
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import static com.formkiq.aws.dynamodb.DbKeys.PK;
import static com.formkiq.aws.dynamodb.DbKeys.SK;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.formkiq.aws.dynamodb.objects.Strings;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate;
Expand Down Expand Up @@ -125,13 +127,17 @@ public Map<String, AttributeValue> get(final QueryConfig config, final Attribute
public List<Map<String, AttributeValue>> getBatch(
final Collection<Map<String, AttributeValue>> keys) {

Map<String, KeysAndAttributes> items =
Map.of(this.tableName, KeysAndAttributes.builder().keys(keys).build());
List<Map<String, AttributeValue>> list = Collections.emptyList();

BatchGetItemResponse response =
this.dbClient.batchGetItem(BatchGetItemRequest.builder().requestItems(items).build());
if (!keys.isEmpty()) {
Map<String, KeysAndAttributes> items =
Map.of(this.tableName, KeysAndAttributes.builder().keys(keys).build());

List<Map<String, AttributeValue>> list = response.responses().get(this.tableName);
BatchGetItemResponse response =
this.dbClient.batchGetItem(BatchGetItemRequest.builder().requestItems(items).build());

list = response.responses().get(this.tableName);
}
return list;
}

Expand Down Expand Up @@ -181,13 +187,14 @@ public QueryResponse queryBeginsWith(final QueryConfig config, final AttributeVa
final AttributeValue sk, final Map<String, AttributeValue> exclusiveStartKey,
final int limit) {

String expression = PK + " = :pk and begins_with(" + SK + ",:sk)";
String gsi = Strings.isEmpty(config.indexName()) ? "" : config.indexName();
String expression = gsi + PK + " = :pk and begins_with(" + gsi + SK + ",:sk)";

Map<String, AttributeValue> values = Map.of(":pk", pk, ":sk", sk);

QueryRequest q =
QueryRequest.builder().tableName(this.tableName).keyConditionExpression(expression)
.expressionAttributeValues(values).scanIndexForward(Boolean.FALSE)
.expressionAttributeValues(values).scanIndexForward(config.isScanIndexForward())
.projectionExpression(config.projectionExpression()).indexName(config.indexName())
.exclusiveStartKey(exclusiveStartKey).limit(Integer.valueOf(limit)).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class QueryConfig {
private String indexName;
/** Projection Expression. */
private String projectionExpression;
/** True - ASC, False - DESC. */
private Boolean scanIndexForward = Boolean.FALSE;

/**
* constructor.
Expand Down Expand Up @@ -86,6 +88,15 @@ public QueryConfig indexName(final String index) {
return this;
}

/**
* Is ScanIndexForward.
*
* @return boolean
*/
public Boolean isScanIndexForward() {
return this.scanIndexForward;
}

/**
* Get Projection Expression.
*
Expand All @@ -106,4 +117,15 @@ public QueryConfig projectionExpression(final String projection) {
return this;
}

/**
* Set Is ScanIndexForward.
*
* @param isScanIndexForward {@link Boolean}
* @return {@link QueryConfig}
*/
public QueryConfig scanIndexForward(final Boolean isScanIndexForward) {
this.scanIndexForward = isScanIndexForward;
return this;
}

}

0 comments on commit e61cf72

Please sign in to comment.