Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriesen committed Jun 4, 2024
1 parent 7d86690 commit 5603d95
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

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;
import software.amazon.awssdk.services.dynamodb.model.BatchWriteItemRequest;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse;
import software.amazon.awssdk.services.dynamodb.model.DeleteRequest;
Expand All @@ -59,9 +60,9 @@
public class DynamoDbServiceImpl implements DynamoDbService {

/** {@link DynamoDbClient}. */
private DynamoDbClient dbClient;
private final DynamoDbClient dbClient;
/** Table Name. */
private String tableName;
private final String tableName;

/**
* constructor.
Expand Down Expand Up @@ -111,7 +112,7 @@ public QueryResponse between(final QueryConfig config, final AttributeValue pk,
QueryRequest.builder().tableName(this.tableName).keyConditionExpression(expression)
.expressionAttributeValues(values).scanIndexForward(config.isScanIndexForward())
.projectionExpression(config.projectionExpression()).indexName(config.indexName())
.exclusiveStartKey(startkey).limit(Integer.valueOf(limit)).build();
.exclusiveStartKey(startkey).limit(limit).build();

return this.dbClient.query(q);
}
Expand Down Expand Up @@ -159,8 +160,7 @@ public boolean deleteItemsBeginsWith(final AttributeValue pk, final AttributeVal

QueryResponse response = queryBeginsWith(config, pk, sk, startkey, limit);

List<Map<String, AttributeValue>> attrs =
response.items().stream().collect(Collectors.toList());
List<Map<String, AttributeValue>> attrs = response.items().stream().toList();
list.addAll(attrs);

startkey = response.lastEvaluatedKey();
Expand Down Expand Up @@ -210,9 +210,9 @@ public List<Map<String, AttributeValue>> getBatch(final BatchGetConfig config,
list = batchReadItems.get(this.tableName);

Map<String, Map<String, AttributeValue>> data =
list.stream().collect(Collectors.toMap(l -> getKey(l), l -> l));
list.stream().collect(Collectors.toMap(this::getKey, l -> l));

list = keys.stream().map(k -> data.get(getKey(k))).filter(k -> k != null)
list = keys.stream().map(k -> data.get(getKey(k))).filter(Objects::nonNull)
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -287,11 +287,11 @@ private void putItem(final String dynamoDbTable, final Map<String, AttributeValu
public void putItems(final List<Map<String, AttributeValue>> attrs) {

if (!attrs.isEmpty()) {
Map<String, Collection<WriteRequest>> items =
new AttributeValuesToWriteRequests(this.tableName).apply(attrs);

BatchWriteItemRequest batch = BatchWriteItemRequest.builder().requestItems(items).build();
this.dbClient.batchWriteItem(batch);
WriteRequestBuilder builder = new WriteRequestBuilder();
for (Map<String, AttributeValue> attr : attrs) {
builder.append(this.tableName, attr);
}
builder.batchWriteItem(this.dbClient);
}
}

Expand All @@ -315,7 +315,7 @@ public QueryResponse query(final QueryConfig config, final AttributeValue pk,
.expressionAttributeNames(config.expressionAttributeNames())
.keyConditionExpression(expression).projectionExpression(config.projectionExpression())
.expressionAttributeValues(values).scanIndexForward(config.isScanIndexForward())
.exclusiveStartKey(exclusiveStartKey).limit(Integer.valueOf(limit)).build();
.exclusiveStartKey(exclusiveStartKey).limit(limit).build();

return this.dbClient.query(q);
}
Expand All @@ -332,7 +332,7 @@ public QueryResponse query(final QueryConfig config, final AttributeValue pk,
.expressionAttributeNames(config.expressionAttributeNames())
.keyConditionExpression(expression).projectionExpression(config.projectionExpression())
.expressionAttributeValues(values).scanIndexForward(config.isScanIndexForward())
.exclusiveStartKey(exclusiveStartKey).limit(Integer.valueOf(limit)).build();
.exclusiveStartKey(exclusiveStartKey).limit(limit).build();

return this.dbClient.query(q);
}
Expand All @@ -356,10 +356,9 @@ public QueryResponse queryBeginsWith(final QueryConfig config, final AttributeVa
QueryRequest.builder().tableName(this.tableName).keyConditionExpression(expression)
.expressionAttributeValues(values).scanIndexForward(config.isScanIndexForward())
.projectionExpression(config.projectionExpression()).indexName(config.indexName())
.exclusiveStartKey(exclusiveStartKey).limit(Integer.valueOf(limit)).build();
.exclusiveStartKey(exclusiveStartKey).limit(limit).build();

QueryResponse response = this.dbClient.query(q);
return response;
return this.dbClient.query(q);
}

@Override
Expand All @@ -372,7 +371,7 @@ public QueryResponse queryIndex(final String indexName, final AttributeValue pk,
QueryRequest q =
QueryRequest.builder().tableName(this.tableName).keyConditionExpression(expression)
.expressionAttributeValues(values).scanIndexForward(Boolean.FALSE).indexName(indexName)
.exclusiveStartKey(exclusiveStartKey).limit(Integer.valueOf(limit)).build();
.exclusiveStartKey(exclusiveStartKey).limit(limit).build();

return this.dbClient.query(q);
}
Expand All @@ -395,9 +394,8 @@ public Map<String, AttributeValue> updateValues(final AttributeValue pk, final A
final Map<String, AttributeValue> updateValues) {

Map<String, AttributeValueUpdate> values = new HashMap<>();
updateValues.forEach((key, value) -> {
values.put(key, AttributeValueUpdate.builder().value(value).build());
});
updateValues.forEach(
(key, value) -> values.put(key, AttributeValueUpdate.builder().value(value).build()));

return updateItem(pk, sk, values);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class WriteRequestBuilder {
/** Max Batch Size. */
private static final int MAX_BATCH_SIZE = 25;
/** {@link Map} of {@link WriteRequest}. */
private Map<String, List<WriteRequest>> items = new HashMap<>();
private final Map<String, List<WriteRequest>> items = new HashMap<>();

/**
* constructor.
Expand Down

0 comments on commit 5603d95

Please sign in to comment.