mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-06 12:18:26 +00:00
Remove no-longer-needed DatabaseResult.
This commit is contained in:
1 parent
3d9e1d27d0
commit
e412f333d6
9 files changed
+138
-396
No files matched your search
@@ -70,10 +70,9 @@ public struct ArticleCounts: Sendable {
|
||||
self.retentionStyle = retentionStyle
|
||||
self.accountID = accountID
|
||||
|
||||
try! queue.runCreateStatements(ArticlesDatabase.tableCreationStatements)
|
||||
queue.runInDatabase { databaseResult in
|
||||
queue.runCreateStatements(ArticlesDatabase.tableCreationStatements)
|
||||
queue.runInDatabase { database in
|
||||
Self.logger.debug("ArticlesDatabase: creating tables \(accountID, privacy: .public)")
|
||||
let database = databaseResult.database!
|
||||
if !self.articlesTable.containsColumn("searchRowID", in: database) {
|
||||
database.executeStatements("ALTER TABLE articles add column searchRowID INTEGER;")
|
||||
}
|
||||
|
||||
@@ -111,17 +111,10 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
// MARK: - Fetching Counts Async
|
||||
|
||||
func fetchArticleCountsAsync(_ feedIDs: Set<String>, _ completion: @escaping @Sendable (Result<ArticleCounts, Error>) -> Void) {
|
||||
queue.runInDatabase { databaseResult in
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
let counts = self.articleCounts(feedIDs: feedIDs, database: database)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(counts))
|
||||
}
|
||||
case .failure(let databaseError):
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(databaseError))
|
||||
}
|
||||
queue.runInDatabase { database in
|
||||
let counts = self.articleCounts(feedIDs: feedIDs, database: database)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(counts))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -129,17 +122,10 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
// MARK: - Fetching Last Update Dates
|
||||
|
||||
func fetchLastUpdateDatesAsync(_ completion: @escaping @Sendable (Result<[String: Date], Error>) -> Void) {
|
||||
queue.runInDatabase { databaseResult in
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
let result = self.fetchLastUpdateDates(database)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(result))
|
||||
}
|
||||
case .failure(let databaseError):
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(databaseError))
|
||||
}
|
||||
queue.runInDatabase { database in
|
||||
let result = self.fetchLastUpdateDates(database)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(result))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -148,20 +134,11 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
|
||||
func fetchArticlesMatching(_ searchString: String) throws -> Set<Article> {
|
||||
nonisolated(unsafe) var articles: Set<Article> = Set<Article>()
|
||||
nonisolated(unsafe) var error: Error?
|
||||
|
||||
queue.runInDatabaseSync { (databaseResult) in
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
articles = self.fetchArticlesMatching(searchString, database)
|
||||
case .failure(let databaseError):
|
||||
error = databaseError
|
||||
}
|
||||
queue.runInDatabaseSync { database in
|
||||
articles = self.fetchArticlesMatching(searchString, database)
|
||||
}
|
||||
|
||||
if let error = error {
|
||||
throw(error)
|
||||
}
|
||||
return articles
|
||||
}
|
||||
|
||||
@@ -248,9 +225,9 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
// 8. Delete Articles in database no longer present in the feed.
|
||||
// 9. Update search index.
|
||||
|
||||
self.queue.runInTransaction { (databaseResult) in
|
||||
self.queue.runInTransaction { database in
|
||||
|
||||
func makeDatabaseCalls(_ database: FMDatabase) {
|
||||
func makeDatabaseCalls() {
|
||||
let articleIDs = parsedItems.articleIDs()
|
||||
|
||||
// Split by age: articles older than ~6 months default to read.
|
||||
@@ -307,14 +284,7 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
makeDatabaseCalls(database)
|
||||
case .failure(let databaseError):
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(databaseError))
|
||||
}
|
||||
}
|
||||
makeDatabaseCalls()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -334,9 +304,9 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
// 7. Call back with new and updated Articles.
|
||||
// 8. Update search index.
|
||||
|
||||
self.queue.runInTransaction { (databaseResult) in
|
||||
self.queue.runInTransaction { database in
|
||||
|
||||
func makeDatabaseCalls(_ database: FMDatabase) {
|
||||
func makeDatabaseCalls() {
|
||||
var articleIDs = Set<String>()
|
||||
for (_, parsedItems) in feedIDsAndItems {
|
||||
articleIDs.formUnion(parsedItems.articleIDs())
|
||||
@@ -378,38 +348,17 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
makeDatabaseCalls(database)
|
||||
case .failure(let databaseError):
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(databaseError))
|
||||
}
|
||||
}
|
||||
makeDatabaseCalls()
|
||||
}
|
||||
}
|
||||
|
||||
public func delete(articleIDs: Set<String>, completion: DatabaseCompletionBlock?) {
|
||||
self.queue.runInTransaction { (databaseResult) in
|
||||
|
||||
func makeDatabaseCalls(_ database: FMDatabase) {
|
||||
self.removeArticles(articleIDs, database)
|
||||
DispatchQueue.main.async {
|
||||
completion?(nil)
|
||||
}
|
||||
self.queue.runInTransaction { database in
|
||||
self.removeArticles(articleIDs, database)
|
||||
DispatchQueue.main.async {
|
||||
completion?(nil)
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
makeDatabaseCalls(database)
|
||||
case .failure(let databaseError):
|
||||
DispatchQueue.main.async {
|
||||
completion?(databaseError)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Unread Counts
|
||||
@@ -420,7 +369,7 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
return
|
||||
}
|
||||
|
||||
queue.runInDatabase { databaseResult in
|
||||
queue.runInDatabase { database in
|
||||
|
||||
func makeDatabaseCalls(_ database: FMDatabase) throws -> UnreadCountDictionary {
|
||||
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(feedIDs.count))!
|
||||
@@ -445,19 +394,12 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
return unreadCountDictionary
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
do {
|
||||
let unreadCountDictionary = try makeDatabaseCalls(database)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(unreadCountDictionary))
|
||||
}
|
||||
} catch {
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(error))
|
||||
}
|
||||
do {
|
||||
let unreadCountDictionary = try makeDatabaseCalls(database)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(unreadCountDictionary))
|
||||
}
|
||||
case .failure(let error):
|
||||
} catch {
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(error))
|
||||
}
|
||||
@@ -472,31 +414,19 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
return
|
||||
}
|
||||
|
||||
queue.runInDatabase { databaseResult 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;"
|
||||
|
||||
func makeDatabaseCalls(_ database: FMDatabase) {
|
||||
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;"
|
||||
var parameters = [Any]()
|
||||
parameters += Array(feedIDs) as [Any]
|
||||
parameters += [since] as [Any]
|
||||
parameters += [since] as [Any]
|
||||
|
||||
var parameters = [Any]()
|
||||
parameters += Array(feedIDs) as [Any]
|
||||
parameters += [since] as [Any]
|
||||
parameters += [since] as [Any]
|
||||
let unreadCount = self.numberWithSQLAndParameters(sql, parameters, in: database)
|
||||
|
||||
let unreadCount = self.numberWithSQLAndParameters(sql, parameters, in: database)
|
||||
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(unreadCount))
|
||||
}
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
makeDatabaseCalls(database)
|
||||
case .failure(let databaseError):
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(databaseError))
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(unreadCount))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -507,27 +437,15 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
return
|
||||
}
|
||||
|
||||
queue.runInDatabase { databaseResult 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;"
|
||||
let parameters = Array(feedIDs) as [Any]
|
||||
|
||||
func makeDatabaseCalls(_ database: FMDatabase) {
|
||||
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;"
|
||||
let parameters = Array(feedIDs) as [Any]
|
||||
let unreadCount = self.numberWithSQLAndParameters(sql, parameters, in: database)
|
||||
|
||||
let unreadCount = self.numberWithSQLAndParameters(sql, parameters, in: database)
|
||||
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(unreadCount))
|
||||
}
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
makeDatabaseCalls(database)
|
||||
case .failure(let databaseError):
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(databaseError))
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(unreadCount))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -551,33 +469,19 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
}
|
||||
|
||||
func mark(_ articles: Set<Article>, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ completion: @escaping ArticleStatusesResultBlock) {
|
||||
self.queue.runInTransaction { databaseResult in
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
let statuses = self.statusesTable.mark(articles.statuses(), statusKey, flag, database)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(statuses ?? Set<ArticleStatus>()))
|
||||
}
|
||||
case .failure(let databaseError):
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(databaseError))
|
||||
}
|
||||
self.queue.runInTransaction { database in
|
||||
let statuses = self.statusesTable.mark(articles.statuses(), statusKey, flag, database)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(statuses ?? Set<ArticleStatus>()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func markAndFetchNew(_ articleIDs: Set<String>, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ completion: @escaping ArticleIDsCompletionBlock) {
|
||||
queue.runInTransaction { databaseResult in
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
let newStatusIDs = self.statusesTable.markAndFetchNew(articleIDs, statusKey, flag, database)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(newStatusIDs))
|
||||
}
|
||||
case .failure(let databaseError):
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(databaseError))
|
||||
}
|
||||
queue.runInTransaction { database in
|
||||
let newStatusIDs = self.statusesTable.markAndFetchNew(articleIDs, statusKey, flag, database)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(newStatusIDs))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -588,17 +492,10 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
return
|
||||
}
|
||||
|
||||
queue.runInTransaction { databaseResult in
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
self.statusesTable.ensureStatusesForArticleIDs(articleIDs, true, database)
|
||||
DispatchQueue.main.async {
|
||||
completion(nil)
|
||||
}
|
||||
case .failure(let databaseError):
|
||||
DispatchQueue.main.async {
|
||||
completion(databaseError)
|
||||
}
|
||||
queue.runInTransaction { database in
|
||||
self.statusesTable.ensureStatusesForArticleIDs(articleIDs, true, database)
|
||||
DispatchQueue.main.async {
|
||||
completion(nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -606,26 +503,19 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
// MARK: - Indexing
|
||||
|
||||
func indexUnindexedArticles() {
|
||||
queue.runInDatabase { databaseResult in
|
||||
|
||||
func makeDatabaseCalls(_ database: FMDatabase) {
|
||||
let sql = "select articleID from articles where searchRowID is null limit 500;"
|
||||
guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else {
|
||||
return
|
||||
}
|
||||
let articleIDs = resultSet.mapToSet { $0.swiftString(forColumn: DatabaseKey.articleID) }
|
||||
if articleIDs.isEmpty {
|
||||
return
|
||||
}
|
||||
self.searchTable.ensureIndexedArticles(articleIDs, database)
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.indexUnindexedArticles()
|
||||
}
|
||||
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
|
||||
}
|
||||
let articleIDs = resultSet.mapToSet { $0.swiftString(forColumn: DatabaseKey.articleID) }
|
||||
if articleIDs.isEmpty {
|
||||
return
|
||||
}
|
||||
self.searchTable.ensureIndexedArticles(articleIDs, database)
|
||||
|
||||
if let database = databaseResult.database {
|
||||
makeDatabaseCalls(database)
|
||||
DispatchQueue.main.async {
|
||||
self.indexUnindexedArticles()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -657,11 +547,7 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
func deleteOldArticles() {
|
||||
precondition(retentionStyle == .syncSystem)
|
||||
|
||||
queue.runInTransaction { databaseResult in
|
||||
guard let database = databaseResult.database else {
|
||||
return
|
||||
}
|
||||
|
||||
queue.runInTransaction { database in
|
||||
func deleteOldArticles(cutoffDate: Date) {
|
||||
let sql = "delete from articles where articleID in (select articleID from articles natural join statuses where dateArrived<? and read=1 and starred=0);"
|
||||
let parameters = [cutoffDate] as [Any]
|
||||
@@ -687,11 +573,7 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
|
||||
/// Delete old statuses.
|
||||
func deleteOldStatuses() {
|
||||
queue.runInTransaction { databaseResult in
|
||||
guard let database = databaseResult.database else {
|
||||
return
|
||||
}
|
||||
|
||||
queue.runInTransaction { database in
|
||||
let sql: String
|
||||
let cutoffDate: Date
|
||||
|
||||
@@ -716,26 +598,19 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
if feedIDs.isEmpty {
|
||||
return
|
||||
}
|
||||
queue.runInDatabase { databaseResult in
|
||||
|
||||
func makeDatabaseCalls(_ database: FMDatabase) {
|
||||
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]
|
||||
guard let resultSet = database.executeQuery(sql, withArgumentsIn: parameters) else {
|
||||
return
|
||||
}
|
||||
let articleIDs = resultSet.mapToSet { $0.swiftString(forColumn: DatabaseKey.articleID) }
|
||||
if articleIDs.isEmpty {
|
||||
return
|
||||
}
|
||||
self.removeArticles(articleIDs, database)
|
||||
self.statusesTable.removeStatuses(articleIDs, database)
|
||||
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]
|
||||
guard let resultSet = database.executeQuery(sql, withArgumentsIn: parameters) else {
|
||||
return
|
||||
}
|
||||
|
||||
if let database = databaseResult.database {
|
||||
makeDatabaseCalls(database)
|
||||
let articleIDs = resultSet.mapToSet { $0.swiftString(forColumn: DatabaseKey.articleID) }
|
||||
if articleIDs.isEmpty {
|
||||
return
|
||||
}
|
||||
self.removeArticles(articleIDs, database)
|
||||
self.statusesTable.removeStatuses(articleIDs, database)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -744,11 +619,7 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
/// This is not intended for wide use: this is part of implementing
|
||||
/// the April 2020 retention policy change for feed-based accounts.
|
||||
func markOlderStatusesAsRead() {
|
||||
queue.runInDatabase { databaseResult in
|
||||
guard let database = databaseResult.database else {
|
||||
return
|
||||
}
|
||||
|
||||
queue.runInDatabase { database in
|
||||
let sql = "update statuses set read = 1 where dateArrived<?;"
|
||||
let parameters = [self.articleCutoffDate] as [Any]
|
||||
database.executeUpdate(sql, withArgumentsIn: parameters)
|
||||
@@ -764,53 +635,27 @@ nonisolated private extension ArticlesTable {
|
||||
|
||||
private func fetchArticles(_ fetchMethod: @escaping ArticlesFetchMethod) throws -> Set<Article> {
|
||||
nonisolated(unsafe) var articles = Set<Article>()
|
||||
nonisolated(unsafe) var error: Error?
|
||||
|
||||
queue.runInDatabaseSync { databaseResult in
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
articles = fetchMethod(database)
|
||||
case .failure(let databaseError):
|
||||
error = databaseError
|
||||
}
|
||||
}
|
||||
if let error = error {
|
||||
throw(error)
|
||||
queue.runInDatabaseSync { database in
|
||||
articles = fetchMethod(database)
|
||||
}
|
||||
return articles
|
||||
}
|
||||
|
||||
private func fetchArticlesCount(_ fetchMethod: @escaping ArticlesCountFetchMethod) throws -> Int {
|
||||
nonisolated(unsafe) var articlesCount = 0
|
||||
nonisolated(unsafe) var error: Error?
|
||||
|
||||
queue.runInDatabaseSync { databaseResult in
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
articlesCount = fetchMethod(database)
|
||||
case .failure(let databaseError):
|
||||
error = databaseError
|
||||
}
|
||||
}
|
||||
if let error = error {
|
||||
throw(error)
|
||||
queue.runInDatabaseSync { database in
|
||||
articlesCount = fetchMethod(database)
|
||||
}
|
||||
return articlesCount
|
||||
}
|
||||
|
||||
private func fetchArticlesAsync(_ fetchMethod: @escaping ArticlesFetchMethod, _ completion: @escaping ArticleSetResultBlock) {
|
||||
queue.runInDatabase { databaseResult in
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
let articles = fetchMethod(database)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(articles))
|
||||
}
|
||||
case .failure(let databaseError):
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(databaseError))
|
||||
}
|
||||
queue.runInDatabase { database in
|
||||
let articles = fetchMethod(database)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(articles))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,8 +62,8 @@ private extension AuthorsSchemaMigration {
|
||||
|
||||
func fetchNextBatch(limit: Int) async -> Set<String> {
|
||||
await withCheckedContinuation { continuation in
|
||||
queue.runInDatabase { result in
|
||||
guard let database = try? result.get(), database.tableExists("authorsLookup") else {
|
||||
queue.runInDatabase { database in
|
||||
guard database.tableExists("authorsLookup") else {
|
||||
continuation.resume(returning: Set<String>())
|
||||
return
|
||||
}
|
||||
@@ -88,11 +88,7 @@ private extension AuthorsSchemaMigration {
|
||||
|
||||
func dropLegacyTables() async {
|
||||
await withCheckedContinuation { continuation in
|
||||
queue.runInDatabase { result in
|
||||
guard let database = try? result.get() else {
|
||||
continuation.resume()
|
||||
return
|
||||
}
|
||||
queue.runInDatabase { database in
|
||||
database.executeStatements("drop index if exists authorsLookup_articleID; drop table if exists authorsLookup; drop table if exists authors;")
|
||||
continuation.resume()
|
||||
}
|
||||
@@ -101,11 +97,7 @@ private extension AuthorsSchemaMigration {
|
||||
|
||||
func backfillBatch(_ articleIDs: Set<String>) async {
|
||||
await withCheckedContinuation { continuation in
|
||||
queue.runInDatabase { result in
|
||||
guard let database = try? result.get() else {
|
||||
continuation.resume()
|
||||
return
|
||||
}
|
||||
queue.runInDatabase { database in
|
||||
let authorsByArticleID = fetchAuthorsByArticleID(articleIDs, database: database)
|
||||
if authorsByArticleID.isEmpty {
|
||||
continuation.resume()
|
||||
|
||||
+3
-8
@@ -21,19 +21,14 @@ import RSDatabaseObjC
|
||||
}
|
||||
|
||||
public override func run() {
|
||||
queue.runInDatabase { databaseResult in
|
||||
queue.runInDatabase { database in
|
||||
if self.isCanceled {
|
||||
self.didComplete()
|
||||
return
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
let unreadCountDictionary = self.fetchUnreadCounts(database) ?? UnreadCountDictionary()
|
||||
self.result = .success(unreadCountDictionary)
|
||||
case .failure(let error):
|
||||
self.result = .failure(error)
|
||||
}
|
||||
let unreadCountDictionary = self.fetchUnreadCounts(database) ?? UnreadCountDictionary()
|
||||
self.result = .success(unreadCountDictionary)
|
||||
|
||||
self.didComplete()
|
||||
}
|
||||
|
||||
@@ -90,10 +90,8 @@ final class SearchTable: DatabaseTable, @unchecked Sendable {
|
||||
guard !articleIDs.isEmpty else {
|
||||
return
|
||||
}
|
||||
queue.runInTransaction { databaseResult in
|
||||
if let database = databaseResult.database {
|
||||
self.ensureIndexedArticles(articleIDs, database)
|
||||
}
|
||||
queue.runInTransaction { database in
|
||||
self.ensureIndexedArticles(articleIDs, database)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -104,78 +104,49 @@ final class StatusesTable: DatabaseTable, Sendable {
|
||||
}
|
||||
|
||||
func fetchArticleIDsAsync(_ statusKey: ArticleStatus.Key, _ value: Bool, _ completion: @escaping ArticleIDsCompletionBlock) {
|
||||
queue.runInDatabase { databaseResult in
|
||||
queue.runInDatabase { database in
|
||||
var sql = "select articleID from statuses where \(statusKey.rawValue)="
|
||||
sql += value ? "1" : "0"
|
||||
sql += ";"
|
||||
|
||||
func makeDatabaseCalls(_ database: FMDatabase) {
|
||||
var sql = "select articleID from statuses where \(statusKey.rawValue)="
|
||||
sql += value ? "1" : "0"
|
||||
sql += ";"
|
||||
|
||||
guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else {
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(Set<String>()))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let articleIDs = resultSet.mapToSet { $0.swiftString(forColumnIndex: 0) }
|
||||
guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else {
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(articleIDs))
|
||||
completion(.success(Set<String>()))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
makeDatabaseCalls(database)
|
||||
case .failure(let databaseError):
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(databaseError))
|
||||
}
|
||||
let articleIDs = resultSet.mapToSet { $0.swiftString(forColumnIndex: 0) }
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(articleIDs))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fetchArticleIDsForStatusesWithoutArticlesNewerThan(_ cutoffDate: Date, _ completion: @escaping ArticleIDsCompletionBlock) {
|
||||
queue.runInDatabase { databaseResult in
|
||||
|
||||
let result: Result<Set<String>, Error>
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
let sql = "select articleID from statuses s where (starred=1 or dateArrived>?) and not exists (select 1 from articles a where a.articleID = s.articleID);"
|
||||
if let resultSet = database.executeQuery(sql, withArgumentsIn: [cutoffDate]) {
|
||||
let articleIDs = resultSet.mapToSet(self.articleIDWithRow)
|
||||
result = .success(articleIDs)
|
||||
} else {
|
||||
result = .success(Set<String>())
|
||||
}
|
||||
case .failure(let databaseError):
|
||||
result = .failure(databaseError)
|
||||
queue.runInDatabase { database in
|
||||
let sql = "select articleID from statuses s where (starred=1 or dateArrived>?) and not exists (select 1 from articles a where a.articleID = s.articleID);"
|
||||
let articleIDs: Set<String>
|
||||
if let resultSet = database.executeQuery(sql, withArgumentsIn: [cutoffDate]) {
|
||||
articleIDs = resultSet.mapToSet(self.articleIDWithRow)
|
||||
} else {
|
||||
articleIDs = Set<String>()
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
completion(result)
|
||||
completion(.success(articleIDs))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fetchArticleIDs(_ sql: String) throws -> Set<String> {
|
||||
nonisolated(unsafe) var error: Error?
|
||||
nonisolated(unsafe) var articleIDs = Set<String>()
|
||||
queue.runInDatabaseSync { databaseResult in
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
if let resultSet = database.executeQuery(sql, withArgumentsIn: nil) {
|
||||
articleIDs = resultSet.mapToSet(self.articleIDWithRow)
|
||||
}
|
||||
case .failure(let databaseError):
|
||||
error = databaseError
|
||||
queue.runInDatabaseSync { database in
|
||||
if let resultSet = database.executeQuery(sql, withArgumentsIn: nil) {
|
||||
articleIDs = resultSet.mapToSet(self.articleIDWithRow)
|
||||
}
|
||||
}
|
||||
|
||||
if let error = error {
|
||||
throw(error)
|
||||
}
|
||||
return articleIDs
|
||||
}
|
||||
|
||||
|
||||
@@ -41,16 +41,13 @@ import RSDatabaseObjC
|
||||
.appendingPathComponent("ImageMetadata.db").path
|
||||
self.databasePath = databasePath
|
||||
let queue = DatabaseQueue(databasePath: databasePath)
|
||||
try? queue.runCreateStatements(Self.tableCreationStatements)
|
||||
queue.runCreateStatements(Self.tableCreationStatements)
|
||||
self.queue = queue
|
||||
|
||||
loadCachesFromDatabase()
|
||||
|
||||
let cutoff = Date().timeIntervalSince1970 - TimeInterval(days: Self.failureRetentionDays)
|
||||
queue.runInDatabase { result in
|
||||
guard let database = try? result.get() else {
|
||||
return
|
||||
}
|
||||
queue.runInDatabase { database in
|
||||
DownloadFailureTable.removeExpired(olderThan: cutoff, database: database)
|
||||
database.vacuumIfNeeded()
|
||||
}
|
||||
@@ -80,10 +77,7 @@ import RSDatabaseObjC
|
||||
homePagesWithNoFavicon.insert(homePageURL)
|
||||
homePageToFaviconURL.removeValue(forKey: homePageURL)
|
||||
}
|
||||
queue.runInDatabase { result in
|
||||
guard let database = try? result.get() else {
|
||||
return
|
||||
}
|
||||
queue.runInDatabase { database in
|
||||
HomePageFaviconTable.save(homePageURL: homePageURL, faviconURL: faviconURL, database: database)
|
||||
}
|
||||
}
|
||||
@@ -96,10 +90,7 @@ import RSDatabaseObjC
|
||||
|
||||
public func saveFeedIconURL(feedURL: String, iconURL: String) {
|
||||
feedURLToIconURL[feedURL] = iconURL
|
||||
queue.runInDatabase { result in
|
||||
guard let database = try? result.get() else {
|
||||
return
|
||||
}
|
||||
queue.runInDatabase { database in
|
||||
FeedIconURLTable.save(feedURL: feedURL, iconURL: iconURL, database: database)
|
||||
}
|
||||
}
|
||||
@@ -115,10 +106,7 @@ import RSDatabaseObjC
|
||||
|
||||
public func recordFailure(url: String, statusCode: Int?) {
|
||||
failureDates[url] = Date()
|
||||
queue.runInDatabase { result in
|
||||
guard let database = try? result.get() else {
|
||||
return
|
||||
}
|
||||
queue.runInDatabase { database in
|
||||
DownloadFailureTable.save(url: url, statusCode: statusCode, database: database)
|
||||
}
|
||||
}
|
||||
@@ -127,10 +115,7 @@ import RSDatabaseObjC
|
||||
guard failureDates.removeValue(forKey: url) != nil else {
|
||||
return
|
||||
}
|
||||
queue.runInDatabase { result in
|
||||
guard let database = try? result.get() else {
|
||||
return
|
||||
}
|
||||
queue.runInDatabase { database in
|
||||
DownloadFailureTable.clear(url: url, database: database)
|
||||
}
|
||||
}
|
||||
@@ -147,10 +132,7 @@ private extension ImageMetadataDatabase {
|
||||
nonisolated(unsafe) var failureDuration: TimeInterval = 0
|
||||
|
||||
let totalStart = Date()
|
||||
queue.runInDatabaseSync { result in
|
||||
guard let database = try? result.get() else {
|
||||
return
|
||||
}
|
||||
queue.runInDatabaseSync { database in
|
||||
var stepStart = Date()
|
||||
homePageRecords = HomePageFaviconTable.fetchAll(database: database)
|
||||
homePageDuration = Date().timeIntervalSince(stepStart)
|
||||
|
||||
@@ -9,38 +9,11 @@
|
||||
import Foundation
|
||||
import RSDatabaseObjC
|
||||
|
||||
/// Result type that provides an FMDatabase or an Error.
|
||||
public typealias DatabaseResult = Result<FMDatabase, Error>
|
||||
|
||||
/// Block that executes database code or handles an Error.
|
||||
public typealias DatabaseBlock = @Sendable (DatabaseResult) -> Void
|
||||
/// Block that executes database code.
|
||||
public typealias DatabaseBlock = @Sendable (FMDatabase) -> Void
|
||||
|
||||
/// Completion block that provides an optional Error.
|
||||
public typealias DatabaseCompletionBlock = @Sendable (Error?) -> Void
|
||||
|
||||
/// Dictionary representing one row of database values, keyed by column name.
|
||||
public typealias DatabaseDictionary = [String: Any]
|
||||
|
||||
// MARK: - Extensions
|
||||
|
||||
public extension DatabaseResult {
|
||||
/// Convenience for getting the database from a DatabaseResult.
|
||||
var database: FMDatabase? {
|
||||
switch self {
|
||||
case .success(let database):
|
||||
return database
|
||||
case .failure:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
/// Convenience for getting the error from a DatabaseResult.
|
||||
var error: Error? {
|
||||
switch self {
|
||||
case .success:
|
||||
return nil
|
||||
case .failure(let error):
|
||||
return error
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,28 +87,17 @@ public final class DatabaseQueue: Sendable {
|
||||
|
||||
/// Run all the lines that start with "create".
|
||||
/// Use this to create tables, indexes, etc.
|
||||
public func runCreateStatements(_ statements: String) throws {
|
||||
nonisolated(unsafe) var error: Error?
|
||||
|
||||
runInDatabaseSync { result in
|
||||
public func runCreateStatements(_ statements: String) {
|
||||
runInDatabaseSync { database in
|
||||
Self.logger.debug("DatabaseQueue: runCreateStatements")
|
||||
|
||||
switch result {
|
||||
case .success(let database):
|
||||
statements.enumerateLines { (line, stop) in
|
||||
if line.lowercased().hasPrefix("create") {
|
||||
database.executeStatements(line)
|
||||
}
|
||||
stop = false
|
||||
statements.enumerateLines { (line, stop) in
|
||||
if line.lowercased().hasPrefix("create") {
|
||||
database.executeStatements(line)
|
||||
}
|
||||
case .failure(let databaseError):
|
||||
error = databaseError
|
||||
stop = false
|
||||
}
|
||||
}
|
||||
|
||||
if let error {
|
||||
throw(error)
|
||||
}
|
||||
}
|
||||
|
||||
/// Compact the database. This should be done from time to time —
|
||||
@@ -117,10 +106,8 @@ public final class DatabaseQueue: Sendable {
|
||||
/// since the last vacuum() call.
|
||||
public func vacuum() async {
|
||||
await withCheckedContinuation { continuation in
|
||||
runInDatabase { result in
|
||||
if let database = try? result.get() {
|
||||
database.vacuum()
|
||||
}
|
||||
runInDatabase { database in
|
||||
database.vacuum()
|
||||
continuation.resume()
|
||||
}
|
||||
}
|
||||
@@ -141,7 +128,7 @@ private extension DatabaseQueue {
|
||||
if useTransaction {
|
||||
state.database.beginTransaction()
|
||||
}
|
||||
databaseBlock(.success(state.database))
|
||||
databaseBlock(state.database)
|
||||
if useTransaction {
|
||||
state.database.commit()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user