mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-03 10:47:21 +00:00
Make ImageMetadataDatabase the sole source of metadata info. Make it @MainActor so it’s easier to use from other code.
This commit is contained in:
1 parent
1ad3a09680
commit
41b3883906
1 file changed
+98
-30
@@ -11,17 +11,19 @@ import RSDatabase
|
||||
import RSDatabaseObjC
|
||||
|
||||
/// Per-URL image metadata: discovered URLs and failure tracking.
|
||||
public actor ImageMetadataDatabase {
|
||||
@MainActor public final class ImageMetadataDatabase {
|
||||
|
||||
@MainActor public static let shared: ImageMetadataDatabase = {
|
||||
let databasePath = AppConfig.dataFolder
|
||||
.appendingPathComponent("ImageMetadata.db").path
|
||||
return ImageMetadataDatabase(databasePath: databasePath)
|
||||
}()
|
||||
public static let shared = ImageMetadataDatabase()
|
||||
|
||||
public static let failureRetryDays = 5
|
||||
private static let failureRetentionDays = 33
|
||||
|
||||
private let database: FMDatabase
|
||||
private let queue: DatabaseQueue
|
||||
|
||||
private var homePageToFaviconURL = [String: String]()
|
||||
private var homePagesWithNoFavicon = Set<String>()
|
||||
private var feedURLToIconURL = [String: String]()
|
||||
private var failureDates = [String: Date]()
|
||||
|
||||
private static let tableCreationStatements = """
|
||||
CREATE TABLE IF NOT EXISTS downloadFailure (url TEXT PRIMARY KEY NOT NULL, statusCode INTEGER, lastChecked REAL NOT NULL);
|
||||
@@ -29,57 +31,123 @@ public actor ImageMetadataDatabase {
|
||||
CREATE TABLE IF NOT EXISTS feedIconURL (feedURL TEXT PRIMARY KEY NOT NULL, iconURL TEXT NOT NULL, lastChecked REAL NOT NULL);
|
||||
"""
|
||||
|
||||
private init(databasePath: String) {
|
||||
let database = FMDatabase.openAndSetUpDatabase(path: databasePath)
|
||||
database.executeStatements("PRAGMA journal_mode = WAL;")
|
||||
database.runCreateStatements(Self.tableCreationStatements)
|
||||
self.database = database
|
||||
private init() {
|
||||
let databasePath = AppConfig.dataFolder
|
||||
.appendingPathComponent("ImageMetadata.db").path
|
||||
let queue = DatabaseQueue(databasePath: databasePath)
|
||||
try? queue.runCreateStatements(Self.tableCreationStatements)
|
||||
self.queue = queue
|
||||
|
||||
Task {
|
||||
await performStartupMaintenance()
|
||||
loadCachesFromDatabase()
|
||||
|
||||
let cutoff = Date().timeIntervalSince1970 - TimeInterval(days: Self.failureRetentionDays)
|
||||
queue.runInDatabase { result in
|
||||
guard let database = try? result.get() else {
|
||||
return
|
||||
}
|
||||
DownloadFailureTable.removeExpired(olderThan: cutoff, database: database)
|
||||
database.vacuumIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - HomePageFavicon
|
||||
|
||||
public func allHomePageFavicons() -> [HomePageFaviconRecord] {
|
||||
HomePageFaviconTable.fetchAll(database: database)
|
||||
public func faviconURL(forHomePageURL homePageURL: String) -> String? {
|
||||
homePageToFaviconURL[homePageURL]
|
||||
}
|
||||
|
||||
public func homePageHasNoFavicon(_ homePageURL: String) -> Bool {
|
||||
homePagesWithNoFavicon.contains(homePageURL)
|
||||
}
|
||||
|
||||
public func saveHomePageFavicon(homePageURL: String, faviconURL: String?) {
|
||||
HomePageFaviconTable.save(homePageURL: homePageURL, faviconURL: faviconURL, database: database)
|
||||
if let faviconURL {
|
||||
homePageToFaviconURL[homePageURL] = faviconURL
|
||||
homePagesWithNoFavicon.remove(homePageURL)
|
||||
} else {
|
||||
homePagesWithNoFavicon.insert(homePageURL)
|
||||
homePageToFaviconURL.removeValue(forKey: homePageURL)
|
||||
}
|
||||
queue.runInDatabase { result in
|
||||
guard let database = try? result.get() else {
|
||||
return
|
||||
}
|
||||
HomePageFaviconTable.save(homePageURL: homePageURL, faviconURL: faviconURL, database: database)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - FeedIconURL
|
||||
|
||||
public func allFeedIconURLs() -> [String: String] {
|
||||
FeedIconURLTable.fetchAll(database: database)
|
||||
public func iconURL(forFeedURL feedURL: String) -> String? {
|
||||
feedURLToIconURL[feedURL]
|
||||
}
|
||||
|
||||
public func saveFeedIconURL(feedURL: String, iconURL: String) {
|
||||
FeedIconURLTable.save(feedURL: feedURL, iconURL: iconURL, database: database)
|
||||
feedURLToIconURL[feedURL] = iconURL
|
||||
queue.runInDatabase { result in
|
||||
guard let database = try? result.get() else {
|
||||
return
|
||||
}
|
||||
FeedIconURLTable.save(feedURL: feedURL, iconURL: iconURL, database: database)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - DownloadFailure
|
||||
|
||||
public func allDownloadFailures() -> [String: Date] {
|
||||
DownloadFailureTable.fetchAll(database: database)
|
||||
public func recentlyFailed(url: String) -> Bool {
|
||||
guard let lastFailure = failureDates[url] else {
|
||||
return false
|
||||
}
|
||||
return Date().timeIntervalSince(lastFailure) < TimeInterval(days: Self.failureRetryDays)
|
||||
}
|
||||
|
||||
public func recordDownloadFailure(url: String, statusCode: Int?) {
|
||||
DownloadFailureTable.save(url: url, statusCode: statusCode, database: database)
|
||||
public func recordFailure(url: String, statusCode: Int?) {
|
||||
failureDates[url] = Date()
|
||||
queue.runInDatabase { result in
|
||||
guard let database = try? result.get() else {
|
||||
return
|
||||
}
|
||||
DownloadFailureTable.save(url: url, statusCode: statusCode, database: database)
|
||||
}
|
||||
}
|
||||
|
||||
public func clearDownloadFailure(url: String) {
|
||||
DownloadFailureTable.clear(url: url, database: database)
|
||||
public func clearFailure(url: String) {
|
||||
guard failureDates.removeValue(forKey: url) != nil else {
|
||||
return
|
||||
}
|
||||
queue.runInDatabase { result in
|
||||
guard let database = try? result.get() else {
|
||||
return
|
||||
}
|
||||
DownloadFailureTable.clear(url: url, database: database)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension ImageMetadataDatabase {
|
||||
|
||||
func performStartupMaintenance() {
|
||||
let cutoff = Date().timeIntervalSince1970 - TimeInterval(days: Self.failureRetentionDays)
|
||||
DownloadFailureTable.removeExpired(olderThan: cutoff, database: database)
|
||||
database.vacuumIfNeeded()
|
||||
func loadCachesFromDatabase() {
|
||||
nonisolated(unsafe) var homePageRecords = [HomePageFaviconRecord]()
|
||||
nonisolated(unsafe) var feedIcons = [String: String]()
|
||||
nonisolated(unsafe) var failures = [String: Date]()
|
||||
|
||||
queue.runInDatabaseSync { result in
|
||||
guard let database = try? result.get() else {
|
||||
return
|
||||
}
|
||||
homePageRecords = HomePageFaviconTable.fetchAll(database: database)
|
||||
feedIcons = FeedIconURLTable.fetchAll(database: database)
|
||||
failures = DownloadFailureTable.fetchAll(database: database)
|
||||
}
|
||||
|
||||
for record in homePageRecords {
|
||||
if let faviconURL = record.faviconURL {
|
||||
homePageToFaviconURL[record.homePageURL] = faviconURL
|
||||
} else {
|
||||
homePagesWithNoFavicon.insert(record.homePageURL)
|
||||
}
|
||||
}
|
||||
feedURLToIconURL = feedIcons
|
||||
failureDates = failures
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user