Replaced Database.getParent() with getGroupMessageParent().

The new method checks whether the parent is present in the database
and belongs to the same group, so separate methods for those checks
have been removed.
This commit is contained in:
akwizgran
2011-09-26 18:00:56 +01:00
parent 124188a0a1
commit 53b5a61ab3
6 changed files with 149 additions and 161 deletions

View File

@@ -191,11 +191,13 @@ interface Database<T> {
long getFreeSpace() throws DbException;
/**
* Returns the group that contains the given message.
* Returns the parent of the given group message, or null if either the
* message has no parent, or the parent is absent from the database, or the
* parent belongs to a different group.
* <p>
* Locking: messages read.
*/
GroupId getGroup(T txn, MessageId m) throws DbException;
MessageId getGroupMessageParent(T txn, MessageId m) throws DbException;
/**
* Returns the IDs of any batches sent to the given contact that should now
@@ -248,14 +250,6 @@ interface Database<T> {
*/
Collection<MessageId> getOldMessages(T txn, int size) throws DbException;
/**
* Returns the parent of the given message, or null if the message has
* no parent.
* <p>
* Locking: messages read.
*/
MessageId getParent(T txn, MessageId m) throws DbException;
/**
* Returns the user's rating for the given author.
* <p>

View File

@@ -278,19 +278,13 @@ DatabaseCleaner.Callback {
*/
private int updateAncestorSendability(T txn, MessageId m, boolean increment)
throws DbException {
GroupId group = db.getGroup(txn, m);
int affected = 0;
boolean changed = true;
while(changed) {
// Stop if the message has no parent
MessageId parent = db.getParent(txn, m);
// Stop if the message has no parent, or the parent isn't in the
// database, or the parent belongs to a different group
MessageId parent = db.getGroupMessageParent(txn, m);
if(parent == null) break;
// Stop if the parent isn't in the database
if(!db.containsMessage(txn, parent)) break;
// Stop if the message and the parent aren't in the same group
assert group != null;
GroupId parentGroup = db.getGroup(txn, parent);
if(!group.equals(parentGroup)) break;
// Increment or decrement the parent's sendability
int parentSendability = db.getSendability(txn, parent);
if(increment) {
@@ -306,7 +300,6 @@ DatabaseCleaner.Callback {
db.setSendability(txn, parent, parentSendability);
// Move on to the parent's parent
m = parent;
group = parentGroup;
}
return affected;
}

View File

@@ -1098,20 +1098,27 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public MessageId getParent(Connection txn, MessageId m) throws DbException {
public MessageId getGroupMessageParent(Connection txn, MessageId m)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT parentId FROM messages WHERE messageId = ?";
String sql = "SELECT m1.parentId FROM messages AS m1"
+ " JOIN messages AS m2"
+ " ON m1.parentId = m2.messageId"
+ " AND m1.groupId = m2.groupId"
+ " WHERE m1.messageId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getBytes());
rs = ps.executeQuery();
if(!rs.next()) throw new DbStateException();
byte[] parent = rs.getBytes(1);
if(rs.next()) throw new DbStateException();
MessageId parent = null;
if(rs.next()) {
parent = new MessageId(rs.getBytes(1));
if(rs.next()) throw new DbStateException();
}
rs.close();
ps.close();
return parent == null ? null : new MessageId(parent);
return parent;
} catch(SQLException e) {
tryToClose(rs);
tryToClose(ps);