Skip to content
This repository has been archived by the owner on Oct 10, 2019. It is now read-only.

Commit

Permalink
Reformat cache.cpp to (rougly) fit 80 columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Minoru committed Oct 23, 2016
1 parent 5438afc commit 5785a11
Showing 1 changed file with 137 additions and 53 deletions.
190 changes: 137 additions & 53 deletions src/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ static int lastmodified_callback(void * handler, int argc, char ** argv, char **
} else {
result->etag = "";
}
LOG(LOG_INFO, "lastmodified_callback: lastmodified = %d etag = %s", result->lastmodified, result->etag.c_str());
LOG(LOG_INFO, "lastmodified_callback: lastmodified = %d etag = %s",
result->lastmodified, result->etag.c_str());
return 0;
}

Expand Down Expand Up @@ -172,7 +173,8 @@ static int fill_content_callback(void * myfeed, int argc, char ** argv, char **
}

static int search_item_callback(void * myfeed, int argc, char ** argv, char ** /* azColName */) {
std::vector<std::shared_ptr<rss_item>> * items = static_cast<std::vector<std::shared_ptr<rss_item>> *>(myfeed);
std::vector<std::shared_ptr<rss_item>> * items =
static_cast<std::vector<std::shared_ptr<rss_item>> *>(myfeed);
assert (argc == 13);
std::shared_ptr<rss_item> item(new rss_item(nullptr));
item->set_guid(argv[0]);
Expand Down Expand Up @@ -204,7 +206,8 @@ static int search_item_callback(void * myfeed, int argc, char ** argv, char ** /
cache::cache(const std::string& cachefile, configcontainer * c) : db(0),cfg(c) {
int error = sqlite3_open(cachefile.c_str(),&db);
if (error != SQLITE_OK) {
LOG(LOG_ERROR,"couldn't sqlite3_open(%s): error = %d", cachefile.c_str(), error);
LOG(LOG_ERROR,"couldn't sqlite3_open(%s): error = %d",
cachefile.c_str(), error);
throw dbexception(db);
}

Expand All @@ -213,7 +216,8 @@ cache::cache(const std::string& cachefile, configcontainer * c) : db(0),cfg(c) {

clean_old_articles();

// we need to manually lock all DB operations because SQLite has no explicit support for multithreading.
// we need to manually lock all DB operations because SQLite has no
// explicit support for multithreading.
}

cache::~cache() {
Expand Down Expand Up @@ -294,7 +298,10 @@ void cache::populate_tables() {

void cache::fetch_lastmodified(const std::string& feedurl, time_t& t, std::string& etag) {
std::lock_guard<std::mutex> lock(mtx);
std::string query = prepare_query("SELECT lastmodified, etag FROM rss_feed WHERE rssurl = '%q';", feedurl.c_str());
std::string query =
prepare_query(
"SELECT lastmodified, etag FROM rss_feed WHERE rssurl = '%q';",
feedurl.c_str());
header_values result = { 0, "" };
run_sql(query, lastmodified_callback, &result);
t = result.lastmodified;
Expand All @@ -304,23 +311,33 @@ void cache::fetch_lastmodified(const std::string& feedurl, time_t& t, std::strin

void cache::update_lastmodified(const std::string& feedurl, time_t t, const std::string& etag) {
if (t == 0 && etag.length() == 0) {
LOG(LOG_INFO, "cache::update_lastmodified: both time and etag are empty, not updating anything");
LOG(LOG_INFO, "cache::update_lastmodified: both time and etag are "
"empty, not updating anything");
return;
}
std::lock_guard<std::mutex> lock(mtx);
std::string query = "UPDATE rss_feed SET ";
if (t > 0)
query.append(utils::strprintf("lastmodified = '%d'", t));
if (etag.length() > 0)
query.append(utils::strprintf("%c etag = %s", (t > 0 ? ',' : ' '), prepare_query("'%q'", etag.c_str()).c_str()));
if (etag.length() > 0) {
query.append(
utils::strprintf(
"%c etag = %s",
(t > 0 ? ',' : ' '),
prepare_query("'%q'", etag.c_str()).c_str()));
}
query.append(" WHERE rssurl = ");
query.append(prepare_query("'%q'", feedurl.c_str()));
run_sql_nothrow(query);
}

void cache::mark_item_deleted(const std::string& guid, bool b) {
std::lock_guard<std::mutex> lock(mtx);
std::string query = prepare_query("UPDATE rss_item SET deleted = %u WHERE guid = '%q'", b ? 1 : 0, guid.c_str());
std::string query =
prepare_query(
"UPDATE rss_item SET deleted = %u WHERE guid = '%q'",
b ? 1 : 0,
guid.c_str());
run_sql_nothrow(query);
}

Expand Down Expand Up @@ -473,7 +490,9 @@ std::shared_ptr<rss_feed> cache::internalize_rssfeed(std::string rssurl, rss_ign
return feed;
}

std::vector<std::shared_ptr<rss_item>> cache::search_for_items(const std::string& querystr, const std::string& feedurl) {
std::vector<std::shared_ptr<rss_item>> cache::search_for_items(
const std::string& querystr, const std::string& feedurl)
{
assert(feedurl.substr(0,6) != "query:");
std::string query;
std::vector<std::shared_ptr<rss_item>> items;
Expand Down Expand Up @@ -514,7 +533,10 @@ std::vector<std::shared_ptr<rss_item>> cache::search_for_items(const std::string
}

void cache::delete_item(const std::shared_ptr<rss_item> item) {
std::string query = prepare_query("DELETE FROM rss_item WHERE guid = '%q';",item->guid().c_str());
std::string query =
prepare_query(
"DELETE FROM rss_item WHERE guid = '%q';",
item->guid().c_str());
run_sql(query);
}

Expand All @@ -527,13 +549,15 @@ void cache::cleanup_cache(std::vector<std::shared_ptr<rss_feed>>& feeds) {
mtx.lock(); // we don't use the std::lock_guard<> here... see comments below

/*
* cache cleanup means that all entries in both the rss_feed and rss_item tables that are associated with
* an RSS feed URL that is not contained in the current configuration are deleted.
* Such entries are the result when a user deletes one or more lines in the urls configuration file. We
* then assume that the user isn't interested anymore in reading this feed, and delete all associated entries
* because they would be non-accessible.
* cache cleanup means that all entries in both the rss_feed and rss_item
* tables that are associated with an RSS feed URL that is not contained in
* the current configuration are deleted. Such entries are the result when
* a user deletes one or more lines in the urls configuration file. We then
* assume that the user isn't interested anymore in reading this feed, and
* delete all associated entries because they would be non-accessible.
*
* The behaviour whether the cleanup is done or not is configurable via the configuration file.
* The behaviour whether the cleanup is done or not is configurable via the
* configuration file.
*/
if (cfg->get_configvalue_as_bool("cleanup-on-quit")) {
LOG(LOG_DEBUG,"cache::cleanup_cache: cleaning up cache...");
Expand All @@ -546,17 +570,18 @@ void cache::cleanup_cache(std::vector<std::shared_ptr<rss_feed>>& feeds) {
}
list.append("'')");

std::string cleanup_rss_feeds_statement("DELETE FROM rss_feed WHERE rssurl NOT IN ");
std::string cleanup_rss_feeds_statement(
"DELETE FROM rss_feed WHERE rssurl NOT IN ");
cleanup_rss_feeds_statement.append(list);
cleanup_rss_feeds_statement.append(1,';');

std::string cleanup_rss_items_statement("DELETE FROM rss_item WHERE feedurl NOT IN ");
std::string cleanup_rss_items_statement(
"DELETE FROM rss_item WHERE feedurl NOT IN ");
cleanup_rss_items_statement.append(list);
cleanup_rss_items_statement.append(1,';');

std::string cleanup_read_items_statement("DELETE FROM rss_item WHERE unread = 0");

// std::cerr << "statements: " << cleanup_rss_feeds_statement << " " << cleanup_rss_items_statement << std::endl;
std::string cleanup_read_items_statement(
"DELETE FROM rss_item WHERE unread = 0");

run_sql(cleanup_rss_feeds_statement);
run_sql(cleanup_rss_items_statement);
Expand All @@ -572,51 +597,84 @@ void cache::cleanup_cache(std::vector<std::shared_ptr<rss_feed>>& feeds) {
}
}

void cache::update_rssitem_unlocked(std::shared_ptr<rss_item> item, const std::string& feedurl, bool reset_unread) {
std::string query = prepare_query("SELECT count(*) FROM rss_item WHERE guid = '%q';",item->guid().c_str());
void cache::update_rssitem_unlocked(
std::shared_ptr<rss_item> item, const std::string& feedurl, bool reset_unread)
{
std::string query =
prepare_query(
"SELECT count(*) FROM rss_item WHERE guid = '%q';",
item->guid().c_str());
cb_handler count_cbh;
run_sql(query, count_callback, &count_cbh);
if (count_cbh.count() > 0) {
if (reset_unread) {
std::string content;
query = prepare_query("SELECT content FROM rss_item WHERE guid = '%q';", item->guid().c_str());
query = prepare_query(
"SELECT content FROM rss_item WHERE guid = '%q';",
item->guid().c_str());
run_sql(query, single_string_callback, &content);
if (content != item->description_raw()) {
LOG(LOG_DEBUG, "cache::update_rssitem_unlocked: '%s' is different from '%s'", content.c_str(), item->description_raw().c_str());
query = prepare_query("UPDATE rss_item SET unread = 1 WHERE guid = '%q';", item->guid().c_str());
LOG(LOG_DEBUG,
"cache::update_rssitem_unlocked: '%s' is different from '%s'",
content.c_str(), item->description_raw().c_str());
query = prepare_query(
"UPDATE rss_item SET unread = 1 WHERE guid = '%q';",
item->guid().c_str());
run_sql(query);
}
}
std::string update;
if (item->override_unread()) {
update = prepare_query("UPDATE rss_item SET title = '%q', author = '%q', url = '%q', feedurl = '%q', content = '%q', enclosure_url = '%q', enclosure_type = '%q', base = '%q', unread = '%d' WHERE guid = '%q'",
item->title_raw().c_str(), item->author_raw().c_str(), item->link().c_str(),
feedurl.c_str(), item->description_raw().c_str(),
item->enclosure_url().c_str(), item->enclosure_type().c_str(), item->get_base().c_str(),
(item->unread() ? 1 : 0),
item->guid().c_str());
update =
prepare_query(
"UPDATE rss_item "
"SET title = '%q', author = '%q', url = '%q', feedurl = '%q', "
"content = '%q', enclosure_url = '%q', "
"enclosure_type = '%q', base = '%q', unread = '%d' "
"WHERE guid = '%q'",
item->title_raw().c_str(), item->author_raw().c_str(),
item->link().c_str(), feedurl.c_str(),
item->description_raw().c_str(), item->enclosure_url().c_str(),
item->enclosure_type().c_str(), item->get_base().c_str(),
(item->unread() ? 1 : 0), item->guid().c_str());
} else {
update = prepare_query("UPDATE rss_item SET title = '%q', author = '%q', url = '%q', feedurl = '%q', content = '%q', enclosure_url = '%q', enclosure_type = '%q', base = '%q' WHERE guid = '%q'",
item->title_raw().c_str(), item->author_raw().c_str(), item->link().c_str(),
feedurl.c_str(), item->description_raw().c_str(),
item->enclosure_url().c_str(), item->enclosure_type().c_str(), item->get_base().c_str(),
item->guid().c_str());
update =
prepare_query(
"UPDATE rss_item "
"SET title = '%q', author = '%q', url = '%q', feedurl = '%q', "
"content = '%q', enclosure_url = '%q', "
"enclosure_type = '%q', base = '%q' "
"WHERE guid = '%q'",
item->title_raw().c_str(), item->author_raw().c_str(),
item->link().c_str(), feedurl.c_str(),
item->description_raw().c_str(), item->enclosure_url().c_str(),
item->enclosure_type().c_str(), item->get_base().c_str(),
item->guid().c_str());
}
run_sql(update);
} else {
std::string insert = prepare_query("INSERT INTO rss_item (guid,title,author,url,feedurl,pubDate,content,unread,enclosure_url,enclosure_type,enqueued, base) "
"VALUES ('%q','%q','%q','%q','%q','%u','%q','%d','%q','%q',%d, '%q')",
item->guid().c_str(), item->title_raw().c_str(), item->author_raw().c_str(),
item->link().c_str(), feedurl.c_str(), item->pubDate_timestamp(), item->description_raw().c_str(), (item->unread() ? 1 : 0),
item->enclosure_url().c_str(), item->enclosure_type().c_str(), item->enqueued() ? 1 : 0, item->get_base().c_str());
std::string insert =
prepare_query(
"INSERT INTO rss_item (guid, title, author, url, feedurl, "
"pubDate, content, unread, enclosure_url, "
"enclosure_type, enqueued, base) "
"VALUES ('%q','%q','%q','%q','%q','%u','%q','%d','%q','%q',%d, "
"'%q')",
item->guid().c_str(), item->title_raw().c_str(),
item->author_raw().c_str(), item->link().c_str(), feedurl.c_str(),
item->pubDate_timestamp(), item->description_raw().c_str(),
(item->unread() ? 1 : 0), item->enclosure_url().c_str(),
item->enclosure_type().c_str(), item->enqueued() ? 1 : 0,
item->get_base().c_str());
run_sql(insert);
}
}

void cache::catchup_all(std::shared_ptr<rss_feed> feed) {
std::lock_guard<std::mutex> lock(mtx);
std::lock_guard<std::mutex> itemlock(feed->item_mutex);
std::string query = "UPDATE rss_item SET unread = '0' WHERE unread != '0' AND guid IN (";
std::string query =
"UPDATE rss_item SET unread = '0' WHERE unread != '0' AND guid IN (";

for (auto item : feed->items()) {
query.append(prepare_query("'%q',", item->guid().c_str()));
Expand All @@ -632,9 +690,17 @@ void cache::catchup_all(const std::string& feedurl) {

std::string query;
if (feedurl.length() > 0) {
query = prepare_query("UPDATE rss_item SET unread = '0' WHERE unread != '0' AND feedurl = '%q';", feedurl.c_str());
query = prepare_query(
"UPDATE rss_item "
"SET unread = '0' "
"WHERE unread != '0' "
"AND feedurl = '%q';",
feedurl.c_str());
} else {
query = prepare_query("UPDATE rss_item SET unread = '0' WHERE unread != '0';");
query = prepare_query(
"UPDATE rss_item "
"SET unread = '0' "
"WHERE unread != '0';");
}
run_sql(query);
}
Expand Down Expand Up @@ -676,7 +742,9 @@ void cache::update_rssitem_unread_and_enqueued(rss_item* item, const std::string
}

/* this function updates the unread and enqueued flags */
void cache::update_rssitem_unread_and_enqueued(std::shared_ptr<rss_item> item, const std::string& feedurl) {
void cache::update_rssitem_unread_and_enqueued(
std::shared_ptr<rss_item> item, const std::string& feedurl)
{
update_rssitem_unread_and_enqueued(item.get(), feedurl);
}

Expand Down Expand Up @@ -705,18 +773,28 @@ void cache::update_rssitem_flags(rss_item* item) {
run_sql(update);
}

void cache::remove_old_deleted_items(const std::string& rssurl, const std::vector<std::string>& guids) {
void cache::remove_old_deleted_items(
const std::string& rssurl, const std::vector<std::string>& guids)
{
scope_measure m1("cache::remove_old_deleted_items");
if (guids.size() == 0) {
LOG(LOG_DEBUG, "cache::remove_old_deleted_items: not cleaning up anything because last reload brought no new items (detected no changes)");
LOG(LOG_DEBUG, "cache::remove_old_deleted_items: not cleaning up "
"anything because last reload brought no new items (detected "
"no changes)");
return;
}
std::string guidset = "(";
for (auto guid : guids) {
guidset.append(prepare_query("'%q', ", guid.c_str()));
}
guidset.append("'')");
std::string query = prepare_query("DELETE FROM rss_item WHERE feedurl = '%q' AND deleted = 1 AND guid NOT IN %s;", rssurl.c_str(), guidset.c_str());
std::string query = prepare_query(
"DELETE FROM rss_item "
"WHERE feedurl = '%q' "
"AND deleted = 1 "
"AND guid NOT IN %s;",
rssurl.c_str(),
guidset.c_str());
std::lock_guard<std::mutex> lock(mtx);
run_sql(query);
}
Expand All @@ -740,7 +818,10 @@ void cache::mark_items_read_by_guid(const std::vector<std::string>& guids) {
}
guidset.append("'')");

std::string updatequery = utils::strprintf("UPDATE rss_item SET unread = 0 WHERE unread = 1 AND guid IN %s;", guidset.c_str());
std::string updatequery =
utils::strprintf(
"UPDATE rss_item SET unread = 0 WHERE unread = 1 AND guid IN %s;",
guidset.c_str());

std::lock_guard<std::mutex> lock(mtx);
run_sql(updatequery);
Expand All @@ -763,8 +844,11 @@ void cache::clean_old_articles() {
if (days > 0) {
time_t old_date = time(nullptr) - days*24*60*60;

std::string query(utils::strprintf("DELETE FROM rss_item WHERE pubDate < %d", old_date));
LOG(LOG_DEBUG, "cache::clean_old_articles: about to delete articles with a pubDate older than %d", old_date);
std::string query(
utils::strprintf(
"DELETE FROM rss_item WHERE pubDate < %d", old_date));
LOG(LOG_DEBUG, "cache::clean_old_articles: about to delete articles "
"with a pubDate older than %d", old_date);
run_sql(query);
} else {
LOG(LOG_DEBUG, "cache::clean_old_articles, days == 0, not cleaning up anything");
Expand Down

0 comments on commit 5785a11

Please sign in to comment.