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

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

@ -362,7 +362,7 @@ NS_ASSUME_NONNULL_BEGIN
}
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;
}
@ -649,6 +649,8 @@ NS_ASSUME_NONNULL_BEGIN
[attachment setLazyRestoreFragmentId:nil];
}];
}];
[self ensureThumbnail];
}
@end

@ -389,7 +389,7 @@ NSString *const TSLazyRestoreAttachmentsGroup = @"TSLazyRestoreAttachmentsGroup"
options.allowedCollections =
[[YapWhitelistBlacklist alloc] initWithWhitelist:[NSSet setWithObject:[TSAttachment collection]]];
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];
}

Loading…
Cancel
Save