Replaced clearSubscriptions() and addSubscription() with setSubscriptions().

This commit is contained in:
akwizgran
2011-07-14 09:52:05 +01:00
parent a121dcdda8
commit d4382fd232
6 changed files with 50 additions and 56 deletions

View File

@@ -105,10 +105,6 @@ interface Database<T> {
*/
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.
* <p>
@@ -314,6 +310,14 @@ interface Database<T> {
*/
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
* details.

View File

@@ -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)
throws DbException {
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)
throws DbException {
PreparedStatement ps = null;

View File

@@ -473,10 +473,8 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
try {
Txn txn = db.startTransaction();
try {
// FIXME: Replace clearSubs and addSub with setSubs
db.clearSubscriptions(txn, c);
Set<GroupId> subs = h.getSubscriptions();
for(GroupId sub : subs) db.addSubscription(txn, c, sub);
db.setSubscriptions(txn, c, subs);
if(LOG.isLoggable(Level.FINE))
LOG.fine("Received " + subs.size() + " subscriptions");
db.commitTransaction(txn);

View File

@@ -354,10 +354,8 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
synchronized(subscriptionLock) {
Txn txn = db.startTransaction();
try {
// FIXME: Replace clearSubs and addSub with setSubs
db.clearSubscriptions(txn, c);
Set<GroupId> subs = h.getSubscriptions();
for(GroupId sub : subs) db.addSubscription(txn, c, sub);
db.setSubscriptions(txn, c, subs);
if(LOG.isLoggable(Level.FINE))
LOG.fine("Received " + subs.size() + " subscriptions");
db.commitTransaction(txn);

View File

@@ -548,10 +548,9 @@ public abstract class DatabaseComponentTest extends TestCase {
will(returnValue(acks));
oneOf(database).removeAckedBatch(txn, contactId, batchId);
// Subscriptions
oneOf(database).clearSubscriptions(txn, contactId);
oneOf(header).getSubscriptions();
will(returnValue(subs));
oneOf(database).addSubscription(txn, contactId, groupId);
oneOf(database).setSubscriptions(txn, contactId, subs);
// Transports
oneOf(header).getTransports();
will(returnValue(transports));

View File

@@ -211,7 +211,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
assertEquals(contactId, db.addContact(txn, null));
db.addSubscription(txn, groupId);
db.addSubscription(txn, contactId, groupId);
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
db.addMessage(txn, message);
db.setStatus(txn, contactId, messageId, Status.NEW);
db.commitTransaction(txn);
@@ -253,7 +253,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
assertEquals(contactId, db.addContact(txn, null));
db.addSubscription(txn, groupId);
db.addSubscription(txn, contactId, groupId);
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
db.addMessage(txn, message);
db.setSendability(txn, messageId, 1);
db.commitTransaction(txn);
@@ -315,7 +315,7 @@ public class H2DatabaseTest extends TestCase {
// The contact subscribing should make the message sendable
txn = db.startTransaction();
db.addSubscription(txn, contactId, groupId);
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
assertTrue(it.hasNext());
assertEquals(messageId, it.next());
@@ -323,7 +323,7 @@ public class H2DatabaseTest extends TestCase {
// The contact unsubscribing should make the message unsendable
txn = db.startTransaction();
db.clearSubscriptions(txn, contactId);
db.setSubscriptions(txn, contactId, Collections.<GroupId>emptySet());
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
assertFalse(it.hasNext());
db.commitTransaction(txn);
@@ -342,7 +342,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
assertEquals(contactId, db.addContact(txn, null));
db.addSubscription(txn, groupId);
db.addSubscription(txn, contactId, groupId);
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
db.addMessage(txn, message);
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW);
@@ -408,7 +408,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
assertEquals(contactId, db.addContact(txn, null));
db.addSubscription(txn, groupId);
db.addSubscription(txn, contactId, groupId);
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
db.addMessage(txn, message);
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW);
@@ -451,7 +451,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
assertEquals(contactId, db.addContact(txn, null));
db.addSubscription(txn, groupId);
db.addSubscription(txn, contactId, groupId);
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
db.addMessage(txn, message);
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW);