Out with arrays, in with collections.

This commit is contained in:
Michael Bayne
2013-05-06 18:19:31 +00:00
parent a625c831ae
commit c63ce0c10e
3 changed files with 25 additions and 33 deletions
@@ -494,21 +494,18 @@ public class DepotMarshaller<T extends PersistentRecord> implements QueryMarshal
// figure out the list of fields that correspond to actual table columns and generate the // figure out the list of fields that correspond to actual table columns and generate the
// SQL used to create and migrate our table (unless we're a computed entity) // SQL used to create and migrate our table (unless we're a computed entity)
_columnFields = new ColumnExp<?>[_allFields.length]; List<ColumnExp<?>> columnFields = Lists.newArrayList();
ColumnDefinition[] declarations = new ColumnDefinition[_allFields.length]; List<ColumnDefinition> declarations = Lists.newArrayList();
int jj = 0;
for (ColumnExp<?> field : _allFields) { for (ColumnExp<?> field : _allFields) {
FieldMarshaller<?> fm = _fields.get(field.name); FieldMarshaller<?> fm = _fields.get(field.name);
// include all persistent non-computed fields // include all persistent non-computed fields
ColumnDefinition colDef = fm.getColumnDefinition(); ColumnDefinition colDef = fm.getColumnDefinition();
if (colDef != null) { if (colDef != null) {
_columnFields[jj] = field; columnFields.add(field);
declarations[jj] = colDef; declarations.add(colDef);
jj ++;
} }
} }
_columnFields = ArrayUtil.splice(_columnFields, jj); _columnFields = columnFields.toArray(new ColumnExp<?>[columnFields.size()]);
declarations = ArrayUtil.splice(declarations, jj);
// determine whether or not this record has ever been seen // determine whether or not this record has ever been seen
int currentVersion = _meta.getVersion(getTableName(), false); int currentVersion = _meta.getVersion(getTableName(), false);
@@ -681,7 +678,7 @@ public class DepotMarshaller<T extends PersistentRecord> implements QueryMarshal
} }
protected void createTable (PersistenceContext ctx, final SQLBuilder builder, protected void createTable (PersistenceContext ctx, final SQLBuilder builder,
final ColumnDefinition[] declarations) final List<ColumnDefinition> declarations)
throws DatabaseException throws DatabaseException
{ {
log.info("Creating initial table '" + getTableName() + "'."); log.info("Creating initial table '" + getTableName() + "'.");
@@ -690,15 +687,14 @@ public class DepotMarshaller<T extends PersistentRecord> implements QueryMarshal
@Override @Override
protected int invoke (Connection conn, DatabaseLiaison liaison) throws SQLException { protected int invoke (Connection conn, DatabaseLiaison liaison) throws SQLException {
// create the table // create the table
String[] primaryKeyColumns = null; List<String> pkColumns = Lists.newArrayList();
if (hasPrimaryKey()) { if (hasPrimaryKey()) {
primaryKeyColumns = new String[_pkColumns.size()]; for (int ii = 0; ii < _pkColumns.size(); ii ++) {
for (int ii = 0; ii < primaryKeyColumns.length; ii ++) { pkColumns.add(_pkColumns.get(ii).getColumnName());
primaryKeyColumns[ii] = _pkColumns.get(ii).getColumnName();
} }
} }
liaison.createTableIfMissing(conn, getTableName(), fieldsToColumns(_columnFields), liaison.createTableIfMissing(conn, getTableName(), fieldsToColumns(_columnFields),
declarations, null, primaryKeyColumns); declarations, pkColumns);
// add its indexen // add its indexen
for (CreateIndexClause iclause : _indexes) { for (CreateIndexClause iclause : _indexes) {
@@ -964,14 +960,14 @@ public class DepotMarshaller<T extends PersistentRecord> implements QueryMarshal
} }
// translate an array of field names to an array of column names // translate an array of field names to an array of column names
protected String[] fieldsToColumns (ColumnExp<?>[] fields) protected List<String> fieldsToColumns (ColumnExp<?>[] fields)
{ {
String[] columns = new String[fields.length]; List<String> columns = Lists.newArrayListWithExpectedSize(fields.length);
for (int ii = 0; ii < columns.length; ii ++) { for (int ii = 0; ii < fields.length; ii ++) {
FieldMarshaller<?> fm = _fields.get(fields[ii].name); FieldMarshaller<?> fm = _fields.get(fields[ii].name);
checkArgument(fm != null, "Unknown field on record [field=%s, class=%s]", checkArgument(fm != null, "Unknown field on record [field=%s, class=%s]",
fields[ii], _pClass); fields[ii], _pClass);
columns[ii] = fm.getColumnName(); columns.add(fm.getColumnName());
} }
return columns; return columns;
} }
@@ -8,6 +8,7 @@ import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.Arrays;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
@@ -37,14 +38,11 @@ public class DepotMetaData
// create our schema version table if needed // create our schema version table if needed
liaison.createTableIfMissing( liaison.createTableIfMissing(
stmt.getConnection(), SCHEMA_VERSION_TABLE, stmt.getConnection(), SCHEMA_VERSION_TABLE,
new String[] { P_COLUMN, V_COLUMN, MV_COLUMN }, Arrays.asList(P_COLUMN, V_COLUMN, MV_COLUMN),
new ColumnDefinition[] { Arrays.asList(new ColumnDefinition("VARCHAR(255)", false, true, null),
new ColumnDefinition("VARCHAR(255)", false, true, null),
new ColumnDefinition("INTEGER", false, false, null), new ColumnDefinition("INTEGER", false, false, null),
new ColumnDefinition("INTEGER", false, false, null) new ColumnDefinition("INTEGER", false, false, null)),
}, Arrays.asList(P_COLUMN));
null,
new String[] { P_COLUMN });
// slurp in the current versions of all records // slurp in the current versions of all records
readVersions(liaison, stmt); readVersions(liaison, stmt);
@@ -8,6 +8,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Arrays;
import com.samskivert.depot.annotation.GeneratedValue; import com.samskivert.depot.annotation.GeneratedValue;
import com.samskivert.depot.annotation.TableGenerator; import com.samskivert.depot.annotation.TableGenerator;
@@ -43,13 +44,10 @@ public class TableValueGenerator extends ValueGenerator
// make sure our table exists // make sure our table exists
liaison.createTableIfMissing( liaison.createTableIfMissing(
conn, _valueTable, conn, _valueTable,
new String[] { _pkColumnName, _valueColumnName }, Arrays.asList(_pkColumnName, _valueColumnName),
new ColumnDefinition[] { Arrays.asList(new ColumnDefinition("VARCHAR(255)", true, false, null),
new ColumnDefinition("VARCHAR(255)", true, false, null), new ColumnDefinition("INTEGER")),
new ColumnDefinition("INTEGER") Arrays.asList(_pkColumnName));
},
null,
new String[] { _pkColumnName });
// and also that there's a row in it for us // and also that there's a row in it for us
PreparedStatement stmt = conn.prepareStatement( PreparedStatement stmt = conn.prepareStatement(