Create and use ArticlesDatabase.RetentionStyle enum.

This commit is contained in:
Brent Simmons 2020-03-29 18:51:03 -07:00
parent 715671edad
commit 2c4ee99dc2
3 changed files with 26 additions and 8 deletions

View File

@ -251,7 +251,8 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
self.dataFolder = dataFolder self.dataFolder = dataFolder
let databaseFilePath = (dataFolder as NSString).appendingPathComponent("DB.sqlite3") let databaseFilePath = (dataFolder as NSString).appendingPathComponent("DB.sqlite3")
self.database = ArticlesDatabase(databaseFilePath: databaseFilePath, accountID: accountID) let retentionStyle: ArticlesDatabase.RetentionStyle = type == .onMyMac ? .feedBased : .syncSystem
self.database = ArticlesDatabase(databaseFilePath: databaseFilePath, accountID: accountID, retentionStyle: retentionStyle)
switch type { switch type {
case .onMyMac: case .onMyMac:

View File

@ -43,14 +43,21 @@ public typealias ArticleStatusesResultBlock = (ArticleStatusesResult) -> Void
public final class ArticlesDatabase { public final class ArticlesDatabase {
public enum RetentionStyle {
case feedBased // Local and iCloud: article retention is defined by contents of feed
case syncSystem // Feedbin, Feedly, etc.: article retention is defined by external system
}
private let articlesTable: ArticlesTable private let articlesTable: ArticlesTable
private let queue: DatabaseQueue private let queue: DatabaseQueue
private let operationQueue = MainThreadOperationQueue() private let operationQueue = MainThreadOperationQueue()
private let retentionStyle: RetentionStyle
public init(databaseFilePath: String, accountID: String) { public init(databaseFilePath: String, accountID: String, retentionStyle: RetentionStyle) {
let queue = DatabaseQueue(databasePath: databaseFilePath) let queue = DatabaseQueue(databasePath: databaseFilePath)
self.queue = queue self.queue = queue
self.articlesTable = ArticlesTable(name: DatabaseTableName.articles, accountID: accountID, queue: queue) self.articlesTable = ArticlesTable(name: DatabaseTableName.articles, accountID: accountID, queue: queue, retentionStyle: retentionStyle)
self.retentionStyle = retentionStyle
try! queue.runCreateStatements(ArticlesDatabase.tableCreationStatements) try! queue.runCreateStatements(ArticlesDatabase.tableCreationStatements)
queue.runInDatabase { databaseResult in queue.runInDatabase { databaseResult in
@ -62,7 +69,6 @@ public final class ArticlesDatabase {
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;DROP TABLE if EXISTS attachments;DROP TABLE if EXISTS attachmentsLookup;") 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;DROP TABLE if EXISTS attachments;DROP TABLE if EXISTS attachmentsLookup;")
} }
// queue.vacuumIfNeeded(daysBetweenVacuums: 9) // TODO: restore this after we do database cleanups.
DispatchQueue.main.async { DispatchQueue.main.async {
self.articlesTable.indexUnindexedArticles() self.articlesTable.indexUnindexedArticles()
} }
@ -183,8 +189,14 @@ public final class ArticlesDatabase {
// MARK: - Saving and Updating Articles // MARK: - Saving and Updating Articles
/// Update articles and save new ones. /// Update articles and save new ones  for feed-based systems (local and iCloud).
public func update(with feed: ParsedFeed, completion: @escaping UpdateArticlesCompletionBlock) {
precondition(retentionStyle == .feedBased)
}
/// Update articles and save new ones for sync systems (Feedbin, Feedly, etc.).
public func update(webFeedIDsAndItems: [String: Set<ParsedItem>], defaultRead: Bool, completion: @escaping UpdateArticlesCompletionBlock) { public func update(webFeedIDsAndItems: [String: Set<ParsedItem>], defaultRead: Bool, completion: @escaping UpdateArticlesCompletionBlock) {
precondition(retentionStyle == .syncSystem)
articlesTable.update(webFeedIDsAndItems, defaultRead, completion) articlesTable.update(webFeedIDsAndItems, defaultRead, completion)
} }
@ -219,6 +231,7 @@ public final class ArticlesDatabase {
articlesTable.createStatusesIfNeeded(articleIDs, completion) articlesTable.createStatusesIfNeeded(articleIDs, completion)
} }
#if os(iOS)
// MARK: - Suspend and Resume (for iOS) // MARK: - Suspend and Resume (for iOS)
/// Cancel current operations and close the database. /// Cancel current operations and close the database.
@ -239,7 +252,8 @@ public final class ArticlesDatabase {
queue.resume() queue.resume()
operationQueue.resume() operationQueue.resume()
} }
#endif
// MARK: - Caches // MARK: - Caches
/// Call to free up some memory. Should be done when the app is backgrounded, for instance. /// Call to free up some memory. Should be done when the app is backgrounded, for instance.

View File

@ -19,6 +19,8 @@ final class ArticlesTable: DatabaseTable {
private let queue: DatabaseQueue private let queue: DatabaseQueue
private let statusesTable: StatusesTable private let statusesTable: StatusesTable
private let authorsLookupTable: DatabaseLookupTable private let authorsLookupTable: DatabaseLookupTable
private let retentionStyle: ArticlesDatabase.RetentionStyle
private var articlesCache = [String: Article]() private var articlesCache = [String: Article]()
private lazy var searchTable: SearchTable = { private lazy var searchTable: SearchTable = {
@ -30,13 +32,14 @@ final class ArticlesTable: DatabaseTable {
private typealias ArticlesFetchMethod = (FMDatabase) -> Set<Article> private typealias ArticlesFetchMethod = (FMDatabase) -> Set<Article>
init(name: String, accountID: String, queue: DatabaseQueue) { init(name: String, accountID: String, queue: DatabaseQueue, retentionStyle: ArticlesDatabase.RetentionStyle) {
self.name = name self.name = name
self.accountID = accountID self.accountID = accountID
self.queue = queue self.queue = queue
self.statusesTable = StatusesTable(queue: queue) self.statusesTable = StatusesTable(queue: queue)
self.retentionStyle = retentionStyle
let authorsTable = AuthorsTable(name: DatabaseTableName.authors) let authorsTable = AuthorsTable(name: DatabaseTableName.authors)
self.authorsLookupTable = DatabaseLookupTable(name: DatabaseTableName.authorsLookup, objectIDKey: DatabaseKey.articleID, relatedObjectIDKey: DatabaseKey.authorID, relatedTable: authorsTable, relationshipName: RelationshipName.authors) self.authorsLookupTable = DatabaseLookupTable(name: DatabaseTableName.authorsLookup, objectIDKey: DatabaseKey.articleID, relatedObjectIDKey: DatabaseKey.authorID, relatedTable: authorsTable, relationshipName: RelationshipName.authors)
} }