Index: chrome/browser/predictors/resource_prefetch_predictor_tables.cc |
diff --git a/chrome/browser/predictors/resource_prefetch_predictor_tables.cc b/chrome/browser/predictors/resource_prefetch_predictor_tables.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0c49fe11895d48ef8a0a56d3520cb2890621de64 |
--- /dev/null |
+++ b/chrome/browser/predictors/resource_prefetch_predictor_tables.cc |
@@ -0,0 +1,223 @@ |
+// Copyright (c) 2012 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 "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
+ |
+#include "base/logging.h" |
+#include "base/metrics/histogram.h" |
+#include "base/stringprintf.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "sql/statement.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace { |
+ |
+const char kResourcePredictorUrlTableName[] = "resource_prefetch_predictor_url"; |
+ |
+// The maximum length allowed for strings in the database. |
+const size_t kMaxURLLength = 2048; |
+ |
+void BindUrlTableRowToStatement( |
+ const predictors::ResourcePrefetchPredictorTables::UrlTableRow& row, |
+ sql::Statement* statement) { |
+ statement->BindString(0, row.main_frame_url_.spec().substr(0, kMaxURLLength)); |
+ statement->BindString(1, row.resource_url_.spec().substr(0, kMaxURLLength)); |
+ statement->BindInt(2, static_cast<int>(row.resource_type_)); |
+ statement->BindInt(3, row.number_of_hits_); |
+ statement->BindInt(4, row.number_of_misses_); |
+ statement->BindInt(5, row.consecutive_misses_); |
+ statement->BindDouble(6, row.average_position_); |
+} |
+ |
+bool StepAndInitializeUrlTableRow( |
+ sql::Statement* statement, |
+ predictors::ResourcePrefetchPredictorTables::UrlTableRow* row) { |
+ if (!statement->Step()) |
+ return false; |
+ |
+ row->main_frame_url_ = GURL(statement->ColumnString(0)); |
+ row->resource_url_ = GURL(statement->ColumnString(1)); |
+ row->resource_type_ = ResourceType::FromInt(statement->ColumnInt(2)); |
+ row->number_of_hits_ = statement->ColumnInt(3); |
+ row->number_of_misses_ = statement->ColumnInt(4); |
+ row->consecutive_misses_ = statement->ColumnInt(5); |
+ row->average_position_ = statement->ColumnDouble(6); |
+ return true; |
+} |
+ |
+} // namespace |
+ |
+namespace predictors { |
+ |
+ResourcePrefetchPredictorTables::UrlTableRow::UrlTableRow() |
+ : resource_type_(ResourceType::LAST_TYPE), |
+ number_of_hits_(0), |
+ number_of_misses_(0), |
+ consecutive_misses_(0), |
+ average_position_(0.0), |
+ score_(0.0) { |
+} |
+ |
+ResourcePrefetchPredictorTables::UrlTableRow::UrlTableRow( |
+ const UrlTableRow& other) |
+ : main_frame_url_(other.main_frame_url_), |
+ resource_url_(other.resource_url_), |
+ resource_type_(other.resource_type_), |
+ number_of_hits_(other.number_of_hits_), |
+ number_of_misses_(other.number_of_misses_), |
+ consecutive_misses_(other.consecutive_misses_), |
+ average_position_(other.average_position_), |
+ score_(other.score_) { |
+} |
+ |
+void ResourcePrefetchPredictorTables::UrlTableRow::UpdateScore() { |
+ // The score is calculated so that when the rows are sorted, the stylesheets |
+ // and scripts appear first, sorted by position(ascending) and then the rest |
+ // of the resources sorted by position(ascending). |
+ static const int kMaxResourcesPerType = 100; |
+ switch (resource_type_) { |
+ case ResourceType::STYLESHEET: |
+ case ResourceType::SCRIPT: |
+ score_ = (2 * kMaxResourcesPerType) - average_position_; |
+ break; |
+ |
+ case ResourceType::IMAGE: |
+ score_ = kMaxResourcesPerType - average_position_; |
+ break; |
+ |
+ default: |
+ score_ = kMaxResourcesPerType - average_position_; |
+ break; |
+ } |
+} |
+ |
+bool ResourcePrefetchPredictorTables::UrlTableRowSorter::operator()( |
+ const UrlTableRow& x, |
+ const UrlTableRow& y) const { |
+ return x.score_ > y.score_; |
+} |
+ |
+ResourcePrefetchPredictorTables::ResourcePrefetchPredictorTables() |
+ : PredictorTableBase() { |
+} |
+ |
+ResourcePrefetchPredictorTables::~ResourcePrefetchPredictorTables() { |
+} |
+ |
+void ResourcePrefetchPredictorTables::CreateTableIfNonExistent() { |
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
+ if (CantAccessDatabase()) |
+ return; |
+ |
+ std::string url_table_creation_statement = base::StringPrintf( |
+ "CREATE TABLE %s ( " |
+ "main_page_url TEXT, " |
+ "resource_url TEXT, " |
+ "resource_type INTEGER, " |
+ "number_of_hits INTEGER, " |
+ "number_of_misses INTEGER, " |
+ "consecutive_misses INTEGER, " |
+ "average_position DOUBLE, " |
+ "PRIMARY KEY(main_page_url, resource_url))", |
+ kResourcePredictorUrlTableName); |
+ |
+ if (!DB()->DoesTableExist(kResourcePredictorUrlTableName)) { |
dominich
2012/05/30 15:35:01
nit:
if (!DB()->DoesTableExist(...) && !DB()->Exe
Shishir
2012/05/30 18:07:15
Done.
|
+ if (!DB()->Execute(url_table_creation_statement.c_str())) { |
+ ResetDB(); |
+ return; |
dominich
2012/05/30 15:35:01
nit: unnecessary return
Shishir
2012/05/30 18:07:15
Done.
|
+ } |
+ } |
+} |
+ |
+void ResourcePrefetchPredictorTables::LogDatabaseStats() { |
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
+ if (CantAccessDatabase()) |
+ return; |
+ |
+ sql::Statement url_statement(DB()->GetUniqueStatement( |
+ base::StringPrintf("SELECT count(*) FROM %s", |
+ kResourcePredictorUrlTableName).c_str())); |
+ if (url_statement.Step()) |
+ UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.UrlTableRowCount", |
+ url_statement.ColumnInt(0)); |
+} |
+ |
+void ResourcePrefetchPredictorTables::GetAllRows( |
+ std::vector<UrlTableRow>* url_row_buffer) { |
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
+ if (CantAccessDatabase()) |
+ return; |
+ |
+ CHECK(url_row_buffer); |
+ url_row_buffer->clear(); |
dominich
2012/05/30 15:35:01
you could avoid the call to clear() and CHECK that
Shishir
2012/05/30 18:07:15
Done.
|
+ sql::Statement url_statement(DB()->GetCachedStatement(SQL_FROM_HERE, |
+ base::StringPrintf("SELECT * FROM %s", |
+ kResourcePredictorUrlTableName).c_str())); |
+ |
+ UrlTableRow url_row; |
+ while (StepAndInitializeUrlTableRow(&url_statement, &url_row)) { |
+ url_row.UpdateScore(); |
+ url_row_buffer->push_back(url_row); |
+ } |
+} |
+ |
+void ResourcePrefetchPredictorTables::UpdateRowsForUrl( |
+ const GURL& main_page_url, |
+ const std::vector<UrlTableRow>& row_buffer) { |
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
+ if (CantAccessDatabase()) |
+ return; |
+ |
+ DB()->BeginTransaction(); |
+ |
+ sql::Statement delete_statement(DB()->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ base::StringPrintf("DELETE FROM %s WHERE main_page_url='%s'", |
+ kResourcePredictorUrlTableName, |
+ main_page_url.spec().c_str()).c_str())); |
+ if (!delete_statement.Run()) { |
+ DB()->RollbackTransaction(); |
+ return; |
+ } |
+ |
+ for (int i = 0; i < static_cast<int>(row_buffer.size()); ++i) { |
dominich
2012/05/30 15:35:01
use iterator loop
Shishir
2012/05/30 18:07:15
Done.
|
+ const UrlTableRow& row = row_buffer.at(i); |
+ |
+ sql::Statement add_statement( |
+ DB()->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ base::StringPrintf( |
+ "INSERT INTO %s " |
+ "(main_page_url, resource_url, resource_type, number_of_hits, " |
+ "number_of_misses, consecutive_misses, average_position) " |
+ "VALUES (?,?,?,?,?,?,?)", |
+ kResourcePredictorUrlTableName).c_str())); |
+ BindUrlTableRowToStatement(row, &add_statement); |
+ if (!add_statement.Run()) { |
+ DB()->RollbackTransaction(); |
+ return; |
+ } |
+ } |
+ |
+ DB()->CommitTransaction(); |
+} |
+ |
+void ResourcePrefetchPredictorTables::DeleteUrlRows( |
+ const std::vector<GURL>& urls) { |
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
+ if (CantAccessDatabase()) |
+ return; |
+ |
+ // These do not need to be a transaction. |
+ for (int i = 0; i < static_cast<int>(urls.size()); ++i) { |
dominich
2012/05/30 15:35:01
iterator loop
Shishir
2012/05/30 18:07:15
Done.
|
+ sql::Statement delete_statement(DB()->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ base::StringPrintf("DELETE FROM %s WHERE main_page_url='%s'", |
+ kResourcePredictorUrlTableName, |
+ urls[i].spec().c_str()).c_str())); |
+ } |
+} |
+ |
+} // namespace predictors |