Allow blog posts to be loaded within one transaction

This commit is contained in:
Torsten Grote
2021-01-11 16:33:07 -03:00
parent ae923e5777
commit ad20e5230a
2 changed files with 41 additions and 6 deletions

View File

@@ -10,6 +10,7 @@ import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.MessageId;
import java.util.Collection;
import java.util.List;
import javax.annotation.Nullable;
@@ -98,6 +99,11 @@ public interface BlogManager {
*/
Collection<Blog> getBlogs() throws DbException;
/**
* Returns the group IDs of all blogs to which the user subscribes.
*/
Collection<GroupId> getBlogIds(Transaction txn) throws DbException;
/**
* Returns the header of the blog post with the given ID.
*/
@@ -108,11 +114,22 @@ public interface BlogManager {
*/
String getPostText(MessageId m) throws DbException;
/**
* Returns the text of the blog post with the given ID.
*/
String getPostText(Transaction txn, MessageId m) throws DbException;
/**
* Returns the headers of all posts in the given blog.
*/
Collection<BlogPostHeader> getPostHeaders(GroupId g) throws DbException;
/**
* Returns the headers of all posts in the given blog.
*/
List<BlogPostHeader> getPostHeaders(Transaction txn, GroupId g)
throws DbException;
/**
* Marks a blog post as read or unread.
*/

View File

@@ -445,6 +445,14 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
}
}
@Override
public Collection<GroupId> getBlogIds(Transaction txn) throws DbException {
List<GroupId> groupIds = new ArrayList<>();
Collection<Group> groups = db.getGroups(txn, CLIENT_ID, MAJOR_VERSION);
for (Group g : groups) groupIds.add(g.getId());
return groupIds;
}
@Override
public BlogPostHeader getPostHeader(GroupId g, MessageId m)
throws DbException {
@@ -471,6 +479,15 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
}
}
@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 message) throws FormatException {
MessageType type = MessageType.valueOf(message.getLong(0).intValue());
if (type == POST) {
@@ -488,7 +505,12 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
@Override
public Collection<BlogPostHeader> getPostHeaders(GroupId g)
throws DbException {
return db.transactionWithResult(true, txn -> getPostHeaders(txn, g));
}
@Override
public List<BlogPostHeader> getPostHeaders(Transaction txn, GroupId g)
throws DbException {
// Query for posts and comments only
BdfDictionary query1 = BdfDictionary.of(
new BdfEntry(KEY_TYPE, POST.getInt())
@@ -497,8 +519,7 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
new BdfEntry(KEY_TYPE, COMMENT.getInt())
);
Collection<BlogPostHeader> headers = new ArrayList<>();
Transaction txn = db.startTransaction(true);
List<BlogPostHeader> headers = new ArrayList<>();
try {
Map<MessageId, BdfDictionary> metadata1 =
clientHelper.getMessageMetadataAsDictionary(txn, g, query1);
@@ -528,13 +549,10 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
entry.getKey(), meta, authorInfos);
headers.add(h);
}
db.commitTransaction(txn);
return headers;
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
return headers;
}
@Override