Added a method for getting unread message counts for all groups.

This commit is contained in:
akwizgran
2011-10-26 17:56:35 +01:00
parent 6d91603bf7
commit 7d73f9604d
5 changed files with 121 additions and 0 deletions

View File

@@ -406,6 +406,13 @@ interface Database<T> {
*/
long getTransportsSent(T txn, ContactId c) throws DbException;
/**
* Returns the number of unread messages in each subscribed group.
* <p>
* Locking: message read, messageFlag read, subscription read.
*/
Map<GroupId, Integer> getUnreadMessageCounts(T txn) throws DbException;
/**
* Returns the contacts to which the given group is visible.
* <p>

View File

@@ -885,6 +885,34 @@ DatabaseCleaner.Callback {
}
}
public Map<GroupId, Integer> getUnreadMessageCounts() throws DbException {
messageLock.readLock().lock();
try {
messageFlagLock.readLock().lock();
try {
subscriptionLock.readLock().lock();
try {
T txn = db.startTransaction();
try {
Map<GroupId, Integer> counts =
db.getUnreadMessageCounts(txn);
db.commitTransaction(txn);
return counts;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
subscriptionLock.readLock().unlock();
}
} finally {
messageFlagLock.readLock().unlock();
}
} finally {
messageLock.readLock().unlock();
}
}
public Collection<ContactId> getVisibility(GroupId g) throws DbException {
contactLock.readLock().lock();
try {

View File

@@ -1709,6 +1709,33 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public Map<GroupId, Integer> getUnreadMessageCounts(Connection txn)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT groupId, COUNT(*)"
+ " FROM messages LEFT OUTER JOIN flags"
+ " ON messages.messageId = flags.messageId"
+ " WHERE (NOT read) OR (read IS NULL)"
+ " GROUP BY groupId";
ps = txn.prepareStatement(sql);
rs = ps.executeQuery();
Map<GroupId, Integer> counts = new HashMap<GroupId, Integer>();
while(rs.next()) {
GroupId g = new GroupId(rs.getBytes(1));
counts.put(g, rs.getInt(2));
}
rs.close();
ps.close();
return counts;
} catch(SQLException e) {
tryToClose(rs);
tryToClose(ps);
throw new DbException(e);
}
}
public Collection<ContactId> getVisibility(Connection txn, GroupId g)
throws DbException {
PreparedStatement ps = null;