Use null instead of MessageId.NONE and AuthorId.NONE, as for other

optional fields.
This commit is contained in:
akwizgran
2011-09-13 14:04:23 +01:00
parent 70b1487140
commit 1d25b5a92e
13 changed files with 51 additions and 47 deletions

View File

@@ -8,9 +8,6 @@ import net.sf.briar.api.serial.Writer;
/** Type-safe wrapper for a byte array that uniquely identifies an author. */
public class AuthorId extends UniqueId {
/** Used to indicate that a message is anonymous. */
public static final AuthorId NONE = new AuthorId(new byte[UniqueId.LENGTH]);
public AuthorId(byte[] id) {
super(id);
}

View File

@@ -8,10 +8,6 @@ import net.sf.briar.api.serial.Writer;
/** Type-safe wrapper for a byte array that uniquely identifies a message. */
public class MessageId extends UniqueId {
/** Used to indicate that the first message in a thread has no parent. */
public static final MessageId NONE =
new MessageId(new byte[UniqueId.LENGTH]);
public MessageId(byte[] id) {
super(id);
}

View File

@@ -241,7 +241,8 @@ interface Database<T> {
Collection<MessageId> getOldMessages(T txn, int size) throws DbException;
/**
* Returns the parent of the given message.
* Returns the parent of the given message, or null if the message has
* no parent.
* <p>
* Locking: messages read.
*/

View File

@@ -213,7 +213,7 @@ DatabaseCleaner.Callback {
boolean changed = true;
while(changed) {
MessageId parent = db.getParent(txn, m);
if(parent.equals(MessageId.NONE)) break;
if(parent == null) break;
if(!db.containsMessage(txn, parent)) break;
if(!db.getGroup(txn, m).equals(db.getGroup(txn, parent))) break;
Integer parentSendability = db.getSendability(txn, parent);

View File

@@ -8,6 +8,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@@ -51,9 +52,9 @@ abstract class JdbcDatabase implements Database<Connection> {
private static final String CREATE_MESSAGES =
"CREATE TABLE messages"
+ " (messageId HASH NOT NULL,"
+ " parentId HASH NOT NULL,"
+ " parentId HASH,"
+ " groupId HASH NOT NULL,"
+ " authorId HASH NOT NULL,"
+ " authorId HASH,"
+ " timestamp BIGINT NOT NULL,"
+ " size INT NOT NULL,"
+ " raw BLOB NOT NULL,"
@@ -536,9 +537,11 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getId().getBytes());
ps.setBytes(2, m.getParent().getBytes());
if(m.getParent() == null) ps.setNull(2, Types.BINARY);
else ps.setBytes(2, m.getParent().getBytes());
ps.setBytes(3, m.getGroup().getBytes());
ps.setBytes(4, m.getAuthor().getBytes());
if(m.getAuthor() == null) ps.setNull(4, Types.BINARY);
else ps.setBytes(4, m.getAuthor().getBytes());
ps.setLong(5, m.getTimestamp());
ps.setInt(6, m.getSize());
byte[] raw = m.getBytes();

View File

@@ -66,7 +66,8 @@ class MessageEncoderImpl implements MessageEncoder {
Writer w = writerFactory.createWriter(out);
// Write the message
w.writeUserDefinedTag(Types.MESSAGE);
parent.writeTo(w);
if(parent == null) w.writeNull();
else parent.writeTo(w);
group.writeTo(w);
if(author == null) w.writeNull();
else author.writeTo(w);
@@ -99,7 +100,7 @@ class MessageEncoderImpl implements MessageEncoder {
messageDigest.reset();
messageDigest.update(raw);
MessageId id = new MessageId(messageDigest.digest());
AuthorId authorId = author == null ? AuthorId.NONE : author.getId();
AuthorId authorId = author == null ? null : author.getId();
return new MessageImpl(id, parent, group.getId(), authorId, timestamp,
raw);
}

View File

@@ -48,20 +48,28 @@ class MessageReader implements ObjectReader<Message> {
r.addConsumer(counting);
// Read the initial tag
r.readUserDefinedId(Types.MESSAGE);
// Read the parent's message ID
r.addObjectReader(Types.MESSAGE_ID, messageIdReader);
MessageId parent = r.readUserDefined(Types.MESSAGE_ID, MessageId.class);
r.removeObjectReader(Types.MESSAGE_ID);
// Read the parent's message ID, if there is one
MessageId parent = null;
if(r.hasNull()) {
r.readNull();
} else {
r.addObjectReader(Types.MESSAGE_ID, messageIdReader);
parent = r.readUserDefined(Types.MESSAGE_ID, MessageId.class);
r.removeObjectReader(Types.MESSAGE_ID);
}
// Read the group
r.addObjectReader(Types.GROUP, groupReader);
Group group = r.readUserDefined(Types.GROUP, Group.class);
r.removeObjectReader(Types.GROUP);
// Read the author, if there is one
r.addObjectReader(Types.AUTHOR, authorReader);
Author author = null;
if(r.hasNull()) r.readNull();
else author = r.readUserDefined(Types.AUTHOR, Author.class);
r.removeObjectReader(Types.AUTHOR);
if(r.hasNull()) {
r.readNull();
} else {
r.addObjectReader(Types.AUTHOR, authorReader);
author = r.readUserDefined(Types.AUTHOR, Author.class);
r.removeObjectReader(Types.AUTHOR);
}
// Read the timestamp
long timestamp = r.readInt64();
if(timestamp < 0L) throw new FormatException();
@@ -109,7 +117,7 @@ class MessageReader implements ObjectReader<Message> {
messageDigest.reset();
messageDigest.update(raw);
MessageId id = new MessageId(messageDigest.digest());
AuthorId authorId = author == null ? AuthorId.NONE : author.getId();
AuthorId authorId = author == null ? null : author.getId();
return new MessageImpl(id, parent, group.getId(), authorId, timestamp,
raw);
}

View File

@@ -109,13 +109,13 @@ public class FileReadWriteTest extends TestCase {
authorKeyPair.getPublic().getEncoded());
// Create two messages to each group: one anonymous, one pseudonymous
MessageEncoder messageEncoder = i.getInstance(MessageEncoder.class);
message = messageEncoder.encodeMessage(MessageId.NONE, group,
message = messageEncoder.encodeMessage(null, group,
messageBody.getBytes("UTF-8"));
message1 = messageEncoder.encodeMessage(MessageId.NONE, group1,
message1 = messageEncoder.encodeMessage(null, group1,
groupKeyPair.getPrivate(), messageBody.getBytes("UTF-8"));
message2 = messageEncoder.encodeMessage(MessageId.NONE, group, author,
message2 = messageEncoder.encodeMessage(null, group, author,
authorKeyPair.getPrivate(), messageBody.getBytes("UTF-8"));
message3 = messageEncoder.encodeMessage(MessageId.NONE, group1,
message3 = messageEncoder.encodeMessage(null, group1,
groupKeyPair.getPrivate(), author, authorKeyPair.getPrivate(),
messageBody.getBytes("UTF-8"));
transports = Collections.singletonMap("foo",

View File

@@ -6,7 +6,6 @@ import static net.sf.briar.api.db.DatabaseComponent.MIN_FREE_SPACE;
import java.util.Collections;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.db.DatabaseCleaner.Callback;
import org.jmock.Expectations;
@@ -109,7 +108,7 @@ public abstract class DatabaseComponentImplTest extends DatabaseComponentTest {
oneOf(database).getSendability(txn, messageId);
will(returnValue(1));
oneOf(database).getParent(txn, messageId);
will(returnValue(MessageId.NONE));
will(returnValue(null));
oneOf(database).removeMessage(txn, messageId);
oneOf(database).commitTransaction(txn);
oneOf(database).getFreeSpace();

View File

@@ -68,8 +68,8 @@ public abstract class DatabaseComponentTest extends TestCase {
timestamp = System.currentTimeMillis();
size = 1234;
raw = new byte[size];
message = new TestMessage(messageId, MessageId.NONE, groupId, authorId,
timestamp, raw);
message =
new TestMessage(messageId, null, groupId, authorId, timestamp, raw);
group = new TestGroup(groupId, "The really exciting group", null);
transports = Collections.singletonMap("foo",
Collections.singletonMap("bar", "baz"));
@@ -198,7 +198,7 @@ public abstract class DatabaseComponentTest extends TestCase {
oneOf(database).setSendability(txn, messageId, 1);
// Backward inclusion stops when the message has no parent
oneOf(database).getParent(txn, messageId);
will(returnValue(MessageId.NONE));
will(returnValue(null));
oneOf(database).commitTransaction(txn);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);
@@ -349,7 +349,7 @@ public abstract class DatabaseComponentTest extends TestCase {
will(returnValue(0));
oneOf(database).setSendability(txn, parentId, 1);
oneOf(database).getParent(txn, parentId);
will(returnValue(MessageId.NONE));
will(returnValue(null));
oneOf(database).commitTransaction(txn);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);
@@ -462,7 +462,7 @@ public abstract class DatabaseComponentTest extends TestCase {
oneOf(database).setSendability(txn, messageId, 3);
// The sendability of the message's ancestors should be updated
oneOf(database).getParent(txn, messageId);
will(returnValue(MessageId.NONE));
will(returnValue(null));
oneOf(database).commitTransaction(txn);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);
@@ -988,7 +988,7 @@ public abstract class DatabaseComponentTest extends TestCase {
will(returnValue(1));
oneOf(database).setSendability(txn, messageId, 2);
oneOf(database).getParent(txn, messageId);
will(returnValue(MessageId.NONE));
will(returnValue(null));
// The batch needs to be acknowledged
oneOf(batch).getId();
will(returnValue(batchId));

View File

@@ -84,8 +84,8 @@ public class H2DatabaseTest extends TestCase {
size = 1234;
raw = new byte[size];
random.nextBytes(raw);
message = new TestMessage(messageId, MessageId.NONE, groupId, authorId,
timestamp, raw);
message =
new TestMessage(messageId, null, groupId, authorId, timestamp, raw);
group = groupFactory.createGroup(groupId, "Group name", null);
transports = Collections.singletonMap("foo",
Collections.singletonMap("bar", "baz"));
@@ -675,8 +675,8 @@ public class H2DatabaseTest extends TestCase {
public void testGetMessagesByAuthor() throws DbException {
AuthorId authorId1 = new AuthorId(TestUtils.getRandomId());
MessageId messageId1 = new MessageId(TestUtils.getRandomId());
Message message1 = new TestMessage(messageId1, MessageId.NONE, groupId,
authorId1, timestamp, raw);
Message message1 = new TestMessage(messageId1, null, groupId, authorId1,
timestamp, raw);
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
@@ -744,8 +744,8 @@ public class H2DatabaseTest extends TestCase {
@Test
public void testGetOldMessages() throws DbException {
MessageId messageId1 = new MessageId(TestUtils.getRandomId());
Message message1 = new TestMessage(messageId1, MessageId.NONE, groupId,
authorId, timestamp + 1000, raw);
Message message1 = new TestMessage(messageId1, null, groupId, authorId,
timestamp + 1000, raw);
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
@@ -775,8 +775,8 @@ public class H2DatabaseTest extends TestCase {
public void testGetFreeSpace() throws Exception {
byte[] largeBody = new byte[ONE_MEGABYTE];
for(int i = 0; i < largeBody.length; i++) largeBody[i] = (byte) i;
Message message1 = new TestMessage(messageId, MessageId.NONE, groupId,
authorId, timestamp, largeBody);
Message message1 = new TestMessage(messageId, null, groupId, authorId,
timestamp, largeBody);
Database<Connection> db = open(false);
// Sanity check: there should be enough space on disk for this test

View File

@@ -15,7 +15,6 @@ import net.sf.briar.api.protocol.Group;
import net.sf.briar.api.protocol.GroupFactory;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageEncoder;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Offer;
import net.sf.briar.api.protocol.ProtocolReader;
import net.sf.briar.api.protocol.ProtocolReaderFactory;
@@ -61,7 +60,7 @@ public class ProtocolReadWriteTest extends TestCase {
GroupFactory groupFactory = i.getInstance(GroupFactory.class);
group = groupFactory.createGroup("Unrestricted group", null);
MessageEncoder messageEncoder = i.getInstance(MessageEncoder.class);
message = messageEncoder.encodeMessage(MessageId.NONE, group,
message = messageEncoder.encodeMessage(null, group,
messageBody.getBytes("UTF-8"));
bitSet = new BitSet();
bitSet.set(3);

View File

@@ -87,7 +87,7 @@ public class ConstantsTest extends TestCase {
PrivateKey groupPrivate = crypto.generateKeyPair().getPrivate();
PrivateKey authorPrivate = crypto.generateKeyPair().getPrivate();
byte[] body = new byte[Message.MAX_BODY_LENGTH];
Message message = messageEncoder.encodeMessage(MessageId.NONE, group,
Message message = messageEncoder.encodeMessage(null, group,
groupPrivate, author, authorPrivate, body);
// Add the message to a batch
ByteArrayOutputStream out = new ByteArrayOutputStream(