diff --git a/SignalMessaging/environment/migrations/OWS115GRDBMigration.swift b/SignalMessaging/environment/migrations/OWS115GRDBMigration.swift index 610e178e4c..8b6316ce81 100644 --- a/SignalMessaging/environment/migrations/OWS115GRDBMigration.swift +++ b/SignalMessaging/environment/migrations/OWS115GRDBMigration.swift @@ -5,6 +5,36 @@ import Foundation import GRDBCipher +private protocol GRDBMigrator { + func migrate(grdbTransaction: GRDBWriteTransaction) throws +} + +private class GRDBKeyValueStoreMigrator : GRDBMigrator { + private let label: String + private let finder: LegacyKeyValueFinder + private let memorySamplerRatio: Float + + init(label: String, keyStore: SDSKeyValueStore, yapTransaction: YapDatabaseReadTransaction, memorySamplerRatio: Float) { + self.label = label + self.finder = LegacyKeyValueFinder(store: keyStore, transaction: yapTransaction) + self.memorySamplerRatio = memorySamplerRatio + } + + func migrate(grdbTransaction: GRDBWriteTransaction) throws { + try Bench(title: "Migrate \(label)", memorySamplerRatio: memorySamplerRatio) { memorySampler in + var recordCount = 0 + try finder.enumerateLegacyKeysAndObjects { legacyKey, legacyObject in + recordCount += 1 + self.finder.store.setObject(legacyObject, key: legacyKey, transaction: grdbTransaction.asAnyWrite) + memorySampler.sample() + } + Logger.info("completed with recordCount: \(recordCount)") + } + } +} + +// MARK: - + @objc public class OWS115GRDBMigration: OWSDatabaseMigration { @@ -96,33 +126,14 @@ extension OWS115GRDBMigration { var threadFinder: LegacyUnorderedFinder! // KeyValue Finders - var sessionStoreFinder: LegacyKeyValueFinder<[Int: SessionRecord]>! - var preKeyStoreFinder: LegacyKeyValueFinder! - var preKeyMetadataFinder: LegacyKeyValueFinder! - var signedPreKeyStoreFinder: LegacyKeyValueFinder! - var signedPreKeyMetadataFinder: LegacyKeyValueFinder! - - var ownIdentityFinder: LegacyKeyValueFinder! - var queuedVerificationStateSyncMessagesFinder: LegacyKeyValueFinder! - var tsAccountManagerFinder: LegacyKeyValueFinder! + var migrators = [GRDBMigrator]() dbReadConnection.read { yapTransaction in jobRecordFinder = LegacyJobRecordFinder(transaction: yapTransaction) interactionFinder = LegacyInteractionFinder(transaction: yapTransaction) decryptJobFinder = LegacyUnorderedFinder(transaction: yapTransaction) - // protocol store finders - preKeyStoreFinder = LegacyKeyValueFinder(store: self.preKeyStore.keyStore, transaction: yapTransaction) - preKeyMetadataFinder = LegacyKeyValueFinder(store: self.preKeyStore.metadataStore, transaction: yapTransaction) - - signedPreKeyStoreFinder = LegacyKeyValueFinder(store: self.signedPreKeyStore.keyStore, transaction: yapTransaction) - signedPreKeyMetadataFinder = LegacyKeyValueFinder(store: self.signedPreKeyStore.metadataStore, transaction: yapTransaction) - - ownIdentityFinder = LegacyKeyValueFinder(store: self.identityManager.ownIdentityKeyValueStore, transaction: yapTransaction) - - sessionStoreFinder = LegacyKeyValueFinder(store: self.sessionStore.keyValueStore, transaction: yapTransaction) - queuedVerificationStateSyncMessagesFinder = LegacyKeyValueFinder(store: self.identityManager.queuedVerificationStateSyncMessagesKeyValueStore, transaction: yapTransaction) - tsAccountManagerFinder = LegacyKeyValueFinder(store: self.tsAccountManager.keyValueStore, transaction: yapTransaction) + migrators += self.allKeyValueMigrators(yapTransaction: yapTransaction) // unordered finders attachmentFinder = LegacyUnorderedFinder(transaction: yapTransaction) @@ -140,17 +151,10 @@ extension OWS115GRDBMigration { return SSKMessageDecryptJobRecord(envelopeData: legacyJob.envelopeData, label: SSKMessageDecryptJobQueue.jobRecordLabel) } - // migrate keyvalue stores - try! self.migrateKeyValueStore(label: "sessionStore", finder: sessionStoreFinder, memorySamplerRatio: 1.0, transaction: grdbTransaction) - - try! self.migrateKeyValueStore(label: "ownIdentity", finder: ownIdentityFinder, memorySamplerRatio: 1.0, transaction: grdbTransaction) - try! self.migrateKeyValueStore(label: "queuedVerificationStateSyncMessages", finder: queuedVerificationStateSyncMessagesFinder, memorySamplerRatio: 0.3, transaction: grdbTransaction) - try! self.migrateKeyValueStore(label: "tsAccountManager", finder: tsAccountManagerFinder, memorySamplerRatio: 0.3, transaction: grdbTransaction) - - try! self.migrateKeyValueStore(label: "preKey Store", finder: preKeyStoreFinder, memorySamplerRatio: 0.3, transaction: grdbTransaction) - try! self.migrateKeyValueStore(label: "preKey Metadata", finder: preKeyMetadataFinder, memorySamplerRatio: 0.3, transaction: grdbTransaction) - try! self.migrateKeyValueStore(label: "signedPreKey Store", finder: signedPreKeyStoreFinder, memorySamplerRatio: 0.3, transaction: grdbTransaction) - try! self.migrateKeyValueStore(label: "signedPreKey Metadata", finder: signedPreKeyMetadataFinder, memorySamplerRatio: 0.3, transaction: grdbTransaction) + // Migrate migrators + for migrator in migrators { + try! migrator.migrate(grdbTransaction: grdbTransaction) + } // unordered migrations try! self.migrateUnorderedRecords(label: "threads", finder: threadFinder, memorySamplerRatio: 0.2, transaction: grdbTransaction) @@ -163,6 +167,24 @@ extension OWS115GRDBMigration { } } + private func allKeyValueMigrators(yapTransaction: YapDatabaseReadTransaction) -> [GRDBMigrator] { + return [ + GRDBKeyValueStoreMigrator(label: "preKey Store", keyStore: preKeyStore.keyStore, yapTransaction: yapTransaction, memorySamplerRatio: 0.3), + GRDBKeyValueStoreMigrator(label: "preKey Metadata", keyStore: preKeyStore.metadataStore, yapTransaction: yapTransaction, memorySamplerRatio: 0.3), + + GRDBKeyValueStoreMigrator(label: "signedPreKey Store", keyStore: signedPreKeyStore.keyStore, yapTransaction: yapTransaction, memorySamplerRatio: 0.3), + GRDBKeyValueStoreMigrator(label: "signedPreKey Metadata", keyStore: signedPreKeyStore.metadataStore, yapTransaction: yapTransaction, memorySamplerRatio: 0.3), + + GRDBKeyValueStoreMigrator(label: "ownIdentity", keyStore: identityManager.ownIdentityKeyValueStore, yapTransaction: yapTransaction, memorySamplerRatio: 1.0), + + GRDBKeyValueStoreMigrator<[Int: SessionRecord]>(label: "sessionStore", keyStore: sessionStore.keyValueStore, yapTransaction: yapTransaction, memorySamplerRatio: 1.0), + + GRDBKeyValueStoreMigrator(label: "queuedVerificationStateSyncMessages", keyStore: identityManager.queuedVerificationStateSyncMessagesKeyValueStore, yapTransaction: yapTransaction, memorySamplerRatio: 0.3), + + GRDBKeyValueStoreMigrator(label: "tsAccountManager", keyStore: tsAccountManager.keyValueStore, yapTransaction: yapTransaction, memorySamplerRatio: 0.3) + ] + } + private func migrateUnorderedRecords(label: String, finder: LegacyUnorderedFinder, memorySamplerRatio: Float, transaction: GRDBWriteTransaction) throws where T: SDSModel { try Bench(title: "Migrate \(label)", memorySamplerRatio: memorySamplerRatio) { memorySampler in var recordCount = 0