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 b56ed4cf37d943a9a481f8902cb47b1a42016397..5140fb832e49b0b714a6d4e60a6b9c92b86ef547 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; |
|
pasko
2016/09/26 14:18:23
Please put a comment above these declarations:
//
alexilin
2016/09/26 15:38:29
Done.
|
| +using chrome_browser_predictors::RedirectData; |
| +using RedirectStat = chrome_browser_predictors::RedirectData::RedirectStat; |
| // Interface for database tables used by the ResourcePrefetchPredictor. |
| // All methods except the constructor and destructor need to be called on the DB |
| @@ -36,13 +38,12 @@ 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: |
| - // Sorts the resources by score, decreasing. |
| - static void SortResources(std::vector<ResourceData>* resources); |
| - |
| // 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 |
| // it allows us to use the same functions. |
| @@ -63,58 +64,96 @@ class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| // Map from primary key to PrefetchData for the key. |
| typedef std::map<std::string, PrefetchData> PrefetchDataMap; |
| + // 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(); |
| + // Sorts the resources by score, decreasing. |
| + static void SortResources(std::vector<ResourceData>* resources); |
| + |
| + // Sorts the redirects by score, decreasing. |
| + static void SortRedirects(std::vector<RedirectStat>* redirects); |
| + |
| // The maximum length of the string that can be stored in the DB. |
| static constexpr size_t kMaxStringLength = 1024; |
| private: |
| + // Represents the type of information that is stored in prefetch database. |
| + enum PrefetchDataType { |
| + PREFETCH_DATA_TYPE_RESOURCE, |
| + PREFETCH_DATA_TYPE_REDIRECT |
| + }; |
| + |
| friend class PredictorDatabaseInternal; |
| friend class MockResourcePrefetchPredictorTables; |
| FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| DatabaseVersionIsSet); |
| FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| DatabaseIsResetWhenIncompatible); |
| - FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, ComputeScore); |
| + FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| + ComputeResourceScore); |
| ResourcePrefetchPredictorTables(); |
| ~ResourcePrefetchPredictorTables() override; |
| // 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 GetAllResourceDataHelper(PrefetchKeyType key_type, |
| + PrefetchDataMap* data_map, |
| + std::vector<std::string>* to_delete); |
| + void GetAllRedirectDataHelper(PrefetchKeyType key_type, |
| + RedirectDataMap* redirect_map); |
| + bool UpdateResourceDataHelper(PrefetchKeyType key_type, |
| + const PrefetchData& data); |
| + bool UpdateRedirectDataHelper(PrefetchKeyType key_type, |
| + const RedirectData& data); |
| void DeleteDataHelper(PrefetchKeyType key_type, |
| + PrefetchDataType data_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); |
| // Computes score of |data|. |
| - static float ComputeScore(const ResourceData& data); |
| + static float ComputeResourceScore(const ResourceData& data); |
| + static float ComputeRedirectScore(const RedirectStat& data); |
| // PredictorTableBase methods. |
| void CreateTableIfNonExistent() override; |
| @@ -122,7 +161,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); |
| @@ -134,11 +173,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); |
| }; |