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

Batch writing of records to SQLite store #498

Merged
merged 1 commit into from
Oct 1, 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
15 changes: 13 additions & 2 deletions apollo-ios/Sources/ApolloSQLite/SQLiteDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,25 @@ public protocol SQLiteDatabase {

func selectRawRows(forKeys keys: Set<CacheKey>) throws -> [DatabaseRow]

func addOrUpdateRecordString(_ recordString: String, for cacheKey: CacheKey) throws
func addOrUpdate(records: [(cacheKey: CacheKey, recordString: String)]) throws

func deleteRecord(for cacheKey: CacheKey) throws

func deleteRecords(matching pattern: CacheKey) throws

func clearDatabase(shouldVacuumOnClear: Bool) throws

@available(*, deprecated, renamed: "addOrUpdate(records:)")
func addOrUpdateRecordString(_ recordString: String, for cacheKey: CacheKey) throws

}

extension SQLiteDatabase {

public func addOrUpdateRecordString(_ recordString: String, for cacheKey: CacheKey) throws {
try addOrUpdate(records: [(cacheKey, recordString)])
}

}

public extension SQLiteDatabase {
Expand Down
14 changes: 10 additions & 4 deletions apollo-ios/Sources/ApolloSQLite/SQLiteDotSwiftDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ public final class SQLiteDotSwiftDatabase: SQLiteDatabase {
return DatabaseRow(cacheKey: key, storedInfo: record)
}
}

public func addOrUpdateRecordString(_ recordString: String, for cacheKey: CacheKey) throws {
try self.db.run(self.records.insert(or: .replace, self.keyColumn <- cacheKey, self.recordColumn <- recordString))

public func addOrUpdate(records: [(cacheKey: CacheKey, recordString: String)]) throws {
guard !records.isEmpty else { return }

let setters = records.map {
[self.keyColumn <- $0.cacheKey, self.recordColumn <- $0.recordString]
}

try self.db.run(self.records.insertMany(or: .replace, setters))
}

public func deleteRecord(for cacheKey: CacheKey) throws {
let query = self.records.filter(keyColumn == cacheKey)
try self.db.run(query.delete())
Expand Down
22 changes: 13 additions & 9 deletions apollo-ios/Sources/ApolloSQLite/SQLiteNormalizedCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,21 @@ public final class SQLiteNormalizedCache {
var recordSet = RecordSet(records: try self.selectRecords(for: records.keys))
let changedFieldKeys = recordSet.merge(records: records)
let changedRecordKeys = changedFieldKeys.map { self.recordCacheKey(forFieldCacheKey: $0) }
for recordKey in Set(changedRecordKeys) {
if let recordFields = recordSet[recordKey]?.fields {
let recordData = try SQLiteSerialization.serialize(fields: recordFields)
guard let recordString = String(data: recordData, encoding: .utf8) else {
assertionFailure("Serialization should yield UTF-8 data")
continue

let serializedRecords = try Set(changedRecordKeys)
.compactMap { recordKey -> (CacheKey, String)? in
if let recordFields = recordSet[recordKey]?.fields {
let recordData = try SQLiteSerialization.serialize(fields: recordFields)
guard let recordString = String(data: recordData, encoding: .utf8) else {
assertionFailure("Serialization should yield UTF-8 data")
return nil
}
return (recordKey, recordString)
}

try self.database.addOrUpdateRecordString(recordString, for: recordKey)
return nil
}
}

try self.database.addOrUpdate(records: serializedRecords)
return Set(changedFieldKeys)
}

Expand Down
Loading