Perform thread list core access within a single transaction

This commit is contained in:
Torsten Grote
2021-01-06 14:14:48 -03:00
parent 4663e727eb
commit 36a9174781
4 changed files with 78 additions and 31 deletions

View File

@@ -11,6 +11,7 @@ import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.api.client.MessageTracker.GroupCount; import org.briarproject.briar.api.client.MessageTracker.GroupCount;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@@ -84,11 +85,22 @@ public interface ForumManager {
*/ */
String getPostText(MessageId m) throws DbException; String getPostText(MessageId m) throws DbException;
/**
* Returns the text of the forum post with the given ID.
*/
String getPostText(Transaction txn, MessageId m) throws DbException;
/** /**
* Returns the headers of all posts in the given forum. * Returns the headers of all posts in the given forum.
*/ */
Collection<ForumPostHeader> getPostHeaders(GroupId g) throws DbException; Collection<ForumPostHeader> getPostHeaders(GroupId g) throws DbException;
/**
* Returns the headers of all posts in the given forum.
*/
List<ForumPostHeader> getPostHeaders(Transaction txn, GroupId g)
throws DbException;
/** /**
* Registers a hook to be called whenever a forum is removed. * Registers a hook to be called whenever a forum is removed.
*/ */

View File

@@ -12,6 +12,7 @@ import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.api.client.MessageTracker.GroupCount; import org.briarproject.briar.api.client.MessageTracker.GroupCount;
import java.util.Collection; import java.util.Collection;
import java.util.List;
@NotNullByDefault @NotNullByDefault
public interface PrivateGroupManager { public interface PrivateGroupManager {
@@ -107,11 +108,22 @@ public interface PrivateGroupManager {
*/ */
String getMessageText(MessageId m) throws DbException; String getMessageText(MessageId m) throws DbException;
/**
* Returns the text of the private group message with the given ID.
*/
String getMessageText(Transaction txn, MessageId m) throws DbException;
/** /**
* Returns the headers of all messages in the given private group. * Returns the headers of all messages in the given private group.
*/ */
Collection<GroupMessageHeader> getHeaders(GroupId g) throws DbException; Collection<GroupMessageHeader> getHeaders(GroupId g) throws DbException;
/**
* Returns the headers of all messages in the given private group.
*/
List<GroupMessageHeader> getHeaders(Transaction txn, GroupId g)
throws DbException;
/** /**
* Returns all members of the given private group. * Returns all members of the given private group.
*/ */

View File

@@ -192,6 +192,15 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
} }
} }
@Override
public String getPostText(Transaction txn, MessageId m) throws DbException {
try {
return getPostText(clientHelper.getMessageAsList(txn, m));
} catch (FormatException e) {
throw new DbException(e);
}
}
private String getPostText(BdfList body) throws FormatException { private String getPostText(BdfList body) throws FormatException {
// Parent ID, author, text, signature // Parent ID, author, text, signature
return body.getString(2); return body.getString(2);
@@ -200,33 +209,35 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
@Override @Override
public Collection<ForumPostHeader> getPostHeaders(GroupId g) public Collection<ForumPostHeader> getPostHeaders(GroupId g)
throws DbException { throws DbException {
return db.transactionWithResult(true, txn -> getPostHeaders(txn, g));
}
@Override
public List<ForumPostHeader> getPostHeaders(Transaction txn, GroupId g)
throws DbException {
try { try {
return db.transactionWithResult(true, txn -> { List<ForumPostHeader> headers = new ArrayList<>();
Collection<ForumPostHeader> headers = new ArrayList<>(); Map<MessageId, BdfDictionary> metadata =
Map<MessageId, BdfDictionary> metadata = clientHelper.getMessageMetadataAsDictionary(txn, g);
clientHelper.getMessageMetadataAsDictionary(txn, g); // get all authors we need to get the info for
// get all authors we need to get the info for Set<AuthorId> authors = new HashSet<>();
Set<AuthorId> authors = new HashSet<>(); for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) {
for (Entry<MessageId, BdfDictionary> entry : BdfList authorList = entry.getValue().getList(KEY_AUTHOR);
metadata.entrySet()) { Author a = clientHelper.parseAndValidateAuthor(authorList);
BdfList authorList = entry.getValue().getList(KEY_AUTHOR); authors.add(a.getId());
Author a = clientHelper.parseAndValidateAuthor(authorList); }
authors.add(a.getId()); // get information for all authors
} Map<AuthorId, AuthorInfo> authorInfos = new HashMap<>();
// get information for all authors for (AuthorId id : authors) {
Map<AuthorId, AuthorInfo> authorInfos = new HashMap<>(); authorInfos.put(id, authorManager.getAuthorInfo(txn, id));
for (AuthorId id : authors) { }
authorInfos.put(id, authorManager.getAuthorInfo(txn, id)); // Parse the metadata
} for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) {
// Parse the metadata BdfDictionary meta = entry.getValue();
for (Entry<MessageId, BdfDictionary> entry : headers.add(getForumPostHeader(txn, entry.getKey(), meta,
metadata.entrySet()) { authorInfos));
BdfDictionary meta = entry.getValue(); }
headers.add(getForumPostHeader(txn, entry.getKey(), meta, return headers;
authorInfos));
}
return headers;
});
} catch (FormatException e) { } catch (FormatException e) {
throw new DbException(e); throw new DbException(e);
} }

View File

@@ -315,6 +315,16 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
} }
} }
@Override
public String getMessageText(Transaction txn, MessageId m)
throws DbException {
try {
return getMessageText(clientHelper.getMessageAsList(txn, m));
} catch (FormatException e) {
throw new DbException(e);
}
}
private String getMessageText(BdfList body) throws FormatException { private String getMessageText(BdfList body) throws FormatException {
// Message type (0), member (1), parent ID (2), previous message ID (3), // Message type (0), member (1), parent ID (2), previous message ID (3),
// text (4), signature (5) // text (4), signature (5)
@@ -324,8 +334,13 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
@Override @Override
public Collection<GroupMessageHeader> getHeaders(GroupId g) public Collection<GroupMessageHeader> getHeaders(GroupId g)
throws DbException { throws DbException {
Collection<GroupMessageHeader> headers = new ArrayList<>(); return db.transactionWithResult(true, txn -> getHeaders(txn, g));
Transaction txn = db.startTransaction(true); }
@Override
public List<GroupMessageHeader> getHeaders(Transaction txn, GroupId g)
throws DbException {
List<GroupMessageHeader> headers = new ArrayList<>();
try { try {
Map<MessageId, BdfDictionary> metadata = Map<MessageId, BdfDictionary> metadata =
clientHelper.getMessageMetadataAsDictionary(txn, g); clientHelper.getMessageMetadataAsDictionary(txn, g);
@@ -354,12 +369,9 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
meta, authorInfos)); meta, authorInfos));
} }
} }
db.commitTransaction(txn);
return headers; return headers;
} catch (FormatException e) { } catch (FormatException e) {
throw new DbException(e); throw new DbException(e);
} finally {
db.endTransaction(txn);
} }
} }