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 <cstddef> | 8 #include <cstddef> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <unordered_map> | |
| 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 "chrome/browser/predictors/predictor_table_base.h" | 17 #include "chrome/browser/predictors/predictor_table_base.h" |
| 17 #include "chrome/browser/predictors/resource_prefetch_common.h" | 18 #include "chrome/browser/predictors/resource_prefetch_common.h" |
| 18 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" | 19 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" |
| 19 #include "components/precache/core/proto/precache.pb.h" | 20 #include "components/precache/core/proto/precache.pb.h" |
| 20 | 21 |
| 21 namespace sql { | 22 namespace sql { |
| 22 class Statement; | 23 class Statement; |
| 23 } | 24 } |
| 24 | 25 |
| 25 namespace predictors { | 26 namespace predictors { |
| 26 | 27 |
| 27 // From resource_prefetch_predictor.proto. | 28 // From resource_prefetch_predictor.proto. |
| 28 using RedirectStat = RedirectData_RedirectStat; | 29 using RedirectStat = RedirectData_RedirectStat; |
| 30 using OriginStat = OriginData_OriginStat; | |
| 29 | 31 |
| 30 // Interface for database tables used by the ResourcePrefetchPredictor. | 32 // Interface for database tables used by the ResourcePrefetchPredictor. |
| 31 // All methods except the constructor and destructor need to be called on the DB | 33 // All methods except the constructor and destructor need to be called on the DB |
| 32 // thread. | 34 // thread. |
| 33 // | 35 // |
| 34 // Currently manages: | 36 // Currently manages: |
| 35 // - UrlResourceTable - resources per Urls. | 37 // - UrlResourceTable - resources per Urls. |
| 36 // - UrlRedirectTable - redirects per Urls. | 38 // - UrlRedirectTable - redirects per Urls. |
| 37 // - HostResourceTable - resources per host. | 39 // - HostResourceTable - resources per host. |
| 38 // - HostRedirectTable - redirects per host. | 40 // - HostRedirectTable - redirects per host. |
| 39 class ResourcePrefetchPredictorTables : public PredictorTableBase { | 41 class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| 40 public: | 42 public: |
| 41 typedef std::map<std::string, PrefetchData> PrefetchDataMap; | 43 typedef std::unordered_map<std::string, PrefetchData> PrefetchDataMap; |
|
alexilin
2017/04/10 14:58:28
Wow, this is a big change! Maybe for another CL?
A
Benoit L
2017/04/11 09:04:05
I really dislike map<>, but unordered_map<> is not
| |
| 42 typedef std::map<std::string, RedirectData> RedirectDataMap; | 44 typedef std::unordered_map<std::string, RedirectData> RedirectDataMap; |
| 43 typedef std::map<std::string, precache::PrecacheManifest> ManifestDataMap; | 45 typedef std::unordered_map<std::string, precache::PrecacheManifest> |
| 46 ManifestDataMap; | |
| 47 typedef std::unordered_map<std::string, OriginData> OriginDataMap; | |
| 44 | 48 |
| 45 // Returns data for all Urls and Hosts. | 49 // Returns data for all Urls and Hosts. |
| 46 virtual void GetAllData(PrefetchDataMap* url_data_map, | 50 virtual void GetAllData(PrefetchDataMap* url_data_map, |
| 47 PrefetchDataMap* host_data_map, | 51 PrefetchDataMap* host_data_map, |
| 48 RedirectDataMap* url_redirect_data_map, | 52 RedirectDataMap* url_redirect_data_map, |
| 49 RedirectDataMap* host_redirect_data_map, | 53 RedirectDataMap* host_redirect_data_map, |
| 50 ManifestDataMap* manifest_map); | 54 ManifestDataMap* manifest_map, |
| 55 OriginDataMap* origin_data_map); | |
| 51 | 56 |
| 52 // Updates data for a Url and a host. If either of the |url_data| or | 57 // Updates data for a Url and a host. If any of the arguments has an empty |
| 53 // |host_data| or |url_redirect_data| or |host_redirect_data| has an empty | |
| 54 // primary key, it will be ignored. | 58 // primary key, it will be ignored. |
| 55 // Note that the Urls and primary key in |url_data|, |host_data|, | 59 // Note that all the keys should be less |kMaxStringLength| in length. |
| 56 // |url_redirect_data| and |host_redirect_data| should be less than | |
| 57 // |kMaxStringLength| in length. | |
| 58 virtual void UpdateData(const PrefetchData& url_data, | 60 virtual void UpdateData(const PrefetchData& url_data, |
| 59 const PrefetchData& host_data, | 61 const PrefetchData& host_data, |
| 60 const RedirectData& url_redirect_data, | 62 const RedirectData& url_redirect_data, |
| 61 const RedirectData& host_redirect_data); | 63 const RedirectData& host_redirect_data); |
| 62 | 64 |
| 63 // Updates manifest data for the input |host|. | 65 // Updates manifest data for the input |host|. |
| 64 virtual void UpdateManifestData( | 66 virtual void UpdateManifestData( |
| 65 const std::string& host, | 67 const std::string& host, |
| 66 const precache::PrecacheManifest& manifest_data); | 68 const precache::PrecacheManifest& manifest_data); |
| 67 | 69 |
| 70 // Updates the origin data for a given |host|. | |
| 71 virtual void UpdateOriginData(const std::string& host, | |
| 72 const OriginData& origin_data); | |
| 73 | |
| 68 // Delete data for the input |urls| and |hosts|. | 74 // Delete data for the input |urls| and |hosts|. |
| 69 virtual void DeleteResourceData(const std::vector<std::string>& urls, | 75 virtual void DeleteResourceData(const std::vector<std::string>& urls, |
| 70 const std::vector<std::string>& hosts); | 76 const std::vector<std::string>& hosts); |
| 71 | 77 |
| 72 // Wrapper over DeleteResourceData for convenience. | 78 // Wrapper over DeleteResourceData for convenience. |
| 73 virtual void DeleteSingleResourceDataPoint(const std::string& key, | 79 virtual void DeleteSingleResourceDataPoint(const std::string& key, |
| 74 PrefetchKeyType key_type); | 80 PrefetchKeyType key_type); |
| 75 | 81 |
| 76 // Delete data for the input |urls| and |hosts|. | 82 // Delete data for the input |urls| and |hosts|. |
| 77 virtual void DeleteRedirectData(const std::vector<std::string>& urls, | 83 virtual void DeleteRedirectData(const std::vector<std::string>& urls, |
| 78 const std::vector<std::string>& hosts); | 84 const std::vector<std::string>& hosts); |
| 79 | 85 |
| 80 // Wrapper over DeleteRedirectData for convenience. | 86 // Wrapper over DeleteRedirectData for convenience. |
| 81 virtual void DeleteSingleRedirectDataPoint(const std::string& key, | 87 virtual void DeleteSingleRedirectDataPoint(const std::string& key, |
| 82 PrefetchKeyType key_type); | 88 PrefetchKeyType key_type); |
| 83 | 89 |
| 84 // Delete data for the input |hosts|. | 90 // Delete data for the input |hosts|. |
| 85 virtual void DeleteManifestData(const std::vector<std::string>& hosts); | 91 virtual void DeleteManifestData(const std::vector<std::string>& hosts); |
| 86 | 92 |
| 93 // Deletes the origin data for a list of |hosts|. | |
| 94 virtual void DeleteOriginData(const std::vector<std::string>& hosts); | |
| 95 | |
| 87 // Deletes all data in all the tables. | 96 // Deletes all data in all the tables. |
| 88 virtual void DeleteAllData(); | 97 virtual void DeleteAllData(); |
| 89 | 98 |
| 90 // Removes the resources with more than |max_consecutive_misses| consecutive | 99 // Removes the resources with more than |max_consecutive_misses| consecutive |
| 91 // misses from |data|. | 100 // misses from |data|. |
| 92 static void TrimResources(PrefetchData* data, size_t max_consecutive_misses); | 101 static void TrimResources(PrefetchData* data, size_t max_consecutive_misses); |
| 93 | 102 |
| 94 // Sorts the resources by score, decreasing. | 103 // Sorts the resources by score, decreasing. |
| 95 static void SortResources(PrefetchData* data); | 104 static void SortResources(PrefetchData* data); |
| 96 | 105 |
| 97 // Computes score of |data|. | 106 // Computes score of |data|. |
| 98 static float ComputeResourceScore(const ResourceData& data); | 107 static float ComputeResourceScore(const ResourceData& data); |
| 99 | 108 |
| 100 // Removes the redirects with more than |max_consecutive_misses| consecutive | 109 // Removes the redirects with more than |max_consecutive_misses| consecutive |
| 101 // misses from |data|. | 110 // misses from |data|. |
| 102 static void TrimRedirects(RedirectData* data, size_t max_consecutive_misses); | 111 static void TrimRedirects(RedirectData* data, size_t max_consecutive_misses); |
| 103 | 112 |
| 113 // Removes the origins with more than |max_consecutive_misses| consecutive | |
| 114 // misses from |data|. | |
| 115 static void TrimOrigins(OriginData* data, size_t max_consecutive_misses); | |
| 116 | |
| 117 // Sorts the origins by score, decreasing. | |
| 118 static void SortOrigins(OriginData* data); | |
| 119 | |
| 120 // Computes score of |data|. | |
| 121 static float ComputeOriginScore(const OriginStat& origin); | |
| 122 | |
| 104 // The maximum length of the string that can be stored in the DB. | 123 // The maximum length of the string that can be stored in the DB. |
| 105 static constexpr size_t kMaxStringLength = 1024; | 124 static constexpr size_t kMaxStringLength = 1024; |
| 106 | 125 |
| 107 protected: | 126 protected: |
| 108 // Protected for testing. Use PredictorDatabase::resource_prefetch_tables() | 127 // Protected for testing. Use PredictorDatabase::resource_prefetch_tables() |
| 109 // instead of this constructor. | 128 // instead of this constructor. |
| 110 ResourcePrefetchPredictorTables(); | 129 ResourcePrefetchPredictorTables(); |
| 111 ~ResourcePrefetchPredictorTables() override; | 130 ~ResourcePrefetchPredictorTables() override; |
| 112 | 131 |
| 113 private: | 132 private: |
| 114 // Represents the type of information that is stored in prefetch database. | 133 // Represents the type of information that is stored in prefetch database. |
| 115 enum class PrefetchDataType { RESOURCE, REDIRECT, MANIFEST }; | 134 enum class PrefetchDataType { RESOURCE, REDIRECT, MANIFEST, ORIGIN }; |
| 116 | 135 |
| 117 enum class TableOperationType { INSERT, REMOVE }; | 136 enum class TableOperationType { INSERT, REMOVE }; |
| 118 | 137 |
| 119 friend class PredictorDatabaseInternal; | 138 friend class PredictorDatabaseInternal; |
| 120 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 139 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 121 DatabaseVersionIsSet); | 140 DatabaseVersionIsSet); |
| 122 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 141 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 123 DatabaseIsResetWhenIncompatible); | 142 DatabaseIsResetWhenIncompatible); |
| 124 | 143 |
| 125 // 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 |
| 126 // schema (including the .proto). | 145 // schema (including the .proto). |
| 127 static constexpr int kDatabaseVersion = 6; | 146 static constexpr int kDatabaseVersion = 7; |
| 128 | 147 |
| 129 // Helper functions below help perform functions on the Url and host table | 148 // Helper functions below help perform functions on the Url and host table |
| 130 // using the same code. | 149 // using the same code. |
| 131 void GetAllResourceDataHelper(PrefetchKeyType key_type, | 150 void GetAllResourceDataHelper(PrefetchKeyType key_type, |
| 132 PrefetchDataMap* data_map); | 151 PrefetchDataMap* data_map); |
| 133 void GetAllRedirectDataHelper(PrefetchKeyType key_type, | 152 void GetAllRedirectDataHelper(PrefetchKeyType key_type, |
| 134 RedirectDataMap* redirect_map); | 153 RedirectDataMap* redirect_map); |
| 135 void GetAllManifestDataHelper(ManifestDataMap* manifest_map); | 154 void GetAllManifestDataHelper(ManifestDataMap* manifest_map); |
| 155 void GetAllOriginDataHelper(OriginDataMap* manifest_map); | |
| 136 | 156 |
| 137 bool UpdateDataHelper(PrefetchKeyType key_type, | 157 bool UpdateDataHelper(PrefetchKeyType key_type, |
| 138 PrefetchDataType data_type, | 158 PrefetchDataType data_type, |
| 139 const std::string& key, | 159 const std::string& key, |
| 140 const google::protobuf::MessageLite& data); | 160 const google::protobuf::MessageLite& data); |
| 141 void DeleteDataHelper(PrefetchKeyType key_type, | 161 void DeleteDataHelper(PrefetchKeyType key_type, |
| 142 PrefetchDataType data_type, | 162 PrefetchDataType data_type, |
| 143 const std::vector<std::string>& keys); | 163 const std::vector<std::string>& keys); |
| 144 | 164 |
| 145 // PredictorTableBase: | 165 // PredictorTableBase: |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 158 | 178 |
| 159 static const char* GetTableName(PrefetchKeyType key_type, | 179 static const char* GetTableName(PrefetchKeyType key_type, |
| 160 PrefetchDataType data_type); | 180 PrefetchDataType data_type); |
| 161 | 181 |
| 162 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); | 182 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); |
| 163 }; | 183 }; |
| 164 | 184 |
| 165 } // namespace predictors | 185 } // namespace predictors |
| 166 | 186 |
| 167 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 187 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
| OLD | NEW |