Use makeDatabaseCalls/switch pattern in ArticlesTable.

This commit is contained in:
Brent Simmons 2019-12-16 12:49:46 -08:00
parent 3981312d6f
commit aa5859ff27
2 changed files with 222 additions and 174 deletions

View File

@ -164,7 +164,7 @@ public final class ArticlesDatabase {
articlesTable.update(webFeedIDsAndItems, defaultRead, completion) articlesTable.update(webFeedIDsAndItems, defaultRead, completion)
} }
public func ensureStatuses(_ articleIDs: Set<String>, _ defaultRead: Bool, _ statusKey: ArticleStatus.Key, _ flag: Bool, completion: DatabaseCompletionBlock? = nil) { public func ensureStatuses(_ articleIDs: Set<String>, _ defaultRead: Bool, _ statusKey: ArticleStatus.Key, _ flag: Bool, completion: @escaping DatabaseCompletionBlock) {
articlesTable.ensureStatuses(articleIDs, defaultRead, statusKey, flag, completion: completion) articlesTable.ensureStatuses(articleIDs, defaultRead, statusKey, flag, completion: completion)
} }

View File

@ -104,6 +104,7 @@ final class ArticlesTable: DatabaseTable {
func fetchArticlesMatching(_ searchString: String) throws -> Set<Article> { func fetchArticlesMatching(_ searchString: String) throws -> Set<Article> {
var articles: Set<Article> = Set<Article>() var articles: Set<Article> = Set<Article>()
var error: DatabaseError? = nil var error: DatabaseError? = nil
queue.runInDatabaseSync { (databaseResult) in queue.runInDatabaseSync { (databaseResult) in
switch databaseResult { switch databaseResult {
case .success(let database): case .success(let database):
@ -112,6 +113,7 @@ final class ArticlesTable: DatabaseTable {
error = databaseError error = databaseError
} }
} }
if let error = error { if let error = error {
throw(error) throw(error)
} }
@ -188,13 +190,8 @@ final class ArticlesTable: DatabaseTable {
} }
self.queue.runInTransaction { (databaseResult) in self.queue.runInTransaction { (databaseResult) in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseResult.error!))
}
return
}
func makeDatabaseCalls(_ database: FMDatabase) {
let statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, read, database) //1 let statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, read, database) //1
assert(statusesDictionary.count == articleIDs.count) assert(statusesDictionary.count == articleIDs.count)
@ -227,37 +224,45 @@ final class ArticlesTable: DatabaseTable {
self.searchTable.indexUpdatedArticles(updatedArticles, database) self.searchTable.indexUpdatedArticles(updatedArticles, database)
} }
} }
}
func ensureStatuses(_ articleIDs: Set<String>, _ defaultRead: Bool, _ statusKey: ArticleStatus.Key, _ flag: Bool, completion: DatabaseCompletionBlock? = nil) { switch databaseResult {
queue.runInTransaction { databaseResult in case .success(let database):
guard let database = databaseResult.database else { makeDatabaseCalls(database)
case .failure(let databaseError):
DispatchQueue.main.async { DispatchQueue.main.async {
completion?(databaseResult.error!) completion(.failure(databaseError))
}
}
} }
return
} }
func ensureStatuses(_ articleIDs: Set<String>, _ defaultRead: Bool, _ statusKey: ArticleStatus.Key, _ flag: Bool, completion: @escaping DatabaseCompletionBlock) {
queue.runInTransaction { databaseResult in
func makeDatabaseCalls(_ database: FMDatabase) {
let statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, defaultRead, database) let statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, defaultRead, database)
let statuses = Set(statusesDictionary.values) let statuses = Set(statusesDictionary.values)
self.statusesTable.mark(statuses, statusKey, flag, database) self.statusesTable.mark(statuses, statusKey, flag, database)
if let completion = completion {
DispatchQueue.main.async { DispatchQueue.main.async {
completion(nil) completion(nil)
} }
} }
switch databaseResult {
case .success(let database):
makeDatabaseCalls(database)
case .failure(let databaseError):
DispatchQueue.main.async {
completion(databaseError)
}
}
} }
} }
func fetchStatuses(_ articleIDs: Set<String>, _ createIfNeeded: Bool, _ completion: @escaping ArticleStatusesResultBlock) { func fetchStatuses(_ articleIDs: Set<String>, _ createIfNeeded: Bool, _ completion: @escaping ArticleStatusesResultBlock) {
queue.runInTransaction { databaseResult in queue.runInTransaction { databaseResult in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseResult.error!))
}
return
}
func makeDatabaseCalls(_ database: FMDatabase) {
var statusesDictionary = [String: ArticleStatus]() var statusesDictionary = [String: ArticleStatus]()
if createIfNeeded { if createIfNeeded {
statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, false, database) statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, false, database)
@ -270,6 +275,16 @@ final class ArticlesTable: DatabaseTable {
completion(.success(statuses)) completion(.success(statuses))
} }
} }
switch databaseResult {
case .success(let database):
makeDatabaseCalls(database)
case .failure(let databaseError):
DispatchQueue.main.async {
completion(.failure(databaseError))
}
}
}
} }
// MARK: - Unread Counts // MARK: - Unread Counts
@ -281,13 +296,8 @@ final class ArticlesTable: DatabaseTable {
} }
queue.runInDatabase { databaseResult in queue.runInDatabase { databaseResult in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseResult.error!))
}
return
}
func makeDatabaseCalls(_ database: FMDatabase) {
var unreadCountDictionary = UnreadCountDictionary() var unreadCountDictionary = UnreadCountDictionary()
for webFeedID in webFeedIDs { for webFeedID in webFeedIDs {
unreadCountDictionary[webFeedID] = self.fetchUnreadCount(webFeedID, database) unreadCountDictionary[webFeedID] = self.fetchUnreadCount(webFeedID, database)
@ -297,6 +307,16 @@ final class ArticlesTable: DatabaseTable {
completion(.success(unreadCountDictionary)) completion(.success(unreadCountDictionary))
} }
} }
switch databaseResult {
case .success(let database):
makeDatabaseCalls(database)
case .failure(let databaseError):
DispatchQueue.main.async {
completion(.failure(databaseError))
}
}
}
} }
func fetchUnreadCount(_ webFeedIDs: Set<String>, _ since: Date, _ completion: @escaping SingleUnreadCountCompletionBlock) { func fetchUnreadCount(_ webFeedIDs: Set<String>, _ since: Date, _ completion: @escaping SingleUnreadCountCompletionBlock) {
@ -307,13 +327,8 @@ final class ArticlesTable: DatabaseTable {
} }
queue.runInDatabase { databaseResult in queue.runInDatabase { databaseResult in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseResult.error!))
}
return
}
func makeDatabaseCalls(_ database: FMDatabase) {
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(webFeedIDs.count))! let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(webFeedIDs.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;" 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;"
@ -328,6 +343,16 @@ final class ArticlesTable: DatabaseTable {
completion(.success(unreadCount)) completion(.success(unreadCount))
} }
} }
switch databaseResult {
case .success(let database):
makeDatabaseCalls(database)
case .failure(let databaseError):
DispatchQueue.main.async {
completion(.failure(databaseError))
}
}
}
} }
func fetchAllUnreadCounts(_ completion: @escaping UnreadCountDictionaryCompletionBlock) { func fetchAllUnreadCounts(_ completion: @escaping UnreadCountDictionaryCompletionBlock) {
@ -335,13 +360,8 @@ final class ArticlesTable: DatabaseTable {
let cutoffDate = articleCutoffDate let cutoffDate = articleCutoffDate
queue.runInDatabase { databaseResult in queue.runInDatabase { databaseResult in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseResult.error!))
}
return
}
func makeDatabaseCalls(_ database: FMDatabase) {
let sql = "select distinct feedID, count(*) from articles natural join statuses where read=0 and userDeleted=0 and (starred=1 or (datePublished > ? or (datePublished is null and dateArrived > ?))) group by feedID;" let sql = "select distinct feedID, count(*) from articles natural join statuses where read=0 and userDeleted=0 and (starred=1 or (datePublished > ? or (datePublished is null and dateArrived > ?))) group by feedID;"
guard let resultSet = database.executeQuery(sql, withArgumentsIn: [cutoffDate, cutoffDate]) else { guard let resultSet = database.executeQuery(sql, withArgumentsIn: [cutoffDate, cutoffDate]) else {
@ -363,6 +383,16 @@ final class ArticlesTable: DatabaseTable {
completion(.success(d)) completion(.success(d))
} }
} }
switch databaseResult {
case .success(let database):
makeDatabaseCalls(database)
case .failure(let databaseError):
DispatchQueue.main.async {
completion(.failure(databaseError))
}
}
}
} }
func fetchStarredAndUnreadCount(_ webFeedIDs: Set<String>, _ completion: @escaping SingleUnreadCountCompletionBlock) { func fetchStarredAndUnreadCount(_ webFeedIDs: Set<String>, _ completion: @escaping SingleUnreadCountCompletionBlock) {
@ -372,13 +402,8 @@ final class ArticlesTable: DatabaseTable {
} }
queue.runInDatabase { databaseResult in queue.runInDatabase { databaseResult in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseResult.error!))
}
return
}
func makeDatabaseCalls(_ database: FMDatabase) {
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(webFeedIDs.count))! let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(webFeedIDs.count))!
let sql = "select count(*) from articles natural join statuses where feedID in \(placeholders) and read=0 and starred=1 and userDeleted=0;" 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(webFeedIDs) as [Any] let parameters = Array(webFeedIDs) as [Any]
@ -389,6 +414,16 @@ final class ArticlesTable: DatabaseTable {
completion(.success(unreadCount)) completion(.success(unreadCount))
} }
} }
switch databaseResult {
case .success(let database):
makeDatabaseCalls(database)
case .failure(let databaseError):
DispatchQueue.main.async {
completion(.failure(databaseError))
}
}
}
} }
// MARK: - Statuses // MARK: - Statuses
@ -431,9 +466,8 @@ final class ArticlesTable: DatabaseTable {
func indexUnindexedArticles() { func indexUnindexedArticles() {
queue.runInDatabase { databaseResult in queue.runInDatabase { databaseResult in
guard let database = databaseResult.database else {
return func makeDatabaseCalls(_ database: FMDatabase) {
}
let sql = "select articleID from articles where searchRowID is null limit 500;" let sql = "select articleID from articles where searchRowID is null limit 500;"
guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else { guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else {
return return
@ -448,6 +482,11 @@ final class ArticlesTable: DatabaseTable {
self.indexUnindexedArticles() self.indexUnindexedArticles()
} }
} }
if let database = databaseResult.database {
makeDatabaseCalls(database)
}
}
} }
// MARK: - Caches // MARK: - Caches
@ -468,10 +507,8 @@ final class ArticlesTable: DatabaseTable {
return return
} }
queue.runInDatabase { databaseResult in queue.runInDatabase { databaseResult in
guard let database = databaseResult.database else {
return
}
func makeDatabaseCalls(_ database: FMDatabase) {
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(webFeedIDs.count))! let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(webFeedIDs.count))!
let sql = "select articleID from articles where feedID not in \(placeholders);" let sql = "select articleID from articles where feedID not in \(placeholders);"
let parameters = Array(webFeedIDs) as [Any] let parameters = Array(webFeedIDs) as [Any]
@ -485,6 +522,11 @@ final class ArticlesTable: DatabaseTable {
self.removeArticles(articleIDs, database) self.removeArticles(articleIDs, database)
self.statusesTable.removeStatuses(articleIDs, database) self.statusesTable.removeStatuses(articleIDs, database)
} }
if let database = databaseResult.database {
makeDatabaseCalls(database)
}
}
} }
} }
@ -513,17 +555,18 @@ private extension ArticlesTable {
private func fetchArticlesAsync(_ fetchMethod: @escaping ArticlesFetchMethod, _ completion: @escaping ArticleSetResultBlock) { private func fetchArticlesAsync(_ fetchMethod: @escaping ArticlesFetchMethod, _ completion: @escaping ArticleSetResultBlock) {
queue.runInDatabase { databaseResult in queue.runInDatabase { databaseResult in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseResult.error!))
}
return
}
switch databaseResult {
case .success(let database):
let articles = fetchMethod(database) let articles = fetchMethod(database)
DispatchQueue.main.async { DispatchQueue.main.async {
completion(.success(articles)) completion(.success(articles))
} }
case .failure(let databaseError):
DispatchQueue.main.async {
completion(.failure(databaseError))
}
}
} }
} }
@ -682,13 +725,8 @@ private extension ArticlesTable {
} }
queue.runInDatabase { databaseResult in queue.runInDatabase { databaseResult in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseResult.error!))
}
return
}
func makeDatabaseCalls(_ database: FMDatabase) {
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(webFeedIDs.count))! let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(webFeedIDs.count))!
var sql = "select articleID from articles natural join statuses where feedID in \(placeholders) and \(statusKey.rawValue)=" var sql = "select articleID from articles natural join statuses where feedID in \(placeholders) and \(statusKey.rawValue)="
sql += value ? "1" : "0" sql += value ? "1" : "0"
@ -711,6 +749,16 @@ private extension ArticlesTable {
completion(.success(articleIDs)) completion(.success(articleIDs))
} }
} }
switch databaseResult {
case .success(let database):
makeDatabaseCalls(database)
case .failure(let databaseError):
DispatchQueue.main.async {
completion(.failure(databaseError))
}
}
}
} }
func fetchArticles(_ webFeedIDs: Set<String>, _ database: FMDatabase) -> Set<Article> { func fetchArticles(_ webFeedIDs: Set<String>, _ database: FMDatabase) -> Set<Article> {