@@ -36,6 +36,7 @@ public class MessageBackupManagerImpl: MessageBackupManager {
3636 private let appVersion : AppVersion
3737 private let attachmentDownloadManager : AttachmentDownloadManager
3838 private let attachmentUploadManager : AttachmentUploadManager
39+ private let avatarFetcher : MessageBackupAvatarFetcher
3940 private let backupAttachmentDownloadManager : BackupAttachmentDownloadManager
4041 private let backupAttachmentUploadManager : BackupAttachmentUploadManager
4142 private let backupRequestManager : MessageBackupRequestManager
@@ -72,6 +73,7 @@ public class MessageBackupManagerImpl: MessageBackupManager {
7273 appVersion: AppVersion ,
7374 attachmentDownloadManager: AttachmentDownloadManager ,
7475 attachmentUploadManager: AttachmentUploadManager ,
76+ avatarFetcher: MessageBackupAvatarFetcher ,
7577 backupAttachmentDownloadManager: BackupAttachmentDownloadManager ,
7678 backupAttachmentUploadManager: BackupAttachmentUploadManager ,
7779 backupRequestManager: MessageBackupRequestManager ,
@@ -105,6 +107,7 @@ public class MessageBackupManagerImpl: MessageBackupManager {
105107 self . appVersion = appVersion
106108 self . attachmentDownloadManager = attachmentDownloadManager
107109 self . attachmentUploadManager = attachmentUploadManager
110+ self . avatarFetcher = avatarFetcher
108111 self . backupAttachmentDownloadManager = backupAttachmentDownloadManager
109112 self . backupAttachmentUploadManager = backupAttachmentUploadManager
110113 self . backupRequestManager = backupRequestManager
@@ -675,14 +678,6 @@ public class MessageBackupManagerImpl: MessageBackupManager {
675678 ) async throws {
676679 let result : Result < BackupProto_BackupInfo , Error > = await db. awaitableWriteWithTxCompletion { tx in
677680 do {
678- /// Drops all indexes on the `TSInteraction` table before doing
679- /// the import, which dramatically speeds up the import. We'll
680- /// then recreate all these indexes in bulk afterwards.
681- let interactionIndexes = try dropAllIndexes (
682- forTable: InteractionRecord . databaseTableName,
683- tx: tx
684- )
685-
686681 let backupInfo = try Bench (
687682 title: benchTitle,
688683 memorySamplerRatio: FeatureFlags . messageBackupMemorySamplerRatio,
@@ -709,19 +704,6 @@ public class MessageBackupManagerImpl: MessageBackupManager {
709704 }
710705 }
711706
712- let timeBeforeCreatingIndexes = dateProviderMonotonic ( )
713-
714- /// Now that we've imported successfully, we want to recreate
715- /// the indexes we temporarily dropped.
716- try createIndexes (
717- interactionIndexes,
718- onTable: InteractionRecord . databaseTableName,
719- tx: tx
720- )
721-
722- let timeAfterCreatingIndexes = dateProviderMonotonic ( )
723- Logger . info ( " Created indexes in \( timeAfterCreatingIndexes. millisSince ( timeBeforeCreatingIndexes) ) ms " )
724-
725707 return . commit( . success( backupInfo) )
726708 } catch let error {
727709 return . rollback( . failure( error) )
@@ -750,6 +732,16 @@ public class MessageBackupManagerImpl: MessageBackupManager {
750732 throw OWSAssertionError ( " Restoring from backup twice! " )
751733 }
752734
735+ // Drops all indexes on the `TSInteraction` table before doing the
736+ // import, which dramatically speeds up the import. We'll then recreate
737+ // all these indexes in bulk afterwards.
738+ let interactionIndexes = try bencher. benchPreFrameAction ( . DropInteractionIndexes) {
739+ try dropAllIndexes (
740+ forTable: InteractionRecord . databaseTableName,
741+ tx: tx
742+ )
743+ }
744+
753745 var frameErrors = [ LoggableErrorAndProto] ( )
754746 let result = Result < BackupProto_BackupInfo , Error > ( catching: {
755747
@@ -1042,26 +1034,50 @@ public class MessageBackupManagerImpl: MessageBackupManager {
10421034
10431035 stream. closeFileStream ( )
10441036
1045- /// Take any necessary post-frame-restore actions.
1037+ // Now that we've imported successfully, we want to recreate the
1038+ // the indexes we temporarily dropped.
1039+ try bencher. benchPostFrameAction ( . RecreateInteractionIndexes) {
1040+ try createIndexes (
1041+ interactionIndexes,
1042+ onTable: InteractionRecord . databaseTableName,
1043+ tx: tx
1044+ )
1045+ }
1046+
1047+ // Take any necessary post-frame-restore actions.
10461048 try postFrameRestoreActionManager. performPostFrameRestoreActions (
10471049 recipientActions: contexts. recipient. postFrameRestoreActions,
10481050 chatActions: contexts. chat. postFrameRestoreActions,
10491051 bencher: bencher,
10501052 chatItemContext: contexts. chatItem
10511053 )
10521054
1053- // Index threads synchronously
1055+ // Index threads synchronously, since that should be fast.
10541056 bencher. benchPostFrameAction ( . IndexThreads) {
10551057 fullTextSearchIndexer. indexThreads ( tx: tx)
10561058 }
1057- // Schedule message indexing asynchronously
1059+
1060+ // Schedule background message indexing, since that'll be slow.
10581061 try fullTextSearchIndexer. scheduleMessagesJob ( tx: tx)
10591062
1060- tx. addAsyncCompletion ( on: DispatchQueue . global ( ) ) { [ backupAttachmentDownloadManager, disappearingMessagesJob] in
1063+ // Record that we've restored a Backup!
1064+ kvStore. setBool ( true , key: Constants . keyValueStoreHasRestoredBackupKey, transaction: tx)
1065+
1066+ tx. addAsyncCompletion ( on: DispatchQueue . global ( ) ) { [
1067+ avatarFetcher,
1068+ backupAttachmentDownloadManager,
1069+ disappearingMessagesJob
1070+ ] in
1071+ Task {
1072+ // Kick off avatar fetches enqueued during restore.
1073+ try await avatarFetcher. runIfNeeded ( )
1074+ }
1075+
10611076 Task {
1062- // Enqueue downloads for all the attachments .
1077+ // Kick off attachment downloads enqueued during restore .
10631078 try await backupAttachmentDownloadManager. restoreAttachmentsIfNeeded ( )
10641079 }
1080+
10651081 // Start ticking down for disappearing messages.
10661082 disappearingMessagesJob. startIfNecessary ( )
10671083 }
@@ -1071,10 +1087,9 @@ public class MessageBackupManagerImpl: MessageBackupManager {
10711087 Logger . info ( " Backup first app version: \( backupInfo. firstAppVersion. nilIfEmpty ?? " Missing! " ) " )
10721088 bencher. logResults ( )
10731089
1074- kvStore. setBool ( true , key: Constants . keyValueStoreHasRestoredBackupKey, transaction: tx)
1075-
10761090 return backupInfo
10771091 } )
1092+
10781093 processErrors ( errors: frameErrors, didFail: result. isSuccess. negated, tx: tx)
10791094 return try result. get ( )
10801095 }
0 commit comments