mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 14:19:53 +01:00
Add method for getting cooked message from DB.
This commit is contained in:
@@ -263,6 +263,15 @@ public interface DatabaseComponent {
|
|||||||
*/
|
*/
|
||||||
Collection<LocalAuthor> getLocalAuthors(Transaction txn) throws DbException;
|
Collection<LocalAuthor> getLocalAuthors(Transaction txn) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the message with the given ID.
|
||||||
|
* <p/>
|
||||||
|
* Read-only.
|
||||||
|
*
|
||||||
|
* @throws MessageDeletedException if the message has been deleted
|
||||||
|
*/
|
||||||
|
Message getMessage(Transaction txn, MessageId m) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the IDs of all delivered messages in the given group.
|
* Returns the IDs of all delivered messages in the given group.
|
||||||
* <p/>
|
* <p/>
|
||||||
|
|||||||
@@ -298,6 +298,15 @@ interface Database<T> {
|
|||||||
*/
|
*/
|
||||||
Collection<LocalAuthor> getLocalAuthors(T txn) throws DbException;
|
Collection<LocalAuthor> getLocalAuthors(T txn) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the message with the given ID.
|
||||||
|
* <p/>
|
||||||
|
* Read-only.
|
||||||
|
*
|
||||||
|
* @throws MessageDeletedException if the message has been deleted
|
||||||
|
*/
|
||||||
|
Message getMessage(T txn, MessageId m) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the IDs and states of all dependencies of the given message.
|
* Returns the IDs and states of all dependencies of the given message.
|
||||||
* For missing dependencies and dependencies in other groups, the state
|
* For missing dependencies and dependencies in other groups, the state
|
||||||
|
|||||||
@@ -457,6 +457,15 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
return db.getLocalAuthors(txn);
|
return db.getLocalAuthors(txn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Message getMessage(Transaction transaction, MessageId m)
|
||||||
|
throws DbException {
|
||||||
|
T txn = unbox(transaction);
|
||||||
|
if (!db.containsMessage(txn, m))
|
||||||
|
throw new NoSuchMessageException();
|
||||||
|
return db.getMessage(txn, m);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<MessageId> getMessageIds(Transaction transaction,
|
public Collection<MessageId> getMessageIds(Transaction transaction,
|
||||||
GroupId g) throws DbException {
|
GroupId g) throws DbException {
|
||||||
|
|||||||
@@ -1483,6 +1483,32 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Message getMessage(Connection txn, MessageId m) throws DbException {
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
try {
|
||||||
|
String sql = "SELECT groupId, timestamp, raw FROM messages"
|
||||||
|
+ " WHERE messageId = ?";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setBytes(1, m.getBytes());
|
||||||
|
rs = ps.executeQuery();
|
||||||
|
if (!rs.next()) throw new DbStateException();
|
||||||
|
GroupId g = new GroupId(rs.getBytes(1));
|
||||||
|
long timestamp = rs.getLong(2);
|
||||||
|
byte[] raw = rs.getBytes(3);
|
||||||
|
if (rs.next()) throw new DbStateException();
|
||||||
|
rs.close();
|
||||||
|
ps.close();
|
||||||
|
if (raw == null) throw new MessageDeletedException();
|
||||||
|
return new Message(m, g, timestamp, raw);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
tryToClose(rs);
|
||||||
|
tryToClose(ps);
|
||||||
|
throw new DbException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<MessageId> getMessageIds(Connection txn, GroupId g)
|
public Collection<MessageId> getMessageIds(Connection txn, GroupId g)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
|
|||||||
@@ -613,11 +613,11 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
|||||||
throws Exception {
|
throws Exception {
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
// Check whether the message is in the DB (which it's not)
|
// Check whether the message is in the DB (which it's not)
|
||||||
exactly(11).of(database).startTransaction();
|
exactly(12).of(database).startTransaction();
|
||||||
will(returnValue(txn));
|
will(returnValue(txn));
|
||||||
exactly(11).of(database).containsMessage(txn, messageId);
|
exactly(12).of(database).containsMessage(txn, messageId);
|
||||||
will(returnValue(false));
|
will(returnValue(false));
|
||||||
exactly(11).of(database).abortTransaction(txn);
|
exactly(12).of(database).abortTransaction(txn);
|
||||||
// This is needed for getMessageStatus() to proceed
|
// This is needed for getMessageStatus() to proceed
|
||||||
exactly(1).of(database).containsContact(txn, contactId);
|
exactly(1).of(database).containsContact(txn, contactId);
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
@@ -645,6 +645,16 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
|||||||
db.endTransaction(transaction);
|
db.endTransaction(transaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
transaction = db.startTransaction(false);
|
||||||
|
try {
|
||||||
|
db.getMessage(transaction, messageId);
|
||||||
|
fail();
|
||||||
|
} catch (NoSuchMessageException expected) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
db.endTransaction(transaction);
|
||||||
|
}
|
||||||
|
|
||||||
transaction = db.startTransaction(false);
|
transaction = db.startTransaction(false);
|
||||||
try {
|
try {
|
||||||
db.getRawMessage(transaction, messageId);
|
db.getRawMessage(transaction, messageId);
|
||||||
|
|||||||
@@ -1638,6 +1638,13 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
ids = db.getMessagesToOffer(txn, contactId, 100);
|
ids = db.getMessagesToOffer(txn, contactId, 100);
|
||||||
assertEquals(singletonList(messageId), ids);
|
assertEquals(singletonList(messageId), ids);
|
||||||
|
|
||||||
|
// The message should be available
|
||||||
|
Message m = db.getMessage(txn, messageId);
|
||||||
|
assertEquals(messageId, m.getId());
|
||||||
|
assertEquals(groupId, m.getGroupId());
|
||||||
|
assertEquals(message.getTimestamp(), m.getTimestamp());
|
||||||
|
assertArrayEquals(message.getRaw(), m.getRaw());
|
||||||
|
|
||||||
// The raw message should be available
|
// The raw message should be available
|
||||||
assertArrayEquals(message.getRaw(), db.getRawMessage(txn, messageId));
|
assertArrayEquals(message.getRaw(), db.getRawMessage(txn, messageId));
|
||||||
|
|
||||||
@@ -1653,6 +1660,14 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
ids = db.getMessagesToOffer(txn, contactId, 100);
|
ids = db.getMessagesToOffer(txn, contactId, 100);
|
||||||
assertTrue(ids.isEmpty());
|
assertTrue(ids.isEmpty());
|
||||||
|
|
||||||
|
// Requesting the message should throw an exception
|
||||||
|
try {
|
||||||
|
db.getMessage(txn, messageId);
|
||||||
|
fail();
|
||||||
|
} catch (MessageDeletedException expected) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
|
||||||
// Requesting the raw message should throw an exception
|
// Requesting the raw message should throw an exception
|
||||||
try {
|
try {
|
||||||
db.getRawMessage(txn, messageId);
|
db.getRawMessage(txn, messageId);
|
||||||
|
|||||||
Reference in New Issue
Block a user