mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Replaced clearSubscriptions() and addSubscription() with setSubscriptions().
This commit is contained in:
@@ -105,10 +105,6 @@ interface Database<T> {
|
|||||||
*/
|
*/
|
||||||
void addSubscription(T txn, GroupId g) throws DbException;
|
void addSubscription(T txn, GroupId g) throws DbException;
|
||||||
|
|
||||||
// FIXME: Replace these two methods with a setSubscriptions() method
|
|
||||||
void addSubscription(T txn, ContactId c, GroupId g) throws DbException;
|
|
||||||
void clearSubscriptions(T txn, ContactId c) throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true iff the database contains the given contact.
|
* Returns true iff the database contains the given contact.
|
||||||
* <p>
|
* <p>
|
||||||
@@ -314,6 +310,14 @@ interface Database<T> {
|
|||||||
*/
|
*/
|
||||||
void setStatus(T txn, ContactId c, MessageId m, Status s) throws DbException;
|
void setStatus(T txn, ContactId c, MessageId m, Status s) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the subscriptions for the given contact, replacing any existing
|
||||||
|
* subscriptions.
|
||||||
|
* <p>
|
||||||
|
* Locking: contacts read, subscriptions write.
|
||||||
|
*/
|
||||||
|
void setSubscriptions(T txn, ContactId c, Set<GroupId> subs) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the local transport details, replacing any existing transport
|
* Sets the local transport details, replacing any existing transport
|
||||||
* details.
|
* details.
|
||||||
|
|||||||
@@ -526,43 +526,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSubscription(Connection txn, ContactId c, GroupId g)
|
|
||||||
throws DbException {
|
|
||||||
PreparedStatement ps = null;
|
|
||||||
try {
|
|
||||||
String sql = "INSERT INTO contactSubscriptions"
|
|
||||||
+ " (contactId, groupId)"
|
|
||||||
+ " VALUES (?, ?)";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
ps.setInt(1, c.getInt());
|
|
||||||
ps.setBytes(2, g.getBytes());
|
|
||||||
int rowsAffected = ps.executeUpdate();
|
|
||||||
assert rowsAffected == 1;
|
|
||||||
ps.close();
|
|
||||||
} catch(SQLException e) {
|
|
||||||
tryToClose(ps);
|
|
||||||
tryToClose(txn);
|
|
||||||
throw new DbException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clearSubscriptions(Connection txn, ContactId c)
|
|
||||||
throws DbException {
|
|
||||||
PreparedStatement ps = null;
|
|
||||||
try {
|
|
||||||
String sql = "DELETE FROM contactSubscriptions"
|
|
||||||
+ " WHERE contactId = ?";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
ps.setInt(1, c.getInt());
|
|
||||||
ps.executeUpdate();
|
|
||||||
ps.close();
|
|
||||||
} catch(SQLException e) {
|
|
||||||
tryToClose(ps);
|
|
||||||
tryToClose(txn);
|
|
||||||
throw new DbException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean containsContact(Connection txn, ContactId c)
|
public boolean containsContact(Connection txn, ContactId c)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
@@ -1327,6 +1290,38 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setSubscriptions(Connection txn, ContactId c, Set<GroupId> subs)
|
||||||
|
throws DbException {
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
try {
|
||||||
|
// Delete any existing subscriptions
|
||||||
|
String sql = "DELETE FROM contactSubscriptions WHERE contactId = ?";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setInt(1, c.getInt());
|
||||||
|
ps.executeUpdate();
|
||||||
|
ps.close();
|
||||||
|
// Store the new subscriptions
|
||||||
|
sql = "INSERT INTO contactSubscriptions (contactId, groupId)"
|
||||||
|
+ " VALUES (?, ?)";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setInt(1, c.getInt());
|
||||||
|
for(GroupId g : subs) {
|
||||||
|
ps.setBytes(2, g.getBytes());
|
||||||
|
ps.addBatch();
|
||||||
|
}
|
||||||
|
int[] rowsAffectedArray = ps.executeBatch();
|
||||||
|
assert rowsAffectedArray.length == subs.size();
|
||||||
|
for(int i = 0; i < rowsAffectedArray.length; i++) {
|
||||||
|
assert rowsAffectedArray[i] == 1;
|
||||||
|
}
|
||||||
|
ps.close();
|
||||||
|
} catch(SQLException e) {
|
||||||
|
tryToClose(ps);
|
||||||
|
tryToClose(txn);
|
||||||
|
throw new DbException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void setTransports(Connection txn, Map<String, String> transports)
|
public void setTransports(Connection txn, Map<String, String> transports)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
|
|||||||
@@ -473,10 +473,8 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
try {
|
try {
|
||||||
Txn txn = db.startTransaction();
|
Txn txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
// FIXME: Replace clearSubs and addSub with setSubs
|
|
||||||
db.clearSubscriptions(txn, c);
|
|
||||||
Set<GroupId> subs = h.getSubscriptions();
|
Set<GroupId> subs = h.getSubscriptions();
|
||||||
for(GroupId sub : subs) db.addSubscription(txn, c, sub);
|
db.setSubscriptions(txn, c, subs);
|
||||||
if(LOG.isLoggable(Level.FINE))
|
if(LOG.isLoggable(Level.FINE))
|
||||||
LOG.fine("Received " + subs.size() + " subscriptions");
|
LOG.fine("Received " + subs.size() + " subscriptions");
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
|
|||||||
@@ -354,10 +354,8 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
synchronized(subscriptionLock) {
|
synchronized(subscriptionLock) {
|
||||||
Txn txn = db.startTransaction();
|
Txn txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
// FIXME: Replace clearSubs and addSub with setSubs
|
|
||||||
db.clearSubscriptions(txn, c);
|
|
||||||
Set<GroupId> subs = h.getSubscriptions();
|
Set<GroupId> subs = h.getSubscriptions();
|
||||||
for(GroupId sub : subs) db.addSubscription(txn, c, sub);
|
db.setSubscriptions(txn, c, subs);
|
||||||
if(LOG.isLoggable(Level.FINE))
|
if(LOG.isLoggable(Level.FINE))
|
||||||
LOG.fine("Received " + subs.size() + " subscriptions");
|
LOG.fine("Received " + subs.size() + " subscriptions");
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
|
|||||||
@@ -548,10 +548,9 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
will(returnValue(acks));
|
will(returnValue(acks));
|
||||||
oneOf(database).removeAckedBatch(txn, contactId, batchId);
|
oneOf(database).removeAckedBatch(txn, contactId, batchId);
|
||||||
// Subscriptions
|
// Subscriptions
|
||||||
oneOf(database).clearSubscriptions(txn, contactId);
|
|
||||||
oneOf(header).getSubscriptions();
|
oneOf(header).getSubscriptions();
|
||||||
will(returnValue(subs));
|
will(returnValue(subs));
|
||||||
oneOf(database).addSubscription(txn, contactId, groupId);
|
oneOf(database).setSubscriptions(txn, contactId, subs);
|
||||||
// Transports
|
// Transports
|
||||||
oneOf(header).getTransports();
|
oneOf(header).getTransports();
|
||||||
will(returnValue(transports));
|
will(returnValue(transports));
|
||||||
|
|||||||
@@ -211,7 +211,7 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
assertEquals(contactId, db.addContact(txn, null));
|
assertEquals(contactId, db.addContact(txn, null));
|
||||||
db.addSubscription(txn, groupId);
|
db.addSubscription(txn, groupId);
|
||||||
db.addSubscription(txn, contactId, groupId);
|
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
|
||||||
db.addMessage(txn, message);
|
db.addMessage(txn, message);
|
||||||
db.setStatus(txn, contactId, messageId, Status.NEW);
|
db.setStatus(txn, contactId, messageId, Status.NEW);
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
@@ -253,7 +253,7 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
assertEquals(contactId, db.addContact(txn, null));
|
assertEquals(contactId, db.addContact(txn, null));
|
||||||
db.addSubscription(txn, groupId);
|
db.addSubscription(txn, groupId);
|
||||||
db.addSubscription(txn, contactId, groupId);
|
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
|
||||||
db.addMessage(txn, message);
|
db.addMessage(txn, message);
|
||||||
db.setSendability(txn, messageId, 1);
|
db.setSendability(txn, messageId, 1);
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
@@ -315,7 +315,7 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
|
|
||||||
// The contact subscribing should make the message sendable
|
// The contact subscribing should make the message sendable
|
||||||
txn = db.startTransaction();
|
txn = db.startTransaction();
|
||||||
db.addSubscription(txn, contactId, groupId);
|
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
|
||||||
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
|
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
|
||||||
assertTrue(it.hasNext());
|
assertTrue(it.hasNext());
|
||||||
assertEquals(messageId, it.next());
|
assertEquals(messageId, it.next());
|
||||||
@@ -323,7 +323,7 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
|
|
||||||
// The contact unsubscribing should make the message unsendable
|
// The contact unsubscribing should make the message unsendable
|
||||||
txn = db.startTransaction();
|
txn = db.startTransaction();
|
||||||
db.clearSubscriptions(txn, contactId);
|
db.setSubscriptions(txn, contactId, Collections.<GroupId>emptySet());
|
||||||
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
|
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
|
||||||
assertFalse(it.hasNext());
|
assertFalse(it.hasNext());
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
@@ -342,7 +342,7 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
assertEquals(contactId, db.addContact(txn, null));
|
assertEquals(contactId, db.addContact(txn, null));
|
||||||
db.addSubscription(txn, groupId);
|
db.addSubscription(txn, groupId);
|
||||||
db.addSubscription(txn, contactId, groupId);
|
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
|
||||||
db.addMessage(txn, message);
|
db.addMessage(txn, message);
|
||||||
db.setSendability(txn, messageId, 1);
|
db.setSendability(txn, messageId, 1);
|
||||||
db.setStatus(txn, contactId, messageId, Status.NEW);
|
db.setStatus(txn, contactId, messageId, Status.NEW);
|
||||||
@@ -408,7 +408,7 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
assertEquals(contactId, db.addContact(txn, null));
|
assertEquals(contactId, db.addContact(txn, null));
|
||||||
db.addSubscription(txn, groupId);
|
db.addSubscription(txn, groupId);
|
||||||
db.addSubscription(txn, contactId, groupId);
|
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
|
||||||
db.addMessage(txn, message);
|
db.addMessage(txn, message);
|
||||||
db.setSendability(txn, messageId, 1);
|
db.setSendability(txn, messageId, 1);
|
||||||
db.setStatus(txn, contactId, messageId, Status.NEW);
|
db.setStatus(txn, contactId, messageId, Status.NEW);
|
||||||
@@ -451,7 +451,7 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
assertEquals(contactId, db.addContact(txn, null));
|
assertEquals(contactId, db.addContact(txn, null));
|
||||||
db.addSubscription(txn, groupId);
|
db.addSubscription(txn, groupId);
|
||||||
db.addSubscription(txn, contactId, groupId);
|
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
|
||||||
db.addMessage(txn, message);
|
db.addMessage(txn, message);
|
||||||
db.setSendability(txn, messageId, 1);
|
db.setSendability(txn, messageId, 1);
|
||||||
db.setStatus(txn, contactId, messageId, Status.NEW);
|
db.setStatus(txn, contactId, messageId, Status.NEW);
|
||||||
|
|||||||
Reference in New Issue
Block a user