From a6376af7c2df2bcde8dd727c3de5ee7ddc8f7871 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Fri, 24 May 2019 12:44:23 +0100 Subject: [PATCH] Extract TransactionManager from DatabaseComponent. --- .../bramble/api/db/DatabaseComponent.java | 46 +--------------- .../bramble/api/db/TransactionManager.java | 54 +++++++++++++++++++ .../bramble/db/DatabaseModule.java | 6 +++ .../conversation/ConversationViewModel.java | 7 ++- 4 files changed, 64 insertions(+), 49 deletions(-) create mode 100644 bramble-api/src/main/java/org/briarproject/bramble/api/db/TransactionManager.java diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/db/DatabaseComponent.java b/bramble-api/src/main/java/org/briarproject/bramble/api/db/DatabaseComponent.java index 1f97269d2..a5e57224d 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/db/DatabaseComponent.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/db/DatabaseComponent.java @@ -38,7 +38,7 @@ import javax.annotation.Nullable; * to other components. */ @NotNullByDefault -public interface DatabaseComponent { +public interface DatabaseComponent extends TransactionManager { /** * Opens the database and returns true if the database already existed. @@ -56,50 +56,6 @@ public interface DatabaseComponent { */ void close() throws DbException; - /** - * Starts a new transaction and returns an object representing it. - *

- * This method acquires locks, so it must not be called while holding a - * lock. - * - * @param readOnly true if the transaction will only be used for reading. - */ - Transaction startTransaction(boolean readOnly) throws DbException; - - /** - * Commits a transaction to the database. - */ - void commitTransaction(Transaction txn) throws DbException; - - /** - * Ends a transaction. If the transaction has not been committed, - * it will be aborted. If the transaction has been committed, - * any events attached to the transaction are broadcast. - * The database lock will be released in either case. - */ - void endTransaction(Transaction txn); - - /** - * Runs the given task within a transaction. - */ - void transaction(boolean readOnly, - DbRunnable task) throws DbException, E; - - /** - * Runs the given task within a transaction and returns the result of the - * task. - */ - R transactionWithResult(boolean readOnly, - DbCallable task) throws DbException, E; - - /** - * Runs the given task within a transaction and returns the result of the - * task, which may be null. - */ - @Nullable - R transactionWithNullableResult(boolean readOnly, - NullableDbCallable task) throws DbException, E; - /** * Stores a contact associated with the given local and remote pseudonyms, * and returns an ID for the contact. diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/db/TransactionManager.java b/bramble-api/src/main/java/org/briarproject/bramble/api/db/TransactionManager.java new file mode 100644 index 000000000..6850c5b99 --- /dev/null +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/db/TransactionManager.java @@ -0,0 +1,54 @@ +package org.briarproject.bramble.api.db; + +import org.briarproject.bramble.api.nullsafety.NotNullByDefault; + +import javax.annotation.Nullable; + +@NotNullByDefault +public interface TransactionManager { + + /** + * Starts a new transaction and returns an object representing it. + *

+ * This method acquires locks, so it must not be called while holding a + * lock. + * + * @param readOnly true if the transaction will only be used for reading. + */ + Transaction startTransaction(boolean readOnly) throws DbException; + + /** + * Commits a transaction to the database. + */ + void commitTransaction(Transaction txn) throws DbException; + + /** + * Ends a transaction. If the transaction has not been committed, + * it will be aborted. If the transaction has been committed, + * any events attached to the transaction are broadcast. + * The database lock will be released in either case. + */ + void endTransaction(Transaction txn); + + /** + * Runs the given task within a transaction. + */ + void transaction(boolean readOnly, + DbRunnable task) throws DbException, E; + + /** + * Runs the given task within a transaction and returns the result of the + * task. + */ + R transactionWithResult(boolean readOnly, + DbCallable task) throws DbException, E; + + /** + * Runs the given task within a transaction and returns the result of the + * task, which may be null. + */ + @Nullable + R transactionWithNullableResult(boolean readOnly, + NullableDbCallable task) throws DbException, E; + +} diff --git a/bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseModule.java b/bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseModule.java index e1b86e719..915ce7f16 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseModule.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseModule.java @@ -2,6 +2,7 @@ package org.briarproject.bramble.db; import org.briarproject.bramble.api.db.DatabaseComponent; import org.briarproject.bramble.api.db.DatabaseConfig; +import org.briarproject.bramble.api.db.TransactionManager; import org.briarproject.bramble.api.event.EventBus; import org.briarproject.bramble.api.event.EventExecutor; import org.briarproject.bramble.api.lifecycle.ShutdownManager; @@ -34,4 +35,9 @@ public class DatabaseModule { return new DatabaseComponentImpl<>(db, Connection.class, eventBus, eventExecutor, shutdownManager); } + + @Provides + TransactionManager provideTransactionManager(DatabaseComponent db) { + return db; + } } diff --git a/briar-android/src/main/java/org/briarproject/briar/android/conversation/ConversationViewModel.java b/briar-android/src/main/java/org/briarproject/briar/android/conversation/ConversationViewModel.java index 7270697d4..ba1457e7c 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/conversation/ConversationViewModel.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/conversation/ConversationViewModel.java @@ -16,10 +16,10 @@ import org.briarproject.bramble.api.contact.Contact; import org.briarproject.bramble.api.contact.ContactId; import org.briarproject.bramble.api.contact.ContactManager; import org.briarproject.bramble.api.crypto.CryptoExecutor; -import org.briarproject.bramble.api.db.DatabaseComponent; import org.briarproject.bramble.api.db.DatabaseExecutor; import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.NoSuchContactException; +import org.briarproject.bramble.api.db.TransactionManager; import org.briarproject.bramble.api.identity.AuthorId; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.settings.Settings; @@ -72,8 +72,7 @@ public class ConversationViewModel extends AndroidViewModel { private final Executor dbExecutor; @CryptoExecutor private final Executor cryptoExecutor; - // TODO replace with TransactionManager once it exists - private final DatabaseComponent db; + private final TransactionManager db; private final MessagingManager messagingManager; private final ContactManager contactManager; private final SettingsManager settingsManager; @@ -105,7 +104,7 @@ public class ConversationViewModel extends AndroidViewModel { @Inject ConversationViewModel(Application application, @DatabaseExecutor Executor dbExecutor, - @CryptoExecutor Executor cryptoExecutor, DatabaseComponent db, + @CryptoExecutor Executor cryptoExecutor, TransactionManager db, MessagingManager messagingManager, ContactManager contactManager, SettingsManager settingsManager, PrivateMessageFactory privateMessageFactory) {