2019-02-25 00:34:10 +01:00
|
|
|
|
//
|
|
|
|
|
// SearchTable.swift
|
2019-07-09 07:20:57 +02:00
|
|
|
|
// NetNewsWire
|
2019-02-25 00:34:10 +01:00
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 2/23/19.
|
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import RSCore
|
|
|
|
|
import RSDatabase
|
|
|
|
|
import Articles
|
|
|
|
|
import RSParser
|
|
|
|
|
|
|
|
|
|
final class ArticleSearchInfo: Hashable {
|
|
|
|
|
|
|
|
|
|
let articleID: String
|
|
|
|
|
let title: String?
|
|
|
|
|
let contentHTML: String?
|
|
|
|
|
let contentText: String?
|
|
|
|
|
let summary: String?
|
|
|
|
|
let searchRowID: Int?
|
|
|
|
|
|
|
|
|
|
var preferredText: String {
|
|
|
|
|
if let body = contentHTML, !body.isEmpty {
|
|
|
|
|
return body
|
|
|
|
|
}
|
|
|
|
|
if let body = contentText, !body.isEmpty {
|
|
|
|
|
return body
|
|
|
|
|
}
|
|
|
|
|
return summary ?? ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lazy var bodyForIndex: String = {
|
|
|
|
|
let s = preferredText.rsparser_stringByDecodingHTMLEntities()
|
2020-01-17 03:09:18 +01:00
|
|
|
|
return s.strippingHTML().collapsingWhitespace
|
2019-02-25 00:34:10 +01:00
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
init(articleID: String, title: String?, contentHTML: String?, contentText: String?, summary: String?, searchRowID: Int?) {
|
|
|
|
|
self.articleID = articleID
|
|
|
|
|
self.title = title
|
|
|
|
|
self.contentHTML = contentHTML
|
|
|
|
|
self.contentText = contentText
|
|
|
|
|
self.summary = summary
|
|
|
|
|
self.searchRowID = searchRowID
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 02:14:20 +01:00
|
|
|
|
convenience init(article: Article) {
|
|
|
|
|
self.init(articleID: article.articleID, title: article.title, contentHTML: article.contentHTML, contentText: article.contentText, summary: article.summary, searchRowID: nil)
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-25 00:34:10 +01:00
|
|
|
|
// MARK: Hashable
|
|
|
|
|
|
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
|
|
|
hasher.combine(articleID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Equatable
|
|
|
|
|
|
|
|
|
|
static func == (lhs: ArticleSearchInfo, rhs: ArticleSearchInfo) -> Bool {
|
|
|
|
|
return lhs.articleID == rhs.articleID && lhs.title == rhs.title && lhs.contentHTML == rhs.contentHTML && lhs.contentText == rhs.contentText && lhs.summary == rhs.summary && lhs.searchRowID == rhs.searchRowID
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class SearchTable: DatabaseTable {
|
|
|
|
|
|
|
|
|
|
let name = "search"
|
2019-11-30 06:49:44 +01:00
|
|
|
|
private let queue: DatabaseQueue
|
2019-02-25 00:34:10 +01:00
|
|
|
|
private weak var articlesTable: ArticlesTable?
|
|
|
|
|
|
2019-11-30 06:49:44 +01:00
|
|
|
|
init(queue: DatabaseQueue, articlesTable: ArticlesTable) {
|
2019-02-25 00:34:10 +01:00
|
|
|
|
self.queue = queue
|
|
|
|
|
self.articlesTable = articlesTable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ensureIndexedArticles(for articleIDs: Set<String>) {
|
2019-12-16 07:09:27 +01:00
|
|
|
|
guard !articleIDs.isEmpty else {
|
2019-02-25 00:34:10 +01:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-12-16 07:09:27 +01:00
|
|
|
|
queue.runInTransaction { databaseResult in
|
|
|
|
|
if let database = databaseResult.database {
|
|
|
|
|
self.ensureIndexedArticles(articleIDs, database)
|
|
|
|
|
}
|
2019-02-25 00:34:10 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 20:49:02 +01:00
|
|
|
|
/// Add to, or update, the search index for articles with specified IDs.
|
2019-02-25 00:34:10 +01:00
|
|
|
|
func ensureIndexedArticles(_ articleIDs: Set<String>, _ database: FMDatabase) {
|
|
|
|
|
guard let articlesTable = articlesTable else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
guard let articleSearchInfos = articlesTable.fetchArticleSearchInfos(articleIDs, in: database) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let unindexedArticles = articleSearchInfos.filter { $0.searchRowID == nil }
|
|
|
|
|
performInitialIndexForArticles(unindexedArticles, database)
|
|
|
|
|
|
|
|
|
|
let indexedArticles = articleSearchInfos.filter { $0.searchRowID != nil }
|
|
|
|
|
updateIndexForArticles(indexedArticles, database)
|
|
|
|
|
}
|
2019-12-09 02:14:20 +01:00
|
|
|
|
|
|
|
|
|
/// Index new articles.
|
|
|
|
|
func indexNewArticles(_ articles: Set<Article>, _ database: FMDatabase) {
|
|
|
|
|
let articleSearchInfos = Set(articles.map{ ArticleSearchInfo(article: $0) })
|
|
|
|
|
performInitialIndexForArticles(articleSearchInfos, database)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Index updated articles.
|
|
|
|
|
func indexUpdatedArticles(_ articles: Set<Article>, _ database: FMDatabase) {
|
|
|
|
|
ensureIndexedArticles(articles.articleIDs(), database)
|
|
|
|
|
}
|
2019-12-08 20:49:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
|
|
|
|
private extension SearchTable {
|
2019-02-25 00:34:10 +01:00
|
|
|
|
|
|
|
|
|
func performInitialIndexForArticles(_ articles: Set<ArticleSearchInfo>, _ database: FMDatabase) {
|
|
|
|
|
articles.forEach { performInitialIndex($0, database) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func performInitialIndex(_ article: ArticleSearchInfo, _ database: FMDatabase) {
|
|
|
|
|
let rowid = insert(article, database)
|
|
|
|
|
articlesTable?.updateRowsWithValue(rowid, valueKey: DatabaseKey.searchRowID, whereKey: DatabaseKey.articleID, matches: [article.articleID], database: database)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func insert(_ article: ArticleSearchInfo, _ database: FMDatabase) -> Int {
|
2019-03-03 01:17:06 +01:00
|
|
|
|
let rowDictionary: DatabaseDictionary = [DatabaseKey.body: article.bodyForIndex, DatabaseKey.title: article.title ?? ""]
|
2019-02-25 00:34:10 +01:00
|
|
|
|
insertRow(rowDictionary, insertType: .normal, in: database)
|
|
|
|
|
return Int(database.lastInsertRowId())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private struct SearchInfo: Hashable {
|
|
|
|
|
let rowID: Int
|
|
|
|
|
let title: String
|
|
|
|
|
let body: String
|
|
|
|
|
|
|
|
|
|
init(row: FMResultSet) {
|
|
|
|
|
self.rowID = Int(row.longLongInt(forColumn: DatabaseKey.rowID))
|
|
|
|
|
self.title = row.string(forColumn: DatabaseKey.title) ?? ""
|
|
|
|
|
self.body = row.string(forColumn: DatabaseKey.body) ?? ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Hashable
|
|
|
|
|
|
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
|
|
|
hasher.combine(rowID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func updateIndexForArticles(_ articles: Set<ArticleSearchInfo>, _ database: FMDatabase) {
|
|
|
|
|
if articles.isEmpty {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
guard let searchInfos = fetchSearchInfos(articles, database) else {
|
|
|
|
|
// The articles that get here have a non-nil searchRowID, and we should have found rows in the search table for them.
|
|
|
|
|
// But we didn’t. Recover by doing an initial index.
|
|
|
|
|
performInitialIndexForArticles(articles, database)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let groupedSearchInfos = Dictionary(grouping: searchInfos, by: { $0.rowID })
|
|
|
|
|
let searchInfosDictionary = groupedSearchInfos.mapValues { $0.first! }
|
|
|
|
|
|
|
|
|
|
articles.forEach { (article) in
|
|
|
|
|
updateIndexForArticle(article, searchInfosDictionary, database)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func updateIndexForArticle(_ article: ArticleSearchInfo, _ searchInfosDictionary: [Int: SearchInfo], _ database: FMDatabase) {
|
|
|
|
|
guard let searchRowID = article.searchRowID else {
|
|
|
|
|
assertionFailure("Expected article.searchRowID, got nil")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
guard let searchInfo: SearchInfo = searchInfosDictionary[searchRowID] else {
|
|
|
|
|
// Shouldn’t happen. The article has a searchRowID, but we didn’t find that row in the search table.
|
|
|
|
|
// Easy to recover from: just do an initial index, and all’s well.
|
|
|
|
|
performInitialIndex(article, database)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let title = article.title ?? ""
|
|
|
|
|
if title == searchInfo.title && article.bodyForIndex == searchInfo.body {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-03 01:17:06 +01:00
|
|
|
|
var updateDictionary = DatabaseDictionary()
|
2019-02-25 00:34:10 +01:00
|
|
|
|
if title != searchInfo.title {
|
2019-03-03 01:17:06 +01:00
|
|
|
|
updateDictionary[DatabaseKey.title] = title
|
2019-02-25 00:34:10 +01:00
|
|
|
|
}
|
|
|
|
|
if article.bodyForIndex != searchInfo.body {
|
2019-03-03 01:17:06 +01:00
|
|
|
|
updateDictionary[DatabaseKey.body] = article.bodyForIndex
|
2019-02-25 00:34:10 +01:00
|
|
|
|
}
|
|
|
|
|
updateRowsWithDictionary(updateDictionary, whereKey: DatabaseKey.rowID, matches: searchInfo.rowID, database: database)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func fetchSearchInfos(_ articles: Set<ArticleSearchInfo>, _ database: FMDatabase) -> Set<SearchInfo>? {
|
|
|
|
|
let searchRowIDs = articles.compactMap { $0.searchRowID }
|
|
|
|
|
guard !searchRowIDs.isEmpty else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(searchRowIDs.count))!
|
|
|
|
|
let sql = "select rowid, title, body from \(name) where rowid in \(placeholders);"
|
|
|
|
|
guard let resultSet = database.executeQuery(sql, withArgumentsIn: searchRowIDs) else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return resultSet.mapToSet { SearchInfo(row: $0) }
|
|
|
|
|
}
|
|
|
|
|
}
|