Migrated core and extension modules to Kotlin

(Except BitmessageContext and Bytes)
This commit is contained in:
2017-06-06 16:36:07 +02:00
parent 811625c051
commit fa0e53289c
213 changed files with 9846 additions and 11067 deletions

View File

@ -97,10 +97,10 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
private List<BitmessageAddress> find(String where) {
List<BitmessageAddress> result = new LinkedList<>();
try (
Connection connection = config.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT address, alias, public_key, private_key, subscribed, chan " +
"FROM Address WHERE " + where)
Connection connection = config.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT address, alias, public_key, private_key, subscribed, chan " +
"FROM Address WHERE " + where)
) {
while (rs.next()) {
BitmessageAddress address;
@ -111,7 +111,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
Blob publicKeyBlob = rs.getBlob("public_key");
if (publicKeyBlob != null) {
Pubkey pubkey = Factory.readPubkey(address.getVersion(), address.getStream(),
publicKeyBlob.getBinaryStream(), (int) publicKeyBlob.length(), false);
publicKeyBlob.getBinaryStream(), (int) publicKeyBlob.length(), false);
if (address.getVersion() == 4 && pubkey instanceof V3Pubkey) {
pubkey = new V4Pubkey((V3Pubkey) pubkey);
}
@ -127,7 +127,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
result.add(address);
}
} catch (IOException | SQLException e) {
} catch (SQLException e) {
LOG.error(e.getMessage(), e);
}
return result;
@ -135,10 +135,10 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
private boolean exists(BitmessageAddress address) {
try (
Connection connection = config.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM Address " +
"WHERE address='" + address.getAddress() + "'")
Connection connection = config.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM Address " +
"WHERE address='" + address.getAddress() + "'")
) {
if (rs.next()) {
return rs.getInt(1) > 0;
@ -172,8 +172,8 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
}
statement.append(", subscribed=?, chan=? WHERE address=?");
try (
Connection connection = config.getConnection();
PreparedStatement ps = connection.prepareStatement(statement.toString())
Connection connection = config.getConnection();
PreparedStatement ps = connection.prepareStatement(statement.toString())
) {
int i = 0;
ps.setString(++i, address.getAlias());
@ -192,10 +192,10 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
private void insert(BitmessageAddress address) throws IOException, SQLException {
try (
Connection connection = config.getConnection();
PreparedStatement ps = connection.prepareStatement(
"INSERT INTO Address (address, version, alias, public_key, private_key, subscribed, chan) " +
"VALUES (?, ?, ?, ?, ?, ?, ?)")
Connection connection = config.getConnection();
PreparedStatement ps = connection.prepareStatement(
"INSERT INTO Address (address, version, alias, public_key, private_key, subscribed, chan) " +
"VALUES (?, ?, ?, ?, ?, ?, ?)")
) {
ps.setString(1, address.getAddress());
ps.setLong(2, address.getVersion());
@ -221,8 +221,8 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
@Override
public void remove(BitmessageAddress address) {
try (
Connection connection = config.getConnection();
Statement stmt = connection.createStatement()
Connection connection = config.getConnection();
Statement stmt = connection.createStatement()
) {
stmt.executeUpdate("DELETE FROM Address WHERE address = '" + address.getAddress() + "'");
} catch (SQLException e) {

View File

@ -17,24 +17,16 @@
package ch.dissem.bitmessage.repository;
import ch.dissem.bitmessage.entity.Streamable;
import ch.dissem.bitmessage.entity.payload.ObjectType;
import ch.dissem.bitmessage.exception.ApplicationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;
import static ch.dissem.bitmessage.utils.Strings.hex;
/**
* Helper class that does Flyway migration, provides JDBC connections and some helper methods.
*/
public abstract class JdbcHelper {
private static final Logger LOG = LoggerFactory.getLogger(JdbcHelper.class);
protected final JdbcConfig config;

View File

@ -49,8 +49,8 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
List<InventoryVector> result = new LinkedList<>();
for (long stream : streams) {
getCache(stream).entrySet().stream()
.filter(e -> e.getValue() > now())
.forEach(e -> result.add(e.getKey()));
.filter(e -> e.getValue() > now())
.forEach(e -> result.add(e.getKey()));
}
return result;
}
@ -63,10 +63,10 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
result = new ConcurrentHashMap<>();
cache.put(stream, result);
try (
Connection connection = config.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT hash, expires FROM Inventory " +
"WHERE expires > " + now(-5 * MINUTE) + " AND stream = " + stream)
Connection connection = config.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT hash, expires FROM Inventory " +
"WHERE expires > " + (now() - 5 * MINUTE) + " AND stream = " + stream)
) {
while (rs.next()) {
result.put(InventoryVector.fromHash(rs.getBytes("hash")), rs.getLong("expires"));
@ -91,9 +91,9 @@ 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 + "'")
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");
@ -121,9 +121,9 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
query.append(" AND type IN (").append(join(types)).append(')');
}
try (
Connection connection = config.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query.toString())
Connection connection = config.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query.toString())
) {
List<ObjectMessage> result = new LinkedList<>();
while (rs.next()) {
@ -143,9 +143,9 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
return;
try (
Connection connection = config.getConnection();
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();
LOG.trace("Storing object " + iv);
@ -167,21 +167,21 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
@Override
public boolean contains(ObjectMessage object) {
return getCache(object.getStream()).entrySet().stream()
.anyMatch(x -> x.getKey().equals(object.getInventoryVector()));
.anyMatch(x -> x.getKey().equals(object.getInventoryVector()));
}
@Override
public void cleanup() {
try (
Connection connection = config.getConnection();
Statement stmt = connection.createStatement()
Connection connection = config.getConnection();
Statement stmt = connection.createStatement()
) {
stmt.executeUpdate("DELETE FROM Inventory WHERE expires < " + now(-5 * MINUTE));
stmt.executeUpdate("DELETE FROM Inventory WHERE expires < " + (now() - 5 * MINUTE));
} catch (SQLException e) {
LOG.debug(e.getMessage(), e);
}
for (Map<InventoryVector, Long> c : cache.values()) {
c.entrySet().removeIf(e -> e.getValue() < now(-5 * MINUTE));
c.entrySet().removeIf(e -> e.getValue() < (now() - 5 * MINUTE));
}
}
}

View File

@ -111,8 +111,8 @@ public class JdbcMessageRepository extends AbstractMessageRepository implements
long id = rs.getLong("id");
builder.id(id);
builder.IV(InventoryVector.fromHash(iv));
builder.from(ctx.getAddressRepository().getAddress(rs.getString("sender")));
builder.to(ctx.getAddressRepository().getAddress(rs.getString("recipient")));
builder.from(getCtx().getAddressRepository().getAddress(rs.getString("sender")));
builder.to(getCtx().getAddressRepository().getAddress(rs.getString("recipient")));
builder.ackData(rs.getBytes("ack_data"));
builder.sent(rs.getObject("sent", Long.class));
builder.received(rs.getObject("received", Long.class));
@ -127,7 +127,7 @@ public class JdbcMessageRepository extends AbstractMessageRepository implements
message.setInitialHash(rs.getBytes("initial_hash"));
result.add(message);
}
} catch (IOException | SQLException e) {
} catch (SQLException e) {
LOG.error(e.getMessage(), e);
}
return result;

View File

@ -1,3 +1,19 @@
/*
* Copyright 2016 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.repository;
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
@ -33,7 +49,7 @@ public class JdbcNodeRegistry extends JdbcHelper implements NodeRegistry {
PreparedStatement ps = connection.prepareStatement(
"DELETE FROM Node WHERE time<?")
) {
ps.setLong(1, now(-28 * DAY));
ps.setLong(1, now() - 28 * DAY);
ps.executeUpdate();
} catch (SQLException e) {
LOG.error(e.getMessage(), e);
@ -135,7 +151,7 @@ public class JdbcNodeRegistry extends JdbcHelper implements NodeRegistry {
public void offerAddresses(List<NetworkAddress> nodes) {
cleanUp();
nodes.stream()
.filter(node -> node.getTime() < now(+2 * MINUTE) && node.getTime() > now(-28 * DAY))
.filter(node -> node.getTime() < now() + 2 * MINUTE && node.getTime() > now() - 28 * DAY)
.forEach(node -> {
synchronized (this) {
NetworkAddress existing = loadExisting(node);

View File

@ -1,3 +1,19 @@
/*
* Copyright 2016 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.repository;
import ch.dissem.bitmessage.InternalContext;
@ -30,9 +46,9 @@ public class JdbcProofOfWorkRepository extends JdbcHelper implements ProofOfWork
@Override
public Item getItem(byte[] initialHash) {
try (
Connection connection = config.getConnection();
PreparedStatement ps = connection.prepareStatement("SELECT data, version, nonce_trials_per_byte, " +
"extra_bytes, expiration_time, message_id FROM POW WHERE initial_hash=?")
Connection connection = config.getConnection();
PreparedStatement ps = connection.prepareStatement("SELECT data, version, nonce_trials_per_byte, " +
"extra_bytes, expiration_time, message_id FROM POW WHERE initial_hash=?")
) {
ps.setBytes(1, initialHash);
try (ResultSet rs = ps.executeQuery()) {
@ -40,17 +56,17 @@ public class JdbcProofOfWorkRepository extends JdbcHelper implements ProofOfWork
Blob data = rs.getBlob("data");
if (rs.getObject("message_id") == null) {
return new Item(
Factory.getObjectMessage(rs.getInt("version"), data.getBinaryStream(), (int) data.length()),
rs.getLong("nonce_trials_per_byte"),
rs.getLong("extra_bytes")
Factory.getObjectMessage(rs.getInt("version"), data.getBinaryStream(), (int) data.length()),
rs.getLong("nonce_trials_per_byte"),
rs.getLong("extra_bytes")
);
} else {
return new Item(
Factory.getObjectMessage(rs.getInt("version"), data.getBinaryStream(), (int) data.length()),
rs.getLong("nonce_trials_per_byte"),
rs.getLong("extra_bytes"),
rs.getLong("expiration_time"),
ctx.getMessageRepository().getMessage(rs.getLong("message_id"))
Factory.getObjectMessage(rs.getInt("version"), data.getBinaryStream(), (int) data.length()),
rs.getLong("nonce_trials_per_byte"),
rs.getLong("extra_bytes"),
rs.getLong("expiration_time"),
ctx.getMessageRepository().getMessage(rs.getLong("message_id"))
);
}
} else {
@ -66,9 +82,9 @@ public class JdbcProofOfWorkRepository extends JdbcHelper implements ProofOfWork
@Override
public List<byte[]> getItems() {
try (
Connection connection = config.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT initial_hash FROM POW")
Connection connection = config.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT initial_hash FROM POW")
) {
List<byte[]> result = new LinkedList<>();
while (rs.next()) {
@ -84,27 +100,27 @@ public class JdbcProofOfWorkRepository extends JdbcHelper implements ProofOfWork
@Override
public void putObject(Item item) {
try (
Connection connection = config.getConnection();
PreparedStatement ps = connection.prepareStatement("INSERT INTO POW (initial_hash, data, version, " +
"nonce_trials_per_byte, extra_bytes, expiration_time, message_id) " +
"VALUES (?, ?, ?, ?, ?, ?, ?)")
Connection connection = config.getConnection();
PreparedStatement ps = connection.prepareStatement("INSERT INTO POW (initial_hash, data, version, " +
"nonce_trials_per_byte, extra_bytes, expiration_time, message_id) " +
"VALUES (?, ?, ?, ?, ?, ?, ?)")
) {
ps.setBytes(1, cryptography().getInitialHash(item.object));
writeBlob(ps, 2, item.object);
ps.setLong(3, item.object.getVersion());
ps.setLong(4, item.nonceTrialsPerByte);
ps.setLong(5, item.extraBytes);
ps.setBytes(1, cryptography().getInitialHash(item.getObject()));
writeBlob(ps, 2, item.getObject());
ps.setLong(3, item.getObject().getVersion());
ps.setLong(4, item.getNonceTrialsPerByte());
ps.setLong(5, item.getExtraBytes());
if (item.message == null) {
if (item.getMessage() == null) {
ps.setObject(6, null);
ps.setObject(7, null);
} else {
ps.setLong(6, item.expirationTime);
ps.setLong(7, (Long) item.message.getId());
ps.setLong(6, item.getExpirationTime());
ps.setLong(7, (Long) item.getMessage().getId());
}
ps.executeUpdate();
} catch (IOException | SQLException e) {
LOG.debug("Error storing object of type " + item.object.getPayload().getClass().getSimpleName(), e);
LOG.debug("Error storing object of type " + item.getObject().getPayload().getClass().getSimpleName(), e);
throw new ApplicationException(e);
}
}
@ -117,8 +133,8 @@ public class JdbcProofOfWorkRepository extends JdbcHelper implements ProofOfWork
@Override
public void removeObject(byte[] initialHash) {
try (
Connection connection = config.getConnection();
PreparedStatement ps = connection.prepareStatement("DELETE FROM POW WHERE initial_hash=?")
Connection connection = config.getConnection();
PreparedStatement ps = connection.prepareStatement("DELETE FROM POW WHERE initial_hash=?")
) {
ps.setBytes(1, initialHash);
ps.executeUpdate();