Lazy restore attachments.

pull/1/head
Matthew Chen 7 years ago
parent b2ac8f10e2
commit 1dced463cb

@ -129,12 +129,6 @@ NSString *const kOWSBackup_ImportDatabaseKeySpec = @"kOWSBackup_ImportDatabaseKe
return; return;
} }
[weakSelf restoreAttachmentFiles];
if (weakSelf.isComplete) {
return;
}
[weakSelf restoreDatabaseWithCompletion:^(BOOL restoreDatabaseSuccess) { [weakSelf restoreDatabaseWithCompletion:^(BOOL restoreDatabaseSuccess) {
if (!restoreDatabaseSuccess) { if (!restoreDatabaseSuccess) {
[weakSelf [weakSelf
@ -161,6 +155,15 @@ NSString *const kOWSBackup_ImportDatabaseKeySpec = @"kOWSBackup_ImportDatabaseKe
return; return;
} }
[weakSelf restoreAttachmentFiles];
if (weakSelf.isComplete) {
return;
}
// Kick off lazy restore.
[OWSBackupLazyRestoreJob run];
[weakSelf succeed]; [weakSelf succeed];
}]; }];
}]; }];

@ -9,8 +9,6 @@ import SignalServiceKit
@objc @objc
public class OWSBackupLazyRestoreJob: NSObject { public class OWSBackupLazyRestoreJob: NSObject {
let TAG = "[OWSBackupLazyRestoreJob]"
let primaryStorage: OWSPrimaryStorage let primaryStorage: OWSPrimaryStorage
private var jobTempDirPath: String? private var jobTempDirPath: String?
@ -43,10 +41,9 @@ public class OWSBackupLazyRestoreJob: NSObject {
private func restoreAttachments() { private func restoreAttachments() {
let temporaryDirectory = NSTemporaryDirectory() let temporaryDirectory = NSTemporaryDirectory()
let jobTempDirPath = (temporaryDirectory as NSString).appendingPathComponent(NSUUID().uuidString) let jobTempDirPath = (temporaryDirectory as NSString).appendingPathComponent(NSUUID().uuidString)
// let jobTempDirPath = temporaryDirectory.appendingPathComponent(UUID().uuidString)
guard OWSFileSystem.ensureDirectoryExists(jobTempDirPath) else { guard OWSFileSystem.ensureDirectoryExists(jobTempDirPath) else {
Logger.error("\(TAG) could not create temp directory.") Logger.error("\(logTag) could not create temp directory.")
return return
} }
@ -55,6 +52,11 @@ public class OWSBackupLazyRestoreJob: NSObject {
let backupIO = OWSBackupIO(jobTempDirPath: jobTempDirPath) let backupIO = OWSBackupIO(jobTempDirPath: jobTempDirPath)
let attachmentIds = OWSBackup.shared().attachmentIdsForLazyRestore() let attachmentIds = OWSBackup.shared().attachmentIdsForLazyRestore()
guard attachmentIds.count > 0 else {
Logger.info("\(logTag) No attachments need lazy restore.")
return
}
Logger.info("\(logTag) Lazy restoring \(attachmentIds.count) attachments.")
self.tryToRestoreNextAttachment(attachmentIds: attachmentIds, backupIO: backupIO) self.tryToRestoreNextAttachment(attachmentIds: attachmentIds, backupIO: backupIO)
} }
@ -62,12 +64,12 @@ public class OWSBackupLazyRestoreJob: NSObject {
var attachmentIdsCopy = attachmentIds var attachmentIdsCopy = attachmentIds
guard let attachmentId = attachmentIdsCopy.last else { guard let attachmentId = attachmentIdsCopy.last else {
// This job is done. // This job is done.
Logger.verbose("\(TAG) job is done.") Logger.verbose("\(logTag) job is done.")
return return
} }
attachmentIdsCopy.removeLast() attachmentIdsCopy.removeLast()
guard let attachment = TSAttachmentStream.fetch(uniqueId: attachmentId) else { guard let attachment = TSAttachmentStream.fetch(uniqueId: attachmentId) else {
Logger.warn("\(TAG) could not load attachment.") Logger.warn("\(logTag) could not load attachment.")
// Not necessarily an error. // Not necessarily an error.
// The attachment might have been deleted since the job began. // The attachment might have been deleted since the job began.
// Continue trying to restore the other attachments. // Continue trying to restore the other attachments.
@ -78,9 +80,9 @@ public class OWSBackupLazyRestoreJob: NSObject {
backupIO: backupIO, backupIO: backupIO,
completion: { (success) in completion: { (success) in
if success { if success {
Logger.info("\(self.TAG) restored attachment.") Logger.info("\(self.logTag) restored attachment.")
} else { } else {
Logger.warn("\(self.TAG) could not restore attachment.") Logger.warn("\(self.logTag) could not restore attachment.")
} }
// Continue trying to restore the other attachments. // Continue trying to restore the other attachments.
self.tryToRestoreNextAttachment(attachmentIds: attachmentIdsCopy, backupIO: backupIO) self.tryToRestoreNextAttachment(attachmentIds: attachmentIdsCopy, backupIO: backupIO)

@ -362,7 +362,7 @@ NS_ASSUME_NONNULL_BEGIN
} }
if (![[NSFileManager defaultManager] fileExistsAtPath:self.mediaURL.path]) { if (![[NSFileManager defaultManager] fileExistsAtPath:self.mediaURL.path]) {
OWSFail(@"%@ while generating thumbnail, source file doesn't exist: %@", self.logTag, self.mediaURL); DDLogError(@"%@ while generating thumbnail, source file doesn't exist: %@", self.logTag, self.mediaURL);
return; return;
} }
@ -649,6 +649,8 @@ NS_ASSUME_NONNULL_BEGIN
[attachment setLazyRestoreFragmentId:nil]; [attachment setLazyRestoreFragmentId:nil];
}]; }];
}]; }];
[self ensureThumbnail];
} }
@end @end

@ -389,7 +389,7 @@ NSString *const TSLazyRestoreAttachmentsGroup = @"TSLazyRestoreAttachmentsGroup"
options.allowedCollections = options.allowedCollections =
[[YapWhitelistBlacklist alloc] initWithWhitelist:[NSSet setWithObject:[TSAttachment collection]]]; [[YapWhitelistBlacklist alloc] initWithWhitelist:[NSSet setWithObject:[TSAttachment collection]]];
YapDatabaseView *view = YapDatabaseView *view =
[[YapDatabaseAutoView alloc] initWithGrouping:viewGrouping sorting:viewSorting versionTag:@"1" options:options]; [[YapDatabaseAutoView alloc] initWithGrouping:viewGrouping sorting:viewSorting versionTag:@"2" options:options];
[storage asyncRegisterExtension:view withName:TSLazyRestoreAttachmentsDatabaseViewExtensionName]; [storage asyncRegisterExtension:view withName:TSLazyRestoreAttachmentsDatabaseViewExtensionName];
} }

Loading…
Cancel
Save