NetNewsWire/Frameworks/ArticlesDatabase/StatusesTable.swift

239 lines
5.9 KiB
Swift
Raw Normal View History

//
// StatusesTable.swift
2018-08-29 07:18:24 +02:00
// NetNewsWire
//
// Created by Brent Simmons on 5/8/16.
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import RSCore
import RSDatabase
import Articles
2017-08-06 21:37:47 +02:00
// Article->ArticleStatus is a to-one relationship.
//
// CREATE TABLE if not EXISTS statuses (articleID TEXT NOT NULL PRIMARY KEY, read BOOL NOT NULL DEFAULT 0, starred BOOL NOT NULL DEFAULT 0, userDeleted BOOL NOT NULL DEFAULT 0, dateArrived DATE NOT NULL DEFAULT 0);
2017-08-06 21:37:47 +02:00
final class StatusesTable: DatabaseTable {
2017-07-30 20:22:21 +02:00
let name = DatabaseTableName.statuses
private let cache = StatusCache()
2017-09-05 17:53:45 +02:00
private let queue: RSDatabaseQueue
init(queue: RSDatabaseQueue) {
self.queue = queue
}
// MARK: Creating/Updating
func ensureStatusesForArticleIDs(_ articleIDs: Set<String>, _ read: Bool, _ database: FMDatabase) -> [String: ArticleStatus] {
2017-09-05 17:53:45 +02:00
// Check cache.
let articleIDsMissingCachedStatus = articleIDsWithNoCachedStatus(articleIDs)
if articleIDsMissingCachedStatus.isEmpty {
return statusesDictionary(articleIDs)
}
2017-09-05 17:53:45 +02:00
// Check database.
fetchAndCacheStatusesForArticleIDs(articleIDsMissingCachedStatus, database)
2017-09-05 17:53:45 +02:00
let articleIDsNeedingStatus = self.articleIDsWithNoCachedStatus(articleIDs)
if !articleIDsNeedingStatus.isEmpty {
// Create new statuses.
self.createAndSaveStatusesForArticleIDs(articleIDsNeedingStatus, read, database)
}
return statusesDictionary(articleIDs)
}
// MARK: Marking
func mark(_ statuses: Set<ArticleStatus>, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ database: FMDatabase) -> Set<ArticleStatus>? {
2017-09-16 20:04:29 +02:00
// Sets flag in both memory and in database.
var updatedStatuses = Set<ArticleStatus>()
for status in statuses {
if status.boolStatus(forKey: statusKey) == flag {
continue
}
status.setBoolStatus(flag, forKey: statusKey)
updatedStatuses.insert(status)
2017-09-16 20:04:29 +02:00
}
if updatedStatuses.isEmpty {
return nil
2017-09-16 20:04:29 +02:00
}
let articleIDs = updatedStatuses.articleIDs()
self.markArticleIDs(articleIDs, statusKey, flag, database)
return updatedStatuses
}
// MARK: Fetching
func fetchUnreadArticleIDs() -> Set<String> {
return fetchArticleIDs("select articleID from statuses where read=0 and userDeleted=0;")
}
func fetchStarredArticleIDs() -> Set<String> {
return fetchArticleIDs("select articleID from statuses where starred=1 and userDeleted=0;")
}
func fetchArticleIDsForStatusesWithoutArticles() -> Set<String> {
return fetchArticleIDs("select articleID from statuses s where (read=0 or starred=1) and userDeleted=0 and not exists (select 1 from articles a where a.articleID = s.articleID);")
}
func fetchArticleIDs(_ sql: String) -> Set<String> {
var statuses: Set<String>? = nil
queue.fetchSync { (database) in
if let resultSet = database.executeQuery(sql, withArgumentsIn: nil) {
statuses = resultSet.mapToSet(self.articleIDWithRow)
}
}
return statuses != nil ? statuses! : Set<String>()
}
func articleIDWithRow(_ row: FMResultSet) -> String? {
return row.string(forColumn: DatabaseKey.articleID)
}
func statusWithRow(_ row: FMResultSet) -> ArticleStatus? {
2017-08-06 21:37:47 +02:00
guard let articleID = row.string(forColumn: DatabaseKey.articleID) else {
return nil
}
if let cachedStatus = cache[articleID] {
return cachedStatus
}
guard let dateArrived = row.date(forColumn: DatabaseKey.dateArrived) else {
return nil
2017-08-06 21:37:47 +02:00
}
let articleStatus = ArticleStatus(articleID: articleID, dateArrived: dateArrived, row: row)
cache.addStatusIfNotCached(articleStatus)
return articleStatus
}
func statusesDictionary(_ articleIDs: Set<String>) -> [String: ArticleStatus] {
var d = [String: ArticleStatus]()
for articleID in articleIDs {
if let articleStatus = cache[articleID] {
d[articleID] = articleStatus
}
}
return d
}
}
// MARK: - Private
private extension StatusesTable {
2017-09-05 17:53:45 +02:00
// MARK: Cache
func articleIDsWithNoCachedStatus(_ articleIDs: Set<String>) -> Set<String> {
return Set(articleIDs.filter { cache[$0] == nil })
}
// MARK: Creating
func saveStatuses(_ statuses: Set<ArticleStatus>, _ database: FMDatabase) {
2017-08-27 00:37:15 +02:00
let statusArray = statuses.map { $0.databaseDictionary()! }
self.insertRows(statusArray, insertType: .orIgnore, in: database)
2017-08-27 00:37:15 +02:00
}
func createAndSaveStatusesForArticleIDs(_ articleIDs: Set<String>, _ read: Bool, _ database: FMDatabase) {
2017-08-27 00:37:15 +02:00
let now = Date()
let statuses = Set(articleIDs.map { ArticleStatus(articleID: $0, read: read, dateArrived: now) })
2017-09-05 17:53:45 +02:00
cache.addIfNotCached(statuses)
saveStatuses(statuses, database)
2017-08-27 00:37:15 +02:00
}
func fetchAndCacheStatusesForArticleIDs(_ articleIDs: Set<String>, _ database: FMDatabase) {
2017-09-05 17:53:45 +02:00
guard let resultSet = self.selectRowsWhere(key: DatabaseKey.articleID, inValues: Array(articleIDs), in: database) else {
return
}
let statuses = resultSet.mapToSet(self.statusWithRow)
self.cache.addIfNotCached(statuses)
}
2017-09-16 20:04:29 +02:00
// MARK: Marking
func markArticleIDs(_ articleIDs: Set<String>, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ database: FMDatabase) {
2017-09-16 20:04:29 +02:00
updateRowsWithValue(NSNumber(value: flag), valueKey: statusKey.rawValue, whereKey: DatabaseKey.articleID, matches: Array(articleIDs), database: database)
2017-09-16 20:04:29 +02:00
}
}
// MARK: -
private final class StatusCache {
// Serial database queue only.
var dictionary = [String: ArticleStatus]()
2017-11-20 01:28:26 +01:00
var cachedStatuses: Set<ArticleStatus> {
return Set(dictionary.values)
2017-11-20 01:28:26 +01:00
}
func add(_ statuses: Set<ArticleStatus>) {
// Replaces any cached statuses.
for status in statuses {
self[status.articleID] = status
}
}
func addStatusIfNotCached(_ status: ArticleStatus) {
addIfNotCached(Set([status]))
}
2017-09-05 17:53:45 +02:00
func addIfNotCached(_ statuses: Set<ArticleStatus>) {
// Does not replace already cached statuses.
for status in statuses {
let articleID = status.articleID
if let _ = self[articleID] {
continue
}
2017-09-05 17:53:45 +02:00
self[articleID] = status
}
}
2017-11-20 01:28:26 +01:00
subscript(_ articleID: String) -> ArticleStatus? {
get {
return dictionary[articleID]
}
set {
dictionary[articleID] = newValue
}
}
}