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

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

Issue 2195503003: predictors: Add the request priority to the reource_prefetch_predictor DB. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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>
(...skipping 20 matching lines...) Expand all
31 const predictors::ResourcePrefetchPredictorTables::ResourceRow& row, 31 const predictors::ResourcePrefetchPredictorTables::ResourceRow& row,
32 const std::string& primary_key, 32 const std::string& primary_key,
33 Statement* statement) { 33 Statement* statement) {
34 statement->BindString(0, primary_key); 34 statement->BindString(0, primary_key);
35 statement->BindString(1, row.resource_url.spec()); 35 statement->BindString(1, row.resource_url.spec());
36 statement->BindInt(2, static_cast<int>(row.resource_type)); 36 statement->BindInt(2, static_cast<int>(row.resource_type));
37 statement->BindInt(3, row.number_of_hits); 37 statement->BindInt(3, row.number_of_hits);
38 statement->BindInt(4, row.number_of_misses); 38 statement->BindInt(4, row.number_of_misses);
39 statement->BindInt(5, row.consecutive_misses); 39 statement->BindInt(5, row.consecutive_misses);
40 statement->BindDouble(6, row.average_position); 40 statement->BindDouble(6, row.average_position);
41 statement->BindInt(7, static_cast<int>(row.priority));
41 } 42 }
42 43
43 bool StepAndInitializeResourceRow( 44 bool StepAndInitializeResourceRow(
44 Statement* statement, 45 Statement* statement,
45 predictors::ResourcePrefetchPredictorTables::ResourceRow* row) { 46 predictors::ResourcePrefetchPredictorTables::ResourceRow* row) {
46 if (!statement->Step()) 47 if (!statement->Step())
47 return false; 48 return false;
48 49
49 row->primary_key = statement->ColumnString(0); 50 row->primary_key = statement->ColumnString(0);
50 row->resource_url = GURL(statement->ColumnString(1)); 51 row->resource_url = GURL(statement->ColumnString(1));
51 row->resource_type = static_cast<content::ResourceType>( 52 row->resource_type = static_cast<content::ResourceType>(
52 statement->ColumnInt(2)); 53 statement->ColumnInt(2));
53 row->number_of_hits = statement->ColumnInt(3); 54 row->number_of_hits = statement->ColumnInt(3);
54 row->number_of_misses = statement->ColumnInt(4); 55 row->number_of_misses = statement->ColumnInt(4);
55 row->consecutive_misses = statement->ColumnInt(5); 56 row->consecutive_misses = statement->ColumnInt(5);
56 row->average_position = statement->ColumnDouble(6); 57 row->average_position = statement->ColumnDouble(6);
58 row->priority = static_cast<net::RequestPriority>(statement->ColumnInt(7));
57 return true; 59 return true;
58 } 60 }
59 61
60 } // namespace 62 } // namespace
61 63
62 namespace predictors { 64 namespace predictors {
63 65
64 // static 66 // static
65 const size_t ResourcePrefetchPredictorTables::kMaxStringLength = 1024; 67 const size_t ResourcePrefetchPredictorTables::kMaxStringLength = 1024;
66 68
67 ResourcePrefetchPredictorTables::ResourceRow::ResourceRow() 69 ResourcePrefetchPredictorTables::ResourceRow::ResourceRow()
68 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), 70 : resource_type(content::RESOURCE_TYPE_LAST_TYPE),
69 number_of_hits(0), 71 number_of_hits(0),
70 number_of_misses(0), 72 number_of_misses(0),
71 consecutive_misses(0), 73 consecutive_misses(0),
72 average_position(0.0), 74 average_position(0.0),
73 score(0.0) { 75 priority(net::IDLE),
74 } 76 score(0.0) {}
75 77
76 ResourcePrefetchPredictorTables::ResourceRow::ResourceRow( 78 ResourcePrefetchPredictorTables::ResourceRow::ResourceRow(
77 const ResourceRow& other) 79 const ResourceRow& other)
78 : primary_key(other.primary_key), 80 : primary_key(other.primary_key),
79 resource_url(other.resource_url), 81 resource_url(other.resource_url),
80 resource_type(other.resource_type), 82 resource_type(other.resource_type),
81 number_of_hits(other.number_of_hits), 83 number_of_hits(other.number_of_hits),
82 number_of_misses(other.number_of_misses), 84 number_of_misses(other.number_of_misses),
83 consecutive_misses(other.consecutive_misses), 85 consecutive_misses(other.consecutive_misses),
84 average_position(other.average_position), 86 average_position(other.average_position),
85 score(other.score) { 87 priority(other.priority),
86 } 88 score(other.score) {}
87 89
88 ResourcePrefetchPredictorTables::ResourceRow::ResourceRow( 90 ResourcePrefetchPredictorTables::ResourceRow::ResourceRow(
89 const std::string& i_primary_key, 91 const std::string& i_primary_key,
90 const std::string& i_resource_url, 92 const std::string& i_resource_url,
91 content::ResourceType i_resource_type, 93 content::ResourceType i_resource_type,
92 int i_number_of_hits, 94 int i_number_of_hits,
93 int i_number_of_misses, 95 int i_number_of_misses,
94 int i_consecutive_misses, 96 int i_consecutive_misses,
95 double i_average_position) 97 double i_average_position,
96 : primary_key(i_primary_key), 98 net::RequestPriority i_priority)
97 resource_url(i_resource_url), 99 : primary_key(i_primary_key),
98 resource_type(i_resource_type), 100 resource_url(i_resource_url),
99 number_of_hits(i_number_of_hits), 101 resource_type(i_resource_type),
100 number_of_misses(i_number_of_misses), 102 number_of_hits(i_number_of_hits),
101 consecutive_misses(i_consecutive_misses), 103 number_of_misses(i_number_of_misses),
102 average_position(i_average_position) { 104 consecutive_misses(i_consecutive_misses),
105 average_position(i_average_position),
106 priority(i_priority) {
103 UpdateScore(); 107 UpdateScore();
104 } 108 }
105 109
106 void ResourcePrefetchPredictorTables::ResourceRow::UpdateScore() { 110 void ResourcePrefetchPredictorTables::ResourceRow::UpdateScore() {
107 // The score is calculated so that when the rows are sorted, the stylesheets 111 // The score is calculated so that when the rows are sorted, the stylesheets
108 // and scripts appear first, sorted by position(ascending) and then the rest 112 // and scripts appear first, sorted by position(ascending) and then the rest
109 // of the resources sorted by position(ascending). 113 // of the resources sorted by position(ascending).
110 static const int kMaxResourcesPerType = 100; 114 static const int kMaxResourcesPerType = 100;
111 switch (resource_type) { 115 switch (resource_type) {
112 case content::RESOURCE_TYPE_STYLESHEET: 116 case content::RESOURCE_TYPE_STYLESHEET:
113 case content::RESOURCE_TYPE_SCRIPT: 117 case content::RESOURCE_TYPE_SCRIPT:
114 score = (2 * kMaxResourcesPerType) - average_position; 118 score = (2 * kMaxResourcesPerType) - average_position;
115 break; 119 break;
116 120
117 case content::RESOURCE_TYPE_IMAGE: 121 case content::RESOURCE_TYPE_IMAGE:
118 default: 122 default:
119 score = kMaxResourcesPerType - average_position; 123 score = kMaxResourcesPerType - average_position;
120 break; 124 break;
121 } 125 }
126 // TODO(lizeb): Take priority into account.
pasko 2016/07/29 17:13:32 is it obvious that this should be done? :) I think
122 } 127 }
123 128
124 bool ResourcePrefetchPredictorTables::ResourceRow::operator==( 129 bool ResourcePrefetchPredictorTables::ResourceRow::operator==(
125 const ResourceRow& rhs) const { 130 const ResourceRow& rhs) const {
126 return primary_key == rhs.primary_key && 131 return primary_key == rhs.primary_key && resource_url == rhs.resource_url &&
127 resource_url == rhs.resource_url && 132 resource_type == rhs.resource_type &&
128 resource_type == rhs.resource_type && 133 number_of_hits == rhs.number_of_hits &&
129 number_of_hits == rhs.number_of_hits && 134 number_of_misses == rhs.number_of_misses &&
130 number_of_misses == rhs.number_of_misses && 135 consecutive_misses == rhs.consecutive_misses &&
131 consecutive_misses == rhs.consecutive_misses && 136 average_position == rhs.average_position && priority == rhs.priority &&
132 average_position == rhs.average_position && 137 score == rhs.score;
133 score == rhs.score;
134 } 138 }
135 139
136 bool ResourcePrefetchPredictorTables::ResourceRowSorter::operator()( 140 bool ResourcePrefetchPredictorTables::ResourceRowSorter::operator()(
137 const ResourceRow& x, const ResourceRow& y) const { 141 const ResourceRow& x, const ResourceRow& y) const {
138 return x.score > y.score; 142 return x.score > y.score;
139 } 143 }
140 144
141 ResourcePrefetchPredictorTables::PrefetchData::PrefetchData( 145 ResourcePrefetchPredictorTables::PrefetchData::PrefetchData(
142 PrefetchKeyType i_key_type, 146 PrefetchKeyType i_key_type,
143 const std::string& i_primary_key) 147 const std::string& i_primary_key)
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 return false; 383 return false;
380 384
381 for (ResourceRows::const_iterator it = data.resources.begin(); 385 for (ResourceRows::const_iterator it = data.resources.begin();
382 it != data.resources.end(); ++it) { 386 it != data.resources.end(); ++it) {
383 if (it->resource_url.spec().length() > kMaxStringLength) 387 if (it->resource_url.spec().length() > kMaxStringLength)
384 return false; 388 return false;
385 } 389 }
386 return true; 390 return true;
387 } 391 }
388 392
393 bool ResourcePrefetchPredictorTables::DropTablesIfOutdated(
394 sql::Connection* db) {
395 bool success = true;
396 for (const char* table_name :
397 {kUrlResourceTableName, kHostResourceTableName}) {
398 if (db->DoesTableExist(table_name) &&
399 !db->DoesColumnExist(table_name, "priority")) {
400 success &=
401 db->Execute(base::StringPrintf("DROP TABLE %s", table_name).c_str());
402 }
403 }
404 return success;
405 }
406
389 void ResourcePrefetchPredictorTables::CreateTableIfNonExistent() { 407 void ResourcePrefetchPredictorTables::CreateTableIfNonExistent() {
390 DCHECK_CURRENTLY_ON(BrowserThread::DB); 408 DCHECK_CURRENTLY_ON(BrowserThread::DB);
391 if (CantAccessDatabase()) 409 if (CantAccessDatabase())
392 return; 410 return;
393 411
394 const char resource_table_creator[] = 412 const char resource_table_creator[] =
395 "CREATE TABLE %s ( " 413 "CREATE TABLE %s ( "
396 "main_page_url TEXT, " 414 "main_page_url TEXT, "
397 "resource_url TEXT, " 415 "resource_url TEXT, "
398 "resource_type INTEGER, " 416 "resource_type INTEGER, "
399 "number_of_hits INTEGER, " 417 "number_of_hits INTEGER, "
400 "number_of_misses INTEGER, " 418 "number_of_misses INTEGER, "
401 "consecutive_misses INTEGER, " 419 "consecutive_misses INTEGER, "
402 "average_position DOUBLE, " 420 "average_position DOUBLE, "
421 "priority INTEGER, "
403 "PRIMARY KEY(main_page_url, resource_url))"; 422 "PRIMARY KEY(main_page_url, resource_url))";
404 const char* metadata_table_creator = 423 const char* metadata_table_creator =
405 "CREATE TABLE %s ( " 424 "CREATE TABLE %s ( "
406 "main_page_url TEXT, " 425 "main_page_url TEXT, "
407 "last_visit_time INTEGER, " 426 "last_visit_time INTEGER, "
408 "PRIMARY KEY(main_page_url))"; 427 "PRIMARY KEY(main_page_url))";
409 428
410 sql::Connection* db = DB(); 429 sql::Connection* db = DB();
411 bool success = 430 bool success = DropTablesIfOutdated(db) &&
412 (db->DoesTableExist(kUrlResourceTableName) || 431 (db->DoesTableExist(kUrlResourceTableName) ||
413 db->Execute(base::StringPrintf(resource_table_creator, 432 db->Execute(base::StringPrintf(resource_table_creator,
414 kUrlResourceTableName).c_str())) && 433 kUrlResourceTableName)
415 (db->DoesTableExist(kUrlMetadataTableName) || 434 .c_str())) &&
416 db->Execute(base::StringPrintf(metadata_table_creator, 435 (db->DoesTableExist(kUrlMetadataTableName) ||
417 kUrlMetadataTableName).c_str())) && 436 db->Execute(base::StringPrintf(metadata_table_creator,
418 (db->DoesTableExist(kHostResourceTableName) || 437 kUrlMetadataTableName)
419 db->Execute(base::StringPrintf(resource_table_creator, 438 .c_str())) &&
420 kHostResourceTableName).c_str())) && 439 (db->DoesTableExist(kHostResourceTableName) ||
421 (db->DoesTableExist(kHostMetadataTableName) || 440 db->Execute(base::StringPrintf(resource_table_creator,
422 db->Execute(base::StringPrintf(metadata_table_creator, 441 kHostResourceTableName)
423 kHostMetadataTableName).c_str())); 442 .c_str())) &&
443 (db->DoesTableExist(kHostMetadataTableName) ||
444 db->Execute(base::StringPrintf(metadata_table_creator,
445 kHostMetadataTableName)
446 .c_str()));
424 447
425 if (!success) 448 if (!success)
426 ResetDB(); 449 ResetDB();
427 } 450 }
428 451
429 void ResourcePrefetchPredictorTables::LogDatabaseStats() { 452 void ResourcePrefetchPredictorTables::LogDatabaseStats() {
430 DCHECK_CURRENTLY_ON(BrowserThread::DB); 453 DCHECK_CURRENTLY_ON(BrowserThread::DB);
431 if (CantAccessDatabase()) 454 if (CantAccessDatabase())
432 return; 455 return;
433 456
(...skipping 20 matching lines...) Expand all
454 kUrlResourceTableName).c_str())); 477 kUrlResourceTableName).c_str()));
455 } 478 }
456 479
457 Statement* 480 Statement*
458 ResourcePrefetchPredictorTables::GetUrlResourceUpdateStatement() { 481 ResourcePrefetchPredictorTables::GetUrlResourceUpdateStatement() {
459 return new Statement(DB()->GetCachedStatement( 482 return new Statement(DB()->GetCachedStatement(
460 SQL_FROM_HERE, 483 SQL_FROM_HERE,
461 base::StringPrintf( 484 base::StringPrintf(
462 "INSERT INTO %s " 485 "INSERT INTO %s "
463 "(main_page_url, resource_url, resource_type, number_of_hits, " 486 "(main_page_url, resource_url, resource_type, number_of_hits, "
464 "number_of_misses, consecutive_misses, average_position) " 487 "number_of_misses, consecutive_misses, average_position, priority) "
465 "VALUES (?,?,?,?,?,?,?)", kUrlResourceTableName).c_str())); 488 "VALUES (?,?,?,?,?,?,?,?)",
489 kUrlResourceTableName)
490 .c_str()));
466 } 491 }
467 492
468 Statement* 493 Statement*
469 ResourcePrefetchPredictorTables::GetUrlMetadataDeleteStatement() { 494 ResourcePrefetchPredictorTables::GetUrlMetadataDeleteStatement() {
470 return new Statement(DB()->GetCachedStatement( 495 return new Statement(DB()->GetCachedStatement(
471 SQL_FROM_HERE, 496 SQL_FROM_HERE,
472 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?", 497 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?",
473 kUrlMetadataTableName).c_str())); 498 kUrlMetadataTableName).c_str()));
474 } 499 }
475 500
(...skipping 14 matching lines...) Expand all
490 kHostResourceTableName).c_str())); 515 kHostResourceTableName).c_str()));
491 } 516 }
492 517
493 Statement* 518 Statement*
494 ResourcePrefetchPredictorTables::GetHostResourceUpdateStatement() { 519 ResourcePrefetchPredictorTables::GetHostResourceUpdateStatement() {
495 return new Statement(DB()->GetCachedStatement( 520 return new Statement(DB()->GetCachedStatement(
496 SQL_FROM_HERE, 521 SQL_FROM_HERE,
497 base::StringPrintf( 522 base::StringPrintf(
498 "INSERT INTO %s " 523 "INSERT INTO %s "
499 "(main_page_url, resource_url, resource_type, number_of_hits, " 524 "(main_page_url, resource_url, resource_type, number_of_hits, "
500 "number_of_misses, consecutive_misses, average_position) " 525 "number_of_misses, consecutive_misses, average_position, priority) "
501 "VALUES (?,?,?,?,?,?,?)", kHostResourceTableName).c_str())); 526 "VALUES (?,?,?,?,?,?,?,?)",
527 kHostResourceTableName)
528 .c_str()));
502 } 529 }
503 530
504 Statement* 531 Statement*
505 ResourcePrefetchPredictorTables::GetHostMetadataDeleteStatement() { 532 ResourcePrefetchPredictorTables::GetHostMetadataDeleteStatement() {
506 return new Statement(DB()->GetCachedStatement( 533 return new Statement(DB()->GetCachedStatement(
507 SQL_FROM_HERE, 534 SQL_FROM_HERE,
508 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?", 535 base::StringPrintf("DELETE FROM %s WHERE main_page_url=?",
509 kHostMetadataTableName).c_str())); 536 kHostMetadataTableName).c_str()));
510 } 537 }
511 538
512 Statement* ResourcePrefetchPredictorTables::GetHostMetadataUpdateStatement() { 539 Statement* ResourcePrefetchPredictorTables::GetHostMetadataUpdateStatement() {
513 return new Statement(DB()->GetCachedStatement( 540 return new Statement(DB()->GetCachedStatement(
514 SQL_FROM_HERE, 541 SQL_FROM_HERE,
515 base::StringPrintf( 542 base::StringPrintf(
516 "INSERT INTO %s (main_page_url, last_visit_time) VALUES (?,?)", 543 "INSERT INTO %s (main_page_url, last_visit_time) VALUES (?,?)",
517 kHostMetadataTableName).c_str())); 544 kHostMetadataTableName).c_str()));
518 } 545 }
519 546
520 } // namespace predictors 547 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698