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 java.util.Collection;
import java.util.List;
import javax.annotation.Nullable;
@@ -84,11 +85,22 @@ public interface ForumManager {
*/
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.
*/
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.
*/

View File

@@ -12,6 +12,7 @@ import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
import java.util.Collection;
import java.util.List;
@NotNullByDefault
public interface PrivateGroupManager {
@@ -107,11 +108,22 @@ public interface PrivateGroupManager {
*/
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.
*/
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.
*/

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 {
// Parent ID, author, text, signature
return body.getString(2);
@@ -200,33 +209,35 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
@Override
public Collection<ForumPostHeader> getPostHeaders(GroupId g)
throws DbException {
return db.transactionWithResult(true, txn -> getPostHeaders(txn, g));
}
@Override
public List<ForumPostHeader> getPostHeaders(Transaction txn, GroupId g)
throws DbException {
try {
return db.transactionWithResult(true, txn -> {
Collection<ForumPostHeader> headers = new ArrayList<>();
Map<MessageId, BdfDictionary> metadata =
clientHelper.getMessageMetadataAsDictionary(txn, g);
// get all authors we need to get the info for
Set<AuthorId> authors = new HashSet<>();
for (Entry<MessageId, BdfDictionary> entry :
metadata.entrySet()) {
BdfList authorList = entry.getValue().getList(KEY_AUTHOR);
Author a = clientHelper.parseAndValidateAuthor(authorList);
authors.add(a.getId());
}
// get information for all authors
Map<AuthorId, AuthorInfo> authorInfos = new HashMap<>();
for (AuthorId id : authors) {
authorInfos.put(id, authorManager.getAuthorInfo(txn, id));
}
// Parse the metadata
for (Entry<MessageId, BdfDictionary> entry :
metadata.entrySet()) {
BdfDictionary meta = entry.getValue();
headers.add(getForumPostHeader(txn, entry.getKey(), meta,
authorInfos));
}
return headers;
});
List<ForumPostHeader> headers = new ArrayList<>();
Map<MessageId, BdfDictionary> metadata =
clientHelper.getMessageMetadataAsDictionary(txn, g);
// get all authors we need to get the info for
Set<AuthorId> authors = new HashSet<>();
for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) {
BdfList authorList = entry.getValue().getList(KEY_AUTHOR);
Author a = clientHelper.parseAndValidateAuthor(authorList);
authors.add(a.getId());
}
// get information for all authors
Map<AuthorId, AuthorInfo> authorInfos = new HashMap<>();
for (AuthorId id : authors) {
authorInfos.put(id, authorManager.getAuthorInfo(txn, id));
}
// Parse the metadata
for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) {
BdfDictionary meta = entry.getValue();
headers.add(getForumPostHeader(txn, entry.getKey(), meta,
authorInfos));
}
return headers;
} catch (FormatException 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 {
// Message type (0), member (1), parent ID (2), previous message ID (3),
// text (4), signature (5)
@@ -324,8 +334,13 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
@Override
public Collection<GroupMessageHeader> getHeaders(GroupId g)
throws DbException {
Collection<GroupMessageHeader> headers = new ArrayList<>();
Transaction txn = db.startTransaction(true);
return db.transactionWithResult(true, txn -> getHeaders(txn, g));
}
@Override
public List<GroupMessageHeader> getHeaders(Transaction txn, GroupId g)
throws DbException {
List<GroupMessageHeader> headers = new ArrayList<>();
try {
Map<MessageId, BdfDictionary> metadata =
clientHelper.getMessageMetadataAsDictionary(txn, g);
@@ -354,12 +369,9 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
meta, authorInfos));
}
}
db.commitTransaction(txn);
return headers;
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
}