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

@@ -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);
}