From 2c58bd31d7f89b19b15e41eb925aae6e792d6e36 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 29 Nov 2019 21:12:16 -0800 Subject: [PATCH 1/3] Update to latest RSDatabase. --- submodules/RSDatabase | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/submodules/RSDatabase b/submodules/RSDatabase index 807bb445f..573bf1d99 160000 --- a/submodules/RSDatabase +++ b/submodules/RSDatabase @@ -1 +1 @@ -Subproject commit 807bb445f0bc692cb6b9210f66db82a85efe5c59 +Subproject commit 573bf1d99418b218d264105df620163832c36c34 From 6fdaf773cdb3f62a4b7df8821e2fac7707a8488d Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 29 Nov 2019 21:49:24 -0800 Subject: [PATCH 2/3] Add SyncDatabase to referenced frameworks in project settings. --- Frameworks/Account/Account.xcodeproj/project.pbxproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Frameworks/Account/Account.xcodeproj/project.pbxproj b/Frameworks/Account/Account.xcodeproj/project.pbxproj index e9264a54c..8775622f4 100644 --- a/Frameworks/Account/Account.xcodeproj/project.pbxproj +++ b/Frameworks/Account/Account.xcodeproj/project.pbxproj @@ -48,6 +48,7 @@ 844B297F210CE37E004020B3 /* UnreadCountProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 844B297E210CE37E004020B3 /* UnreadCountProvider.swift */; }; 844B2981210CE3BF004020B3 /* RSWeb.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 844B2980210CE3BF004020B3 /* RSWeb.framework */; }; 8469F81C1F6DD15E0084783E /* Account.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848935101F62486800CEBD24 /* Account.swift */; }; + 846CA1882392349E00B55117 /* SyncDatabase.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 846CA1872392349E00B55117 /* SyncDatabase.framework */; }; 846E77451F6EF9B900A165E2 /* Container.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8419740D1F6DD25F006346C4 /* Container.swift */; }; 846E774F1F6EF9C000A165E2 /* LocalAccountDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8419742C1F6DDE84006346C4 /* LocalAccountDelegate.swift */; }; 846E77501F6EF9C400A165E2 /* LocalAccountRefresher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8419742D1F6DDE96006346C4 /* LocalAccountRefresher.swift */; }; @@ -153,6 +154,7 @@ 844B297C2106C7EC004020B3 /* Feed.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Feed.swift; sourceTree = ""; }; 844B297E210CE37E004020B3 /* UnreadCountProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnreadCountProvider.swift; sourceTree = ""; }; 844B2980210CE3BF004020B3 /* RSWeb.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = RSWeb.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 846CA1872392349E00B55117 /* SyncDatabase.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SyncDatabase.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 846E77531F6F00E300A165E2 /* AccountManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountManager.swift; sourceTree = ""; }; 848934F61F62484F00CEBD24 /* Account.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Account.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 848934FA1F62484F00CEBD24 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -184,6 +186,7 @@ 844B2981210CE3BF004020B3 /* RSWeb.framework in Frameworks */, 841D4D722106B40A00DD04E6 /* Articles.framework in Frameworks */, 841D4D702106B40400DD04E6 /* ArticlesDatabase.framework in Frameworks */, + 846CA1882392349E00B55117 /* SyncDatabase.framework in Frameworks */, 841973FE1F6DD1BC006346C4 /* RSCore.framework in Frameworks */, 841973FF1F6DD1C5006346C4 /* RSParser.framework in Frameworks */, ); @@ -285,6 +288,7 @@ 8469F80F1F6DC3C10084783E /* Frameworks */ = { isa = PBXGroup; children = ( + 846CA1872392349E00B55117 /* SyncDatabase.framework */, 84EAC4812148CC6300F154AB /* RSDatabase.framework */, 844B2980210CE3BF004020B3 /* RSWeb.framework */, 841D4D712106B40A00DD04E6 /* Articles.framework */, From 9dc3ed03c78b10dcc3b5ff937b116ffa0be96a38 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 29 Nov 2019 21:49:44 -0800 Subject: [PATCH 3/3] Adopt DatabaseQueue. --- .../ArticlesDatabase/ArticlesDatabase.swift | 29 ++++++++++-- .../ArticlesDatabase/ArticlesTable.swift | 30 ++++++------- Frameworks/ArticlesDatabase/SearchTable.swift | 6 +-- .../ArticlesDatabase/StatusesTable.swift | 6 +-- Frameworks/SyncDatabase/SyncDatabase.swift | 45 +++++++++++++------ Frameworks/SyncDatabase/SyncStatusTable.swift | 33 +++++--------- 6 files changed, 89 insertions(+), 60 deletions(-) diff --git a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift index e14dac4de..262d76ec2 100644 --- a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift +++ b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift @@ -22,21 +22,29 @@ public typealias UpdateArticlesCompletionBlock = (Set
?, Set
?) public final class ArticlesDatabase { + /// When ArticlesDatabase is suspended, database calls will crash the app. + public var isSuspended: Bool { + return queue.isSuspended + } + private let articlesTable: ArticlesTable + private let queue: DatabaseQueue public init(databaseFilePath: String, accountID: String) { - let queue = RSDatabaseQueue(filepath: databaseFilePath, excludeFromBackup: false) + let queue = DatabaseQueue(databasePath: databaseFilePath) + self.queue = queue self.articlesTable = ArticlesTable(name: DatabaseTableName.articles, accountID: accountID, queue: queue) - queue.createTables(usingStatements: ArticlesDatabase.tableCreationStatements) - queue.update { (database) in + queue.runCreateStatements(ArticlesDatabase.tableCreationStatements) + queue.runInDatabase { database in if !self.articlesTable.containsColumn("searchRowID", in: database) { database.executeStatements("ALTER TABLE articles add column searchRowID INTEGER;") } database.executeStatements("CREATE INDEX if not EXISTS articles_searchRowID on articles(searchRowID);") database.executeStatements("DROP TABLE if EXISTS tags;DROP INDEX if EXISTS tags_tagName_index;DROP INDEX if EXISTS articles_feedID_index;DROP INDEX if EXISTS statuses_read_index;") } - queue.vacuumIfNeeded() + + queue.vacuumIfNeeded(daysBetweenVacuums: 9) DispatchQueue.main.async { self.articlesTable.indexUnindexedArticles() } @@ -145,6 +153,19 @@ public final class ArticlesDatabase { return articlesTable.mark(articles, statusKey, flag) } + // MARK: - Suspend and Resume (for iOS) + + /// Close the database and stop running database calls. + /// Any pending calls will complete first. + public func suspend() { + queue.suspend() + } + + /// Open the database and allow for running database calls again. + public func resume() { + queue.resume() + } + // MARK: - Caches /// Call to free up some memory. Should be done when the app is backgrounded, for instance. diff --git a/Frameworks/ArticlesDatabase/ArticlesTable.swift b/Frameworks/ArticlesDatabase/ArticlesTable.swift index a0b6edbd2..ccb912ffa 100644 --- a/Frameworks/ArticlesDatabase/ArticlesTable.swift +++ b/Frameworks/ArticlesDatabase/ArticlesTable.swift @@ -16,7 +16,7 @@ final class ArticlesTable: DatabaseTable { let name: String private let accountID: String - private let queue: RSDatabaseQueue + private let queue: DatabaseQueue private let statusesTable: StatusesTable private let authorsLookupTable: DatabaseLookupTable private let attachmentsLookupTable: DatabaseLookupTable @@ -32,7 +32,7 @@ final class ArticlesTable: DatabaseTable { private typealias ArticlesFetchMethod = (FMDatabase) -> Set
- init(name: String, accountID: String, queue: RSDatabaseQueue) { + init(name: String, accountID: String, queue: DatabaseQueue) { self.name = name self.accountID = accountID @@ -149,7 +149,7 @@ final class ArticlesTable: DatabaseTable { func fetchArticlesMatching(_ searchString: String, _ feedIDs: Set) -> Set
{ var articles: Set
= Set
() - queue.fetchSync { (database) in + queue.runInDatabaseSync { (database) in articles = self.fetchArticlesMatching(searchString, database) } articles = articles.filter{ feedIDs.contains($0.feedID) } @@ -229,7 +229,7 @@ final class ArticlesTable: DatabaseTable { articleIDs.formUnion(parsedItems.articleIDs()) } - self.queue.update { (database) in + self.queue.runInTransaction { (database) in let statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, read, database) //1 assert(statusesDictionary.count == articleIDs.count) @@ -273,7 +273,7 @@ final class ArticlesTable: DatabaseTable { } func ensureStatuses(_ articleIDs: Set, _ defaultRead: Bool, _ statusKey: ArticleStatus.Key, _ flag: Bool) { - self.queue.update { (database) in + self.queue.runInTransaction { (database) in let statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, defaultRead, database) let statuses = Set(statusesDictionary.values) self.statusesTable.mark(statuses, statusKey, flag, database) @@ -290,7 +290,7 @@ final class ArticlesTable: DatabaseTable { var unreadCountDictionary = UnreadCountDictionary() - queue.fetch { (database) in + queue.runInDatabase { (database) in for feedID in feedIDs { unreadCountDictionary[feedID] = self.fetchUnreadCount(feedID, database) } @@ -309,7 +309,7 @@ final class ArticlesTable: DatabaseTable { return } - queue.fetch { (database) in + queue.runInDatabase { (database) in let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(feedIDs.count))! let sql = "select count(*) from articles natural join statuses where feedID in \(placeholders) and (datePublished > ? or (datePublished is null and dateArrived > ?)) and read=0 and userDeleted=0;" @@ -331,7 +331,7 @@ final class ArticlesTable: DatabaseTable { let cutoffDate = articleCutoffDate - queue.fetch { (database) in + queue.runInDatabase { (database) in let sql = "select distinct feedID, count(*) from articles natural join statuses where read=0 and userDeleted=0 and (starred=1 or dateArrived>?) group by feedID;" guard let resultSet = database.executeQuery(sql, withArgumentsIn: [cutoffDate]) else { @@ -361,7 +361,7 @@ final class ArticlesTable: DatabaseTable { return } - queue.fetch { (database) in + queue.runInDatabase { (database) in let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(feedIDs.count))! let sql = "select count(*) from articles natural join statuses where feedID in \(placeholders) and read=0 and starred=1 and userDeleted=0;" let parameters = Array(feedIDs) as [Any] @@ -390,7 +390,7 @@ final class ArticlesTable: DatabaseTable { func mark(_ articles: Set
, _ statusKey: ArticleStatus.Key, _ flag: Bool) -> Set? { var statuses: Set? - self.queue.updateSync { (database) in + self.queue.runInTransactionSync { (database) in statuses = self.statusesTable.mark(articles.statuses(), statusKey, flag, database) } return statuses @@ -399,7 +399,7 @@ final class ArticlesTable: DatabaseTable { // MARK: - Indexing func indexUnindexedArticles() { - queue.fetch { (database) in + queue.runInDatabase { (database) in let sql = "select articleID from articles where searchRowID is null limit 500;" guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else { return @@ -419,7 +419,7 @@ final class ArticlesTable: DatabaseTable { // MARK: - Caches func emptyCaches() { - queue.run { _ in + queue.runInDatabase { _ in self.databaseArticlesCache = [String: DatabaseArticle]() } } @@ -433,7 +433,7 @@ final class ArticlesTable: DatabaseTable { if feedIDs.isEmpty { return } - queue.run { (database) in + queue.runInDatabase { (database) in let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(feedIDs.count))! let sql = "select articleID from articles where feedID not in \(placeholders);" let parameters = Array(feedIDs) as [Any] @@ -458,14 +458,14 @@ private extension ArticlesTable { private func fetchArticles(_ fetchMethod: @escaping ArticlesFetchMethod) -> Set
{ var articles = Set
() - queue.fetchSync { (database) in + queue.runInDatabaseSync { (database) in articles = fetchMethod(database) } return articles } private func fetchArticlesAsync(_ fetchMethod: @escaping ArticlesFetchMethod, _ callback: @escaping ArticleSetBlock) { - queue.fetch { (database) in + queue.runInDatabase { (database) in let articles = fetchMethod(database) DispatchQueue.main.async { callback(articles) diff --git a/Frameworks/ArticlesDatabase/SearchTable.swift b/Frameworks/ArticlesDatabase/SearchTable.swift index d0c95838a..8007b366d 100644 --- a/Frameworks/ArticlesDatabase/SearchTable.swift +++ b/Frameworks/ArticlesDatabase/SearchTable.swift @@ -61,10 +61,10 @@ final class ArticleSearchInfo: Hashable { final class SearchTable: DatabaseTable { let name = "search" - private let queue: RSDatabaseQueue + private let queue: DatabaseQueue private weak var articlesTable: ArticlesTable? - init(queue: RSDatabaseQueue, articlesTable: ArticlesTable) { + init(queue: DatabaseQueue, articlesTable: ArticlesTable) { self.queue = queue self.articlesTable = articlesTable } @@ -73,7 +73,7 @@ final class SearchTable: DatabaseTable { if articleIDs.isEmpty { return } - queue.update { (database) in + queue.runInTransaction { (database) in self.ensureIndexedArticles(articleIDs, database) } } diff --git a/Frameworks/ArticlesDatabase/StatusesTable.swift b/Frameworks/ArticlesDatabase/StatusesTable.swift index 65615c1b0..745bd9efb 100644 --- a/Frameworks/ArticlesDatabase/StatusesTable.swift +++ b/Frameworks/ArticlesDatabase/StatusesTable.swift @@ -19,9 +19,9 @@ final class StatusesTable: DatabaseTable { let name = DatabaseTableName.statuses private let cache = StatusCache() - private let queue: RSDatabaseQueue + private let queue: DatabaseQueue - init(queue: RSDatabaseQueue) { + init(queue: DatabaseQueue) { self.queue = queue } @@ -88,7 +88,7 @@ final class StatusesTable: DatabaseTable { func fetchArticleIDs(_ sql: String) -> Set { var articleIDs = Set() - queue.fetchSync { (database) in + queue.runInDatabaseSync { (database) in guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else { return } diff --git a/Frameworks/SyncDatabase/SyncDatabase.swift b/Frameworks/SyncDatabase/SyncDatabase.swift index 39a781391..6545c84f1 100644 --- a/Frameworks/SyncDatabase/SyncDatabase.swift +++ b/Frameworks/SyncDatabase/SyncDatabase.swift @@ -9,20 +9,27 @@ import Foundation import RSDatabase -public final class SyncDatabase { - - private let syncStatusTable: SyncStatusTable - - public init(databaseFilePath: String) { - - let queue = RSDatabaseQueue(filepath: databaseFilePath, excludeFromBackup: false) - self.syncStatusTable = SyncStatusTable(queue: queue) - - queue.createTables(usingStatements: SyncDatabase.tableCreationStatements) - queue.vacuumIfNeeded() +public struct SyncDatabase { + /// When SyncDatabase is suspended, database calls will crash the app. + public var isSuspended: Bool { + return queue.isSuspended } - + + private let syncStatusTable: SyncStatusTable + private let queue: DatabaseQueue + + public init(databaseFilePath: String) { + let queue = DatabaseQueue(databasePath: databaseFilePath) + queue.runCreateStatements(SyncDatabase.tableCreationStatements) + queue.vacuumIfNeeded(daysBetweenVacuums: 11) + self.queue = queue + + self.syncStatusTable = SyncStatusTable(queue: queue) + } + + // MARK: - API + public func insertStatuses(_ statuses: [SyncStatus]) { syncStatusTable.insertStatuses(statuses) } @@ -42,7 +49,19 @@ public final class SyncDatabase { public func deleteSelectedForProcessing(_ articleIDs: [String]) { syncStatusTable.deleteSelectedForProcessing(articleIDs) } - + + // MARK: - Suspend and Resume (for iOS) + + /// Close the database and stop running database calls. + /// Any pending calls will complete first. + public func suspend() { + queue.suspend() + } + + /// Open the database and allow for running database calls again. + public func resume() { + queue.resume() + } } // MARK: - Private diff --git a/Frameworks/SyncDatabase/SyncStatusTable.swift b/Frameworks/SyncDatabase/SyncStatusTable.swift index 63a2036fe..0b5c0a44d 100644 --- a/Frameworks/SyncDatabase/SyncStatusTable.swift +++ b/Frameworks/SyncDatabase/SyncStatusTable.swift @@ -10,21 +10,19 @@ import Foundation import Articles import RSDatabase -final class SyncStatusTable: DatabaseTable { - +struct SyncStatusTable: DatabaseTable { + let name = DatabaseTableName.syncStatus - private let queue: RSDatabaseQueue - - init(queue: RSDatabaseQueue) { + private let queue: DatabaseQueue + + init(queue: DatabaseQueue) { self.queue = queue } func selectForProcessing() -> [SyncStatus] { - var statuses: Set? = nil - self.queue.updateSync { database in - + queue.runInDatabaseSync { database in let updateSQL = "update syncStatus set selected = true" database.executeUpdate(updateSQL, withArgumentsIn: nil) @@ -32,32 +30,26 @@ final class SyncStatusTable: DatabaseTable { if let resultSet = database.executeQuery(selectSQL, withArgumentsIn: nil) { statuses = resultSet.mapToSet(self.statusWithRow) } - } return statuses != nil ? Array(statuses!) : [SyncStatus]() - } func selectPendingCount() -> Int { - var count: Int = 0 - self.queue.fetchSync { (database) in + queue.runInDatabaseSync { database in let sql = "select count(*) from syncStatus" if let resultSet = database.executeQuery(sql, withArgumentsIn: nil) { - resultSet.next() - count = Int(resultSet.int(forColumnIndex: 0)) + count = numberWithCountResultSet(resultSet) } - } return count - } func resetSelectedForProcessing(_ articleIDs: [String]) { - self.queue.update { database in + queue.runInTransaction { database in let parameters = articleIDs.map { $0 as AnyObject } let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(articleIDs.count))! let updateSQL = "update syncStatus set selected = false where articleID in \(placeholders)" @@ -66,7 +58,7 @@ final class SyncStatusTable: DatabaseTable { } func deleteSelectedForProcessing(_ articleIDs: [String]) { - self.queue.update { database in + queue.runInTransaction { database in let parameters = articleIDs.map { $0 as AnyObject } let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(articleIDs.count))! let deleteSQL = "delete from syncStatus where articleID in \(placeholders)" @@ -75,18 +67,16 @@ final class SyncStatusTable: DatabaseTable { } func insertStatuses(_ statuses: [SyncStatus]) { - self.queue.update { database in + queue.runInTransaction { database in let statusArray = statuses.map { $0.databaseDictionary() } self.insertRows(statusArray, insertType: .orReplace, in: database) } } - } private extension SyncStatusTable { func statusWithRow(_ row: FMResultSet) -> SyncStatus? { - guard let articleID = row.string(forColumn: DatabaseKey.articleID), let rawKey = row.string(forColumn: DatabaseKey.key), let key = ArticleStatus.Key(rawValue: rawKey) else { @@ -97,6 +87,5 @@ private extension SyncStatusTable { let selected = row.bool(forColumn: DatabaseKey.selected) return SyncStatus(articleID: articleID, key: key, flag: flag, selected: selected) - } }