Skip to content

Commit

Permalink
#54 Adding PrintableConsumedCapacity class
Browse files Browse the repository at this point in the history
  • Loading branch information
prondzyn committed Sep 9, 2016
1 parent 1d47662 commit 6cfe329
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 28 deletions.
15 changes: 12 additions & 3 deletions src/main/java/com/jcabi/dynamo/AwsItem.java
Expand Up @@ -126,7 +126,10 @@ public boolean has(final String attr) throws IOException {
has = result.getItem().get(attrib) != null;
Logger.info(
this, "#has('%s'): %B from DynamoDB, %s, in %[ms]s",
attr, has, AwsTable.print(result.getConsumedCapacity()),
attr, has,
new PrintableConsumedCapacity(
result.getConsumedCapacity()
).print(),
System.currentTimeMillis() - start
);
} catch (final AmazonClientException ex) {
Expand Down Expand Up @@ -164,7 +167,10 @@ public AttributeValue get(final String attr) throws IOException {
this,
// @checkstyle LineLength (1 line)
"#get('%s'): loaded '%[text]s' from DynamoDB, %s, in %[ms]s",
attrib, value, AwsTable.print(result.getConsumedCapacity()),
attrib, value,
new PrintableConsumedCapacity(
result.getConsumedCapacity()
).print(),
System.currentTimeMillis() - start
);
} catch (final AmazonClientException ex) {
Expand Down Expand Up @@ -210,7 +216,10 @@ public Map<String, AttributeValue> put(
final UpdateItemResult result = aws.updateItem(request);
Logger.info(
this, "#put('%s'): updated item to DynamoDB, %s, in %[ms]s",
attrs, AwsTable.print(result.getConsumedCapacity()),
attrs,
new PrintableConsumedCapacity(
result.getConsumedCapacity()
).print(),
System.currentTimeMillis() - start
);
return result.getAttributes();
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/jcabi/dynamo/AwsIterator.java
Expand Up @@ -208,7 +208,10 @@ public void remove() {
Logger.info(
this,
"#remove(): item #%d removed from DynamoDB, %s, in %[ms]s",
this.position, AwsTable.print(res.getConsumedCapacity()),
this.position,
new PrintableConsumedCapacity(
res.getConsumedCapacity()
).print(),
System.currentTimeMillis() - start
);
} finally {
Expand Down
25 changes: 6 additions & 19 deletions src/main/java/com/jcabi/dynamo/AwsTable.java
Expand Up @@ -32,7 +32,6 @@
import com.amazonaws.AmazonClientException;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ConsumedCapacity;
import com.amazonaws.services.dynamodbv2.model.DeleteItemRequest;
import com.amazonaws.services.dynamodbv2.model.DeleteItemResult;
import com.amazonaws.services.dynamodbv2.model.DescribeTableRequest;
Expand Down Expand Up @@ -111,7 +110,9 @@ public Item put(final Map<String, AttributeValue> attributes)
Logger.info(
this, "#put('%[text]s'): created item in '%s', %s, in %[ms]s",
attributes, this.self,
AwsTable.print(result.getConsumedCapacity()),
new PrintableConsumedCapacity(
result.getConsumedCapacity()
).print(),
System.currentTimeMillis() - start
);
return new AwsItem(
Expand Down Expand Up @@ -185,22 +186,6 @@ public Collection<String> keys() throws IOException {
}
}

/**
* Print consumed capacity nicely.
* @param capacity Consumed capacity or NULL
* @return Suffix to add to a log line
*/
public static String print(
final ConsumedCapacity capacity) {
final String txt;
if (capacity == null) {
txt = "";
} else {
txt = String.format("%.2f units", capacity.getCapacityUnits());
}
return txt;
}

@Override
public void delete(final Map<String, AttributeValue> attributes)
throws IOException {
Expand All @@ -217,7 +202,9 @@ public void delete(final Map<String, AttributeValue> attributes)
this,
"#delete('%[text]s'): deleted item in '%s', %s, in %[ms]s",
attributes, this.self,
AwsTable.print(result.getConsumedCapacity()),
new PrintableConsumedCapacity(
result.getConsumedCapacity()
).print(),
System.currentTimeMillis() - start
);
} catch (final AmazonClientException ex) {
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/com/jcabi/dynamo/PrintableConsumedCapacity.java
@@ -0,0 +1,69 @@
/**
* Copyright (c) 2012-2015, jcabi.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the jcabi.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jcabi.dynamo;

import com.amazonaws.services.dynamodbv2.model.ConsumedCapacity;

/**
* Printable consumed capacity.
*
* @author Piotr Pradzynski (prondzyn@gmail.com)
* @version $Id$
* @since 0.22
*/
final class PrintableConsumedCapacity {

/**
* Consumed capacity.
*/
private final ConsumedCapacity capacity;

/**
* Default ctor.
* @param capacity Consumed capacity
*/
PrintableConsumedCapacity(final ConsumedCapacity capacity) {
this.capacity = capacity;
}

/**
* Print consumed capacity nicely.
* @return Suffix to add to a log line
*/
public String print() {
final String txt;
if (this.capacity == null) {
txt = "";
} else {
txt = String.format("%.2f units", this.capacity.getCapacityUnits());
}
return txt;
}
}
12 changes: 9 additions & 3 deletions src/main/java/com/jcabi/dynamo/QueryValve.java
Expand Up @@ -160,7 +160,9 @@ public Dosage fetch(final Credentials credentials, final String table,
this,
"#items(): loaded %d item(s) from '%s' using %s, %s, in %[ms]s",
result.getCount(), table, conditions,
AwsTable.print(result.getConsumedCapacity()),
new PrintableConsumedCapacity(
result.getConsumedCapacity()
).print(),
System.currentTimeMillis() - start
);
return new QueryValve.NextDosage(credentials, request, result);
Expand Down Expand Up @@ -200,7 +202,9 @@ public int count(final Credentials credentials, final String table,
// @checkstyle LineLength (1 line)
"#total(): COUNT=%d in '%s' using %s, %s, in %[ms]s",
count, request.getTableName(), request.getQueryFilter(),
AwsTable.print(rslt.getConsumedCapacity()),
new PrintableConsumedCapacity(
rslt.getConsumedCapacity()
).print(),
System.currentTimeMillis() - start
);
return count;
Expand Down Expand Up @@ -384,7 +388,9 @@ public Dosage next() {
"#next(): loaded %d item(s) from '%s' using %s, %s, in %[ms]s",
rslt.getCount(), rqst.getTableName(),
rqst.getKeyConditions(),
AwsTable.print(rslt.getConsumedCapacity()),
new PrintableConsumedCapacity(
rslt.getConsumedCapacity()
).print(),
System.currentTimeMillis() - start
);
return new QueryValve.NextDosage(this.credentials, rqst, rslt);
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/jcabi/dynamo/ScanValve.java
Expand Up @@ -116,7 +116,9 @@ public Dosage fetch(final Credentials credentials,
this,
"#items(): loaded %d item(s) from '%s' using %s, %s, in %[ms]s",
result.getCount(), table, conditions,
AwsTable.print(result.getConsumedCapacity()),
new PrintableConsumedCapacity(
result.getConsumedCapacity()
).print(),
System.currentTimeMillis() - start
);
return new ScanValve.NextDosage(credentials, request, result);
Expand Down Expand Up @@ -247,7 +249,9 @@ public Dosage next() {
// @checkstyle LineLength (1 line)
"#next(): loaded %d item(s) from '%s' using %s, %s, in %[ms]s",
rslt.getCount(), rqst.getTableName(), rqst.getScanFilter(),
AwsTable.print(rslt.getConsumedCapacity()),
new PrintableConsumedCapacity(
rslt.getConsumedCapacity()
).print(),
System.currentTimeMillis() - start
);
return new ScanValve.NextDosage(this.credentials, rqst, rslt);
Expand Down

0 comments on commit 6cfe329

Please sign in to comment.