Parse, save, post HTML thumbnail URLs in batches of 100.

This commit is contained in:
Brent Simmons committed 2026-10-31 11:56:03 -07:00
1 parent b00dce516d
commit 210474f6ac
2 files changed
+39 -11

No files matched your search

@@ -87,6 +87,8 @@ extension Notification.Name {
#endif
/// HTML images saved per article, so one that fails or is too small falls through to the next.
nonisolated private static let maxHTMLThumbnailURLsPerArticle = 3
/// Articles parsed and saved at a time, which also limits how long a save holds the database queue.
static let parsingBatchSize = 100
/// Entries in htmlThumbnailURLs, a few hundred bytes each. Past this, it’s emptied, and the next discovery reloads its articles from the database.
private static let maxCachedArticleCount = 50_000
/// Entries per mode in chosenThumbnailURLs. Past this, that mode’s entries are emptied.
@@ -155,8 +157,9 @@ extension Notification.Name {
/// Looks for thumbnails in the HTML of articles that have no usable provided image.
/// Results saved in the database are ready when this returns, so that rows laid out next can use them.
/// This blocks until the database has answered.
/// The rest are parsed off the main thread and saved, and `.articleThumbnailURLsDidBecomeAvailable` is posted
/// for those that have thumbnails. The returned task finishes after that.
/// The rest are parsed off the main thread and saved in batches, in the order given, so the first rows don’t wait for the last.
/// `.articleThumbnailURLsDidBecomeAvailable` is posted after each batch for those that have thumbnails.
/// The returned task finishes after the last batch.
@discardableResult public func findThumbnailURLs(for articles: some Collection<Article>) -> Task<Void, Never> {
if htmlThumbnailURLs.count > Self.maxCachedArticleCount {
htmlThumbnailURLs.removeAll()
@@ -175,16 +178,16 @@ extension Notification.Name {
defer {
articleIDsBeingDiscovered.subtract(articleIDs)
}
guard !articleHTMLs.isEmpty else {
return
}
let parsedRecords = await Self.parseThumbnailURLs(articleHTMLs)
cacheRecords(parsedRecords)
imageDatabase.saveArticleThumbnails(parsedRecords)
for batchStart in stride(from: 0, to: articleHTMLs.count, by: Self.parsingBatchSize) {
let batchEnd = min(batchStart + Self.parsingBatchSize, articleHTMLs.count)
let parsedRecords = await Self.parseThumbnailURLs(Array(articleHTMLs[batchStart..<batchEnd]))
cacheRecords(parsedRecords)
imageDatabase.saveArticleThumbnails(parsedRecords)
let foundArticleIDs = Set(parsedRecords.filter { !$0.thumbnailURLs.isEmpty }.map(\.articleID))
if !foundArticleIDs.isEmpty {
NotificationCenter.default.post(name: .articleThumbnailURLsDidBecomeAvailable, object: self, userInfo: [Self.articleIDsKey: foundArticleIDs])
let foundArticleIDs = Set(parsedRecords.filter { !$0.thumbnailURLs.isEmpty }.map(\.articleID))
if !foundArticleIDs.isEmpty {
NotificationCenter.default.post(name: .articleThumbnailURLsDidBecomeAvailable, object: self, userInfo: [Self.articleIDsKey: foundArticleIDs])
}
}
}
}
@@ -137,6 +137,16 @@ private typealias ArticleAttachment = Articles.Attachment
#expect(finderAfterRelaunch.thumbnailURL(for: newArticle, mode: .large) == "https://example.com/uploads/new.jpg")
}
@Test func parsedThumbnailURLsArePostedInBatches() async {
let finder = ArticleThumbnailFinder(imageDatabase: ImageDatabase(databasePath: ":memory:"))
let articles = (0...ArticleThumbnailFinder.parsingBatchSize).map { makeArticle(articleID: String($0), contentHTML: #"<p><img src="/uploads/photo.jpg" width="640" height="480"></p>"#) }
let recorder = ThumbnailURLNotificationRecorder(finder: finder)
await finder.findThumbnailURLs(for: articles).value
#expect(recorder.articleIDCounts == [ArticleThumbnailFinder.parsingBatchSize, 1])
}
@Test func thumbnailTooSmallIsRejected() async throws {
let smallImageData = try #require(jpegData(width: 48, height: 48))
@@ -179,6 +189,21 @@ private typealias ArticleAttachment = Articles.Attachment
// MARK: - Helpers
@MainActor private final class ThumbnailURLNotificationRecorder {
private(set) var articleIDCounts = [Int]()
init(finder: ArticleThumbnailFinder) {
NotificationCenter.default.addObserver(self, selector: #selector(handleArticleThumbnailURLsDidBecomeAvailable(_:)), name: .articleThumbnailURLsDidBecomeAvailable, object: finder)
}
@objc func handleArticleThumbnailURLsDidBecomeAvailable(_ notification: Notification) {
if let articleIDs = notification.userInfo?[ArticleThumbnailFinder.articleIDsKey] as? Set<String> {
articleIDCounts.append(articleIDs.count)
}
}
}
private func makeArticle(articleID: String = "1", contentHTML: String? = nil, imageURL: String? = nil, thumbnailURL: String? = nil, attachments: [ArticleAttachment]? = nil) -> Article {
let status = ArticleStatus(articleID: articleID, read: false, starred: false, dateArrived: Date())
return Article(accountID: "account", articleID: articleID, feedID: "feed", uniqueID: articleID, title: nil, contentHTML: contentHTML, contentText: nil, markdown: nil, url: "https://example.com/posts/\(articleID)", externalURL: nil, summary: nil, imageURL: imageURL, thumbnailURL: thumbnailURL, datePublished: nil, dateModified: nil, authors: nil, attachments: attachments, status: status)