OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/metrics/histogram.h" |
| 9 #include "base/stringprintf.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "sql/statement.h" |
| 12 |
| 13 using content::BrowserThread; |
| 14 |
| 15 namespace { |
| 16 |
| 17 const char kResourcePredictorUrlTableName[] = "resource_prefetch_predictor_url"; |
| 18 |
| 19 // The maximum length allowed for strings in the database. |
| 20 const size_t kMaxURLLength = 2048; |
| 21 |
| 22 void BindUrlTableRowToStatement( |
| 23 const predictors::ResourcePrefetchPredictorTables::UrlTableRow& row, |
| 24 sql::Statement* statement) { |
| 25 statement->BindString(0, row.main_frame_url_.spec().substr(0, kMaxURLLength)); |
| 26 statement->BindString(1, row.resource_url_.spec().substr(0, kMaxURLLength)); |
| 27 statement->BindInt(2, static_cast<int>(row.resource_type_)); |
| 28 statement->BindInt(3, row.number_of_hits_); |
| 29 statement->BindInt(4, row.number_of_misses_); |
| 30 statement->BindInt(5, row.consecutive_misses_); |
| 31 statement->BindDouble(6, row.average_position_); |
| 32 } |
| 33 |
| 34 bool StepAndInitializeUrlTableRow( |
| 35 sql::Statement* statement, |
| 36 predictors::ResourcePrefetchPredictorTables::UrlTableRow* row) { |
| 37 if (!statement->Step()) |
| 38 return false; |
| 39 |
| 40 row->main_frame_url_ = GURL(statement->ColumnString(0)); |
| 41 row->resource_url_ = GURL(statement->ColumnString(1)); |
| 42 row->resource_type_ = ResourceType::FromInt(statement->ColumnInt(2)); |
| 43 row->number_of_hits_ = statement->ColumnInt(3); |
| 44 row->number_of_misses_ = statement->ColumnInt(4); |
| 45 row->consecutive_misses_ = statement->ColumnInt(5); |
| 46 row->average_position_ = statement->ColumnDouble(6); |
| 47 return true; |
| 48 } |
| 49 |
| 50 } // namespace |
| 51 |
| 52 namespace predictors { |
| 53 |
| 54 ResourcePrefetchPredictorTables::UrlTableRow::UrlTableRow() |
| 55 : resource_type_(ResourceType::LAST_TYPE), |
| 56 number_of_hits_(0), |
| 57 number_of_misses_(0), |
| 58 consecutive_misses_(0), |
| 59 average_position_(0.0), |
| 60 score_(0.0) { |
| 61 } |
| 62 |
| 63 ResourcePrefetchPredictorTables::UrlTableRow::UrlTableRow( |
| 64 const UrlTableRow& other) |
| 65 : main_frame_url_(other.main_frame_url_), |
| 66 resource_url_(other.resource_url_), |
| 67 resource_type_(other.resource_type_), |
| 68 number_of_hits_(other.number_of_hits_), |
| 69 number_of_misses_(other.number_of_misses_), |
| 70 consecutive_misses_(other.consecutive_misses_), |
| 71 average_position_(other.average_position_), |
| 72 score_(other.score_) { |
| 73 } |
| 74 |
| 75 void ResourcePrefetchPredictorTables::UrlTableRow::UpdateScore() { |
| 76 // The score is calculated so that when the rows are sorted, the stylesheets |
| 77 // and scripts appear first, sorted by position(ascending) and then the rest |
| 78 // of the resources sorted by position(ascending). |
| 79 static const int kMaxResourcesPerType = 100; |
| 80 switch (resource_type_) { |
| 81 case ResourceType::STYLESHEET: |
| 82 case ResourceType::SCRIPT: |
| 83 score_ = (2 * kMaxResourcesPerType) - average_position_; |
| 84 break; |
| 85 |
| 86 case ResourceType::IMAGE: |
| 87 score_ = kMaxResourcesPerType - average_position_; |
| 88 break; |
| 89 |
| 90 default: |
| 91 score_ = kMaxResourcesPerType - average_position_; |
| 92 break; |
| 93 } |
| 94 } |
| 95 |
| 96 bool ResourcePrefetchPredictorTables::UrlTableRowSorter::operator()( |
| 97 const UrlTableRow& x, |
| 98 const UrlTableRow& y) const { |
| 99 return x.score_ > y.score_; |
| 100 } |
| 101 |
| 102 ResourcePrefetchPredictorTables::ResourcePrefetchPredictorTables() |
| 103 : PredictorTableBase() { |
| 104 } |
| 105 |
| 106 ResourcePrefetchPredictorTables::~ResourcePrefetchPredictorTables() { |
| 107 } |
| 108 |
| 109 void ResourcePrefetchPredictorTables::CreateTableIfNonExistent() { |
| 110 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 111 if (CantAccessDatabase()) |
| 112 return; |
| 113 |
| 114 std::string url_table_creation_statement = base::StringPrintf( |
| 115 "CREATE TABLE %s ( " |
| 116 "main_page_url TEXT, " |
| 117 "resource_url TEXT, " |
| 118 "resource_type INTEGER, " |
| 119 "number_of_hits INTEGER, " |
| 120 "number_of_misses INTEGER, " |
| 121 "consecutive_misses INTEGER, " |
| 122 "average_position DOUBLE, " |
| 123 "PRIMARY KEY(main_page_url, resource_url))", |
| 124 kResourcePredictorUrlTableName); |
| 125 |
| 126 if (!DB()->DoesTableExist(kResourcePredictorUrlTableName) && |
| 127 !DB()->Execute(url_table_creation_statement.c_str())) |
| 128 ResetDB(); |
| 129 } |
| 130 |
| 131 void ResourcePrefetchPredictorTables::LogDatabaseStats() { |
| 132 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 133 if (CantAccessDatabase()) |
| 134 return; |
| 135 |
| 136 sql::Statement url_statement(DB()->GetUniqueStatement( |
| 137 base::StringPrintf("SELECT count(*) FROM %s", |
| 138 kResourcePredictorUrlTableName).c_str())); |
| 139 if (url_statement.Step()) |
| 140 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.UrlTableRowCount", |
| 141 url_statement.ColumnInt(0)); |
| 142 } |
| 143 |
| 144 void ResourcePrefetchPredictorTables::GetAllRows( |
| 145 std::vector<UrlTableRow>* url_row_buffer) { |
| 146 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 147 if (CantAccessDatabase()) |
| 148 return; |
| 149 |
| 150 CHECK(url_row_buffer && url_row_buffer->empty()); |
| 151 sql::Statement url_statement(DB()->GetCachedStatement(SQL_FROM_HERE, |
| 152 base::StringPrintf("SELECT * FROM %s", |
| 153 kResourcePredictorUrlTableName).c_str())); |
| 154 |
| 155 UrlTableRow url_row; |
| 156 while (StepAndInitializeUrlTableRow(&url_statement, &url_row)) { |
| 157 url_row.UpdateScore(); |
| 158 url_row_buffer->push_back(url_row); |
| 159 } |
| 160 } |
| 161 |
| 162 void ResourcePrefetchPredictorTables::UpdateRowsForUrl( |
| 163 const GURL& main_page_url, |
| 164 const std::vector<UrlTableRow>& row_buffer) { |
| 165 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 166 if (CantAccessDatabase()) |
| 167 return; |
| 168 |
| 169 DB()->BeginTransaction(); |
| 170 |
| 171 sql::Statement delete_statement(DB()->GetCachedStatement( |
| 172 SQL_FROM_HERE, |
| 173 base::StringPrintf("DELETE FROM %s WHERE main_page_url='%s'", |
| 174 kResourcePredictorUrlTableName, |
| 175 main_page_url.spec().c_str()).c_str())); |
| 176 if (!delete_statement.Run()) { |
| 177 DB()->RollbackTransaction(); |
| 178 return; |
| 179 } |
| 180 |
| 181 for (std::vector<UrlTableRow>::const_iterator it = row_buffer.begin(); |
| 182 it != row_buffer.end(); ++it) { |
| 183 sql::Statement add_statement( |
| 184 DB()->GetCachedStatement( |
| 185 SQL_FROM_HERE, |
| 186 base::StringPrintf( |
| 187 "INSERT INTO %s " |
| 188 "(main_page_url, resource_url, resource_type, number_of_hits, " |
| 189 "number_of_misses, consecutive_misses, average_position) " |
| 190 "VALUES (?,?,?,?,?,?,?)", |
| 191 kResourcePredictorUrlTableName).c_str())); |
| 192 BindUrlTableRowToStatement(*it, &add_statement); |
| 193 if (!add_statement.Run()) { |
| 194 DB()->RollbackTransaction(); |
| 195 return; |
| 196 } |
| 197 } |
| 198 |
| 199 DB()->CommitTransaction(); |
| 200 } |
| 201 |
| 202 void ResourcePrefetchPredictorTables::DeleteUrlRows( |
| 203 const std::vector<GURL>& urls) { |
| 204 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 205 if (CantAccessDatabase()) |
| 206 return; |
| 207 |
| 208 // These do not need to be a transaction. |
| 209 for (std::vector<GURL>::const_iterator it = urls.begin(); it != urls.end(); |
| 210 ++it) { |
| 211 sql::Statement delete_statement(DB()->GetCachedStatement( |
| 212 SQL_FROM_HERE, |
| 213 base::StringPrintf("DELETE FROM %s WHERE main_page_url='%s'", |
| 214 kResourcePredictorUrlTableName, |
| 215 it->spec().c_str()).c_str())); |
| 216 } |
| 217 } |
| 218 |
| 219 } // namespace predictors |
OLD | NEW |