Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MLIBZ-2689: allow cache to be compacted during launch #327

Merged
merged 5 commits into from Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .codebeatsettings
@@ -0,0 +1,6 @@
{
"SWIFT": {
"ARITY": [8, 10, 14, 20],
"TOO_MANY_IVARS": [8, 10, 14, 20],
}
}
32 changes: 29 additions & 3 deletions Kinvey/Kinvey/Client.swift
Expand Up @@ -165,6 +165,7 @@ open class Client: Credential {
apiHostName: URL = Client.defaultApiHostName,
authHostName: URL = Client.defaultAuthHostName,
schema: Schema? = nil,
compactCacheOnLaunch: Bool = true,
options: Options? = nil,
completionHandler: ((Result<U?, Swift.Error>) -> Void)? = nil
) {
Expand All @@ -176,6 +177,7 @@ open class Client: Credential {
apiHostName: apiHostName,
authHostName: authHostName,
schema: schema,
compactCacheOnLaunch: compactCacheOnLaunch,
options: options
) { (result: Result<U?, Swift.Error>) in
completionHandler?(result)
Expand Down Expand Up @@ -249,6 +251,7 @@ open class Client: Credential {
authHostName: URL = Client.defaultAuthHostName,
encrypted: Bool,
schema: Schema? = nil,
compactCacheOnLaunch: Bool = true,
options: Options? = nil,
completionHandler: @escaping (Result<U?, Swift.Error>) -> Void
) {
Expand All @@ -269,22 +272,34 @@ open class Client: Credential {
authHostName: authHostName,
encryptionKey: encryptionKey,
schema: Schema(version: schema?.version ?? 0, migrationHandler: schema?.migrationHandler),
compactCacheOnLaunch: compactCacheOnLaunch,
options: options,
completionHandler: completionHandler
)
}

/// Initialize a `Client` instance with all the needed parameters.
@available(*, deprecated: 3.17.0, message: "Please use Client.initialize(appKey:appSecret:accessGroup:apiHostName:authHostName:encryptionKey:schema:completionHandler:(Result<U?, Swift.Error>) -> Void) instead")
open func initialize<U: User>(appKey: String, appSecret: String, accessGroup: String? = nil, apiHostName: URL = Client.defaultApiHostName, authHostName: URL = Client.defaultAuthHostName, encryptionKey: Data? = nil, schema: Schema? = nil, completionHandler: @escaping User.UserHandler<U>) {
open func initialize<U: User>(
appKey: String,
appSecret: String,
accessGroup: String? = nil,
apiHostName: URL = Client.defaultApiHostName,
authHostName: URL = Client.defaultAuthHostName,
encryptionKey: Data? = nil,
schema: Schema? = nil,
compactCacheOnLaunch: Bool = true,
completionHandler: @escaping User.UserHandler<U>
) {
initialize(
appKey: appKey,
appSecret: appSecret,
accessGroup: accessGroup,
apiHostName: apiHostName,
authHostName: authHostName,
encryptionKey: encryptionKey,
schema: schema
schema: schema,
compactCacheOnLaunch: compactCacheOnLaunch
) { (result: Result<U?, Swift.Error>) in
switch result {
case .success(let user):
Expand Down Expand Up @@ -314,6 +329,7 @@ open class Client: Credential {
accessGroup: String? = nil,
encrypted: Bool,
schema: Schema? = nil,
compactCacheOnLaunch: Bool = true,
options: Options? = nil,
completionHandler: @escaping (Result<U?, Swift.Error>) -> Void
) {
Expand Down Expand Up @@ -344,6 +360,7 @@ open class Client: Credential {
authHostName: authHostName,
encryptionKey: encryptionKey,
schema: schema,
compactCacheOnLaunch: compactCacheOnLaunch,
options: options,
completionHandler: completionHandler
)
Expand All @@ -368,6 +385,7 @@ open class Client: Credential {
accessGroup: String? = nil,
encryptionKey: Data? = nil,
schema: Schema? = nil,
compactCacheOnLaunch: Bool = true,
options: Options? = nil,
completionHandler: @escaping (Result<U?, Swift.Error>) -> Void
) {
Expand All @@ -389,6 +407,7 @@ open class Client: Credential {
authHostName: authHostName,
encryptionKey: encryptionKey,
schema: schema,
compactCacheOnLaunch: compactCacheOnLaunch,
options: options,
completionHandler: completionHandler
)
Expand All @@ -407,6 +426,7 @@ open class Client: Credential {
authHostName: URL = Client.defaultAuthHostName,
encryptionKey: Data? = nil,
schema: Schema? = nil,
compactCacheOnLaunch: Bool = true,
options: Options? = nil,
completionHandler: @escaping (Result<U?, Swift.Error>) -> Void
) {
Expand All @@ -421,7 +441,13 @@ open class Client: Credential {
self.schemaVersion = schema?.version ?? 0
self.options = options

Migration.performMigration(persistenceId: appKey, encryptionKey: encryptionKey, schemaVersion: schemaVersion, migrationHandler: schema?.migrationHandler)
Migration.performMigration(
persistenceId: appKey,
encryptionKey: encryptionKey,
schemaVersion: schemaVersion,
migrationHandler: schema?.migrationHandler,
compactCacheOnLaunch: compactCacheOnLaunch
)

cacheManager = CacheManager(persistenceId: appKey, encryptionKey: encryptionKey as Data?, schemaVersion: schemaVersion)
syncManager = SyncManager(persistenceId: appKey, encryptionKey: encryptionKey as Data?, schemaVersion: schemaVersion)
Expand Down
33 changes: 24 additions & 9 deletions Kinvey/Kinvey/Migration.swift
Expand Up @@ -24,8 +24,14 @@ open class Migration: NSObject {
self.realmMigration = realmMigration
}

internal class func performMigration(persistenceId: String, encryptionKey: Data? = nil, schemaVersion: CUnsignedLongLong = 0, migrationHandler: Migration.MigrationHandler? = nil) {
var realmBaseConfiguration = Realm.Configuration()
internal class func performMigration(
persistenceId: String,
encryptionKey: Data? = nil,
schemaVersion: CUnsignedLongLong = 0,
migrationHandler: Migration.MigrationHandler? = nil,
compactCacheOnLaunch: Bool = true
) {
var realmBaseConfiguration = Realm.Configuration.defaultConfiguration
if let encryptionKey = encryptionKey {
realmBaseConfiguration.encryptionKey = encryptionKey
}
Expand All @@ -38,23 +44,32 @@ open class Migration: NSObject {
} else {
realmBaseConfiguration.deleteRealmIfMigrationNeeded = true
}
realmBaseConfiguration.shouldCompactOnLaunch = { totalBytes, usedBytes in
log.debug("Cache: \(usedBytes) bytes used in a total of \(totalBytes) bytes. Compact Cache on Launch: \(compactCacheOnLaunch)")
return compactCacheOnLaunch
}
let baseFolderURL = Client.fileURL(appKey: persistenceId).deletingLastPathComponent()
let fileManager = FileManager.default
if let allFilesURL = try? fileManager.contentsOfDirectory(at: baseFolderURL, includingPropertiesForKeys: nil) {
for realmFileURL in allFilesURL.filter({ $0.lastPathComponent.hasSuffix(".realm") }) {
var realmConfiguration = realmBaseConfiguration //copy
realmConfiguration.fileURL = realmFileURL
do {
try Realm.performMigration(for: realmConfiguration)
} catch {
log.error("Database migration failed: deleting local database.\nDetails of the failure: \(error)")
realmConfiguration.deleteRealmIfMigrationNeeded = true
try! Realm.performMigration(for: realmConfiguration)
}
performMigration(realmConfiguration: realmConfiguration)
}
}
}

private class func performMigration(realmConfiguration: Realm.Configuration) {
var realmConfiguration = realmConfiguration //copy
do {
try Realm.performMigration(for: realmConfiguration)
} catch {
log.error("Database migration failed: deleting local database.\nDetails of the failure: \(error)")
realmConfiguration.deleteRealmIfMigrationNeeded = true
try! Realm.performMigration(for: realmConfiguration)
}
}

/// Method that performs a migration in a specific collection.
open func execute<T: Entity>(_ type: T.Type, oldClassName: String? = nil, migrationObjectHandler: MigrationObjectHandler? = nil) {
let className = type.className()
Expand Down