From 2fede76e2de4fedf7103787e7e20f0ecdce74008 Mon Sep 17 00:00:00 2001 From: ribeaudc <ribeaudc> Date: Thu, 6 Mar 2008 10:01:58 +0000 Subject: [PATCH] minor: - Checks whether info is enabled before "infoying" the log. - Add final keywords when possible. SVN: 4651 --- .../cisd/dbmigration/h2/H2MassUploader.java | 44 ++++++++++-------- .../postgresql/PostgreSQLMassUploader.java | 46 +++++++++++-------- 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/dbmigration/source/java/ch/systemsx/cisd/dbmigration/h2/H2MassUploader.java b/dbmigration/source/java/ch/systemsx/cisd/dbmigration/h2/H2MassUploader.java index e00ff10953a..41a49c57ccc 100644 --- a/dbmigration/source/java/ch/systemsx/cisd/dbmigration/h2/H2MassUploader.java +++ b/dbmigration/source/java/ch/systemsx/cisd/dbmigration/h2/H2MassUploader.java @@ -62,7 +62,8 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade /** * Creates an instance for the specified data source and sequence mapper. */ - public H2MassUploader(DataSource dataSource, ISequenceNameMapper sequenceNameMapper) throws SQLException + public H2MassUploader(final DataSource dataSource, final ISequenceNameMapper sequenceNameMapper) + throws SQLException { this.sequenceNameMapper = sequenceNameMapper; setDataSource(dataSource); @@ -76,7 +77,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade final BitSet isBinaryColumn; - MassUploadRecord(File massUploadFile, String tableName, BitSet isBinaryColumn) + MassUploadRecord(final File massUploadFile, final String tableName, final BitSet isBinaryColumn) { this.massUploadFile = massUploadFile; this.tableName = tableName; @@ -84,7 +85,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade } } - public void performMassUpload(File[] massUploadFiles) + public final void performMassUpload(final File[] massUploadFiles) { String task = "Get database metadata"; try @@ -93,14 +94,15 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade final DatabaseMetaData dbMetaData = getConnection().getMetaData(); try { - for (File massUploadFile : massUploadFiles) + for (final File massUploadFile : massUploadFiles) { final String[] splitName = StringUtils.split(massUploadFile.getName(), "="); assert splitName.length == 2 : "Missing '=' in name of file '" + massUploadFile.getName() + "'."; final String tableNameWithExtension = splitName[1]; - boolean tsvFileType = TSV.isOfType(tableNameWithExtension); + final boolean tsvFileType = TSV.isOfType(tableNameWithExtension); assert tsvFileType : "Not a " + TSV.getFileType() + " file: " + massUploadFile.getName(); - final String tableName = tableNameWithExtension.substring(0, tableNameWithExtension.lastIndexOf('.')); + final String tableName = + tableNameWithExtension.substring(0, tableNameWithExtension.lastIndexOf('.')); final BitSet isBinaryColumn = findBinaryColumns(dbMetaData, tableName); massUploadRecords.add(new MassUploadRecord(massUploadFile, tableName, isBinaryColumn)); } @@ -109,21 +111,21 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade task = "Close connection"; dbMetaData.getConnection().close(); } - for (MassUploadRecord record : massUploadRecords) + for (final MassUploadRecord record : massUploadRecords) { performMassUpload(record); } - for (MassUploadRecord record : massUploadRecords) + for (final MassUploadRecord record : massUploadRecords) { fixSequence(record.tableName); } - } catch (SQLException ex) + } catch (final SQLException ex) { throw new UncategorizedSQLException(task, "UNKNOWN", ex); } } - private void performMassUpload(final MassUploadRecord record) + private final void performMassUpload(final MassUploadRecord record) { try { @@ -152,7 +154,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade return numberOfRows; } - public void setValues(PreparedStatement ps, int rowNo) throws SQLException + public void setValues(final PreparedStatement ps, final int rowNo) throws SQLException { for (int colNo = 0; colNo < numberOfColumns; ++colNo) { @@ -160,7 +162,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade } } - private Object tryGetValue(int rowNo, int colNo) + private Object tryGetValue(final int rowNo, final int colNo) { final String stringValueOrNull = rows.get(rowNo)[colNo]; if (stringValueOrNull == null) @@ -176,13 +178,14 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade } } }); - } catch (Exception ex) + } catch (final Exception ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); } } - private BitSet findBinaryColumns(final DatabaseMetaData dbMetaData, final String tableName) throws SQLException + private final BitSet findBinaryColumns(final DatabaseMetaData dbMetaData, final String tableName) + throws SQLException { final BitSet binary = new BitSet(); final ResultSet rs = dbMetaData.getColumns(null, null, tableName.toUpperCase(), "%"); @@ -197,7 +200,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade return binary; } - private List<String[]> readTSVFile(File tsvFile) throws IOException + private final List<String[]> readTSVFile(final File tsvFile) throws IOException { final List<String[]> result = new ArrayList<String[]>(); final BufferedReader reader = new BufferedReader(new InputStreamReader(FileUtils.openInputStream(tsvFile))); @@ -239,7 +242,7 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade return result; } - private void fixSequence(String tableName) + private final void fixSequence(final String tableName) { final String sequenceName = sequenceNameMapper.getSequencerForTable(tableName); if (sequenceName == null) @@ -250,11 +253,14 @@ public class H2MassUploader extends SimpleJdbcDaoSupport implements IMassUploade { final long maxId = getSimpleJdbcTemplate().queryForLong(String.format("select max(id) from %s", tableName)); final long newSequenceValue = maxId + 1; - operationLog.info("Updating sequence " + sequenceName + " for table " + tableName + " to value " - + newSequenceValue); + if (operationLog.isInfoEnabled()) + { + operationLog.info("Updating sequence " + sequenceName + " for table " + tableName + " to value " + + newSequenceValue); + } getJdbcTemplate().execute( String.format("alter sequence %s restart with %d", sequenceName, newSequenceValue)); - } catch (DataAccessException ex) + } catch (final DataAccessException ex) { operationLog.error("Failed to set new value for sequence '" + sequenceName + "' of table '" + tableName + "'.", ex); diff --git a/dbmigration/source/java/ch/systemsx/cisd/dbmigration/postgresql/PostgreSQLMassUploader.java b/dbmigration/source/java/ch/systemsx/cisd/dbmigration/postgresql/PostgreSQLMassUploader.java index 99c38e0c144..39232c520f0 100644 --- a/dbmigration/source/java/ch/systemsx/cisd/dbmigration/postgresql/PostgreSQLMassUploader.java +++ b/dbmigration/source/java/ch/systemsx/cisd/dbmigration/postgresql/PostgreSQLMassUploader.java @@ -63,14 +63,15 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas /** * Creates an instance for the specified data source and sequence mapper. */ - public PostgreSQLMassUploader(DataSource dataSource, ISequenceNameMapper sequenceNameMapper) throws SQLException + public PostgreSQLMassUploader(final DataSource dataSource, final ISequenceNameMapper sequenceNameMapper) + throws SQLException { this.dataSource = dataSource; this.sequenceNameMapper = sequenceNameMapper; setDataSource(dataSource); } - private CopyManager getCopyManager() throws SQLException, NoSuchFieldException, IllegalAccessException + private final CopyManager getCopyManager() throws SQLException, NoSuchFieldException, IllegalAccessException { if (copyManager == null) { @@ -79,28 +80,28 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas return copyManager; } - public void performMassUpload(File[] massUploadFiles) + public final void performMassUpload(final File[] massUploadFiles) { - Set<String> tables = new LinkedHashSet<String>(); - for (File file : massUploadFiles) + final Set<String> tables = new LinkedHashSet<String>(); + for (final File file : massUploadFiles) { performMassUpload(file, tables); } - for (String name : tables) + for (final String name : tables) { fixSequence(name); } } - private void performMassUpload(File massUploadFile, Set<String> tables) + private final void performMassUpload(final File massUploadFile, final Set<String> tables) { try { final String[] splitName = StringUtils.split(massUploadFile.getName(), "="); assert splitName.length == 2 : "Missing '=' in name of file '" + massUploadFile.getName() + "'."; final String tableNameWithExtension = splitName[1]; - boolean csvFileType = CSV.isOfType(tableNameWithExtension); - boolean tsvFileType = TSV.isOfType(tableNameWithExtension); + final boolean csvFileType = CSV.isOfType(tableNameWithExtension); + final boolean tsvFileType = TSV.isOfType(tableNameWithExtension); assert tsvFileType || csvFileType : "Non of expected file types [" + TSV.getFileType() + ", " + CSV.getFileType() + "]: " + massUploadFile.getName(); final String tableName = tableNameWithExtension.substring(0, tableNameWithExtension.lastIndexOf('.')); @@ -123,13 +124,13 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas { IOUtils.closeQuietly(is); } - } catch (Exception ex) + } catch (final Exception ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); } } - private void fixSequence(String tableName) + private final void fixSequence(final String tableName) { final String sequenceName = sequenceNameMapper.getSequencerForTable(tableName); if (sequenceName == null) @@ -138,21 +139,28 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas } try { - getSimpleJdbcTemplate().queryForLong( - String.format("select setval('%s', max(id)) from %s", sequenceName, tableName)); - } catch (DataAccessException ex) + // The result returned by setval is just the value of its second argument. + final long newSequenceValue = + getSimpleJdbcTemplate().queryForLong( + String.format("select setval('%s', max(id)) from %s", sequenceName, tableName)); + if (operationLog.isInfoEnabled()) + { + operationLog.info("Updating sequence " + sequenceName + " for table " + tableName + " to value " + + newSequenceValue); + } + } catch (final DataAccessException ex) { operationLog.error("Failed to set new value for sequence '" + sequenceName + "' of table '" + tableName + "'.", ex); } } - private PGConnection getPGConnection() throws SQLException, NoSuchFieldException, IllegalAccessException + private final PGConnection getPGConnection() throws SQLException, NoSuchFieldException, IllegalAccessException { return getPGConnection(dataSource.getConnection()); } - private PGConnection getPGConnection(Connection conn) throws SQLException, NoSuchFieldException, + private final PGConnection getPGConnection(final Connection conn) throws SQLException, NoSuchFieldException, IllegalAccessException { if (conn instanceof PGConnection) @@ -163,7 +171,7 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas { operationLog.debug("Found connection of type '" + conn.getClass().getCanonicalName() + "'."); } - Field delegateField = getField(conn.getClass(), "_conn"); + final Field delegateField = getField(conn.getClass(), "_conn"); if (delegateField == null) { throw new RuntimeException("No PostgreSQL driver found - cannot perform mass upload."); @@ -172,7 +180,7 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas return getPGConnection((Connection) delegateField.get(conn)); } - private static Field getField(Class<?> clazz, String fieldName) + private final static Field getField(final Class<?> clazz, final String fieldName) { assert fieldName != null; if (clazz == null) @@ -180,7 +188,7 @@ public class PostgreSQLMassUploader extends SimpleJdbcDaoSupport implements IMas return null; } - for (Field field : clazz.getDeclaredFields()) + for (final Field field : clazz.getDeclaredFields()) { if (fieldName.equals(field.getName())) { -- GitLab