Moved transactions out of database component.

This commit is contained in:
akwizgran
2016-02-11 13:35:46 +00:00
parent ef2b2b9710
commit de8cc50fb4
24 changed files with 1828 additions and 1451 deletions

View File

@@ -5,6 +5,7 @@ import com.google.inject.Inject;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.NoSuchLocalAuthorException;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.LocalAuthorAddedEvent;
import org.briarproject.api.event.LocalAuthorRemovedEvent;
@@ -47,19 +48,31 @@ class IdentityManagerImpl implements IdentityManager, Service {
public boolean start() {
// Finish adding/removing any partly added/removed pseudonyms
try {
for (LocalAuthor a : db.getLocalAuthors()) {
if (a.getStatus().equals(ADDING)) {
for (AddIdentityHook hook : addHooks)
hook.addingIdentity(a);
db.setLocalAuthorStatus(a.getId(), ACTIVE);
eventBus.broadcast(new LocalAuthorAddedEvent(a.getId()));
} else if (a.getStatus().equals(REMOVING)) {
for (RemoveIdentityHook hook : removeHooks)
hook.removingIdentity(a);
db.removeLocalAuthor(a.getId());
eventBus.broadcast(new LocalAuthorRemovedEvent(a.getId()));
List<AuthorId> added = new ArrayList<AuthorId>();
List<AuthorId> removed = new ArrayList<AuthorId>();
Transaction txn = db.startTransaction();
try {
for (LocalAuthor a : db.getLocalAuthors(txn)) {
if (a.getStatus().equals(ADDING)) {
for (AddIdentityHook hook : addHooks)
hook.addingIdentity(txn, a);
db.setLocalAuthorStatus(txn, a.getId(), ACTIVE);
added.add(a.getId());
} else if (a.getStatus().equals(REMOVING)) {
for (RemoveIdentityHook hook : removeHooks)
hook.removingIdentity(txn, a);
db.removeLocalAuthor(txn, a.getId());
removed.add(a.getId());
}
}
txn.setComplete();
} finally {
db.endTransaction(txn);
}
for (AuthorId a : added)
eventBus.broadcast(new LocalAuthorAddedEvent(a));
for (AuthorId a : removed)
eventBus.broadcast(new LocalAuthorRemovedEvent(a));
return true;
} catch (DbException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
@@ -84,22 +97,43 @@ class IdentityManagerImpl implements IdentityManager, Service {
@Override
public void addLocalAuthor(LocalAuthor localAuthor) throws DbException {
db.addLocalAuthor(localAuthor);
for (AddIdentityHook hook : addHooks) hook.addingIdentity(localAuthor);
db.setLocalAuthorStatus(localAuthor.getId(), ACTIVE);
Transaction txn = db.startTransaction();
try {
db.addLocalAuthor(txn, localAuthor);
for (AddIdentityHook hook : addHooks)
hook.addingIdentity(txn, localAuthor);
db.setLocalAuthorStatus(txn, localAuthor.getId(), ACTIVE);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
eventBus.broadcast(new LocalAuthorAddedEvent(localAuthor.getId()));
}
@Override
public LocalAuthor getLocalAuthor(AuthorId a) throws DbException {
LocalAuthor author = db.getLocalAuthor(a);
LocalAuthor author;
Transaction txn = db.startTransaction();
try {
author = db.getLocalAuthor(txn, a);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
if (author.getStatus().equals(ACTIVE)) return author;
throw new NoSuchLocalAuthorException();
}
@Override
public Collection<LocalAuthor> getLocalAuthors() throws DbException {
Collection<LocalAuthor> authors = db.getLocalAuthors();
Collection<LocalAuthor> authors;
Transaction txn = db.startTransaction();
try {
authors = db.getLocalAuthors(txn);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
// Filter out any pseudonyms that are being added or removed
List<LocalAuthor> active = new ArrayList<LocalAuthor>(authors.size());
for (LocalAuthor a : authors)
@@ -109,11 +143,17 @@ class IdentityManagerImpl implements IdentityManager, Service {
@Override
public void removeLocalAuthor(AuthorId a) throws DbException {
LocalAuthor localAuthor = db.getLocalAuthor(a);
db.setLocalAuthorStatus(a, REMOVING);
for (RemoveIdentityHook hook : removeHooks)
hook.removingIdentity(localAuthor);
db.removeLocalAuthor(a);
Transaction txn = db.startTransaction();
try {
LocalAuthor localAuthor = db.getLocalAuthor(txn, a);
db.setLocalAuthorStatus(txn, a, REMOVING);
for (RemoveIdentityHook hook : removeHooks)
hook.removingIdentity(txn, localAuthor);
db.removeLocalAuthor(txn, a);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
eventBus.broadcast(new LocalAuthorRemovedEvent(a));
}
}