2017-07-03 20:20:14 +02:00
//
2017-07-29 21:08:10 +02:00
// A r t i c l e s T a b l e . s w i f t
2017-07-03 20:20:14 +02:00
// E v e r g r e e n
//
// C r e a t e d b y B r e n t S i m m o n s o n 5 / 9 / 1 6 .
// C o p y r i g h t © 2 0 1 6 R a n c h e r o S o f t w a r e , L L C . A l l r i g h t s r e s e r v e d .
//
import Foundation
2017-08-23 22:23:12 +02:00
import RSCore
2017-07-29 21:29:05 +02:00
import RSDatabase
2017-08-23 22:23:12 +02:00
import RSParser
2017-07-03 20:20:14 +02:00
import Data
2017-08-21 22:31:14 +02:00
final class ArticlesTable : DatabaseTable {
let name : String
2017-08-27 00:37:15 +02:00
private weak var account : Account ?
private let queue : RSDatabaseQueue
2017-09-02 23:19:42 +02:00
private let statusesTable = StatusesTable ( )
2017-08-23 22:23:12 +02:00
private let authorsLookupTable : DatabaseLookupTable
private let attachmentsLookupTable : DatabaseLookupTable
private let tagsLookupTable : DatabaseLookupTable
2017-08-24 06:30:28 +02:00
private let articleCache = ArticleCache ( )
2017-08-31 22:35:48 +02:00
// TODO: u p d a t e a r t i c l e C u t o f f D a t e a s t i m e p a s s e s a n d b a s e d o n u s e r p r e f e r e n c e s .
private var articleCutoffDate = NSDate . rs_dateWithNumberOfDays ( inThePast : 3 * 31 ) !
2017-09-05 02:10:02 +02:00
private var maximumArticleCutoffDate = NSDate . rs_dateWithNumberOfDays ( inThePast : 4 * 31 ) !
2017-08-31 22:35:48 +02:00
2017-08-27 00:37:15 +02:00
init ( name : String , account : Account , queue : RSDatabaseQueue ) {
2017-07-29 21:29:05 +02:00
2017-08-21 22:31:14 +02:00
self . name = name
2017-08-27 00:37:15 +02:00
self . account = account
self . queue = queue
2017-08-23 22:23:12 +02:00
let authorsTable = AuthorsTable ( name : DatabaseTableName . authors )
self . authorsLookupTable = DatabaseLookupTable ( name : DatabaseTableName . authorsLookup , objectIDKey : DatabaseKey . articleID , relatedObjectIDKey : DatabaseKey . authorID , relatedTable : authorsTable , relationshipName : RelationshipName . authors )
let tagsTable = TagsTable ( name : DatabaseTableName . tags )
self . tagsLookupTable = DatabaseLookupTable ( name : DatabaseTableName . tags , objectIDKey : DatabaseKey . articleID , relatedObjectIDKey : DatabaseKey . tagName , relatedTable : tagsTable , relationshipName : RelationshipName . tags )
let attachmentsTable = AttachmentsTable ( name : DatabaseTableName . attachments )
self . attachmentsLookupTable = DatabaseLookupTable ( name : DatabaseTableName . attachmentsLookup , objectIDKey : DatabaseKey . articleID , relatedObjectIDKey : DatabaseKey . attachmentID , relatedTable : attachmentsTable , relationshipName : RelationshipName . attachments )
2017-08-21 22:31:14 +02:00
}
2017-07-29 21:29:05 +02:00
2017-08-21 22:31:14 +02:00
// MARK: F e t c h i n g
2017-08-23 22:23:12 +02:00
func fetchArticles ( _ feed : Feed ) -> Set < Article > {
2017-08-21 22:31:14 +02:00
2017-08-27 00:37:15 +02:00
let feedID = feed . feedID
2017-08-27 22:03:15 +02:00
var articles = Set < Article > ( )
2017-08-27 00:37:15 +02:00
queue . fetchSync { ( database : FMDatabase ! ) -> Void in
2017-09-05 02:10:02 +02:00
articles = self . fetchArticlesForFeedID ( feedID , withLimits : true , database : database )
2017-08-27 00:37:15 +02:00
}
2017-08-27 22:03:15 +02:00
return articleCache . uniquedArticles ( articles )
2017-08-21 22:31:14 +02:00
}
2017-09-05 02:10:02 +02:00
func fetchArticlesAsync ( _ feed : Feed , withLimits : Bool , _ resultBlock : @ escaping ArticleResultBlock ) {
2017-08-21 22:31:14 +02:00
2017-08-27 00:37:15 +02:00
let feedID = feed . feedID
queue . fetch { ( database : FMDatabase ! ) -> Void in
2017-09-05 02:10:02 +02:00
let fetchedArticles = self . fetchArticlesForFeedID ( feedID , withLimits : withLimits , database : database )
2017-08-27 00:37:15 +02:00
DispatchQueue . main . async {
let articles = self . articleCache . uniquedArticles ( fetchedArticles )
resultBlock ( articles )
}
}
2017-08-21 22:31:14 +02:00
}
2017-08-27 22:03:15 +02:00
func fetchUnreadArticles ( for feeds : Set < Feed > ) -> Set < Article > {
return fetchUnreadArticles ( feeds . feedIDs ( ) )
2017-08-21 22:31:14 +02:00
}
2017-09-01 22:31:27 +02:00
2017-08-23 22:23:12 +02:00
// MARK: U p d a t i n g
func update ( _ feed : Feed , _ parsedFeed : ParsedFeed , _ completion : @ escaping RSVoidCompletionBlock ) {
2017-09-02 23:19:42 +02:00
if parsedFeed . items . isEmpty {
completion ( )
return
}
2017-09-05 02:10:02 +02:00
// 1 . E n s u r e s t a t u s e s f o r a l l t h e p a r s e d I t e m s .
// 2 . F e t c h a l l a r t i c l e s f o r t h e f e e d .
// 3 . F o r e a c h p a r s e d I t e m :
// - i f u s e r D e l e t e d | | ( ! s t a r r e d & & s t a t u s . d a t e A r r i v e d < c u t o f f ) , t h e n i g n o r e
// - i f m a t c h e s e x i s t i n g a r t i c l e , t h e n u p d a t e d a t a b a s e w i t h c h a n g e s b e t w e e n t h e t w o
// - i f n e w , c r e a t e a r t i c l e a n d s a v e i n d a t a b a s e
fetchArticlesAsync ( feed , withLimits : false ) { ( articles ) in
2017-09-02 23:19:42 +02:00
self . updateArticles ( articles . dictionary ( ) , parsedFeed . itemsDictionary ( with : feed ) , feed , completion )
}
2017-08-23 22:23:12 +02:00
}
2017-09-01 22:31:27 +02:00
2017-08-23 22:23:12 +02:00
// MARK: U n r e a d C o u n t s
func fetchUnreadCounts ( _ feeds : Set < Feed > , _ completion : @ escaping UnreadCountCompletionBlock ) {
2017-09-01 22:31:27 +02:00
let feedIDs = feeds . feedIDs ( )
var unreadCountTable = UnreadCountTable ( )
queue . fetch { ( database ) in
for feedID in feedIDs {
unreadCountTable [ feedID ] = self . fetchUnreadCount ( feedID , database )
}
DispatchQueue . main . async ( ) {
completion ( unreadCountTable )
}
}
2017-08-23 22:23:12 +02:00
}
2017-09-01 22:31:27 +02:00
2017-08-23 22:23:12 +02:00
// MARK: S t a t u s
func mark ( _ articles : Set < Article > , _ statusKey : String , _ flag : Bool ) {
2017-08-29 22:32:36 +02:00
// S e t s f l a g i n b o t h m e m o r y a n d i n d a t a b a s e .
let articleIDs = articles . flatMap { ( article ) -> String ? in
guard let status = article . status else {
assertionFailure ( " Each article must have a status. " )
return nil
}
if status . boolStatus ( forKey : statusKey ) = = flag {
return nil
}
status . setBoolStatus ( flag , forKey : statusKey )
return article . articleID
}
if articleIDs . isEmpty {
return
}
queue . update { ( database ) in
self . statusesTable . markArticleIDs ( Set ( articleIDs ) , statusKey , flag , database )
}
2017-08-23 22:23:12 +02:00
}
2017-08-21 22:31:14 +02:00
}
2017-07-29 21:29:05 +02:00
2017-09-02 23:19:42 +02:00
// MARK: - P r i v a t e
2017-08-27 00:37:15 +02:00
private extension ArticlesTable {
// MARK: F e t c h i n g
func attachRelatedObjects ( _ articles : Set < Article > , _ database : FMDatabase ) {
let articleArray = articles . map { $0 as DatabaseObject }
authorsLookupTable . attachRelatedObjects ( to : articleArray , in : database )
attachmentsLookupTable . attachRelatedObjects ( to : articleArray , in : database )
tagsLookupTable . attachRelatedObjects ( to : articleArray , in : database )
// I n t h e o r y , i t ’ s i m p o s s i b l e t o h a v e a f e t c h e d a r t i c l e w i t h o u t a s t a t u s .
// L e t ’ s h a n d l e t h a t i m p o s s i b i l i t y a n y w a y .
// R e m e m b e r t h a t , i f n o t h i n g e l s e , t h e u s e r c a n e d i t t h e S Q L i t e d a t a b a s e ,
// a n d t h u s c o u l d d e l e t e a l l t h e i r s t a t u s e s .
statusesTable . ensureStatusesForArticles ( articles , database )
}
func articleWithRow ( _ row : FMResultSet ) -> Article ? {
guard let account = account else {
return nil
}
guard let article = Article ( row : row , account : account ) else {
return nil
}
// N o t e : t h e r o w i s a r e s u l t o f a J O I N q u e r y w i t h t h e s t a t u s e s t a b l e ,
// s o w e c a n g e t t h e s t a t u s a t t h e s a m e t i m e a n d a v o i d a d d i t i o n a l d a t a b a s e l o o k u p s .
article . status = statusesTable . statusWithRow ( row )
return article
}
func articlesWithResultSet ( _ resultSet : FMResultSet , _ database : FMDatabase ) -> Set < Article > {
let articles = resultSet . mapToSet ( articleWithRow )
attachRelatedObjects ( articles , database )
return articles
}
2017-09-05 02:10:02 +02:00
func fetchArticlesWithWhereClause ( _ database : FMDatabase , whereClause : String , parameters : [ AnyObject ] , withLimits : Bool ) -> Set < Article > {
2017-08-27 00:37:15 +02:00
2017-08-31 22:35:48 +02:00
// D o n ’ t f e t c h a r t i c l e s t h a t s h o u l d n ’ t a p p e a r i n t h e U I . T h e r u l e s :
// * M u s t n o t b e d e l e t e d .
// * M u s t b e e i t h e r 1 ) s t a r r e d o r 2 ) d a t e A r r i v e d m u s t b e n e w e r t h a n c u t o f f d a t e .
2017-09-05 02:10:02 +02:00
let sql = withLimits ? " select * from articles natural join statuses where \( whereClause ) and userDeleted=0 and (starred=1 or dateArrived>?); " : " select * from articles natural join statuses where \( whereClause ) ; "
2017-08-31 22:35:48 +02:00
return articlesWithSQL ( sql , parameters + [ articleCutoffDate as AnyObject ] , database )
2017-08-27 22:03:15 +02:00
}
2017-08-31 22:35:48 +02:00
func fetchUnreadCount ( _ feedID : String , _ database : FMDatabase ) -> Int {
// C o u n t o n l y t h e a r t i c l e s t h a t w o u l d a p p e a r i n t h e U I .
// * M u s t b e u n r e a d .
// * M u s t n o t b e d e l e t e d .
// * M u s t b e e i t h e r 1 ) s t a r r e d o r 2 ) d a t e A r r i v e d m u s t b e n e w e r t h a n c u t o f f d a t e .
let sql = " select count(*) from articles natural join statuses where feedID=? and read=0 and userDeleted=0 and (starred=1 or dateArrived>?); "
return numberWithSQLAndParameters ( sql , [ feedID , articleCutoffDate ] , in : database )
}
2017-09-05 02:10:02 +02:00
func fetchArticlesForFeedID ( _ feedID : String , withLimits : Bool , database : FMDatabase ) -> Set < Article > {
2017-08-27 22:03:15 +02:00
2017-09-05 02:10:02 +02:00
return fetchArticlesWithWhereClause ( database , whereClause : " articles.feedID = ? " , parameters : [ feedID as AnyObject ] , withLimits : withLimits )
2017-08-27 22:03:15 +02:00
}
2017-08-27 00:37:15 +02:00
2017-08-27 22:03:15 +02:00
func fetchUnreadArticles ( _ feedIDs : Set < String > ) -> Set < Article > {
if feedIDs . isEmpty {
return Set < Article > ( )
2017-08-27 00:37:15 +02:00
}
2017-08-27 22:03:15 +02:00
var articles = Set < Article > ( )
queue . fetchSync { ( database ) in
// s e l e c t * f r o m a r t i c l e s n a t u r a l j o i n s t a t u s e s w h e r e f e e d I D i n ( ' h t t p : / / r a n c h e r o . c o m / x m l / r s s . x m l ' ) a n d r e a d = 0
let parameters = feedIDs . map { $0 as AnyObject }
let placeholders = NSString . rs_SQLValueList ( withPlaceholders : UInt ( feedIDs . count ) ) !
let whereClause = " feedID in \( placeholders ) and read=0 "
2017-09-05 02:10:02 +02:00
articles = self . fetchArticlesWithWhereClause ( database , whereClause : whereClause , parameters : parameters , withLimits : true )
2017-08-27 22:03:15 +02:00
}
return articleCache . uniquedArticles ( articles )
2017-08-27 00:37:15 +02:00
}
2017-08-27 22:03:15 +02:00
func articlesWithSQL ( _ sql : String , _ parameters : [ AnyObject ] , _ database : FMDatabase ) -> Set < Article > {
2017-08-27 00:37:15 +02:00
2017-08-27 22:03:15 +02:00
guard let resultSet = database . executeQuery ( sql , withArgumentsIn : parameters ) else {
return Set < Article > ( )
}
return articlesWithResultSet ( resultSet , database )
2017-08-27 00:37:15 +02:00
}
2017-09-02 23:19:42 +02:00
// MARK: S a v i n g / U p d a t i n g
func updateArticles ( _ articlesDictionary : [ String : Article ] , _ parsedItemsDictionary : [ String : ParsedItem ] , _ feed : Feed , _ completion : @ escaping RSVoidCompletionBlock ) {
2017-09-05 02:10:02 +02:00
// 1 . F e t c h s t a t u s e s f o r p a r s e d I t e m s .
// 2 . F i l t e r o u t p a r s e d I t e m s w h e r e u s e r D e l e t e d = = 1 o r ( a r r i v a l d a t e > 4 m o n t h s a n d n o t s t a r r e d ) .
// ( U n d e r n o u s e r s e t t i n g d o w e r e t a i n a r t i c l e s o l d e r w i t h a n a r r i v a l d a t e > 4 m o n t h s . )
// 3 . F i n d p a r s e d I t e m s w i t h n o s t a t u s a n d n o m a t c h i n g a r t i c l e : s a v e t h e m a s e n t i r e l y n e w a r t i c l e s .
// 4 . C o m p a r e r e m a i n i n g p a r s e d I t e m s w i t h a r t i c l e s , a n d u p d a t e d a t a b a s e w i t h a n y c h a n g e s .
assert ( Thread . isMainThread )
queue . fetch { ( database ) in
let parsedItemArticleIDs = Set ( parsedItemsDictionary . keys )
let fetchedStatuses = self . statusesTable . fetchStatusesForArticleIDs ( parsedItemArticleIDs , database )
DispatchQueue . main . async {
// # 2 . D r o p a n y p a r s e d I t e m s t h a t c a n b e i g n o r e d .
// I f t h a t ’ s a l l o f t h e m , t h e n g r e a t — n o t h i n g t o d o .
let filteredParsedItems = self . filterParsedItems ( parsedItemsDictionary , fetchedStatuses )
if filteredParsedItems . isEmpty {
completion ( )
return
}
// # 3 . S a v e e n t i r e l y n e w p a r s e d I t e m s .
let newParsedItems = self . findNewParsedItems ( parsedItemsDictionary , fetchedStatuses , articlesDictionary )
if ! newParsedItems . isEmpty {
self . saveNewParsedItems ( newParsedItems , feed )
}
// # 4 . U p d a t e e x i s t i n g p a r s e d I t e m s .
let parsedItemsToUpdate = self . findExistingParsedItems ( parsedItemsDictionary , fetchedStatuses , articlesDictionary )
if ! parsedItemsToUpdate . isEmpty {
self . updateParsedItems ( parsedItemsToUpdate , articlesDictionary , feed )
}
completion ( )
}
}
}
func updateParsedItems ( _ parsedItems : [ String : ParsedItem ] , _ articles : [ String : Article ] , _ feed : Feed ) {
assert ( Thread . isMainThread )
updateRelatedObjects ( _ parsedItems : [ String : ParsedItem ] , _ articles : [ String : Article ] )
}
func updateRelatedObjects ( _ parsedItems : [ String : ParsedItem ] , _ articles : [ String : Article ] ) {
// U p d a t e t h e i n - m e m o r y A r t i c l e s w h e n n e e d e d .
// S a v e o n l y w h e n t h e r e a r e c h a n g e s , w h i c h s h o u l d b e p r e t t y i n f r e q u e n t .
assert ( Thread . isMainThread )
var articlesWithTagChanges = Set < Article > ( )
var articlesWithAttachmentChanges = Set < Article > ( )
var articlesWithAuthorChanges = Set < Article > ( )
for ( articleID , parsedItem ) in parsedItems {
guard let article = articles [ articleID ] else {
continue
}
if article . updateTagsWithParsedTags ( parsedItem . tags ) {
articlesWithTagChanges . insert ( article )
}
if article . updateAttachmentsWithParsedAttachments ( parsedItem . attachments ) {
articlesWithAttachmentChanges . insert ( article )
}
if article . updateAuthorsWithParsedAuthors ( parsedItem . authors ) {
articlesWithAuthorChanges . insert ( article )
}
}
if articlesWithTagChanges . isEmpty && articlesWithAttachmentChanges . isEmpty && articlesWithAuthorChanges . isEmpty {
// S h o u l d b e p r e t t y c o m m o n .
return
}
// W e u s e d d e t a c h e d C o p y b e c a u s e t h e A r t i c l e o b j e c t s b e i n g u p d a t e d a r e m a i n - t h r e a d o b j e c t s .
articlesWithTagChanges = Set ( articlesWithTagChanges . map { $0 . detachedCopy ( ) } )
articlesWithAttachmentChanges = Set ( articlesWithAttachmentChanges . map { $0 . detachedCopy ( ) } )
articlesWithAuthorChanges = Set ( articlesWithAuthorChanges . map { $0 . detachedCopy ( ) } )
queue . update { ( database ) in
if ! articlesWithTagChanges . isEmpty {
tagsLookupTable . saveRelatedObjects ( for : articlesWithTagChanges . databaseObjects ( ) , in : database )
}
if ! articlesWithAttachmentChanges . isEmpty {
attachmentsLookupTable . saveRelatedObjects ( for : articlesWithAttachmentChanges . databaseObjects ( ) , in : database )
}
if ! articlesWithAuthorChanges . isEmpty {
authorsLookupTable . saveRelatedObjects ( for : articlesWithAuthorChanges . databaseObjects ( ) , in : database )
}
}
}
func updateRelatedAttachments ( _ parsedItems : [ String : ParsedItem ] , _ articles : [ String : Article ] ) {
var articlesWithChanges = Set < Article > ( )
for ( articleID , parsedItem ) in parsedItems {
guard let article = articles [ articleID ] else {
continue
}
if ! parsedItemTagsMatchArticlesTag ( parsedItem , article ) {
articlesChanges . insert ( article )
}
}
if articlesWithChanges . isEmpty {
return
}
queue . update { ( database ) in
tagsLookupTable . saveRelatedObjects ( for : articlesWithChanges . databaseObjects ( ) , in : database )
}
}
func updateRelatedTags ( _ parsedItems : [ String : ParsedItem ] , _ articles : [ String : Article ] ) {
var articlesWithChanges = Set < Article > ( )
for ( articleID , parsedItem ) in parsedItems {
guard let article = articles [ articleID ] else {
continue
}
if ! parsedItemTagsMatchArticlesTag ( parsedItem , article ) {
articlesChanges . insert ( article )
}
}
if articlesWithChanges . isEmpty {
return
}
queue . update { ( database ) in
tagsLookupTable . saveRelatedObjects ( for : articlesWithChanges . databaseObjects ( ) , in : database )
}
}
func parsedItemTagsMatchArticlesTag ( _ parsedItem : ParsedItem , _ article : Article ) -> Bool {
let parsedItemTags = parsedItem . tags
let articleTags = article . tags
if parsedItemTags = = nil && articleTags = = nil {
return true
}
if parsedItemTags != nil && articleTags = = nil {
return false
}
if parsedItemTags = = nil && articleTags != nil {
return true
}
return Set ( parsedItemTags ! ) = = articleTags !
}
func saveNewParsedItems ( _ parsedItems : [ String : ParsedItem ] , _ feed : Feed ) {
// T h e s e p a r s e d I t e m s h a v e n o e x i s t i n g s t a t u s o r A r t i c l e .
2017-09-03 01:08:02 +02:00
queue . update { ( database ) in
2017-09-05 02:10:02 +02:00
let articleIDs = Set ( parsedItems . keys )
self . statusesTable . ensureStatusesForArticleIDs ( articleIDs , database )
let articles = self . articlesWithParsedItems ( Set ( parsedItems . values ) , feed )
self . saveUncachedNewArticles ( articles , database )
}
}
func articlesWithParsedItems ( _ parsedItems : Set < ParsedItem > , _ feed : Feed ) -> Set < Article > {
// T h e s e A r t i c l e s d o n ’ t g e t c a c h e d . B a c k g r o u n d - q u e u e o n l y .
let feedID = feed . feedID
return Set ( parsedItems . flatMap { articleWithParsedItem ( $0 , feedID ) } )
}
func articleWithParsedItem ( _ parsedItem : ParsedItem , _ feedID : String ) -> Article ? {
guard let account = account else {
assertionFailure ( " account is unexpectedly nil. " )
return nil
}
return Article ( parsedItem : parsedItem , feedID : feedID , account : account )
}
func saveUncachedNewArticles ( _ articles : Set < Article > , _ database : FMDatabase ) {
saveRelatedObjects ( articles , database )
let databaseDictionaries = articles . map { $0 . databaseDictionary ( ) }
insertRows ( databaseDictionaries , insertType : . orIgnore , in : database )
}
func saveRelatedObjects ( _ articles : Set < Article > , _ database : FMDatabase ) {
let databaseObjects = articles . databaseObjects ( )
authorsLookupTable . saveRelatedObjects ( for : databaseObjects , in : database )
attachmentsLookupTable . saveRelatedObjects ( for : databaseObjects , in : database )
tagsLookupTable . saveRelatedObjects ( for : databaseObjects , in : database )
}
func statusIndicatesArticleIsIgnorable ( _ status : ArticleStatus ) -> Bool {
// I g n o r a b l e a r t i c l e s : e i t h e r u s e r D e l e t e d = = 1 o r ( n o t s t a r r e d a n d a r r i v a l d a t e > 4 m o n t h s ) .
if status . userDeleted {
return true
}
if status . starred {
return false
}
return status . dateArrived < maximumArticleCutoffDate
}
func filterParsedItems ( _ parsedItems : [ String : ParsedItem ] , _ statuses : [ String : ArticleStatus ] ) -> [ String : ParsedItem ] {
2017-09-03 01:08:02 +02:00
2017-09-05 02:10:02 +02:00
// D r o p p a r s e d I t e m s t h a t w e c a n i g n o r e .
assert ( Thread . isMainThread )
var d = [ String : ParsedItem ] ( )
for ( articleID , parsedItem ) in parsedItems {
if let status = statuses [ articleID ] {
if statusIndicatesArticleIsIgnorable ( status ) {
continue
}
}
d [ articleID ] = parsedItem
2017-09-03 01:08:02 +02:00
}
2017-09-05 02:10:02 +02:00
return d
}
func findNewParsedItems ( _ parsedItems : [ String : ParsedItem ] , _ statuses : [ String : ArticleStatus ] , _ articles : [ String : Article ] ) -> [ String : ParsedItem ] {
// I f t h e r e ’ s n o e x i s t i n g s t a t u s o r A r t i c l e , t h e n i t ’ s c o m p l e t e l y n e w .
2017-09-03 01:08:02 +02:00
2017-09-05 02:10:02 +02:00
assert ( Thread . isMainThread )
var d = [ String : ParsedItem ] ( )
for ( articleID , parsedItem ) in parsedItems {
if statuses [ articleID ] = = nil && articles [ articleID ] = = nil {
d [ articleID ] = parsedItem
}
}
return d
2017-09-02 23:19:42 +02:00
}
2017-09-05 02:10:02 +02:00
func findExistingParsedItems ( _ parsedItems : [ String : ParsedItem ] , _ statuses : [ String : ArticleStatus ] , _ articles : [ String : Article ] ) -> [ String : ParsedItem ] {
return [ String : ParsedItem ] ( ) // T O D O
}
2017-08-27 00:37:15 +02:00
}
2017-07-29 21:29:05 +02:00
2017-08-27 01:44:39 +02:00
// MARK: -
2017-08-24 06:30:28 +02:00
private struct ArticleCache {
2017-08-27 00:37:15 +02:00
// M a i n t h r e a d o n l y — u n l i k e t h e o t h e r o b j e c t c a c h e s .
2017-08-24 06:30:28 +02:00
// T h e c a c h e c o n t a i n s a g i v e n a r t i c l e o n l y u n t i l a l l o u t s i d e r e f e r e n c e s a r e g o n e .
// C a c h e k e y i s a r t i c l e I D .
private let articlesMapTable : NSMapTable < NSString , Article > = NSMapTable . weakToWeakObjects ( )
func uniquedArticles ( _ articles : Set < Article > ) -> Set < Article > {
var articlesToReturn = Set < Article > ( )
for article in articles {
let articleID = article . articleID
if let cachedArticle = cachedArticle ( for : articleID ) {
articlesToReturn . insert ( cachedArticle )
}
else {
articlesToReturn . insert ( article )
addToCache ( article )
}
}
2017-08-27 00:37:15 +02:00
// A t t h i s p o i n t , e v e r y A r t i c l e m u s t h a v e a n a t t a c h e d S t a t u s .
assert ( articlesToReturn . eachHasAStatus ( ) )
2017-08-24 06:30:28 +02:00
return articlesToReturn
}
private func cachedArticle ( for articleID : String ) -> Article ? {
return articlesMapTable . object ( forKey : articleID as NSString )
}
private func addToCache ( _ article : Article ) {
articlesMapTable . setObject ( article , forKey : article . articleID as NSString )
}
}
2017-09-02 23:19:42 +02:00