mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 19:29:06 +01:00
Add transactional DB interface.
This commit is contained in:
@@ -9,7 +9,9 @@ import org.briarproject.bramble.api.contact.event.ContactVerifiedEvent;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.db.ContactExistsException;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
import org.briarproject.bramble.api.db.DbCallable;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.DbRunnable;
|
||||
import org.briarproject.bramble.api.db.Metadata;
|
||||
import org.briarproject.bramble.api.db.MigrationListener;
|
||||
import org.briarproject.bramble.api.db.NoSuchContactException;
|
||||
@@ -166,6 +168,43 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
for (Event e : transaction.getEvents()) eventBus.broadcast(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transaction(boolean readOnly, DbRunnable<DbException> task)
|
||||
throws DbException {
|
||||
throwingTransaction(readOnly, task);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> R transactionWithResult(boolean readOnly,
|
||||
DbCallable<R, DbException> task) throws DbException {
|
||||
return throwingTransactionWithResult(readOnly, task);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends Exception> void throwingTransaction(boolean readOnly,
|
||||
DbRunnable<E> task) throws DbException, E {
|
||||
final Transaction txn = startTransaction(readOnly);
|
||||
try {
|
||||
task.run(txn);
|
||||
commitTransaction(txn);
|
||||
} finally {
|
||||
endTransaction(txn);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, E extends Exception> R throwingTransactionWithResult(
|
||||
boolean readOnly, DbCallable<R, E> task) throws DbException, E {
|
||||
final Transaction txn = startTransaction(readOnly);
|
||||
try {
|
||||
R result = task.call(txn);
|
||||
commitTransaction(txn);
|
||||
return result;
|
||||
} finally {
|
||||
endTransaction(txn);
|
||||
}
|
||||
}
|
||||
|
||||
private T unbox(Transaction transaction) {
|
||||
if (transaction.isCommitted()) throw new IllegalStateException();
|
||||
return txnClass.cast(transaction.unbox());
|
||||
|
||||
Reference in New Issue
Block a user