Skip to content

Commit

Permalink
feat: migrate storage to for better thread safety (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen authored Mar 15, 2024
1 parent 0bbd2e0 commit 2bbe919
Show file tree
Hide file tree
Showing 18 changed files with 709 additions and 107 deletions.
35 changes: 34 additions & 1 deletion Amplitude-Swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 54;
objectVersion = 52;
objects = {

/* Begin PBXAggregateTarget section */
Expand All @@ -21,6 +21,9 @@
/* End PBXAggregateTarget section */

/* Begin PBXBuildFile section */
3E281B8C2B967F19009D913B /* Diagonostics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E281B8B2B967F19009D913B /* Diagonostics.swift */; };
3E281B8E2B96833D009D913B /* DiagnosticsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E281B8D2B96833D009D913B /* DiagnosticsTests.swift */; };
3E281B912B9BCC14009D913B /* DispatchQueueHolder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E281B902B9BCC14009D913B /* DispatchQueueHolder.swift */; };
8EDEC02B99EE2092B567A61D /* ObjCIngestionMetadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8EDEC500EBDA8B813056E2DB /* ObjCIngestionMetadata.swift */; };
8EDEC1073A308B12B5CCD975 /* AnalyticsConnectorPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8EDECD39BAA97DD4320C0AA5 /* AnalyticsConnectorPlugin.swift */; };
8EDEC10C56FA7F7DEEB48B6F /* ObjCBaseEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8EDECCFD935A0C5A6FE85E87 /* ObjCBaseEvent.swift */; };
Expand Down Expand Up @@ -141,6 +144,9 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
3E281B8B2B967F19009D913B /* Diagonostics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Diagonostics.swift; sourceTree = "<group>"; };
3E281B8D2B96833D009D913B /* DiagnosticsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiagnosticsTests.swift; sourceTree = "<group>"; };
3E281B902B9BCC14009D913B /* DispatchQueueHolder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DispatchQueueHolder.swift; sourceTree = "<group>"; };
8EDEC0630C3B587334275D9B /* AmplitudeSessionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AmplitudeSessionTests.swift; sourceTree = "<group>"; };
8EDEC1160D95DC3F0E48DDF7 /* ObjCPlugin.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjCPlugin.swift; sourceTree = "<group>"; };
8EDEC1576C95A2EB2FEF00A8 /* ObjCAmplitude.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjCAmplitude.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -415,6 +421,8 @@
BA9BEA4A299FB43B00BC0F7C /* IdentifyInterceptor.swift */,
8EDEC54AB4DF9E1074C3D6A4 /* Weak.swift */,
D010435E2B6C59EE00F8173C /* SandboxHelper.swift */,
3E281B8B2B967F19009D913B /* Diagonostics.swift */,
3E281B902B9BCC14009D913B /* DispatchQueueHolder.swift */,
);
path = Utilities;
sourceTree = "<group>";
Expand Down Expand Up @@ -522,6 +530,7 @@
BA9BEA4C299FB4BB00BC0F7C /* IdentifyInterceptorTests.swift */,
8EDEC4F83BFAA664749FAEF0 /* QueueTimeTests.swift */,
D01043602B6C5A8500F8173C /* SandboxHelperTests.swift */,
3E281B8D2B96833D009D913B /* DiagnosticsTests.swift */,
);
path = Utilities;
sourceTree = "<group>";
Expand All @@ -544,6 +553,7 @@
buildPhases = (
OBJ_87 /* Sources */,
OBJ_125 /* Frameworks */,
3E281B8F2B98EC92009D913B /* ShellScript */,
);
buildRules = (
);
Expand Down Expand Up @@ -633,6 +643,26 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
3E281B8F2B98EC92009D913B /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi\n";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
OBJ_130 /* Sources */ = {
isa = PBXSourcesBuildPhase;
Expand Down Expand Up @@ -662,6 +692,7 @@
OBJ_153 /* TimelineTests.swift in Sources */,
OBJ_154 /* TypesTests.swift in Sources */,
OBJ_155 /* EventPipelineTests.swift in Sources */,
3E281B8E2B96833D009D913B /* DiagnosticsTests.swift in Sources */,
B6F338A32B685793006179E2 /* NetworkConnectivityCheckerPluginTests.swift in Sources */,
OBJ_156 /* HttpClientTests.swift in Sources */,
D01043612B6C5A8500F8173C /* SandboxHelperTests.swift in Sources */,
Expand Down Expand Up @@ -705,6 +736,7 @@
OBJ_110 /* State.swift in Sources */,
B6CCC6CD2B6B14510004B203 /* ObjCNetworkConnectivityCheckerPlugin.swift in Sources */,
OBJ_111 /* InMemoryStorage.swift in Sources */,
3E281B912B9BCC14009D913B /* DispatchQueueHolder.swift in Sources */,
OBJ_112 /* PersistentStorage.swift in Sources */,
BA9BEA4B299FB43B00BC0F7C /* IdentifyInterceptor.swift in Sources */,
BA0639F62A4DD491000F1CEE /* LegacyDatabaseStorage.swift in Sources */,
Expand All @@ -720,6 +752,7 @@
OBJ_122 /* QueueTimer.swift in Sources */,
OBJ_124 /* UrlExtension.swift in Sources */,
8EDECFCCF4219767F26210D6 /* Sessions.swift in Sources */,
3E281B8C2B967F19009D913B /* Diagonostics.swift in Sources */,
8EDECD602E181B3E2E85D4DF /* StoragePrefixMigration.swift in Sources */,
8EDEC8F8DD2CDCD6568512F8 /* RemnantDataMigration.swift in Sources */,
8EDEC977C03AA2676724F436 /* BasePlugins.swift in Sources */,
Expand Down
12 changes: 6 additions & 6 deletions Sources/Amplitude/Amplitude.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,12 @@ public class Amplitude {
}
configuration.loggerProvider.debug(message: "Running migrateApiKeyStorages")
if let persistentStorage = configuration.storageProvider as? PersistentStorage {
let apiKeyStorage = PersistentStorage(storagePrefix: "\(PersistentStorage.DEFAULT_STORAGE_PREFIX)-\(configuration.apiKey)")
let apiKeyStorage = PersistentStorage(storagePrefix: "\(PersistentStorage.DEFAULT_STORAGE_PREFIX)-\(configuration.apiKey)", logger: self.logger, diagonostics: configuration.diagonostics)
StoragePrefixMigration(source: apiKeyStorage, destination: persistentStorage, logger: logger).execute()
}

if let persistentIdentifyStorage = configuration.identifyStorageProvider as? PersistentStorage {
let apiKeyIdentifyStorage = PersistentStorage(storagePrefix: "\(PersistentStorage.DEFAULT_STORAGE_PREFIX)-identify-\(configuration.apiKey)")
let apiKeyIdentifyStorage = PersistentStorage(storagePrefix: "\(PersistentStorage.DEFAULT_STORAGE_PREFIX)-identify-\(configuration.apiKey)", logger: self.logger, diagonostics: configuration.diagonostics)
StoragePrefixMigration(source: apiKeyIdentifyStorage, destination: persistentIdentifyStorage, logger: logger).execute()
}
}
Expand All @@ -367,12 +367,12 @@ public class Amplitude {
configuration.loggerProvider.debug(message: "Running migrateDefaultInstanceStorages")
let legacyDefaultInstanceName = "default_instance"
if let persistentStorage = configuration.storageProvider as? PersistentStorage {
let legacyStorage = PersistentStorage(storagePrefix: "storage-\(legacyDefaultInstanceName)")
let legacyStorage = PersistentStorage(storagePrefix: "storage-\(legacyDefaultInstanceName)", logger: self.logger, diagonostics: configuration.diagonostics)
StoragePrefixMigration(source: legacyStorage, destination: persistentStorage, logger: logger).execute()
}

if let persistentIdentifyStorage = configuration.identifyStorageProvider as? PersistentStorage {
let legacyIdentifyStorage = PersistentStorage(storagePrefix: "identify-\(legacyDefaultInstanceName)")
let legacyIdentifyStorage = PersistentStorage(storagePrefix: "identify-\(legacyDefaultInstanceName)", logger: self.logger, diagonostics: configuration.diagonostics)
StoragePrefixMigration(source: legacyIdentifyStorage, destination: persistentIdentifyStorage, logger: logger).execute()
}
}
Expand All @@ -393,7 +393,7 @@ public class Amplitude {
let instanceName = configuration.getNormalizeInstanceName()
if let persistentStorage = configuration.storageProvider as? PersistentStorage {
let instanceOnlyEventPrefix = "\(PersistentStorage.DEFAULT_STORAGE_PREFIX)-storage-\(instanceName)"
let instanceNameOnlyStorage = PersistentStorage(storagePrefix: instanceOnlyEventPrefix)
let instanceNameOnlyStorage = PersistentStorage(storagePrefix: instanceOnlyEventPrefix, logger: self.logger, diagonostics: configuration.diagonostics)
StoragePrefixMigration(
source: instanceNameOnlyStorage,
destination: persistentStorage,
Expand All @@ -403,7 +403,7 @@ public class Amplitude {

if let persistentIdentifyStorage = configuration.identifyStorageProvider as? PersistentStorage {
let instanceOnlyIdentifyPrefix = "\(PersistentStorage.DEFAULT_STORAGE_PREFIX)-identify-\(instanceName)"
let instanceNameOnlyIdentifyStorage = PersistentStorage(storagePrefix: instanceOnlyIdentifyPrefix)
let instanceNameOnlyIdentifyStorage = PersistentStorage(storagePrefix: instanceOnlyIdentifyPrefix, logger: self.logger, diagonostics: configuration.diagonostics)
StoragePrefixMigration(
source: instanceNameOnlyIdentifyStorage,
destination: persistentIdentifyStorage,
Expand Down
10 changes: 6 additions & 4 deletions Sources/Amplitude/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class Configuration {
public internal(set) var migrateLegacyData: Bool
public var defaultTracking: DefaultTrackingOptions
public var offline: Bool?
internal let diagonostics: Diagnostics

public init(
apiKey: String,
Expand Down Expand Up @@ -71,12 +72,13 @@ public class Configuration {
self.flushIntervalMillis = flushIntervalMillis
self.instanceName = normalizedInstanceName
self.optOut = optOut
self.storageProvider = storageProvider
?? PersistentStorage(storagePrefix: PersistentStorage.getEventStoragePrefix(apiKey, normalizedInstanceName))
self.identifyStorageProvider = identifyStorageProvider
?? PersistentStorage(storagePrefix: PersistentStorage.getIdentifyStoragePrefix(apiKey, normalizedInstanceName))
self.diagonostics = Diagnostics()
self.logLevel = logLevel
self.loggerProvider = loggerProvider
self.storageProvider = storageProvider
?? PersistentStorage(storagePrefix: PersistentStorage.getEventStoragePrefix(apiKey, normalizedInstanceName), logger: self.loggerProvider, diagonostics: self.diagonostics)
self.identifyStorageProvider = identifyStorageProvider
?? PersistentStorage(storagePrefix: PersistentStorage.getIdentifyStoragePrefix(apiKey, normalizedInstanceName), logger: self.loggerProvider, diagonostics: self.diagonostics)
self.minIdLength = minIdLength
self.partnerId = partnerId
self.callback = callback
Expand Down
Loading

0 comments on commit 2bbe919

Please sign in to comment.