Skip to content

Commit

Permalink
#84 COUNT for Scan
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Nov 18, 2016
1 parent a71cb74 commit 9361237
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/jcabi/dynamo/AwsTable.java
Expand Up @@ -194,7 +194,7 @@ public static String print(
final ConsumedCapacity capacity) {
final String txt;
if (capacity == null) {
txt = "";
txt = "no units";
} else {
txt = String.format("%.2f units", capacity.getCapacityUnits());
}
Expand Down
34 changes: 23 additions & 11 deletions src/main/java/com/jcabi/dynamo/ScanValve.java
Expand Up @@ -36,6 +36,7 @@
import com.amazonaws.services.dynamodbv2.model.ReturnConsumedCapacity;
import com.amazonaws.services.dynamodbv2.model.ScanRequest;
import com.amazonaws.services.dynamodbv2.model.ScanResult;
import com.amazonaws.services.dynamodbv2.model.Select;
import com.google.common.collect.Iterables;
import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.Loggable;
Expand Down Expand Up @@ -136,18 +137,29 @@ public Dosage fetch(final Credentials credentials,
@Override
public int count(final Credentials credentials, final String table,
final Map<String, Condition> conditions) throws IOException {
Dosage dosage = this.fetch(
credentials, table, conditions, Collections.<String>emptyList()
);
int count = 0;
while (true) {
count += dosage.items().size();
if (!dosage.hasNext()) {
break;
}
dosage = dosage.next();
final AmazonDynamoDB aws = credentials.aws();
try {
final ScanRequest request = new ScanRequest()
.withTableName(table)
.withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL)
.withScanFilter(conditions)
.withSelect(Select.COUNT)
.withLimit(Integer.MAX_VALUE);
final long start = System.currentTimeMillis();
final ScanResult rslt = aws.scan(request);
final int count = rslt.getCount();
Logger.info(
this,
// @checkstyle LineLength (1 line)
"#total(): COUNT=%d in '%s' using %s, %s, in %[ms]s",
count, request.getTableName(), request.getFilterExpression(),
AwsTable.print(rslt.getConsumedCapacity()),
System.currentTimeMillis() - start
);
return count;
} finally {
aws.shutdown();
}
return count;
}

/**
Expand Down

0 comments on commit 9361237

Please sign in to comment.