| 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 <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 "chrome/browser/predictors/predictor_table_base.h" | 16 #include "chrome/browser/predictors/predictor_table_base.h" |
| 17 #include "chrome/browser/predictors/resource_prefetch_common.h" | 17 #include "chrome/browser/predictors/resource_prefetch_common.h" |
| 18 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" | 18 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" |
| 19 #include "components/precache/core/proto/precache.pb.h" | 19 #include "components/precache/core/proto/precache.pb.h" |
| 20 | 20 |
| 21 namespace sql { | 21 namespace sql { |
| 22 class Statement; | 22 class Statement; |
| 23 } | 23 } |
| 24 | 24 |
| 25 namespace predictors { | 25 namespace predictors { |
| 26 | 26 |
| 27 // From resource_prefetch_predictor.proto. | |
| 28 using RedirectStat = RedirectData_RedirectStat; | |
| 29 | |
| 30 // Interface for database tables used by the ResourcePrefetchPredictor. | 27 // Interface for database tables used by the ResourcePrefetchPredictor. |
| 31 // All methods except the constructor and destructor need to be called on the DB | 28 // All methods except the constructor and destructor need to be called on the DB |
| 32 // thread. | 29 // thread. |
| 33 // | 30 // |
| 34 // Currently manages: | 31 // Currently manages: |
| 35 // - UrlResourceTable - resources per Urls. | 32 // - UrlResourceTable - resources per Urls. |
| 36 // - UrlRedirectTable - redirects per Urls. | 33 // - UrlRedirectTable - redirects per Urls. |
| 37 // - HostResourceTable - resources per host. | 34 // - HostResourceTable - resources per host. |
| 38 // - HostRedirectTable - redirects per host. | 35 // - HostRedirectTable - redirects per host. |
| 39 class ResourcePrefetchPredictorTables : public PredictorTableBase { | 36 class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 // Sorts the resources by score, decreasing. | 100 // Sorts the resources by score, decreasing. |
| 104 static void SortResources(PrefetchData* data); | 101 static void SortResources(PrefetchData* data); |
| 105 | 102 |
| 106 // Computes score of |data|. | 103 // Computes score of |data|. |
| 107 static float ComputeResourceScore(const ResourceData& data); | 104 static float ComputeResourceScore(const ResourceData& data); |
| 108 | 105 |
| 109 // Removes the redirects with more than |max_consecutive_misses| consecutive | 106 // Removes the redirects with more than |max_consecutive_misses| consecutive |
| 110 // misses from |data|. | 107 // misses from |data|. |
| 111 static void TrimRedirects(RedirectData* data, size_t max_consecutive_misses); | 108 static void TrimRedirects(RedirectData* data, size_t max_consecutive_misses); |
| 112 | 109 |
| 110 // Computes score of |data|. |
| 111 static float ComputePrecacheResourceScore( |
| 112 const precache::PrecacheResource& data); |
| 113 |
| 113 // Removes the origins with more than |max_consecutive_misses| consecutive | 114 // Removes the origins with more than |max_consecutive_misses| consecutive |
| 114 // misses from |data|. | 115 // misses from |data|. |
| 115 static void TrimOrigins(OriginData* data, size_t max_consecutive_misses); | 116 static void TrimOrigins(OriginData* data, size_t max_consecutive_misses); |
| 116 | 117 |
| 117 // Sorts the origins by score, decreasing. | 118 // Sorts the origins by score, decreasing. |
| 118 static void SortOrigins(OriginData* data); | 119 static void SortOrigins(OriginData* data); |
| 119 | 120 |
| 120 // Computes score of |data|. | 121 // Computes score of |origin|. |
| 121 static float ComputeOriginScore(const OriginStat& origin); | 122 static float ComputeOriginScore(const OriginStat& origin); |
| 122 | 123 |
| 123 // The maximum length of the string that can be stored in the DB. | 124 // The maximum length of the string that can be stored in the DB. |
| 124 static constexpr size_t kMaxStringLength = 1024; | 125 static constexpr size_t kMaxStringLength = 1024; |
| 125 | 126 |
| 126 protected: | 127 protected: |
| 127 // Protected for testing. Use PredictorDatabase::resource_prefetch_tables() | 128 // Protected for testing. Use PredictorDatabase::resource_prefetch_tables() |
| 128 // instead of this constructor. | 129 // instead of this constructor. |
| 129 ResourcePrefetchPredictorTables(); | 130 ResourcePrefetchPredictorTables(); |
| 130 ~ResourcePrefetchPredictorTables() override; | 131 ~ResourcePrefetchPredictorTables() override; |
| 131 | 132 |
| 132 private: | 133 private: |
| 133 // Represents the type of information that is stored in prefetch database. | 134 // Represents the type of information that is stored in prefetch database. |
| 134 enum class PrefetchDataType { RESOURCE, REDIRECT, MANIFEST, ORIGIN }; | 135 enum class PrefetchDataType { RESOURCE, REDIRECT, MANIFEST, ORIGIN }; |
| 135 | 136 |
| 136 enum class TableOperationType { INSERT, REMOVE }; | 137 enum class TableOperationType { INSERT, REMOVE }; |
| 137 | 138 |
| 138 friend class PredictorDatabaseInternal; | 139 friend class PredictorDatabaseInternal; |
| 139 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 140 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 140 DatabaseVersionIsSet); | 141 DatabaseVersionIsSet); |
| 141 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 142 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 142 DatabaseIsResetWhenIncompatible); | 143 DatabaseIsResetWhenIncompatible); |
| 143 | 144 |
| 144 // Database version. Always increment it when any change is made to the data | 145 // Database version. Always increment it when any change is made to the data |
| 145 // schema (including the .proto). | 146 // schema (including the .proto). |
| 146 static constexpr int kDatabaseVersion = 7; | 147 static constexpr int kDatabaseVersion = 8; |
| 147 | 148 |
| 148 // Helper functions below help perform functions on the Url and host table | 149 // Helper functions below help perform functions on the Url and host table |
| 149 // using the same code. | 150 // using the same code. |
| 150 void GetAllResourceDataHelper(PrefetchKeyType key_type, | 151 void GetAllResourceDataHelper(PrefetchKeyType key_type, |
| 151 PrefetchDataMap* data_map); | 152 PrefetchDataMap* data_map); |
| 152 void GetAllRedirectDataHelper(PrefetchKeyType key_type, | 153 void GetAllRedirectDataHelper(PrefetchKeyType key_type, |
| 153 RedirectDataMap* redirect_map); | 154 RedirectDataMap* redirect_map); |
| 154 void GetAllManifestDataHelper(ManifestDataMap* manifest_map); | 155 void GetAllManifestDataHelper(ManifestDataMap* manifest_map); |
| 155 void GetAllOriginDataHelper(OriginDataMap* manifest_map); | 156 void GetAllOriginDataHelper(OriginDataMap* manifest_map); |
| 156 | 157 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 178 | 179 |
| 179 static const char* GetTableName(PrefetchKeyType key_type, | 180 static const char* GetTableName(PrefetchKeyType key_type, |
| 180 PrefetchDataType data_type); | 181 PrefetchDataType data_type); |
| 181 | 182 |
| 182 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); | 183 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); |
| 183 }; | 184 }; |
| 184 | 185 |
| 185 } // namespace predictors | 186 } // namespace predictors |
| 186 | 187 |
| 187 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 188 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
| OLD | NEW |