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

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

Powered by Google App Engine
This is Rietveld 408576698