Synchronisation API and related refactorings / improvements

-> lets you synchronize with the Bitmessage network without staying connected
This commit is contained in:
2015-10-07 21:50:41 +02:00
parent c3fdee79ca
commit f9ff22bebe
13 changed files with 154 additions and 49 deletions

View File

@ -24,7 +24,6 @@ import ch.dissem.bitmessage.ports.Inventory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;
@ -139,6 +138,23 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
}
}
@Override
public boolean contains(ObjectMessage object) {
try (Connection connection = config.getConnection()) {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT count(1) FROM Inventory WHERE hash = X'"
+ object.getInventoryVector() + "'");
if (rs.next()) {
return rs.getInt(1) > 0;
} else {
throw new RuntimeException("Couldn't query if inventory contains " + object.getInventoryVector());
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
@Override
public void cleanup() {
try (Connection connection = config.getConnection()) {

View File

@ -25,7 +25,6 @@ import ch.dissem.bitmessage.ports.MessageRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
@ -86,6 +85,29 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
return result;
}
@Override
public int countUnread(Label label) {
String where;
if (label != null) {
where = "id IN (SELECT message_id FROM Message_Label WHERE label_id=" + label.getId() + ") AND ";
} else {
where = "";
}
where += "id IN (SELECT message_id FROM Message_Label WHERE label_id IN (" +
"SELECT id FROM Label WHERE type = '" + Label.Type.UNREAD.name() + "'))";
try (Connection connection = config.getConnection()) {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT count(*) FROM Message WHERE " + where);
if (rs.next()) {
return rs.getInt(1);
}
} catch (SQLException e) {
LOG.error(e.getMessage(), e);
}
return 0;
}
@Override
public List<Plaintext> findMessages(Label label) {
return find("id IN (SELECT message_id FROM Message_Label WHERE label_id=" + label.getId() + ")");
@ -230,8 +252,20 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
@Override
public void remove(Plaintext message) {
try (Connection connection = config.getConnection()) {
Statement stmt = connection.createStatement();
stmt.executeUpdate("DELETE FROM Message WHERE id = " + message.getId());
try {
connection.setAutoCommit(false);
Statement stmt = connection.createStatement();
stmt.executeUpdate("DELETE FROM Message_Label WHERE message_id = " + message.getId());
stmt.executeUpdate("DELETE FROM Message WHERE id = " + message.getId());
connection.commit();
} catch (SQLException e) {
try {
connection.rollback();
} catch (SQLException e1) {
LOG.debug(e1.getMessage(), e);
}
LOG.error(e.getMessage(), e);
}
} catch (SQLException e) {
LOG.error(e.getMessage(), e);
}

View File

@ -110,6 +110,17 @@ public class JdbcInventoryTest extends TestBase {
assertNotNull(inventory.getObject(object.getInventoryVector()));
}
@Test
public void testContains() {
ObjectMessage object = getObjectMessage(5, 0, getGetPubkey());
assertFalse(inventory.contains(object));
inventory.storeObject(object);
assertTrue(inventory.contains(object));
}
@Test
public void testCleanup() throws Exception {
assertNotNull(inventory.getObject(inventoryVectorIgnore));