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

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

Issue 27047003: Precache tracking database (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@precache
Patch Set: General fixes, added tests, and moved PrecacheManager into component Created 7 years, 2 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
new file mode 100644
index 0000000000000000000000000000000000000000..09a7c4b7207a395eaa2186d0df08028cc2a9e402
--- /dev/null
+++ b/components/precache/core/precache_url_table.cc
@@ -0,0 +1,119 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/precache/core/precache_url_table.h"
+
+#include <string>
+
+#include "base/logging.h"
+#include "base/strings/stringprintf.h"
+#include "base/time/time.h"
+#include "sql/connection.h"
+#include "sql/statement.h"
+#include "url/gurl.h"
+
+using sql::Statement;
+
+namespace precache {
+
+namespace {
+
+const char kUrlTableName[] = "precache_urls";
+
+} // namespace
+
+// static
+std::string PrecacheURLTable::GetKey(const GURL& url) {
+ return url.spec();
+}
+
+PrecacheURLTable::PrecacheURLTable() : db_(NULL) {}
+
+PrecacheURLTable::~PrecacheURLTable() {}
+
+void PrecacheURLTable::Init(sql::Connection* db) {
+ DCHECK(!db_); // Init must only be called once.
+ DCHECK(db); // The database connection must be non-NULL.
+ db_ = db;
+ CreateTableIfNonExistent();
+}
+
+void PrecacheURLTable::AddURL(const GURL& url,
+ const base::Time& precache_time) {
+ DCHECK(db_);
+
+ Statement statement(db_->GetCachedStatement(
+ SQL_FROM_HERE,
+ base::StringPrintf("INSERT OR REPLACE INTO %s (url, time) VALUES(?,?)",
+ kUrlTableName).c_str()));
+ statement.BindString(0, GetKey(url));
+ statement.BindInt64(1, precache_time.ToInternalValue());
+
+ DCHECK(statement.Run());
+}
+
+void PrecacheURLTable::DeleteAllPrecachedBetween(const base::Time& delete_begin,
+ const base::Time& delete_end) {
+ DCHECK(db_);
+
+ Statement statement(db_->GetCachedStatement(
+ SQL_FROM_HERE,
+ base::StringPrintf("DELETE FROM %s WHERE time >= ? AND time <= ?",
+ kUrlTableName).c_str()));
+
+ statement.BindInt64(0, delete_begin.ToInternalValue());
+ statement.BindInt64(1, delete_end.ToInternalValue());
+
+ DCHECK(statement.Run());
+}
+
+bool PrecacheURLTable::HasURL(const GURL& url) {
+ DCHECK(db_);
+
+ Statement statement(db_->GetCachedStatement(
+ SQL_FROM_HERE, base::StringPrintf("SELECT time FROM %s WHERE url=?",
+ kUrlTableName).c_str()));
+ statement.BindString(0, GetKey(url));
+
+ return statement.Step();
+}
+
+void PrecacheURLTable::DeleteURL(const GURL& url) {
+ DCHECK(db_);
+
+ Statement statement(db_->GetCachedStatement(
+ SQL_FROM_HERE,
+ base::StringPrintf("DELETE FROM %s WHERE url=?", kUrlTableName).c_str()));
+ statement.BindString(0, GetKey(url));
+
+ DCHECK(statement.Run());
+}
+
+void PrecacheURLTable::GetAllData(std::map<GURL, base::Time>* map) {
+ DCHECK(db_);
+
+ Statement statement(db_->GetCachedStatement(
+ SQL_FROM_HERE,
+ base::StringPrintf("SELECT url, time FROM %s", kUrlTableName).c_str()));
+
+ while (statement.Step()) {
+ GURL url = GURL(statement.ColumnString(0));
+ (*map)[url] = base::Time::FromInternalValue(statement.ColumnInt64(1));
+ }
+}
+
+void PrecacheURLTable::CreateTableIfNonExistent() {
+ DCHECK(db_);
+
+ if (db_->DoesTableExist(kUrlTableName))
+ return;
+
+ const char* table_creator =
+ "CREATE TABLE %s (url TEXT, time INTEGER, PRIMARY KEY(url))";
+
+ DCHECK(
+ db_->Execute(base::StringPrintf(table_creator, kUrlTableName).c_str()));
+}
+
+} // namespace precache

Powered by Google App Engine
This is Rietveld 408576698