Skip to content

Commit

Permalink
refactor: DRY GetItemRequest creation in AwsItem
Browse files Browse the repository at this point in the history
  • Loading branch information
sandordargo committed Oct 20, 2017
1 parent 79f82a3 commit b771b30
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/main/java/com/jcabi/dynamo/AwsItem.java
Expand Up @@ -115,12 +115,7 @@ public boolean has(final String attr) throws IOException {
if (!has) {
final AmazonDynamoDB aws = this.credentials.aws();
try {
final GetItemRequest request = new GetItemRequest();
request.setTableName(this.name);
request.setAttributesToGet(Collections.singletonList(attr));
request.setKey(this.attributes.only(this.keys));
request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
request.setConsistentRead(true);
final GetItemRequest request = this.makeItemRequestFor(attr);
final long start = System.currentTimeMillis();
final GetItemResult result = aws.getItem(request);
has = result.getItem().get(attrib) != null;
Expand Down Expand Up @@ -154,12 +149,7 @@ public AttributeValue get(final String attr) throws IOException {
if (value == null) {
final AmazonDynamoDB aws = this.credentials.aws();
try {
final GetItemRequest request = new GetItemRequest();
request.setTableName(this.name);
request.setAttributesToGet(Collections.singletonList(attrib));
request.setKey(this.attributes.only(this.keys));
request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
request.setConsistentRead(true);
final GetItemRequest request = this.makeItemRequestFor(attrib);
final long start = System.currentTimeMillis();
final GetItemResult result = aws.getItem(request);
value = result.getItem().get(attrib);
Expand Down Expand Up @@ -241,4 +231,19 @@ public Frame frame() {
return this.frm;
}

/**
* Makes a GetItemRequest for a given attribute.
* @param attr Attribute name
* @return GetItemRequest
*/
private GetItemRequest makeItemRequestFor(final String attr) {
final GetItemRequest request = new GetItemRequest();
request.setTableName(this.name);
request.setAttributesToGet(Collections.singletonList(attr));
request.setKey(this.attributes.only(this.keys));
request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
request.setConsistentRead(true);
return request;
}

}

0 comments on commit b771b30

Please sign in to comment.