| 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 chrome_browser_predictors::ResourceData; |
| 19 using content::BrowserThread; | 20 using content::BrowserThread; |
| 20 using sql::Statement; | 21 using sql::Statement; |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 using ResourceRow = predictors::ResourcePrefetchPredictorTables::ResourceRow; | |
| 25 | |
| 26 const char kUrlResourceTableName[] = "resource_prefetch_predictor_url"; | 25 const char kUrlResourceTableName[] = "resource_prefetch_predictor_url"; |
| 27 const char kUrlMetadataTableName[] = "resource_prefetch_predictor_url_metadata"; | 26 const char kUrlMetadataTableName[] = "resource_prefetch_predictor_url_metadata"; |
| 28 const char kHostResourceTableName[] = "resource_prefetch_predictor_host"; | 27 const char kHostResourceTableName[] = "resource_prefetch_predictor_host"; |
| 29 const char kHostMetadataTableName[] = | 28 const char kHostMetadataTableName[] = |
| 30 "resource_prefetch_predictor_host_metadata"; | 29 "resource_prefetch_predictor_host_metadata"; |
| 31 | 30 |
| 32 const char kInsertResourceTableStatementTemplate[] = | 31 const char kInsertResourceTableStatementTemplate[] = |
| 33 "INSERT INTO %s (main_page_url, resource_url, proto) VALUES (?,?,?)"; | 32 "INSERT INTO %s (main_page_url, resource_url, proto) VALUES (?,?,?)"; |
| 34 const char kInsertMetadataTableStatementTemplate[] = | 33 const char kInsertMetadataTableStatementTemplate[] = |
| 35 "INSERT INTO %s (main_page_url, last_visit_time) VALUES (?,?)"; | 34 "INSERT INTO %s (main_page_url, last_visit_time) VALUES (?,?)"; |
| 36 const char kDeleteStatementTemplate[] = "DELETE FROM %s WHERE main_page_url=?"; | 35 const char kDeleteStatementTemplate[] = "DELETE FROM %s WHERE main_page_url=?"; |
| 37 | 36 |
| 38 void BindResourceRowToStatement(const ResourceRow& row, | 37 void BindToStatement(const ResourceData& resource, |
| 39 const std::string& primary_key, | 38 const std::string& primary_key, |
| 40 Statement* statement) { | 39 Statement* statement) { |
| 41 chrome_browser_predictors::ResourceData proto; | 40 int size = resource.ByteSize(); |
| 42 row.ToProto(&proto); | 41 std::vector<char> buffer(size); |
| 43 int size = proto.ByteSize(); | 42 resource.SerializeToArray(&buffer[0], size); |
| 44 std::vector<char> proto_buffer(size); | |
| 45 proto.SerializeToArray(&proto_buffer[0], size); | |
| 46 | 43 |
| 47 statement->BindString(0, primary_key); | 44 statement->BindString(0, primary_key); |
| 48 statement->BindString(1, row.resource_url.spec()); | 45 statement->BindString(1, resource.resource_url()); |
| 49 statement->BindBlob(2, &proto_buffer[0], size); | 46 statement->BindBlob(2, &buffer[0], size); |
| 50 } | 47 } |
| 51 | 48 |
| 52 bool StepAndInitializeResourceRow(Statement* statement, ResourceRow* row) { | 49 bool StepAndInitializeResource(Statement* statement, ResourceData* resource) { |
| 53 if (!statement->Step()) | 50 if (!statement->Step()) |
| 54 return false; | 51 return false; |
| 52 std::string primary_key = statement->ColumnString(0); |
| 53 std::string resource_url = statement->ColumnString(1); |
| 55 | 54 |
| 56 int size = statement->ColumnByteLength(2); | 55 int size = statement->ColumnByteLength(2); |
| 57 const void* data = statement->ColumnBlob(2); | 56 const void* data = statement->ColumnBlob(2); |
| 58 DCHECK(data); | 57 DCHECK(data); |
| 59 chrome_browser_predictors::ResourceData proto; | 58 resource->ParseFromArray(data, size); |
| 60 proto.ParseFromArray(data, size); | 59 resource->set_primary_key(primary_key); |
| 61 ResourceRow::FromProto(proto, row); | 60 DCHECK(resource_url == resource->resource_url()); |
| 62 | 61 |
| 63 row->primary_key = statement->ColumnString(0); | |
| 64 row->resource_url = GURL(statement->ColumnString(1)); | |
| 65 return true; | 62 return true; |
| 66 } | 63 } |
| 67 | 64 |
| 68 } // namespace | 65 } // namespace |
| 69 | 66 |
| 70 namespace predictors { | 67 namespace predictors { |
| 71 | 68 |
| 72 // static | 69 // static |
| 73 const size_t ResourcePrefetchPredictorTables::kMaxStringLength = 1024; | 70 void ResourcePrefetchPredictorTables::SortResources( |
| 74 | 71 std::vector<ResourceData>* resources) { |
| 75 ResourceRow::ResourceRow() | 72 std::sort(resources->begin(), resources->end(), |
| 76 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), | 73 [](const ResourceData& x, const ResourceData& y) { |
| 77 number_of_hits(0), | 74 return x.score() > y.score(); |
| 78 number_of_misses(0), | 75 }); |
| 79 consecutive_misses(0), | |
| 80 average_position(0.0), | |
| 81 priority(net::IDLE), | |
| 82 has_validators(false), | |
| 83 always_revalidate(false), | |
| 84 score(0.0) {} | |
| 85 | |
| 86 ResourceRow::ResourceRow(const ResourceRow& other) | |
| 87 : primary_key(other.primary_key), | |
| 88 resource_url(other.resource_url), | |
| 89 resource_type(other.resource_type), | |
| 90 number_of_hits(other.number_of_hits), | |
| 91 number_of_misses(other.number_of_misses), | |
| 92 consecutive_misses(other.consecutive_misses), | |
| 93 average_position(other.average_position), | |
| 94 priority(other.priority), | |
| 95 has_validators(other.has_validators), | |
| 96 always_revalidate(other.always_revalidate), | |
| 97 score(other.score) {} | |
| 98 | |
| 99 ResourceRow::ResourceRow(const std::string& i_primary_key, | |
| 100 const std::string& i_resource_url, | |
| 101 content::ResourceType i_resource_type, | |
| 102 int i_number_of_hits, | |
| 103 int i_number_of_misses, | |
| 104 int i_consecutive_misses, | |
| 105 double i_average_position, | |
| 106 net::RequestPriority i_priority, | |
| 107 bool i_has_validators, | |
| 108 bool i_always_revalidate) | |
| 109 : primary_key(i_primary_key), | |
| 110 resource_url(i_resource_url), | |
| 111 resource_type(i_resource_type), | |
| 112 number_of_hits(i_number_of_hits), | |
| 113 number_of_misses(i_number_of_misses), | |
| 114 consecutive_misses(i_consecutive_misses), | |
| 115 average_position(i_average_position), | |
| 116 priority(i_priority), | |
| 117 has_validators(i_has_validators), | |
| 118 always_revalidate(i_always_revalidate) { | |
| 119 UpdateScore(); | |
| 120 } | 76 } |
| 121 | 77 |
| 122 void ResourceRow::UpdateScore() { | 78 // static |
| 79 void ResourcePrefetchPredictorTables::UpdateResourceScore( |
| 80 ResourceData* resource) { |
| 123 // The score is calculated so that when the rows are sorted, stylesheets, | 81 // The score is calculated so that when the rows are sorted, stylesheets, |
| 124 // scripts and fonts appear first, sorted by position(ascending) and then the | 82 // scripts and fonts appear first, sorted by position(ascending) and then the |
| 125 // rest of the resources sorted by position (ascending). | 83 // rest of the resources sorted by position (ascending). |
| 126 static const int kMaxResourcesPerType = 100; | 84 static const int kMaxResourcesPerType = 100; |
| 85 content::ResourceType resource_type = |
| 86 static_cast<content::ResourceType>(resource->resource_type()); |
| 127 switch (resource_type) { | 87 switch (resource_type) { |
| 128 case content::RESOURCE_TYPE_STYLESHEET: | 88 case content::RESOURCE_TYPE_STYLESHEET: |
| 129 case content::RESOURCE_TYPE_SCRIPT: | 89 case content::RESOURCE_TYPE_SCRIPT: |
| 130 case content::RESOURCE_TYPE_FONT_RESOURCE: | 90 case content::RESOURCE_TYPE_FONT_RESOURCE: |
| 131 score = (2 * kMaxResourcesPerType) - average_position; | 91 resource->set_score((2 * kMaxResourcesPerType) - |
| 92 resource->average_position()); |
| 132 break; | 93 break; |
| 133 | 94 |
| 134 case content::RESOURCE_TYPE_IMAGE: | 95 case content::RESOURCE_TYPE_IMAGE: |
| 135 default: | 96 default: |
| 136 score = kMaxResourcesPerType - average_position; | 97 resource->set_score(kMaxResourcesPerType - resource->average_position()); |
| 137 break; | 98 break; |
| 138 } | 99 } |
| 139 // TODO(lizeb): Take priority into account. | 100 // TODO(lizeb): Take priority into account. |
| 140 } | 101 } |
| 141 | 102 |
| 142 bool ResourceRow::operator==(const ResourceRow& rhs) const { | |
| 143 return primary_key == rhs.primary_key && resource_url == rhs.resource_url && | |
| 144 resource_type == rhs.resource_type && | |
| 145 number_of_hits == rhs.number_of_hits && | |
| 146 number_of_misses == rhs.number_of_misses && | |
| 147 consecutive_misses == rhs.consecutive_misses && | |
| 148 average_position == rhs.average_position && priority == rhs.priority && | |
| 149 has_validators == rhs.has_validators && | |
| 150 always_revalidate == rhs.always_revalidate && score == rhs.score; | |
| 151 } | |
| 152 | |
| 153 void ResourceRow::ToProto(ResourceData* resource_data) const { | |
| 154 using chrome_browser_predictors::ResourceData_Priority; | |
| 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 }); | |
| 192 } | |
| 193 | |
| 194 ResourcePrefetchPredictorTables::PrefetchData::PrefetchData( | 103 ResourcePrefetchPredictorTables::PrefetchData::PrefetchData( |
| 195 PrefetchKeyType i_key_type, | 104 PrefetchKeyType i_key_type, |
| 196 const std::string& i_primary_key) | 105 const std::string& i_primary_key) |
| 197 : key_type(i_key_type), | 106 : key_type(i_key_type), |
| 198 primary_key(i_primary_key) { | 107 primary_key(i_primary_key) { |
| 199 } | 108 } |
| 200 | 109 |
| 201 ResourcePrefetchPredictorTables::PrefetchData::PrefetchData( | 110 ResourcePrefetchPredictorTables::PrefetchData::PrefetchData( |
| 202 const PrefetchData& other) | 111 const PrefetchData& other) |
| 203 : key_type(other.key_type), | 112 : key_type(other.key_type), |
| 204 primary_key(other.primary_key), | 113 primary_key(other.primary_key), |
| 205 last_visit(other.last_visit), | 114 last_visit(other.last_visit), |
| 206 resources(other.resources) { | 115 resources(other.resources) { |
| 207 } | 116 } |
| 208 | 117 |
| 209 ResourcePrefetchPredictorTables::PrefetchData::~PrefetchData() { | 118 ResourcePrefetchPredictorTables::PrefetchData::~PrefetchData() {} |
| 210 } | |
| 211 | |
| 212 bool ResourcePrefetchPredictorTables::PrefetchData::operator==( | |
| 213 const PrefetchData& rhs) const { | |
| 214 return key_type == rhs.key_type && primary_key == rhs.primary_key && | |
| 215 resources == rhs.resources; | |
| 216 } | |
| 217 | 119 |
| 218 void ResourcePrefetchPredictorTables::GetAllData( | 120 void ResourcePrefetchPredictorTables::GetAllData( |
| 219 PrefetchDataMap* url_data_map, | 121 PrefetchDataMap* url_data_map, |
| 220 PrefetchDataMap* host_data_map) { | 122 PrefetchDataMap* host_data_map) { |
| 221 DCHECK_CURRENTLY_ON(BrowserThread::DB); | 123 DCHECK_CURRENTLY_ON(BrowserThread::DB); |
| 222 if (CantAccessDatabase()) | 124 if (CantAccessDatabase()) |
| 223 return; | 125 return; |
| 224 | 126 |
| 225 DCHECK(url_data_map); | 127 DCHECK(url_data_map); |
| 226 DCHECK(host_data_map); | 128 DCHECK(host_data_map); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 if (CantAccessDatabase()) | 179 if (CantAccessDatabase()) |
| 278 return; | 180 return; |
| 279 | 181 |
| 280 DeleteDataHelper(key_type, std::vector<std::string>(1, key)); | 182 DeleteDataHelper(key_type, std::vector<std::string>(1, key)); |
| 281 } | 183 } |
| 282 | 184 |
| 283 void ResourcePrefetchPredictorTables::DeleteAllData() { | 185 void ResourcePrefetchPredictorTables::DeleteAllData() { |
| 284 if (CantAccessDatabase()) | 186 if (CantAccessDatabase()) |
| 285 return; | 187 return; |
| 286 | 188 |
| 287 Statement deleter(DB()->GetUniqueStatement( | 189 Statement deleter; |
| 288 base::StringPrintf("DELETE FROM %s", kUrlResourceTableName).c_str())); | 190 for (const char* table_name : |
| 289 deleter.Run(); | 191 {kUrlResourceTableName, kUrlMetadataTableName, kHostResourceTableName, |
| 290 deleter.Assign(DB()->GetUniqueStatement( | 192 kHostMetadataTableName}) { |
| 291 base::StringPrintf("DELETE FROM %s", kUrlMetadataTableName).c_str())); | 193 deleter.Assign(DB()->GetUniqueStatement( |
| 292 deleter.Run(); | 194 base::StringPrintf("DELETE FROM %s", table_name).c_str())); |
| 293 deleter.Assign(DB()->GetUniqueStatement( | 195 deleter.Run(); |
| 294 base::StringPrintf("DELETE FROM %s", kHostResourceTableName).c_str())); | 196 } |
| 295 deleter.Run(); | |
| 296 deleter.Assign(DB()->GetUniqueStatement( | |
| 297 base::StringPrintf("DELETE FROM %s", kHostMetadataTableName).c_str())); | |
| 298 deleter.Run(); | |
| 299 } | 197 } |
| 300 | 198 |
| 301 ResourcePrefetchPredictorTables::ResourcePrefetchPredictorTables() | 199 ResourcePrefetchPredictorTables::ResourcePrefetchPredictorTables() |
| 302 : PredictorTableBase() { | 200 : PredictorTableBase() { |
| 303 } | 201 } |
| 304 | 202 |
| 305 ResourcePrefetchPredictorTables::~ResourcePrefetchPredictorTables() { | 203 ResourcePrefetchPredictorTables::~ResourcePrefetchPredictorTables() { |
| 306 } | 204 } |
| 307 | 205 |
| 308 void ResourcePrefetchPredictorTables::GetAllDataHelper( | 206 void ResourcePrefetchPredictorTables::GetAllDataHelper( |
| 309 PrefetchKeyType key_type, | 207 PrefetchKeyType key_type, |
| 310 PrefetchDataMap* data_map, | 208 PrefetchDataMap* data_map, |
| 311 std::vector<std::string>* to_delete) { | 209 std::vector<std::string>* to_delete) { |
| 312 bool is_host = key_type == PREFETCH_KEY_TYPE_HOST; | 210 bool is_host = key_type == PREFETCH_KEY_TYPE_HOST; |
| 313 | 211 |
| 314 // Read the resources table and organize it per primary key. | 212 // Read the resources table and organize it per primary key. |
| 315 const char* resource_table_name = is_host ? kHostResourceTableName : | 213 const char* resource_table_name = is_host ? kHostResourceTableName : |
| 316 kUrlResourceTableName; | 214 kUrlResourceTableName; |
| 317 Statement resource_reader(DB()->GetUniqueStatement( | 215 Statement resource_reader(DB()->GetUniqueStatement( |
| 318 base::StringPrintf("SELECT * FROM %s", resource_table_name).c_str())); | 216 base::StringPrintf("SELECT * FROM %s", resource_table_name).c_str())); |
| 319 | 217 |
| 320 ResourceRow row; | 218 ResourceData resource; |
| 321 while (StepAndInitializeResourceRow(&resource_reader, &row)) { | 219 while (StepAndInitializeResource(&resource_reader, &resource)) { |
| 322 row.UpdateScore(); | 220 UpdateResourceScore(&resource); |
| 323 std::string primary_key = row.primary_key; | 221 std::string primary_key = resource.primary_key(); |
| 324 // Don't need to store primary key since the data is grouped by primary key. | 222 // Don't need to store primary key since the data is grouped by primary key. |
| 325 row.primary_key.clear(); | 223 resource.clear_primary_key(); |
| 326 | |
| 327 PrefetchDataMap::iterator it = data_map->find(primary_key); | 224 PrefetchDataMap::iterator it = data_map->find(primary_key); |
| 328 if (it == data_map->end()) { | 225 if (it == data_map->end()) { |
| 329 it = data_map->insert(std::make_pair( | 226 it = data_map->insert(std::make_pair( |
| 330 primary_key, PrefetchData(key_type, primary_key))).first; | 227 primary_key, PrefetchData(key_type, primary_key))).first; |
| 331 } | 228 } |
| 332 it->second.resources.push_back(row); | 229 it->second.resources.push_back(resource); |
| 333 } | 230 } |
| 334 | 231 |
| 335 // Sort each of the resource row vectors by score. | 232 // Sort each of the resource row vectors by score. |
| 336 for (auto& kv : *data_map) | 233 for (auto& kv : *data_map) |
| 337 SortResourceRows(&(kv.second.resources)); | 234 SortResources(&(kv.second.resources)); |
| 338 | 235 |
| 339 // Read the metadata and keep track of entries that have metadata, but no | 236 // Read the metadata and keep track of entries that have metadata, but no |
| 340 // resource entries, so they can be deleted. | 237 // resource entries, so they can be deleted. |
| 341 const char* metadata_table_name = is_host ? kHostMetadataTableName : | 238 const char* metadata_table_name = is_host ? kHostMetadataTableName : |
| 342 kUrlMetadataTableName; | 239 kUrlMetadataTableName; |
| 343 Statement metadata_reader(DB()->GetUniqueStatement( | 240 Statement metadata_reader(DB()->GetUniqueStatement( |
| 344 base::StringPrintf("SELECT * FROM %s", metadata_table_name).c_str())); | 241 base::StringPrintf("SELECT * FROM %s", metadata_table_name).c_str())); |
| 345 | 242 |
| 346 while (metadata_reader.Step()) { | 243 while (metadata_reader.Step()) { |
| 347 std::string primary_key = metadata_reader.ColumnString(0); | 244 std::string primary_key = metadata_reader.ColumnString(0); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 373 if (!deleter->Run()) | 270 if (!deleter->Run()) |
| 374 return false; | 271 return false; |
| 375 | 272 |
| 376 deleter.reset(data.is_host() ? GetHostMetadataDeleteStatement() : | 273 deleter.reset(data.is_host() ? GetHostMetadataDeleteStatement() : |
| 377 GetUrlMetadataDeleteStatement()); | 274 GetUrlMetadataDeleteStatement()); |
| 378 deleter->BindString(0, data.primary_key); | 275 deleter->BindString(0, data.primary_key); |
| 379 if (!deleter->Run()) | 276 if (!deleter->Run()) |
| 380 return false; | 277 return false; |
| 381 | 278 |
| 382 // Add the new data to the tables. | 279 // Add the new data to the tables. |
| 383 const ResourceRows& resources = data.resources; | 280 for (const ResourceData& resource : data.resources) { |
| 384 for (const ResourceRow& resource : resources) { | |
| 385 std::unique_ptr<Statement> resource_inserter( | 281 std::unique_ptr<Statement> resource_inserter( |
| 386 data.is_host() ? GetHostResourceUpdateStatement() | 282 data.is_host() ? GetHostResourceUpdateStatement() |
| 387 : GetUrlResourceUpdateStatement()); | 283 : GetUrlResourceUpdateStatement()); |
| 388 BindResourceRowToStatement(resource, data.primary_key, | 284 BindToStatement(resource, data.primary_key, resource_inserter.get()); |
| 389 resource_inserter.get()); | |
| 390 if (!resource_inserter->Run()) | 285 if (!resource_inserter->Run()) |
| 391 return false; | 286 return false; |
| 392 } | 287 } |
| 393 | 288 |
| 394 std::unique_ptr<Statement> metadata_inserter( | 289 std::unique_ptr<Statement> metadata_inserter( |
| 395 data.is_host() ? GetHostMetadataUpdateStatement() | 290 data.is_host() ? GetHostMetadataUpdateStatement() |
| 396 : GetUrlMetadataUpdateStatement()); | 291 : GetUrlMetadataUpdateStatement()); |
| 397 metadata_inserter->BindString(0, data.primary_key); | 292 metadata_inserter->BindString(0, data.primary_key); |
| 398 metadata_inserter->BindInt64(1, data.last_visit.ToInternalValue()); | 293 metadata_inserter->BindInt64(1, data.last_visit.ToInternalValue()); |
| 399 if (!metadata_inserter->Run()) | 294 if (!metadata_inserter->Run()) |
| 400 return false; | 295 return false; |
| 401 | 296 |
| 402 return true; | 297 return true; |
| 403 } | 298 } |
| 404 | 299 |
| 405 void ResourcePrefetchPredictorTables::DeleteDataHelper( | 300 void ResourcePrefetchPredictorTables::DeleteDataHelper( |
| 406 PrefetchKeyType key_type, | 301 PrefetchKeyType key_type, |
| 407 const std::vector<std::string>& keys) { | 302 const std::vector<std::string>& keys) { |
| 408 bool is_host = key_type == PREFETCH_KEY_TYPE_HOST; | 303 bool is_host = key_type == PREFETCH_KEY_TYPE_HOST; |
| 409 | 304 |
| 410 for (std::vector<std::string>::const_iterator it = keys.begin(); | 305 for (const std::string& key : keys) { |
| 411 it != keys.end(); ++it) { | |
| 412 std::unique_ptr<Statement> deleter(is_host | 306 std::unique_ptr<Statement> deleter(is_host |
| 413 ? GetHostResourceDeleteStatement() | 307 ? GetHostResourceDeleteStatement() |
| 414 : GetUrlResourceDeleteStatement()); | 308 : GetUrlResourceDeleteStatement()); |
| 415 deleter->BindString(0, *it); | 309 deleter->BindString(0, key); |
| 416 deleter->Run(); | 310 deleter->Run(); |
| 417 | 311 |
| 418 deleter.reset(is_host ? GetHostMetadataDeleteStatement() : | 312 deleter.reset(is_host ? GetHostMetadataDeleteStatement() |
| 419 GetUrlMetadataDeleteStatement()); | 313 : GetUrlMetadataDeleteStatement()); |
| 420 deleter->BindString(0, *it); | 314 deleter->BindString(0, key); |
| 421 deleter->Run(); | 315 deleter->Run(); |
| 422 } | 316 } |
| 423 } | 317 } |
| 424 | 318 |
| 319 // static |
| 425 bool ResourcePrefetchPredictorTables::StringsAreSmallerThanDBLimit( | 320 bool ResourcePrefetchPredictorTables::StringsAreSmallerThanDBLimit( |
| 426 const PrefetchData& data) const { | 321 const PrefetchData& data) { |
| 427 if (data.primary_key.length() > kMaxStringLength) | 322 if (data.primary_key.length() > kMaxStringLength) |
| 428 return false; | 323 return false; |
| 429 | 324 |
| 430 for (ResourceRows::const_iterator it = data.resources.begin(); | 325 for (const ResourceData& resource : data.resources) { |
| 431 it != data.resources.end(); ++it) { | 326 if (resource.resource_url().length() > kMaxStringLength) |
| 432 if (it->resource_url.spec().length() > kMaxStringLength) | |
| 433 return false; | 327 return false; |
| 434 } | 328 } |
| 435 return true; | 329 return true; |
| 436 } | 330 } |
| 437 | 331 |
| 438 bool ResourcePrefetchPredictorTables::DropTablesIfOutdated( | 332 bool ResourcePrefetchPredictorTables::DropTablesIfOutdated( |
| 439 sql::Connection* db) { | 333 sql::Connection* db) { |
| 440 bool success = true; | 334 bool success = true; |
| 441 for (const char* table_name : | 335 for (const char* table_name : |
| 442 {kUrlResourceTableName, kHostResourceTableName}) { | 336 {kUrlResourceTableName, kHostResourceTableName}) { |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 } | 460 } |
| 567 | 461 |
| 568 Statement* ResourcePrefetchPredictorTables::GetHostMetadataUpdateStatement() { | 462 Statement* ResourcePrefetchPredictorTables::GetHostMetadataUpdateStatement() { |
| 569 return new Statement(DB()->GetCachedStatement( | 463 return new Statement(DB()->GetCachedStatement( |
| 570 SQL_FROM_HERE, base::StringPrintf(kInsertMetadataTableStatementTemplate, | 464 SQL_FROM_HERE, base::StringPrintf(kInsertMetadataTableStatementTemplate, |
| 571 kHostMetadataTableName) | 465 kHostMetadataTableName) |
| 572 .c_str())); | 466 .c_str())); |
| 573 } | 467 } |
| 574 | 468 |
| 575 } // namespace predictors | 469 } // namespace predictors |
| OLD | NEW |