Commit 2cce0f5f authored by akwizgran's avatar akwizgran

Remove OpenDatabaseHook priorities.

parent ebae1037
......@@ -4,7 +4,6 @@ import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.util.concurrent.ExecutorService;
......@@ -48,7 +47,7 @@ public interface LifecycleManager {
* {@link Service services} are started. This method should be called
* before {@link #startServices(SecretKey)}.
*/
void registerOpenDatabaseHook(OpenDatabaseHook hook, Priority p);
void registerOpenDatabaseHook(OpenDatabaseHook hook);
/**
* Registers a {@link Service} to be started and stopped. This method
......@@ -99,10 +98,10 @@ public interface LifecycleManager {
LifecycleState getLifecycleState();
interface OpenDatabaseHook {
enum Priority {EARLY, NORMAL, LATE}
/**
* Called when the database is being opened, before
* {@link #waitForDatabase()} returns.
*
* @param txn A read-write transaction
*/
void onDatabaseOpened(Transaction txn) throws DbException;
......
......@@ -10,8 +10,6 @@ import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.EARLY;
@Module
public class IdentityModule {
......@@ -29,7 +27,7 @@ public class IdentityModule {
@Singleton
IdentityManager provideIdentityManager(LifecycleManager lifecycleManager,
IdentityManagerImpl identityManager) {
lifecycleManager.registerOpenDatabaseHook(identityManager, EARLY);
lifecycleManager.registerOpenDatabaseHook(identityManager);
return identityManager;
}
}
package org.briarproject.bramble.lifecycle;
import org.briarproject.bramble.api.Pair;
import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.db.DataTooNewException;
import org.briarproject.bramble.api.db.DataTooOldException;
......@@ -9,13 +8,11 @@ import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.MigrationListener;
import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority;
import org.briarproject.bramble.api.lifecycle.Service;
import org.briarproject.bramble.api.lifecycle.ServiceException;
import org.briarproject.bramble.api.lifecycle.event.LifecycleEvent;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
......@@ -26,7 +23,6 @@ import java.util.logging.Logger;
import javax.annotation.concurrent.ThreadSafe;
import javax.inject.Inject;
import static java.util.Collections.sort;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
......@@ -57,7 +53,7 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
private final DatabaseComponent db;
private final EventBus eventBus;
private final List<Service> services;
private final List<Pair<OpenDatabaseHook, Priority>> openDatabaseHooks;
private final List<OpenDatabaseHook> openDatabaseHooks;
private final List<ExecutorService> executors;
private final Semaphore startStopSemaphore = new Semaphore(1);
private final CountDownLatch dbLatch = new CountDownLatch(1);
......@@ -83,12 +79,12 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
}
@Override
public void registerOpenDatabaseHook(OpenDatabaseHook hook, Priority p) {
public void registerOpenDatabaseHook(OpenDatabaseHook hook) {
if (LOG.isLoggable(INFO)) {
LOG.info("Registering open database hook "
+ hook.getClass().getSimpleName());
}
openDatabaseHooks.add(new Pair<>(hook, p));
openDatabaseHooks.add(hook);
}
@Override
......@@ -109,13 +105,10 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
boolean reopened = db.open(dbKey, this);
if (reopened) logDuration(LOG, "Reopening database", start);
else logDuration(LOG, "Creating database", start);
List<Pair<OpenDatabaseHook, Priority>> hooks =
new ArrayList<>(openDatabaseHooks);
sort(hooks, (a, b) -> a.getSecond().compareTo(b.getSecond()));
db.transaction(false, txn -> {
for (Pair<OpenDatabaseHook, Priority> pair : hooks) {
for (OpenDatabaseHook hook : openDatabaseHooks) {
long start1 = now();
OpenDatabaseHook hook = pair.getFirst();
hook.onDatabaseOpened(txn);
if (LOG.isLoggable(FINE)) {
logDuration(LOG, "Calling open database hook "
......
......@@ -15,7 +15,6 @@ import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.NORMAL;
import static org.briarproject.bramble.api.properties.TransportPropertyManager.CLIENT_ID;
import static org.briarproject.bramble.api.properties.TransportPropertyManager.MAJOR_VERSION;
import static org.briarproject.bramble.api.properties.TransportPropertyManager.MINOR_VERSION;
......@@ -49,8 +48,7 @@ public class PropertiesModule {
ValidationManager validationManager, ContactManager contactManager,
ClientVersioningManager clientVersioningManager,
TransportPropertyManagerImpl transportPropertyManager) {
lifecycleManager.registerOpenDatabaseHook(transportPropertyManager,
NORMAL);
lifecycleManager.registerOpenDatabaseHook(transportPropertyManager);
validationManager.registerIncomingMessageHook(CLIENT_ID, MAJOR_VERSION,
transportPropertyManager);
contactManager.registerContactHook(transportPropertyManager);
......
......@@ -14,7 +14,6 @@ import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.NORMAL;
import static org.briarproject.bramble.api.versioning.ClientVersioningManager.CLIENT_ID;
import static org.briarproject.bramble.api.versioning.ClientVersioningManager.MAJOR_VERSION;
......@@ -35,8 +34,7 @@ public class VersioningModule {
ClientVersioningManagerImpl clientVersioningManager,
LifecycleManager lifecycleManager, ContactManager contactManager,
ValidationManager validationManager) {
lifecycleManager.registerOpenDatabaseHook(clientVersioningManager,
NORMAL);
lifecycleManager.registerOpenDatabaseHook(clientVersioningManager);
lifecycleManager.registerService(clientVersioningManager);
contactManager.registerContactHook(clientVersioningManager);
validationManager.registerIncomingMessageHook(CLIENT_ID, MAJOR_VERSION,
......
......@@ -11,13 +11,9 @@ import org.briarproject.bramble.test.DbExpectations;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import static java.util.Arrays.asList;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.EARLY;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.LATE;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.NORMAL;
import static junit.framework.TestCase.assertTrue;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResult.SUCCESS;
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
import static org.junit.Assert.assertEquals;
......@@ -37,12 +33,10 @@ public class LifecycleManagerImplTest extends BrambleMockTestCase {
}
@Test
public void testOpenDatabaseHooksRunInOrderOfPriority() throws Exception {
public void testOpenDatabaseHooksAreCalledAtStartup() throws Exception {
Transaction txn = new Transaction(null, false);
List<Integer> results = new ArrayList<>();
OpenDatabaseHook hook1 = transaction -> results.add(1);
OpenDatabaseHook hook2 = transaction -> results.add(2);
OpenDatabaseHook hook3 = transaction -> results.add(3);
AtomicBoolean called = new AtomicBoolean(false);
OpenDatabaseHook hook = transaction -> called.set(true);
context.checking(new DbExpectations() {{
oneOf(db).open(dbKey, lifecycleManager);
......@@ -51,11 +45,9 @@ public class LifecycleManagerImplTest extends BrambleMockTestCase {
allowing(eventBus).broadcast(with(any(LifecycleEvent.class)));
}});
lifecycleManager.registerOpenDatabaseHook(hook1, LATE);
lifecycleManager.registerOpenDatabaseHook(hook2, NORMAL);
lifecycleManager.registerOpenDatabaseHook(hook3, EARLY);
lifecycleManager.registerOpenDatabaseHook(hook);
assertEquals(SUCCESS, lifecycleManager.startServices(dbKey));
assertEquals(asList(3, 2, 1), results);
assertTrue(called.get());
}
}
......@@ -3,7 +3,6 @@ package org.briarproject.bramble.test;
import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.lifecycle.IoExecutor;
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority;
import org.briarproject.bramble.api.lifecycle.Service;
import org.briarproject.bramble.api.lifecycle.ShutdownManager;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
......@@ -32,8 +31,7 @@ public class TestLifecycleModule {
}
@Override
public void registerOpenDatabaseHook(OpenDatabaseHook hook,
Priority p) {
public void registerOpenDatabaseHook(OpenDatabaseHook hook) {
}
@Override
......
......@@ -56,7 +56,6 @@ import dagger.Provides;
import static android.content.Context.MODE_PRIVATE;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.NORMAL;
import static org.briarproject.bramble.api.reporting.ReportingConstants.DEV_ONION_ADDRESS;
import static org.briarproject.bramble.api.reporting.ReportingConstants.DEV_PUBLIC_KEY_HEX;
......@@ -227,7 +226,7 @@ public class AppModule {
@Singleton
RecentEmoji provideRecentEmoji(LifecycleManager lifecycleManager,
RecentEmojiImpl recentEmoji) {
lifecycleManager.registerOpenDatabaseHook(recentEmoji, NORMAL);
lifecycleManager.registerOpenDatabaseHook(recentEmoji);
return recentEmoji;
}
}
......@@ -18,7 +18,6 @@ import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.NORMAL;
import static org.briarproject.briar.api.blog.BlogManager.CLIENT_ID;
import static org.briarproject.briar.api.blog.BlogManager.MAJOR_VERSION;
......@@ -37,7 +36,7 @@ public class BlogModule {
BlogManager provideBlogManager(BlogManagerImpl blogManager,
LifecycleManager lifecycleManager, ContactManager contactManager,
ValidationManager validationManager) {
lifecycleManager.registerOpenDatabaseHook(blogManager, NORMAL);
lifecycleManager.registerOpenDatabaseHook(blogManager);
contactManager.registerContactHook(blogManager);
validationManager.registerIncomingMessageHook(CLIENT_ID, MAJOR_VERSION,
blogManager);
......
......@@ -11,8 +11,6 @@ import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.NORMAL;
@Module
public class FeedModule {
......@@ -26,7 +24,7 @@ public class FeedModule {
FeedManager provideFeedManager(FeedManagerImpl feedManager,
LifecycleManager lifecycleManager, EventBus eventBus,
BlogManager blogManager) {
lifecycleManager.registerOpenDatabaseHook(feedManager, NORMAL);
lifecycleManager.registerOpenDatabaseHook(feedManager);
eventBus.addListener(feedManager);
blogManager.registerRemoveBlogHook(feedManager);
return feedManager;
......
......@@ -16,7 +16,6 @@ import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.NORMAL;
import static org.briarproject.briar.api.introduction.IntroductionManager.CLIENT_ID;
import static org.briarproject.briar.api.introduction.IntroductionManager.MAJOR_VERSION;
import static org.briarproject.briar.api.introduction.IntroductionManager.MINOR_VERSION;
......@@ -52,7 +51,7 @@ public class IntroductionModule {
ConversationManager conversationManager,
ClientVersioningManager clientVersioningManager,
IntroductionManagerImpl introductionManager) {
lifecycleManager.registerOpenDatabaseHook(introductionManager, NORMAL);
lifecycleManager.registerOpenDatabaseHook(introductionManager);
contactManager.registerContactHook(introductionManager);
validationManager.registerIncomingMessageHook(CLIENT_ID,
MAJOR_VERSION, introductionManager);
......
......@@ -17,7 +17,6 @@ import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.NORMAL;
import static org.briarproject.briar.api.messaging.MessagingManager.CLIENT_ID;
import static org.briarproject.briar.api.messaging.MessagingManager.MAJOR_VERSION;
import static org.briarproject.briar.api.messaging.MessagingManager.MINOR_VERSION;
......@@ -59,7 +58,7 @@ public class MessagingModule {
ConversationManager conversationManager,
ClientVersioningManager clientVersioningManager,
MessagingManagerImpl messagingManager) {
lifecycleManager.registerOpenDatabaseHook(messagingManager, NORMAL);
lifecycleManager.registerOpenDatabaseHook(messagingManager);
contactManager.registerContactHook(messagingManager);
validationManager.registerIncomingMessageHook(CLIENT_ID, MAJOR_VERSION,
messagingManager);
......
......@@ -19,7 +19,6 @@ import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.NORMAL;
import static org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager.CLIENT_ID;
import static org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager.MAJOR_VERSION;
import static org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager.MINOR_VERSION;
......@@ -43,8 +42,7 @@ public class GroupInvitationModule {
PrivateGroupManager privateGroupManager,
ConversationManager conversationManager,
ClientVersioningManager clientVersioningManager) {
lifecycleManager.registerOpenDatabaseHook(groupInvitationManager,
NORMAL);
lifecycleManager.registerOpenDatabaseHook(groupInvitationManager);
validationManager.registerIncomingMessageHook(CLIENT_ID, MAJOR_VERSION,
groupInvitationManager);
contactManager.registerContactHook(groupInvitationManager);
......
......@@ -25,8 +25,6 @@ import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.NORMAL;
@Module
public class SharingModule {
......@@ -78,7 +76,7 @@ public class SharingModule {
ConversationManager conversationManager, BlogManager blogManager,
ClientVersioningManager clientVersioningManager,
BlogSharingManagerImpl blogSharingManager) {
lifecycleManager.registerOpenDatabaseHook(blogSharingManager, NORMAL);
lifecycleManager.registerOpenDatabaseHook(blogSharingManager);
contactManager.registerContactHook(blogSharingManager);
validationManager.registerIncomingMessageHook(
BlogSharingManager.CLIENT_ID, BlogSharingManager.MAJOR_VERSION,
......@@ -137,7 +135,7 @@ public class SharingModule {
ConversationManager conversationManager, ForumManager forumManager,
ClientVersioningManager clientVersioningManager,
ForumSharingManagerImpl forumSharingManager) {
lifecycleManager.registerOpenDatabaseHook(forumSharingManager, NORMAL);
lifecycleManager.registerOpenDatabaseHook(forumSharingManager);
contactManager.registerContactHook(forumSharingManager);
validationManager.registerIncomingMessageHook(
ForumSharingManager.CLIENT_ID,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment