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

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

Issue 2287473002: predictors: Remove unused field from the resource_prefetch_predictor database. (Closed)
Patch Set: Created 4 years, 3 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 "PRIMARY KEY(main_page_url))"; 49 "PRIMARY KEY(main_page_url))";
50 50
51 const char kInsertResourceTableStatementTemplate[] = 51 const char kInsertResourceTableStatementTemplate[] =
52 "INSERT INTO %s (main_page_url, resource_url, proto) VALUES (?,?,?)"; 52 "INSERT INTO %s (main_page_url, resource_url, proto) VALUES (?,?,?)";
53 const char kInsertMetadataTableStatementTemplate[] = 53 const char kInsertMetadataTableStatementTemplate[] =
54 "INSERT INTO %s (main_page_url, last_visit_time) VALUES (?,?)"; 54 "INSERT INTO %s (main_page_url, last_visit_time) VALUES (?,?)";
55 const char kDeleteStatementTemplate[] = "DELETE FROM %s WHERE main_page_url=?"; 55 const char kDeleteStatementTemplate[] = "DELETE FROM %s WHERE main_page_url=?";
56 56
57 // Database version. Always increment it when any change is made to the data 57 // Database version. Always increment it when any change is made to the data
58 // schema (including the .proto). 58 // schema (including the .proto).
59 const int kDatabaseVersion = 1; 59 const int kDatabaseVersion = 2;
60 60
61 void BindResourceRowToStatement(const ResourceRow& row, 61 void BindResourceRowToStatement(const ResourceRow& row,
62 const std::string& primary_key, 62 const std::string& primary_key,
63 Statement* statement) { 63 Statement* statement) {
64 chrome_browser_predictors::ResourceData proto; 64 chrome_browser_predictors::ResourceData proto;
65 row.ToProto(&proto); 65 row.ToProto(&proto);
66 int size = proto.ByteSize(); 66 int size = proto.ByteSize();
67 std::vector<char> proto_buffer(size); 67 std::vector<char> proto_buffer(size);
68 proto.SerializeToArray(&proto_buffer[0], size); 68 proto.SerializeToArray(&proto_buffer[0], size);
69 69
70 statement->BindString(0, primary_key); 70 statement->BindString(0, primary_key);
71 statement->BindString(1, row.resource_url.spec()); 71 statement->BindString(1, row.resource_url.spec());
72 statement->BindBlob(2, &proto_buffer[0], size); 72 statement->BindBlob(2, &proto_buffer[0], size);
73 } 73 }
74 74
75 bool StepAndInitializeResourceRow(Statement* statement, ResourceRow* row) { 75 bool StepAndInitializeResourceRow(Statement* statement,
76 ResourceRow* row,
77 std::string* primary_key) {
76 if (!statement->Step()) 78 if (!statement->Step())
77 return false; 79 return false;
78 80
81 *primary_key = statement->ColumnString(0);
82
79 int size = statement->ColumnByteLength(2); 83 int size = statement->ColumnByteLength(2);
80 const void* data = statement->ColumnBlob(2); 84 const void* data = statement->ColumnBlob(2);
81 DCHECK(data); 85 DCHECK(data);
82 chrome_browser_predictors::ResourceData proto; 86 chrome_browser_predictors::ResourceData proto;
83 proto.ParseFromArray(data, size); 87 proto.ParseFromArray(data, size);
84 ResourceRow::FromProto(proto, row); 88 ResourceRow::FromProto(proto, row);
85 89
86 row->primary_key = statement->ColumnString(0); 90 GURL resource_url(statement->ColumnString(1));
87 row->resource_url = GURL(statement->ColumnString(1)); 91 DCHECK(resource_url == row->resource_url);
92
88 return true; 93 return true;
89 } 94 }
90 95
91 } // namespace 96 } // namespace
92 97
93 namespace predictors { 98 namespace predictors {
94 99
95 // static
96 const size_t ResourcePrefetchPredictorTables::kMaxStringLength = 1024;
97
98 ResourceRow::ResourceRow() 100 ResourceRow::ResourceRow()
99 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), 101 : resource_type(content::RESOURCE_TYPE_LAST_TYPE),
100 number_of_hits(0), 102 number_of_hits(0),
101 number_of_misses(0), 103 number_of_misses(0),
102 consecutive_misses(0), 104 consecutive_misses(0),
103 average_position(0.0), 105 average_position(0.0),
104 priority(net::IDLE), 106 priority(net::IDLE),
105 has_validators(false), 107 has_validators(false),
106 always_revalidate(false), 108 always_revalidate(false),
107 score(0.0) {} 109 score(0.0) {}
108 110
109 ResourceRow::ResourceRow(const ResourceRow& other) 111 ResourceRow::ResourceRow(const ResourceRow& other)
110 : primary_key(other.primary_key), 112 : resource_url(other.resource_url),
111 resource_url(other.resource_url),
112 resource_type(other.resource_type), 113 resource_type(other.resource_type),
113 number_of_hits(other.number_of_hits), 114 number_of_hits(other.number_of_hits),
114 number_of_misses(other.number_of_misses), 115 number_of_misses(other.number_of_misses),
115 consecutive_misses(other.consecutive_misses), 116 consecutive_misses(other.consecutive_misses),
116 average_position(other.average_position), 117 average_position(other.average_position),
117 priority(other.priority), 118 priority(other.priority),
118 has_validators(other.has_validators), 119 has_validators(other.has_validators),
119 always_revalidate(other.always_revalidate), 120 always_revalidate(other.always_revalidate),
120 score(other.score) {} 121 score(other.score) {}
121 122
122 ResourceRow::ResourceRow(const std::string& i_primary_key, 123 ResourceRow::ResourceRow(const std::string& i_resource_url,
123 const std::string& i_resource_url,
124 content::ResourceType i_resource_type, 124 content::ResourceType i_resource_type,
125 int i_number_of_hits, 125 int i_number_of_hits,
126 int i_number_of_misses, 126 int i_number_of_misses,
127 int i_consecutive_misses, 127 int i_consecutive_misses,
128 double i_average_position, 128 double i_average_position,
129 net::RequestPriority i_priority, 129 net::RequestPriority i_priority,
130 bool i_has_validators, 130 bool i_has_validators,
131 bool i_always_revalidate) 131 bool i_always_revalidate)
132 : primary_key(i_primary_key), 132 : resource_url(i_resource_url),
133 resource_url(i_resource_url),
134 resource_type(i_resource_type), 133 resource_type(i_resource_type),
135 number_of_hits(i_number_of_hits), 134 number_of_hits(i_number_of_hits),
136 number_of_misses(i_number_of_misses), 135 number_of_misses(i_number_of_misses),
137 consecutive_misses(i_consecutive_misses), 136 consecutive_misses(i_consecutive_misses),
138 average_position(i_average_position), 137 average_position(i_average_position),
139 priority(i_priority), 138 priority(i_priority),
140 has_validators(i_has_validators), 139 has_validators(i_has_validators),
141 always_revalidate(i_always_revalidate) { 140 always_revalidate(i_always_revalidate) {
142 UpdateScore(); 141 UpdateScore();
143 } 142 }
(...skipping 12 matching lines...) Expand all
156 155
157 case content::RESOURCE_TYPE_IMAGE: 156 case content::RESOURCE_TYPE_IMAGE:
158 default: 157 default:
159 score = kMaxResourcesPerType - average_position; 158 score = kMaxResourcesPerType - average_position;
160 break; 159 break;
161 } 160 }
162 // TODO(lizeb): Take priority into account. 161 // TODO(lizeb): Take priority into account.
163 } 162 }
164 163
165 bool ResourceRow::operator==(const ResourceRow& rhs) const { 164 bool ResourceRow::operator==(const ResourceRow& rhs) const {
166 return primary_key == rhs.primary_key && resource_url == rhs.resource_url && 165 return resource_url == rhs.resource_url &&
167 resource_type == rhs.resource_type && 166 resource_type == rhs.resource_type &&
168 number_of_hits == rhs.number_of_hits && 167 number_of_hits == rhs.number_of_hits &&
169 number_of_misses == rhs.number_of_misses && 168 number_of_misses == rhs.number_of_misses &&
170 consecutive_misses == rhs.consecutive_misses && 169 consecutive_misses == rhs.consecutive_misses &&
171 average_position == rhs.average_position && priority == rhs.priority && 170 average_position == rhs.average_position && priority == rhs.priority &&
172 has_validators == rhs.has_validators && 171 has_validators == rhs.has_validators &&
173 always_revalidate == rhs.always_revalidate && score == rhs.score; 172 always_revalidate == rhs.always_revalidate && score == rhs.score;
174 } 173 }
175 174
176 void ResourceRow::ToProto(ResourceData* resource_data) const { 175 void ResourceRow::ToProto(ResourceData* resource_data) const {
177 using chrome_browser_predictors::ResourceData_Priority; 176 using chrome_browser_predictors::ResourceData_Priority;
178 using chrome_browser_predictors::ResourceData_ResourceType; 177 using chrome_browser_predictors::ResourceData_ResourceType;
179 178
180 resource_data->set_primary_key(primary_key);
181 resource_data->set_resource_url(resource_url.spec()); 179 resource_data->set_resource_url(resource_url.spec());
182 resource_data->set_resource_type( 180 resource_data->set_resource_type(
183 static_cast<ResourceData_ResourceType>(resource_type)); 181 static_cast<ResourceData_ResourceType>(resource_type));
184 resource_data->set_number_of_hits(number_of_hits); 182 resource_data->set_number_of_hits(number_of_hits);
185 resource_data->set_number_of_misses(number_of_misses); 183 resource_data->set_number_of_misses(number_of_misses);
186 resource_data->set_consecutive_misses(consecutive_misses); 184 resource_data->set_consecutive_misses(consecutive_misses);
187 resource_data->set_average_position(average_position); 185 resource_data->set_average_position(average_position);
188 resource_data->set_priority(static_cast<ResourceData_Priority>(priority)); 186 resource_data->set_priority(static_cast<ResourceData_Priority>(priority));
189 resource_data->set_has_validators(has_validators); 187 resource_data->set_has_validators(has_validators);
190 resource_data->set_always_revalidate(always_revalidate); 188 resource_data->set_always_revalidate(always_revalidate);
191 } 189 }
192 190
193 // static 191 // static
194 void ResourceRow::FromProto(const ResourceData& proto, ResourceRow* row) { 192 void ResourceRow::FromProto(const ResourceData& proto, ResourceRow* row) {
195 DCHECK(proto.has_primary_key());
196 row->primary_key = proto.primary_key();
197 row->resource_url = GURL(proto.resource_url()); 193 row->resource_url = GURL(proto.resource_url());
198 row->resource_type = 194 row->resource_type =
199 static_cast<content::ResourceType>(proto.resource_type()); 195 static_cast<content::ResourceType>(proto.resource_type());
200 row->number_of_hits = proto.number_of_hits(); 196 row->number_of_hits = proto.number_of_hits();
201 row->number_of_misses = proto.number_of_misses(); 197 row->number_of_misses = proto.number_of_misses();
202 row->consecutive_misses = proto.consecutive_misses(); 198 row->consecutive_misses = proto.consecutive_misses();
203 row->average_position = proto.average_position(); 199 row->average_position = proto.average_position();
204 row->priority = static_cast<net::RequestPriority>(proto.priority()); 200 row->priority = static_cast<net::RequestPriority>(proto.priority());
205 row->has_validators = proto.has_validators(); 201 row->has_validators = proto.has_validators();
206 row->always_revalidate = proto.always_revalidate(); 202 row->always_revalidate = proto.always_revalidate();
203 row->UpdateScore();
207 } 204 }
208 205
209 // static 206 // static
210 void ResourcePrefetchPredictorTables::SortResourceRows(ResourceRows* rows) { 207 void ResourcePrefetchPredictorTables::SortResourceRows(ResourceRows* rows) {
211 std::sort(rows->begin(), rows->end(), 208 std::sort(rows->begin(), rows->end(),
212 [](const ResourceRow& x, const ResourceRow& y) { 209 [](const ResourceRow& x, const ResourceRow& y) {
213 return x.score > y.score; 210 return x.score > y.score;
214 }); 211 });
215 } 212 }
216 213
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 if (CantAccessDatabase()) 297 if (CantAccessDatabase())
301 return; 298 return;
302 299
303 DeleteDataHelper(key_type, std::vector<std::string>(1, key)); 300 DeleteDataHelper(key_type, std::vector<std::string>(1, key));
304 } 301 }
305 302
306 void ResourcePrefetchPredictorTables::DeleteAllData() { 303 void ResourcePrefetchPredictorTables::DeleteAllData() {
307 if (CantAccessDatabase()) 304 if (CantAccessDatabase())
308 return; 305 return;
309 306
310 Statement deleter(DB()->GetUniqueStatement( 307 Statement deleter;
311 base::StringPrintf("DELETE FROM %s", kUrlResourceTableName).c_str())); 308 for (const char* table_name :
312 deleter.Run(); 309 {kUrlResourceTableName, kUrlMetadataTableName, kHostResourceTableName,
313 deleter.Assign(DB()->GetUniqueStatement( 310 kHostMetadataTableName}) {
314 base::StringPrintf("DELETE FROM %s", kUrlMetadataTableName).c_str())); 311 deleter.Assign(DB()->GetUniqueStatement(
315 deleter.Run(); 312 base::StringPrintf("DELETE FROM %s", table_name).c_str()));
316 deleter.Assign(DB()->GetUniqueStatement( 313 deleter.Run();
317 base::StringPrintf("DELETE FROM %s", kHostResourceTableName).c_str())); 314 }
318 deleter.Run();
319 deleter.Assign(DB()->GetUniqueStatement(
320 base::StringPrintf("DELETE FROM %s", kHostMetadataTableName).c_str()));
321 deleter.Run();
322 } 315 }
323 316
324 ResourcePrefetchPredictorTables::ResourcePrefetchPredictorTables() 317 ResourcePrefetchPredictorTables::ResourcePrefetchPredictorTables()
325 : PredictorTableBase() { 318 : PredictorTableBase() {
326 } 319 }
327 320
328 ResourcePrefetchPredictorTables::~ResourcePrefetchPredictorTables() { 321 ResourcePrefetchPredictorTables::~ResourcePrefetchPredictorTables() {
329 } 322 }
330 323
331 void ResourcePrefetchPredictorTables::GetAllDataHelper( 324 void ResourcePrefetchPredictorTables::GetAllDataHelper(
332 PrefetchKeyType key_type, 325 PrefetchKeyType key_type,
333 PrefetchDataMap* data_map, 326 PrefetchDataMap* data_map,
334 std::vector<std::string>* to_delete) { 327 std::vector<std::string>* to_delete) {
335 bool is_host = key_type == PREFETCH_KEY_TYPE_HOST; 328 bool is_host = key_type == PREFETCH_KEY_TYPE_HOST;
336 329
337 // Read the resources table and organize it per primary key. 330 // Read the resources table and organize it per primary key.
338 const char* resource_table_name = is_host ? kHostResourceTableName : 331 const char* resource_table_name = is_host ? kHostResourceTableName :
339 kUrlResourceTableName; 332 kUrlResourceTableName;
340 Statement resource_reader(DB()->GetUniqueStatement( 333 Statement resource_reader(DB()->GetUniqueStatement(
341 base::StringPrintf("SELECT * FROM %s", resource_table_name).c_str())); 334 base::StringPrintf("SELECT * FROM %s", resource_table_name).c_str()));
342 335
343 ResourceRow row; 336 ResourceRow row;
344 while (StepAndInitializeResourceRow(&resource_reader, &row)) { 337 std::string primary_key;
345 row.UpdateScore(); 338 while (StepAndInitializeResourceRow(&resource_reader, &row, &primary_key)) {
346 std::string primary_key = row.primary_key;
347 // Don't need to store primary key since the data is grouped by primary key.
348 row.primary_key.clear();
349
350 PrefetchDataMap::iterator it = data_map->find(primary_key); 339 PrefetchDataMap::iterator it = data_map->find(primary_key);
351 if (it == data_map->end()) { 340 if (it == data_map->end()) {
352 it = data_map->insert(std::make_pair( 341 it = data_map->insert(std::make_pair(
353 primary_key, PrefetchData(key_type, primary_key))).first; 342 primary_key, PrefetchData(key_type, primary_key))).first;
354 } 343 }
355 it->second.resources.push_back(row); 344 it->second.resources.push_back(row);
356 } 345 }
357 346
358 // Sort each of the resource row vectors by score. 347 // Sort each of the resource row vectors by score.
359 for (auto& kv : *data_map) 348 for (auto& kv : *data_map)
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 if (!deleter->Run()) 385 if (!deleter->Run())
397 return false; 386 return false;
398 387
399 deleter.reset(data.is_host() ? GetHostMetadataDeleteStatement() : 388 deleter.reset(data.is_host() ? GetHostMetadataDeleteStatement() :
400 GetUrlMetadataDeleteStatement()); 389 GetUrlMetadataDeleteStatement());
401 deleter->BindString(0, data.primary_key); 390 deleter->BindString(0, data.primary_key);
402 if (!deleter->Run()) 391 if (!deleter->Run())
403 return false; 392 return false;
404 393
405 // Add the new data to the tables. 394 // Add the new data to the tables.
406 const ResourceRows& resources = data.resources; 395 for (const ResourceRow& resource : data.resources) {
407 for (const ResourceRow& resource : resources) {
408 std::unique_ptr<Statement> resource_inserter( 396 std::unique_ptr<Statement> resource_inserter(
409 data.is_host() ? GetHostResourceUpdateStatement() 397 data.is_host() ? GetHostResourceUpdateStatement()
410 : GetUrlResourceUpdateStatement()); 398 : GetUrlResourceUpdateStatement());
411 BindResourceRowToStatement(resource, data.primary_key, 399 BindResourceRowToStatement(resource, data.primary_key,
412 resource_inserter.get()); 400 resource_inserter.get());
413 if (!resource_inserter->Run()) 401 if (!resource_inserter->Run())
414 return false; 402 return false;
415 } 403 }
416 404
417 std::unique_ptr<Statement> metadata_inserter( 405 std::unique_ptr<Statement> metadata_inserter(
418 data.is_host() ? GetHostMetadataUpdateStatement() 406 data.is_host() ? GetHostMetadataUpdateStatement()
419 : GetUrlMetadataUpdateStatement()); 407 : GetUrlMetadataUpdateStatement());
420 metadata_inserter->BindString(0, data.primary_key); 408 metadata_inserter->BindString(0, data.primary_key);
421 metadata_inserter->BindInt64(1, data.last_visit.ToInternalValue()); 409 metadata_inserter->BindInt64(1, data.last_visit.ToInternalValue());
422 if (!metadata_inserter->Run()) 410 if (!metadata_inserter->Run())
423 return false; 411 return false;
424 412
425 return true; 413 return true;
426 } 414 }
427 415
428 void ResourcePrefetchPredictorTables::DeleteDataHelper( 416 void ResourcePrefetchPredictorTables::DeleteDataHelper(
429 PrefetchKeyType key_type, 417 PrefetchKeyType key_type,
430 const std::vector<std::string>& keys) { 418 const std::vector<std::string>& keys) {
431 bool is_host = key_type == PREFETCH_KEY_TYPE_HOST; 419 bool is_host = key_type == PREFETCH_KEY_TYPE_HOST;
432 420
433 for (std::vector<std::string>::const_iterator it = keys.begin(); 421 for (const std::string& key : keys) {
434 it != keys.end(); ++it) {
435 std::unique_ptr<Statement> deleter(is_host 422 std::unique_ptr<Statement> deleter(is_host
436 ? GetHostResourceDeleteStatement() 423 ? GetHostResourceDeleteStatement()
437 : GetUrlResourceDeleteStatement()); 424 : GetUrlResourceDeleteStatement());
438 deleter->BindString(0, *it); 425 deleter->BindString(0, key);
439 deleter->Run(); 426 deleter->Run();
440 427
441 deleter.reset(is_host ? GetHostMetadataDeleteStatement() : 428 deleter.reset(is_host ? GetHostMetadataDeleteStatement() :
442 GetUrlMetadataDeleteStatement()); 429 GetUrlMetadataDeleteStatement());
443 deleter->BindString(0, *it); 430 deleter->BindString(0, key);
444 deleter->Run(); 431 deleter->Run();
445 } 432 }
446 } 433 }
447 434
435 // static
448 bool ResourcePrefetchPredictorTables::StringsAreSmallerThanDBLimit( 436 bool ResourcePrefetchPredictorTables::StringsAreSmallerThanDBLimit(
449 const PrefetchData& data) const { 437 const PrefetchData& data) {
450 if (data.primary_key.length() > kMaxStringLength) 438 if (data.primary_key.length() > kMaxStringLength)
451 return false; 439 return false;
452 440
453 for (ResourceRows::const_iterator it = data.resources.begin(); 441 for (const ResourceRow& row : data.resources) {
454 it != data.resources.end(); ++it) { 442 if (row.resource_url.spec().length() > kMaxStringLength)
455 if (it->resource_url.spec().length() > kMaxStringLength)
456 return false; 443 return false;
457 } 444 }
458 return true; 445 return true;
459 } 446 }
460 447
461 // static 448 // static
462 bool ResourcePrefetchPredictorTables::DropTablesIfOutdated( 449 bool ResourcePrefetchPredictorTables::DropTablesIfOutdated(
463 sql::Connection* db) { 450 sql::Connection* db) {
464 int tables_version = 0; 451 int tables_version = 0;
465 if (db->DoesTableExist(kMetadataTableName)) { 452 if (db->DoesTableExist(kMetadataTableName)) {
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
618 } 605 }
619 606
620 Statement* ResourcePrefetchPredictorTables::GetHostMetadataUpdateStatement() { 607 Statement* ResourcePrefetchPredictorTables::GetHostMetadataUpdateStatement() {
621 return new Statement(DB()->GetCachedStatement( 608 return new Statement(DB()->GetCachedStatement(
622 SQL_FROM_HERE, base::StringPrintf(kInsertMetadataTableStatementTemplate, 609 SQL_FROM_HERE, base::StringPrintf(kInsertMetadataTableStatementTemplate,
623 kHostMetadataTableName) 610 kHostMetadataTableName)
624 .c_str())); 611 .c_str()));
625 } 612 }
626 613
627 } // namespace predictors 614 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698