mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 21:59:54 +01:00
added a cache to the IdentityManager, changed its signature, modified when and where the author is stored
made the author creation single-threaded again in the LifecycleManager, removed redundant code
This commit is contained in:
@@ -10,8 +10,7 @@ import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
@@ -22,104 +21,62 @@ import static org.briarproject.api.identity.Author.Status.VERIFIED;
|
||||
|
||||
class IdentityManagerImpl implements IdentityManager {
|
||||
private final DatabaseComponent db;
|
||||
private final List<AddIdentityHook> addHooks;
|
||||
private final List<RemoveIdentityHook> removeHooks;
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(IdentityManagerImpl.class.getName());
|
||||
|
||||
// Make sure that this variable is immutable
|
||||
private volatile LocalAuthor cachedAuthor;
|
||||
|
||||
@Inject
|
||||
IdentityManagerImpl(DatabaseComponent db) {
|
||||
this.db = db;
|
||||
addHooks = new CopyOnWriteArrayList<AddIdentityHook>();
|
||||
removeHooks = new CopyOnWriteArrayList<RemoveIdentityHook>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerAddIdentityHook(AddIdentityHook hook) {
|
||||
addHooks.add(hook);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerRemoveIdentityHook(RemoveIdentityHook hook) {
|
||||
removeHooks.add(hook);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLocalAuthor(LocalAuthor localAuthor) throws DbException {
|
||||
public void registerLocalAuthor(LocalAuthor localAuthor) throws DbException {
|
||||
Transaction txn = db.startTransaction(false);
|
||||
try {
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
for (AddIdentityHook hook : addHooks)
|
||||
hook.addingIdentity(txn, localAuthor);
|
||||
txn.setComplete();
|
||||
cachedAuthor = localAuthor;
|
||||
LOG.info("Local Author created");
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalAuthor getLocalAuthor(AuthorId a) throws DbException {
|
||||
LocalAuthor author;
|
||||
Transaction txn = db.startTransaction(true);
|
||||
try {
|
||||
author = getLocalAuthor(txn, a);
|
||||
txn.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
return author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalAuthor getLocalAuthor(Transaction txn, AuthorId a)
|
||||
throws DbException {
|
||||
return db.getLocalAuthor(txn, a);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalAuthor getLocalAuthor() throws DbException {
|
||||
return getLocalAuthors().iterator().next();
|
||||
if (cachedAuthor == null) {
|
||||
Transaction txn = db.startTransaction(true);
|
||||
try {
|
||||
cachedAuthor = loadLocalAuthor(txn);
|
||||
LOG.info("Author loaded from db");
|
||||
txn.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
}
|
||||
return cachedAuthor;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public LocalAuthor getLocalAuthor(Transaction txn) throws DbException {
|
||||
return getLocalAuthors(txn).iterator().next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<LocalAuthor> getLocalAuthors() throws DbException {
|
||||
Collection<LocalAuthor> authors;
|
||||
Transaction txn = db.startTransaction(true);
|
||||
try {
|
||||
authors = getLocalAuthors(txn);
|
||||
txn.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
if (cachedAuthor == null) {
|
||||
cachedAuthor = loadLocalAuthor(txn);
|
||||
}
|
||||
return authors;
|
||||
return cachedAuthor;
|
||||
}
|
||||
|
||||
private Collection<LocalAuthor> getLocalAuthors(Transaction txn)
|
||||
throws DbException {
|
||||
|
||||
return db.getLocalAuthors(txn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeLocalAuthor(AuthorId a) throws DbException {
|
||||
Transaction txn = db.startTransaction(false);
|
||||
try {
|
||||
LocalAuthor localAuthor = db.getLocalAuthor(txn, a);
|
||||
for (RemoveIdentityHook hook : removeHooks)
|
||||
hook.removingIdentity(txn, localAuthor);
|
||||
db.removeLocalAuthor(txn, a);
|
||||
txn.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
private LocalAuthor loadLocalAuthor(Transaction txn) throws DbException{
|
||||
return db.getLocalAuthors(txn).iterator().next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Status getAuthorStatus(AuthorId authorId) throws DbException {
|
||||
Transaction txn = db.startTransaction(false);
|
||||
Transaction txn = db.startTransaction(true);
|
||||
try {
|
||||
return getAuthorStatus(txn, authorId);
|
||||
} finally {
|
||||
@@ -131,10 +88,8 @@ class IdentityManagerImpl implements IdentityManager {
|
||||
public Status getAuthorStatus(Transaction txn, AuthorId authorId)
|
||||
throws DbException {
|
||||
|
||||
// Compare to the IDs of the user's identities
|
||||
for (LocalAuthor a : db.getLocalAuthors(txn)) {
|
||||
if (a.getId().equals(authorId)) return OURSELVES;
|
||||
}
|
||||
// Compare to the IDs of the user's identity
|
||||
if (getLocalAuthor(txn).getId().equals(authorId)) return OURSELVES;
|
||||
|
||||
Collection<Contact> contacts = db.getContactsByAuthorId(txn, authorId);
|
||||
if (contacts.isEmpty()) return UNKNOWN;
|
||||
|
||||
@@ -31,7 +31,7 @@ public class IdentityModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
IdentityManager provideIdendityModule(DatabaseComponent db) {
|
||||
IdentityManager provideIdentityModule(DatabaseComponent db) {
|
||||
return new IdentityManagerImpl(db);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user