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

Initial CoreData implementation in EmbraceUploadCache #151

Merged
merged 8 commits into from
Feb 5, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,16 @@
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "EmbraceCoreDataInternalTests"
BuildableName = "EmbraceCoreDataInternalTests"
BlueprintName = "EmbraceCoreDataInternalTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
17 changes: 17 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ let package = Package(
dependencies: [
"EmbraceCommonInternal",
"EmbraceOTelInternal",
"EmbraceCoreDataInternal",
.product(name: "GRDB", package: "GRDB.swift")
]
),
Expand All @@ -216,6 +217,22 @@ let package = Package(
dependencies: [
"EmbraceUploadInternal",
"EmbraceOTelInternal",
"EmbraceCoreDataInternal",
"TestSupport"
]
),

// core data -----------------------------------------------------------------
.target(
name: "EmbraceCoreDataInternal",
dependencies: [
"EmbraceCommonInternal"
]
),
.testTarget(
name: "EmbraceCoreDataInternalTests",
dependencies: [
"EmbraceCommonInternal",
"TestSupport"
]
),
Expand Down
51 changes: 51 additions & 0 deletions Sources/EmbraceCommonInternal/Storage/StorageMechanism.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// Copyright © 2025 Embrace Mobile, Inc. All rights reserved.
//

import Foundation

public enum StorageMechanism {
case inMemory(name: String)
case onDisk(name: String, baseURL: URL)
}

public extension StorageMechanism {

/// Name identifier
var name: String {
switch self {
case .onDisk(let name, _): return name
case .inMemory(let name): return name
}
}

/// URL pointing to the folder where the storage will be saved
var baseUrl: URL? {
switch self {
case .onDisk(_, let url):
return url

default: return nil
}
}

/// URL pointing to the folder where the storage will be saved
var fileName: String? {
switch self {
case .onDisk(let name, _):
return name + ".sqlite"

default: return nil
}
}

/// URL to the storage file
var fileURL: URL? {
switch self {
case .onDisk(let name, let url):
return url.appendingPathComponent(name + ".sqlite")

default: return nil
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ extension RemoteConfig {
/// - digits: The number of digits used to calculate the total space. Must match the number of digits used to determine the hexValue
/// - threshold: The percentage threshold to test against. Values between 0.0 and 100.0
static func isEnabled(hexValue: UInt64, digits: UInt, threshold: Float) -> Bool {
if threshold <= 0 || threshold > 100 {
guard threshold > 0 else {
return false
}

let space = powf(16, Float(digits)) - 1
let result = (Float(hexValue) / space) * 100

return result <= threshold
return result <= min(100, threshold)
}
}
12 changes: 8 additions & 4 deletions Sources/EmbraceCore/Internal/Embrace+Setup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@ extension Embrace {
guard let cacheUrl = EmbraceFileSystem.uploadsDirectoryPath(
partitionIdentifier: appId,
appGroupId: options.appGroupId
),
let cache = EmbraceUpload.CacheOptions(cacheBaseUrl: cacheUrl)
else {
) else {
Embrace.logger.error("Failed to initialize upload cache!")
return nil
}

let storageMechanism = StorageMechanism.onDisk(
name: "EmbraceUploadStorage",
baseURL: cacheUrl
)
let cache = EmbraceUpload.CacheOptions(storageMechanism: storageMechanism)

// metadata
let metadata = EmbraceUpload.MetadataOptions(
apiKey: appId,
Expand All @@ -72,7 +76,7 @@ extension Embrace {

do {
let options = EmbraceUpload.Options(endpoints: uploadEndpoints, cache: cache, metadata: metadata)
let queue = DispatchQueue(label: "com.embrace.upload", attributes: .concurrent)
let queue = DispatchQueue(label: "com.embrace.upload", qos: .background, attributes: .concurrent)

return try EmbraceUpload(options: options, logger: Embrace.logger, queue: queue)
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,7 @@ extension MetadataHandler {
private func update(key: UserResourceKey, value: String?) {
if let value = value {
do {
let record = MetadataRecord(
key: key.rawValue,
value: .string(value),
type: .customProperty,
lifespan: .permanent,
lifespanId: MetadataRecord.lifespanIdForPermanent
)

_ = try storage?.addMetadata(record)
try addProperty(key: key.rawValue, value: value, lifespan: .permanent)
} catch {
Embrace.logger.warning("Unable to update user metadata!")
}
Expand Down
67 changes: 66 additions & 1 deletion Sources/EmbraceCore/Public/Metadata/MetadataHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import Foundation
import EmbraceCommonInternal
import EmbraceStorageInternal
import EmbraceCoreDataInternal
import CoreData

@objc public enum MetadataLifespan: Int {
/// The resource will be removed when the session ends.
Expand All @@ -26,9 +28,36 @@ public class MetadataHandler: NSObject {
weak var storage: EmbraceStorage?
weak var sessionController: SessionControllable?

let coreData: CoreDataWrapper?

init(storage: EmbraceStorage?, sessionController: SessionControllable?) {
self.storage = storage
self.sessionController = sessionController

// tmp core data stack
let coreDataStackName = "EmbraceMetadataTmp"
var storageMechanism: StorageMechanism = .inMemory(name: coreDataStackName) // in memory only used for tests

if let storage = storage,
let url = storage.options.baseUrl {
storageMechanism = .onDisk(name: coreDataStackName, baseURL: url)
}

let options = CoreDataWrapper.Options(
storageMechanism: storageMechanism,
entities: [MetadataRecordTmp.entityDescription]
)

do {
self.coreData = try CoreDataWrapper(options: options, logger: Embrace.logger)
} catch {
Embrace.logger.error("Error setting up temp metadata database!:\n\(error.localizedDescription)")
self.coreData = nil
}

super.init()

cloneDataBase()
}

/// Adds a resource with the given key, value and lifespan.
Expand Down Expand Up @@ -111,7 +140,8 @@ public class MetadataHandler: NSObject {
key: key,
value: validateValue(value),
type: type,
lifespan: lifespan.recordLifespan
lifespan: lifespan.recordLifespan,
lifespanId: try currentContext(for: lifespan.recordLifespan)
)
}

Expand Down Expand Up @@ -205,3 +235,38 @@ extension MetadataLifespan {
}
}
}

// tmp core data stack
extension MetadataHandler {
func cloneDataBase() {
guard let coreData = coreData,
let storage = storage else {
return
}

let request = NSFetchRequest<MetadataRecordTmp>(entityName: MetadataRecordTmp.entityName)
let oldRecords = coreData.fetch(withRequest: request)
coreData.deleteRecords(oldRecords)

do {
var newRecords: [MetadataRecord] = []
try storage.dbQueue.read { db in
newRecords = try MetadataRecord.fetchAll(db)
}

coreData.context.perform {
for record in newRecords {
_ = MetadataRecordTmp.create(context: coreData.context, record: record)
}

do {
try coreData.context.save()
} catch {
Embrace.logger.error("Error saving metadata core data!:\n\(error.localizedDescription)")
}
}
} catch {
Embrace.logger.error("Error cloning metadata!:\n\(error.localizedDescription)")
}
}
}
86 changes: 86 additions & 0 deletions Sources/EmbraceCore/Public/Metadata/MetadataRecordTmp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// Copyright © 2024 Embrace Mobile, Inc. All rights reserved.
//

import Foundation
import EmbraceCommonInternal
import EmbraceStorageInternal
import CoreData

public class MetadataRecordTmp: NSManagedObject {
@NSManaged var key: String
@NSManaged var value: String
@NSManaged var type: String
@NSManaged var lifespan: String
@NSManaged var lifespanId: String
@NSManaged var collectedAt: Date

class func create(
context: NSManagedObjectContext,
key: String,
value: String,
type: String,
lifespan: String,
lifespanId: String,
collectedAt: Date = Date()
) -> MetadataRecordTmp {
let record = MetadataRecordTmp(context: context)
record.key = key
record.value = value
record.type = type
record.lifespan = lifespan
record.lifespanId = lifespanId
record.collectedAt = collectedAt

return record
}

class func create(context: NSManagedObjectContext, record: MetadataRecord) -> MetadataRecordTmp {
return create(
context: context,
key: record.key,
value: record.value.description,
type: record.type.rawValue,
lifespan: record.lifespan.rawValue,
lifespanId: record.lifespanId,
collectedAt: record.collectedAt
)
}
}

extension MetadataRecordTmp {
static let entityName = "MetadataRecordTmp"

static var entityDescription: NSEntityDescription {
let entity = NSEntityDescription()
entity.name = entityName
entity.managedObjectClassName = NSStringFromClass(MetadataRecordTmp.self)

let keyAttribute = NSAttributeDescription()
keyAttribute.name = "key"
keyAttribute.attributeType = .stringAttributeType

let valueAttribute = NSAttributeDescription()
valueAttribute.name = "value"
valueAttribute.attributeType = .stringAttributeType

let typeAttribute = NSAttributeDescription()
typeAttribute.name = "type"
typeAttribute.attributeType = .stringAttributeType

let lifespanAttribute = NSAttributeDescription()
lifespanAttribute.name = "lifespan"
lifespanAttribute.attributeType = .stringAttributeType

let lifespanIdAttribute = NSAttributeDescription()
lifespanIdAttribute.name = "lifespanId"
lifespanIdAttribute.attributeType = .stringAttributeType

let dateAttribute = NSAttributeDescription()
dateAttribute.name = "collectedAt"
dateAttribute.attributeType = .dateAttributeType

entity.properties = [keyAttribute, valueAttribute, typeAttribute, lifespanAttribute, lifespanIdAttribute, dateAttribute]
return entity
}
}
26 changes: 26 additions & 0 deletions Sources/EmbraceCoreDataInternal/CoreDataWrapper+Options.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Copyright © 2025 Embrace Mobile, Inc. All rights reserved.
//

import Foundation
import CoreData
import EmbraceCommonInternal

public extension CoreDataWrapper {

class Options {
/// Determines where the db is going to be stored
let storageMechanism: StorageMechanism

/// Array on NSEntityDescriptions that define the db model
let entities: [NSEntityDescription]

public init(
storageMechanism: StorageMechanism,
entities: [NSEntityDescription]
) {
self.storageMechanism = storageMechanism
self.entities = entities
}
}
}
Loading
Loading