Skip to content

Commit

Permalink
jcabi#7 - base32-encode H2 table names to support all possible Dynamo…
Browse files Browse the repository at this point in the history
…DB names
  • Loading branch information
RomanKisilenko committed Jan 17, 2015
1 parent 3fd48e8 commit 616482f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
</dependencies>
<build>
<pluginManagement>
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/com/jcabi/dynamo/mock/H2Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.util.Properties;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.codec.binary.Base32;
import org.h2.Driver;

/**
Expand All @@ -71,7 +72,7 @@
@ToString
@Loggable(Loggable.DEBUG)
@EqualsAndHashCode(of = "jdbc")
@SuppressWarnings("PMD.TooManyMethods")
@SuppressWarnings({ "PMD.TooManyMethods", "PMD.ExcessiveImports" })
public final class H2Data implements MkData {

/**
Expand Down Expand Up @@ -170,8 +171,9 @@ public H2Data(final File file) {
public Iterable<Attributes> iterate(final String table,
final Conditions conds) throws IOException {
try {
final String h2Table = this.encodeTableName(table);
final StringBuilder sql = new StringBuilder("SELECT * FROM ")
.append(table);
.append(h2Table);
if (!conds.isEmpty()) {
sql.append(" WHERE ");
Joiner.on(" AND ").appendTo(
Expand Down Expand Up @@ -204,18 +206,20 @@ public Iterable<Attributes> iterate(final String table,
throw new IOException(ex);
}
}

@Override
public void put(final String table, final Attributes attrs)
throws IOException {
try {
final String h2Table = this.encodeTableName(table);
JdbcSession session = new JdbcSession(this.connection());
for (final AttributeValue value : attrs.values()) {
session = session.set(H2Data.value(value));
}
session.sql(
String.format(
"INSERT INTO %s (%s) VALUES (%s)",
table,
h2Table,
Joiner.on(',').join(attrs.keySet()),
Joiner.on(',').join(Collections.nCopies(attrs.size(), "?"))
)
Expand All @@ -231,6 +235,7 @@ public void update(final String table, final Attributes keys,
final AttributeUpdates attrs)
throws IOException {
try {
final String h2Table = this.encodeTableName(table);
JdbcSession session = new JdbcSession(this.connection());
for (final AttributeValueUpdate value : attrs.values()) {
session = session.set(H2Data.value(value.getValue()));
Expand All @@ -241,7 +246,7 @@ public void update(final String table, final Attributes keys,
session.sql(
String.format(
"UPDATE %s SET %s WHERE %s",
table,
h2Table,
Joiner.on(',').join(
Iterables.transform(attrs.keySet(), H2Data.WHERE)
),
Expand Down Expand Up @@ -271,8 +276,9 @@ public H2Data with(final String table, final String[] keys,
String.format("empty list of keys for %s table", table)
);
}
final String h2Table = this.encodeTableName(table);
final StringBuilder sql = new StringBuilder("CREATE TABLE ")
.append(table).append(" (");
.append(h2Table).append(" (");
Joiner.on(',').appendTo(
sql,
Iterables.transform(Arrays.asList(keys), H2Data.CREATE_KEY)
Expand Down Expand Up @@ -319,4 +325,13 @@ private static String value(final AttributeValue attr) {
return val;
}

/**
* Base32-encodes table name for use with H2.
* @param table Table name to encode
* @return Base-32-encoded table name
*/
private String encodeTableName(final String table) {
return new Base32(true, (byte) '_').encodeAsString(table.getBytes());
}

}
17 changes: 17 additions & 0 deletions src/test/java/com/jcabi/dynamo/mock/H2DataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
package com.jcabi.dynamo.mock;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.google.common.base.Joiner;
import com.jcabi.dynamo.Attributes;
import com.jcabi.dynamo.Conditions;
import java.io.File;
import java.util.Collections;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Ignore;
Expand Down Expand Up @@ -111,4 +113,19 @@ public void createsManyTables() throws Exception {
.with("secondtable", new String[]{"secondid"}, new String[0]);
}

/**
* H2Data can create tables with long names (max length of DynamoDb table
* name is 255 characters).
* @throws Exception In case test fails
*/
@Test
public void createsTablesWithLongNames() throws Exception {
new H2Data()
.with(
//@checkstyle MagicNumberCheck (1 line)
Joiner.on("").join(Collections.nCopies(255, "a")),
new String[] {"key"}, new String[0]
);
}

}

0 comments on commit 616482f

Please sign in to comment.