Added method for deleting metadata.

This commit is contained in:
akwizgran
2016-02-10 13:58:46 +00:00
parent 00275e260f
commit a91d500263
6 changed files with 71 additions and 3 deletions

View File

@@ -69,6 +69,9 @@ public interface DatabaseComponent {
*/
void deleteMessage(MessageId m) throws DbException;
/** Deletes any metadata associated with the given message. */
void deleteMessageMetadata(MessageId m) throws DbException;
/**
* Returns an acknowledgement for the given contact, or null if there are
* no messages to acknowledge.

View File

@@ -225,6 +225,13 @@ interface Database<T> {
*/
void deleteMessage(T txn, MessageId m) throws DbException;
/**
* Deletes any metadata associated with the given message.
* <p>
* Locking: write.
*/
void deleteMessageMetadata(T txn, MessageId m) throws DbException;
/**
* Returns the contact with the given ID.
* <p>

View File

@@ -310,6 +310,23 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
}
public void deleteMessageMetadata(MessageId m) throws DbException {
lock.writeLock().lock();
try {
T txn = db.startTransaction();
try {
if (!db.containsMessage(txn, m))
throw new NoSuchMessageException();
db.deleteMessageMetadata(txn, m);
} catch (DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
lock.writeLock().unlock();
}
}
public Ack generateAck(ContactId c, int maxMessages) throws DbException {
Collection<MessageId> ids;
lock.writeLock().lock();

View File

@@ -949,6 +949,22 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public void deleteMessageMetadata(Connection txn, MessageId m)
throws DbException {
PreparedStatement ps = null;
try {
String sql = "DELETE FROM messageMetadata WHERE messageId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getBytes());
int affected = ps.executeUpdate();
if (affected < 0) throw new DbStateException();
ps.close();
} catch (SQLException e) {
tryToClose(ps);
throw new DbException(e);
}
}
public Contact getContact(Connection txn, ContactId c) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;

View File

@@ -543,11 +543,11 @@ public class DatabaseComponentImplTest extends BriarTestCase {
final EventBus eventBus = context.mock(EventBus.class);
context.checking(new Expectations() {{
// Check whether the message is in the DB (which it's not)
exactly(6).of(database).startTransaction();
exactly(8).of(database).startTransaction();
will(returnValue(txn));
exactly(6).of(database).containsMessage(txn, messageId);
exactly(8).of(database).containsMessage(txn, messageId);
will(returnValue(false));
exactly(6).of(database).abortTransaction(txn);
exactly(8).of(database).abortTransaction(txn);
// This is needed for getMessageStatus() to proceed
exactly(1).of(database).containsContact(txn, contactId);
will(returnValue(true));
@@ -555,6 +555,20 @@ public class DatabaseComponentImplTest extends BriarTestCase {
DatabaseComponent db = createDatabaseComponent(database, eventBus,
shutdown);
try {
db.deleteMessage(messageId);
fail();
} catch (NoSuchMessageException expected) {
// Expected
}
try {
db.deleteMessageMetadata(messageId);
fail();
} catch (NoSuchMessageException expected) {
// Expected
}
try {
db.getRawMessage(messageId);
fail();

View File

@@ -939,6 +939,17 @@ public class H2DatabaseTest extends BriarTestCase {
assertTrue(retrieved.containsKey("baz"));
assertArrayEquals(metadata.get("baz"), retrieved.get("baz"));
// Delete the metadata
db.deleteMessageMetadata(txn, messageId);
// Retrieve the metadata again
retrieved = db.getMessageMetadata(txn, messageId);
assertTrue(retrieved.isEmpty());
// Retrieve the metadata for the group again
all = db.getMessageMetadata(txn, groupId);
assertTrue(all.isEmpty());
db.commitTransaction(txn);
db.close();
}