From 7aeb6029a690d35d195684d1107f321353dd09a6 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Mon, 19 Sep 2011 16:42:27 +0100
Subject: [PATCH] Assertions and comments.

---
 .../sf/briar/db/DatabaseComponentImpl.java    | 14 +++++++---
 .../briar/db/DatabaseComponentImplTest.java   |  2 ++
 .../sf/briar/db/DatabaseComponentTest.java    | 26 ++++++++++++-------
 3 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/components/net/sf/briar/db/DatabaseComponentImpl.java b/components/net/sf/briar/db/DatabaseComponentImpl.java
index f864b88978..e08425e7de 100644
--- a/components/net/sf/briar/db/DatabaseComponentImpl.java
+++ b/components/net/sf/briar/db/DatabaseComponentImpl.java
@@ -271,15 +271,21 @@ 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);
 			if(parent == null) break;
+			// Stop if the parent isn't in the database
 			if(!db.containsMessage(txn, parent)) break;
-			if(!db.getGroup(txn, m).equals(db.getGroup(txn, parent))) break;
-			Integer parentSendability = db.getSendability(txn, parent);
-			assert parentSendability != null;
+			// 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) {
 				parentSendability++;
 				changed = parentSendability == 1;
@@ -291,7 +297,9 @@ DatabaseCleaner.Callback {
 				if(changed) affected++;
 			}
 			db.setSendability(txn, parent, parentSendability);
+			// Move on to the parent's parent
 			m = parent;
+			group = parentGroup;
 		}
 		return affected;
 	}
diff --git a/test/net/sf/briar/db/DatabaseComponentImplTest.java b/test/net/sf/briar/db/DatabaseComponentImplTest.java
index ea17f97171..3c351f3a11 100644
--- a/test/net/sf/briar/db/DatabaseComponentImplTest.java
+++ b/test/net/sf/briar/db/DatabaseComponentImplTest.java
@@ -105,6 +105,8 @@ public class DatabaseComponentImplTest extends DatabaseComponentTest {
 			will(returnValue(Collections.singleton(messageId)));
 			oneOf(database).getSendability(txn, messageId);
 			will(returnValue(1));
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(null));
 			oneOf(database).removeMessage(txn, messageId);
diff --git a/test/net/sf/briar/db/DatabaseComponentTest.java b/test/net/sf/briar/db/DatabaseComponentTest.java
index 18425720cb..886b2d7da4 100644
--- a/test/net/sf/briar/db/DatabaseComponentTest.java
+++ b/test/net/sf/briar/db/DatabaseComponentTest.java
@@ -199,6 +199,8 @@ public abstract class DatabaseComponentTest extends TestCase {
 			will(returnValue(0));
 			oneOf(database).setSendability(txn, messageId, 1);
 			// Backward inclusion stops when the message has no parent
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(null));
 			oneOf(database).commitTransaction(txn);
@@ -228,6 +230,8 @@ public abstract class DatabaseComponentTest extends TestCase {
 			will(returnValue(0));
 			oneOf(database).setSendability(txn, messageId, 1);
 			// The parent exists
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(parentId));
 			// The parent isn't in the DB
@@ -262,13 +266,13 @@ public abstract class DatabaseComponentTest extends TestCase {
 			will(returnValue(0));
 			oneOf(database).setSendability(txn, messageId, 1);
 			// The parent exists and is in the database
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(parentId));
 			oneOf(database).containsMessage(txn, parentId);
 			will(returnValue(true));
 			// The parent is in a different group
-			oneOf(database).getGroup(txn, messageId);
-			will(returnValue(groupId));
 			oneOf(database).getGroup(txn, parentId);
 			will(returnValue(groupId1));
 			oneOf(database).commitTransaction(txn);
@@ -298,13 +302,13 @@ public abstract class DatabaseComponentTest extends TestCase {
 			will(returnValue(0));
 			oneOf(database).setSendability(txn, messageId, 1);
 			// The parent exists and is in the database
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(parentId));
 			oneOf(database).containsMessage(txn, parentId);
 			will(returnValue(true));
 			// The parent is a private message
-			oneOf(database).getGroup(txn, messageId);
-			will(returnValue(groupId));
 			oneOf(database).getGroup(txn, parentId);
 			will(returnValue(null));
 			oneOf(database).commitTransaction(txn);
@@ -335,12 +339,12 @@ public abstract class DatabaseComponentTest extends TestCase {
 			will(returnValue(0));
 			oneOf(database).setSendability(txn, messageId, 1);
 			// The parent exists, is in the DB, and is in the same group
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(parentId));
 			oneOf(database).containsMessage(txn, parentId);
 			will(returnValue(true));
-			oneOf(database).getGroup(txn, messageId);
-			will(returnValue(groupId));
 			oneOf(database).getGroup(txn, parentId);
 			will(returnValue(groupId));
 			// The parent is already sendable
@@ -375,12 +379,12 @@ public abstract class DatabaseComponentTest extends TestCase {
 			will(returnValue(0));
 			oneOf(database).setSendability(txn, messageId, 1);
 			// The parent exists, is in the DB, and is in the same group
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(parentId));
 			oneOf(database).containsMessage(txn, parentId);
 			will(returnValue(true));
-			oneOf(database).getGroup(txn, messageId);
-			will(returnValue(groupId));
 			oneOf(database).getGroup(txn, parentId);
 			will(returnValue(groupId));
 			// The parent is not already sendable
@@ -500,6 +504,8 @@ public abstract class DatabaseComponentTest extends TestCase {
 			will(returnValue(2));
 			oneOf(database).setSendability(txn, messageId, 3);
 			// The sendability of the message's ancestors should be updated
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(null));
 			oneOf(database).commitTransaction(txn);
@@ -1134,12 +1140,14 @@ public abstract class DatabaseComponentTest extends TestCase {
 			// Set the status to NEW for all other contacts (there are none)
 			oneOf(database).getContacts(txn);
 			will(returnValue(Collections.singletonList(contactId)));
-			// Calculate the sendability -ancestors are updated
+			// Calculate the sendability - ancestors are updated
 			oneOf(database).getRating(txn, authorId);
 			will(returnValue(Rating.GOOD));
 			oneOf(database).getNumberOfSendableChildren(txn, messageId);
 			will(returnValue(1));
 			oneOf(database).setSendability(txn, messageId, 2);
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(null));
 			// The batch needs to be acknowledged
-- 
GitLab