Skip to content

Commit

Permalink
yegor256#173 Code review updates and some formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosmiranda committed Jun 13, 2014
1 parent e9eef1e commit b548f86
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
package com.s3auth.hosts;

import com.jcabi.aspects.Immutable;
import java.io.IOException;
import java.util.Map;

Expand All @@ -38,6 +39,7 @@
* @author Carlos Miranda ([email protected])
* @version $Id$
*/
@Immutable
interface DomainStatsData {

/**
Expand All @@ -46,15 +48,15 @@ interface DomainStatsData {
* @param stats The stats to keep.
* @throws IOException If something goes wrong.
*/
void put(final String domain, final Stats stats) throws IOException;
void put(String domain, Stats stats) throws IOException;

/**
* Get the stats for the given domain.
* @param domain The domain whose stats we're interested in
* @return The stats for this domain
* @throws IOException If something goes wrong.
*/
Stats get(final String domain) throws IOException;
Stats get(String domain) throws IOException;

/**
* Get the stats for all domains.
Expand Down
21 changes: 17 additions & 4 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/H2DomainStatsData.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,35 @@ public void put(final String domain, final Stats stats) throws IOException {
@Override
public Stats get(final String domain) throws IOException {
try {
return new JdbcSession(this.connection())
.sql("SELECT SUM(BYTES) FROM DOMAIN_STATS WHERE DOMAIN = ?")
final JdbcSession session = new JdbcSession(this.connection())
.autocommit(false);
// @checkstyle LineLength (2 lines)
final Stats result = session
.sql("SELECT SUM(BYTES) FROM DOMAIN_STATS WHERE DOMAIN = ? FOR UPDATE")
.set(domain)
.select(STATS);
session.sql("DELETE FROM DOMAIN_STATS WHERE DOMAIN = ?")
.set(domain)
.execute()
.commit();
return result;
} catch (final SQLException ex) {
throw new IOException(ex);
}
}

@Override
@SuppressWarnings("PMD.UseConcurrentHashMap")
public Map<String, Stats> all() throws IOException {
try {
final JdbcSession session = new JdbcSession(this.connection())
.autocommit(false);
// @checkstyle LineLength (2 lines)
return new JdbcSession(this.connection())
.sql("SELECT DOMAIN, SUM(BYTES) FROM DOMAIN_STATS GROUP BY DOMAIN")
final Map<String, Stats> result = session
.sql("SELECT DOMAIN, SUM(BYTES) FROM DOMAIN_STATS GROUP BY DOMAIN FOR UPDATE")
.select(STATS_ALL);
session.sql("DELETE FROM DOMAIN_STATS").execute().commit();
return result;
} catch (final SQLException ex) {
throw new IOException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ public long bytesTransferred() {
}
}
);
final long result = data.get(domain).bytesTransferred();
MatcherAssert.assertThat(
result, Matchers.is(bytes)
data.get(domain).bytesTransferred(),
Matchers.is(bytes)
);
MatcherAssert.assertThat(
data.get(domain).bytesTransferred(),
Matchers.is(0L)
);
}

Expand Down Expand Up @@ -121,6 +125,10 @@ public long bytesTransferred() {
stats.get(second).bytesTransferred(),
Matchers.is((long) Tv.THOUSAND)
);
MatcherAssert.assertThat(
data.all().size(),
Matchers.is(0)
);
}

}

0 comments on commit b548f86

Please sign in to comment.