From 1eb7fc986cd9423fe59decebc65fca5cdf477600 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 2 Aug 2018 11:44:00 -0600 Subject: [PATCH] YapDB introduced a method purpose built to do what we were approximating. This seems a little more future proof / less error prone if we change the registered extensions. --- .../src/Storage/OWSPrimaryStorage.m | 89 ++++++++----------- SignalServiceKit/src/Storage/TSDatabaseView.h | 3 +- SignalServiceKit/src/Storage/TSDatabaseView.m | 5 +- 3 files changed, 38 insertions(+), 59 deletions(-) diff --git a/SignalServiceKit/src/Storage/OWSPrimaryStorage.m b/SignalServiceKit/src/Storage/OWSPrimaryStorage.m index 9cfacba44c..d21dcebea7 100644 --- a/SignalServiceKit/src/Storage/OWSPrimaryStorage.m +++ b/SignalServiceKit/src/Storage/OWSPrimaryStorage.m @@ -30,50 +30,6 @@ NSString *const OWSUIDatabaseConnectionNotificationsKey = @"OWSUIDatabaseConnect NSString *const OWSPrimaryStorageExceptionName_CouldNotCreateDatabaseDirectory = @"TSStorageManagerExceptionName_CouldNotCreateDatabaseDirectory"; -void RunSyncRegistrationsForStorage(OWSStorage *storage) -{ - OWSCAssert(storage); - - // Synchronously register extensions which are essential for views. - [TSDatabaseView registerCrossProcessNotifier:storage]; -} - -void RunAsyncRegistrationsForStorage(OWSStorage *storage, dispatch_block_t completion) -{ - OWSCAssert(storage); - OWSCAssert(completion); - - // Asynchronously register other extensions. - // - // All sync registrations must be done before all async registrations, - // or the sync registrations will block on the async registrations. - - [TSDatabaseView asyncRegisterThreadInteractionsDatabaseView:storage]; - [TSDatabaseView asyncRegisterThreadDatabaseView:storage]; - [TSDatabaseView asyncRegisterUnreadDatabaseView:storage]; - [storage asyncRegisterExtension:[TSDatabaseSecondaryIndexes registerTimeStampIndex] - withName:[TSDatabaseSecondaryIndexes registerTimeStampIndexExtensionName]]; - [OWSMessageReceiver asyncRegisterDatabaseExtension:storage]; - [OWSBatchMessageProcessor asyncRegisterDatabaseExtension:storage]; - - [TSDatabaseView asyncRegisterUnseenDatabaseView:storage]; - [TSDatabaseView asyncRegisterThreadOutgoingMessagesDatabaseView:storage]; - [TSDatabaseView asyncRegisterThreadSpecialMessagesDatabaseView:storage]; - - [FullTextSearchFinder asyncRegisterDatabaseExtensionWithStorage:storage]; - [OWSIncomingMessageFinder asyncRegisterExtensionWithPrimaryStorage:storage]; - [TSDatabaseView asyncRegisterSecondaryDevicesDatabaseView:storage]; - [OWSDisappearingMessagesFinder asyncRegisterDatabaseExtensions:storage]; - [OWSFailedMessagesJob asyncRegisterDatabaseExtensionsWithPrimaryStorage:storage]; - [OWSIncompleteCallsJob asyncRegisterDatabaseExtensionsWithPrimaryStorage:storage]; - [OWSFailedAttachmentDownloadsJob asyncRegisterDatabaseExtensionsWithPrimaryStorage:storage]; - [OWSMediaGalleryFinder asyncRegisterDatabaseExtensionsWithPrimaryStorage:storage]; - - // NOTE: Always pass the completion to the _LAST_ of the async database - // view registrations. - [TSDatabaseView asyncRegisterLazyRestoreAttachmentsDatabaseView:storage completion:completion]; -} - void VerifyRegistrationsForPrimaryStorage(OWSStorage *storage) { OWSCAssert(storage); @@ -208,7 +164,8 @@ void VerifyRegistrationsForPrimaryStorage(OWSStorage *storage) - (void)runSyncRegistrations { - RunSyncRegistrationsForStorage(self); + // Synchronously register extensions which are essential for views. + [TSDatabaseView registerCrossProcessNotifier:self]; // See comments on OWSDatabaseConnection. // @@ -223,22 +180,48 @@ void VerifyRegistrationsForPrimaryStorage(OWSStorage *storage) - (void)runAsyncRegistrationsWithCompletion:(void (^_Nonnull)(void))completion { OWSAssert(completion); + OWSAssert(self.database); DDLogVerbose(@"%@ async registrations enqueuing.", self.logTag); - RunAsyncRegistrationsForStorage(self, ^{ - OWSAssertIsOnMainThread(); + // Asynchronously register other extensions. + // + // All sync registrations must be done before all async registrations, + // or the sync registrations will block on the async registrations. + [TSDatabaseView asyncRegisterThreadInteractionsDatabaseView:self]; + [TSDatabaseView asyncRegisterThreadDatabaseView:self]; + [TSDatabaseView asyncRegisterUnreadDatabaseView:self]; + [self asyncRegisterExtension:[TSDatabaseSecondaryIndexes registerTimeStampIndex] + withName:[TSDatabaseSecondaryIndexes registerTimeStampIndexExtensionName]]; - OWSAssert(!self.areAsyncRegistrationsComplete); + [OWSMessageReceiver asyncRegisterDatabaseExtension:self]; + [OWSBatchMessageProcessor asyncRegisterDatabaseExtension:self]; - DDLogVerbose(@"%@ async registrations complete.", self.logTag); + [TSDatabaseView asyncRegisterUnseenDatabaseView:self]; + [TSDatabaseView asyncRegisterThreadOutgoingMessagesDatabaseView:self]; + [TSDatabaseView asyncRegisterThreadSpecialMessagesDatabaseView:self]; - self.areAsyncRegistrationsComplete = YES; + [FullTextSearchFinder asyncRegisterDatabaseExtensionWithStorage:self]; + [OWSIncomingMessageFinder asyncRegisterExtensionWithPrimaryStorage:self]; + [TSDatabaseView asyncRegisterSecondaryDevicesDatabaseView:self]; + [OWSDisappearingMessagesFinder asyncRegisterDatabaseExtensions:self]; + [OWSFailedMessagesJob asyncRegisterDatabaseExtensionsWithPrimaryStorage:self]; + [OWSIncompleteCallsJob asyncRegisterDatabaseExtensionsWithPrimaryStorage:self]; + [OWSFailedAttachmentDownloadsJob asyncRegisterDatabaseExtensionsWithPrimaryStorage:self]; + [OWSMediaGalleryFinder asyncRegisterDatabaseExtensionsWithPrimaryStorage:self]; + [TSDatabaseView asyncRegisterLazyRestoreAttachmentsDatabaseView:self]; - completion(); + [self.database flushExtensionRequestsWithCompletionQueue:nil + completionBlock:^{ + OWSAssertIsOnMainThread(); + OWSAssert(!self.areAsyncRegistrationsComplete); + DDLogVerbose(@"%@ async registrations complete.", self.logTag); + self.areAsyncRegistrationsComplete = YES; - [self verifyDatabaseViews]; - }); + completion(); + + [self verifyDatabaseViews]; + }]; } - (void)verifyDatabaseViews diff --git a/SignalServiceKit/src/Storage/TSDatabaseView.h b/SignalServiceKit/src/Storage/TSDatabaseView.h index 9954804a6b..3e83d174b2 100644 --- a/SignalServiceKit/src/Storage/TSDatabaseView.h +++ b/SignalServiceKit/src/Storage/TSDatabaseView.h @@ -58,7 +58,6 @@ extern NSString *const TSLazyRestoreAttachmentsDatabaseViewExtensionName; + (void)asyncRegisterSecondaryDevicesDatabaseView:(OWSStorage *)storage; -+ (void)asyncRegisterLazyRestoreAttachmentsDatabaseView:(OWSStorage *)storage - completion:(nullable dispatch_block_t)completion; ++ (void)asyncRegisterLazyRestoreAttachmentsDatabaseView:(OWSStorage *)storage; @end diff --git a/SignalServiceKit/src/Storage/TSDatabaseView.m b/SignalServiceKit/src/Storage/TSDatabaseView.m index 956ac1e12f..8e0d1e0b23 100644 --- a/SignalServiceKit/src/Storage/TSDatabaseView.m +++ b/SignalServiceKit/src/Storage/TSDatabaseView.m @@ -362,7 +362,6 @@ NSString *const TSLazyRestoreAttachmentsGroup = @"TSLazyRestoreAttachmentsGroup" } + (void)asyncRegisterLazyRestoreAttachmentsDatabaseView:(OWSStorage *)storage - completion:(nullable dispatch_block_t)completion { YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *_Nullable( YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) { @@ -411,9 +410,7 @@ NSString *const TSLazyRestoreAttachmentsGroup = @"TSLazyRestoreAttachmentsGroup" [[YapWhitelistBlacklist alloc] initWithWhitelist:[NSSet setWithObject:[TSAttachment collection]]]; YapDatabaseView *view = [[YapDatabaseAutoView alloc] initWithGrouping:viewGrouping sorting:viewSorting versionTag:@"3" options:options]; - [storage asyncRegisterExtension:view - withName:TSLazyRestoreAttachmentsDatabaseViewExtensionName - completion:completion]; + [storage asyncRegisterExtension:view withName:TSLazyRestoreAttachmentsDatabaseViewExtensionName]; } + (id)unseenDatabaseViewExtension:(YapDatabaseReadTransaction *)transaction