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 // From resource_prefetch_predictor.proto. |
| 31 using RedirectStat = RedirectData_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 // - UrlMetadataTable - misc data for Urls (like last visit time). |
| 40 // - UrlRedirectTable - redirects per Urls. | |
| 39 // - HostResourceTable - resources per host. | 41 // - HostResourceTable - resources per host. |
| 40 // - HostMetadataTable - misc data for hosts. | 42 // - HostMetadataTable - misc data for hosts. |
| 43 // - HostRedirectTable - redirects per host. | |
| 41 class ResourcePrefetchPredictorTables : public PredictorTableBase { | 44 class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| 42 public: | 45 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 | 46 // 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 | 47 // store them in the same structure, because most of the fields are common and |
| 48 // it allows us to use the same functions. | 48 // it allows us to use the same functions. |
| 49 struct PrefetchData { | 49 struct PrefetchData { |
| 50 PrefetchData(PrefetchKeyType key_type, const std::string& primary_key); | 50 PrefetchData(PrefetchKeyType key_type, const std::string& primary_key); |
| 51 PrefetchData(const PrefetchData& other); | 51 PrefetchData(const PrefetchData& other); |
| 52 ~PrefetchData(); | 52 ~PrefetchData(); |
| 53 | 53 |
| 54 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } | 54 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } |
| 55 | 55 |
| 56 // Is the data a host as opposed to a Url? | 56 // Is the data a host as opposed to a Url? |
| 57 PrefetchKeyType key_type; // Not const to be able to assign. | 57 PrefetchKeyType key_type; // Not const to be able to assign. |
| 58 std::string primary_key; // is_host() ? host : main frame url. | 58 std::string primary_key; // is_host() ? host : main frame url. |
| 59 | 59 |
| 60 base::Time last_visit; | 60 base::Time last_visit; |
| 61 std::vector<ResourceData> resources; | 61 std::vector<ResourceData> resources; |
| 62 }; | 62 }; |
| 63 // Map from primary key to PrefetchData for the key. | 63 // Map from primary key to PrefetchData for the key. |
| 64 typedef std::map<std::string, PrefetchData> PrefetchDataMap; | 64 typedef std::map<std::string, PrefetchData> PrefetchDataMap; |
| 65 | 65 |
| 66 // Map from primary key to RedirectData for the key. | |
| 67 typedef std::map<std::string, RedirectData> RedirectDataMap; | |
| 68 | |
| 66 // Returns data for all Urls and Hosts. | 69 // Returns data for all Urls and Hosts. |
| 67 virtual void GetAllData(PrefetchDataMap* url_data_map, | 70 virtual void GetAllData(PrefetchDataMap* url_data_map, |
| 68 PrefetchDataMap* host_data_map); | 71 PrefetchDataMap* host_data_map, |
| 72 RedirectDataMap* url_redirect_data_map, | |
| 73 RedirectDataMap* host_redirect_data_map); | |
| 69 | 74 |
| 70 // Updates data for a Url and a host. If either of the |url_data| or | 75 // 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. | 76 // |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 | 77 // primary key, it will be ignored. |
| 73 // less than |kMaxStringLength| in length. | 78 // Note that the Urls and primary key in |url_data|, |host_data|, |
| 79 // |url_redirect_data| and |host_redirect_data| should be less than | |
| 80 // |kMaxStringLength| in length. | |
| 74 virtual void UpdateData(const PrefetchData& url_data, | 81 virtual void UpdateData(const PrefetchData& url_data, |
| 75 const PrefetchData& host_data); | 82 const PrefetchData& host_data, |
| 83 const RedirectData& url_redirect_data, | |
| 84 const RedirectData& host_redirect_data); | |
| 76 | 85 |
| 77 // Delete data for the input |urls| and |hosts|. | 86 // Delete data for the input |urls| and |hosts|. |
| 78 virtual void DeleteData(const std::vector<std::string>& urls, | 87 virtual void DeleteResourceData(const std::vector<std::string>& urls, |
| 79 const std::vector<std::string>& hosts); | 88 const std::vector<std::string>& hosts); |
| 80 | 89 |
| 81 // Wrapper over DeleteData for convenience. | 90 // Wrapper over DeleteResourceData for convenience. |
| 82 virtual void DeleteSingleDataPoint(const std::string& key, | 91 virtual void DeleteSingleResourceDataPoint(const std::string& key, |
| 83 PrefetchKeyType key_type); | 92 PrefetchKeyType key_type); |
| 93 | |
| 94 // Delete data for the input |urls| and |hosts|. | |
| 95 virtual void DeleteRedirectData(const std::vector<std::string>& urls, | |
| 96 const std::vector<std::string>& hosts); | |
| 97 | |
| 98 // Wrapper over DeleteRedirectData for convenience. | |
| 99 virtual void DeleteSingleRedirectDataPoint(const std::string& key, | |
| 100 PrefetchKeyType key_type); | |
| 84 | 101 |
| 85 // Deletes all data in all the tables. | 102 // Deletes all data in all the tables. |
| 86 virtual void DeleteAllData(); | 103 virtual void DeleteAllData(); |
| 87 | 104 |
| 105 // Sorts the resources by score, decreasing. | |
| 106 static void SortResources(std::vector<ResourceData>* resources); | |
| 107 | |
| 108 // Sorts the redirects by score, decreasing. | |
| 109 static void SortRedirects(std::vector<RedirectStat>* redirects); | |
| 110 | |
| 88 // The maximum length of the string that can be stored in the DB. | 111 // The maximum length of the string that can be stored in the DB. |
| 89 static constexpr size_t kMaxStringLength = 1024; | 112 static constexpr size_t kMaxStringLength = 1024; |
| 90 | 113 |
| 91 private: | 114 private: |
| 115 // Represents the type of information that is stored in prefetch database. | |
| 116 enum PrefetchDataType { | |
| 117 PREFETCH_DATA_TYPE_RESOURCE, | |
|
pasko
2016/09/27 16:21:27
there is a new shiny c++11 way to do it:
enum clas
alexilin
2016/09/27 18:11:09
Done.
| |
| 118 PREFETCH_DATA_TYPE_REDIRECT, | |
| 119 PREFETCH_DATA_TYPE_METADATA | |
| 120 }; | |
| 121 | |
| 122 enum TableOperationType { | |
| 123 TABLE_OPERATION_TYPE_INSERT, | |
| 124 TABLE_OPERATION_TYPE_DELETE | |
| 125 }; | |
| 126 | |
| 92 friend class PredictorDatabaseInternal; | 127 friend class PredictorDatabaseInternal; |
| 93 friend class MockResourcePrefetchPredictorTables; | 128 friend class MockResourcePrefetchPredictorTables; |
| 94 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 129 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 95 DatabaseVersionIsSet); | 130 DatabaseVersionIsSet); |
| 96 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 131 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 97 DatabaseIsResetWhenIncompatible); | 132 DatabaseIsResetWhenIncompatible); |
| 98 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, ComputeScore); | 133 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 134 ComputeResourceScore); | |
| 99 | 135 |
| 100 ResourcePrefetchPredictorTables(); | 136 ResourcePrefetchPredictorTables(); |
| 101 ~ResourcePrefetchPredictorTables() override; | 137 ~ResourcePrefetchPredictorTables() override; |
| 102 | 138 |
| 103 // Helper functions below help perform functions on the Url and host table | 139 // Helper functions below help perform functions on the Url and host table |
| 104 // using the same code. | 140 // using the same code. |
| 105 void GetAllDataHelper(PrefetchKeyType key_type, | 141 void GetAllResourceDataHelper(PrefetchKeyType key_type, |
| 106 PrefetchDataMap* data_map, | 142 PrefetchDataMap* data_map, |
| 107 std::vector<std::string>* to_delete); | 143 std::vector<std::string>* to_delete); |
| 108 bool UpdateDataHelper(const PrefetchData& data); | 144 void GetAllRedirectDataHelper(PrefetchKeyType key_type, |
| 145 RedirectDataMap* redirect_map); | |
| 146 bool UpdateResourceDataHelper(PrefetchKeyType key_type, | |
| 147 const PrefetchData& data); | |
| 148 bool UpdateRedirectDataHelper(PrefetchKeyType key_type, | |
| 149 const RedirectData& data); | |
| 109 void DeleteDataHelper(PrefetchKeyType key_type, | 150 void DeleteDataHelper(PrefetchKeyType key_type, |
| 151 PrefetchDataType data_type, | |
| 110 const std::vector<std::string>& keys); | 152 const std::vector<std::string>& keys); |
| 111 | 153 |
| 112 // Returns true if the strings in the |data| are less than |kMaxStringLength| | 154 // Returns true if the strings in the |data| are less than |kMaxStringLength| |
| 113 // in length. | 155 // in length. |
| 114 static bool StringsAreSmallerThanDBLimit(const PrefetchData& data); | 156 static bool StringsAreSmallerThanDBLimit(const PrefetchData& data); |
| 157 static bool StringsAreSmallerThanDBLimit(const RedirectData& data); | |
| 115 | 158 |
| 116 // Computes score of |data|. | 159 // Computes score of |data|. |
| 117 static float ComputeScore(const ResourceData& data); | 160 static float ComputeResourceScore(const ResourceData& data); |
| 161 static float ComputeRedirectScore(const RedirectStat& data); | |
| 118 | 162 |
| 119 // PredictorTableBase methods. | 163 // PredictorTableBase methods. |
| 120 void CreateTableIfNonExistent() override; | 164 void CreateTableIfNonExistent() override; |
| 121 void LogDatabaseStats() override; | 165 void LogDatabaseStats() override; |
| 122 | 166 |
| 123 // Database version. Always increment it when any change is made to the data | 167 // Database version. Always increment it when any change is made to the data |
| 124 // schema (including the .proto). | 168 // schema (including the .proto). |
| 125 static constexpr int kDatabaseVersion = 2; | 169 static constexpr int kDatabaseVersion = 3; |
| 126 | 170 |
| 127 static bool DropTablesIfOutdated(sql::Connection* db); | 171 static bool DropTablesIfOutdated(sql::Connection* db); |
| 128 static int GetDatabaseVersion(sql::Connection* db); | 172 static int GetDatabaseVersion(sql::Connection* db); |
| 129 static bool SetDatabaseVersion(sql::Connection* db, int version); | 173 static bool SetDatabaseVersion(sql::Connection* db, int version); |
| 130 | 174 |
| 131 // Helpers to return Statements for cached Statements. The caller must take | 175 // Helper to return Statements for cached Statements. The caller must take |
| 132 // ownership of the return Statements. | 176 // ownership of the return Statements. |
| 133 sql::Statement* GetUrlResourceDeleteStatement(); | 177 sql::Statement* GetTableUpdateStatement(PrefetchKeyType key_type, |
|
Benoit L
2016/09/27 15:19:34
Why not return a unique_ptr<> right away here?
The
alexilin
2016/09/27 18:11:09
Great! Besides, it meets object ownership conventi
| |
| 134 sql::Statement* GetUrlResourceUpdateStatement(); | 178 PrefetchDataType data_type, |
| 135 sql::Statement* GetUrlMetadataDeleteStatement(); | 179 TableOperationType op_type); |
| 136 sql::Statement* GetUrlMetadataUpdateStatement(); | |
| 137 | 180 |
| 138 sql::Statement* GetHostResourceDeleteStatement(); | 181 static const char* GetTableUpdateStatementTemplate( |
|
pasko
2016/09/27 16:21:27
this does not need to be exposed in the interface
alexilin
2016/09/27 18:11:09
Do you suggest to move all private static function
| |
| 139 sql::Statement* GetHostResourceUpdateStatement(); | 182 TableOperationType op_type, |
| 140 sql::Statement* GetHostMetadataDeleteStatement(); | 183 PrefetchDataType data_type); |
| 141 sql::Statement* GetHostMetadataUpdateStatement(); | 184 static const char* GetTableUpdateStatementTableName( |
| 185 PrefetchKeyType key_type, | |
| 186 PrefetchDataType data_type); | |
| 142 | 187 |
| 143 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); | 188 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); |
| 144 }; | 189 }; |
| 145 | 190 |
| 146 } // namespace predictors | 191 } // namespace predictors |
| 147 | 192 |
| 148 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 193 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
| OLD | NEW |