Chromium Code Reviews| 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 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 5 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
| 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "chrome/browser/predictors/predictor_table_base.h" | 17 #include "chrome/browser/predictors/predictor_table_base.h" |
| 18 #include "chrome/browser/predictors/resource_prefetch_common.h" | 18 #include "chrome/browser/predictors/resource_prefetch_common.h" |
| 19 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" | 19 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" |
| 20 #include "content/public/common/resource_type.h" | 20 #include "content/public/common/resource_type.h" |
| 21 #include "net/base/request_priority.h" | 21 #include "net/base/request_priority.h" |
| 22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 23 | 23 |
| 24 namespace sql { | 24 namespace sql { |
| 25 class Statement; | 25 class Statement; |
| 26 } | 26 } |
| 27 | 27 |
| 28 namespace predictors { | 28 namespace predictors { |
| 29 | 29 |
| 30 using chrome_browser_predictors::ResourceData; | 30 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.
| |
| 31 using chrome_browser_predictors::RedirectData; | |
| 32 using RedirectStat = chrome_browser_predictors::RedirectData::RedirectStat; | |
| 31 | 33 |
| 32 // Interface for database tables used by the ResourcePrefetchPredictor. | 34 // Interface for database tables used by the ResourcePrefetchPredictor. |
| 33 // All methods except the constructor and destructor need to be called on the DB | 35 // All methods except the constructor and destructor need to be called on the DB |
| 34 // thread. | 36 // thread. |
| 35 // | 37 // |
| 36 // Currently manages: | 38 // Currently manages: |
| 37 // - UrlResourceTable - resources per Urls. | 39 // - UrlResourceTable - resources per Urls. |
| 38 // - UrlMetadataTable - misc data for Urls (like last visit time). | 40 // - UrlMetadataTable - misc data for Urls (like last visit time). |
| 41 // - UrlRedirectTable - redirects per Urls. | |
| 39 // - HostResourceTable - resources per host. | 42 // - HostResourceTable - resources per host. |
| 40 // - HostMetadataTable - misc data for hosts. | 43 // - HostMetadataTable - misc data for hosts. |
| 44 // - HostRedirectTable - redirects per host. | |
| 41 class ResourcePrefetchPredictorTables : public PredictorTableBase { | 45 class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| 42 public: | 46 public: |
| 43 // Sorts the resources by score, decreasing. | |
| 44 static void SortResources(std::vector<ResourceData>* resources); | |
| 45 | |
| 46 // Aggregated data for a Url or Host. Although the data differs slightly, we | 47 // Aggregated data for a Url or Host. Although the data differs slightly, we |
| 47 // store them in the same structure, because most of the fields are common and | 48 // store them in the same structure, because most of the fields are common and |
| 48 // it allows us to use the same functions. | 49 // it allows us to use the same functions. |
| 49 struct PrefetchData { | 50 struct PrefetchData { |
| 50 PrefetchData(PrefetchKeyType key_type, const std::string& primary_key); | 51 PrefetchData(PrefetchKeyType key_type, const std::string& primary_key); |
| 51 PrefetchData(const PrefetchData& other); | 52 PrefetchData(const PrefetchData& other); |
| 52 ~PrefetchData(); | 53 ~PrefetchData(); |
| 53 | 54 |
| 54 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } | 55 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } |
| 55 | 56 |
| 56 // Is the data a host as opposed to a Url? | 57 // Is the data a host as opposed to a Url? |
| 57 PrefetchKeyType key_type; // Not const to be able to assign. | 58 PrefetchKeyType key_type; // Not const to be able to assign. |
| 58 std::string primary_key; // is_host() ? host : main frame url. | 59 std::string primary_key; // is_host() ? host : main frame url. |
| 59 | 60 |
| 60 base::Time last_visit; | 61 base::Time last_visit; |
| 61 std::vector<ResourceData> resources; | 62 std::vector<ResourceData> resources; |
| 62 }; | 63 }; |
| 63 // Map from primary key to PrefetchData for the key. | 64 // Map from primary key to PrefetchData for the key. |
| 64 typedef std::map<std::string, PrefetchData> PrefetchDataMap; | 65 typedef std::map<std::string, PrefetchData> PrefetchDataMap; |
| 65 | 66 |
| 67 // Map from primary key to RedirectData for the key. | |
| 68 typedef std::map<std::string, RedirectData> RedirectDataMap; | |
| 69 | |
| 66 // Returns data for all Urls and Hosts. | 70 // Returns data for all Urls and Hosts. |
| 67 virtual void GetAllData(PrefetchDataMap* url_data_map, | 71 virtual void GetAllData(PrefetchDataMap* url_data_map, |
| 68 PrefetchDataMap* host_data_map); | 72 PrefetchDataMap* host_data_map, |
| 73 RedirectDataMap* url_redirect_data_map, | |
| 74 RedirectDataMap* host_redirect_data_map); | |
| 69 | 75 |
| 70 // Updates data for a Url and a host. If either of the |url_data| or | 76 // Updates data for a Url and a host. If either of the |url_data| or |
| 71 // |host_data| has an empty primary key, it will be ignored. | 77 // |host_data| or |url_redirect_data| or |host_redirect_data| has an empty |
| 72 // Note that the Urls and primary key in |url_data| and |host_data| should be | 78 // primary key, it will be ignored. |
| 73 // less than |kMaxStringLength| in length. | 79 // Note that the Urls and primary key in |url_data|, |host_data|, |
| 80 // |url_redirect_data| and |host_redirect_data| should be less than | |
| 81 // |kMaxStringLength| in length. | |
| 74 virtual void UpdateData(const PrefetchData& url_data, | 82 virtual void UpdateData(const PrefetchData& url_data, |
| 75 const PrefetchData& host_data); | 83 const PrefetchData& host_data, |
| 84 const RedirectData& url_redirect_data, | |
| 85 const RedirectData& host_redirect_data); | |
| 76 | 86 |
| 77 // Delete data for the input |urls| and |hosts|. | 87 // Delete data for the input |urls| and |hosts|. |
| 78 virtual void DeleteData(const std::vector<std::string>& urls, | 88 virtual void DeleteResourceData(const std::vector<std::string>& urls, |
| 79 const std::vector<std::string>& hosts); | 89 const std::vector<std::string>& hosts); |
| 80 | 90 |
| 81 // Wrapper over DeleteData for convenience. | 91 // Wrapper over DeleteResourceData for convenience. |
| 82 virtual void DeleteSingleDataPoint(const std::string& key, | 92 virtual void DeleteSingleResourceDataPoint(const std::string& key, |
| 83 PrefetchKeyType key_type); | 93 PrefetchKeyType key_type); |
| 94 | |
| 95 // Delete data for the input |urls| and |hosts|. | |
| 96 virtual void DeleteRedirectData(const std::vector<std::string>& urls, | |
| 97 const std::vector<std::string>& hosts); | |
| 98 | |
| 99 // Wrapper over DeleteRedirectData for convenience. | |
| 100 virtual void DeleteSingleRedirectDataPoint(const std::string& key, | |
| 101 PrefetchKeyType key_type); | |
| 84 | 102 |
| 85 // Deletes all data in all the tables. | 103 // Deletes all data in all the tables. |
| 86 virtual void DeleteAllData(); | 104 virtual void DeleteAllData(); |
| 87 | 105 |
| 106 // Sorts the resources by score, decreasing. | |
| 107 static void SortResources(std::vector<ResourceData>* resources); | |
| 108 | |
| 109 // Sorts the redirects by score, decreasing. | |
| 110 static void SortRedirects(std::vector<RedirectStat>* redirects); | |
| 111 | |
| 88 // The maximum length of the string that can be stored in the DB. | 112 // The maximum length of the string that can be stored in the DB. |
| 89 static constexpr size_t kMaxStringLength = 1024; | 113 static constexpr size_t kMaxStringLength = 1024; |
| 90 | 114 |
| 91 private: | 115 private: |
| 116 // Represents the type of information that is stored in prefetch database. | |
| 117 enum PrefetchDataType { | |
| 118 PREFETCH_DATA_TYPE_RESOURCE, | |
| 119 PREFETCH_DATA_TYPE_REDIRECT | |
| 120 }; | |
| 121 | |
| 92 friend class PredictorDatabaseInternal; | 122 friend class PredictorDatabaseInternal; |
| 93 friend class MockResourcePrefetchPredictorTables; | 123 friend class MockResourcePrefetchPredictorTables; |
| 94 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 124 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 95 DatabaseVersionIsSet); | 125 DatabaseVersionIsSet); |
| 96 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 126 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 97 DatabaseIsResetWhenIncompatible); | 127 DatabaseIsResetWhenIncompatible); |
| 98 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, ComputeScore); | 128 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 129 ComputeResourceScore); | |
| 99 | 130 |
| 100 ResourcePrefetchPredictorTables(); | 131 ResourcePrefetchPredictorTables(); |
| 101 ~ResourcePrefetchPredictorTables() override; | 132 ~ResourcePrefetchPredictorTables() override; |
| 102 | 133 |
| 103 // Helper functions below help perform functions on the Url and host table | 134 // Helper functions below help perform functions on the Url and host table |
| 104 // using the same code. | 135 // using the same code. |
| 105 void GetAllDataHelper(PrefetchKeyType key_type, | 136 void GetAllResourceDataHelper(PrefetchKeyType key_type, |
| 106 PrefetchDataMap* data_map, | 137 PrefetchDataMap* data_map, |
| 107 std::vector<std::string>* to_delete); | 138 std::vector<std::string>* to_delete); |
| 108 bool UpdateDataHelper(const PrefetchData& data); | 139 void GetAllRedirectDataHelper(PrefetchKeyType key_type, |
| 140 RedirectDataMap* redirect_map); | |
| 141 bool UpdateResourceDataHelper(PrefetchKeyType key_type, | |
| 142 const PrefetchData& data); | |
| 143 bool UpdateRedirectDataHelper(PrefetchKeyType key_type, | |
| 144 const RedirectData& data); | |
| 109 void DeleteDataHelper(PrefetchKeyType key_type, | 145 void DeleteDataHelper(PrefetchKeyType key_type, |
| 146 PrefetchDataType data_type, | |
| 110 const std::vector<std::string>& keys); | 147 const std::vector<std::string>& keys); |
| 111 | 148 |
| 112 // Returns true if the strings in the |data| are less than |kMaxStringLength| | 149 // Returns true if the strings in the |data| are less than |kMaxStringLength| |
| 113 // in length. | 150 // in length. |
| 114 static bool StringsAreSmallerThanDBLimit(const PrefetchData& data); | 151 static bool StringsAreSmallerThanDBLimit(const PrefetchData& data); |
| 152 static bool StringsAreSmallerThanDBLimit(const RedirectData& data); | |
| 115 | 153 |
| 116 // Computes score of |data|. | 154 // Computes score of |data|. |
| 117 static float ComputeScore(const ResourceData& data); | 155 static float ComputeResourceScore(const ResourceData& data); |
| 156 static float ComputeRedirectScore(const RedirectStat& data); | |
| 118 | 157 |
| 119 // PredictorTableBase methods. | 158 // PredictorTableBase methods. |
| 120 void CreateTableIfNonExistent() override; | 159 void CreateTableIfNonExistent() override; |
| 121 void LogDatabaseStats() override; | 160 void LogDatabaseStats() override; |
| 122 | 161 |
| 123 // Database version. Always increment it when any change is made to the data | 162 // Database version. Always increment it when any change is made to the data |
| 124 // schema (including the .proto). | 163 // schema (including the .proto). |
| 125 static constexpr int kDatabaseVersion = 2; | 164 static constexpr int kDatabaseVersion = 3; |
| 126 | 165 |
| 127 static bool DropTablesIfOutdated(sql::Connection* db); | 166 static bool DropTablesIfOutdated(sql::Connection* db); |
| 128 static int GetDatabaseVersion(sql::Connection* db); | 167 static int GetDatabaseVersion(sql::Connection* db); |
| 129 static bool SetDatabaseVersion(sql::Connection* db, int version); | 168 static bool SetDatabaseVersion(sql::Connection* db, int version); |
| 130 | 169 |
| 131 // Helpers to return Statements for cached Statements. The caller must take | 170 // Helpers to return Statements for cached Statements. The caller must take |
| 132 // ownership of the return Statements. | 171 // ownership of the return Statements. |
| 133 sql::Statement* GetUrlResourceDeleteStatement(); | 172 sql::Statement* GetUrlResourceDeleteStatement(); |
| 134 sql::Statement* GetUrlResourceUpdateStatement(); | 173 sql::Statement* GetUrlResourceUpdateStatement(); |
| 135 sql::Statement* GetUrlMetadataDeleteStatement(); | 174 sql::Statement* GetUrlMetadataDeleteStatement(); |
| 136 sql::Statement* GetUrlMetadataUpdateStatement(); | 175 sql::Statement* GetUrlMetadataUpdateStatement(); |
| 176 sql::Statement* GetUrlRedirectDeleteStatement(); | |
| 177 sql::Statement* GetUrlRedirectUpdateStatement(); | |
| 137 | 178 |
| 138 sql::Statement* GetHostResourceDeleteStatement(); | 179 sql::Statement* GetHostResourceDeleteStatement(); |
| 139 sql::Statement* GetHostResourceUpdateStatement(); | 180 sql::Statement* GetHostResourceUpdateStatement(); |
| 140 sql::Statement* GetHostMetadataDeleteStatement(); | 181 sql::Statement* GetHostMetadataDeleteStatement(); |
| 141 sql::Statement* GetHostMetadataUpdateStatement(); | 182 sql::Statement* GetHostMetadataUpdateStatement(); |
| 183 sql::Statement* GetHostRedirectDeleteStatement(); | |
| 184 sql::Statement* GetHostRedirectUpdateStatement(); | |
| 142 | 185 |
| 143 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); | 186 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); |
| 144 }; | 187 }; |
| 145 | 188 |
| 146 } // namespace predictors | 189 } // namespace predictors |
| 147 | 190 |
| 148 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 191 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
| OLD | NEW |