diff --git a/api/net/sf/briar/api/db/DatabaseComponent.java b/api/net/sf/briar/api/db/DatabaseComponent.java index 01f449af9..b71012781 100644 --- a/api/net/sf/briar/api/db/DatabaseComponent.java +++ b/api/net/sf/briar/api/db/DatabaseComponent.java @@ -50,6 +50,9 @@ public interface DatabaseComponent { */ void receiveBundle(NeighbourId n, Bundle b) throws DbException; + /** Removes a neighbour (and all associated state) from the database. */ + void removeNeighbour(NeighbourId n) throws DbException; + /** Records the user's rating for the given author. */ void setRating(AuthorId a, Rating r) throws DbException; diff --git a/components/net/sf/briar/db/Database.java b/components/net/sf/briar/db/Database.java index 6d6f1bad4..b00fa2ee0 100644 --- a/components/net/sf/briar/db/Database.java +++ b/components/net/sf/briar/db/Database.java @@ -19,6 +19,16 @@ import net.sf.briar.api.protocol.MessageId; * obtained by calling startTransaction(). Every transaction must be * terminated by calling either abortTransaction() or commitTransaction(), * even if an exception is thrown. + * + * Locking is provided by the DatabaseComponent implementation. To prevent + * deadlock, locks must be acquired in the following order: + *
+ * Locking: contacts read, messageStatuses write. */ void addBatchToAck(T txn, NeighbourId n, BatchId b) throws DbException; /** * Returns false if the given message is already in the database. Otherwise * stores the message and returns true. + *
* Locking: messages write. */ boolean addMessage(T txn, Message m) throws DbException; /** * Adds a new neighbour to the database. - * Locking: neighbours write. + *
+ * Locking: contacts write, messageStatuses write. */ void addNeighbour(T txn, NeighbourId n) throws DbException; /** * Records a sent batch as needing to be acknowledged. - * Locking: neighbours write, messages read. + *
+ * Locking: contacts read, messages read, messageStatuses write.
*/
void addOutstandingBatch(T txn, NeighbourId n, BatchId b, Set
+ * Locking: contacts read, messages read, messageStatuses write.
*/
Set
* Locking: subscriptions write.
*/
void addSubscription(T txn, GroupId g) throws DbException;
/**
* Records a neighbour's subscription to a group.
- * Locking: neighbours write.
+ *
+ * Locking: contacts read, messageStatuses write.
*/
void addSubscription(T txn, NeighbourId n, GroupId g) throws DbException;
/**
* Removes all recorded subscriptions for the given neighbour.
- * Locking: neighbours write.
+ *
+ * Locking: contacts read, messageStatuses write.
*/
void clearSubscriptions(T txn, NeighbourId n) throws DbException;
/**
* Returns true iff the database contains the given message.
+ *
* Locking: messages read.
*/
boolean containsMessage(T txn, MessageId m) throws DbException;
+ /**
+ * Returns true iff the database contains the given neighbour.
+ *
+ * Locking: contacts read.
+ */
+ boolean containsNeighbour(T txn, NeighbourId n) throws DbException;
+
/**
* Returns true iff the user is subscribed to the given group.
+ *
* Locking: subscriptions read.
*/
boolean containsSubscription(T txn, GroupId g) throws DbException;
@@ -114,18 +141,21 @@ interface Database
* Locking: messages read.
*/
long getFreeSpace() throws DbException;
/**
* Returns the message identified by the given ID.
+ *
* Locking: messages read.
*/
Message getMessage(T txn, MessageId m) throws DbException;
/**
* Returns the IDs of all messages signed by the given author.
+ *
* Locking: messages read.
*/
Iterable
* Locking: messages read.
*/
Iterable
+ * Locking: contacts read, messageStatuses read.
*/
Set
* Locking: messages read.
*/
Iterable
* Locking: messages read.
*/
MessageId getParent(T txn, MessageId m) throws DbException;
/**
* Returns the user's rating for the given author.
+ *
* Locking: ratings read.
*/
Rating getRating(T txn, AuthorId a) throws DbException;
@@ -166,6 +201,7 @@ interface Database
* Locking: messages read.
*/
int getSendability(T txn, MessageId m) throws DbException;
@@ -173,12 +209,14 @@ interface Database
+ * Locking: contacts read, messages read, messageStatuses read.
*/
Iterable
* Locking: subscriptions read.
*/
Set
+ * Locking: contacts read, messages read, messageStatuses write.
*/
void removeAckedBatch(T txn, NeighbourId n, BatchId b) throws DbException;
/**
* Removes and returns the IDs of any batches received from the given
* neighbour that need to be acknowledged.
- * Locking: neighbours write.
+ *
+ * Locking: contacts read, messageStatuses write.
*/
Set
+ * Locking: contacts read, messages read, messageStatuses write.
*/
void removeLostBatch(T txn, NeighbourId n, BatchId b) throws DbException;
/**
- * Removes a message from the database.
- * Locking: neighbours write, messages write.
+ * Removes a message (and all associated state) from the database.
+ *
+ * Locking: contacts read, messages write, messageStatuses write.
*/
void removeMessage(T txn, MessageId m) throws DbException;
+ /**
+ * Removes a neighbour (and all associated state) from the database.
+ *
+ * Locking: contacts write, messageStatuses write.
+ */
+ void removeNeighbour(T txn, NeighbourId n) throws DbException;
+
/**
* Unsubscribes from the given group. Any messages belonging to the group
* are deleted from the database.
- * Locking: subscriptions write, neighbours write, messages write.
+ *
+ * Locking: contacts read, subscriptions write, messages write,
+ * messageStatuses write.
*/
void removeSubscription(T txn, GroupId g) throws DbException;
/**
* Records the user's rating for the given author.
+ *
* Locking: ratings write.
*/
Rating setRating(T txn, AuthorId a, Rating r) throws DbException;
/**
* Records the sendability score of the given message.
+ *
* Locking: messages write.
*/
void setSendability(T txn, MessageId m, int sendability) throws DbException;
/**
- * Sets the status of the given message with respect to the given neighbour.
- * Locking: neighbours write, messages read
+ * Sets the status of the given message with respect to the given neighbour.
+ *
+ * Locking: contacts read, messages read, messageStatuses write.
*/
void setStatus(T txn, NeighbourId n, MessageId m, Status s) throws DbException;
}
diff --git a/components/net/sf/briar/db/DatabaseComponentImpl.java b/components/net/sf/briar/db/DatabaseComponentImpl.java
index db7dcc9da..638eecd38 100644
--- a/components/net/sf/briar/db/DatabaseComponentImpl.java
+++ b/components/net/sf/briar/db/DatabaseComponentImpl.java
@@ -74,7 +74,20 @@ abstract class DatabaseComponentImpl