Code cleanup
This commit is contained in:
parent
2a17e6024f
commit
9ca28ead66
@ -88,9 +88,11 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ObjectMessage getObject(InventoryVector vector) {
|
public ObjectMessage getObject(InventoryVector vector) {
|
||||||
try (Connection connection = config.getConnection()) {
|
try (
|
||||||
|
Connection connection = config.getConnection();
|
||||||
Statement stmt = connection.createStatement();
|
Statement stmt = connection.createStatement();
|
||||||
ResultSet rs = stmt.executeQuery("SELECT data, version FROM Inventory WHERE hash = X'" + vector + "'");
|
ResultSet rs = stmt.executeQuery("SELECT data, version FROM Inventory WHERE hash = X'" + vector + "'")
|
||||||
|
) {
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
Blob data = rs.getBlob("data");
|
Blob data = rs.getBlob("data");
|
||||||
return Factory.getObjectMessage(rs.getInt("version"), data.getBinaryStream(), (int) data.length());
|
return Factory.getObjectMessage(rs.getInt("version"), data.getBinaryStream(), (int) data.length());
|
||||||
@ -106,7 +108,6 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ObjectMessage> getObjects(long stream, long version, ObjectType... types) {
|
public List<ObjectMessage> getObjects(long stream, long version, ObjectType... types) {
|
||||||
try (Connection connection = config.getConnection()) {
|
|
||||||
StringBuilder query = new StringBuilder("SELECT data, version FROM Inventory WHERE 1=1");
|
StringBuilder query = new StringBuilder("SELECT data, version FROM Inventory WHERE 1=1");
|
||||||
if (stream > 0) {
|
if (stream > 0) {
|
||||||
query.append(" AND stream = ").append(stream);
|
query.append(" AND stream = ").append(stream);
|
||||||
@ -117,8 +118,11 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
|
|||||||
if (types.length > 0) {
|
if (types.length > 0) {
|
||||||
query.append(" AND type IN (").append(join(types)).append(")");
|
query.append(" AND type IN (").append(join(types)).append(")");
|
||||||
}
|
}
|
||||||
|
try (
|
||||||
|
Connection connection = config.getConnection();
|
||||||
Statement stmt = connection.createStatement();
|
Statement stmt = connection.createStatement();
|
||||||
ResultSet rs = stmt.executeQuery(query.toString());
|
ResultSet rs = stmt.executeQuery(query.toString())
|
||||||
|
) {
|
||||||
List<ObjectMessage> result = new LinkedList<>();
|
List<ObjectMessage> result = new LinkedList<>();
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
Blob data = rs.getBlob("data");
|
Blob data = rs.getBlob("data");
|
||||||
@ -136,8 +140,11 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
|
|||||||
if (getCache(object.getStream()).containsKey(object.getInventoryVector()))
|
if (getCache(object.getStream()).containsKey(object.getInventoryVector()))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
try (Connection connection = config.getConnection()) {
|
try (
|
||||||
PreparedStatement ps = connection.prepareStatement("INSERT INTO Inventory (hash, stream, expires, data, type, version) VALUES (?, ?, ?, ?, ?, ?)");
|
Connection connection = config.getConnection();
|
||||||
|
PreparedStatement ps = connection.prepareStatement("INSERT INTO Inventory " +
|
||||||
|
"(hash, stream, expires, data, type, version) VALUES (?, ?, ?, ?, ?, ?)")
|
||||||
|
) {
|
||||||
InventoryVector iv = object.getInventoryVector();
|
InventoryVector iv = object.getInventoryVector();
|
||||||
LOG.trace("Storing object " + iv);
|
LOG.trace("Storing object " + iv);
|
||||||
ps.setBytes(1, iv.getHash());
|
ps.setBytes(1, iv.getHash());
|
||||||
@ -163,8 +170,11 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void cleanup() {
|
public void cleanup() {
|
||||||
try (Connection connection = config.getConnection()) {
|
try (
|
||||||
connection.createStatement().executeUpdate("DELETE FROM Inventory WHERE expires < " + now(-5 * MINUTE));
|
Connection connection = config.getConnection();
|
||||||
|
Statement stmt = connection.createStatement()
|
||||||
|
) {
|
||||||
|
stmt.executeUpdate("DELETE FROM Inventory WHERE expires < " + now(-5 * MINUTE));
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
LOG.debug(e.getMessage(), e);
|
LOG.debug(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
@ -201,29 +201,43 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
|
|||||||
// save from address if necessary
|
// save from address if necessary
|
||||||
if (message.getId() == null) {
|
if (message.getId() == null) {
|
||||||
BitmessageAddress savedAddress = ctx.getAddressRepository().getAddress(message.getFrom().getAddress());
|
BitmessageAddress savedAddress = ctx.getAddressRepository().getAddress(message.getFrom().getAddress());
|
||||||
if (savedAddress == null || savedAddress.getPrivateKey() == null) {
|
if (savedAddress == null) {
|
||||||
if (savedAddress != null && savedAddress.getAlias() != null) {
|
|
||||||
message.getFrom().setAlias(savedAddress.getAlias());
|
|
||||||
}
|
|
||||||
ctx.getAddressRepository().save(message.getFrom());
|
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 connection = config.getConnection()) {
|
||||||
try {
|
try {
|
||||||
connection.setAutoCommit(false);
|
connection.setAutoCommit(false);
|
||||||
// save message
|
save(connection, message);
|
||||||
|
updateLabels(connection, message);
|
||||||
|
connection.commit();
|
||||||
|
} catch (IOException | SQLException e) {
|
||||||
|
connection.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
} catch (IOException | SQLException e) {
|
||||||
|
throw new ApplicationException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void save(Connection connection, Plaintext message) throws IOException, SQLException {
|
||||||
if (message.getId() == null) {
|
if (message.getId() == null) {
|
||||||
insert(connection, message);
|
insert(connection, message);
|
||||||
} else {
|
} else {
|
||||||
update(connection, message);
|
update(connection, message);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateLabels(Connection connection, Plaintext message) throws SQLException {
|
||||||
// remove existing labels
|
// remove existing labels
|
||||||
try (Statement stmt = connection.createStatement()) {
|
try (Statement stmt = connection.createStatement()) {
|
||||||
stmt.executeUpdate("DELETE FROM Message_Label WHERE message_id=" + message.getId());
|
stmt.executeUpdate("DELETE FROM Message_Label WHERE message_id=" + message.getId());
|
||||||
}
|
}
|
||||||
// save labels
|
// save new labels
|
||||||
try (PreparedStatement ps = connection.prepareStatement("INSERT INTO Message_Label VALUES (" +
|
try (PreparedStatement ps = connection.prepareStatement("INSERT INTO Message_Label VALUES (" +
|
||||||
message.getId() + ", ?)")) {
|
message.getId() + ", ?)")) {
|
||||||
for (Label label : message.getLabels()) {
|
for (Label label : message.getLabels()) {
|
||||||
@ -231,18 +245,6 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
|
|||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
connection.commit();
|
|
||||||
} catch (IOException | SQLException e) {
|
|
||||||
try {
|
|
||||||
connection.rollback();
|
|
||||||
} catch (SQLException e1) {
|
|
||||||
LOG.debug(e1.getMessage(), e);
|
|
||||||
}
|
|
||||||
throw new ApplicationException(e);
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new ApplicationException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void insert(Connection connection, Plaintext message) throws SQLException, IOException {
|
private void insert(Connection connection, Plaintext message) throws SQLException, IOException {
|
||||||
|
Loading…
Reference in New Issue
Block a user