From 9ca28ead66a54ba2f1f499d64244f87dfa9ddc4a Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Fri, 26 Feb 2016 16:12:43 +0100 Subject: [PATCH] Code cleanup --- .../bitmessage/repository/JdbcInventory.java | 50 +++++++++------ .../repository/JdbcMessageRepository.java | 62 ++++++++++--------- 2 files changed, 62 insertions(+), 50 deletions(-) diff --git a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java index aa41e5c..e70cabb 100644 --- a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java +++ b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java @@ -88,9 +88,11 @@ public class JdbcInventory extends JdbcHelper implements Inventory { @Override public ObjectMessage getObject(InventoryVector vector) { - try (Connection connection = config.getConnection()) { - Statement stmt = connection.createStatement(); - ResultSet rs = stmt.executeQuery("SELECT data, version FROM Inventory WHERE hash = X'" + vector + "'"); + try ( + Connection connection = config.getConnection(); + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT data, version FROM Inventory WHERE hash = X'" + vector + "'") + ) { if (rs.next()) { Blob data = rs.getBlob("data"); return Factory.getObjectMessage(rs.getInt("version"), data.getBinaryStream(), (int) data.length()); @@ -106,19 +108,21 @@ public class JdbcInventory extends JdbcHelper implements Inventory { @Override public List getObjects(long stream, long version, ObjectType... types) { - try (Connection connection = config.getConnection()) { - StringBuilder query = new StringBuilder("SELECT data, version FROM Inventory WHERE 1=1"); - if (stream > 0) { - query.append(" AND stream = ").append(stream); - } - if (version > 0) { - query.append(" AND version = ").append(version); - } - if (types.length > 0) { - query.append(" AND type IN (").append(join(types)).append(")"); - } - Statement stmt = connection.createStatement(); - ResultSet rs = stmt.executeQuery(query.toString()); + StringBuilder query = new StringBuilder("SELECT data, version FROM Inventory WHERE 1=1"); + if (stream > 0) { + query.append(" AND stream = ").append(stream); + } + if (version > 0) { + query.append(" AND version = ").append(version); + } + if (types.length > 0) { + query.append(" AND type IN (").append(join(types)).append(")"); + } + try ( + Connection connection = config.getConnection(); + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery(query.toString()) + ) { List result = new LinkedList<>(); while (rs.next()) { Blob data = rs.getBlob("data"); @@ -136,8 +140,11 @@ public class JdbcInventory extends JdbcHelper implements Inventory { if (getCache(object.getStream()).containsKey(object.getInventoryVector())) return; - try (Connection connection = config.getConnection()) { - PreparedStatement ps = connection.prepareStatement("INSERT INTO Inventory (hash, stream, expires, data, type, version) VALUES (?, ?, ?, ?, ?, ?)"); + try ( + Connection connection = config.getConnection(); + PreparedStatement ps = connection.prepareStatement("INSERT INTO Inventory " + + "(hash, stream, expires, data, type, version) VALUES (?, ?, ?, ?, ?, ?)") + ) { InventoryVector iv = object.getInventoryVector(); LOG.trace("Storing object " + iv); ps.setBytes(1, iv.getHash()); @@ -163,8 +170,11 @@ public class JdbcInventory extends JdbcHelper implements Inventory { @Override public void cleanup() { - try (Connection connection = config.getConnection()) { - connection.createStatement().executeUpdate("DELETE FROM Inventory WHERE expires < " + now(-5 * MINUTE)); + try ( + Connection connection = config.getConnection(); + Statement stmt = connection.createStatement() + ) { + stmt.executeUpdate("DELETE FROM Inventory WHERE expires < " + now(-5 * MINUTE)); } catch (SQLException e) { LOG.debug(e.getMessage(), e); } diff --git a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java index 1d714d9..e043e7e 100644 --- a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java +++ b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java @@ -201,50 +201,52 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito // save from address if necessary if (message.getId() == null) { BitmessageAddress savedAddress = ctx.getAddressRepository().getAddress(message.getFrom().getAddress()); - if (savedAddress == null || savedAddress.getPrivateKey() == null) { - if (savedAddress != null && savedAddress.getAlias() != null) { - message.getFrom().setAlias(savedAddress.getAlias()); - } + if (savedAddress == null) { ctx.getAddressRepository().save(message.getFrom()); + } else if (savedAddress.getPubkey() == null && message.getFrom().getPubkey() != null) { + savedAddress.setPubkey(message.getFrom().getPubkey()); + ctx.getAddressRepository().save(savedAddress); } } try (Connection connection = config.getConnection()) { try { connection.setAutoCommit(false); - // save message - if (message.getId() == null) { - insert(connection, message); - } else { - update(connection, message); - } - - // remove existing labels - try (Statement stmt = connection.createStatement()) { - stmt.executeUpdate("DELETE FROM Message_Label WHERE message_id=" + message.getId()); - } - // save labels - try (PreparedStatement ps = connection.prepareStatement("INSERT INTO Message_Label VALUES (" + - message.getId() + ", ?)")) { - for (Label label : message.getLabels()) { - ps.setLong(1, (Long) label.getId()); - ps.executeUpdate(); - } - } + save(connection, message); + updateLabels(connection, message); connection.commit(); } catch (IOException | SQLException e) { - try { - connection.rollback(); - } catch (SQLException e1) { - LOG.debug(e1.getMessage(), e); - } - throw new ApplicationException(e); + connection.rollback(); + throw e; } - } catch (SQLException e) { + } catch (IOException | SQLException e) { throw new ApplicationException(e); } } + private void save(Connection connection, Plaintext message) throws IOException, SQLException { + if (message.getId() == null) { + insert(connection, message); + } else { + update(connection, message); + } + } + + private void updateLabels(Connection connection, Plaintext message) throws SQLException { + // remove existing labels + try (Statement stmt = connection.createStatement()) { + stmt.executeUpdate("DELETE FROM Message_Label WHERE message_id=" + message.getId()); + } + // save new labels + try (PreparedStatement ps = connection.prepareStatement("INSERT INTO Message_Label VALUES (" + + message.getId() + ", ?)")) { + for (Label label : message.getLabels()) { + ps.setLong(1, (Long) label.getId()); + ps.executeUpdate(); + } + } + } + private void insert(Connection connection, Plaintext message) throws SQLException, IOException { try (PreparedStatement ps = connection.prepareStatement( "INSERT INTO Message (iv, type, sender, recipient, data, sent, received, status, initial_hash) " +