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

Side by Side Diff: chrome/browser/predictors/resource_prefetch_predictor_tables.cc

Issue 2260573002: predictors: Track whether resources have validators, and require validation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make Visual Studio happy. Created 4 years, 4 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 unified diff | Download patch
OLDNEW
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 const char kUrlResourceTableName[] = "resource_prefetch_predictor_url"; 24 const char kUrlResourceTableName[] = "resource_prefetch_predictor_url";
25 const char kUrlMetadataTableName[] = "resource_prefetch_predictor_url_metadata"; 25 const char kUrlMetadataTableName[] = "resource_prefetch_predictor_url_metadata";
26 const char kHostResourceTableName[] = "resource_prefetch_predictor_host"; 26 const char kHostResourceTableName[] = "resource_prefetch_predictor_host";
27 const char kHostMetadataTableName[] = 27 const char kHostMetadataTableName[] =
28 "resource_prefetch_predictor_host_metadata"; 28 "resource_prefetch_predictor_host_metadata";
29 29
30 const char kInsertResourceTableStatementTemplate[] =
31 "INSERT INTO %s "
32 "(main_page_url, resource_url, resource_type, number_of_hits, "
33 "number_of_misses, consecutive_misses, average_position, priority, "
34 "has_validators, always_revalidate) "
35 "VALUES (?,?,?,?,?,?,?,?,?,?)";
36
30 void BindResourceRowToStatement( 37 void BindResourceRowToStatement(
31 const predictors::ResourcePrefetchPredictorTables::ResourceRow& row, 38 const predictors::ResourcePrefetchPredictorTables::ResourceRow& row,
32 const std::string& primary_key, 39 const std::string& primary_key,
33 Statement* statement) { 40 Statement* statement) {
34 statement->BindString(0, primary_key); 41 statement->BindString(0, primary_key);
35 statement->BindString(1, row.resource_url.spec()); 42 statement->BindString(1, row.resource_url.spec());
36 statement->BindInt(2, static_cast<int>(row.resource_type)); 43 statement->BindInt(2, static_cast<int>(row.resource_type));
37 statement->BindInt(3, row.number_of_hits); 44 statement->BindInt(3, row.number_of_hits);
38 statement->BindInt(4, row.number_of_misses); 45 statement->BindInt(4, row.number_of_misses);
39 statement->BindInt(5, row.consecutive_misses); 46 statement->BindInt(5, row.consecutive_misses);
40 statement->BindDouble(6, row.average_position); 47 statement->BindDouble(6, row.average_position);
41 statement->BindInt(7, static_cast<int>(row.priority)); 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));
42 } 51 }
43 52
44 bool StepAndInitializeResourceRow( 53 bool StepAndInitializeResourceRow(
45 Statement* statement, 54 Statement* statement,
46 predictors::ResourcePrefetchPredictorTables::ResourceRow* row) { 55 predictors::ResourcePrefetchPredictorTables::ResourceRow* row) {
47 if (!statement->Step()) 56 if (!statement->Step())
48 return false; 57 return false;
49 58
50 row->primary_key = statement->ColumnString(0); 59 row->primary_key = statement->ColumnString(0);
51 row->resource_url = GURL(statement->ColumnString(1)); 60 row->resource_url = GURL(statement->ColumnString(1));
52 row->resource_type = static_cast<content::ResourceType>( 61 row->resource_type = static_cast<content::ResourceType>(
53 statement->ColumnInt(2)); 62 statement->ColumnInt(2));
54 row->number_of_hits = statement->ColumnInt(3); 63 row->number_of_hits = statement->ColumnInt(3);
55 row->number_of_misses = statement->ColumnInt(4); 64 row->number_of_misses = statement->ColumnInt(4);
56 row->consecutive_misses = statement->ColumnInt(5); 65 row->consecutive_misses = statement->ColumnInt(5);
57 row->average_position = statement->ColumnDouble(6); 66 row->average_position = statement->ColumnDouble(6);
58 row->priority = static_cast<net::RequestPriority>(statement->ColumnInt(7)); 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
59 return true; 72 return true;
60 } 73 }
61 74
62 } // namespace 75 } // namespace
63 76
64 namespace predictors { 77 namespace predictors {
65 78
66 // static 79 // static
67 const size_t ResourcePrefetchPredictorTables::kMaxStringLength = 1024; 80 const size_t ResourcePrefetchPredictorTables::kMaxStringLength = 1024;
68 81
69 ResourcePrefetchPredictorTables::ResourceRow::ResourceRow() 82 ResourcePrefetchPredictorTables::ResourceRow::ResourceRow()
70 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), 83 : resource_type(content::RESOURCE_TYPE_LAST_TYPE),
71 number_of_hits(0), 84 number_of_hits(0),
72 number_of_misses(0), 85 number_of_misses(0),
73 consecutive_misses(0), 86 consecutive_misses(0),
74 average_position(0.0), 87 average_position(0.0),
75 priority(net::IDLE), 88 priority(net::IDLE),
89 has_validators(false),
90 always_revalidate(false),
76 score(0.0) {} 91 score(0.0) {}
77 92
78 ResourcePrefetchPredictorTables::ResourceRow::ResourceRow( 93 ResourcePrefetchPredictorTables::ResourceRow::ResourceRow(
79 const ResourceRow& other) 94 const ResourceRow& other)
80 : primary_key(other.primary_key), 95 : primary_key(other.primary_key),
81 resource_url(other.resource_url), 96 resource_url(other.resource_url),
82 resource_type(other.resource_type), 97 resource_type(other.resource_type),
83 number_of_hits(other.number_of_hits), 98 number_of_hits(other.number_of_hits),
84 number_of_misses(other.number_of_misses), 99 number_of_misses(other.number_of_misses),
85 consecutive_misses(other.consecutive_misses), 100 consecutive_misses(other.consecutive_misses),
86 average_position(other.average_position), 101 average_position(other.average_position),
87 priority(other.priority), 102 priority(other.priority),
103 has_validators(other.has_validators),
104 always_revalidate(other.always_revalidate),
88 score(other.score) {} 105 score(other.score) {}
89 106
90 ResourcePrefetchPredictorTables::ResourceRow::ResourceRow( 107 ResourcePrefetchPredictorTables::ResourceRow::ResourceRow(
91 const std::string& i_primary_key, 108 const std::string& i_primary_key,
92 const std::string& i_resource_url, 109 const std::string& i_resource_url,
93 content::ResourceType i_resource_type, 110 content::ResourceType i_resource_type,
94 int i_number_of_hits, 111 int i_number_of_hits,
95 int i_number_of_misses, 112 int i_number_of_misses,
96 int i_consecutive_misses, 113 int i_consecutive_misses,
97 double i_average_position, 114 double i_average_position,
98 net::RequestPriority i_priority) 115 net::RequestPriority i_priority,
116 bool i_has_validators,
117 bool i_always_revalidate)
99 : primary_key(i_primary_key), 118 : primary_key(i_primary_key),
100 resource_url(i_resource_url), 119 resource_url(i_resource_url),
101 resource_type(i_resource_type), 120 resource_type(i_resource_type),
102 number_of_hits(i_number_of_hits), 121 number_of_hits(i_number_of_hits),
103 number_of_misses(i_number_of_misses), 122 number_of_misses(i_number_of_misses),
104 consecutive_misses(i_consecutive_misses), 123 consecutive_misses(i_consecutive_misses),
105 average_position(i_average_position), 124 average_position(i_average_position),
106 priority(i_priority) { 125 priority(i_priority),
126 has_validators(i_has_validators),
127 always_revalidate(i_always_revalidate) {
107 UpdateScore(); 128 UpdateScore();
108 } 129 }
109 130
110 void ResourcePrefetchPredictorTables::ResourceRow::UpdateScore() { 131 void ResourcePrefetchPredictorTables::ResourceRow::UpdateScore() {
111 // The score is calculated so that when the rows are sorted, stylesheets, 132 // The score is calculated so that when the rows are sorted, stylesheets,
112 // scripts and fonts appear first, sorted by position(ascending) and then the 133 // scripts and fonts appear first, sorted by position(ascending) and then the
113 // rest of the resources sorted by position (ascending). 134 // rest of the resources sorted by position (ascending).
114 static const int kMaxResourcesPerType = 100; 135 static const int kMaxResourcesPerType = 100;
115 switch (resource_type) { 136 switch (resource_type) {
116 case content::RESOURCE_TYPE_STYLESHEET: 137 case content::RESOURCE_TYPE_STYLESHEET:
(...skipping 11 matching lines...) Expand all
128 } 149 }
129 150
130 bool ResourcePrefetchPredictorTables::ResourceRow::operator==( 151 bool ResourcePrefetchPredictorTables::ResourceRow::operator==(
131 const ResourceRow& rhs) const { 152 const ResourceRow& rhs) const {
132 return primary_key == rhs.primary_key && resource_url == rhs.resource_url && 153 return primary_key == rhs.primary_key && resource_url == rhs.resource_url &&
133 resource_type == rhs.resource_type && 154 resource_type == rhs.resource_type &&
134 number_of_hits == rhs.number_of_hits && 155 number_of_hits == rhs.number_of_hits &&
135 number_of_misses == rhs.number_of_misses && 156 number_of_misses == rhs.number_of_misses &&
136 consecutive_misses == rhs.consecutive_misses && 157 consecutive_misses == rhs.consecutive_misses &&
137 average_position == rhs.average_position && priority == rhs.priority && 158 average_position == rhs.average_position && priority == rhs.priority &&
138 score == rhs.score; 159 has_validators == rhs.has_validators &&
160 always_revalidate == rhs.always_revalidate && score == rhs.score;
139 } 161 }
140 162
141 bool ResourcePrefetchPredictorTables::ResourceRowSorter::operator()( 163 bool ResourcePrefetchPredictorTables::ResourceRowSorter::operator()(
142 const ResourceRow& x, const ResourceRow& y) const { 164 const ResourceRow& x, const ResourceRow& y) const {
143 return x.score > y.score; 165 return x.score > y.score;
144 } 166 }
145 167
146 ResourcePrefetchPredictorTables::PrefetchData::PrefetchData( 168 ResourcePrefetchPredictorTables::PrefetchData::PrefetchData(
147 PrefetchKeyType i_key_type, 169 PrefetchKeyType i_key_type,
148 const std::string& i_primary_key) 170 const std::string& i_primary_key)
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 } 412 }
391 return true; 413 return true;
392 } 414 }
393 415
394 bool ResourcePrefetchPredictorTables::DropTablesIfOutdated( 416 bool ResourcePrefetchPredictorTables::DropTablesIfOutdated(
395 sql::Connection* db) { 417 sql::Connection* db) {
396 bool success = true; 418 bool success = true;
397 for (const char* table_name : 419 for (const char* table_name :
398 {kUrlResourceTableName, kHostResourceTableName}) { 420 {kUrlResourceTableName, kHostResourceTableName}) {
399 if (db->DoesTableExist(table_name) && 421 if (db->DoesTableExist(table_name) &&
400 !db->DoesColumnExist(table_name, "priority")) { 422 !db->DoesColumnExist(table_name, "always_revalidate")) {
401 success &= 423 success &=
402 db->Execute(base::StringPrintf("DROP TABLE %s", table_name).c_str()); 424 db->Execute(base::StringPrintf("DROP TABLE %s", table_name).c_str());
403 } 425 }
404 } 426 }
405 return success; 427 return success;
406 } 428 }
407 429
408 void ResourcePrefetchPredictorTables::CreateTableIfNonExistent() { 430 void ResourcePrefetchPredictorTables::CreateTableIfNonExistent() {
409 DCHECK_CURRENTLY_ON(BrowserThread::DB); 431 DCHECK_CURRENTLY_ON(BrowserThread::DB);
410 if (CantAccessDatabase()) 432 if (CantAccessDatabase())
411 return; 433 return;
412 434
413 const char resource_table_creator[] = 435 const char resource_table_creator[] =
414 "CREATE TABLE %s ( " 436 "CREATE TABLE %s ( "
415 "main_page_url TEXT, " 437 "main_page_url TEXT, "
416 "resource_url TEXT, " 438 "resource_url TEXT, "
417 "resource_type INTEGER, " 439 "resource_type INTEGER, "
418 "number_of_hits INTEGER, " 440 "number_of_hits INTEGER, "
419 "number_of_misses INTEGER, " 441 "number_of_misses INTEGER, "
420 "consecutive_misses INTEGER, " 442 "consecutive_misses INTEGER, "
421 "average_position DOUBLE, " 443 "average_position DOUBLE, "
422 "priority INTEGER, " 444 "priority INTEGER, "
445 "has_validators INTEGER, "
446 "always_revalidate INTEGER, "
423 "PRIMARY KEY(main_page_url, resource_url))"; 447 "PRIMARY KEY(main_page_url, resource_url))";
424 const char* metadata_table_creator = 448 const char* metadata_table_creator =
425 "CREATE TABLE %s ( " 449 "CREATE TABLE %s ( "
426 "main_page_url TEXT, " 450 "main_page_url TEXT, "
427 "last_visit_time INTEGER, " 451 "last_visit_time INTEGER, "
428 "PRIMARY KEY(main_page_url))"; 452 "PRIMARY KEY(main_page_url))";
429 453
430 sql::Connection* db = DB(); 454 sql::Connection* db = DB();
431 bool success = DropTablesIfOutdated(db) && 455 bool success = DropTablesIfOutdated(db) &&
432 (db->DoesTableExist(kUrlResourceTableName) || 456 (db->DoesTableExist(kUrlResourceTableName) ||
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 ResourcePrefetchPredictorTables::GetUrlResourceDeleteStatement() { 498 ResourcePrefetchPredictorTables::GetUrlResourceDeleteStatement() {
475 return new Statement(DB()->GetCachedStatement( 499 return new Statement(DB()->GetCachedStatement(
476 SQL_FROM_HERE, 500 SQL_FROM_HERE,
477 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?", 501 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?",
478 kUrlResourceTableName).c_str())); 502 kUrlResourceTableName).c_str()));
479 } 503 }
480 504
481 Statement* 505 Statement*
482 ResourcePrefetchPredictorTables::GetUrlResourceUpdateStatement() { 506 ResourcePrefetchPredictorTables::GetUrlResourceUpdateStatement() {
483 return new Statement(DB()->GetCachedStatement( 507 return new Statement(DB()->GetCachedStatement(
484 SQL_FROM_HERE, 508 SQL_FROM_HERE, base::StringPrintf(kInsertResourceTableStatementTemplate,
485 base::StringPrintf( 509 kUrlResourceTableName)
486 "INSERT INTO %s " 510 .c_str()));
487 "(main_page_url, resource_url, resource_type, number_of_hits, "
488 "number_of_misses, consecutive_misses, average_position, priority) "
489 "VALUES (?,?,?,?,?,?,?,?)",
490 kUrlResourceTableName)
491 .c_str()));
492 } 511 }
493 512
494 Statement* 513 Statement*
495 ResourcePrefetchPredictorTables::GetUrlMetadataDeleteStatement() { 514 ResourcePrefetchPredictorTables::GetUrlMetadataDeleteStatement() {
496 return new Statement(DB()->GetCachedStatement( 515 return new Statement(DB()->GetCachedStatement(
497 SQL_FROM_HERE, 516 SQL_FROM_HERE,
498 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?", 517 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?",
499 kUrlMetadataTableName).c_str())); 518 kUrlMetadataTableName).c_str()));
500 } 519 }
501 520
(...skipping 10 matching lines...) Expand all
512 ResourcePrefetchPredictorTables::GetHostResourceDeleteStatement() { 531 ResourcePrefetchPredictorTables::GetHostResourceDeleteStatement() {
513 return new Statement(DB()->GetCachedStatement( 532 return new Statement(DB()->GetCachedStatement(
514 SQL_FROM_HERE, 533 SQL_FROM_HERE,
515 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?", 534 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?",
516 kHostResourceTableName).c_str())); 535 kHostResourceTableName).c_str()));
517 } 536 }
518 537
519 Statement* 538 Statement*
520 ResourcePrefetchPredictorTables::GetHostResourceUpdateStatement() { 539 ResourcePrefetchPredictorTables::GetHostResourceUpdateStatement() {
521 return new Statement(DB()->GetCachedStatement( 540 return new Statement(DB()->GetCachedStatement(
522 SQL_FROM_HERE, 541 SQL_FROM_HERE, base::StringPrintf(kInsertResourceTableStatementTemplate,
523 base::StringPrintf( 542 kHostResourceTableName)
524 "INSERT INTO %s " 543 .c_str()));
525 "(main_page_url, resource_url, resource_type, number_of_hits, "
526 "number_of_misses, consecutive_misses, average_position, priority) "
527 "VALUES (?,?,?,?,?,?,?,?)",
528 kHostResourceTableName)
529 .c_str()));
530 } 544 }
531 545
532 Statement* 546 Statement*
533 ResourcePrefetchPredictorTables::GetHostMetadataDeleteStatement() { 547 ResourcePrefetchPredictorTables::GetHostMetadataDeleteStatement() {
534 return new Statement(DB()->GetCachedStatement( 548 return new Statement(DB()->GetCachedStatement(
535 SQL_FROM_HERE, 549 SQL_FROM_HERE,
536 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?", 550 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?",
537 kHostMetadataTableName).c_str())); 551 kHostMetadataTableName).c_str()));
538 } 552 }
539 553
540 Statement* ResourcePrefetchPredictorTables::GetHostMetadataUpdateStatement() { 554 Statement* ResourcePrefetchPredictorTables::GetHostMetadataUpdateStatement() {
541 return new Statement(DB()->GetCachedStatement( 555 return new Statement(DB()->GetCachedStatement(
542 SQL_FROM_HERE, 556 SQL_FROM_HERE,
543 base::StringPrintf( 557 base::StringPrintf(
544 "INSERT INTO %s (main_page_url, last_visit_time) VALUES (?,?)", 558 "INSERT INTO %s (main_page_url, last_visit_time) VALUES (?,?)",
545 kHostMetadataTableName).c_str())); 559 kHostMetadataTableName).c_str()));
546 } 560 }
547 561
548 } // namespace predictors 562 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698