Chromium Code Reviews| Index: chrome/browser/predictors/resource_prefetch_predictor_tables.h |
| diff --git a/chrome/browser/predictors/resource_prefetch_predictor_tables.h b/chrome/browser/predictors/resource_prefetch_predictor_tables.h |
| index 953c23f1c553437fb7354050c2bc7bb7b3a2c521..bc63d5c095e9e48b90178777c937b79bd589f9b6 100644 |
| --- a/chrome/browser/predictors/resource_prefetch_predictor_tables.h |
| +++ b/chrome/browser/predictors/resource_prefetch_predictor_tables.h |
| @@ -28,6 +28,8 @@ class Statement; |
| namespace predictors { |
| using chrome_browser_predictors::ResourceData; |
| +using chrome_browser_predictors::RedirectProto; |
| +using RedirectStat = chrome_browser_predictors::RedirectProto::RedirectStat; |
| // Interface for database tables used by the ResourcePrefetchPredictor. |
| // All methods except the constructor and destructor need to be called on the DB |
| @@ -36,8 +38,10 @@ using chrome_browser_predictors::ResourceData; |
| // Currently manages: |
| // - UrlResourceTable - resources per Urls. |
| // - UrlMetadataTable - misc data for Urls (like last visit time). |
| +// - UrlRedirectTable - redirects per Urls. |
| // - HostResourceTable - resources per host. |
| // - HostMetadataTable - misc data for hosts. |
| +// - HostRedirectTable - redirects per host. |
| class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| public: |
| // Used in the UrlResourceTable and HostResourceTable to store resources |
| @@ -72,10 +76,9 @@ class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| // Not stored. |
| float score; |
| }; |
| - typedef std::vector<ResourceRow> ResourceRows; |
| // Sorts the resource rows by score, decreasing. |
| - static void SortResourceRows(ResourceRows* rows); |
| + static void SortResourceRows(std::vector<ResourceRow>* rows); |
| // Aggregated data for a Url or Host. Although the data differs slightly, we |
| // store them in the same structure, because most of the fields are common and |
| @@ -93,29 +96,89 @@ class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| std::string primary_key; // is_host() ? main frame url : host. |
| base::Time last_visit; |
| - ResourceRows resources; |
| + std::vector<ResourceRow> resources; |
| }; |
| // Map from primary key to PrefetchData for the key. |
| typedef std::map<std::string, PrefetchData> PrefetchDataMap; |
| + // Used in the UrlRedirectTable and HostRedirectTable to store redirects |
|
mattcary
2016/09/15 09:40:47
For example of how to simplify everything, the onl
alexilin
2016/09/15 10:51:49
I didn't consider that the use of proto-generated
mattcary
2016/09/15 11:44:27
Yes, exactly. This is a pretty big pro. All the bo
alexilin
2016/09/15 17:56:16
Yeah, now I see that this is even good if we treat
|
| + // required for the page or host. |
| + struct RedirectRow { |
| + RedirectRow(); |
| + RedirectRow(const RedirectRow& other); |
| + RedirectRow(const std::string& url, |
| + size_t number_of_hits, |
| + size_t number_of_misses, |
| + size_t consecutive_misses); |
| + void UpdateScore(); |
| + bool operator==(const RedirectRow& rhs) const; |
| + static void FromProto(const RedirectStat& proto, RedirectRow* row); |
| + void ToProto(RedirectStat* proto) const; |
| + |
| + std::string url; |
| + size_t number_of_hits; |
| + size_t number_of_misses; |
| + size_t consecutive_misses; |
| + |
| + // Not stored. |
| + float score; |
| + }; |
| + |
| + // Sorts the redirect rows by score, decreasing. |
| + static void SortRedirectRows(std::vector<RedirectRow>* rows); |
| + |
| + // Aggregated data about redirections for a Url or Host. |
| + struct RedirectData { |
| + RedirectData(PrefetchKeyType key_type, |
| + const std::string& primary_key = std::string()); |
| + RedirectData(const RedirectData& other); |
| + ~RedirectData(); |
| + bool operator==(const RedirectData& rhs) const; |
| + static void FromProto(const RedirectProto& proto, RedirectData* data); |
| + void ToProto(RedirectProto* proto) const; |
| + |
| + bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } |
| + |
| + PrefetchKeyType key_type; |
| + std::string primary_key; |
| + base::Time last_visit; |
| + std::vector<RedirectRow> redirects; |
| + }; |
| + // Map from primary key to RedirectData for the key. |
| + typedef std::map<std::string, RedirectData> RedirectDataMap; |
| + |
| // Returns data for all Urls and Hosts. |
| virtual void GetAllData(PrefetchDataMap* url_data_map, |
| - PrefetchDataMap* host_data_map); |
| + PrefetchDataMap* host_data_map, |
| + RedirectDataMap* url_redirect_data_map, |
| + RedirectDataMap* host_redirect_data_map); |
| // Updates data for a Url and a host. If either of the |url_data| or |
| - // |host_data| has an empty primary key, it will be ignored. |
| - // Note that the Urls and primary key in |url_data| and |host_data| should be |
| - // less than |kMaxStringLength| in length. |
| + // |host_data| or |url_redirect_data| or |host_redirect_data| has an empty |
| + // primary key, it will be ignored. |
| + // Note that the Urls and primary key in |url_data|, |host_data|, |
| + // |url_redirect_data| and |host_redirect_data| should be less than |
| + // |kMaxStringLength| in length. |
| virtual void UpdateData(const PrefetchData& url_data, |
| - const PrefetchData& host_data); |
| + const PrefetchData& host_data, |
| + const RedirectData& url_redirect_data, |
| + const RedirectData& host_redirect_data); |
| + |
| + // Delete data for the input |urls| and |hosts|. |
| + virtual void DeleteResourceData(const std::vector<std::string>& urls, |
| + const std::vector<std::string>& hosts); |
| + |
| + // Wrapper over DeleteResourceData for convenience. |
| + virtual void DeleteSingleResourceDataPoint(const std::string& key, |
| + PrefetchKeyType key_type); |
| // Delete data for the input |urls| and |hosts|. |
| - virtual void DeleteData(const std::vector<std::string>& urls, |
| - const std::vector<std::string>& hosts); |
| + virtual void DeleteRedirectData(const std::vector<std::string>& urls, |
| + const std::vector<std::string>& hosts); |
| - // Wrapper over DeleteData for convenience. |
| - virtual void DeleteSingleDataPoint(const std::string& key, |
| - PrefetchKeyType key_type); |
| + // Wrapper over DeleteRedirectData for convenience. |
| + virtual void DeleteSingleRedirectDataPoint(const std::string& key, |
| + PrefetchKeyType key_type); |
| // Deletes all data in all the tables. |
| virtual void DeleteAllData(); |
| @@ -136,16 +199,22 @@ class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| // Helper functions below help perform functions on the Url and host table |
| // using the same code. |
| - void GetAllDataHelper(PrefetchKeyType key_type, |
| - PrefetchDataMap* data_map, |
| - std::vector<std::string>* to_delete); |
| - bool UpdateDataHelper(const PrefetchData& data); |
| - void DeleteDataHelper(PrefetchKeyType key_type, |
| - const std::vector<std::string>& keys); |
| + void GetAllResourceDataHelper(PrefetchKeyType key_type, |
| + PrefetchDataMap* data_map, |
| + std::vector<std::string>* to_delete); |
| + void GetAllRedirectDataHelper(PrefetchKeyType key_type, |
| + RedirectDataMap* redirect_map); |
| + bool UpdateResourceDataHelper(const PrefetchData& data); |
| + bool UpdateRedirectDataHelper(const RedirectData& data); |
| + void DeleteResourceDataHelper(PrefetchKeyType key_type, |
| + const std::vector<std::string>& keys); |
| + void DeleteRedirectDataHelper(PrefetchKeyType key_type, |
| + const std::vector<std::string>& keys); |
| // Returns true if the strings in the |data| are less than |kMaxStringLength| |
| // in length. |
| static bool StringsAreSmallerThanDBLimit(const PrefetchData& data); |
| + static bool StringsAreSmallerThanDBLimit(const RedirectData& data); |
| // PredictorTableBase methods. |
| void CreateTableIfNonExistent() override; |
| @@ -153,7 +222,7 @@ class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| // Database version. Always increment it when any change is made to the data |
| // schema (including the .proto). |
| - static constexpr int kDatabaseVersion = 2; |
| + static constexpr int kDatabaseVersion = 3; |
| static bool DropTablesIfOutdated(sql::Connection* db); |
| static int GetDatabaseVersion(sql::Connection* db); |
| @@ -165,11 +234,15 @@ class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| sql::Statement* GetUrlResourceUpdateStatement(); |
| sql::Statement* GetUrlMetadataDeleteStatement(); |
| sql::Statement* GetUrlMetadataUpdateStatement(); |
| + sql::Statement* GetUrlRedirectDeleteStatement(); |
| + sql::Statement* GetUrlRedirectUpdateStatement(); |
| sql::Statement* GetHostResourceDeleteStatement(); |
| sql::Statement* GetHostResourceUpdateStatement(); |
| sql::Statement* GetHostMetadataDeleteStatement(); |
| sql::Statement* GetHostMetadataUpdateStatement(); |
| + sql::Statement* GetHostRedirectDeleteStatement(); |
| + sql::Statement* GetHostRedirectUpdateStatement(); |
| DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); |
| }; |