| 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 <cstddef> |
| 9 | |
| 10 #include <map> | 9 #include <map> |
| 11 #include <memory> | 10 #include <memory> |
| 12 #include <string> | 11 #include <string> |
| 13 #include <vector> | 12 #include <vector> |
| 14 | 13 |
| 15 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 16 #include "base/macros.h" | 15 #include "base/macros.h" |
| 17 #include "base/memory/ptr_util.h" | |
| 18 #include "chrome/browser/predictors/predictor_table_base.h" | 16 #include "chrome/browser/predictors/predictor_table_base.h" |
| 19 #include "chrome/browser/predictors/resource_prefetch_common.h" | 17 #include "chrome/browser/predictors/resource_prefetch_common.h" |
| 20 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" | 18 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" |
| 21 #include "content/public/common/resource_type.h" | |
| 22 #include "net/base/request_priority.h" | |
| 23 #include "url/gurl.h" | |
| 24 | 19 |
| 25 namespace sql { | 20 namespace sql { |
| 26 class Statement; | 21 class Statement; |
| 27 } | 22 } |
| 28 | 23 |
| 29 namespace predictors { | 24 namespace predictors { |
| 30 | 25 |
| 31 // From resource_prefetch_predictor.proto. | 26 // From resource_prefetch_predictor.proto. |
| 32 using RedirectStat = RedirectData_RedirectStat; | 27 using RedirectStat = RedirectData_RedirectStat; |
| 33 | 28 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 // Deletes all data in all the tables. | 79 // Deletes all data in all the tables. |
| 85 virtual void DeleteAllData(); | 80 virtual void DeleteAllData(); |
| 86 | 81 |
| 87 // Removes the resources with more than |max_consecutive_misses| consecutive | 82 // Removes the resources with more than |max_consecutive_misses| consecutive |
| 88 // misses from |data|. | 83 // misses from |data|. |
| 89 static void TrimResources(PrefetchData* data, size_t max_consecutive_misses); | 84 static void TrimResources(PrefetchData* data, size_t max_consecutive_misses); |
| 90 | 85 |
| 91 // Sorts the resources by score, decreasing. | 86 // Sorts the resources by score, decreasing. |
| 92 static void SortResources(PrefetchData* data); | 87 static void SortResources(PrefetchData* data); |
| 93 | 88 |
| 89 // Computes score of |data|. |
| 90 static float ComputeResourceScore(const ResourceData& data); |
| 91 |
| 94 // Removes the redirects with more than |max_consecutive_misses| consecutive | 92 // Removes the redirects with more than |max_consecutive_misses| consecutive |
| 95 // misses from |data|. | 93 // misses from |data|. |
| 96 static void TrimRedirects(RedirectData* data, size_t max_consecutive_misses); | 94 static void TrimRedirects(RedirectData* data, size_t max_consecutive_misses); |
| 97 | 95 |
| 98 // The maximum length of the string that can be stored in the DB. | 96 // The maximum length of the string that can be stored in the DB. |
| 99 static constexpr size_t kMaxStringLength = 1024; | 97 static constexpr size_t kMaxStringLength = 1024; |
| 100 | 98 |
| 99 protected: |
| 100 // Protected for testing. Use PredictorDatabase::resource_prefetch_tables() |
| 101 // instead of this constructor. |
| 102 ResourcePrefetchPredictorTables(); |
| 103 ~ResourcePrefetchPredictorTables() override; |
| 104 |
| 101 private: | 105 private: |
| 102 // Represents the type of information that is stored in prefetch database. | 106 // Represents the type of information that is stored in prefetch database. |
| 103 enum class PrefetchDataType { RESOURCE, REDIRECT }; | 107 enum class PrefetchDataType { RESOURCE, REDIRECT }; |
| 104 | 108 |
| 105 enum class TableOperationType { INSERT, REMOVE }; | 109 enum class TableOperationType { INSERT, REMOVE }; |
| 106 | 110 |
| 107 friend class PredictorDatabaseInternal; | 111 friend class PredictorDatabaseInternal; |
| 108 friend class MockResourcePrefetchPredictorTables; | |
| 109 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 112 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 110 DatabaseVersionIsSet); | 113 DatabaseVersionIsSet); |
| 111 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 114 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 112 DatabaseIsResetWhenIncompatible); | 115 DatabaseIsResetWhenIncompatible); |
| 113 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | |
| 114 ComputeResourceScore); | |
| 115 | 116 |
| 116 ResourcePrefetchPredictorTables(); | 117 // Database version. Always increment it when any change is made to the data |
| 117 ~ResourcePrefetchPredictorTables() override; | 118 // schema (including the .proto). |
| 119 static constexpr int kDatabaseVersion = 5; |
| 118 | 120 |
| 119 // Helper functions below help perform functions on the Url and host table | 121 // Helper functions below help perform functions on the Url and host table |
| 120 // using the same code. | 122 // using the same code. |
| 121 void GetAllResourceDataHelper(PrefetchKeyType key_type, | 123 void GetAllResourceDataHelper(PrefetchKeyType key_type, |
| 122 PrefetchDataMap* data_map); | 124 PrefetchDataMap* data_map); |
| 123 void GetAllRedirectDataHelper(PrefetchKeyType key_type, | 125 void GetAllRedirectDataHelper(PrefetchKeyType key_type, |
| 124 RedirectDataMap* redirect_map); | 126 RedirectDataMap* redirect_map); |
| 125 bool UpdateDataHelper(PrefetchKeyType key_type, | 127 bool UpdateDataHelper(PrefetchKeyType key_type, |
| 126 PrefetchDataType data_type, | 128 PrefetchDataType data_type, |
| 127 const std::string& key, | 129 const std::string& key, |
| 128 const google::protobuf::MessageLite& data); | 130 const google::protobuf::MessageLite& data); |
| 129 void DeleteDataHelper(PrefetchKeyType key_type, | 131 void DeleteDataHelper(PrefetchKeyType key_type, |
| 130 PrefetchDataType data_type, | 132 PrefetchDataType data_type, |
| 131 const std::vector<std::string>& keys); | 133 const std::vector<std::string>& keys); |
| 132 | 134 |
| 133 // Computes score of |data|. | 135 // PredictorTableBase: |
| 134 static float ComputeResourceScore(const ResourceData& data); | |
| 135 | |
| 136 // PredictorTableBase methods. | |
| 137 void CreateTableIfNonExistent() override; | 136 void CreateTableIfNonExistent() override; |
| 138 void LogDatabaseStats() override; | 137 void LogDatabaseStats() override; |
| 139 | 138 |
| 140 // Database version. Always increment it when any change is made to the data | |
| 141 // schema (including the .proto). | |
| 142 static constexpr int kDatabaseVersion = 5; | |
| 143 | |
| 144 static bool DropTablesIfOutdated(sql::Connection* db); | 139 static bool DropTablesIfOutdated(sql::Connection* db); |
| 145 static int GetDatabaseVersion(sql::Connection* db); | 140 static int GetDatabaseVersion(sql::Connection* db); |
| 146 static bool SetDatabaseVersion(sql::Connection* db, int version); | 141 static bool SetDatabaseVersion(sql::Connection* db, int version); |
| 147 | 142 |
| 148 // Helper to return Statements for cached Statements. | 143 // Helper to return cached Statements. |
| 149 std::unique_ptr<sql::Statement> GetTableUpdateStatement( | 144 std::unique_ptr<sql::Statement> GetTableUpdateStatement( |
| 150 PrefetchKeyType key_type, | 145 PrefetchKeyType key_type, |
| 151 PrefetchDataType data_type, | 146 PrefetchDataType data_type, |
| 152 TableOperationType op_type); | 147 TableOperationType op_type); |
| 153 | 148 |
| 154 static const char* GetTableName(PrefetchKeyType key_type, | 149 static const char* GetTableName(PrefetchKeyType key_type, |
| 155 PrefetchDataType data_type); | 150 PrefetchDataType data_type); |
| 156 | 151 |
| 157 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); | 152 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); |
| 158 }; | 153 }; |
| 159 | 154 |
| 160 } // namespace predictors | 155 } // namespace predictors |
| 161 | 156 |
| 162 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 157 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
| OLD | NEW |