| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 5 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "sql/statement.h" | 17 #include "sql/statement.h" |
| 18 | 18 |
| 19 using content::BrowserThread; | 19 using content::BrowserThread; |
| 20 using sql::Statement; | 20 using sql::Statement; |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 using ResourceRow = predictors::ResourcePrefetchPredictorTables::ResourceRow; |
| 25 |
| 24 const char kUrlResourceTableName[] = "resource_prefetch_predictor_url"; | 26 const char kUrlResourceTableName[] = "resource_prefetch_predictor_url"; |
| 25 const char kUrlMetadataTableName[] = "resource_prefetch_predictor_url_metadata"; | 27 const char kUrlMetadataTableName[] = "resource_prefetch_predictor_url_metadata"; |
| 26 const char kHostResourceTableName[] = "resource_prefetch_predictor_host"; | 28 const char kHostResourceTableName[] = "resource_prefetch_predictor_host"; |
| 27 const char kHostMetadataTableName[] = | 29 const char kHostMetadataTableName[] = |
| 28 "resource_prefetch_predictor_host_metadata"; | 30 "resource_prefetch_predictor_host_metadata"; |
| 29 | 31 |
| 30 const char kInsertResourceTableStatementTemplate[] = | 32 const char kInsertResourceTableStatementTemplate[] = |
| 31 "INSERT INTO %s " | 33 "INSERT INTO %s (main_page_url, resource_url, proto) VALUES (?,?,?)"; |
| 32 "(main_page_url, resource_url, resource_type, number_of_hits, " | 34 const char kInsertMetadataTableStatementTemplate[] = |
| 33 "number_of_misses, consecutive_misses, average_position, priority, " | 35 "INSERT INTO %s (main_page_url, last_visit_time) VALUES (?,?)"; |
| 34 "has_validators, always_revalidate) " | 36 const char kDeleteStatementTemplate[] = "DELETE FROM %s WHERE main_page_url=?"; |
| 35 "VALUES (?,?,?,?,?,?,?,?,?,?)"; | |
| 36 | 37 |
| 37 void BindResourceRowToStatement( | 38 void BindResourceRowToStatement(const ResourceRow& row, |
| 38 const predictors::ResourcePrefetchPredictorTables::ResourceRow& row, | 39 const std::string& primary_key, |
| 39 const std::string& primary_key, | 40 Statement* statement) { |
| 40 Statement* statement) { | 41 chrome_browser_predictors::ResourceData proto; |
| 42 row.ToProto(&proto); |
| 43 int size = proto.ByteSize(); |
| 44 std::vector<char> proto_buffer(size); |
| 45 proto.SerializeToArray(&proto_buffer[0], size); |
| 46 |
| 41 statement->BindString(0, primary_key); | 47 statement->BindString(0, primary_key); |
| 42 statement->BindString(1, row.resource_url.spec()); | 48 statement->BindString(1, row.resource_url.spec()); |
| 43 statement->BindInt(2, static_cast<int>(row.resource_type)); | 49 statement->BindBlob(2, &proto_buffer[0], size); |
| 44 statement->BindInt(3, row.number_of_hits); | |
| 45 statement->BindInt(4, row.number_of_misses); | |
| 46 statement->BindInt(5, row.consecutive_misses); | |
| 47 statement->BindDouble(6, row.average_position); | |
| 48 statement->BindInt(7, static_cast<int>(row.priority)); | |
| 49 statement->BindInt(8, static_cast<int>(row.has_validators)); | |
| 50 statement->BindInt(9, static_cast<int>(row.always_revalidate)); | |
| 51 } | 50 } |
| 52 | 51 |
| 53 bool StepAndInitializeResourceRow( | 52 bool StepAndInitializeResourceRow(Statement* statement, ResourceRow* row) { |
| 54 Statement* statement, | |
| 55 predictors::ResourcePrefetchPredictorTables::ResourceRow* row) { | |
| 56 if (!statement->Step()) | 53 if (!statement->Step()) |
| 57 return false; | 54 return false; |
| 58 | 55 |
| 56 int size = statement->ColumnByteLength(2); |
| 57 const void* data = statement->ColumnBlob(2); |
| 58 DCHECK(data); |
| 59 chrome_browser_predictors::ResourceData proto; |
| 60 proto.ParseFromArray(data, size); |
| 61 ResourceRow::FromProto(proto, row); |
| 62 |
| 59 row->primary_key = statement->ColumnString(0); | 63 row->primary_key = statement->ColumnString(0); |
| 60 row->resource_url = GURL(statement->ColumnString(1)); | 64 row->resource_url = GURL(statement->ColumnString(1)); |
| 61 row->resource_type = static_cast<content::ResourceType>( | |
| 62 statement->ColumnInt(2)); | |
| 63 row->number_of_hits = statement->ColumnInt(3); | |
| 64 row->number_of_misses = statement->ColumnInt(4); | |
| 65 row->consecutive_misses = statement->ColumnInt(5); | |
| 66 row->average_position = statement->ColumnDouble(6); | |
| 67 row->priority = static_cast<net::RequestPriority>(statement->ColumnInt(7)); | |
| 68 // static_cast<bool> creates a C4800 warning with Visual Studio. | |
| 69 row->has_validators = statement->ColumnInt(8) != 0; | |
| 70 row->always_revalidate = statement->ColumnInt(9) != 0; | |
| 71 | |
| 72 return true; | 65 return true; |
| 73 } | 66 } |
| 74 | 67 |
| 75 } // namespace | 68 } // namespace |
| 76 | 69 |
| 77 namespace predictors { | 70 namespace predictors { |
| 78 | 71 |
| 79 // static | 72 // static |
| 80 const size_t ResourcePrefetchPredictorTables::kMaxStringLength = 1024; | 73 const size_t ResourcePrefetchPredictorTables::kMaxStringLength = 1024; |
| 81 | 74 |
| 82 ResourcePrefetchPredictorTables::ResourceRow::ResourceRow() | 75 ResourceRow::ResourceRow() |
| 83 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), | 76 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), |
| 84 number_of_hits(0), | 77 number_of_hits(0), |
| 85 number_of_misses(0), | 78 number_of_misses(0), |
| 86 consecutive_misses(0), | 79 consecutive_misses(0), |
| 87 average_position(0.0), | 80 average_position(0.0), |
| 88 priority(net::IDLE), | 81 priority(net::IDLE), |
| 89 has_validators(false), | 82 has_validators(false), |
| 90 always_revalidate(false), | 83 always_revalidate(false), |
| 91 score(0.0) {} | 84 score(0.0) {} |
| 92 | 85 |
| 93 ResourcePrefetchPredictorTables::ResourceRow::ResourceRow( | 86 ResourceRow::ResourceRow(const ResourceRow& other) |
| 94 const ResourceRow& other) | |
| 95 : primary_key(other.primary_key), | 87 : primary_key(other.primary_key), |
| 96 resource_url(other.resource_url), | 88 resource_url(other.resource_url), |
| 97 resource_type(other.resource_type), | 89 resource_type(other.resource_type), |
| 98 number_of_hits(other.number_of_hits), | 90 number_of_hits(other.number_of_hits), |
| 99 number_of_misses(other.number_of_misses), | 91 number_of_misses(other.number_of_misses), |
| 100 consecutive_misses(other.consecutive_misses), | 92 consecutive_misses(other.consecutive_misses), |
| 101 average_position(other.average_position), | 93 average_position(other.average_position), |
| 102 priority(other.priority), | 94 priority(other.priority), |
| 103 has_validators(other.has_validators), | 95 has_validators(other.has_validators), |
| 104 always_revalidate(other.always_revalidate), | 96 always_revalidate(other.always_revalidate), |
| 105 score(other.score) {} | 97 score(other.score) {} |
| 106 | 98 |
| 107 ResourcePrefetchPredictorTables::ResourceRow::ResourceRow( | 99 ResourceRow::ResourceRow(const std::string& i_primary_key, |
| 108 const std::string& i_primary_key, | 100 const std::string& i_resource_url, |
| 109 const std::string& i_resource_url, | 101 content::ResourceType i_resource_type, |
| 110 content::ResourceType i_resource_type, | 102 int i_number_of_hits, |
| 111 int i_number_of_hits, | 103 int i_number_of_misses, |
| 112 int i_number_of_misses, | 104 int i_consecutive_misses, |
| 113 int i_consecutive_misses, | 105 double i_average_position, |
| 114 double i_average_position, | 106 net::RequestPriority i_priority, |
| 115 net::RequestPriority i_priority, | 107 bool i_has_validators, |
| 116 bool i_has_validators, | 108 bool i_always_revalidate) |
| 117 bool i_always_revalidate) | |
| 118 : primary_key(i_primary_key), | 109 : primary_key(i_primary_key), |
| 119 resource_url(i_resource_url), | 110 resource_url(i_resource_url), |
| 120 resource_type(i_resource_type), | 111 resource_type(i_resource_type), |
| 121 number_of_hits(i_number_of_hits), | 112 number_of_hits(i_number_of_hits), |
| 122 number_of_misses(i_number_of_misses), | 113 number_of_misses(i_number_of_misses), |
| 123 consecutive_misses(i_consecutive_misses), | 114 consecutive_misses(i_consecutive_misses), |
| 124 average_position(i_average_position), | 115 average_position(i_average_position), |
| 125 priority(i_priority), | 116 priority(i_priority), |
| 126 has_validators(i_has_validators), | 117 has_validators(i_has_validators), |
| 127 always_revalidate(i_always_revalidate) { | 118 always_revalidate(i_always_revalidate) { |
| 128 UpdateScore(); | 119 UpdateScore(); |
| 129 } | 120 } |
| 130 | 121 |
| 131 void ResourcePrefetchPredictorTables::ResourceRow::UpdateScore() { | 122 void ResourceRow::UpdateScore() { |
| 132 // The score is calculated so that when the rows are sorted, stylesheets, | 123 // The score is calculated so that when the rows are sorted, stylesheets, |
| 133 // scripts and fonts appear first, sorted by position(ascending) and then the | 124 // scripts and fonts appear first, sorted by position(ascending) and then the |
| 134 // rest of the resources sorted by position (ascending). | 125 // rest of the resources sorted by position (ascending). |
| 135 static const int kMaxResourcesPerType = 100; | 126 static const int kMaxResourcesPerType = 100; |
| 136 switch (resource_type) { | 127 switch (resource_type) { |
| 137 case content::RESOURCE_TYPE_STYLESHEET: | 128 case content::RESOURCE_TYPE_STYLESHEET: |
| 138 case content::RESOURCE_TYPE_SCRIPT: | 129 case content::RESOURCE_TYPE_SCRIPT: |
| 139 case content::RESOURCE_TYPE_FONT_RESOURCE: | 130 case content::RESOURCE_TYPE_FONT_RESOURCE: |
| 140 score = (2 * kMaxResourcesPerType) - average_position; | 131 score = (2 * kMaxResourcesPerType) - average_position; |
| 141 break; | 132 break; |
| 142 | 133 |
| 143 case content::RESOURCE_TYPE_IMAGE: | 134 case content::RESOURCE_TYPE_IMAGE: |
| 144 default: | 135 default: |
| 145 score = kMaxResourcesPerType - average_position; | 136 score = kMaxResourcesPerType - average_position; |
| 146 break; | 137 break; |
| 147 } | 138 } |
| 148 // TODO(lizeb): Take priority into account. | 139 // TODO(lizeb): Take priority into account. |
| 149 } | 140 } |
| 150 | 141 |
| 151 bool ResourcePrefetchPredictorTables::ResourceRow::operator==( | 142 bool ResourceRow::operator==(const ResourceRow& rhs) const { |
| 152 const ResourceRow& rhs) const { | |
| 153 return primary_key == rhs.primary_key && resource_url == rhs.resource_url && | 143 return primary_key == rhs.primary_key && resource_url == rhs.resource_url && |
| 154 resource_type == rhs.resource_type && | 144 resource_type == rhs.resource_type && |
| 155 number_of_hits == rhs.number_of_hits && | 145 number_of_hits == rhs.number_of_hits && |
| 156 number_of_misses == rhs.number_of_misses && | 146 number_of_misses == rhs.number_of_misses && |
| 157 consecutive_misses == rhs.consecutive_misses && | 147 consecutive_misses == rhs.consecutive_misses && |
| 158 average_position == rhs.average_position && priority == rhs.priority && | 148 average_position == rhs.average_position && priority == rhs.priority && |
| 159 has_validators == rhs.has_validators && | 149 has_validators == rhs.has_validators && |
| 160 always_revalidate == rhs.always_revalidate && score == rhs.score; | 150 always_revalidate == rhs.always_revalidate && score == rhs.score; |
| 161 } | 151 } |
| 162 | 152 |
| 163 bool ResourcePrefetchPredictorTables::ResourceRowSorter::operator()( | 153 void ResourceRow::ToProto(ResourceData* resource_data) const { |
| 164 const ResourceRow& x, const ResourceRow& y) const { | 154 using chrome_browser_predictors::ResourceData_Priority; |
| 165 return x.score > y.score; | 155 using chrome_browser_predictors::ResourceData_ResourceType; |
| 156 |
| 157 resource_data->set_primary_key(primary_key); |
| 158 resource_data->set_resource_url(resource_url.spec()); |
| 159 resource_data->set_resource_type( |
| 160 static_cast<ResourceData_ResourceType>(resource_type)); |
| 161 resource_data->set_number_of_hits(number_of_hits); |
| 162 resource_data->set_number_of_misses(number_of_misses); |
| 163 resource_data->set_consecutive_misses(consecutive_misses); |
| 164 resource_data->set_average_position(average_position); |
| 165 resource_data->set_priority(static_cast<ResourceData_Priority>(priority)); |
| 166 resource_data->set_has_validators(has_validators); |
| 167 resource_data->set_always_revalidate(always_revalidate); |
| 168 } |
| 169 |
| 170 // static |
| 171 void ResourceRow::FromProto(const ResourceData& proto, ResourceRow* row) { |
| 172 DCHECK(proto.has_primary_key()); |
| 173 row->primary_key = proto.primary_key(); |
| 174 row->resource_url = GURL(proto.resource_url()); |
| 175 row->resource_type = |
| 176 static_cast<content::ResourceType>(proto.resource_type()); |
| 177 row->number_of_hits = proto.number_of_hits(); |
| 178 row->number_of_misses = proto.number_of_misses(); |
| 179 row->consecutive_misses = proto.consecutive_misses(); |
| 180 row->average_position = proto.average_position(); |
| 181 row->priority = static_cast<net::RequestPriority>(proto.priority()); |
| 182 row->has_validators = proto.has_validators(); |
| 183 row->always_revalidate = proto.always_revalidate(); |
| 184 } |
| 185 |
| 186 // static |
| 187 void ResourcePrefetchPredictorTables::SortResourceRows(ResourceRows* rows) { |
| 188 std::sort(rows->begin(), rows->end(), |
| 189 [](const ResourceRow& x, const ResourceRow& y) { |
| 190 return x.score > y.score; |
| 191 }); |
| 166 } | 192 } |
| 167 | 193 |
| 168 ResourcePrefetchPredictorTables::PrefetchData::PrefetchData( | 194 ResourcePrefetchPredictorTables::PrefetchData::PrefetchData( |
| 169 PrefetchKeyType i_key_type, | 195 PrefetchKeyType i_key_type, |
| 170 const std::string& i_primary_key) | 196 const std::string& i_primary_key) |
| 171 : key_type(i_key_type), | 197 : key_type(i_key_type), |
| 172 primary_key(i_primary_key) { | 198 primary_key(i_primary_key) { |
| 173 } | 199 } |
| 174 | 200 |
| 175 ResourcePrefetchPredictorTables::PrefetchData::PrefetchData( | 201 ResourcePrefetchPredictorTables::PrefetchData::PrefetchData( |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 | 326 |
| 301 PrefetchDataMap::iterator it = data_map->find(primary_key); | 327 PrefetchDataMap::iterator it = data_map->find(primary_key); |
| 302 if (it == data_map->end()) { | 328 if (it == data_map->end()) { |
| 303 it = data_map->insert(std::make_pair( | 329 it = data_map->insert(std::make_pair( |
| 304 primary_key, PrefetchData(key_type, primary_key))).first; | 330 primary_key, PrefetchData(key_type, primary_key))).first; |
| 305 } | 331 } |
| 306 it->second.resources.push_back(row); | 332 it->second.resources.push_back(row); |
| 307 } | 333 } |
| 308 | 334 |
| 309 // Sort each of the resource row vectors by score. | 335 // Sort each of the resource row vectors by score. |
| 310 for (PrefetchDataMap::iterator it = data_map->begin(); it != data_map->end(); | 336 for (auto& kv : *data_map) |
| 311 ++it) { | 337 SortResourceRows(&(kv.second.resources)); |
| 312 std::sort(it->second.resources.begin(), | |
| 313 it->second.resources.end(), | |
| 314 ResourceRowSorter()); | |
| 315 } | |
| 316 | 338 |
| 317 // Read the metadata and keep track of entries that have metadata, but no | 339 // Read the metadata and keep track of entries that have metadata, but no |
| 318 // resource entries, so they can be deleted. | 340 // resource entries, so they can be deleted. |
| 319 const char* metadata_table_name = is_host ? kHostMetadataTableName : | 341 const char* metadata_table_name = is_host ? kHostMetadataTableName : |
| 320 kUrlMetadataTableName; | 342 kUrlMetadataTableName; |
| 321 Statement metadata_reader(DB()->GetUniqueStatement( | 343 Statement metadata_reader(DB()->GetUniqueStatement( |
| 322 base::StringPrintf("SELECT * FROM %s", metadata_table_name).c_str())); | 344 base::StringPrintf("SELECT * FROM %s", metadata_table_name).c_str())); |
| 323 | 345 |
| 324 while (metadata_reader.Step()) { | 346 while (metadata_reader.Step()) { |
| 325 std::string primary_key = metadata_reader.ColumnString(0); | 347 std::string primary_key = metadata_reader.ColumnString(0); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 352 return false; | 374 return false; |
| 353 | 375 |
| 354 deleter.reset(data.is_host() ? GetHostMetadataDeleteStatement() : | 376 deleter.reset(data.is_host() ? GetHostMetadataDeleteStatement() : |
| 355 GetUrlMetadataDeleteStatement()); | 377 GetUrlMetadataDeleteStatement()); |
| 356 deleter->BindString(0, data.primary_key); | 378 deleter->BindString(0, data.primary_key); |
| 357 if (!deleter->Run()) | 379 if (!deleter->Run()) |
| 358 return false; | 380 return false; |
| 359 | 381 |
| 360 // Add the new data to the tables. | 382 // Add the new data to the tables. |
| 361 const ResourceRows& resources = data.resources; | 383 const ResourceRows& resources = data.resources; |
| 362 for (ResourceRows::const_iterator it = resources.begin(); | 384 for (const ResourceRow& resource : resources) { |
| 363 it != resources.end(); ++it) { | |
| 364 std::unique_ptr<Statement> resource_inserter( | 385 std::unique_ptr<Statement> resource_inserter( |
| 365 data.is_host() ? GetHostResourceUpdateStatement() | 386 data.is_host() ? GetHostResourceUpdateStatement() |
| 366 : GetUrlResourceUpdateStatement()); | 387 : GetUrlResourceUpdateStatement()); |
| 367 BindResourceRowToStatement(*it, data.primary_key, resource_inserter.get()); | 388 BindResourceRowToStatement(resource, data.primary_key, |
| 389 resource_inserter.get()); |
| 368 if (!resource_inserter->Run()) | 390 if (!resource_inserter->Run()) |
| 369 return false; | 391 return false; |
| 370 } | 392 } |
| 371 | 393 |
| 372 std::unique_ptr<Statement> metadata_inserter( | 394 std::unique_ptr<Statement> metadata_inserter( |
| 373 data.is_host() ? GetHostMetadataUpdateStatement() | 395 data.is_host() ? GetHostMetadataUpdateStatement() |
| 374 : GetUrlMetadataUpdateStatement()); | 396 : GetUrlMetadataUpdateStatement()); |
| 375 metadata_inserter->BindString(0, data.primary_key); | 397 metadata_inserter->BindString(0, data.primary_key); |
| 376 metadata_inserter->BindInt64(1, data.last_visit.ToInternalValue()); | 398 metadata_inserter->BindInt64(1, data.last_visit.ToInternalValue()); |
| 377 if (!metadata_inserter->Run()) | 399 if (!metadata_inserter->Run()) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 } | 434 } |
| 413 return true; | 435 return true; |
| 414 } | 436 } |
| 415 | 437 |
| 416 bool ResourcePrefetchPredictorTables::DropTablesIfOutdated( | 438 bool ResourcePrefetchPredictorTables::DropTablesIfOutdated( |
| 417 sql::Connection* db) { | 439 sql::Connection* db) { |
| 418 bool success = true; | 440 bool success = true; |
| 419 for (const char* table_name : | 441 for (const char* table_name : |
| 420 {kUrlResourceTableName, kHostResourceTableName}) { | 442 {kUrlResourceTableName, kHostResourceTableName}) { |
| 421 if (db->DoesTableExist(table_name) && | 443 if (db->DoesTableExist(table_name) && |
| 422 !db->DoesColumnExist(table_name, "always_revalidate")) { | 444 !db->DoesColumnExist(table_name, "proto")) { |
| 423 success &= | 445 success &= |
| 424 db->Execute(base::StringPrintf("DROP TABLE %s", table_name).c_str()); | 446 db->Execute(base::StringPrintf("DROP TABLE %s", table_name).c_str()); |
| 425 } | 447 } |
| 426 } | 448 } |
| 427 return success; | 449 return success; |
| 428 } | 450 } |
| 429 | 451 |
| 430 void ResourcePrefetchPredictorTables::CreateTableIfNonExistent() { | 452 void ResourcePrefetchPredictorTables::CreateTableIfNonExistent() { |
| 431 DCHECK_CURRENTLY_ON(BrowserThread::DB); | 453 DCHECK_CURRENTLY_ON(BrowserThread::DB); |
| 432 if (CantAccessDatabase()) | 454 if (CantAccessDatabase()) |
| 433 return; | 455 return; |
| 434 | 456 |
| 435 const char resource_table_creator[] = | 457 const char resource_table_creator[] = |
| 436 "CREATE TABLE %s ( " | 458 "CREATE TABLE %s ( " |
| 437 "main_page_url TEXT, " | 459 "main_page_url TEXT, " |
| 438 "resource_url TEXT, " | 460 "resource_url TEXT, " |
| 439 "resource_type INTEGER, " | 461 "proto BLOB, " |
| 440 "number_of_hits INTEGER, " | |
| 441 "number_of_misses INTEGER, " | |
| 442 "consecutive_misses INTEGER, " | |
| 443 "average_position DOUBLE, " | |
| 444 "priority INTEGER, " | |
| 445 "has_validators INTEGER, " | |
| 446 "always_revalidate INTEGER, " | |
| 447 "PRIMARY KEY(main_page_url, resource_url))"; | 462 "PRIMARY KEY(main_page_url, resource_url))"; |
| 448 const char* metadata_table_creator = | 463 const char* metadata_table_creator = |
| 449 "CREATE TABLE %s ( " | 464 "CREATE TABLE %s ( " |
| 450 "main_page_url TEXT, " | 465 "main_page_url TEXT, " |
| 451 "last_visit_time INTEGER, " | 466 "last_visit_time INTEGER, " |
| 452 "PRIMARY KEY(main_page_url))"; | 467 "PRIMARY KEY(main_page_url))"; |
| 453 | 468 |
| 454 sql::Connection* db = DB(); | 469 sql::Connection* db = DB(); |
| 455 bool success = DropTablesIfOutdated(db) && | 470 bool success = DropTablesIfOutdated(db) && |
| 456 (db->DoesTableExist(kUrlResourceTableName) || | 471 (db->DoesTableExist(kUrlResourceTableName) || |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 kHostResourceTableName).c_str())); | 506 kHostResourceTableName).c_str())); |
| 492 if (statement.Step()) | 507 if (statement.Step()) |
| 493 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HostTableRowCount", | 508 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HostTableRowCount", |
| 494 statement.ColumnInt(0)); | 509 statement.ColumnInt(0)); |
| 495 } | 510 } |
| 496 | 511 |
| 497 Statement* | 512 Statement* |
| 498 ResourcePrefetchPredictorTables::GetUrlResourceDeleteStatement() { | 513 ResourcePrefetchPredictorTables::GetUrlResourceDeleteStatement() { |
| 499 return new Statement(DB()->GetCachedStatement( | 514 return new Statement(DB()->GetCachedStatement( |
| 500 SQL_FROM_HERE, | 515 SQL_FROM_HERE, |
| 501 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?", | 516 base::StringPrintf(kDeleteStatementTemplate, kUrlResourceTableName) |
| 502 kUrlResourceTableName).c_str())); | 517 .c_str())); |
| 503 } | 518 } |
| 504 | 519 |
| 505 Statement* | 520 Statement* |
| 506 ResourcePrefetchPredictorTables::GetUrlResourceUpdateStatement() { | 521 ResourcePrefetchPredictorTables::GetUrlResourceUpdateStatement() { |
| 507 return new Statement(DB()->GetCachedStatement( | 522 return new Statement(DB()->GetCachedStatement( |
| 508 SQL_FROM_HERE, base::StringPrintf(kInsertResourceTableStatementTemplate, | 523 SQL_FROM_HERE, base::StringPrintf(kInsertResourceTableStatementTemplate, |
| 509 kUrlResourceTableName) | 524 kUrlResourceTableName) |
| 510 .c_str())); | 525 .c_str())); |
| 511 } | 526 } |
| 512 | 527 |
| 513 Statement* | 528 Statement* |
| 514 ResourcePrefetchPredictorTables::GetUrlMetadataDeleteStatement() { | 529 ResourcePrefetchPredictorTables::GetUrlMetadataDeleteStatement() { |
| 515 return new Statement(DB()->GetCachedStatement( | 530 return new Statement(DB()->GetCachedStatement( |
| 516 SQL_FROM_HERE, | 531 SQL_FROM_HERE, |
| 517 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?", | 532 base::StringPrintf(kDeleteStatementTemplate, kUrlMetadataTableName) |
| 518 kUrlMetadataTableName).c_str())); | 533 .c_str())); |
| 519 } | 534 } |
| 520 | 535 |
| 521 Statement* | 536 Statement* |
| 522 ResourcePrefetchPredictorTables::GetUrlMetadataUpdateStatement() { | 537 ResourcePrefetchPredictorTables::GetUrlMetadataUpdateStatement() { |
| 523 return new Statement(DB()->GetCachedStatement( | 538 return new Statement(DB()->GetCachedStatement( |
| 524 SQL_FROM_HERE, | 539 SQL_FROM_HERE, base::StringPrintf(kInsertMetadataTableStatementTemplate, |
| 525 base::StringPrintf( | 540 kUrlMetadataTableName) |
| 526 "INSERT INTO %s (main_page_url, last_visit_time) VALUES (?,?)", | 541 .c_str())); |
| 527 kUrlMetadataTableName).c_str())); | |
| 528 } | 542 } |
| 529 | 543 |
| 530 Statement* | 544 Statement* |
| 531 ResourcePrefetchPredictorTables::GetHostResourceDeleteStatement() { | 545 ResourcePrefetchPredictorTables::GetHostResourceDeleteStatement() { |
| 532 return new Statement(DB()->GetCachedStatement( | 546 return new Statement(DB()->GetCachedStatement( |
| 533 SQL_FROM_HERE, | 547 SQL_FROM_HERE, |
| 534 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?", | 548 base::StringPrintf(kDeleteStatementTemplate, kHostResourceTableName) |
| 535 kHostResourceTableName).c_str())); | 549 .c_str())); |
| 536 } | 550 } |
| 537 | 551 |
| 538 Statement* | 552 Statement* |
| 539 ResourcePrefetchPredictorTables::GetHostResourceUpdateStatement() { | 553 ResourcePrefetchPredictorTables::GetHostResourceUpdateStatement() { |
| 540 return new Statement(DB()->GetCachedStatement( | 554 return new Statement(DB()->GetCachedStatement( |
| 541 SQL_FROM_HERE, base::StringPrintf(kInsertResourceTableStatementTemplate, | 555 SQL_FROM_HERE, base::StringPrintf(kInsertResourceTableStatementTemplate, |
| 542 kHostResourceTableName) | 556 kHostResourceTableName) |
| 543 .c_str())); | 557 .c_str())); |
| 544 } | 558 } |
| 545 | 559 |
| 546 Statement* | 560 Statement* |
| 547 ResourcePrefetchPredictorTables::GetHostMetadataDeleteStatement() { | 561 ResourcePrefetchPredictorTables::GetHostMetadataDeleteStatement() { |
| 548 return new Statement(DB()->GetCachedStatement( | 562 return new Statement(DB()->GetCachedStatement( |
| 549 SQL_FROM_HERE, | 563 SQL_FROM_HERE, |
| 550 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?", | 564 base::StringPrintf(kDeleteStatementTemplate, kHostMetadataTableName) |
| 551 kHostMetadataTableName).c_str())); | 565 .c_str())); |
| 552 } | 566 } |
| 553 | 567 |
| 554 Statement* ResourcePrefetchPredictorTables::GetHostMetadataUpdateStatement() { | 568 Statement* ResourcePrefetchPredictorTables::GetHostMetadataUpdateStatement() { |
| 555 return new Statement(DB()->GetCachedStatement( | 569 return new Statement(DB()->GetCachedStatement( |
| 556 SQL_FROM_HERE, | 570 SQL_FROM_HERE, base::StringPrintf(kInsertMetadataTableStatementTemplate, |
| 557 base::StringPrintf( | 571 kHostMetadataTableName) |
| 558 "INSERT INTO %s (main_page_url, last_visit_time) VALUES (?,?)", | 572 .c_str())); |
| 559 kHostMetadataTableName).c_str())); | |
| 560 } | 573 } |
| 561 | 574 |
| 562 } // namespace predictors | 575 } // namespace predictors |
| OLD | NEW |