Show which contacts subscribe to each forum. Dev task #79.

This commit is contained in:
akwizgran
2014-03-21 18:08:08 +00:00
parent e1d099903d
commit 4dcf9f632e
6 changed files with 187 additions and 10 deletions

View File

@@ -486,6 +486,13 @@ interface Database<T> {
*/
Settings getSettings(T txn) throws DbException;
/**
* Returns all contacts who subscribe to the given group.
* <p>
* Locking: subscription read.
*/
Collection<Contact> getSubscribers(T txn, GroupId g) throws DbException;
/**
* Returns a subscription ack for the given contact, or null if no ack is
* due.

View File

@@ -1154,6 +1154,23 @@ DatabaseCleaner.Callback {
}
}
public Collection<Contact> getSubscribers(GroupId g) throws DbException {
subscriptionLock.readLock().lock();
try {
T txn = db.startTransaction();
try {
Collection<Contact> contacts = db.getSubscribers(txn, g);
db.commitTransaction(txn);
return contacts;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
subscriptionLock.readLock().unlock();
}
}
public Map<TransportId, Long> getTransportLatencies() throws DbException {
transportLock.readLock().lock();
try {

View File

@@ -2154,6 +2154,40 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public Collection<Contact> getSubscribers(Connection txn, GroupId g)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT c.contactId, authorId, c.name, publicKey,"
+ " localAuthorId"
+ " FROM contacts AS c"
+ " JOIN contactGroups AS cg"
+ " ON c.contactId = cg.contactId"
+ " WHERE groupId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, g.getBytes());
rs = ps.executeQuery();
List<Contact> contacts = new ArrayList<Contact>();
while(rs.next()) {
ContactId contactId = new ContactId(rs.getInt(1));
AuthorId authorId = new AuthorId(rs.getBytes(2));
String name = rs.getString(3);
byte[] publicKey = rs.getBytes(4);
Author author = new Author(authorId, name, publicKey);
AuthorId localAuthorId = new AuthorId(rs.getBytes(5));
contacts.add(new Contact(contactId, author, localAuthorId));
}
rs.close();
ps.close();
return Collections.unmodifiableList(contacts);
} catch(SQLException e) {
tryToClose(rs);
tryToClose(ps);
throw new DbException(e);
}
}
public SubscriptionAck getSubscriptionAck(Connection txn, ContactId c)
throws DbException {
PreparedStatement ps = null;