mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 06:09:55 +01:00
Moved MessageHeader into DB component and added read/starred flags.
This commit is contained in:
@@ -10,13 +10,13 @@ import net.sf.briar.api.TransportConfig;
|
||||
import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.api.TransportProperties;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.db.MessageHeader;
|
||||
import net.sf.briar.api.db.Status;
|
||||
import net.sf.briar.api.protocol.AuthorId;
|
||||
import net.sf.briar.api.protocol.BatchId;
|
||||
import net.sf.briar.api.protocol.Group;
|
||||
import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageHeader;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.transport.ConnectionWindow;
|
||||
|
||||
@@ -257,7 +257,7 @@ interface Database<T> {
|
||||
/**
|
||||
* Returns the headers of all messages in the given group.
|
||||
* <p>
|
||||
* Locking: message read.
|
||||
* Locking: message read, messageFlag read.
|
||||
*/
|
||||
Collection<MessageHeader> getMessageHeaders(T txn, GroupId g)
|
||||
throws DbException;
|
||||
|
||||
@@ -27,6 +27,7 @@ import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.api.TransportProperties;
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.db.MessageHeader;
|
||||
import net.sf.briar.api.db.NoSuchContactException;
|
||||
import net.sf.briar.api.db.Status;
|
||||
import net.sf.briar.api.db.event.BatchReceivedEvent;
|
||||
@@ -45,7 +46,6 @@ import net.sf.briar.api.protocol.BatchId;
|
||||
import net.sf.briar.api.protocol.Group;
|
||||
import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageHeader;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.Offer;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
@@ -789,15 +789,20 @@ DatabaseCleaner.Callback {
|
||||
throws DbException {
|
||||
messageLock.readLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
messageFlagLock.readLock().lock();
|
||||
try {
|
||||
Collection<MessageHeader> headers =
|
||||
db.getMessageHeaders(txn, g);
|
||||
db.commitTransaction(txn);
|
||||
return headers;
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
Collection<MessageHeader> headers =
|
||||
db.getMessageHeaders(txn, g);
|
||||
db.commitTransaction(txn);
|
||||
return headers;
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
messageFlagLock.readLock().unlock();
|
||||
}
|
||||
} finally {
|
||||
messageLock.readLock().unlock();
|
||||
|
||||
@@ -9,7 +9,6 @@ import net.sf.briar.api.db.DatabaseDirectory;
|
||||
import net.sf.briar.api.db.DatabaseMaxSize;
|
||||
import net.sf.briar.api.db.DatabasePassword;
|
||||
import net.sf.briar.api.protocol.GroupFactory;
|
||||
import net.sf.briar.api.protocol.MessageHeaderFactory;
|
||||
import net.sf.briar.api.transport.ConnectionWindowFactory;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
@@ -27,10 +26,9 @@ public class DatabaseModule extends AbstractModule {
|
||||
Database<Connection> getDatabase(@DatabaseDirectory File dir,
|
||||
@DatabasePassword Password password, @DatabaseMaxSize long maxSize,
|
||||
ConnectionWindowFactory connectionWindowFactory,
|
||||
GroupFactory groupFactory,
|
||||
MessageHeaderFactory messageHeaderFactory) {
|
||||
GroupFactory groupFactory) {
|
||||
return new H2Database(dir, password, maxSize, connectionWindowFactory,
|
||||
groupFactory, messageHeaderFactory);
|
||||
groupFactory);
|
||||
}
|
||||
|
||||
@Provides @Singleton
|
||||
|
||||
@@ -16,7 +16,6 @@ import net.sf.briar.api.db.DatabaseMaxSize;
|
||||
import net.sf.briar.api.db.DatabasePassword;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.protocol.GroupFactory;
|
||||
import net.sf.briar.api.protocol.MessageHeaderFactory;
|
||||
import net.sf.briar.api.transport.ConnectionWindowFactory;
|
||||
|
||||
import org.apache.commons.io.FileSystemUtils;
|
||||
@@ -38,9 +37,8 @@ class H2Database extends JdbcDatabase {
|
||||
H2Database(@DatabaseDirectory File dir, @DatabasePassword Password password,
|
||||
@DatabaseMaxSize long maxSize,
|
||||
ConnectionWindowFactory connectionWindowFactory,
|
||||
GroupFactory groupFactory,
|
||||
MessageHeaderFactory messageHeaderFactory) {
|
||||
super(connectionWindowFactory, groupFactory, messageHeaderFactory,
|
||||
GroupFactory groupFactory) {
|
||||
super(connectionWindowFactory, groupFactory,
|
||||
"BINARY(32)", "BINARY", "INT NOT NULL AUTO_INCREMENT");
|
||||
home = new File(dir, "db");
|
||||
this.password = password;
|
||||
|
||||
@@ -23,6 +23,7 @@ import net.sf.briar.api.TransportConfig;
|
||||
import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.api.TransportProperties;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.db.MessageHeader;
|
||||
import net.sf.briar.api.db.Status;
|
||||
import net.sf.briar.api.protocol.AuthorId;
|
||||
import net.sf.briar.api.protocol.BatchId;
|
||||
@@ -30,8 +31,6 @@ import net.sf.briar.api.protocol.Group;
|
||||
import net.sf.briar.api.protocol.GroupFactory;
|
||||
import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageHeader;
|
||||
import net.sf.briar.api.protocol.MessageHeaderFactory;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.transport.ConnectionWindow;
|
||||
import net.sf.briar.api.transport.ConnectionWindowFactory;
|
||||
@@ -241,7 +240,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
private final String hashType, binaryType, counterType;
|
||||
private final ConnectionWindowFactory connectionWindowFactory;
|
||||
private final GroupFactory groupFactory;
|
||||
private final MessageHeaderFactory messageHeaderFactory;
|
||||
|
||||
private final LinkedList<Connection> connections =
|
||||
new LinkedList<Connection>(); // Locking: self
|
||||
@@ -252,12 +250,10 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
protected abstract Connection createConnection() throws SQLException;
|
||||
|
||||
JdbcDatabase(ConnectionWindowFactory connectionWindowFactory,
|
||||
GroupFactory groupFactory,
|
||||
MessageHeaderFactory messageHeaderFactory,
|
||||
String hashType, String binaryType, String counterType) {
|
||||
GroupFactory groupFactory, String hashType, String binaryType,
|
||||
String counterType) {
|
||||
this.connectionWindowFactory = connectionWindowFactory;
|
||||
this.groupFactory = groupFactory;
|
||||
this.messageHeaderFactory = messageHeaderFactory;
|
||||
this.hashType = hashType;
|
||||
this.binaryType = binaryType;
|
||||
this.counterType = counterType;
|
||||
@@ -1115,8 +1111,10 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT messageId, parentId, authorId, subject,"
|
||||
+ " timestamp FROM messages"
|
||||
String sql = "SELECT messages.messageId, parentId, authorId,"
|
||||
+ " subject, timestamp, read, starred"
|
||||
+ " FROM messages LEFT OUTER JOIN flags"
|
||||
+ " ON messages.messageId = flags.messageId"
|
||||
+ " WHERE groupId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setBytes(1, g.getBytes());
|
||||
@@ -1129,8 +1127,10 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
AuthorId author = new AuthorId(rs.getBytes(3));
|
||||
String subject = rs.getString(4);
|
||||
long timestamp = rs.getLong(5);
|
||||
headers.add(messageHeaderFactory.createMessageHeader(id, parent,
|
||||
g, author, subject, timestamp));
|
||||
boolean read = rs.getBoolean(6); // False if absent
|
||||
boolean starred = rs.getBoolean(7); // False if absent
|
||||
headers.add(new MessageHeaderImpl(id, parent, g, author,
|
||||
subject, timestamp, read, starred));
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package net.sf.briar.protocol;
|
||||
package net.sf.briar.db;
|
||||
|
||||
import net.sf.briar.api.db.MessageHeader;
|
||||
import net.sf.briar.api.protocol.AuthorId;
|
||||
import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.MessageHeader;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
|
||||
class MessageHeaderImpl implements MessageHeader {
|
||||
@@ -12,15 +12,19 @@ class MessageHeaderImpl implements MessageHeader {
|
||||
private final AuthorId author;
|
||||
private final String subject;
|
||||
private final long timestamp;
|
||||
private final boolean read, starred;
|
||||
|
||||
MessageHeaderImpl(MessageId id, MessageId parent, GroupId group,
|
||||
AuthorId author, String subject, long timestamp) {
|
||||
AuthorId author, String subject, long timestamp, boolean read,
|
||||
boolean starred) {
|
||||
this.id = id;
|
||||
this.parent = parent;
|
||||
this.group = group;
|
||||
this.author = author;
|
||||
this.subject = subject;
|
||||
this.timestamp = timestamp;
|
||||
this.read = read;
|
||||
this.starred = starred;
|
||||
}
|
||||
|
||||
public MessageId getId() {
|
||||
@@ -46,4 +50,12 @@ class MessageHeaderImpl implements MessageHeader {
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public boolean getRead() {
|
||||
return read;
|
||||
}
|
||||
|
||||
public boolean getStarred() {
|
||||
return starred;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package net.sf.briar.protocol;
|
||||
|
||||
import net.sf.briar.api.protocol.AuthorId;
|
||||
import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.MessageHeader;
|
||||
import net.sf.briar.api.protocol.MessageHeaderFactory;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
|
||||
class MessageHeaderFactoryImpl implements MessageHeaderFactory {
|
||||
|
||||
public MessageHeader createMessageHeader(MessageId id, MessageId parent,
|
||||
GroupId group, AuthorId author, String subject, long timestamp) {
|
||||
return new MessageHeaderImpl(id, parent, group, author, subject,
|
||||
timestamp);
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,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.MessageHeaderFactory;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.Offer;
|
||||
import net.sf.briar.api.protocol.ProtocolReaderFactory;
|
||||
@@ -31,7 +30,6 @@ public class ProtocolModule extends AbstractModule {
|
||||
bind(BatchFactory.class).to(BatchFactoryImpl.class);
|
||||
bind(GroupFactory.class).to(GroupFactoryImpl.class);
|
||||
bind(MessageEncoder.class).to(MessageEncoderImpl.class);
|
||||
bind(MessageHeaderFactory.class).to(MessageHeaderFactoryImpl.class);
|
||||
bind(OfferFactory.class).to(OfferFactoryImpl.class);
|
||||
bind(ProtocolReaderFactory.class).to(ProtocolReaderFactoryImpl.class);
|
||||
bind(RequestFactory.class).to(RequestFactoryImpl.class);
|
||||
|
||||
Reference in New Issue
Block a user