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);