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 ResourcePrefetchPredictorTables::UrlTableRow::UrlTableRow( |
| 76 const std::string& i_main_frame_url, |
| 77 const std::string& i_resource_url, |
| 78 ResourceType::Type i_resource_type, |
| 79 int i_number_of_hits, |
| 80 int i_number_of_misses, |
| 81 int i_consecutive_misses, |
| 82 double i_average_position) |
| 83 : main_frame_url(i_main_frame_url), |
| 84 resource_url(i_resource_url), |
| 85 resource_type(i_resource_type), |
| 86 number_of_hits(i_number_of_hits), |
| 87 number_of_misses(i_number_of_misses), |
| 88 consecutive_misses(i_consecutive_misses), |
| 89 average_position(i_average_position) { |
| 90 UpdateScore(); |
| 91 } |
| 92 |
| 93 void ResourcePrefetchPredictorTables::UrlTableRow::UpdateScore() { |
| 94 // The score is calculated so that when the rows are sorted, the stylesheets |
| 95 // and scripts appear first, sorted by position(ascending) and then the rest |
| 96 // of the resources sorted by position(ascending). |
| 97 static const int kMaxResourcesPerType = 100; |
| 98 switch (resource_type) { |
| 99 case ResourceType::STYLESHEET: |
| 100 case ResourceType::SCRIPT: |
| 101 score = (2 * kMaxResourcesPerType) - average_position; |
| 102 break; |
| 103 |
| 104 case ResourceType::IMAGE: |
| 105 score = kMaxResourcesPerType - average_position; |
| 106 break; |
| 107 |
| 108 default: |
| 109 score = kMaxResourcesPerType - average_position; |
| 110 break; |
| 111 } |
| 112 } |
| 113 |
| 114 bool ResourcePrefetchPredictorTables::UrlTableRow::operator==( |
| 115 const UrlTableRow& rhs) const { |
| 116 return main_frame_url == rhs.main_frame_url && |
| 117 resource_url == rhs.resource_url && |
| 118 resource_type == rhs.resource_type && |
| 119 number_of_hits == rhs.number_of_hits && |
| 120 number_of_misses == rhs.number_of_misses && |
| 121 consecutive_misses == rhs.consecutive_misses && |
| 122 average_position == rhs.average_position && |
| 123 score == rhs.score; |
| 124 } |
| 125 |
| 126 bool ResourcePrefetchPredictorTables::UrlTableRowSorter::operator()( |
| 127 const UrlTableRow& x, |
| 128 const UrlTableRow& y) const { |
| 129 return x.score > y.score; |
| 130 } |
| 131 |
| 132 ResourcePrefetchPredictorTables::ResourcePrefetchPredictorTables() |
| 133 : PredictorTableBase() { |
| 134 } |
| 135 |
| 136 ResourcePrefetchPredictorTables::~ResourcePrefetchPredictorTables() { |
| 137 } |
| 138 |
| 139 void ResourcePrefetchPredictorTables::GetAllRows(UrlTableRows* url_row_buffer) { |
| 140 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 141 if (CantAccessDatabase()) |
| 142 return; |
| 143 |
| 144 CHECK(url_row_buffer && url_row_buffer->empty()); |
| 145 sql::Statement url_statement(DB()->GetCachedStatement(SQL_FROM_HERE, |
| 146 base::StringPrintf("SELECT * FROM %s", |
| 147 kResourcePredictorUrlTableName).c_str())); |
| 148 |
| 149 UrlTableRow url_row; |
| 150 while (StepAndInitializeUrlTableRow(&url_statement, &url_row)) { |
| 151 url_row.UpdateScore(); |
| 152 url_row_buffer->push_back(url_row); |
| 153 } |
| 154 } |
| 155 |
| 156 void ResourcePrefetchPredictorTables::UpdateRowsForUrl( |
| 157 const GURL& main_page_url, |
| 158 const UrlTableRows& row_buffer) { |
| 159 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 160 if (CantAccessDatabase()) |
| 161 return; |
| 162 |
| 163 DB()->BeginTransaction(); |
| 164 |
| 165 sql::Statement delete_statement(DB()->GetCachedStatement( |
| 166 SQL_FROM_HERE, |
| 167 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?", |
| 168 kResourcePredictorUrlTableName).c_str())); |
| 169 delete_statement.BindString(0, main_page_url.spec()); |
| 170 if (!delete_statement.Run()) { |
| 171 DB()->RollbackTransaction(); |
| 172 return; |
| 173 } |
| 174 |
| 175 for (UrlTableRows::const_iterator it = row_buffer.begin(); |
| 176 it != row_buffer.end(); ++it) { |
| 177 sql::Statement add_statement( |
| 178 DB()->GetCachedStatement( |
| 179 SQL_FROM_HERE, |
| 180 base::StringPrintf( |
| 181 "INSERT INTO %s " |
| 182 "(main_page_url, resource_url, resource_type, number_of_hits, " |
| 183 "number_of_misses, consecutive_misses, average_position) " |
| 184 "VALUES (?,?,?,?,?,?,?)", |
| 185 kResourcePredictorUrlTableName).c_str())); |
| 186 BindUrlTableRowToStatement(*it, &add_statement); |
| 187 if (!add_statement.Run()) { |
| 188 DB()->RollbackTransaction(); |
| 189 return; |
| 190 } |
| 191 } |
| 192 |
| 193 DB()->CommitTransaction(); |
| 194 } |
| 195 |
| 196 void ResourcePrefetchPredictorTables::DeleteRowsForUrls( |
| 197 const std::vector<GURL>& urls) { |
| 198 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 199 if (CantAccessDatabase()) |
| 200 return; |
| 201 |
| 202 // These do not need to be a transaction. |
| 203 for (std::vector<GURL>::const_iterator it = urls.begin(); it != urls.end(); |
| 204 ++it) { |
| 205 sql::Statement delete_statement(DB()->GetCachedStatement( |
| 206 SQL_FROM_HERE, |
| 207 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?", |
| 208 kResourcePredictorUrlTableName).c_str())); |
| 209 delete_statement.BindString(0, it->spec()); |
| 210 delete_statement.Run(); |
| 211 } |
| 212 } |
| 213 |
| 214 void ResourcePrefetchPredictorTables::DeleteAllRows() { |
| 215 if (CantAccessDatabase()) |
| 216 return; |
| 217 |
| 218 sql::Statement statement(DB()->GetCachedStatement( |
| 219 SQL_FROM_HERE, |
| 220 base::StringPrintf("DELETE FROM %s", |
| 221 kResourcePredictorUrlTableName).c_str())); |
| 222 statement.Run(); |
| 223 } |
| 224 |
| 225 void ResourcePrefetchPredictorTables::CreateTableIfNonExistent() { |
| 226 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 227 if (CantAccessDatabase()) |
| 228 return; |
| 229 |
| 230 std::string url_table_creation_statement = base::StringPrintf( |
| 231 "CREATE TABLE %s ( " |
| 232 "main_page_url TEXT, " |
| 233 "resource_url TEXT, " |
| 234 "resource_type INTEGER, " |
| 235 "number_of_hits INTEGER, " |
| 236 "number_of_misses INTEGER, " |
| 237 "consecutive_misses INTEGER, " |
| 238 "average_position DOUBLE, " |
| 239 "PRIMARY KEY(main_page_url, resource_url))", |
| 240 kResourcePredictorUrlTableName); |
| 241 |
| 242 if (!DB()->DoesTableExist(kResourcePredictorUrlTableName) && |
| 243 !DB()->Execute(url_table_creation_statement.c_str())) |
| 244 ResetDB(); |
| 245 } |
| 246 |
| 247 void ResourcePrefetchPredictorTables::LogDatabaseStats() { |
| 248 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 249 if (CantAccessDatabase()) |
| 250 return; |
| 251 |
| 252 sql::Statement url_statement(DB()->GetUniqueStatement( |
| 253 base::StringPrintf("SELECT count(*) FROM %s", |
| 254 kResourcePredictorUrlTableName).c_str())); |
| 255 if (url_statement.Step()) |
| 256 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.UrlTableRowCount", |
| 257 url_statement.ColumnInt(0)); |
| 258 } |
| 259 |
| 260 } // namespace predictors |
OLD | NEW |