| Index: chrome/browser/predictors/resource_prefetch_predictor_tables.cc
|
| diff --git a/chrome/browser/predictors/resource_prefetch_predictor_tables.cc b/chrome/browser/predictors/resource_prefetch_predictor_tables.cc
|
| index a3dd8c1e429adf005511c75f364ad9f0f19045ca..2ed345ed024eae9763bba173c6c434fc58addca5 100644
|
| --- a/chrome/browser/predictors/resource_prefetch_predictor_tables.cc
|
| +++ b/chrome/browser/predictors/resource_prefetch_predictor_tables.cc
|
| @@ -16,13 +16,12 @@
|
| #include "content/public/browser/browser_thread.h"
|
| #include "sql/statement.h"
|
|
|
| +using chrome_browser_predictors::ResourceData;
|
| using content::BrowserThread;
|
| using sql::Statement;
|
|
|
| namespace {
|
|
|
| -using ResourceRow = predictors::ResourcePrefetchPredictorTables::ResourceRow;
|
| -
|
| const char kUrlResourceTableName[] = "resource_prefetch_predictor_url";
|
| const char kUrlMetadataTableName[] = "resource_prefetch_predictor_url_metadata";
|
| const char kHostResourceTableName[] = "resource_prefetch_predictor_host";
|
| @@ -35,33 +34,31 @@ const char kInsertMetadataTableStatementTemplate[] =
|
| "INSERT INTO %s (main_page_url, last_visit_time) VALUES (?,?)";
|
| const char kDeleteStatementTemplate[] = "DELETE FROM %s WHERE main_page_url=?";
|
|
|
| -void BindResourceRowToStatement(const ResourceRow& row,
|
| - const std::string& primary_key,
|
| - Statement* statement) {
|
| - chrome_browser_predictors::ResourceData proto;
|
| - row.ToProto(&proto);
|
| - int size = proto.ByteSize();
|
| - std::vector<char> proto_buffer(size);
|
| - proto.SerializeToArray(&proto_buffer[0], size);
|
| +void BindToStatement(const ResourceData& resource,
|
| + const std::string& primary_key,
|
| + Statement* statement) {
|
| + int size = resource.ByteSize();
|
| + std::vector<char> buffer(size);
|
| + resource.SerializeToArray(&buffer[0], size);
|
|
|
| statement->BindString(0, primary_key);
|
| - statement->BindString(1, row.resource_url.spec());
|
| - statement->BindBlob(2, &proto_buffer[0], size);
|
| + statement->BindString(1, resource.resource_url());
|
| + statement->BindBlob(2, &buffer[0], size);
|
| }
|
|
|
| -bool StepAndInitializeResourceRow(Statement* statement, ResourceRow* row) {
|
| +bool StepAndInitializeResource(Statement* statement, ResourceData* resource) {
|
| if (!statement->Step())
|
| return false;
|
| + std::string primary_key = statement->ColumnString(0);
|
| + std::string resource_url = statement->ColumnString(1);
|
|
|
| int size = statement->ColumnByteLength(2);
|
| const void* data = statement->ColumnBlob(2);
|
| DCHECK(data);
|
| - chrome_browser_predictors::ResourceData proto;
|
| - proto.ParseFromArray(data, size);
|
| - ResourceRow::FromProto(proto, row);
|
| + resource->ParseFromArray(data, size);
|
| + resource->set_primary_key(primary_key);
|
| + DCHECK(resource_url == resource->resource_url());
|
|
|
| - row->primary_key = statement->ColumnString(0);
|
| - row->resource_url = GURL(statement->ColumnString(1));
|
| return true;
|
| }
|
|
|
| @@ -70,127 +67,39 @@ bool StepAndInitializeResourceRow(Statement* statement, ResourceRow* row) {
|
| namespace predictors {
|
|
|
| // static
|
| -const size_t ResourcePrefetchPredictorTables::kMaxStringLength = 1024;
|
| -
|
| -ResourceRow::ResourceRow()
|
| - : resource_type(content::RESOURCE_TYPE_LAST_TYPE),
|
| - number_of_hits(0),
|
| - number_of_misses(0),
|
| - consecutive_misses(0),
|
| - average_position(0.0),
|
| - priority(net::IDLE),
|
| - has_validators(false),
|
| - always_revalidate(false),
|
| - score(0.0) {}
|
| -
|
| -ResourceRow::ResourceRow(const ResourceRow& other)
|
| - : primary_key(other.primary_key),
|
| - resource_url(other.resource_url),
|
| - resource_type(other.resource_type),
|
| - number_of_hits(other.number_of_hits),
|
| - number_of_misses(other.number_of_misses),
|
| - consecutive_misses(other.consecutive_misses),
|
| - average_position(other.average_position),
|
| - priority(other.priority),
|
| - has_validators(other.has_validators),
|
| - always_revalidate(other.always_revalidate),
|
| - score(other.score) {}
|
| -
|
| -ResourceRow::ResourceRow(const std::string& i_primary_key,
|
| - const std::string& i_resource_url,
|
| - content::ResourceType i_resource_type,
|
| - int i_number_of_hits,
|
| - int i_number_of_misses,
|
| - int i_consecutive_misses,
|
| - double i_average_position,
|
| - net::RequestPriority i_priority,
|
| - bool i_has_validators,
|
| - bool i_always_revalidate)
|
| - : primary_key(i_primary_key),
|
| - resource_url(i_resource_url),
|
| - resource_type(i_resource_type),
|
| - number_of_hits(i_number_of_hits),
|
| - number_of_misses(i_number_of_misses),
|
| - consecutive_misses(i_consecutive_misses),
|
| - average_position(i_average_position),
|
| - priority(i_priority),
|
| - has_validators(i_has_validators),
|
| - always_revalidate(i_always_revalidate) {
|
| - UpdateScore();
|
| +void ResourcePrefetchPredictorTables::SortResources(
|
| + std::vector<ResourceData>* resources) {
|
| + std::sort(resources->begin(), resources->end(),
|
| + [](const ResourceData& x, const ResourceData& y) {
|
| + return x.score() > y.score();
|
| + });
|
| }
|
|
|
| -void ResourceRow::UpdateScore() {
|
| +// static
|
| +void ResourcePrefetchPredictorTables::UpdateResourceScore(
|
| + ResourceData* resource) {
|
| // The score is calculated so that when the rows are sorted, stylesheets,
|
| // scripts and fonts appear first, sorted by position(ascending) and then the
|
| // rest of the resources sorted by position (ascending).
|
| static const int kMaxResourcesPerType = 100;
|
| + content::ResourceType resource_type =
|
| + static_cast<content::ResourceType>(resource->resource_type());
|
| switch (resource_type) {
|
| case content::RESOURCE_TYPE_STYLESHEET:
|
| case content::RESOURCE_TYPE_SCRIPT:
|
| case content::RESOURCE_TYPE_FONT_RESOURCE:
|
| - score = (2 * kMaxResourcesPerType) - average_position;
|
| + resource->set_score((2 * kMaxResourcesPerType) -
|
| + resource->average_position());
|
| break;
|
|
|
| case content::RESOURCE_TYPE_IMAGE:
|
| default:
|
| - score = kMaxResourcesPerType - average_position;
|
| + resource->set_score(kMaxResourcesPerType - resource->average_position());
|
| break;
|
| }
|
| // TODO(lizeb): Take priority into account.
|
| }
|
|
|
| -bool ResourceRow::operator==(const ResourceRow& rhs) const {
|
| - return primary_key == rhs.primary_key && resource_url == rhs.resource_url &&
|
| - resource_type == rhs.resource_type &&
|
| - number_of_hits == rhs.number_of_hits &&
|
| - number_of_misses == rhs.number_of_misses &&
|
| - consecutive_misses == rhs.consecutive_misses &&
|
| - average_position == rhs.average_position && priority == rhs.priority &&
|
| - has_validators == rhs.has_validators &&
|
| - always_revalidate == rhs.always_revalidate && score == rhs.score;
|
| -}
|
| -
|
| -void ResourceRow::ToProto(ResourceData* resource_data) const {
|
| - using chrome_browser_predictors::ResourceData_Priority;
|
| - using chrome_browser_predictors::ResourceData_ResourceType;
|
| -
|
| - resource_data->set_primary_key(primary_key);
|
| - resource_data->set_resource_url(resource_url.spec());
|
| - resource_data->set_resource_type(
|
| - static_cast<ResourceData_ResourceType>(resource_type));
|
| - resource_data->set_number_of_hits(number_of_hits);
|
| - resource_data->set_number_of_misses(number_of_misses);
|
| - resource_data->set_consecutive_misses(consecutive_misses);
|
| - resource_data->set_average_position(average_position);
|
| - resource_data->set_priority(static_cast<ResourceData_Priority>(priority));
|
| - resource_data->set_has_validators(has_validators);
|
| - resource_data->set_always_revalidate(always_revalidate);
|
| -}
|
| -
|
| -// static
|
| -void ResourceRow::FromProto(const ResourceData& proto, ResourceRow* row) {
|
| - DCHECK(proto.has_primary_key());
|
| - row->primary_key = proto.primary_key();
|
| - row->resource_url = GURL(proto.resource_url());
|
| - row->resource_type =
|
| - static_cast<content::ResourceType>(proto.resource_type());
|
| - row->number_of_hits = proto.number_of_hits();
|
| - row->number_of_misses = proto.number_of_misses();
|
| - row->consecutive_misses = proto.consecutive_misses();
|
| - row->average_position = proto.average_position();
|
| - row->priority = static_cast<net::RequestPriority>(proto.priority());
|
| - row->has_validators = proto.has_validators();
|
| - row->always_revalidate = proto.always_revalidate();
|
| -}
|
| -
|
| -// static
|
| -void ResourcePrefetchPredictorTables::SortResourceRows(ResourceRows* rows) {
|
| - std::sort(rows->begin(), rows->end(),
|
| - [](const ResourceRow& x, const ResourceRow& y) {
|
| - return x.score > y.score;
|
| - });
|
| -}
|
| -
|
| ResourcePrefetchPredictorTables::PrefetchData::PrefetchData(
|
| PrefetchKeyType i_key_type,
|
| const std::string& i_primary_key)
|
| @@ -206,14 +115,7 @@ ResourcePrefetchPredictorTables::PrefetchData::PrefetchData(
|
| resources(other.resources) {
|
| }
|
|
|
| -ResourcePrefetchPredictorTables::PrefetchData::~PrefetchData() {
|
| -}
|
| -
|
| -bool ResourcePrefetchPredictorTables::PrefetchData::operator==(
|
| - const PrefetchData& rhs) const {
|
| - return key_type == rhs.key_type && primary_key == rhs.primary_key &&
|
| - resources == rhs.resources;
|
| -}
|
| +ResourcePrefetchPredictorTables::PrefetchData::~PrefetchData() {}
|
|
|
| void ResourcePrefetchPredictorTables::GetAllData(
|
| PrefetchDataMap* url_data_map,
|
| @@ -284,18 +186,14 @@ void ResourcePrefetchPredictorTables::DeleteAllData() {
|
| if (CantAccessDatabase())
|
| return;
|
|
|
| - Statement deleter(DB()->GetUniqueStatement(
|
| - base::StringPrintf("DELETE FROM %s", kUrlResourceTableName).c_str()));
|
| - deleter.Run();
|
| - deleter.Assign(DB()->GetUniqueStatement(
|
| - base::StringPrintf("DELETE FROM %s", kUrlMetadataTableName).c_str()));
|
| - deleter.Run();
|
| - deleter.Assign(DB()->GetUniqueStatement(
|
| - base::StringPrintf("DELETE FROM %s", kHostResourceTableName).c_str()));
|
| - deleter.Run();
|
| - deleter.Assign(DB()->GetUniqueStatement(
|
| - base::StringPrintf("DELETE FROM %s", kHostMetadataTableName).c_str()));
|
| - deleter.Run();
|
| + Statement deleter;
|
| + for (const char* table_name :
|
| + {kUrlResourceTableName, kUrlMetadataTableName, kHostResourceTableName,
|
| + kHostMetadataTableName}) {
|
| + deleter.Assign(DB()->GetUniqueStatement(
|
| + base::StringPrintf("DELETE FROM %s", table_name).c_str()));
|
| + deleter.Run();
|
| + }
|
| }
|
|
|
| ResourcePrefetchPredictorTables::ResourcePrefetchPredictorTables()
|
| @@ -317,24 +215,23 @@ void ResourcePrefetchPredictorTables::GetAllDataHelper(
|
| Statement resource_reader(DB()->GetUniqueStatement(
|
| base::StringPrintf("SELECT * FROM %s", resource_table_name).c_str()));
|
|
|
| - ResourceRow row;
|
| - while (StepAndInitializeResourceRow(&resource_reader, &row)) {
|
| - row.UpdateScore();
|
| - std::string primary_key = row.primary_key;
|
| + ResourceData resource;
|
| + while (StepAndInitializeResource(&resource_reader, &resource)) {
|
| + UpdateResourceScore(&resource);
|
| + std::string primary_key = resource.primary_key();
|
| // Don't need to store primary key since the data is grouped by primary key.
|
| - row.primary_key.clear();
|
| -
|
| + resource.clear_primary_key();
|
| PrefetchDataMap::iterator it = data_map->find(primary_key);
|
| if (it == data_map->end()) {
|
| it = data_map->insert(std::make_pair(
|
| primary_key, PrefetchData(key_type, primary_key))).first;
|
| }
|
| - it->second.resources.push_back(row);
|
| + it->second.resources.push_back(resource);
|
| }
|
|
|
| // Sort each of the resource row vectors by score.
|
| for (auto& kv : *data_map)
|
| - SortResourceRows(&(kv.second.resources));
|
| + SortResources(&(kv.second.resources));
|
|
|
| // Read the metadata and keep track of entries that have metadata, but no
|
| // resource entries, so they can be deleted.
|
| @@ -380,13 +277,11 @@ bool ResourcePrefetchPredictorTables::UpdateDataHelper(
|
| return false;
|
|
|
| // Add the new data to the tables.
|
| - const ResourceRows& resources = data.resources;
|
| - for (const ResourceRow& resource : resources) {
|
| + for (const ResourceData& resource : data.resources) {
|
| std::unique_ptr<Statement> resource_inserter(
|
| data.is_host() ? GetHostResourceUpdateStatement()
|
| : GetUrlResourceUpdateStatement());
|
| - BindResourceRowToStatement(resource, data.primary_key,
|
| - resource_inserter.get());
|
| + BindToStatement(resource, data.primary_key, resource_inserter.get());
|
| if (!resource_inserter->Run())
|
| return false;
|
| }
|
| @@ -407,29 +302,28 @@ void ResourcePrefetchPredictorTables::DeleteDataHelper(
|
| const std::vector<std::string>& keys) {
|
| bool is_host = key_type == PREFETCH_KEY_TYPE_HOST;
|
|
|
| - for (std::vector<std::string>::const_iterator it = keys.begin();
|
| - it != keys.end(); ++it) {
|
| + for (const std::string& key : keys) {
|
| std::unique_ptr<Statement> deleter(is_host
|
| ? GetHostResourceDeleteStatement()
|
| : GetUrlResourceDeleteStatement());
|
| - deleter->BindString(0, *it);
|
| + deleter->BindString(0, key);
|
| deleter->Run();
|
|
|
| - deleter.reset(is_host ? GetHostMetadataDeleteStatement() :
|
| - GetUrlMetadataDeleteStatement());
|
| - deleter->BindString(0, *it);
|
| + deleter.reset(is_host ? GetHostMetadataDeleteStatement()
|
| + : GetUrlMetadataDeleteStatement());
|
| + deleter->BindString(0, key);
|
| deleter->Run();
|
| }
|
| }
|
|
|
| +// static
|
| bool ResourcePrefetchPredictorTables::StringsAreSmallerThanDBLimit(
|
| - const PrefetchData& data) const {
|
| + const PrefetchData& data) {
|
| if (data.primary_key.length() > kMaxStringLength)
|
| return false;
|
|
|
| - for (ResourceRows::const_iterator it = data.resources.begin();
|
| - it != data.resources.end(); ++it) {
|
| - if (it->resource_url.spec().length() > kMaxStringLength)
|
| + for (const ResourceData& resource : data.resources) {
|
| + if (resource.resource_url().length() > kMaxStringLength)
|
| return false;
|
| }
|
| return true;
|
|
|