mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 05:09:53 +01:00
Avoid repeated author status lookups
This commit is contained in:
@@ -44,9 +44,11 @@ import java.util.Arrays;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@@ -550,8 +552,6 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
|
|||||||
new BdfEntry(KEY_TYPE, COMMENT.getInt())
|
new BdfEntry(KEY_TYPE, COMMENT.getInt())
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO this could be optimized by looking up author status once (#625)
|
|
||||||
|
|
||||||
Collection<BlogPostHeader> headers = new ArrayList<BlogPostHeader>();
|
Collection<BlogPostHeader> headers = new ArrayList<BlogPostHeader>();
|
||||||
Transaction txn = db.startTransaction(true);
|
Transaction txn = db.startTransaction(true);
|
||||||
try {
|
try {
|
||||||
@@ -564,10 +564,26 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
|
|||||||
metadata1.size() + metadata2.size());
|
metadata1.size() + metadata2.size());
|
||||||
metadata.putAll(metadata1);
|
metadata.putAll(metadata1);
|
||||||
metadata.putAll(metadata2);
|
metadata.putAll(metadata2);
|
||||||
|
// get all authors we need to get the status for
|
||||||
|
Set<AuthorId> authors = new HashSet<AuthorId>();
|
||||||
|
for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) {
|
||||||
|
authors.add(new AuthorId(
|
||||||
|
entry.getValue().getDictionary(KEY_AUTHOR)
|
||||||
|
.getRaw(KEY_AUTHOR_ID)));
|
||||||
|
}
|
||||||
|
// get statuses for all authors
|
||||||
|
Map<AuthorId, Status> authorStatuses =
|
||||||
|
new HashMap<AuthorId, Status>();
|
||||||
|
for (AuthorId authorId : authors) {
|
||||||
|
authorStatuses.put(authorId,
|
||||||
|
identityManager.getAuthorStatus(txn, authorId));
|
||||||
|
}
|
||||||
|
// get post headers
|
||||||
for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) {
|
for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) {
|
||||||
BdfDictionary meta = entry.getValue();
|
BdfDictionary meta = entry.getValue();
|
||||||
BlogPostHeader h =
|
BlogPostHeader h =
|
||||||
getPostHeaderFromMetadata(txn, g, entry.getKey(), meta);
|
getPostHeaderFromMetadata(txn, g, entry.getKey(), meta,
|
||||||
|
authorStatuses);
|
||||||
headers.add(h);
|
headers.add(h);
|
||||||
}
|
}
|
||||||
txn.setComplete();
|
txn.setComplete();
|
||||||
@@ -611,6 +627,14 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
|
|||||||
private BlogPostHeader getPostHeaderFromMetadata(Transaction txn,
|
private BlogPostHeader getPostHeaderFromMetadata(Transaction txn,
|
||||||
GroupId groupId, MessageId id, BdfDictionary meta)
|
GroupId groupId, MessageId id, BdfDictionary meta)
|
||||||
throws DbException, FormatException {
|
throws DbException, FormatException {
|
||||||
|
return getPostHeaderFromMetadata(txn, groupId, id, meta,
|
||||||
|
Collections.<AuthorId, Status>emptyMap());
|
||||||
|
}
|
||||||
|
|
||||||
|
private BlogPostHeader getPostHeaderFromMetadata(Transaction txn,
|
||||||
|
GroupId groupId, MessageId id, BdfDictionary meta,
|
||||||
|
Map<AuthorId, Status> authorStatuses)
|
||||||
|
throws DbException, FormatException {
|
||||||
|
|
||||||
MessageType type = getMessageType(meta);
|
MessageType type = getMessageType(meta);
|
||||||
|
|
||||||
@@ -622,7 +646,12 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
|
|||||||
String name = d.getString(KEY_AUTHOR_NAME);
|
String name = d.getString(KEY_AUTHOR_NAME);
|
||||||
byte[] publicKey = d.getRaw(KEY_PUBLIC_KEY);
|
byte[] publicKey = d.getRaw(KEY_PUBLIC_KEY);
|
||||||
Author author = new Author(authorId, name, publicKey);
|
Author author = new Author(authorId, name, publicKey);
|
||||||
Status authorStatus = identityManager.getAuthorStatus(txn, authorId);
|
Status authorStatus;
|
||||||
|
if (authorStatuses.containsKey(authorId)) {
|
||||||
|
authorStatus = authorStatuses.get(authorId);
|
||||||
|
} else {
|
||||||
|
authorStatus = identityManager.getAuthorStatus(txn, authorId);
|
||||||
|
}
|
||||||
|
|
||||||
boolean read = meta.getBoolean(KEY_READ, false);
|
boolean read = meta.getBoolean(KEY_READ, false);
|
||||||
|
|
||||||
|
|||||||
@@ -29,9 +29,12 @@ import org.briarproject.util.StringUtils;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
@@ -192,23 +195,38 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
|
|||||||
public Collection<ForumPostHeader> getPostHeaders(GroupId g)
|
public Collection<ForumPostHeader> getPostHeaders(GroupId g)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
|
|
||||||
Map<MessageId, BdfDictionary> metadata;
|
Collection<ForumPostHeader> headers = new ArrayList<ForumPostHeader>();
|
||||||
|
Transaction txn = db.startTransaction(true);
|
||||||
try {
|
try {
|
||||||
metadata = clientHelper.getMessageMetadataAsDictionary(g);
|
Map<MessageId, BdfDictionary> metadata =
|
||||||
|
clientHelper.getMessageMetadataAsDictionary(txn, g);
|
||||||
|
// get all authors we need to get the status for
|
||||||
|
Set<AuthorId> authors = new HashSet<AuthorId>();
|
||||||
|
for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) {
|
||||||
|
BdfDictionary d =
|
||||||
|
entry.getValue().getDictionary(KEY_AUTHOR, null);
|
||||||
|
if (d != null)
|
||||||
|
authors.add(new AuthorId(d.getRaw(KEY_ID)));
|
||||||
|
}
|
||||||
|
// get statuses for all authors
|
||||||
|
Map<AuthorId, Status> statuses = new HashMap<AuthorId, Status>();
|
||||||
|
for (AuthorId id : authors) {
|
||||||
|
statuses.put(id, identityManager.getAuthorStatus(txn, id));
|
||||||
|
}
|
||||||
|
// Parse the metadata
|
||||||
|
for (Entry<MessageId, BdfDictionary> entry : metadata
|
||||||
|
.entrySet()) {
|
||||||
|
BdfDictionary meta = entry.getValue();
|
||||||
|
headers.add(getForumPostHeader(txn, entry.getKey(), meta,
|
||||||
|
statuses));
|
||||||
|
}
|
||||||
|
txn.setComplete();
|
||||||
|
return headers;
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
|
} finally {
|
||||||
|
db.endTransaction(txn);
|
||||||
}
|
}
|
||||||
// Parse the metadata
|
|
||||||
Collection<ForumPostHeader> headers = new ArrayList<ForumPostHeader>();
|
|
||||||
for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) {
|
|
||||||
try {
|
|
||||||
BdfDictionary meta = entry.getValue();
|
|
||||||
headers.add(getForumPostHeader(entry.getKey(), meta));
|
|
||||||
} catch (FormatException e) {
|
|
||||||
throw new DbException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return headers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -236,10 +254,17 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
|
|||||||
|
|
||||||
private ForumPostHeader getForumPostHeader(Transaction txn, MessageId id,
|
private ForumPostHeader getForumPostHeader(Transaction txn, MessageId id,
|
||||||
BdfDictionary meta) throws DbException, FormatException {
|
BdfDictionary meta) throws DbException, FormatException {
|
||||||
|
return getForumPostHeader(txn, id, meta,
|
||||||
|
Collections.<AuthorId, Status>emptyMap());
|
||||||
|
}
|
||||||
|
|
||||||
|
private ForumPostHeader getForumPostHeader(Transaction txn, MessageId id,
|
||||||
|
BdfDictionary meta, Map<AuthorId, Status> statuses)
|
||||||
|
throws DbException, FormatException {
|
||||||
|
|
||||||
long timestamp = meta.getLong(KEY_TIMESTAMP);
|
long timestamp = meta.getLong(KEY_TIMESTAMP);
|
||||||
Author author = null;
|
Author author = null;
|
||||||
Status authorStatus = ANONYMOUS;
|
Status status = ANONYMOUS;
|
||||||
MessageId parentId = null;
|
MessageId parentId = null;
|
||||||
if (meta.containsKey(KEY_PARENT))
|
if (meta.containsKey(KEY_PARENT))
|
||||||
parentId = new MessageId(meta.getRaw(KEY_PARENT));
|
parentId = new MessageId(meta.getRaw(KEY_PARENT));
|
||||||
@@ -249,23 +274,16 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
|
|||||||
String name = d1.getString(KEY_NAME);
|
String name = d1.getString(KEY_NAME);
|
||||||
byte[] publicKey = d1.getRaw(KEY_PUBLIC_NAME);
|
byte[] publicKey = d1.getRaw(KEY_PUBLIC_NAME);
|
||||||
author = new Author(authorId, name, publicKey);
|
author = new Author(authorId, name, publicKey);
|
||||||
if (txn == null) {
|
if (statuses.containsKey(authorId)) {
|
||||||
authorStatus = identityManager.getAuthorStatus(author.getId());
|
status = statuses.get(authorId);
|
||||||
} else {
|
} else {
|
||||||
authorStatus =
|
status = identityManager.getAuthorStatus(txn, author.getId());
|
||||||
identityManager.getAuthorStatus(txn, author.getId());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
boolean read = meta.getBoolean(KEY_READ);
|
boolean read = meta.getBoolean(KEY_READ);
|
||||||
|
|
||||||
return new ForumPostHeader(id, parentId, timestamp, author,
|
return new ForumPostHeader(id, parentId, timestamp, author, status,
|
||||||
authorStatus, read);
|
read);
|
||||||
}
|
|
||||||
|
|
||||||
private ForumPostHeader getForumPostHeader(MessageId id,
|
|
||||||
BdfDictionary meta) throws DbException, FormatException {
|
|
||||||
|
|
||||||
return getForumPostHeader(null, id, meta);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user