Use transactional DB interface for ForumManagerImpl.

This commit is contained in:
akwizgran
2018-10-04 15:59:10 +01:00
parent 3bcc532b4b
commit 7359b6942a

View File

@@ -94,14 +94,7 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
@Override @Override
public Forum addForum(String name) throws DbException { public Forum addForum(String name) throws DbException {
Forum f = forumFactory.createForum(name); Forum f = forumFactory.createForum(name);
db.transaction(false, txn -> db.addGroup(txn, f.getGroup()));
Transaction txn = db.startTransaction(false);
try {
db.addGroup(txn, f.getGroup());
db.commitTransaction(txn);
} finally {
db.endTransaction(txn);
}
return f; return f;
} }
@@ -112,15 +105,11 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
@Override @Override
public void removeForum(Forum f) throws DbException { public void removeForum(Forum f) throws DbException {
Transaction txn = db.startTransaction(false); db.transaction(false, txn -> {
try {
for (RemoveForumHook hook : removeHooks) for (RemoveForumHook hook : removeHooks)
hook.removingForum(txn, f); hook.removingForum(txn, f);
db.removeGroup(txn, f.getGroup()); db.removeGroup(txn, f.getGroup());
db.commitTransaction(txn); });
} finally {
db.endTransaction(txn);
}
} }
@Override @Override
@@ -131,45 +120,35 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
p = forumPostFactory.createPost(groupId, timestamp, parentId, p = forumPostFactory.createPost(groupId, timestamp, parentId,
author, body); author, body);
} catch (GeneralSecurityException | FormatException e) { } catch (GeneralSecurityException | FormatException e) {
throw new RuntimeException(e); throw new AssertionError(e);
} }
return p; return p;
} }
@Override @Override
public ForumPostHeader addLocalPost(ForumPost p) throws DbException { public ForumPostHeader addLocalPost(ForumPost p) throws DbException {
Transaction txn = db.startTransaction(false); db.transaction(false, txn -> {
try { try {
BdfDictionary meta = new BdfDictionary(); BdfDictionary meta = new BdfDictionary();
meta.put(KEY_TIMESTAMP, p.getMessage().getTimestamp()); meta.put(KEY_TIMESTAMP, p.getMessage().getTimestamp());
if (p.getParent() != null) meta.put(KEY_PARENT, p.getParent()); if (p.getParent() != null) meta.put(KEY_PARENT, p.getParent());
Author a = p.getAuthor(); Author a = p.getAuthor();
meta.put(KEY_AUTHOR, clientHelper.toList(a)); meta.put(KEY_AUTHOR, clientHelper.toList(a));
meta.put(KEY_LOCAL, true); meta.put(KEY_LOCAL, true);
meta.put(MSG_KEY_READ, true); meta.put(MSG_KEY_READ, true);
clientHelper.addLocalMessage(txn, p.getMessage(), meta, true); clientHelper.addLocalMessage(txn, p.getMessage(), meta, true);
messageTracker.trackOutgoingMessage(txn, p.getMessage()); messageTracker.trackOutgoingMessage(txn, p.getMessage());
db.commitTransaction(txn); } catch (FormatException e) {
} catch (FormatException e) { throw new AssertionError(e);
throw new RuntimeException(e); }
} finally { });
db.endTransaction(txn);
}
return new ForumPostHeader(p.getMessage().getId(), p.getParent(), return new ForumPostHeader(p.getMessage().getId(), p.getParent(),
p.getMessage().getTimestamp(), p.getAuthor(), OURSELVES, true); p.getMessage().getTimestamp(), p.getAuthor(), OURSELVES, true);
} }
@Override @Override
public Forum getForum(GroupId g) throws DbException { public Forum getForum(GroupId g) throws DbException {
Forum forum; return db.transactionWithResult(true, txn -> getForum(txn, g));
Transaction txn = db.startTransaction(true);
try {
forum = getForum(txn, g);
db.commitTransaction(txn);
} finally {
db.endTransaction(txn);
}
return forum;
} }
@Override @Override
@@ -184,15 +163,9 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
@Override @Override
public Collection<Forum> getForums() throws DbException { public Collection<Forum> getForums() throws DbException {
Collection<Group> groups = db.transactionWithResult(true, txn ->
db.getGroups(txn, CLIENT_ID, MAJOR_VERSION));
try { try {
Collection<Group> groups;
Transaction txn = db.startTransaction(true);
try {
groups = db.getGroups(txn, CLIENT_ID, MAJOR_VERSION);
db.commitTransaction(txn);
} finally {
db.endTransaction(txn);
}
List<Forum> forums = new ArrayList<>(); List<Forum> forums = new ArrayList<>();
for (Group g : groups) forums.add(parseForum(g)); for (Group g : groups) forums.add(parseForum(g));
return forums; return forums;
@@ -218,36 +191,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 {
Collection<ForumPostHeader> headers = new ArrayList<>();
Transaction txn = db.startTransaction(true);
try { try {
Map<MessageId, BdfDictionary> metadata = return db.throwingTransactionWithResult(true, txn -> {
clientHelper.getMessageMetadataAsDictionary(txn, g); Collection<ForumPostHeader> headers = new ArrayList<>();
// get all authors we need to get the status for Map<MessageId, BdfDictionary> metadata =
Set<AuthorId> authors = new HashSet<>(); clientHelper.getMessageMetadataAsDictionary(txn, g);
for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) { // get all authors we need to get the status for
BdfList authorList = entry.getValue().getList(KEY_AUTHOR); Set<AuthorId> authors = new HashSet<>();
Author a = clientHelper.parseAndValidateAuthor(authorList); for (Entry<MessageId, BdfDictionary> entry :
authors.add(a.getId()); metadata.entrySet()) {
} BdfList authorList = entry.getValue().getList(KEY_AUTHOR);
// get statuses for all authors Author a = clientHelper.parseAndValidateAuthor(authorList);
Map<AuthorId, Status> statuses = new HashMap<>(); authors.add(a.getId());
for (AuthorId id : authors) { }
statuses.put(id, identityManager.getAuthorStatus(txn, id)); // get statuses for all authors
} Map<AuthorId, Status> statuses = new HashMap<>();
// Parse the metadata for (AuthorId id : authors) {
for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) { statuses.put(id, identityManager.getAuthorStatus(txn, id));
BdfDictionary meta = entry.getValue(); }
headers.add(getForumPostHeader(txn, entry.getKey(), meta, // Parse the metadata
statuses)); for (Entry<MessageId, BdfDictionary> entry :
} metadata.entrySet()) {
db.commitTransaction(txn); BdfDictionary meta = entry.getValue();
return headers; headers.add(getForumPostHeader(txn, entry.getKey(), meta,
statuses));
}
return headers;
});
} catch (FormatException e) { } catch (FormatException e) {
throw new DbException(e); throw new DbException(e);
} finally {
db.endTransaction(txn);
} }
} }