Retrieve messages from the database in raw form to avoid creating unnecessary short-lived objects. Added timestamps to headers.

This commit is contained in:
akwizgran
2011-07-14 12:01:35 +01:00
parent d4382fd232
commit 836d30f6df
32 changed files with 202 additions and 197 deletions

View File

@@ -158,11 +158,11 @@ interface Database<T> {
Set<BatchId> getLostBatches(T txn, ContactId c) throws DbException;
/**
* Returns the message identified by the given ID.
* Returns the message identified by the given ID, in raw format.
* <p>
* Locking: messages read.
*/
Message getMessage(T txn, MessageId m) throws DbException;
byte[] getMessage(T txn, MessageId m) throws DbException;
/**
* Returns the IDs of all messages signed by the given author.

View File

@@ -4,7 +4,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import com.google.inject.Inject;
public class DatabaseCleanerImpl implements DatabaseCleaner, Runnable {
class DatabaseCleanerImpl implements DatabaseCleaner, Runnable {
private final Callback db;
private final int msBetweenSweeps;

View File

@@ -12,7 +12,6 @@ import java.util.logging.Logger;
import net.sf.briar.api.crypto.Password;
import net.sf.briar.api.db.DatabasePassword;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.protocol.MessageFactory;
import com.google.inject.Inject;
@@ -28,9 +27,8 @@ class H2Database extends JdbcDatabase {
private final long maxSize;
@Inject
H2Database(File dir, MessageFactory messageFactory,
@DatabasePassword Password password, long maxSize) {
super(messageFactory, "BINARY(32)", "BIGINT");
H2Database(File dir, @DatabasePassword Password password, long maxSize) {
super("BINARY(32)", "BIGINT");
home = new File(dir, "db");
this.password = password;
url = "jdbc:h2:split:" + home.getPath()

View File

@@ -27,7 +27,6 @@ import net.sf.briar.api.protocol.AuthorId;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageFactory;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.util.FileUtils;
@@ -157,7 +156,6 @@ abstract class JdbcDatabase implements Database<Connection> {
private static final Logger LOG =
Logger.getLogger(JdbcDatabase.class.getName());
private final MessageFactory messageFactory;
// Different database libraries use different names for certain types
private final String hashType, bigIntType;
private final LinkedList<Connection> connections =
@@ -168,9 +166,7 @@ abstract class JdbcDatabase implements Database<Connection> {
protected abstract Connection createConnection() throws SQLException;
JdbcDatabase(MessageFactory messageFactory, String hashType,
String bigIntType) {
this.messageFactory = messageFactory;
JdbcDatabase(String hashType, String bigIntType) {
this.hashType = hashType;
this.bigIntType = bigIntType;
}
@@ -683,32 +679,25 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public Message getMessage(Connection txn, MessageId m) throws DbException {
public byte[] getMessage(Connection txn, MessageId m) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql =
"SELECT parentId, groupId, authorId, timestamp, size, raw"
+ " FROM messages WHERE messageId = ?";
String sql = "SELECT size, raw FROM messages WHERE messageId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getBytes());
rs = ps.executeQuery();
boolean found = rs.next();
assert found;
MessageId parent = new MessageId(rs.getBytes(1));
GroupId group = new GroupId(rs.getBytes(2));
AuthorId author = new AuthorId(rs.getBytes(3));
long timestamp = rs.getLong(4);
int size = rs.getInt(5);
Blob b = rs.getBlob(6);
int size = rs.getInt(1);
Blob b = rs.getBlob(2);
byte[] raw = b.getBytes(1, size);
assert raw.length == size;
boolean more = rs.next();
assert !more;
rs.close();
ps.close();
return messageFactory.createMessage(m, parent, group, author,
timestamp, raw);
return raw;
} catch(SQLException e) {
tryToClose(rs);
tryToClose(ps);

View File

@@ -25,6 +25,8 @@ import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Header;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.serial.Raw;
import net.sf.briar.api.serial.RawByteArray;
import com.google.inject.Inject;
@@ -282,8 +284,8 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
try {
Txn txn = db.startTransaction();
try {
long capacity = Math.min(b.getRemainingCapacity(),
Batch.MAX_SIZE);
long capacity =
Math.min(b.getRemainingCapacity(), Batch.MAX_SIZE);
Iterator<MessageId> it =
db.getSendableMessages(txn, c, capacity).iterator();
if(!it.hasNext()) {
@@ -291,12 +293,12 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
return false; // No more messages to send
}
sent = new HashSet<MessageId>();
List<Message> messages = new ArrayList<Message>();
List<Raw> messages = new ArrayList<Raw>();
while(it.hasNext()) {
MessageId m = it.next();
Message message = db.getMessage(txn, m);
bytesSent += message.getSize();
messages.add(message);
byte[] message = db.getMessage(txn, m);
bytesSent += message.length;
messages.add(new RawByteArray(message));
sent.add(m);
}
batchId = b.addBatch(messages);

View File

@@ -25,6 +25,8 @@ import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Header;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.serial.Raw;
import net.sf.briar.api.serial.RawByteArray;
import com.google.inject.Inject;
@@ -212,8 +214,8 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
synchronized(messageStatusLock) {
Txn txn = db.startTransaction();
try {
long capacity = Math.min(b.getRemainingCapacity(),
Batch.MAX_SIZE);
long capacity =
Math.min(b.getRemainingCapacity(), Batch.MAX_SIZE);
Iterator<MessageId> it =
db.getSendableMessages(txn, c, capacity).iterator();
if(!it.hasNext()) {
@@ -221,13 +223,13 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
return false; // No more messages to send
}
Set<MessageId> sent = new HashSet<MessageId>();
List<Message> messages = new ArrayList<Message>();
List<Raw> messages = new ArrayList<Raw>();
int bytesSent = 0;
while(it.hasNext()) {
MessageId m = it.next();
Message message = db.getMessage(txn, m);
bytesSent += message.getSize();
messages.add(message);
byte[] message = db.getMessage(txn, m);
bytesSent += message.length;
messages.add(new RawByteArray(message));
sent.add(m);
}
BatchId batchId = b.addBatch(messages);