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