mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Expose transactions through DatabaseComponent interface.
This commit is contained in:
@@ -39,6 +39,21 @@ public interface DatabaseComponent {
|
||||
/** Waits for any open transactions to finish and closes the database. */
|
||||
void close() throws DbException, IOException;
|
||||
|
||||
/** Starts a new transaction and returns an object representing it. */
|
||||
Transaction startTransaction() throws DbException;
|
||||
|
||||
/**
|
||||
* Aborts the given transaction - no changes made during the transaction
|
||||
* will be applied to the database.
|
||||
*/
|
||||
void abortTransaction(Transaction txn);
|
||||
|
||||
/**
|
||||
* Commits the given transaction - all changes made during the transaction
|
||||
* will be applied to the database.
|
||||
*/
|
||||
void commitTransaction(Transaction txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Stores a contact associated with the given local and remote pseudonyms,
|
||||
* and returns an ID for the contact.
|
||||
|
||||
15
briar-api/src/org/briarproject/api/db/Transaction.java
Normal file
15
briar-api/src/org/briarproject/api/db/Transaction.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package org.briarproject.api.db;
|
||||
|
||||
/** A wrapper around a database transaction. */
|
||||
public class Transaction {
|
||||
|
||||
private final Object txn;
|
||||
|
||||
public Transaction(Object txn) {
|
||||
this.txn = txn;
|
||||
}
|
||||
|
||||
public Object unbox() {
|
||||
return txn;
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import org.briarproject.api.db.NoSuchLocalAuthorException;
|
||||
import org.briarproject.api.db.NoSuchMessageException;
|
||||
import org.briarproject.api.db.NoSuchTransportException;
|
||||
import org.briarproject.api.db.StorageStatus;
|
||||
import org.briarproject.api.db.Transaction;
|
||||
import org.briarproject.api.event.EventBus;
|
||||
import org.briarproject.api.event.GroupAddedEvent;
|
||||
import org.briarproject.api.event.GroupRemovedEvent;
|
||||
@@ -77,6 +78,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
Logger.getLogger(DatabaseComponentImpl.class.getName());
|
||||
|
||||
private final Database<T> db;
|
||||
private final Class<T> txnClass;
|
||||
private final EventBus eventBus;
|
||||
private final ShutdownManager shutdown;
|
||||
private final AtomicBoolean closed = new AtomicBoolean(false);
|
||||
@@ -84,9 +86,10 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
private volatile int shutdownHandle = -1;
|
||||
|
||||
@Inject
|
||||
DatabaseComponentImpl(Database<T> db, EventBus eventBus,
|
||||
DatabaseComponentImpl(Database<T> db, Class<T> txnClass, EventBus eventBus,
|
||||
ShutdownManager shutdown) {
|
||||
this.db = db;
|
||||
this.txnClass = txnClass;
|
||||
this.eventBus = eventBus;
|
||||
this.shutdown = shutdown;
|
||||
}
|
||||
@@ -116,6 +119,18 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
db.close();
|
||||
}
|
||||
|
||||
public Transaction startTransaction() throws DbException {
|
||||
return new Transaction(db.startTransaction());
|
||||
}
|
||||
|
||||
public void abortTransaction(Transaction txn) {
|
||||
db.abortTransaction(txnClass.cast(txn.unbox()));
|
||||
}
|
||||
|
||||
public void commitTransaction(Transaction txn) throws DbException {
|
||||
db.commitTransaction(txnClass.cast(txn.unbox()));
|
||||
}
|
||||
|
||||
public ContactId addContact(Author remote, AuthorId local)
|
||||
throws DbException {
|
||||
T txn = db.startTransaction();
|
||||
@@ -975,7 +990,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
|
||||
public void setVisibleToContact(ContactId c, GroupId g, boolean visible)
|
||||
throws DbException {
|
||||
boolean wasVisible = false;
|
||||
boolean wasVisible;
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if (!db.containsContact(txn, c))
|
||||
|
||||
@@ -51,7 +51,8 @@ public class DatabaseModule extends AbstractModule {
|
||||
@Provides @Singleton
|
||||
DatabaseComponent getDatabaseComponent(Database<Connection> db,
|
||||
EventBus eventBus, ShutdownManager shutdown) {
|
||||
return new DatabaseComponentImpl<Connection>(db, eventBus, shutdown);
|
||||
return new DatabaseComponentImpl<Connection>(db, Connection.class,
|
||||
eventBus, shutdown);
|
||||
}
|
||||
|
||||
@Provides @Singleton @DatabaseExecutor
|
||||
|
||||
@@ -106,9 +106,10 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
StorageStatus.ACTIVE);
|
||||
}
|
||||
|
||||
private <T> DatabaseComponent createDatabaseComponent(Database<T> database,
|
||||
private DatabaseComponent createDatabaseComponent(Database<Object> database,
|
||||
EventBus eventBus, ShutdownManager shutdown) {
|
||||
return new DatabaseComponentImpl<T>(database, eventBus, shutdown);
|
||||
return new DatabaseComponentImpl<Object>(database, Object.class,
|
||||
eventBus, shutdown);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user