| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 const char kCreateGlobalMetadataStatementTemplate[] = | 30 const char kCreateGlobalMetadataStatementTemplate[] = |
| 31 "CREATE TABLE %s ( " | 31 "CREATE TABLE %s ( " |
| 32 "key TEXT, value INTEGER, " | 32 "key TEXT, value INTEGER, " |
| 33 "PRIMARY KEY (key))"; | 33 "PRIMARY KEY (key))"; |
| 34 const char kCreateProtoTableStatementTemplate[] = | 34 const char kCreateProtoTableStatementTemplate[] = |
| 35 "CREATE TABLE %s ( " | 35 "CREATE TABLE %s ( " |
| 36 "key TEXT, " | 36 "key TEXT, " |
| 37 "proto BLOB, " | 37 "proto BLOB, " |
| 38 "PRIMARY KEY(key))"; | 38 "PRIMARY KEY(key))"; |
| 39 const char kInsertProtoStatementTemplate[] = | |
| 40 "INSERT INTO %s (key, proto) VALUES (?,?)"; | |
| 41 const char kDeleteProtoStatementTemplate[] = "DELETE FROM %s WHERE key=?"; | |
| 42 const char kSelectAllStatementTemplate[] = "SELECT * FROM %s"; | |
| 43 | |
| 44 void BindProtoDataToStatement(const std::string& key, | |
| 45 const MessageLite& data, | |
| 46 sql::Statement* statement) { | |
| 47 int size = data.ByteSize(); | |
| 48 DCHECK_GT(size, 0); | |
| 49 std::vector<char> proto_buffer(size); | |
| 50 data.SerializeToArray(&proto_buffer[0], size); | |
| 51 | |
| 52 statement->BindString(0, key); | |
| 53 statement->BindBlob(1, &proto_buffer[0], size); | |
| 54 } | |
| 55 | |
| 56 bool StepAndInitializeProtoData(sql::Statement* statement, | |
| 57 std::string* key, | |
| 58 MessageLite* data) { | |
| 59 if (!statement->Step()) | |
| 60 return false; | |
| 61 | |
| 62 *key = statement->ColumnString(0); | |
| 63 | |
| 64 int size = statement->ColumnByteLength(1); | |
| 65 const void* blob = statement->ColumnBlob(1); | |
| 66 DCHECK(blob); | |
| 67 data->ParseFromArray(blob, size); | |
| 68 | |
| 69 return true; | |
| 70 } | |
| 71 | 39 |
| 72 predictors::ResourceData::ResourceType PrecacheResourceTypeToResourceType( | 40 predictors::ResourceData::ResourceType PrecacheResourceTypeToResourceType( |
| 73 precache::PrecacheResource::Type resource_type) { | 41 precache::PrecacheResource::Type resource_type) { |
| 74 using precache::PrecacheResource; | 42 using precache::PrecacheResource; |
| 75 using predictors::ResourceData; | 43 using predictors::ResourceData; |
| 76 switch (resource_type) { | 44 switch (resource_type) { |
| 77 case PrecacheResource::RESOURCE_TYPE_IMAGE: | 45 case PrecacheResource::RESOURCE_TYPE_IMAGE: |
| 78 return ResourceData::RESOURCE_TYPE_IMAGE; | 46 return ResourceData::RESOURCE_TYPE_IMAGE; |
| 79 case PrecacheResource::RESOURCE_TYPE_FONT: | 47 case PrecacheResource::RESOURCE_TYPE_FONT: |
| 80 return ResourceData::RESOURCE_TYPE_FONT_RESOURCE; | 48 return ResourceData::RESOURCE_TYPE_FONT_RESOURCE; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 DCHECK(host_redirect_data_map); | 153 DCHECK(host_redirect_data_map); |
| 186 DCHECK(manifest_map); | 154 DCHECK(manifest_map); |
| 187 DCHECK(origin_data_map); | 155 DCHECK(origin_data_map); |
| 188 url_data_map->clear(); | 156 url_data_map->clear(); |
| 189 host_data_map->clear(); | 157 host_data_map->clear(); |
| 190 url_redirect_data_map->clear(); | 158 url_redirect_data_map->clear(); |
| 191 host_redirect_data_map->clear(); | 159 host_redirect_data_map->clear(); |
| 192 manifest_map->clear(); | 160 manifest_map->clear(); |
| 193 origin_data_map->clear(); | 161 origin_data_map->clear(); |
| 194 | 162 |
| 195 GetAllResourceDataHelper(PREFETCH_KEY_TYPE_URL, url_data_map); | 163 url_resource_table_->GetAllData(url_data_map); |
| 196 GetAllResourceDataHelper(PREFETCH_KEY_TYPE_HOST, host_data_map); | 164 host_resource_table_->GetAllData(host_data_map); |
| 197 GetAllRedirectDataHelper(PREFETCH_KEY_TYPE_URL, url_redirect_data_map); | 165 url_redirect_table_->GetAllData(url_redirect_data_map); |
| 198 GetAllRedirectDataHelper(PREFETCH_KEY_TYPE_HOST, host_redirect_data_map); | 166 host_redirect_table_->GetAllData(host_redirect_data_map); |
| 199 GetAllManifestDataHelper(manifest_map); | 167 manifest_table_->GetAllData(manifest_map); |
| 200 GetAllOriginDataHelper(origin_data_map); | 168 origin_table_->GetAllData(origin_data_map); |
| 201 } | 169 } |
| 202 | 170 |
| 203 void ResourcePrefetchPredictorTables::UpdateResourceData( | 171 void ResourcePrefetchPredictorTables::UpdateResourceData( |
| 204 const PrefetchData& data, | 172 const PrefetchData& data, |
| 205 PrefetchKeyType key_type) { | 173 PrefetchKeyType key_type) { |
| 206 TRACE_EVENT0("browser", "ResourcePrefetchPredictor::UpdateResourceData"); | 174 TRACE_EVENT0("browser", "ResourcePrefetchPredictor::UpdateResourceData"); |
| 207 DCHECK_CURRENTLY_ON(BrowserThread::DB); | 175 DCHECK_CURRENTLY_ON(BrowserThread::DB); |
| 208 if (CantAccessDatabase()) | 176 if (CantAccessDatabase()) |
| 209 return; | 177 return; |
| 210 | 178 |
| 211 UpdateDataHelper(key_type, PrefetchDataType::RESOURCE, data.primary_key(), | 179 if (key_type == PREFETCH_KEY_TYPE_URL) |
| 212 data); | 180 url_resource_table_->UpdateData(data.primary_key(), data); |
| 181 else |
| 182 host_resource_table_->UpdateData(data.primary_key(), data); |
| 213 } | 183 } |
| 214 | 184 |
| 215 void ResourcePrefetchPredictorTables::UpdateRedirectData( | 185 void ResourcePrefetchPredictorTables::UpdateRedirectData( |
| 216 const RedirectData& data, | 186 const RedirectData& data, |
| 217 PrefetchKeyType key_type) { | 187 PrefetchKeyType key_type) { |
| 218 TRACE_EVENT0("browser", "ResourcePrefetchPredictor::UpdateRedirectData"); | 188 TRACE_EVENT0("browser", "ResourcePrefetchPredictor::UpdateRedirectData"); |
| 219 DCHECK_CURRENTLY_ON(BrowserThread::DB); | 189 DCHECK_CURRENTLY_ON(BrowserThread::DB); |
| 220 if (CantAccessDatabase()) | 190 if (CantAccessDatabase()) |
| 221 return; | 191 return; |
| 222 | 192 |
| 223 UpdateDataHelper(key_type, PrefetchDataType::REDIRECT, data.primary_key(), | 193 if (key_type == PREFETCH_KEY_TYPE_URL) |
| 224 data); | 194 url_redirect_table_->UpdateData(data.primary_key(), data); |
| 195 else |
| 196 host_redirect_table_->UpdateData(data.primary_key(), data); |
| 225 } | 197 } |
| 226 | 198 |
| 227 void ResourcePrefetchPredictorTables::UpdateManifestData( | 199 void ResourcePrefetchPredictorTables::UpdateManifestData( |
| 228 const std::string& host, | 200 const std::string& host, |
| 229 const precache::PrecacheManifest& manifest_data) { | 201 const precache::PrecacheManifest& manifest_data) { |
| 230 TRACE_EVENT0("browser", "ResourcePrefetchPredictor::UpdateManifestData"); | 202 TRACE_EVENT0("browser", "ResourcePrefetchPredictor::UpdateManifestData"); |
| 231 DCHECK_CURRENTLY_ON(BrowserThread::DB); | 203 DCHECK_CURRENTLY_ON(BrowserThread::DB); |
| 232 if (CantAccessDatabase()) | 204 if (CantAccessDatabase()) |
| 233 return; | 205 return; |
| 234 | 206 |
| 235 UpdateDataHelper(PREFETCH_KEY_TYPE_HOST, PrefetchDataType::MANIFEST, host, | 207 manifest_table_->UpdateData(host, manifest_data); |
| 236 manifest_data); | |
| 237 } | 208 } |
| 238 | 209 |
| 239 void ResourcePrefetchPredictorTables::UpdateOriginData( | 210 void ResourcePrefetchPredictorTables::UpdateOriginData( |
| 240 const OriginData& origin_data) { | 211 const OriginData& origin_data) { |
| 241 TRACE_EVENT0("browser", "ResourcePrefetchPredictor::UpdateOriginData"); | 212 TRACE_EVENT0("browser", "ResourcePrefetchPredictor::UpdateOriginData"); |
| 242 DCHECK_CURRENTLY_ON(BrowserThread::DB); | 213 DCHECK_CURRENTLY_ON(BrowserThread::DB); |
| 243 if (CantAccessDatabase()) | 214 if (CantAccessDatabase()) |
| 244 return; | 215 return; |
| 245 | 216 |
| 246 UpdateDataHelper(PREFETCH_KEY_TYPE_HOST, PrefetchDataType::ORIGIN, | 217 origin_table_->UpdateData(origin_data.host(), origin_data); |
| 247 origin_data.host(), origin_data); | |
| 248 } | 218 } |
| 249 | 219 |
| 250 void ResourcePrefetchPredictorTables::DeleteResourceData( | 220 void ResourcePrefetchPredictorTables::DeleteResourceData( |
| 251 const std::vector<std::string>& urls, | 221 const std::vector<std::string>& urls, |
| 252 const std::vector<std::string>& hosts) { | 222 const std::vector<std::string>& hosts) { |
| 253 DCHECK_CURRENTLY_ON(BrowserThread::DB); | 223 DCHECK_CURRENTLY_ON(BrowserThread::DB); |
| 254 if (CantAccessDatabase()) | 224 if (CantAccessDatabase()) |
| 255 return; | 225 return; |
| 256 | 226 |
| 257 DCHECK(!urls.empty() || !hosts.empty()); | 227 DCHECK(!urls.empty() || !hosts.empty()); |
| 258 | 228 |
| 259 if (!urls.empty()) | 229 if (!urls.empty()) |
| 260 DeleteDataHelper(PREFETCH_KEY_TYPE_URL, PrefetchDataType::RESOURCE, urls); | 230 url_resource_table_->DeleteData(urls); |
| 261 if (!hosts.empty()) | 231 if (!hosts.empty()) |
| 262 DeleteDataHelper(PREFETCH_KEY_TYPE_HOST, PrefetchDataType::RESOURCE, hosts); | 232 host_resource_table_->DeleteData(hosts); |
| 263 } | 233 } |
| 264 | 234 |
| 265 void ResourcePrefetchPredictorTables::DeleteSingleResourceDataPoint( | 235 void ResourcePrefetchPredictorTables::DeleteSingleResourceDataPoint( |
| 266 const std::string& key, | 236 const std::string& key, |
| 267 PrefetchKeyType key_type) { | 237 PrefetchKeyType key_type) { |
| 268 DCHECK_CURRENTLY_ON(BrowserThread::DB); | 238 DCHECK_CURRENTLY_ON(BrowserThread::DB); |
| 269 if (CantAccessDatabase()) | 239 if (CantAccessDatabase()) |
| 270 return; | 240 return; |
| 271 | 241 |
| 272 DeleteDataHelper(key_type, PrefetchDataType::RESOURCE, {key}); | 242 if (key_type == PREFETCH_KEY_TYPE_URL) |
| 243 url_resource_table_->DeleteData({key}); |
| 244 else |
| 245 host_resource_table_->DeleteData({key}); |
| 273 } | 246 } |
| 274 | 247 |
| 275 void ResourcePrefetchPredictorTables::DeleteRedirectData( | 248 void ResourcePrefetchPredictorTables::DeleteRedirectData( |
| 276 const std::vector<std::string>& urls, | 249 const std::vector<std::string>& urls, |
| 277 const std::vector<std::string>& hosts) { | 250 const std::vector<std::string>& hosts) { |
| 278 DCHECK_CURRENTLY_ON(BrowserThread::DB); | 251 DCHECK_CURRENTLY_ON(BrowserThread::DB); |
| 279 if (CantAccessDatabase()) | 252 if (CantAccessDatabase()) |
| 280 return; | 253 return; |
| 281 | 254 |
| 282 DCHECK(!urls.empty() || !hosts.empty()); | 255 DCHECK(!urls.empty() || !hosts.empty()); |
| 283 | 256 |
| 284 if (!urls.empty()) | 257 if (!urls.empty()) |
| 285 DeleteDataHelper(PREFETCH_KEY_TYPE_URL, PrefetchDataType::REDIRECT, urls); | 258 url_redirect_table_->DeleteData(urls); |
| 286 if (!hosts.empty()) | 259 if (!hosts.empty()) |
| 287 DeleteDataHelper(PREFETCH_KEY_TYPE_HOST, PrefetchDataType::REDIRECT, hosts); | 260 host_redirect_table_->DeleteData(hosts); |
| 288 } | 261 } |
| 289 | 262 |
| 290 void ResourcePrefetchPredictorTables::DeleteSingleRedirectDataPoint( | 263 void ResourcePrefetchPredictorTables::DeleteSingleRedirectDataPoint( |
| 291 const std::string& key, | 264 const std::string& key, |
| 292 PrefetchKeyType key_type) { | 265 PrefetchKeyType key_type) { |
| 293 DCHECK_CURRENTLY_ON(BrowserThread::DB); | 266 DCHECK_CURRENTLY_ON(BrowserThread::DB); |
| 294 if (CantAccessDatabase()) | 267 if (CantAccessDatabase()) |
| 295 return; | 268 return; |
| 296 | 269 |
| 297 DeleteDataHelper(key_type, PrefetchDataType::REDIRECT, {key}); | 270 if (key_type == PREFETCH_KEY_TYPE_URL) |
| 271 url_redirect_table_->DeleteData({key}); |
| 272 else |
| 273 host_redirect_table_->DeleteData({key}); |
| 298 } | 274 } |
| 299 | 275 |
| 300 void ResourcePrefetchPredictorTables::DeleteManifestData( | 276 void ResourcePrefetchPredictorTables::DeleteManifestData( |
| 301 const std::vector<std::string>& hosts) { | 277 const std::vector<std::string>& hosts) { |
| 302 DCHECK_CURRENTLY_ON(BrowserThread::DB); | 278 DCHECK_CURRENTLY_ON(BrowserThread::DB); |
| 303 if (CantAccessDatabase()) | 279 if (CantAccessDatabase()) |
| 304 return; | 280 return; |
| 305 | 281 |
| 306 DeleteDataHelper(PREFETCH_KEY_TYPE_HOST, PrefetchDataType::MANIFEST, hosts); | 282 manifest_table_->DeleteData(hosts); |
| 307 } | 283 } |
| 308 | 284 |
| 309 void ResourcePrefetchPredictorTables::DeleteOriginData( | 285 void ResourcePrefetchPredictorTables::DeleteOriginData( |
| 310 const std::vector<std::string>& hosts) { | 286 const std::vector<std::string>& hosts) { |
| 311 DCHECK_CURRENTLY_ON(BrowserThread::DB); | 287 DCHECK_CURRENTLY_ON(BrowserThread::DB); |
| 312 if (CantAccessDatabase()) | 288 if (CantAccessDatabase()) |
| 313 return; | 289 return; |
| 314 | 290 |
| 315 DeleteDataHelper(PREFETCH_KEY_TYPE_HOST, PrefetchDataType::ORIGIN, hosts); | 291 origin_table_->DeleteData(hosts); |
| 316 } | 292 } |
| 317 | 293 |
| 318 void ResourcePrefetchPredictorTables::DeleteAllData() { | 294 void ResourcePrefetchPredictorTables::DeleteAllData() { |
| 319 DCHECK_CURRENTLY_ON(BrowserThread::DB); | 295 DCHECK_CURRENTLY_ON(BrowserThread::DB); |
| 320 if (CantAccessDatabase()) | 296 if (CantAccessDatabase()) |
| 321 return; | 297 return; |
| 322 | 298 |
| 323 sql::Statement deleter; | 299 url_resource_table_->DeleteAllData(); |
| 324 for (const char* table_name : | 300 url_redirect_table_->DeleteAllData(); |
| 325 {kUrlResourceTableName, kUrlRedirectTableName, kHostResourceTableName, | 301 host_resource_table_->DeleteAllData(); |
| 326 kHostRedirectTableName, kManifestTableName}) { | 302 host_redirect_table_->DeleteAllData(); |
| 327 deleter.Assign(DB()->GetUniqueStatement( | 303 manifest_table_->DeleteAllData(); |
| 328 base::StringPrintf("DELETE FROM %s", table_name).c_str())); | 304 origin_table_->DeleteAllData(); |
| 329 deleter.Run(); | |
| 330 } | |
| 331 } | 305 } |
| 332 | 306 |
| 333 ResourcePrefetchPredictorTables::ResourcePrefetchPredictorTables() {} | 307 ResourcePrefetchPredictorTables::ResourcePrefetchPredictorTables() = default; |
| 334 | 308 |
| 335 ResourcePrefetchPredictorTables::~ResourcePrefetchPredictorTables() {} | 309 ResourcePrefetchPredictorTables::~ResourcePrefetchPredictorTables() = default; |
| 336 | |
| 337 void ResourcePrefetchPredictorTables::GetAllResourceDataHelper( | |
| 338 PrefetchKeyType key_type, | |
| 339 PrefetchDataMap* data_map) { | |
| 340 // Read the resources table and organize it per primary key. | |
| 341 const char* table_name = GetTableName(key_type, PrefetchDataType::RESOURCE); | |
| 342 sql::Statement resource_reader(DB()->GetUniqueStatement( | |
| 343 base::StringPrintf(kSelectAllStatementTemplate, table_name).c_str())); | |
| 344 | |
| 345 PrefetchData data; | |
| 346 std::string key; | |
| 347 while (StepAndInitializeProtoData(&resource_reader, &key, &data)) { | |
| 348 data_map->insert({key, data}); | |
| 349 DCHECK_EQ(data.primary_key(), key); | |
| 350 } | |
| 351 } | |
| 352 | |
| 353 void ResourcePrefetchPredictorTables::GetAllRedirectDataHelper( | |
| 354 PrefetchKeyType key_type, | |
| 355 RedirectDataMap* data_map) { | |
| 356 // Read the redirects table and organize it per primary key. | |
| 357 const char* table_name = GetTableName(key_type, PrefetchDataType::REDIRECT); | |
| 358 sql::Statement redirect_reader(DB()->GetUniqueStatement( | |
| 359 base::StringPrintf(kSelectAllStatementTemplate, table_name).c_str())); | |
| 360 | |
| 361 RedirectData data; | |
| 362 std::string key; | |
| 363 while (StepAndInitializeProtoData(&redirect_reader, &key, &data)) { | |
| 364 data_map->insert({key, data}); | |
| 365 DCHECK_EQ(data.primary_key(), key); | |
| 366 } | |
| 367 } | |
| 368 | |
| 369 void ResourcePrefetchPredictorTables::GetAllManifestDataHelper( | |
| 370 ManifestDataMap* manifest_map) { | |
| 371 sql::Statement manifest_reader(DB()->GetUniqueStatement( | |
| 372 base::StringPrintf(kSelectAllStatementTemplate, kManifestTableName) | |
| 373 .c_str())); | |
| 374 | |
| 375 precache::PrecacheManifest data; | |
| 376 std::string key; | |
| 377 while (StepAndInitializeProtoData(&manifest_reader, &key, &data)) { | |
| 378 manifest_map->insert({key, data}); | |
| 379 } | |
| 380 } | |
| 381 | |
| 382 void ResourcePrefetchPredictorTables::GetAllOriginDataHelper( | |
| 383 OriginDataMap* origin_map) { | |
| 384 sql::Statement reader(DB()->GetUniqueStatement( | |
| 385 base::StringPrintf(kSelectAllStatementTemplate, kOriginTableName) | |
| 386 .c_str())); | |
| 387 | |
| 388 OriginData data; | |
| 389 std::string key; | |
| 390 while (StepAndInitializeProtoData(&reader, &key, &data)) { | |
| 391 origin_map->insert({key, data}); | |
| 392 DCHECK_EQ(data.host(), key); | |
| 393 } | |
| 394 } | |
| 395 | |
| 396 void ResourcePrefetchPredictorTables::UpdateDataHelper( | |
| 397 PrefetchKeyType key_type, | |
| 398 PrefetchDataType data_type, | |
| 399 const std::string& key, | |
| 400 const MessageLite& data) { | |
| 401 DB()->BeginTransaction(); | |
| 402 | |
| 403 // Delete the older data from the table. | |
| 404 std::unique_ptr<sql::Statement> deleter( | |
| 405 GetTableUpdateStatement(key_type, data_type, TableOperationType::REMOVE)); | |
| 406 deleter->BindString(0, key); | |
| 407 bool success = deleter->Run(); | |
| 408 | |
| 409 if (success) { | |
| 410 // Add the new data to the table. | |
| 411 std::unique_ptr<sql::Statement> inserter(GetTableUpdateStatement( | |
| 412 key_type, data_type, TableOperationType::INSERT)); | |
| 413 BindProtoDataToStatement(key, data, inserter.get()); | |
| 414 success = inserter->Run(); | |
| 415 } | |
| 416 | |
| 417 if (!success) | |
| 418 DB()->RollbackTransaction(); | |
| 419 else | |
| 420 DB()->CommitTransaction(); | |
| 421 } | |
| 422 | |
| 423 void ResourcePrefetchPredictorTables::DeleteDataHelper( | |
| 424 PrefetchKeyType key_type, | |
| 425 PrefetchDataType data_type, | |
| 426 const std::vector<std::string>& keys) { | |
| 427 for (const std::string& key : keys) { | |
| 428 std::unique_ptr<sql::Statement> deleter(GetTableUpdateStatement( | |
| 429 key_type, data_type, TableOperationType::REMOVE)); | |
| 430 deleter->BindString(0, key); | |
| 431 deleter->Run(); | |
| 432 } | |
| 433 } | |
| 434 | 310 |
| 435 // static | 311 // static |
| 436 float ResourcePrefetchPredictorTables::ComputeResourceScore( | 312 float ResourcePrefetchPredictorTables::ComputeResourceScore( |
| 437 const ResourceData& data) { | 313 const ResourceData& data) { |
| 438 // The ranking is done by considering, in this order: | 314 // The ranking is done by considering, in this order: |
| 439 // 1. Resource Priority | 315 // 1. Resource Priority |
| 440 // 2. Request resource type | 316 // 2. Request resource type |
| 441 // 3. Finally, the average position, giving a higher priotity to earlier | 317 // 3. Finally, the average position, giving a higher priotity to earlier |
| 442 // resources. | 318 // resources. |
| 443 | 319 |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 .c_str())); | 455 .c_str())); |
| 580 } | 456 } |
| 581 | 457 |
| 582 if (success) | 458 if (success) |
| 583 success = db->CommitTransaction(); | 459 success = db->CommitTransaction(); |
| 584 else | 460 else |
| 585 db->RollbackTransaction(); | 461 db->RollbackTransaction(); |
| 586 | 462 |
| 587 if (!success) | 463 if (!success) |
| 588 ResetDB(); | 464 ResetDB(); |
| 465 |
| 466 if (success) { |
| 467 url_resource_table_ = base::MakeUnique<GlowplugKeyValueTable<PrefetchData>>( |
| 468 kUrlResourceTableName, db); |
| 469 url_redirect_table_ = base::MakeUnique<GlowplugKeyValueTable<RedirectData>>( |
| 470 kUrlRedirectTableName, db); |
| 471 host_resource_table_ = |
| 472 base::MakeUnique<GlowplugKeyValueTable<PrefetchData>>( |
| 473 kHostResourceTableName, db); |
| 474 host_redirect_table_ = |
| 475 base::MakeUnique<GlowplugKeyValueTable<RedirectData>>( |
| 476 kHostRedirectTableName, db); |
| 477 manifest_table_ = |
| 478 base::MakeUnique<GlowplugKeyValueTable<precache::PrecacheManifest>>( |
| 479 kManifestTableName, db); |
| 480 origin_table_ = base::MakeUnique<GlowplugKeyValueTable<OriginData>>( |
| 481 kOriginTableName, db); |
| 482 } |
| 589 } | 483 } |
| 590 | 484 |
| 591 void ResourcePrefetchPredictorTables::LogDatabaseStats() { | 485 void ResourcePrefetchPredictorTables::LogDatabaseStats() { |
| 592 DCHECK_CURRENTLY_ON(BrowserThread::DB); | 486 DCHECK_CURRENTLY_ON(BrowserThread::DB); |
| 593 if (CantAccessDatabase()) | 487 if (CantAccessDatabase()) |
| 594 return; | 488 return; |
| 595 | 489 |
| 596 sql::Statement statement(DB()->GetUniqueStatement( | 490 sql::Statement statement(DB()->GetUniqueStatement( |
| 597 base::StringPrintf("SELECT count(*) FROM %s", kUrlResourceTableName) | 491 base::StringPrintf("SELECT count(*) FROM %s", kUrlResourceTableName) |
| 598 .c_str())); | 492 .c_str())); |
| 599 if (statement.Step()) | 493 if (statement.Step()) |
| 600 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.UrlTableRowCount2", | 494 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.UrlTableRowCount2", |
| 601 statement.ColumnInt(0)); | 495 statement.ColumnInt(0)); |
| 602 | 496 |
| 603 statement.Assign(DB()->GetUniqueStatement( | 497 statement.Assign(DB()->GetUniqueStatement( |
| 604 base::StringPrintf("SELECT count(*) FROM %s", kHostResourceTableName) | 498 base::StringPrintf("SELECT count(*) FROM %s", kHostResourceTableName) |
| 605 .c_str())); | 499 .c_str())); |
| 606 if (statement.Step()) | 500 if (statement.Step()) |
| 607 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HostTableRowCount2", | 501 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HostTableRowCount2", |
| 608 statement.ColumnInt(0)); | 502 statement.ColumnInt(0)); |
| 609 } | 503 } |
| 610 | 504 |
| 611 std::unique_ptr<sql::Statement> | |
| 612 ResourcePrefetchPredictorTables::GetTableUpdateStatement( | |
| 613 PrefetchKeyType key_type, | |
| 614 PrefetchDataType data_type, | |
| 615 TableOperationType op_type) { | |
| 616 sql::StatementID id(__FILE__, key_type | (static_cast<int>(data_type) << 1) | | |
| 617 (static_cast<int>(op_type) << 3)); | |
| 618 const char* statement_template = | |
| 619 (op_type == TableOperationType::REMOVE ? kDeleteProtoStatementTemplate | |
| 620 : kInsertProtoStatementTemplate); | |
| 621 const char* table_name = GetTableName(key_type, data_type); | |
| 622 return base::MakeUnique<sql::Statement>(DB()->GetCachedStatement( | |
| 623 id, base::StringPrintf(statement_template, table_name).c_str())); | |
| 624 } | |
| 625 | |
| 626 // static | |
| 627 const char* ResourcePrefetchPredictorTables::GetTableName( | |
| 628 PrefetchKeyType key_type, | |
| 629 PrefetchDataType data_type) { | |
| 630 bool is_host = key_type == PREFETCH_KEY_TYPE_HOST; | |
| 631 switch (data_type) { | |
| 632 case PrefetchDataType::RESOURCE: | |
| 633 return is_host ? kHostResourceTableName : kUrlResourceTableName; | |
| 634 case PrefetchDataType::REDIRECT: | |
| 635 return is_host ? kHostRedirectTableName : kUrlRedirectTableName; | |
| 636 case PrefetchDataType::MANIFEST: | |
| 637 return kManifestTableName; | |
| 638 case PrefetchDataType::ORIGIN: | |
| 639 return kOriginTableName; | |
| 640 } | |
| 641 | |
| 642 NOTREACHED(); | |
| 643 return nullptr; | |
| 644 } | |
| 645 | |
| 646 } // namespace predictors | 505 } // namespace predictors |
| OLD | NEW |