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

Added option to use InMemory CoreData for logs #247

Merged
merged 1 commit into from
Apr 13, 2024
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
7 changes: 7 additions & 0 deletions Sources/Pulse/LoggerStore/LoggerStore+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ extension LoggerStore {
/// Opens store in a readonly mode. It won't perform sweeps and will
/// disallow any other modifications.
public static let readonly = Options(rawValue: 1 << 3)

/// Logs are not persistent on the device storage and new log file is created on each app session.
/// Core Data is configured with container type as `NSInMemoryStoreType
///
/// - warning: This options is not recommended for general use. Use it only when persistent logs do not suite
/// your use case, eg. for security reasons you can not store user logs on device storage
public static let inMemory = Options(rawValue: 1 << 4)
}

/// The store configuration.
Expand Down
5 changes: 3 additions & 2 deletions Sources/Pulse/LoggerStore/LoggerStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public final class LoggerStore: @unchecked Sendable, Identifiable {
self.manifest = .init(storeId: info.storeId, version: try Version(string: info.storeVersion))
}

self.container = LoggerStore.makeContainer(databaseURL: databaseURL)
self.container = LoggerStore.makeContainer(databaseURL: databaseURL, options: options)
try container.loadStore()
self.backgroundContext = container.newBackgroundContext()

Expand Down Expand Up @@ -245,10 +245,11 @@ public final class LoggerStore: @unchecked Sendable, Identifiable {
self.configuration = .init()
}

private static func makeContainer(databaseURL: URL) -> NSPersistentContainer {
private static func makeContainer(databaseURL: URL, options: Options) -> NSPersistentContainer {
let container = NSPersistentContainer(name: databaseURL.lastPathComponent, managedObjectModel: Self.model)
let store = NSPersistentStoreDescription(url: databaseURL)
store.setValue("DELETE" as NSString, forPragmaNamed: "journal_mode")
store.type = options.contains(.inMemory) ? NSInMemoryStoreType : NSSQLiteStoreType
container.persistentStoreDescriptions = [store]
return container
}
Expand Down
17 changes: 17 additions & 0 deletions Tests/PulseTests/LoggerStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ final class LoggerStoreTests: LoggerStoreBaseTests {
try? secondStore.destroy()
}

func testInitCreateInMemoryStore() throws {
// GIVEN
let storeURL = directory.url.appending(filename: UUID().uuidString)
let options: LoggerStore.Options = [.create, .synchronous, .inMemory]

// WHEN
let store = try LoggerStore(storeURL: storeURL, options: options)
populate(store: store)

// THEN data is NOT persisted
let databaseURL = storeURL.appending(filename: "logs.sqlite")
XCTAssertFalse(FileManager.default.fileExists(atPath: databaseURL.path))

// CLEANUP
try? store.destroy()
}

func testInitCreateStoreIntermediateDirectoryMissing() throws {
// GIVEN
let storeURL = directory.url
Expand Down
Loading