Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Unified Diff: components/precache/core/precache_url_table.cc

Issue 2229983002: Send the list of used and unused resources for precache (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added-test Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/precache/core/precache_url_table.cc
diff --git a/components/precache/core/precache_url_table.cc b/components/precache/core/precache_url_table.cc
index 3c39b9e5e0da05b824a96e4ab58f9cfaee846a88..6714524b856993ec5a01cf2405f140c1a8a24e39 100644
--- a/components/precache/core/precache_url_table.cc
+++ b/components/precache/core/precache_url_table.cc
@@ -35,32 +35,78 @@ bool PrecacheURLTable::Init(sql::Connection* db) {
}
void PrecacheURLTable::AddURL(const GURL& url,
+ int64_t referrer_host_id,
+ bool is_precached,
const base::Time& precache_time) {
- Statement statement(db_->GetCachedStatement(
- SQL_FROM_HERE,
- "INSERT OR REPLACE INTO precache_urls (url, time) VALUES(?,?)"));
+ Statement statement(
+ db_->GetCachedStatement(SQL_FROM_HERE,
+ "INSERT OR REPLACE INTO precache_urls (url, "
+ "referrer_host_id, was_used, is_precached, time) "
+ "VALUES(?,?,0,?,?)"));
statement.BindString(0, GetKey(url));
- statement.BindInt64(1, precache_time.ToInternalValue());
+ statement.BindInt64(1, referrer_host_id);
+ statement.BindInt64(2, is_precached ? 1 : 0);
+ statement.BindInt64(3, precache_time.ToInternalValue());
statement.Run();
}
-bool PrecacheURLTable::HasURL(const GURL& url) {
+bool PrecacheURLTable::IsURLPrecached(const GURL& url) {
Statement statement(db_->GetCachedStatement(
- SQL_FROM_HERE, "SELECT time FROM precache_urls WHERE url=?"));
+ SQL_FROM_HERE,
+ "SELECT time FROM precache_urls WHERE url=? and is_precached=1"));
statement.BindString(0, GetKey(url));
return statement.Step();
}
-void PrecacheURLTable::DeleteURL(const GURL& url) {
- Statement statement(db_->GetCachedStatement(
- SQL_FROM_HERE, "DELETE FROM precache_urls WHERE url=?"));
+bool PrecacheURLTable::IsURLPrecachedAndUnused(const GURL& url) {
+ Statement statement(db_->GetCachedStatement(SQL_FROM_HERE,
+ "SELECT time FROM precache_urls "
+ "WHERE url=? and is_precached=1 "
+ "and was_used=0"));
+
+ statement.BindString(0, GetKey(url));
+ return statement.Step();
+}
+
+void PrecacheURLTable::SetPrecachedURLAsUsed(const GURL& url) {
+ Statement statement(
+ db_->GetCachedStatement(SQL_FROM_HERE,
+ "UPDATE precache_urls SET was_used=1, "
+ "is_precached=0 "
+ "WHERE url=? and was_used=0 and is_precached=1"));
+
+ statement.BindString(0, GetKey(url));
+ statement.Run();
+}
+void PrecacheURLTable::SetURLAsNotPrecached(const GURL& url) {
+ Statement statement(
+ db_->GetCachedStatement(SQL_FROM_HERE,
+ "UPDATE precache_urls SET is_precached=0 "
+ "WHERE url=? and is_precached=1"));
statement.BindString(0, GetKey(url));
statement.Run();
}
+void PrecacheURLTable::GetURLListForReferrerHost(
+ int64_t referrer_host_id,
+ std::vector<GURL>* used_urls,
+ std::vector<GURL>* unused_urls) {
+ Statement statement(db_->GetCachedStatement(
+ SQL_FROM_HERE,
+ "SELECT url, was_used from precache_urls where referrer_host_id=?"));
+ statement.BindInt64(0, referrer_host_id);
+ while (statement.Step()) {
+ GURL url(statement.ColumnString(0));
+ if (statement.ColumnInt(1))
+ used_urls->push_back(url);
+ else
+ unused_urls->push_back(url);
+ }
+}
+
void PrecacheURLTable::DeleteAllPrecachedBefore(const base::Time& delete_end) {
Statement statement(db_->GetCachedStatement(
SQL_FROM_HERE, "DELETE FROM precache_urls WHERE time < ?"));
@@ -80,7 +126,8 @@ void PrecacheURLTable::GetAllDataForTesting(std::map<GURL, base::Time>* map) {
map->clear();
Statement statement(db_->GetCachedStatement(
- SQL_FROM_HERE, "SELECT url, time FROM precache_urls"));
+ SQL_FROM_HERE,
+ "SELECT url, time FROM precache_urls where is_precached=1"));
while (statement.Step()) {
GURL url = GURL(statement.ColumnString(0));
@@ -89,9 +136,27 @@ void PrecacheURLTable::GetAllDataForTesting(std::map<GURL, base::Time>* map) {
}
bool PrecacheURLTable::CreateTableIfNonExistent() {
- return db_->Execute(
- "CREATE TABLE IF NOT EXISTS precache_urls (url TEXT PRIMARY KEY, time "
- "INTEGER)");
+ if (!db_->DoesTableExist("precache_urls")) {
+ return db_->Execute(
+ "CREATE TABLE precache_urls "
+ "(url TEXT PRIMARY KEY, referrer_host_id INTEGER, was_used INTEGER, "
+ "is_precached INTEGER, "
+ "time INTEGER)");
+ } else {
+ // Migrate the table by creating the missing columns.
+ if (!db_->DoesColumnExist("precache_urls", "was_used") &&
+ !db_->Execute("ALTER TABLE precache_urls ADD COLUMN was_used INTEGER"))
+ return false;
+ if (!db_->DoesColumnExist("precache_urls", "is_precached") &&
+ !db_->Execute(
+ "ALTER TABLE precache_urls ADD COLUMN is_precached INTEGER"))
+ return false;
+ if (!db_->DoesColumnExist("precache_urls", "referrer_host_id") &&
+ !db_->Execute(
+ "ALTER TABLE precache_urls ADD COLUMN referrer_host_id INTEGER"))
+ return false;
+ }
+ return true;
}
} // namespace precache

Powered by Google App Engine
This is Rietveld 408576698