| 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> |
| (...skipping 10 matching lines...) Expand all Loading... |
| 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; |
| 31 using chrome_browser_predictors::RedirectStat; |
| 31 | 32 |
| 32 // Interface for database tables used by the ResourcePrefetchPredictor. | 33 // Interface for database tables used by the ResourcePrefetchPredictor. |
| 33 // All methods except the constructor and destructor need to be called on the DB | 34 // All methods except the constructor and destructor need to be called on the DB |
| 34 // thread. | 35 // thread. |
| 35 // | 36 // |
| 36 // Currently manages: | 37 // Currently manages: |
| 37 // - UrlResourceTable - resources per Urls. | 38 // - UrlResourceTable - resources per Urls. |
| 38 // - UrlMetadataTable - misc data for Urls (like last visit time). | 39 // - UrlResourceMetadataTable - misc data for Urls (like last visit time). |
| 40 // - UrlRedirectTable - redirects per Urls. |
| 41 // - UrlRedirectMetadataTable - misc data for redirected Urls. |
| 39 // - HostResourceTable - resources per host. | 42 // - HostResourceTable - resources per host. |
| 40 // - HostMetadataTable - misc data for hosts. | 43 // - HostResourceMetadataTable - misc data for hosts. |
| 44 // - HostRedirectTable - redirects per host. |
| 45 // - HostRedirectMetadataTable - misc data for redirecte hosts. |
| 41 class ResourcePrefetchPredictorTables : public PredictorTableBase { | 46 class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| 42 public: | 47 public: |
| 43 // Used in the UrlResourceTable and HostResourceTable to store resources | 48 // Used in the UrlResourceTable and HostResourceTable to store resources |
| 44 // required for the page or host. | 49 // required for the page or host. |
| 45 struct ResourceRow { | 50 struct ResourceRow { |
| 46 ResourceRow(); | 51 ResourceRow(); |
| 47 ResourceRow(const ResourceRow& other); | 52 ResourceRow(const ResourceRow& other); |
| 48 ResourceRow(const std::string& resource_url, | 53 ResourceRow(const std::string& resource_url, |
| 49 content::ResourceType resource_type, | 54 content::ResourceType resource_type, |
| 50 int number_of_hits, | 55 int number_of_hits, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 65 size_t number_of_misses; | 70 size_t number_of_misses; |
| 66 size_t consecutive_misses; | 71 size_t consecutive_misses; |
| 67 double average_position; | 72 double average_position; |
| 68 net::RequestPriority priority; | 73 net::RequestPriority priority; |
| 69 bool has_validators; | 74 bool has_validators; |
| 70 bool always_revalidate; | 75 bool always_revalidate; |
| 71 | 76 |
| 72 // Not stored. | 77 // Not stored. |
| 73 float score; | 78 float score; |
| 74 }; | 79 }; |
| 75 typedef std::vector<ResourceRow> ResourceRows; | |
| 76 | 80 |
| 77 // Sorts the resource rows by score, decreasing. | 81 // Sorts the resource rows by score, decreasing. |
| 78 static void SortResourceRows(ResourceRows* rows); | 82 static void SortResourceRows(std::vector<ResourceRow>* rows); |
| 79 | 83 |
| 80 // Aggregated data for a Url or Host. Although the data differs slightly, we | 84 // Aggregated data for a Url or Host. Although the data differs slightly, we |
| 81 // store them in the same structure, because most of the fields are common and | 85 // store them in the same structure, because most of the fields are common and |
| 82 // it allows us to use the same functions. | 86 // it allows us to use the same functions. |
| 83 struct PrefetchData { | 87 struct PrefetchData { |
| 84 PrefetchData(PrefetchKeyType key_type, const std::string& primary_key); | 88 PrefetchData(PrefetchKeyType key_type, const std::string& primary_key); |
| 85 PrefetchData(const PrefetchData& other); | 89 PrefetchData(const PrefetchData& other); |
| 86 ~PrefetchData(); | 90 ~PrefetchData(); |
| 87 bool operator==(const PrefetchData& rhs) const; | 91 bool operator==(const PrefetchData& rhs) const; |
| 88 | 92 |
| 89 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } | 93 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } |
| 90 | 94 |
| 91 // Is the data a host as opposed to a Url? | 95 // Is the data a host as opposed to a Url? |
| 92 PrefetchKeyType key_type; // Not const to be able to assign. | 96 PrefetchKeyType key_type; // Not const to be able to assign. |
| 93 std::string primary_key; // is_host() ? main frame url : host. | 97 std::string primary_key; // is_host() ? main frame url : host. |
| 94 | 98 |
| 95 base::Time last_visit; | 99 base::Time last_visit; |
| 96 ResourceRows resources; | 100 std::vector<ResourceRow> resources; |
| 97 }; | 101 }; |
| 98 // Map from primary key to PrefetchData for the key. | 102 // Map from primary key to PrefetchData for the key. |
| 99 typedef std::map<std::string, PrefetchData> PrefetchDataMap; | 103 typedef std::map<std::string, PrefetchData> PrefetchDataMap; |
| 100 | 104 |
| 105 // Used in the UrlRedirectTable and HostRedirectTable to store redirects |
| 106 // required for the page or host. |
| 107 struct RedirectRow { |
| 108 RedirectRow(); |
| 109 RedirectRow(const RedirectRow& other); |
| 110 RedirectRow(const std::string& final_redirect, |
| 111 size_t number_of_hits, |
| 112 size_t number_of_misses, |
| 113 size_t consecutive_misses); |
| 114 void UpdateScore(); |
| 115 bool operator==(const RedirectRow& rhs) const; |
| 116 static void FromProto(const RedirectStat& proto, RedirectRow* row); |
| 117 void ToProto(RedirectStat* redirect_data) const; |
| 118 |
| 119 std::string final_redirect; |
| 120 size_t number_of_hits; |
| 121 size_t number_of_misses; |
| 122 size_t consecutive_misses; |
| 123 |
| 124 // Not stored. |
| 125 float score; |
| 126 }; |
| 127 |
| 128 // Sorts the redirect rows by score, decreasing. |
| 129 static void SortRedirectRows(std::vector<RedirectRow>* rows); |
| 130 |
| 131 // Aggregated data about redirections for a Url or Host. |
| 132 struct RedirectData { |
| 133 RedirectData(PrefetchKeyType key_type, const std::string& primary_key); |
| 134 RedirectData(const RedirectData& other); |
| 135 ~RedirectData(); |
| 136 bool operator==(const RedirectData& rhs) const; |
| 137 |
| 138 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } |
| 139 |
| 140 PrefetchKeyType key_type; |
| 141 std::string primary_key; |
| 142 base::Time last_visit; |
| 143 std::vector<RedirectRow> redirects; |
| 144 }; |
| 145 // Map from primary key to RedirectData for the key. |
| 146 typedef std::map<std::string, RedirectData> RedirectDataMap; |
| 147 |
| 101 // Returns data for all Urls and Hosts. | 148 // Returns data for all Urls and Hosts. |
| 102 virtual void GetAllData(PrefetchDataMap* url_data_map, | 149 virtual void GetAllData(PrefetchDataMap* url_data_map, |
| 103 PrefetchDataMap* host_data_map); | 150 PrefetchDataMap* host_data_map, |
| 151 RedirectDataMap* url_redirect_data_map, |
| 152 RedirectDataMap* host_redirect_data_map); |
| 104 | 153 |
| 105 // Updates data for a Url and a host. If either of the |url_data| or | 154 // Updates data for a Url and a host. If either of the |url_data| or |
| 106 // |host_data| has an empty primary key, it will be ignored. | 155 // |host_data| or |url_redirect_data| or |host_redirect_data| has an empty |
| 107 // Note that the Urls and primary key in |url_data| and |host_data| should be | 156 // primary key, it will be ignored. |
| 108 // less than |kMaxStringLength| in length. | 157 // Note that the Urls and primary key in |url_data|, |host_data|, |
| 158 // |url_redirect_data| and |host_redirect_data| should be less than |
| 159 // |kMaxStringLength| in length. |
| 109 virtual void UpdateData(const PrefetchData& url_data, | 160 virtual void UpdateData(const PrefetchData& url_data, |
| 110 const PrefetchData& host_data); | 161 const PrefetchData& host_data, |
| 162 const RedirectData& url_redirect_data, |
| 163 const RedirectData& host_redirect_data); |
| 111 | 164 |
| 112 // Delete data for the input |urls| and |hosts|. | 165 // Delete data for the input |urls| and |hosts|. |
| 113 virtual void DeleteData(const std::vector<std::string>& urls, | 166 virtual void DeleteResourceData(const std::vector<std::string>& urls, |
| 114 const std::vector<std::string>& hosts); | 167 const std::vector<std::string>& hosts); |
| 115 | 168 |
| 116 // Wrapper over DeleteData for convenience. | 169 // Wrapper over DeleteResourceData for convenience. |
| 117 virtual void DeleteSingleDataPoint(const std::string& key, | 170 virtual void DeleteSingleResourceDataPoint(const std::string& key, |
| 118 PrefetchKeyType key_type); | 171 PrefetchKeyType key_type); |
| 172 |
| 173 // Delete data for the input |urls| and |hosts|. |
| 174 virtual void DeleteRedirectData(const std::vector<std::string>& urls, |
| 175 const std::vector<std::string>& hosts); |
| 176 |
| 177 // Wrapper over DeleteRedirectData for convenience. |
| 178 virtual void DeleteSingleRedirectDataPoint(const std::string& key, |
| 179 PrefetchKeyType key_type); |
| 119 | 180 |
| 120 // Deletes all data in all the tables. | 181 // Deletes all data in all the tables. |
| 121 virtual void DeleteAllData(); | 182 virtual void DeleteAllData(); |
| 122 | 183 |
| 123 // The maximum length of the string that can be stored in the DB. | 184 // The maximum length of the string that can be stored in the DB. |
| 124 static constexpr size_t kMaxStringLength = 1024; | 185 static constexpr size_t kMaxStringLength = 1024; |
| 125 | 186 |
| 126 private: | 187 private: |
| 127 friend class PredictorDatabaseInternal; | 188 friend class PredictorDatabaseInternal; |
| 128 friend class MockResourcePrefetchPredictorTables; | 189 friend class MockResourcePrefetchPredictorTables; |
| 129 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 190 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 130 DatabaseVersionIsSet); | 191 DatabaseVersionIsSet); |
| 131 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 192 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 132 DatabaseIsResetWhenIncompatible); | 193 DatabaseIsResetWhenIncompatible); |
| 133 | 194 |
| 134 ResourcePrefetchPredictorTables(); | 195 ResourcePrefetchPredictorTables(); |
| 135 ~ResourcePrefetchPredictorTables() override; | 196 ~ResourcePrefetchPredictorTables() override; |
| 136 | 197 |
| 137 // Helper functions below help perform functions on the Url and host table | 198 // Helper functions below help perform functions on the Url and host table |
| 138 // using the same code. | 199 // using the same code. |
| 139 void GetAllDataHelper(PrefetchKeyType key_type, | 200 void GetAllDataHelper(PrefetchKeyType key_type, |
| 140 PrefetchDataMap* data_map, | 201 PrefetchDataMap* data_map, |
| 141 std::vector<std::string>* to_delete); | 202 std::vector<std::string>* to_delete); |
| 203 void GetAllDataHelper(PrefetchKeyType key_type, |
| 204 RedirectDataMap* redirect_map, |
| 205 std::vector<std::string>* to_delete); |
| 142 bool UpdateDataHelper(const PrefetchData& data); | 206 bool UpdateDataHelper(const PrefetchData& data); |
| 143 void DeleteDataHelper(PrefetchKeyType key_type, | 207 bool UpdateDataHelper(const RedirectData& data); |
| 144 const std::vector<std::string>& keys); | 208 void DeleteResourceDataHelper(PrefetchKeyType key_type, |
| 209 const std::vector<std::string>& keys); |
| 210 void DeleteRedirectDataHelper(PrefetchKeyType key_type, |
| 211 const std::vector<std::string>& keys); |
| 145 | 212 |
| 146 // Returns true if the strings in the |data| are less than |kMaxStringLength| | 213 // Returns true if the strings in the |data| are less than |kMaxStringLength| |
| 147 // in length. | 214 // in length. |
| 148 static bool StringsAreSmallerThanDBLimit(const PrefetchData& data); | 215 static bool StringsAreSmallerThanDBLimit(const PrefetchData& data); |
| 216 static bool StringsAreSmallerThanDBLimit(const RedirectData& data); |
| 149 | 217 |
| 150 // PredictorTableBase methods. | 218 // PredictorTableBase methods. |
| 151 void CreateTableIfNonExistent() override; | 219 void CreateTableIfNonExistent() override; |
| 152 void LogDatabaseStats() override; | 220 void LogDatabaseStats() override; |
| 153 | 221 |
| 154 // Database version. Always increment it when any change is made to the data | 222 // Database version. Always increment it when any change is made to the data |
| 155 // schema (including the .proto). | 223 // schema (including the .proto). |
| 156 static constexpr int kDatabaseVersion = 2; | 224 static constexpr int kDatabaseVersion = 3; |
| 157 | 225 |
| 158 static bool DropTablesIfOutdated(sql::Connection* db); | 226 static bool DropTablesIfOutdated(sql::Connection* db); |
| 159 static int GetDatabaseVersion(sql::Connection* db); | 227 static int GetDatabaseVersion(sql::Connection* db); |
| 160 static bool SetDatabaseVersion(sql::Connection* db, int version); | 228 static bool SetDatabaseVersion(sql::Connection* db, int version); |
| 161 | 229 |
| 162 // Helpers to return Statements for cached Statements. The caller must take | 230 // Helpers to return Statements for cached Statements. The caller must take |
| 163 // ownership of the return Statements. | 231 // ownership of the return Statements. |
| 164 sql::Statement* GetUrlResourceDeleteStatement(); | 232 sql::Statement* GetUrlResourceDeleteStatement(); |
| 165 sql::Statement* GetUrlResourceUpdateStatement(); | 233 sql::Statement* GetUrlResourceUpdateStatement(); |
| 166 sql::Statement* GetUrlMetadataDeleteStatement(); | 234 sql::Statement* GetUrlResourceMetadataDeleteStatement(); |
| 167 sql::Statement* GetUrlMetadataUpdateStatement(); | 235 sql::Statement* GetUrlResourceMetadataUpdateStatement(); |
| 236 sql::Statement* GetUrlRedirectDeleteStatement(); |
| 237 sql::Statement* GetUrlRedirectUpdateStatement(); |
| 238 sql::Statement* GetUrlRedirectMetadataDeleteStatement(); |
| 239 sql::Statement* GetUrlRedirectMetadataUpdateStatement(); |
| 168 | 240 |
| 169 sql::Statement* GetHostResourceDeleteStatement(); | 241 sql::Statement* GetHostResourceDeleteStatement(); |
| 170 sql::Statement* GetHostResourceUpdateStatement(); | 242 sql::Statement* GetHostResourceUpdateStatement(); |
| 171 sql::Statement* GetHostMetadataDeleteStatement(); | 243 sql::Statement* GetHostResourceMetadataDeleteStatement(); |
| 172 sql::Statement* GetHostMetadataUpdateStatement(); | 244 sql::Statement* GetHostResourceMetadataUpdateStatement(); |
| 245 sql::Statement* GetHostRedirectDeleteStatement(); |
| 246 sql::Statement* GetHostRedirectUpdateStatement(); |
| 247 sql::Statement* GetHostRedirectMetadataDeleteStatement(); |
| 248 sql::Statement* GetHostRedirectMetadataUpdateStatement(); |
| 173 | 249 |
| 174 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); | 250 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); |
| 175 }; | 251 }; |
| 176 | 252 |
| 177 } // namespace predictors | 253 } // namespace predictors |
| 178 | 254 |
| 179 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 255 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
| OLD | NEW |