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