mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 03:39:05 +01:00
Use null instead of MessageId.NONE and AuthorId.NONE, as for other
optional fields.
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user