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

View File

@@ -50,12 +50,12 @@ public class SimplexOutgoingSessionTest extends BrambleMockTestCase {
oneOf(eventBus).addListener(session); oneOf(eventBus).addListener(session);
// No acks to send // No acks to send
oneOf(db).transactionWithNullableResult(with(false), oneOf(db).transactionWithNullableResult(with(false),
withDbCallable(noAckTxn)); withNullableDbCallable(noAckTxn));
oneOf(db).generateAck(noAckTxn, contactId, MAX_MESSAGE_IDS); oneOf(db).generateAck(noAckTxn, contactId, MAX_MESSAGE_IDS);
will(returnValue(null)); will(returnValue(null));
// No messages to send // No messages to send
oneOf(db).transactionWithNullableResult(with(false), oneOf(db).transactionWithNullableResult(with(false),
withDbCallable(noMsgTxn)); withNullableDbCallable(noMsgTxn));
oneOf(db).generateBatch(with(noMsgTxn), with(contactId), oneOf(db).generateBatch(with(noMsgTxn), with(contactId),
with(any(int.class)), with(MAX_LATENCY)); with(any(int.class)), with(MAX_LATENCY));
will(returnValue(null)); will(returnValue(null));
@@ -84,25 +84,25 @@ public class SimplexOutgoingSessionTest extends BrambleMockTestCase {
oneOf(eventBus).addListener(session); oneOf(eventBus).addListener(session);
// One ack to send // One ack to send
oneOf(db).transactionWithNullableResult(with(false), oneOf(db).transactionWithNullableResult(with(false),
withDbCallable(ackTxn)); withNullableDbCallable(ackTxn));
oneOf(db).generateAck(ackTxn, contactId, MAX_MESSAGE_IDS); oneOf(db).generateAck(ackTxn, contactId, MAX_MESSAGE_IDS);
will(returnValue(ack)); will(returnValue(ack));
oneOf(recordWriter).writeAck(ack); oneOf(recordWriter).writeAck(ack);
// One message to send // One message to send
oneOf(db).transactionWithNullableResult(with(false), oneOf(db).transactionWithNullableResult(with(false),
withDbCallable(msgTxn)); withNullableDbCallable(msgTxn));
oneOf(db).generateBatch(with(msgTxn), with(contactId), oneOf(db).generateBatch(with(msgTxn), with(contactId),
with(any(int.class)), with(MAX_LATENCY)); with(any(int.class)), with(MAX_LATENCY));
will(returnValue(singletonList(message))); will(returnValue(singletonList(message)));
oneOf(recordWriter).writeMessage(message); oneOf(recordWriter).writeMessage(message);
// No more acks // No more acks
oneOf(db).transactionWithNullableResult(with(false), oneOf(db).transactionWithNullableResult(with(false),
withDbCallable(noAckTxn)); withNullableDbCallable(noAckTxn));
oneOf(db).generateAck(noAckTxn, contactId, MAX_MESSAGE_IDS); oneOf(db).generateAck(noAckTxn, contactId, MAX_MESSAGE_IDS);
will(returnValue(null)); will(returnValue(null));
// No more messages // No more messages
oneOf(db).transactionWithNullableResult(with(false), oneOf(db).transactionWithNullableResult(with(false),
withDbCallable(noMsgTxn)); withNullableDbCallable(noMsgTxn));
oneOf(db).generateBatch(with(noMsgTxn), with(contactId), oneOf(db).generateBatch(with(noMsgTxn), with(contactId),
with(any(int.class)), with(MAX_LATENCY)); with(any(int.class)), with(MAX_LATENCY));
will(returnValue(null)); 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.DbCallable;
import org.briarproject.bramble.api.db.DbRunnable; import org.briarproject.bramble.api.db.DbRunnable;
import org.briarproject.bramble.api.db.NullableDbCallable;
import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.db.Transaction;
import org.jmock.Expectations; import org.jmock.Expectations;
@@ -21,4 +22,11 @@ public class DbExpectations extends Expectations {
return null; return null;
} }
protected <R, E extends Exception> NullableDbCallable<R, E> withNullableDbCallable(
Transaction txn) {
addParameterMatcher(any(NullableDbCallable.class));
currentBuilder().setAction(new RunTransactionWithResultAction(txn));
return null;
}
} }