Add interface for DB tasks will nullable results.

This commit is contained in:
akwizgran
2018-11-12 12:13:26 +00:00
parent b24914408d
commit ecb63d1acb
6 changed files with 38 additions and 14 deletions

View File

@@ -95,7 +95,7 @@ public interface DatabaseComponent {
*/
@Nullable
<R, E extends Exception> R transactionWithNullableResult(boolean readOnly,
DbCallable<R, E> task) throws DbException, E;
NullableDbCallable<R, E> task) throws DbException, E;
/**
* Stores a contact associated with the given local and remote pseudonyms,

View File

@@ -2,11 +2,8 @@ package org.briarproject.bramble.api.db;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import javax.annotation.Nullable;
@NotNullByDefault
public interface DbCallable<R, E extends Exception> {
@Nullable
R call(Transaction txn) throws DbException, E;
}

View File

@@ -0,0 +1,12 @@
package org.briarproject.bramble.api.db;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import javax.annotation.Nullable;
@NotNullByDefault
public interface NullableDbCallable<R, E extends Exception> {
@Nullable
R call(Transaction txn) throws DbException, E;
}

View File

@@ -19,6 +19,7 @@ import org.briarproject.bramble.api.db.NoSuchGroupException;
import org.briarproject.bramble.api.db.NoSuchLocalAuthorException;
import org.briarproject.bramble.api.db.NoSuchMessageException;
import org.briarproject.bramble.api.db.NoSuchTransportException;
import org.briarproject.bramble.api.db.NullableDbCallable;
import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.event.Event;
import org.briarproject.bramble.api.event.EventBus;
@@ -183,14 +184,20 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
@Override
public <R, E extends Exception> R transactionWithResult(boolean readOnly,
DbCallable<R, E> task) throws DbException, E {
R result = transactionWithNullableResult(readOnly, task);
if (result == null) throw new NullPointerException();
return result;
Transaction txn = startTransaction(readOnly);
try {
R result = task.call(txn);
commitTransaction(txn);
return result;
} finally {
endTransaction(txn);
}
}
@Override
public <R, E extends Exception> R transactionWithNullableResult(
boolean readOnly, DbCallable<R, E> task) throws DbException, E {
boolean readOnly, NullableDbCallable<R, E> task)
throws DbException, E {
Transaction txn = startTransaction(readOnly);
try {
R result = task.call(txn);

View File

@@ -50,12 +50,12 @@ public class SimplexOutgoingSessionTest extends BrambleMockTestCase {
oneOf(eventBus).addListener(session);
// No acks to send
oneOf(db).transactionWithNullableResult(with(false),
withDbCallable(noAckTxn));
withNullableDbCallable(noAckTxn));
oneOf(db).generateAck(noAckTxn, contactId, MAX_MESSAGE_IDS);
will(returnValue(null));
// No messages to send
oneOf(db).transactionWithNullableResult(with(false),
withDbCallable(noMsgTxn));
withNullableDbCallable(noMsgTxn));
oneOf(db).generateBatch(with(noMsgTxn), with(contactId),
with(any(int.class)), with(MAX_LATENCY));
will(returnValue(null));
@@ -84,25 +84,25 @@ public class SimplexOutgoingSessionTest extends BrambleMockTestCase {
oneOf(eventBus).addListener(session);
// One ack to send
oneOf(db).transactionWithNullableResult(with(false),
withDbCallable(ackTxn));
withNullableDbCallable(ackTxn));
oneOf(db).generateAck(ackTxn, contactId, MAX_MESSAGE_IDS);
will(returnValue(ack));
oneOf(recordWriter).writeAck(ack);
// One message to send
oneOf(db).transactionWithNullableResult(with(false),
withDbCallable(msgTxn));
withNullableDbCallable(msgTxn));
oneOf(db).generateBatch(with(msgTxn), with(contactId),
with(any(int.class)), with(MAX_LATENCY));
will(returnValue(singletonList(message)));
oneOf(recordWriter).writeMessage(message);
// No more acks
oneOf(db).transactionWithNullableResult(with(false),
withDbCallable(noAckTxn));
withNullableDbCallable(noAckTxn));
oneOf(db).generateAck(noAckTxn, contactId, MAX_MESSAGE_IDS);
will(returnValue(null));
// No more messages
oneOf(db).transactionWithNullableResult(with(false),
withDbCallable(noMsgTxn));
withNullableDbCallable(noMsgTxn));
oneOf(db).generateBatch(with(noMsgTxn), with(contactId),
with(any(int.class)), with(MAX_LATENCY));
will(returnValue(null));

View File

@@ -2,6 +2,7 @@ package org.briarproject.bramble.test;
import org.briarproject.bramble.api.db.DbCallable;
import org.briarproject.bramble.api.db.DbRunnable;
import org.briarproject.bramble.api.db.NullableDbCallable;
import org.briarproject.bramble.api.db.Transaction;
import org.jmock.Expectations;
@@ -21,4 +22,11 @@ public class DbExpectations extends Expectations {
return null;
}
protected <R, E extends Exception> NullableDbCallable<R, E> withNullableDbCallable(
Transaction txn) {
addParameterMatcher(any(NullableDbCallable.class));
currentBuilder().setAction(new RunTransactionWithResultAction(txn));
return null;
}
}