diff --git a/briar-api/src/org/briarproject/api/contact/Contact.java b/briar-api/src/org/briarproject/api/contact/Contact.java index 18a2cb446..5c5b9b197 100644 --- a/briar-api/src/org/briarproject/api/contact/Contact.java +++ b/briar-api/src/org/briarproject/api/contact/Contact.java @@ -1,6 +1,5 @@ package org.briarproject.api.contact; -import org.briarproject.api.db.StorageStatus; import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorId; @@ -9,14 +8,11 @@ public class Contact { private final ContactId id; private final Author author; private final AuthorId localAuthorId; - private final StorageStatus status; - public Contact(ContactId id, Author author, AuthorId localAuthorId, - StorageStatus status) { + public Contact(ContactId id, Author author, AuthorId localAuthorId) { this.id = id; this.author = author; this.localAuthorId = localAuthorId; - this.status = status; } public ContactId getId() { @@ -31,10 +27,6 @@ public class Contact { return localAuthorId; } - public StorageStatus getStatus() { - return status; - } - @Override public int hashCode() { return id.hashCode(); diff --git a/briar-api/src/org/briarproject/api/contact/ContactManager.java b/briar-api/src/org/briarproject/api/contact/ContactManager.java index 08c9dc46a..ffc493477 100644 --- a/briar-api/src/org/briarproject/api/contact/ContactManager.java +++ b/briar-api/src/org/briarproject/api/contact/ContactManager.java @@ -1,6 +1,7 @@ package org.briarproject.api.contact; import org.briarproject.api.db.DbException; +import org.briarproject.api.db.Transaction; import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorId; @@ -30,10 +31,10 @@ public interface ContactManager { void removeContact(ContactId c) throws DbException; interface AddContactHook { - void addingContact(ContactId c); + void addingContact(Transaction txn, Contact c) throws DbException; } interface RemoveContactHook { - void removingContact(ContactId c); + void removingContact(Transaction txn, Contact c) throws DbException; } } diff --git a/briar-api/src/org/briarproject/api/db/DatabaseComponent.java b/briar-api/src/org/briarproject/api/db/DatabaseComponent.java index 5609844f4..ca2497694 100644 --- a/briar-api/src/org/briarproject/api/db/DatabaseComponent.java +++ b/briar-api/src/org/briarproject/api/db/DatabaseComponent.java @@ -26,57 +26,85 @@ import java.util.Map; /** * Encapsulates the database implementation and exposes high-level operations * to other components. - *
+ *
* This interface's methods are blocking, but they do not call out into other * components except to broadcast {@link org.briarproject.api.event.Event * Events}, so they can safely be called while holding locks. */ public interface DatabaseComponent { - /** Opens the database and returns true if the database already existed. */ + /** + * Opens the database and returns true if the database already existed. + */ boolean open() throws DbException; - /** Waits for any open transactions to finish and closes the database. */ + /** + * Waits for any open transactions to finish and closes the database. + */ void close() throws DbException, IOException; + /** + * Starts a new transaction and returns an object representing it. + */ + Transaction startTransaction() throws DbException; + + /** + * Ends a transaction. If the transaction is marked as complete, the + * transaction is committed and any events attached to the transaction are + * broadcast; otherwise the transaction is aborted. + */ + void endTransaction(Transaction txn) throws DbException; + /** * Stores a contact associated with the given local and remote pseudonyms, * and returns an ID for the contact. */ - ContactId addContact(Author remote, AuthorId local) throws DbException; - - /** Stores a group. */ - void addGroup(Group g) throws DbException; - - /** Stores a local pseudonym. */ - void addLocalAuthor(LocalAuthor a) throws DbException; - - /** Stores a local message. */ - void addLocalMessage(Message m, ClientId c, Metadata meta, boolean shared) + ContactId addContact(Transaction txn, Author remote, AuthorId local) throws DbException; - /** Stores a transport. */ - void addTransport(TransportId t, int maxLatency) throws DbException; + /** + * Stores a group. + */ + void addGroup(Transaction txn, Group g) throws DbException; + + /** + * Stores a local pseudonym. + */ + void addLocalAuthor(Transaction txn, LocalAuthor a) throws DbException; + + /** + * Stores a local message. + */ + void addLocalMessage(Transaction txn, Message m, ClientId c, Metadata meta, + boolean shared) throws DbException; + + /** + * Stores a transport. + */ + void addTransport(Transaction txn, TransportId t, int maxLatency) + throws DbException; /** * Stores transport keys for a newly added contact. */ - void addTransportKeys(ContactId c, TransportKeys k) throws DbException; + void addTransportKeys(Transaction txn, ContactId c, TransportKeys k) + throws DbException; /** * Deletes the message with the given ID. The message ID and any other * associated data are not deleted. */ - void deleteMessage(MessageId m) throws DbException; + void deleteMessage(Transaction txn, MessageId m) throws DbException; /** Deletes any metadata associated with the given message. */ - void deleteMessageMetadata(MessageId m) throws DbException; + void deleteMessageMetadata(Transaction txn, MessageId m) throws DbException; /** * Returns an acknowledgement for the given contact, or null if there are * no messages to acknowledge. */ - Ack generateAck(ContactId c, int maxMessages) throws DbException; + Ack generateAck(Transaction txn, ContactId c, int maxMessages) + throws DbException; /** * Returns a batch of raw messages for the given contact, with a total @@ -84,22 +112,23 @@ public interface DatabaseComponent { * transport with the given maximum latency. Returns null if there are no * sendable messages that fit in the given length. */ - Collection
- * Read-write locking is provided by the DatabaseComponent implementation.
*/
interface Database
- * Locking: write.
*/
boolean open() throws DbException;
/**
* Prevents new transactions from starting, waits for all current
* transactions to finish, and closes the database.
- *
- * Locking: write.
*/
void close() throws DbException, IOException;
- /** Starts a new transaction and returns an object representing it. */
+ /**
+ * Starts a new transaction and returns an object representing it.
+ */
T startTransaction() throws DbException;
/**
@@ -65,59 +60,39 @@ interface Database
- * Locking: write.
*/
ContactId addContact(T txn, Author remote, AuthorId local)
throws DbException;
/**
* Stores a group.
- *
- * Locking: write.
*/
void addGroup(T txn, Group g) throws DbException;
/**
* Stores a local pseudonym.
- *
- * Locking: write.
*/
void addLocalAuthor(T txn, LocalAuthor a) throws DbException;
/**
* Stores a message.
- *
- * Locking: write.
*/
void addMessage(T txn, Message m, Validity validity, boolean shared)
throws DbException;
/**
* Records that a message has been offered by the given contact.
- *
- * Locking: write.
*/
void addOfferedMessage(T txn, ContactId c, MessageId m) throws DbException;
/**
* Initialises the status of the given message with respect to the given
* contact.
- *
- * Locking: write.
- * @param ack whether the message needs to be acknowledged.
+ *
+ * @param ack whether the message needs to be acknowledged.
* @param seen whether the contact has seen the message.
*/
void addStatus(T txn, ContactId c, MessageId m, boolean ack, boolean seen)
@@ -125,76 +100,56 @@ interface Database
- * Locking: write.
*/
void addTransport(T txn, TransportId t, int maxLatency)
throws DbException;
/**
* Stores transport keys for a newly added contact.
- *
- * Locking: write.
*/
void addTransportKeys(T txn, ContactId c, TransportKeys k)
throws DbException;
/**
* Makes a group visible to the given contact.
- *
- * Locking: write.
*/
void addVisibility(T txn, ContactId c, GroupId g) throws DbException;
/**
* Returns true if the database contains the given contact for the given
* local pseudonym.
- *
- * Locking: read.
*/
boolean containsContact(T txn, AuthorId remote, AuthorId local)
throws DbException;
/**
* Returns true if the database contains the given contact.
- *
- * Locking: read.
*/
boolean containsContact(T txn, ContactId c) throws DbException;
/**
* Returns true if the database contains the given group.
- *
- * Locking: read.
*/
boolean containsGroup(T txn, GroupId g) throws DbException;
/**
* Returns true if the database contains the given local pseudonym.
- *
- * Locking: read.
*/
boolean containsLocalAuthor(T txn, AuthorId a) throws DbException;
/**
* Returns true if the database contains the given message.
- *
- * Locking: read.
*/
boolean containsMessage(T txn, MessageId m) throws DbException;
/**
* Returns true if the database contains the given transport.
- *
- * Locking: read.
*/
boolean containsTransport(T txn, TransportId t) throws DbException;
/**
* Returns true if the database contains the given group and the group is
* visible to the given contact.
- *
- * Locking: read.
*/
boolean containsVisibleGroup(T txn, ContactId c, GroupId g)
throws DbException;
@@ -202,16 +157,12 @@ interface Database
- * Locking: read.
*/
boolean containsVisibleMessage(T txn, ContactId c, MessageId m)
throws DbException;
/**
* Returns the number of messages offered by the given contact.
- *
- * Locking: read.
*/
int countOfferedMessages(T txn, ContactId c) throws DbException;
@@ -234,36 +185,26 @@ interface Database
- * Locking: read.
*/
Contact getContact(T txn, ContactId c) throws DbException;
/**
* Returns the IDs of all contacts.
- *
- * Locking: read.
*/
Collection
- * Locking: read.
*/
Collection
- * Locking: read.
*/
Collection
- * Locking: read.
*/
DeviceId getDeviceId(T txn) throws DbException;
@@ -276,59 +217,43 @@ interface Database
- * Locking: read.
*/
Group getGroup(T txn, GroupId g) throws DbException;
/**
* Returns the metadata for the given group.
- *
- * Locking: read.
*/
Metadata getGroupMetadata(T txn, GroupId g) throws DbException;
/**
* Returns all groups belonging to the given client.
- *
- * Locking: read.
*/
Collection
- * Locking: read.
*/
LocalAuthor getLocalAuthor(T txn, AuthorId a) throws DbException;
/**
* Returns all local pseudonyms.
- *
- * Locking: read.
*/
Collection
- * Locking: read.
*/
Map
- * Locking: read.
*/
Metadata getMessageMetadata(T txn, MessageId m) throws DbException;
/**
* Returns the status of all messages in the given group with respect
* to the given contact.
- *
- * Locking: read
*/
Collection
- * Locking: read
*/
MessageStatus getMessageStatus(T txn, ContactId c, MessageId m)
throws DbException;
@@ -345,8 +268,6 @@ interface Database
- * Locking: read.
*/
Collection
- * Locking: read.
*/
Collection
- * Locking: read.
*/
Collection
- * Locking: read.
*/
Collection
- * Locking: read.
*/
Collection
- * Locking: read.
*/
byte[] getRawMessage(T txn, MessageId m) throws DbException;
@@ -398,46 +309,34 @@ interface Database
- * Locking: read.
*/
Collection
- * Locking: read.
*/
Settings getSettings(T txn, String namespace) throws DbException;
/**
* Returns all transport keys for the given transport.
- *
- * Locking: read.
*/
Map
- * Locking: read.
*/
Map
- * Locking: read.
*/
Collection
- * Locking: write.
*/
void incrementStreamCounter(T txn, ContactId c, TransportId t,
long rotationPeriod) throws DbException;
@@ -445,8 +344,6 @@ interface Database
- * Locking: write.
*/
void lowerAckFlag(T txn, ContactId c, Collection
- * Locking: write.
*/
void lowerRequestedFlag(T txn, ContactId c, Collection
- * Locking: write.
*/
void mergeGroupMetadata(T txn, GroupId g, Metadata meta)
throws DbException;
@@ -472,8 +365,6 @@ interface Database
- * Locking: write.
*/
void mergeMessageMetadata(T txn, MessageId m, Metadata meta)
throws DbException;
@@ -481,65 +372,47 @@ interface Database
- * Locking: write.
*/
void mergeSettings(T txn, Settings s, String namespace) throws DbException;
/**
* Marks a message as needing to be acknowledged to the given contact.
- *
- * Locking: write.
*/
void raiseAckFlag(T txn, ContactId c, MessageId m) throws DbException;
/**
* Marks a message as having been requested by the given contact.
- *
- * Locking: write.
*/
void raiseRequestedFlag(T txn, ContactId c, MessageId m) throws DbException;
/**
* Marks a message as having been seen by the given contact.
- *
- * Locking: write.
*/
void raiseSeenFlag(T txn, ContactId c, MessageId m) throws DbException;
/**
* Removes a contact from the database.
- *
- * Locking: write.
*/
void removeContact(T txn, ContactId c) throws DbException;
/**
* Removes a group (and all associated state) from the database.
- *
- * Locking: write.
*/
void removeGroup(T txn, GroupId g) throws DbException;
/**
* Removes a local pseudonym (and all associated state) from the database.
- *
- * Locking: write.
*/
void removeLocalAuthor(T txn, AuthorId a) throws DbException;
/**
* Removes a message (and all associated state) from the database.
- *
- * Locking: write.
*/
void removeMessage(T txn, MessageId m) throws DbException;
/**
* Removes an offered message that was offered by the given contact, or
* returns false if there is no such message.
- *
- * Locking: write.
*/
boolean removeOfferedMessage(T txn, ContactId c, MessageId m)
throws DbException;
@@ -547,70 +420,40 @@ interface Database
- * Locking: write.
*/
void removeOfferedMessages(T txn, ContactId c,
Collection
- * Locking: write.
*/
void removeTransport(T txn, TransportId t) throws DbException;
/**
* Makes a group invisible to the given contact.
- *
- * Locking: write.
*/
void removeVisibility(T txn, ContactId c, GroupId g) throws DbException;
/**
* Resets the transmission count and expiry time of the given message with
* respect to the given contact.
- *
- * Locking: write.
*/
void resetExpiryTime(T txn, ContactId c, MessageId m) throws DbException;
- /**
- * Sets the status of the given contact.
- *
- * Locking: write.
- */
- void setContactStatus(T txn, ContactId c, StorageStatus s)
- throws DbException;
-
- /**
- * Sets the status of the given local pseudonym.
- *
- * Locking: write.
- */
- void setLocalAuthorStatus(T txn, AuthorId a, StorageStatus s)
- throws DbException;
-
/**
* Marks the given message as shared or unshared.
- *
- * Locking: write.
*/
void setMessageShared(T txn, MessageId m, boolean shared)
throws DbException;
/**
* Marks the given message as valid or invalid.
- *
- * Locking: write.
*/
void setMessageValid(T txn, MessageId m, boolean valid) throws DbException;
/**
* Sets the reordering window for the given contact and transport in the
* given rotation period.
- *
- * Locking: write.
*/
void setReorderingWindow(T txn, ContactId c, TransportId t,
long rotationPeriod, long base, byte[] bitmap) throws DbException;
@@ -619,16 +462,12 @@ interface Database
- * Locking: write.
*/
void updateExpiryTime(T txn, ContactId c, MessageId m, int maxLatency)
throws DbException;
/**
* Stores the given transport keys, deleting any keys they have replaced.
- *
- * Locking: write.
*/
void updateTransportKeys(T txn, Map
- * Locking: write.
+ *
* @param sender null for a locally generated message.
*/
private void addMessage(T txn, Message m, Validity validity, boolean shared,
@@ -251,1053 +208,457 @@ class DatabaseComponentImpl