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