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::RedirectData; | |
| 32 using chrome_browser_predictors::RedirectData::RedirectStat; | |
|
alexilin
2016/09/23 15:29:15
Woops, it won't be compiled
| |
| 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. | 47 // Sorts the resources by score, decreasing. |
| 44 static void SortResources(std::vector<ResourceData>* resources); | 48 static void SortResources(std::vector<ResourceData>* resources); |
| 45 | 49 |
| 50 // Sorts the redirects by score, decreasing. | |
| 51 static void SortRedirects(std::vector<RedirectStat>* redirects); | |
| 52 | |
| 46 // Aggregated data for a Url or Host. Although the data differs slightly, we | 53 // 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 | 54 // store them in the same structure, because most of the fields are common and |
| 48 // it allows us to use the same functions. | 55 // it allows us to use the same functions. |
| 49 struct PrefetchData { | 56 struct PrefetchData { |
| 50 PrefetchData(PrefetchKeyType key_type, const std::string& primary_key); | 57 PrefetchData(PrefetchKeyType key_type, const std::string& primary_key); |
| 51 PrefetchData(const PrefetchData& other); | 58 PrefetchData(const PrefetchData& other); |
| 52 ~PrefetchData(); | 59 ~PrefetchData(); |
| 53 | 60 |
| 54 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } | 61 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } |
| 55 | 62 |
| 56 // Is the data a host as opposed to a Url? | 63 // Is the data a host as opposed to a Url? |
| 57 PrefetchKeyType key_type; // Not const to be able to assign. | 64 PrefetchKeyType key_type; // Not const to be able to assign. |
| 58 std::string primary_key; // is_host() ? host : main frame url. | 65 std::string primary_key; // is_host() ? host : main frame url. |
| 59 | 66 |
| 60 base::Time last_visit; | 67 base::Time last_visit; |
| 61 std::vector<ResourceData> resources; | 68 std::vector<ResourceData> resources; |
| 62 }; | 69 }; |
| 63 // Map from primary key to PrefetchData for the key. | 70 // Map from primary key to PrefetchData for the key. |
| 64 typedef std::map<std::string, PrefetchData> PrefetchDataMap; | 71 typedef std::map<std::string, PrefetchData> PrefetchDataMap; |
| 65 | 72 |
| 73 // Map from primary key to RedirectData for the key. | |
| 74 typedef std::map<std::string, RedirectData> RedirectDataMap; | |
| 75 | |
| 66 // Returns data for all Urls and Hosts. | 76 // Returns data for all Urls and Hosts. |
| 67 virtual void GetAllData(PrefetchDataMap* url_data_map, | 77 virtual void GetAllData(PrefetchDataMap* url_data_map, |
| 68 PrefetchDataMap* host_data_map); | 78 PrefetchDataMap* host_data_map, |
| 79 RedirectDataMap* url_redirect_data_map, | |
| 80 RedirectDataMap* host_redirect_data_map); | |
| 69 | 81 |
| 70 // Updates data for a Url and a host. If either of the |url_data| or | 82 // 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. | 83 // |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 | 84 // primary key, it will be ignored. |
| 73 // less than |kMaxStringLength| in length. | 85 // Note that the Urls and primary key in |url_data|, |host_data|, |
| 86 // |url_redirect_data| and |host_redirect_data| should be less than | |
| 87 // |kMaxStringLength| in length. | |
| 74 virtual void UpdateData(const PrefetchData& url_data, | 88 virtual void UpdateData(const PrefetchData& url_data, |
| 75 const PrefetchData& host_data); | 89 const PrefetchData& host_data, |
| 90 const RedirectData& url_redirect_data, | |
| 91 const RedirectData& host_redirect_data); | |
| 76 | 92 |
| 77 // Delete data for the input |urls| and |hosts|. | 93 // Delete data for the input |urls| and |hosts|. |
| 78 virtual void DeleteData(const std::vector<std::string>& urls, | 94 virtual void DeleteResourceData(const std::vector<std::string>& urls, |
| 79 const std::vector<std::string>& hosts); | 95 const std::vector<std::string>& hosts); |
| 80 | 96 |
| 81 // Wrapper over DeleteData for convenience. | 97 // Wrapper over DeleteResourceData for convenience. |
| 82 virtual void DeleteSingleDataPoint(const std::string& key, | 98 virtual void DeleteSingleResourceDataPoint(const std::string& key, |
| 83 PrefetchKeyType key_type); | 99 PrefetchKeyType key_type); |
| 100 | |
| 101 // Delete data for the input |urls| and |hosts|. | |
| 102 virtual void DeleteRedirectData(const std::vector<std::string>& urls, | |
| 103 const std::vector<std::string>& hosts); | |
| 104 | |
| 105 // Wrapper over DeleteRedirectData for convenience. | |
| 106 virtual void DeleteSingleRedirectDataPoint(const std::string& key, | |
| 107 PrefetchKeyType key_type); | |
| 84 | 108 |
| 85 // Deletes all data in all the tables. | 109 // Deletes all data in all the tables. |
| 86 virtual void DeleteAllData(); | 110 virtual void DeleteAllData(); |
| 87 | 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: |
| 92 friend class PredictorDatabaseInternal; | 116 friend class PredictorDatabaseInternal; |
| 93 friend class MockResourcePrefetchPredictorTables; | 117 friend class MockResourcePrefetchPredictorTables; |
| 94 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 118 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 95 DatabaseVersionIsSet); | 119 DatabaseVersionIsSet); |
| 96 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 120 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 97 DatabaseIsResetWhenIncompatible); | 121 DatabaseIsResetWhenIncompatible); |
| 98 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, ComputeScore); | 122 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 123 ComputeResourceScore); | |
| 99 | 124 |
| 100 ResourcePrefetchPredictorTables(); | 125 ResourcePrefetchPredictorTables(); |
| 101 ~ResourcePrefetchPredictorTables() override; | 126 ~ResourcePrefetchPredictorTables() override; |
| 102 | 127 |
| 103 // Helper functions below help perform functions on the Url and host table | 128 // Helper functions below help perform functions on the Url and host table |
| 104 // using the same code. | 129 // using the same code. |
| 105 void GetAllDataHelper(PrefetchKeyType key_type, | 130 void GetAllResourceDataHelper(PrefetchKeyType key_type, |
| 106 PrefetchDataMap* data_map, | 131 PrefetchDataMap* data_map, |
| 107 std::vector<std::string>* to_delete); | 132 std::vector<std::string>* to_delete); |
| 108 bool UpdateDataHelper(const PrefetchData& data); | 133 void GetAllRedirectDataHelper(PrefetchKeyType key_type, |
| 109 void DeleteDataHelper(PrefetchKeyType key_type, | 134 RedirectDataMap* redirect_map); |
| 110 const std::vector<std::string>& keys); | 135 bool UpdateResourceDataHelper(PrefetchKeyType key_type, |
| 136 const PrefetchData& data); | |
| 137 bool UpdateRedirectDataHelper(PrefetchKeyType key_type, | |
| 138 const RedirectData& data); | |
| 139 void DeleteResourceDataHelper(PrefetchKeyType key_type, | |
| 140 const std::vector<std::string>& keys); | |
| 141 void DeleteRedirectDataHelper(PrefetchKeyType key_type, | |
| 142 const std::vector<std::string>& keys); | |
| 111 | 143 |
| 112 // Returns true if the strings in the |data| are less than |kMaxStringLength| | 144 // Returns true if the strings in the |data| are less than |kMaxStringLength| |
| 113 // in length. | 145 // in length. |
| 114 static bool StringsAreSmallerThanDBLimit(const PrefetchData& data); | 146 static bool StringsAreSmallerThanDBLimit(const PrefetchData& data); |
| 147 static bool StringsAreSmallerThanDBLimit(const RedirectData& data); | |
| 115 | 148 |
| 116 // Computes score of |data|. | 149 // Computes score of |data|. |
| 117 static float ComputeScore(const ResourceData& data); | 150 static float ComputeResourceScore(const ResourceData& data); |
| 151 static float ComputeRedirectScore(const RedirectStat& data); | |
| 118 | 152 |
| 119 // PredictorTableBase methods. | 153 // PredictorTableBase methods. |
| 120 void CreateTableIfNonExistent() override; | 154 void CreateTableIfNonExistent() override; |
| 121 void LogDatabaseStats() override; | 155 void LogDatabaseStats() override; |
| 122 | 156 |
| 123 // Database version. Always increment it when any change is made to the data | 157 // Database version. Always increment it when any change is made to the data |
| 124 // schema (including the .proto). | 158 // schema (including the .proto). |
| 125 static constexpr int kDatabaseVersion = 2; | 159 static constexpr int kDatabaseVersion = 3; |
| 126 | 160 |
| 127 static bool DropTablesIfOutdated(sql::Connection* db); | 161 static bool DropTablesIfOutdated(sql::Connection* db); |
| 128 static int GetDatabaseVersion(sql::Connection* db); | 162 static int GetDatabaseVersion(sql::Connection* db); |
| 129 static bool SetDatabaseVersion(sql::Connection* db, int version); | 163 static bool SetDatabaseVersion(sql::Connection* db, int version); |
| 130 | 164 |
| 131 // Helpers to return Statements for cached Statements. The caller must take | 165 // Helpers to return Statements for cached Statements. The caller must take |
| 132 // ownership of the return Statements. | 166 // ownership of the return Statements. |
| 133 sql::Statement* GetUrlResourceDeleteStatement(); | 167 sql::Statement* GetUrlResourceDeleteStatement(); |
| 134 sql::Statement* GetUrlResourceUpdateStatement(); | 168 sql::Statement* GetUrlResourceUpdateStatement(); |
| 135 sql::Statement* GetUrlMetadataDeleteStatement(); | 169 sql::Statement* GetUrlMetadataDeleteStatement(); |
| 136 sql::Statement* GetUrlMetadataUpdateStatement(); | 170 sql::Statement* GetUrlMetadataUpdateStatement(); |
| 171 sql::Statement* GetUrlRedirectDeleteStatement(); | |
| 172 sql::Statement* GetUrlRedirectUpdateStatement(); | |
| 137 | 173 |
| 138 sql::Statement* GetHostResourceDeleteStatement(); | 174 sql::Statement* GetHostResourceDeleteStatement(); |
| 139 sql::Statement* GetHostResourceUpdateStatement(); | 175 sql::Statement* GetHostResourceUpdateStatement(); |
| 140 sql::Statement* GetHostMetadataDeleteStatement(); | 176 sql::Statement* GetHostMetadataDeleteStatement(); |
| 141 sql::Statement* GetHostMetadataUpdateStatement(); | 177 sql::Statement* GetHostMetadataUpdateStatement(); |
| 178 sql::Statement* GetHostRedirectDeleteStatement(); | |
| 179 sql::Statement* GetHostRedirectUpdateStatement(); | |
| 142 | 180 |
| 143 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); | 181 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); |
| 144 }; | 182 }; |
| 145 | 183 |
| 146 } // namespace predictors | 184 } // namespace predictors |
| 147 | 185 |
| 148 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 186 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
| OLD | NEW |